Generalized discrete Bayes with likelihood.
All my code in this chapter hard coded the computation of the likelihood inside the update() function, where it had no business. Also, my treatment of the likelihood was rather hand wavey. By pulling it out of update() and maing it explicit I have created a firm foundation for the rest of the book.
This commit is contained in:
parent
eca26a2422
commit
d0b4a1f4bc
File diff suppressed because one or more lines are too long
@ -35,7 +35,7 @@ if matplotlib.__version__ == '1.4.3':
|
||||
warnings.simplefilter(action="ignore", category=FutureWarning)
|
||||
|
||||
np.set_printoptions(precision=3)
|
||||
sys.path.insert(0, './code') # allow us to import book_format
|
||||
sys.path.insert(0, './code') # allow us to import code
|
||||
|
||||
def test_filterpy_version():
|
||||
|
||||
@ -137,4 +137,3 @@ def load_style(directory = '.', name='code/custom.css'):
|
||||
|
||||
styles = open(os.path.join(directory, name), 'r').read()
|
||||
return HTML(styles)
|
||||
|
||||
|
@ -40,12 +40,38 @@ def show_legend():
|
||||
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
|
||||
|
||||
|
||||
def bar_plot(pos, ylim=(0,1), x=None, title=None):
|
||||
plt.cla()
|
||||
def bar_plot(pos, x=None, ylim=(0,1), title=None, c='#30a2da',
|
||||
**kwargs):
|
||||
""" plot the values in `pos` as a bar plot.
|
||||
|
||||
**Parameters**
|
||||
|
||||
pos : list-like
|
||||
list of values to plot as bars
|
||||
|
||||
x : list-like, optional
|
||||
If provided, specifies the x value for each value in pos. If not
|
||||
provided, the first pos element is plotted at x == 0, the second
|
||||
at 1, etc.
|
||||
|
||||
ylim : (lower, upper), default = (0,1)
|
||||
specifies the lower and upper limits for the y-axis
|
||||
|
||||
title : str, optional
|
||||
If specified, provides a title for the plot
|
||||
|
||||
c : color, default='#30a2da'
|
||||
Color for the bars
|
||||
|
||||
**kwargs : keywords, optional
|
||||
extra keyword arguments passed to ax.bar()
|
||||
|
||||
"""
|
||||
|
||||
ax = plt.gca()
|
||||
if x is None:
|
||||
x = np.arange(len(pos))
|
||||
ax.bar(x, pos, color='#30a2da')
|
||||
ax.bar(x, pos, color=c, **kwargs)
|
||||
if ylim:
|
||||
plt.ylim(ylim)
|
||||
plt.xticks(np.asarray(x)+0.4, x)
|
||||
@ -53,6 +79,33 @@ def bar_plot(pos, ylim=(0,1), x=None, title=None):
|
||||
plt.title(title)
|
||||
|
||||
|
||||
def plot_belief_vs_prior(belief, prior, **kwargs):
|
||||
""" plots two discrete probability distributions side by side, with
|
||||
titles "belief" and "prior"
|
||||
"""
|
||||
|
||||
plt.subplot(121)
|
||||
bar_plot(belief, title='belief', **kwargs)
|
||||
plt.subplot(122)
|
||||
bar_plot(prior, title='prior', **kwargs)
|
||||
|
||||
|
||||
def plot_prior_vs_posterior(prior, posterior, reverse=False, **kwargs):
|
||||
""" plots two discrete probability distributions side by side, with
|
||||
titles "prior" and "posterior"
|
||||
"""
|
||||
if reverse:
|
||||
plt.subplot(121)
|
||||
bar_plot(posterior, title='posterior', **kwargs)
|
||||
plt.subplot(122)
|
||||
bar_plot(prior, title='prior', **kwargs)
|
||||
else:
|
||||
plt.subplot(121)
|
||||
bar_plot(prior, title='prior', **kwargs)
|
||||
plt.subplot(122)
|
||||
bar_plot(posterior, title='posterior', **kwargs)
|
||||
|
||||
|
||||
def set_labels(title=None, x=None, y=None):
|
||||
""" helps make code in book shorter. Optional set title, xlabel and ylabel
|
||||
"""
|
||||
|
@ -2,7 +2,7 @@
|
||||
@import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');
|
||||
@import url('http://fonts.googleapis.com/css?family=Vollkorn');
|
||||
@import url('http://fonts.googleapis.com/css?family=Arimo');
|
||||
@import url('http://fonts.googleapis.com/css?family=Fira_sans');
|
||||
@import url('http://fonts.googleapis.com/css?family=Fira+sans');
|
||||
|
||||
.CodeMirror pre {
|
||||
font-family: 'Source Code Pro', Consolas, monocco, monospace;
|
||||
@ -16,7 +16,7 @@
|
||||
background: transparent;
|
||||
color: #000000;
|
||||
font-weight: 600;
|
||||
font-size: 11pt;
|
||||
font-size: 12pt;
|
||||
font-style: bold;
|
||||
font-family: 'Source Code Pro', Consolas, monocco, monospace;
|
||||
}
|
||||
@ -97,11 +97,10 @@
|
||||
white-space: nowrap;
|
||||
}
|
||||
div.text_cell_render{
|
||||
font-family: 'Fira sans', verdana,arial,sans-serif;
|
||||
/*font-family: 'Vollkorn', verdana,arial,sans-serif;*/
|
||||
line-height: 150%;
|
||||
font-size: 110%;
|
||||
font-weight: 400;
|
||||
text-align:justify;
|
||||
font-size: 130%;
|
||||
text-align: justify;
|
||||
text-justify:inter-word;
|
||||
}
|
||||
div.output_subarea.output_text.output_pyout {
|
||||
|
@ -100,11 +100,13 @@ def show_residual_chart():
|
||||
plt.scatter ([0,1], [158.0,est_y], c='k',s=128)
|
||||
plt.scatter ([1], [164.2], c='b',s=128)
|
||||
plt.scatter ([1], [159], c='r', s=128)
|
||||
plt.text (1.0, 158.8, "prediction ($x_t)$", ha='center',va='top',fontsize=18,color='red')
|
||||
plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
|
||||
plt.text (0, 157.8, "prior estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)
|
||||
plt.text (1.05, 158.8, r"prior $(\bar{x}_t)$", ha='center',va='top',fontsize=18,color='red')
|
||||
plt.text (0.5, 159.6, "prediction", ha='center',va='top',fontsize=18,color='red')
|
||||
plt.text (1.0, 164.4, r"measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
|
||||
plt.text (0, 157.8, r"posterior ($x_{t-1}$)", ha='center', va='top',fontsize=18)
|
||||
plt.text (1.02, est_y-1.5, "residual", ha='left', va='center',fontsize=18)
|
||||
plt.text (0.9, est_y, "new estimate ($\hat{x}_{t}$)", ha='right', va='center',fontsize=18)
|
||||
plt.text (0.9, est_y, "new estimate ($x_t$)", ha='right', va='center',fontsize=18)
|
||||
plt.text (0.8, est_y-0.5, "(posterior)", ha='right', va='center',fontsize=18)
|
||||
plt.xlabel('time')
|
||||
ax.yaxis.set_label_position("right")
|
||||
plt.ylabel('state')
|
||||
|
Loading…
Reference in New Issue
Block a user