# model specifications in PyMC3
# are wrapped in a with statement
# define priors
alpha = pm.Normal(‘alpha’, mu= 0 , sd= 20 )
beta = pm.Normal(‘beta’, mu= 0 , sd= 20 )
sigma = pm.Uniform(‘sigma’, lower= 0 , upper= 10 )
# define linear regression
y_est = alpha + beta * x
# define likelihood
likelihood = pm.Normal(‘y’, mu=y_est, sd=sigma, observed=y)
# inference
start = pm.find_MAP()
# find starting value by optimization
step = pm.NUTS(state=start)
# instantiate MCMC sampling algorithm
trace = pm.sample( 100 , step, start=start, progressbar=False)
# draw 100 posterior samples using NUTS sampling
Have a look at the estimates from the first sample:
In [ 28 ]: trace[ 0 ]
Out[28]: {‘alpha’: 3.8783781152509031,
‘beta’: 2.0148472296530033,
‘sigma’: 2.0078134493352975}
All three values are rather close to the original values (4, 2, 2). However, the whole
procedure yields, of course, many more estimates. They are best illustrated with the help
of a trace plot, as in Figure 11-20 — i.e., a plot showing the resulting posterior
distribution for the different parameters as well as all single estimates per sample. The
posterior distribution gives us an intuitive sense about the uncertainty in our estimates:
In [ 29 ]: fig = pm.traceplot(trace, lines={‘alpha’: 4 , ‘beta’: 2 , ‘sigma’: 2 })
plt.figure(figsize=( 8 , 8 ))
Figure 11-20. Trace plots for alpha, beta, and sigma
Taking only the alpha and beta values from the regression, we can draw all resulting
regression lines as shown in Figure 11-21: