Bayesian Monte Carlo and MCMCLesson 70 of 76 · 9 min

Sample a posterior directly

Use independent posterior draws to estimate a posterior probability with a numerical error check.

Once a posterior distribution is available, Monte Carlo can estimate a probability or expectation under it. Direct beta draws are independent in this example, so the ordinary independent-sample standard error applies to their average or event indicator.

The question is P(p above 0.6 given the invented observations and prior), not the probability of more than 60 percent successes in a future group. Each draw is one possible parameter value under the posterior. Averaging its threshold indicator estimates the posterior probability of that event.

SciPy supplies an exact beta survival function for comparison. This benchmark separates numerical approximation error from posterior uncertainty about p. Raising the draw count improves numerical precision; it does not add observations or narrow the exact posterior distribution.

Posterior uncertainty versus MC error
  1. Independent posterior draws
    pi∼Beta⁡(20,14)p_i\sim\operatorname{Beta}(20,14)
  2. Posterior event probability
    r^=mean⁡(pi>0.6)\widehat r=\operatorname{mean}(p_i>0.6)
  3. Numerical standard error
    MCSE^=sindicator/n\widehat{\mathrm{MCSE}}=s_{\rm indicator}/\sqrt n
Reference illustration. The posterior SD describes uncertainty in p under the chosen prior and likelihood. MCSE describes error in the estimated posterior event probability; more draws reduce numerical error, not posterior uncertainty.

Does increasing posterior draws from 1,000 to 100,000 supply more observed evidence?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Posterior P(p > .6), Monte Carlo: 0.45405
Independent-draw MCSE: 0.00352
Exact posterior probability: 0.45296
Posterior outcome SD of p: 0.08286

Read the first result

Posterior SD describes uncertainty about p; MCSE describes numerical error in the estimated posterior event probability. Both are dimensionless, but their target quantities and interpretations differ.

Try an edit

Replace event = p > .6 with event = p > .7 and Posterior P(p > .6), Monte Carlo: with Posterior P(p > .7), Monte Carlo: and stats.beta.sf(.6,a,b) with stats.beta.sf(.7,a,b). Select Run Python again. Compare the new Monte Carlo event estimate with Exact posterior probability and the size of Independent-draw MCSE. Does Posterior outcome SD of p change?

Back to the Python editor

Scope. Independent beta sampling is a convenient benchmark. Correlated Markov-chain draws need different numerical error diagnostics.

Try a fresh case

A posterior mean estimate becomes numerically very precise. What follows?

Choose an answer
Need a hint?
Hint 1

Separate the distribution being sampled from its numerical approximation.

Hint 2

Inspect the posterior SD as well as MCSE.

Your notes