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.
▶ Open the simulator (simple view)▶ Open the detailed view
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.
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).
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).
| Words on the screen | Term | Symbol or formula |
|---|---|---|
| Horizontal axis: chance of heads | Success rate (the parameter of the Bernoulli distribution) | μ |
| Starting belief (your guess before tossing) | Prior distribution | Beta(μ | a, b) |
| Know nothing / Probably fair / Surely fair | Uniform 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 hill | Posterior distribution | Beta(μ | a + m, b + l) |
| Previous hill | The previous posterior (= the prior for the next observation) | Beta(μ | a + m′, b + l′) before the button is pressed |
| Top of the hill | MAP estimate | (a + m − 1)/(a + b + N − 2) |
| Guess (▲) / Guess: chance of heads on the next toss | Posterior mean = posterior predictive probability | (a + m)/(a + b + N) = p(x = 1 | 𝒟) |
| Likely range (95 %) / Shaded | 95 % 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 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).
▶ Simulator (simple view) A: Does this coin land heads more often?
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):
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.
| Words on the screen | Term | Symbol or formula |
|---|---|---|
| True weight | The 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 record | Observed value | xn = μtrue + σ εn |
| Guess of the apple’s weight (▲) | Mean of the posterior | μN |
| Width of the guess (95 %) | Half-width of the 95 % credible interval | 1.96 σN |
| Likely range of the next reading (95 %) (hatched) / Width of the next reading | 95 % 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 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.
▶ Simulator (simple view) B: If you weigh many times, do you get closer to the true weight?
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.
Bayes’ theorem is applied to the success rate μ (viewed as a function of μ, the evidence p(𝒟) in the denominator is a normalizing constant).
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).
The four point estimates and the prediction shown on screen:
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.
For the mean of a Gaussian in ② (noise variance σ2 known, prior 𝒩(μ | μ0, σ02)), completing the square gives
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.
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σ.
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).
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.
▶ Simulator (detailed view) ①: Beta–Bernoulli (success rate)▶ Simulator (detailed view) ②: Mean of a Gaussian
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 →.
Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.
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.
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.
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.
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.
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).
σ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.
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).