Mathematical Foundations

V.10 · Statistics

How does a prior move the posterior mode?

Combine a transparent Beta prior with actual Bernoulli data, normalize the posterior analytically, and compare MAP with MLE while exposing every boundary and nonunique mode.

Update a prior; inspect the posterior mode

Actual fixed sequence: [1, 0, 1, 1, 0]. Successes: 3; failures: 2.

Next outcome 1= 0.6
Next outcome 0= 0.4
Likelihood over candidate parameters
Likelihood over candidate parametersObserved Bernoulli data: 1, 0, 1, 1, 0. 3 successes, 2 failures. Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.0.03801600.51

θ · solid curve is actual ordered-data likelihood · dot is the candidate · dashed marker is MLE

Log-likelihood ≈ -3.36506

Prior and data likelihood
Prior and data likelihoodPrior ≈ 1.44; likelihood 0.03456. Divergent endpoints are omitted from the finite drawing grid.1.500.51

Solid: normalized prior density · dashed: likelihood, which is not normalized over θ

Posterior normalization
Posterior normalizationPosterior ≈ 2.32243, unnormalized posterior ≈ 0.0497664. Interior MAP: 4/7.2.3499700.51

Solid: normalized Beta posterior density · dashed: prior × likelihood · divergent endpoints are limit cases

Prior density ≈ 1.44

Prior × likelihood ≈ 0.0497664

Normalized posterior density ≈ 2.32243

Interior MAP: 4/7.

Beta density is defined on (0,1); endpoint values are interpreted by limits. Divergent endpoints are excluded from the 0.005 plotting grid and stated explicitly; the plotted curve is never renormalized. A divergent endpoint density does not create a probability atom.

Observed Bernoulli data: 1, 0, 1, 1, 0. 3 successes, 2 failures. Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.

Ordered-data likelihood
0.03456
Log-likelihood (natural log)
−3.365058
Maximal data likelihood
0.03456
Log-likelihood gap to optimum
0
Data-derived maximizing parameter
0.6
Posterior mode (stated endpoint convention)
4/70.571429…
Posterior alpha
5
Posterior beta
4
Prior density
1.44
Unnormalized posterior density
0.049766
Normalized posterior density
2.322432
Model evidence / normalizing factor
0.021429
Posterior mean (distinct from MAP)
5/90.555556…

= exact finite-data/model quantity · ≈ binary64 approximation; empirical results describe this run

from 0 to 1, up to 2 decimal places

from 0.5 to 5, up to 1 decimal place

from 0.5 to 5, up to 1 decimal place

Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.

L(θ)=θ3(1−θ)2L(\theta)=\theta^{3}(1-\theta)^{2}
ℓ(θ)=3log⁡θ+2log⁡(1−θ)≈−3.36506\ell(\theta)=3\log\theta+2\log(1-\theta)\approx-3.36506
p(θ∣D)∝θ4(1−θ)3p(\theta\mid D)\propto\theta^{4}(1-\theta)^{3}
θMAP=a−1a+b−2=47\theta_{\mathrm{MAP}}=\frac{a-1}{a+b-2}=\frac{4}{7}

Keep observed data fixed

Observed sequence: 1, 0, 1, 1, 0. The ordered-sequence model has no binomial count coefficient.

L(θ)=θ3(1−θ)2L(\theta)=\theta^{3}(1-\theta)^{2}

Combine a transparent Beta prior with actual Bernoulli data, normalize the posterior analytically, and compare MAP with MLE while exposing every boundary and nonunique mode.

Intuition

The data likelihood describes how candidate parameters explain observations. A prior density weights candidates before these observations. Their product gives an unnormalized posterior; a separate normalizing integral gives the posterior distribution. MAP chooses a posterior density mode, while MLE chooses a likelihood maximum.

Definition

A proper Beta prior has shapes α,β>0\alpha,\beta>0 and density p(θ)=θα−1(1−θ)β−1/B(α,β)p(\theta)=\theta^{\alpha-1}(1-\theta)^{\beta-1}/B(\alpha,\beta) on (0,1). Observed k successes and n−k failures give posterior Beta⁡(a,b)\operatorname{Beta}(a,b) with a=α+ka=\alpha+k and b=β+n−kb=\beta+n-k. Evidence is P(D)=B(a,b)/B(α,β)P(D)=B(a,b)/B(\alpha,\beta) for the fixed ordered sequence.

Mechanics

Edit the actual sequence, candidate θ and prior shapes. The stage displays probability over observations, likelihood over candidates, prior, unnormalized posterior and normalized posterior. The inspector separates posterior mode, posterior mean, MLE and normalization factor. A prior changes the Bayesian result while leaving the data likelihood itself unchanged.

Derivation

Adding exponents gives p(θ)L(θ)∝θa−1(1−θ)b−1p(\theta)L(\theta)\propto\theta^{a-1}(1-\theta)^{b-1}. The Beta integral normalizes this expression. Only when a>1 and b>1 is the unique mode (a−1)/(a+b−2)(a-1)/(a+b-2) interior. Differentiating the log density proves this formula under those conditions; other shapes require their own boundary analysis.

Worked examples

Prior Beta(2,2) and data [1,0,1,1,0] give Beta(5,4). MAP is 4/7, MLE is 3/5, and posterior mean is 5/9. Since B(5,4)=1/280B(5,4)=1/280 and B(2,2)=1/6B(2,2)=1/6, evidence is 3/140. At θ=1/2 the normalized posterior density is 35/16, which can exceed 1 because it is a density, not point probability.

Worked example: Update a prior; inspect the posterior mode

Actual fixed sequence: [1, 0, 1, 1, 0]. Successes: 3; failures: 2.

Next outcome 1= 0.6
Next outcome 0= 0.4
Likelihood over candidate parameters
Likelihood over candidate parametersObserved Bernoulli data: 1, 0, 1, 1, 0. 3 successes, 2 failures. Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.0.03801600.51

θ · solid curve is actual ordered-data likelihood · dot is the candidate · dashed marker is MLE

Log-likelihood ≈ -3.36506

Prior and data likelihood
Prior and data likelihoodPrior ≈ 1.44; likelihood 0.03456. Divergent endpoints are omitted from the finite drawing grid.1.500.51

Solid: normalized prior density · dashed: likelihood, which is not normalized over θ

Posterior normalization
Posterior normalizationPosterior ≈ 2.32243, unnormalized posterior ≈ 0.0497664. Interior MAP: 4/7.2.3499700.51

Solid: normalized Beta posterior density · dashed: prior × likelihood · divergent endpoints are limit cases

Prior density ≈ 1.44

Prior × likelihood ≈ 0.0497664

Normalized posterior density ≈ 2.32243

Interior MAP: 4/7.

Beta density is defined on (0,1); endpoint values are interpreted by limits. Divergent endpoints are excluded from the 0.005 plotting grid and stated explicitly; the plotted curve is never renormalized. A divergent endpoint density does not create a probability atom.

Observed Bernoulli data: 1, 0, 1, 1, 0. 3 successes, 2 failures. Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.

Ordered-data likelihood
0.03456
Log-likelihood (natural log)
−3.365058
Maximal data likelihood
0.03456
Log-likelihood gap to optimum
0
Data-derived maximizing parameter
0.6
Posterior mode (stated endpoint convention)
4/70.571429…
Posterior alpha
5
Posterior beta
4
Prior density
1.44
Unnormalized posterior density
0.049766
Normalized posterior density
2.322432
Model evidence / normalizing factor
0.021429
Posterior mean (distinct from MAP)
5/90.555556…

= exact finite-data/model quantity · ≈ binary64 approximation; empirical results describe this run

from 0 to 1, up to 2 decimal places

from 0.5 to 5, up to 1 decimal place

from 0.5 to 5, up to 1 decimal place

Posterior = Beta(5, 4). Interior MAP: 4/7. Bernoulli MLE = 0.6.

L(θ)=θ3(1−θ)2L(\theta)=\theta^{3}(1-\theta)^{2}
ℓ(θ)=3log⁡θ+2log⁡(1−θ)≈−3.36506\ell(\theta)=3\log\theta+2\log(1-\theta)\approx-3.36506
p(θ∣D)∝θ4(1−θ)3p(\theta\mid D)\propto\theta^{4}(1-\theta)^{3}
θMAP=a−1a+b−2=47\theta_{\mathrm{MAP}}=\frac{a-1}{a+b-2}=\frac{4}{7}

Keep observed data fixed

Observed sequence: 1, 0, 1, 1, 0. The ordered-sequence model has no binomial count coefficient.

L(θ)=θ3(1−θ)2L(\theta)=\theta^{3}(1-\theta)^{2}

Replay the observed data, log evaluation and valid optimizing or mode rule.

Executable reference code

Run both standalone files. The pedagogical version exposes exact mechanics and valid mode cases; the NumPy companion makes array operations explicit. Assertions use literal hand-worked outputs and do not tune data to a preferred estimate.

code/python/statistics_map.py
"""Runnable map reference with explicit boundary policy."""
from fractions import Fraction as F
from math import exp,lgamma,log,log1p
def modes(a,b):
    a,b=F(a),F(b)
    if a>1 and b>1:return [(a-1)/(a+b-2)]
    if a==b==1:return 'all parameters'
    if a<1 and b<1:return 'both divergent endpoint limits'
    return [F(0) if a<=1 and b>=1 else F(1)]
data=[1,0,1,1,0];alpha,beta=2,2;a=alpha+sum(data);b=beta+len(data)-sum(data)
x=.5;log_beta=lgamma(a)+lgamma(b)-lgamma(a+b)
posterior=exp((a-1)*log(x)+(b-1)*log1p(-x)-log_beta)
assert (a,b)==(5,4) and modes(a,b)==[F(4,7)]
assert abs(posterior-35/16)<1e-12
assert modes(1,1)=='all parameters' and modes(.5,.5)=='both divergent endpoint limits'
assert modes(1,4)==[0] and modes(4,1)==[1]
print('Posterior Beta',a,b,'MAP:',modes(a,b),'density:',posterior)
code/python/statistics_map_numpy.py
"""Runnable map reference with explicit boundary policy."""
import numpy as np
from math import lgamma
data=np.array([1,0,1,1,0]);alpha,beta=2.,2.
a=alpha+data.sum();b=beta+data.size-data.sum()
# This formula is used only after checking a>1 and b>1.
assert a>1 and b>1
mode=(a-1)/(a+b-2)
x=np.array([.25,.5,.75])
log_beta=lgamma(a)+lgamma(b)-lgamma(a+b)
posterior=np.exp((a-1)*np.log(x)+(b-1)*np.log1p(-x)-log_beta)
assert np.isclose(mode,4/7) and np.isclose(posterior[1],35/16)
print('MAP:',mode,'posterior densities:',posterior)

Explore

Use a uniform prior, an informative prior and the empty-data presets. Under a uniform prior and mixed nonempty data, MAP equals MLE; empty data leave the posterior equal to the prior. Inspect all-success data with uniform prior to see a boundary MAP. The two-boundary preset exposes divergent limits at both endpoints.

Common mistakes and counterexamples

For a=b=1 the posterior is uniform and every parameter is a mode. If a≤1 and b≥1 with unequal shapes, the limiting mode is at 0; the symmetric case is at 1. If both shapes are below 1, both endpoint density limits diverge and no unique finite maximum is attained on (0,1). We explicitly use a limiting boundary-mode convention. A divergent density does not assign positive probability to a single endpoint. MAP is also sensitive to parameterization because density transforms with a Jacobian; it is not a coordinate-free summary.

Numerical considerations

Prior shapes are restricted to half-integers from 0.5 to 5; observations are bounded at 32. This admits all taught mode cases while allowing log Gamma evaluation by Γ(1)=1, Γ(1/2)=√π and recurrence. Python independently uses math.lgamma; relative/absolute tolerance is 2e−12. Products combine exponents before endpoint evaluation, so a zero likelihood times a divergent prior never produces NaN. A finite plotting grid omits divergent endpoint values and reports their limits explicitly; it does not renormalize the sampled curve.

Engineering and ML connections

Maximizing the log posterior is equivalent to minimizing negative log-likelihood minus log prior, up to a parameter-independent constant. This gives a statistical interpretation of some regularization penalties. The next optimization material concerns how an objective is searched; it does not supply or justify the prior. MAP, MLE and posterior mean remain distinct inferential choices.

Exercises

Work from fixed observed data, distinguish statistical objects, and check corrective feedback with a worked solution.

calculation

Conjugate update

Prior Beta(2,2) sees [1,0,1,1,0]. Give posterior alpha and beta.

calculation

Three estimates

For Beta(5,4) after the canonical data, give MAP, Bernoulli MLE and posterior mean.

calculation

Normalization

With prior Beta(2,2) and the canonical data, give model evidence and posterior density at θ=1/2.

explanation

Uniform posterior

Prior Beta(1,1) with no data gives what MAP result?

Choose one answer

explanation

Divergent endpoints

Beta(1/2,1/2) has which mode policy in this explorer?

Choose one answer

Summary and checkpoints

Perform the Beta–Bernoulli update, calculate evidence, compare three parameter summaries and apply the interior formula only in its valid domain. Explain boundary limits, uniform nonuniqueness and the difference between density and probability.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available