From prior to posterior
Update a belief with data, see how much the prior matters as data accumulate, and read a credible interval for what it says.
What runs in CodeSplash
- Bayesian t-test (JZS Bayes factor) Verified in Python and R
You will be able to
- Describe the prior, the likelihood and the posterior in plain words
- Update a Beta prior with binomial data by hand
- Say how a credible interval differs from a confidence interval
Read first
Beliefs as distributions
ReadA conversion rate, a mean difference, a correlation: you don't know the true value, but you may know something about it.
In Bayesian inference, you describe what you know with a probability distribution over the possible values. Before the data, that is the priorPriorWhat you believe about a parameter before seeing the data, written as a probability distribution.. After, it's the posteriorPosteriorYour belief about a parameter after combining the prior with the data..
Values that predicted the data well gain weight. Values that predicted it badly lose it. That's all of Bayes' rule.
Predict an update
PredictPredict first
You have no idea what a success rate might be, so you give every value from 0% to 100% equal weight. Then you see 7 successes in 10 trials.
Where is the posterior centered now?
Reveal
Near 70%. With a flat prior, the posterior follows the likelihood, whose peak is at . It is still broad: 10 trials leave many rates plausible.
Move the prior
ExploreEExplorable · From prior to posterior
How does evidence update what you believed, and how much does the prior matter?
What is the posterior mean?
Dashed: prior. Second line: what the data alone say. Solid: posterior, a compromise weighted 83% toward the data.
Commit your prediction above to see the result
Make your prediction to see the result.
Try these in turn:
- Set a flat prior, with both Prior successes and Prior failures at 1. Add some data.
- Keep the data and raise the prior counts, so that the prior disagrees with the data.
- Multiply both the successes and the trials by ten.
When does the prior stop mattering much?
An update you can do by hand
ReadThe Beta distribution makes this exact. A prior acts like earlier successes and earlier failures. After successes in trials:
Starting from , 7 successes in 10 trials give . Its mean is .
Conjugate pairs like this are rare in real models. The tests in this track use other priors and numerical integration, but the logic is the same.
What a credible interval says
PredictPredict first
A 95% credible interval for a rate runs from 40% to 85%. What is the literal meaning of that?
Reveal
The first. A credible interval is a statement about , conditional on the model and the prior. A confidence interval is a statement about the procedure over repeated samples. With flat priors and large samples they often nearly coincide, but they answer different questions.
Which prior would you defend?
ReflectPriors can come from earlier studies, from what's physically possible, or from a default chosen to be weakly informative.
Pick a rate you care about, such as a conversion or a defect rate. What range would you find plausible before seeing data? Would a colleague accept it? If not, you can run the analysis under both priors and report the two.
Check your understanding
CheckpointCheck yourself
Your prior is and you see 10 successes in 15 trials. The posterior is . What is ?
Show the answer
Answer: 12
, and .
Check yourself
A posterior is . What is its mean?
Show the answer
Answer: 0.75
.
Check yourself
Two analysts use different reasonable priors on the same large dataset. What should you expect?
Show the answer
Answer: b
With enough data, the likelihood swamps any reasonable prior. Priors matter most with little data, and for Bayes factors, as you'll see.
See it in code
In the appBayesian tests live in Analyze. The next lessons use the Bayesian t test.
from scipy import stats
a, b = 1, 1 # flat prior
x, n = 7, 10 # data
post = stats.beta(a + x, b + n - x)
print(post.mean(), post.ppf([0.025, 0.975]))a <- 1; b <- 1 # flat prior
x <- 7; n <- 10 # data
(a + x) / (a + b + n)
qbeta(c(0.025, 0.975), a + x, b + n - x)Notebook exercise · ~8 min
A beta-binomial update in code
Update a Beta prior with 7 successes in 20 trials, then read the posterior mean and a 95% credible interval.
- The posterior mean
Store the mean of the posterior distribution of the reply rate in
post_mean.Show a solution
Pythonpost_mean = (a0 + successes) / (a0 + b0 + trials)Rpost_mean <- (a0 + successes) / (a0 + b0 + trials) - A 95% credible interval
Store the equal-tailed 95% credible interval for the reply rate as two numbers, lower then upper, in
ci.Show a solution
Pythonci = stats.beta.ppf([0.025, 0.975], a0 + successes, b0 + trials - successes)Rci <- qbeta(c(0.025, 0.975), a0 + successes, b0 + trials - successes)
In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.
Recap
Recap- Prior × likelihood gives the posterior: values that predicted the data gain weight.
- A prior counts as earlier data, so it matters most when you have little of your own.
- A credible interval is a probability statement about the value, given the model and prior.
Concepts in this lesson