Skip to content

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.

Introductory ~11 min9 steps

What runs in CodeSplash

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

01

Beliefs as distributions

Read

A 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..

p(θ∣data)⏟posterior∝p(data∣θ)⏟likelihood×p(θ)⏟prior\underbrace{p(\theta \mid \text{data})}_{\text{posterior}} \propto \underbrace{p(\text{data} \mid \theta)}_{\text{likelihood}} \times \underbrace{p(\theta)}_{\text{prior}}

Values that predicted the data well gain weight. Values that predicted it badly lose it. That's all of Bayes' rule.

02

Predict an update

Predict

Predict 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?

Near 50%Near 70%Exactly 100%
Reveal

Near 70%. With a flat prior, the posterior follows the likelihood, whose peak is at 7/107/10. It is still broad: 10 trials leave many rates plausible.

03

Move the prior

Explore

EExplorable · From prior to posterior

How does evidence update what you believed, and how much does the prior matter?

What is the posterior mean?

0.50
How sure?
2.0
2.0
14
20
2.5%97.5%

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 first; the figure appears when you commit.

Make your prediction to see the result.

Try these in turn:

  1. Set a flat prior, with both Prior successes and Prior failures at 1. Add some data.
  2. Keep the data and raise the prior counts, so that the prior disagrees with the data.
  3. Multiply both the successes and the trials by ten.

When does the prior stop mattering much?

04

An update you can do by hand

Read

The Beta distribution makes this exact. A Beta(a,b)\text{Beta}(a, b) prior acts like aa earlier successes and bb earlier failures. After xx successes in nn trials:

θ∣x∼Beta(a+x, b+n−x)\theta \mid x \sim \text{Beta}(a + x,\ b + n - x)

Starting from Beta(1,1)\text{Beta}(1, 1), 7 successes in 10 trials give Beta(8,4)\text{Beta}(8, 4). Its mean is 8/128 / 12.

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.

05

What a credible interval says

Predict

Predict first

A 95% credible interval for a rate runs from 40% to 85%. What is the literal meaning of that?

There is a 95% probability that θ lies in the interval, given the model and prior95% of intervals built this way would contain θBoth, since they are the same claim
Reveal

The first. A credible interval is a statement about θ\theta, 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.

06

Which prior would you defend?

Reflect

Priors 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.

07

Check your understanding

Checkpoint

Check yourself

Your prior is Beta(2,2)\text{Beta}(2, 2) and you see 10 successes in 15 trials. The posterior is Beta(a,b)\text{Beta}(a, b). What is aa?

Show the answer

Answer: 12

a=2+10=12a = 2 + 10 = 12, and b=2+5=7b = 2 + 5 = 7.

Check yourself

A posterior is Beta(15,5)\text{Beta}(15, 5). What is its mean?

Show the answer

Answer: 0.75

15/(15+5)=0.7515 / (15 + 5) = 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.

08

See it in code

In the app

Bayesian tests live in Analyze. The next lessons use the Bayesian t test.

Python
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]))
R
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.

  1. The posterior mean

    Store the mean of the posterior distribution of the reply rate in post_mean.

    Show a solution
    Python
    post_mean = (a0 + successes) / (a0 + b0 + trials)
    R
    post_mean <- (a0 + successes) / (a0 + b0 + trials)
  2. 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
    Python
    ci = stats.beta.ppf([0.025, 0.975], a0 + successes, b0 + trials - successes)
    R
    ci <- 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.

09

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