Explanation of the Bayesian Update Simulator — What the Guess Hill Computes

This page explains what the “guess hill” seen in the simulator corresponds to in the language of probability and which formulas draw it, in the order words → formulas → calculation procedure. The procedure for the detailed view, the conditions of the calculation, and questions for checking are also collected here.

1. About this page

The Bayesian update simulator is a tool for seeing how a “guess before seeing data” changes each time one piece of data is seen. It treats the Beta–Bernoulli model of Chapter 4 (the basics of Bayesian estimation) and the estimation of the mean of a Gaussian distribution with known variance.

The simulator has two views.

The two scenes of the simple view do the same calculations as ① and ② of the detailed view. We recommend trying the simple view first, then checking what it really shows in Section 2, reading the formulas in Section 3, and operating the detailed view with the procedure in Section 4.

2. What the simple view really shows

The “guess hill” in the simple view is the probability density curve of the posterior distribution (the probability distribution of the quantity we want to know, after seeing the data). The horizontal axis is the quantity we want to know (the chance that the coin lands heads, the weight of the apple), and the vertical axis is how likely each value is (the probability density). The total area of the hill is 1, and the area under the hill over a range is “the probability that the true value lies in that range.” So the higher the hill at a value, the more likely that value is, and the narrower the hill, the more the likely values gather in a narrow range (the more confident we are). The tick marks on the vertical axis are left out in the simple view (the figures in the detailed view have them).

2.1 Scene A: Does this coin land heads more often?

A coin whose true chance of heads μ is unknown is tossed to estimate μ. Each toss is either “heads (success, x = 1)” or “tails (failure, x = 0),” and the hill after seeing m heads and l tails (N = m + l tosses) is the posterior Beta(μ | a + m, b + l) (Section 3.2).

Table 1: Words on the screen in Scene A and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Horizontal axis: chance of headsSuccess rate (the parameter of the Bernoulli distribution)μ
Starting belief (your guess before tossing)Prior distributionBeta(μ | a, b)
Know nothing / Probably fair / Surely fairUniform prior / weak prior / strong prior(a, b) = (1, 1), (2, 2), (20, 20)
Coin results (the row of H and T)Observed data𝒟; m heads, l tails
Current guess hillPosterior distributionBeta(μ | a + m, b + l)
Previous hillThe previous posterior (= the prior for the next observation)Beta(μ | a + m′, b + l′) before the button is pressed
Top of the hillMAP estimate(a + m − 1)/(a + b + N − 2)
Guess (▲) / Guess: chance of heads on the next tossPosterior mean = posterior predictive probability(a + m)/(a + b + N) = p(x = 1 | 𝒟)
Likely range (95 %) / Shaded95 % credible interval (equal-tailed: what remains after removing 2.5 % from each end)From the 2.5 % point to the 97.5 % point of the posterior
True chance (Check the answer)The true value used to make the synthetic dataμtrue

The “Guess” number is not the top of the hill but the center of mass of the hill (the posterior mean). On the screen this is rephrased as “If you cut the hill out of cardboard, this is where it balances left and right,” and it is shown by ▲ on the horizontal axis. The posterior mean is exactly “the probability that the next toss is heads” (Section 3.2). For example, if heads comes up 3 times in a row starting from knowing nothing, the top of the hill is at the right end μ = 1, but the guess is (1 + 3)/(1 + 1 + 3) = 0.80. This is because area remains on the left side of the hill (μ < 1). The likely range at this point is 0.40–0.99.

The “Likely range (95 %)” is what remains after cutting off 2.5 % of the area of the hill from the left end and from the right end (the equal-tailed 95 % credible interval). It is not the range that collects 95 % of the area starting from the highest part of the hill (the highest-density interval). Therefore, when the top is at an end, as for Beta(4, 1), the area right next to the top (to the right of 0.99) is also outside the range. The range means “the probability that the true value is inside is 95 %,” and it is set separately from which values have the highest hill.

Numbers in “Try it” (all computed for coin 1, the coin shown when the page opens):

Where the analogy fits and where it does not

Where it fits: the heads and tails of the coin are computed with the same formulas as ① of the detailed view, as independent Bernoulli trials with the same probability μ. The sequence of results for coin 1 is the same sequence you get by pressing “Draw 1 from true μ” with the defaults of ① in the detailed view (true μ = 0.70, seed 20261112).

Where it does not fit: the chance of heads of a real coin is almost exactly 0.5, but the coins in this scene are synthetic data for teaching purposes, made with chances of heads set to 0.70, 0.35, 0.55, 0.85, 0.20, and 0.50. Also, the three cards for the “Starting belief” choose the shape of a Beta distribution. A person’s belief cannot always be expressed by a Beta distribution. “Three heads in a row?” is not the result of actually tossing the coin but the case where three observations of heads are entered by hand (the same as pressing “Add 1 success” three times in the detailed view).

2.2 Scene B: If you weigh many times, do you get closer to the true weight?

One apple is weighed many times on a scale whose reading wobbles, to estimate its true weight. The reading of the scale is “true weight + wobble”; the wobble follows a Gaussian distribution with mean 0, and its size (the standard deviation σ) is assumed known. The hill after N weighings is the posterior 𝒩(μ | μN, σN2) (Section 3.3).

The calculation uses the same dimensionless quantity μ as ② of the detailed view, and the screen shows it converted to weight (g):

weight (g)=300+10×μ

Because this conversion is linear, only the scale of the horizontal axis changes; the shape of the hill does not. The prior μ0 = 0, σ0 = 2 corresponds to “a typical apple is about 300 g (standard deviation 20 g),” and the wobble of the scale σ = 0.5, 1, 2 corresponds to 5 g, 10 g, 20 g.

Table 2: Words on the screen in Scene B and the corresponding terms and symbols
Words on the screenTermSymbol or formula
True weightThe mean to be estimatedμ (shown on the screen as 300 + 10μ g)
Belief before weighing (about 300 g)Prior distribution𝒩(μ | μ0, σ02), μ0 = 0, σ0 = 2
Type of scale (wobble about 5 / 10 / 20 g)Noise standard deviation (known)σ = 0.5, 1, 2
Scale reading / Dots in the recordObserved valuexn = μtrue + σ εn
Guess of the apple’s weight (▲)Mean of the posteriorμN
Width of the guess (95 %)Half-width of the 95 % credible interval1.96 σN
Likely range of the next reading (95 %) (hatched) / Width of the next reading95 % interval of the posterior predictive distributionμN ± 1.96 √(σ2 + σN2)
True weight (Check the answer)The true value used to make the synthetic dataμtrue

Numbers in “Try it” (apple 1, wobble “Normal”): the first reading is 301.6 g, the guess for the weight is 301.3 g, and the width is ±17.5 g (the screen rounds values of 10 g or more to whole numbers, so it shows 302 g, 301 g, and ±18 g). After 20 weighings the guess is 314.0 g and the width is ±4.4 g (the screen shows 314 g and ±4.4 g). At this point the width of the next single reading is ±20.1 g (±20 g on the screen), which does not become smaller than the ±19.6 g (1.96 × 10 g) set by the wobble σ alone.

Where the analogy fits and where it does not

Where it fits: the calculation uses the same model as ② of the detailed view, reading = true weight + independent Gaussian wobble. The sequence of readings for apple 1 with wobble “Normal” is the same sequence you get by pressing “Observe 1” with the defaults of ② in the detailed view (true μ = 1.5, σ = 1, seed 20261112), converted to g.

Where it does not fit: with a real scale, the size of the wobble may be unknown, the reading may be rounded, or there may be an offset (bias). None of these happen in this scene. The apple weights and the readings are synthetic data for teaching purposes. The wobbling of the reading at each weighing (the motion in which the number wobbles and then stops) is only an effect in the picture and is not used in the calculation.

3. How it works

3.1 Redrawing the guess with Bayes’ theorem

When the quantity we want to know (here a success rate or a mean μ) is unknown, Bayesian estimation expresses μ itself by a probability distribution. The distribution before seeing data is called the prior distribution p(μ), the probability of obtaining the data 𝒟 viewed as a function of μ is called the likelihood p(𝒟 | μ), and the distribution after seeing data is called the posterior distribution p(μ | 𝒟). The three are connected by Bayes’ theorem; viewed as a function of μ, “posterior ∝ likelihood × prior” (Section 4.3 of the text). The hill is redrawn in the simple view because this multiplication and normalization (making the area 1) are done for each observation.

3.2 Estimating a success rate (Beta–Bernoulli model)

Bayes’ theorem is applied to the success rate μ (viewed as a function of μ, the evidence p(𝒟) in the denominator is a normalizing constant).

p(μ∣𝒟)=p(𝒟∣μ)p(μ)p(𝒟)∝μm(1−μ)l×μa−1(1−μ)b−1

If the prior is Beta(μ | a, b) and m successes and l failures (N = m + l) are observed, the posterior is again a Beta distribution (conjugacy).

p(μ∣𝒟)=Beta(μ∣a+m,b+l),Beta(μ∣a,b)=Γ(a+b)Γ(a)Γ(b)μa−1(1−μ)b−1

The four point estimates and the prediction shown on screen:

μML=mN,μMAP=a+m−1a+b+N−2(when a+m>1, b+l>1)𝔼[μ∣𝒟]=p(x=1∣𝒟)=∫01μp(μ∣𝒟)dμ=a+ma+b+Na+ma+b+N=wmN+(1−w)aa+b,w=Na+b+N

The 95 % credible interval runs from the 2.5 % point to the 97.5 % point of the posterior (an equal-tailed interval). The regularized incomplete beta function Ix(a, b) is computed by a continued fraction, and its inverse is found with Newton’s method and bisection.

When updating one observation at a time, the previous posterior is used as the next prior. A success just adds 1 to a and a failure adds 1 to b, so the result is the same as the batch result regardless of order. For the numerical check on screen, ln μ or ln(1 − μ) is added one observation at a time to the log density on a grid of μ (2001 points), and the finally normalized density is compared with the analytical batch solution.

3.3 Estimating a mean (Gaussian distribution, known variance)

For the mean of a Gaussian in ② (noise variance σ2 known, prior 𝒩(μ | μ0, σ02)), completing the square gives

1σN2=1σ02+Nσ2,μN=σN2(μ0σ02+Nx¯σ2)p(x∣𝒟)=𝒩(x∣μN,σ2+σN2)

Of the predictive variance, σ2 comes from noise (aleatoric uncertainty) and σN2 comes from not knowing μ (epistemic uncertainty).

Read in words, this formula says two things (Section 4.6 of the text). First, the reciprocals of the variances (the precisions) add. Each added observation increases the posterior precision by 1/σ2, so the hill only gets narrower. When N is large, σN2 ≈ σ2/N (Section 4.8 of the text). Second, the posterior mean μN is the average of the prior mean μ0 and the sample mean x̄, weighted by their precisions.

μN=wx¯+(1−w)μ0,w=Nσ02Nσ02+σ2

The “Likely range of the next reading (95 %)” in Scene B of the simple view is the 95 % interval of the posterior predictive distribution 𝒩(x | μN, σ2 + σN2). Only σN2 decreases as observations are added, so the half-width of the interval never becomes smaller than 1.96σ.

3.4 Calculation procedure

  1. Set the parameters of the prior (① and A: a, b; ② and B: μ0, σ0, σ).
  2. Add one observation. For random observations in A and ①, the toss is a success (heads) if a uniform random number u is smaller than μtrue. In B and ②, x = μtrue + σ ε (ε is a standard Gaussian random number from the Box–Muller method).
  3. ① and A: count the successes and failures m, l, and find the mean, MAP, and credible interval (the inverse of the regularized incomplete beta function) of Beta(a + m, b + l). ② and B: find σN2 from the sum of precisions, μN from the weighted average, and the predictive variance σ2 + σN2.
  4. Compute the posterior density on a grid and draw the curve (301 points for A, 401 points for B, and 501 and 801 points in the detailed view). The previous hill is the posterior before the button was pressed, drawn with the same formula.

The calculations of the simple view call the same functions as the detailed view (betaSummary, gaussPosterior, drawBernoulli, and drawGauss in bayes-update.model.js). The only calculations added for the simple view are the conversion to g, the grid for the hill curves, and the stacking of the dots in the record (bayes-update.easy.js).

4. How to use the detailed view

The detailed view has two tabs, ① (Beta–Bernoulli) and ② (Mean of a Gaussian). Both have the figures and the “Putting the current numbers into the formula” box on the left, and the controls and the readout on the right.

4.1 Procedure and what to observe

  1. In ①, set the “Prior type” to “Uniform Beta(1,1)” and press “Add 1 success” three times. What to look for: the difference among the maximum-likelihood estimate m/N = 1, MAP = 1, and the posterior mean = 4/5 = 0.8. MAP is the point of maximum posterior density, whereas the posterior mean is the center of mass of the distribution, so for Beta(4, 1), which keeps some probability on the side μ < 1, only the posterior mean falls below 1. In the formulas, MAP (a+m−1)/(a+b+N−2) adds a − 1 and b − 1 (0 pseudo-counts for the uniform prior), while the posterior mean (a+m)/(a+b+N) adds a and b (1 success and 1 failure for the uniform prior; Laplace’s rule of succession).
  2. Press “Add 1 failure,” then cancel it with “Undo last.” What to look for: a single failure makes the posterior density at the right end (μ = 1) equal to 0 and lowers MAP from 1 to 0.75.
  3. After “Clear data,” set the prior to “Strong Beta(20,20)” and press “Draw 10” several times. What to look for: the maximum-likelihood estimate moves toward the true μ (default 0.70), while the posterior mean is pulled back toward 0.5. In the lower figure, as N grows the posterior mean approaches the maximum-likelihood estimate and the credible interval narrows (the data weight w = N/(a+b+N) approaches 1).
  4. Press “Compare sequential and batch updates.” What to look for: the last entry of the parameter sequence updated one observation at a time agrees with the batch Beta(a+m, b+l). The density obtained by applying Bayes’ theorem numerically one observation at a time on a grid also agrees with the analytical batch posterior to within rounding error.
  5. In ②, press “Observe 10” several times. What to look for: the posterior becomes narrower (σN decreases), but the width of the predictive distribution never becomes narrower than σ. In the lower figure and the breakdown bar, only σN2 decreases.
  6. In ②, reduce the prior standard deviation σ0 to 0.1 and move the prior mean μ0 away from the true value. What to look for: a strong prior (small σ0) holds the posterior mean near μ0 while there are few data.

With the keyboard, Tab moves to buttons and sliders, Enter (or Space) presses a button, and the arrow keys move a slider. The tabs can be switched with ← and →.

5. Conditions of the calculation

5.1 Data and random-number conditions

5.2 Conditions fixed in the simple view

5.3 Simplifications

5.4 Conditions under which results become unstable

6. Questions for checking

Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.

  1. In Scene A of the simple view, when heads comes up 3 times in a row with “Know nothing,” are the top of the hill and the “Guess” number at the same place? If not, why?
    Sample answer

    No. The top of the hill (the MAP estimate) is at the right end, 1, but the guess (the posterior mean) is 0.80. The guess is the center of mass of the whole hill, and since area remains on the left side of the hill (where the chance of heads is smaller than 1), it is smaller than 1. This 0.80 is also the probability that the next toss is heads.

  2. In Scene B of the simple view, after 20 weighings, which is wider, the “Width of the guess” or the “Width of the next reading”? What happens to each if you weigh even more times?
    Sample answer

    The width of the next reading (about ±20 g) is much wider than the width of the guess (about ±4 g). Weighing more makes the width of the guess even narrower, but the width of the next reading never becomes narrower than about ±20 g (for wobble “Normal”), which is set by the wobble of the scale alone. Only the part from not knowing the true weight (σN2) can be reduced; the wobble of the scale (σ2) remains.

  1. Under the uniform prior Beta(1,1), 3 successes were observed in 3 trials. What are the maximum-likelihood estimate, MAP, and posterior mean? Why does only the posterior mean differ?
    Sample answer

    Maximum likelihood 3/3 = 1; MAP (1+3−1)/(1+1+3−2) = 3/3 = 1; posterior mean (1+3)/(1+1+3) = 4/5 = 0.8. Under a uniform prior the prior density is constant, so the point of maximum posterior density (MAP) coincides with the point of maximum likelihood. The posterior mean is the center of mass of the whole distribution; since probability remains on the side μ < 1, it is smaller than 1. It is also the probability p(x=1 | 𝒟) that the next trial succeeds.

  2. With the priors Beta(20,20) and Beta(1,1), the same observations (7 successes, 3 failures) are entered. What is the posterior mean in each case? What happens if the observations are multiplied by 100 (700 successes, 300 failures)?
    Sample answer

    N = 10: 27/50 = 0.54 for Beta(20,20) and 8/12 ≈ 0.667 for Beta(1,1). N = 1000: 720/1040 ≈ 0.692 and 701/1002 ≈ 0.700 (the maximum-likelihood estimate is 0.7 in both cases). The data weight w = N/(a+b+N) rises from 10/50 = 0.2 to 1000/1040 ≈ 0.96, and the influence of the prior fades.

  3. Why do the result of updating one observation at a time and the result of updating all at once agree? What happens if the order of observations is changed?
    Sample answer

    If the observations are independent, the likelihood is the product of the individual likelihoods, and multiplying the posterior after k observations by the likelihood of the (k+1)-th and normalizing gives the same result as normalizing the full product. In the Beta–Bernoulli model a success just adds 1 to a and a failure adds 1 to b, so even if the order is changed the final result is Beta(a+m, b+l) (the intermediate steps differ).

  4. In ② with σ = 1 and σ0 = 1, for N = 0, 1, 9, and 99, what are σN2 and the predictive variance? However many observations are added, below what value can the predictive variance never fall?
    Sample answer

    σN2 = 1/(1+N) , so 1, 0.5, 0.1, 0.01. The predictive variance 1 + σN2 is 2, 1.5, 1.1, 1.01, and never falls below the noise variance σ2 = 1. Only the component that comes from lack of knowledge can be reduced by adding data.

7. Corresponding sections of the text and references

References (from the end of Chapter 4):

Back to the Bayesian Update Simulator | Machine Learning — From Probability to GPT (Contents) | Equations are typeset with the browser’s built-in MathML (no external library).