Statistical Inference for EveryoneXYZ Homework Edition

⇩ Download ▾

11.2 One-Dimensional Models

The model we first look at is the coin flip model: given 17 heads in 25 flips, what is the probability distribution of the the measure of the coin's bent-ness, θ\theta. We know the solution is of the form of a beta distribution, but we perform the same analysis with the MCMC technique.

h,N=data=17,25
def P_data(data,theta):
    h,N=data
    distribution=Bernoulli(h,N)
    return distribution(theta)
model=MCMCModel(data,P_data,
                theta=Uniform(0,1))
About a dozen overlaid MCMC chains for θ plotted against time step, 0 to 500. The traces start spread across the whole 0-1 range, then within roughly 25 steps settle into a dense band between about 0.4 and 0.9 centered near 0.7.
Figure 11.1. So-called MCMC “chains” for parameter θ\theta versus time. Observe that the values of θ\theta start spread evenly from 0 to 1 at the beginning and then thin down to a range of about 0.5-0.8 with the middle around 0.7 (17/25 = 0.68).
model.run_mcmc(500)
model.plot_chains()

Reading the Output

We can now plot the distributions of the parameters, just θ\theta in this case, yielding best-fits, uncertainties, etc...

model.plot_distributions()

(see Figure 11.2)

Jagged sampled histogram of the posterior for θ (blue dots) with two smooth fitted curves over it, titled with the estimate θ=0.669 (+0.157/-0.187 for the 2.5-97.5 percentile range). The hump peaks near θ=0.67.
Figure 11.2. Distribution of θ\theta, and the 95% credible interval.

We can further perform some simple calculations on the probabilities for the parameters, such as

model.P('theta>0.5')
Show expected output
0.96173333333333333
model.P('(0.2<theta) & (theta<.5)')
Show expected output
0.038266666666666664

Adapted from Statistical Inference for Everyone, by Brian Blais (Bryant University), licensed under CC BY-SA 4.0 (dual-licensed under the GNU FDL 1.2 or later; this adaptation uses the CC BY-SA grant). Changes were made; this adaptation is distributed under the same license. License: CC-BY-SA-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.