labs / lab-c-bayes-theorem
Next task →
Practical Labs/Lab: Bayes' Theorem and Bayesian Inference
Level 2 · probability-statistics

Lab: Bayes' Theorem and Bayesian Inference

Build a minimal, verifiable implementation that demonstrates Bayes' Theorem and Bayesian Inference. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.

Mathematical basis

For Bayes' Theorem and Bayesian Inference, verify E[X]\mathbb{E}[X] and Var(X)=E[X2]−E[X]2\mathrm{Var}(X)=\mathbb{E}[X^2]-\mathbb{E}[X]^2 on a controlled synthetic sample.

Instructions

  1. 01Read section 7 of the Bayes' Theorem and Bayesian Inference learning page and write its governing equation on paper.
  2. 02Implement the core operation in NumPy / SciPy on synthetic Bernoulli and Gaussian samples. Do not call a library function that solves the whole task.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with the sample size n and record estimated mean and variance.
  5. 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.

Starter code

import numpy as np

def moments(samples):
    x = np.asarray(samples, float)
    return x.mean(), x.var(ddof=1)

m, v = moments([1, 2, 3, 4, 5])
assert abs(m - 3.0) < 1e-12 and abs(v - 2.5) < 1e-12
print(m, v)