Level 3 — ProductionProbability & Statistics

Probability & Statistics

45 minutes60 marksprintable — key stays hidden on paper

Chapter: 1.3 Probability & Statistics Level: 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Instructions: Show all derivations from first principles. Where code is requested, write it from memory (Python/NumPy conventions accepted). "Explain out loud" prompts require a clear conceptual narrative, not just a formula.


Question 1 — Bayes from scratch + application (10 marks)

(a) Starting from the axioms of probability and the definition of conditional probability, derive Bayes' theorem and state the law of total probability used in the denominator. (4)

(b) A disease affects 1%1\% of a population. A test has sensitivity P(+D)=0.95P(+\mid D)=0.95 and specificity P(¬D)=0.90P(-\mid \neg D)=0.90. Compute P(D+)P(D\mid +). (4)

(c) Explain out loud: why is the posterior probability so much lower than the test sensitivity? (2)


Question 2 — MLE derivation from memory (12 marks)

You observe i.i.d. data x1,,xnx_1,\dots,x_n from an exponential distribution with density f(x;λ)=λeλxf(x;\lambda)=\lambda e^{-\lambda x}, x0x\ge 0.

(a) Write the likelihood and log-likelihood functions. (3)

(b) Derive the maximum likelihood estimator λ^\hat\lambda from scratch, including the second-order check that it is a maximum. (5)

(c) For data {2,4,6,8}\{2, 4, 6, 8\}, compute λ^\hat\lambda numerically. (2)

(d) Explain out loud: in one or two sentences, how does MAP estimation differ from MLE, and when do they coincide? (2)


Question 3 — Expectation & variance derivations (10 marks)

(a) For a Binomial random variable XBin(n,p)X\sim\text{Bin}(n,p), derive E[X]\mathbb{E}[X] and Var(X)\text{Var}(X) by expressing XX as a sum of independent Bernoulli variables. (6)

(b) Prove the identity Var(X)=E[X2](E[X])2\text{Var}(X)=\mathbb{E}[X^2]-(\mathbb{E}[X])^2 from the definition of variance. (4)


Question 4 — Code from memory: CLT simulation (10 marks)

(a) Write a Python/NumPy function clt_demo(n, m) that draws mm sample means, each averaging nn i.i.d. Uniform(0,1)(0,1) draws, and returns the array of sample means. Write it from memory. (4)

(b) State the mean and variance of a single Uniform(0,1)(0,1) variable, and hence give the theoretical mean and variance of a sample mean of nn such draws. (4)

(c) Explain out loud: what does the Central Limit Theorem predict about the histogram of the returned means as nn grows? (2)


Question 5 — Entropy, cross-entropy, KL (10 marks)

(a) Define the entropy H(p)H(p) of a discrete distribution, the cross-entropy H(p,q)H(p,q), and the KL divergence DKL(pq)D_{KL}(p\Vert q). State the relationship linking all three. (4)

(b) For p=(0.5,0.5)p=(0.5,0.5) and q=(0.25,0.75)q=(0.25,0.75) (log base 2), compute H(p)H(p), H(p,q)H(p,q), and DKL(pq)D_{KL}(p\Vert q). (4)

(c) Explain out loud: why is DKL(pq)0D_{KL}(p\Vert q)\ge 0 always, and why is it asymmetric? (2)


Question 6 — Confidence interval construction (8 marks)

A sample of n=64n=64 measurements has sample mean xˉ=50\bar x=50 and known population standard deviation σ=8\sigma=8.

(a) Derive the form of a 95%95\% confidence interval for the mean using the CLT, and compute it (use z0.975=1.96z_{0.975}=1.96). (5)

(b) Explain out loud: what does "95%95\% confidence" actually mean — and what does it not mean? (3)

Answer keyMark scheme & solutions

Question 1 (10)

(a) From conditional probability definition: P(AB)=P(AB)P(B)P(A\mid B)=\dfrac{P(A\cap B)}{P(B)} and P(BA)=P(AB)P(A)P(B\mid A)=\dfrac{P(A\cap B)}{P(A)}. (1) Hence P(AB)=P(BA)P(A)P(A\cap B)=P(B\mid A)P(A), substitute: P(AB)=P(BA)P(A)P(B)P(A\mid B)=\dfrac{P(B\mid A)P(A)}{P(B)}. (2) Law of total probability: P(B)=iP(BAi)P(Ai)P(B)=\sum_i P(B\mid A_i)P(A_i) over a partition {Ai}\{A_i\}. (1) Why: denominator normalizes so the posterior sums to 1 over the partition.

(b) P(+)=P(+D)P(D)+P(+¬D)P(¬D)P(+)=P(+\mid D)P(D)+P(+\mid\neg D)P(\neg D). P(+¬D)=10.90=0.10P(+\mid\neg D)=1-0.90=0.10. (1) P(+)=0.95(0.01)+0.10(0.99)=0.0095+0.099=0.1085P(+)=0.95(0.01)+0.10(0.99)=0.0095+0.099=0.1085. (2) P(D+)=0.00950.10850.0876P(D\mid +)=\dfrac{0.0095}{0.1085}\approx 0.0876. (1)

(c) Because the disease is rare (1% prior), the large number of false positives from the healthy 99% swamps the true positives; low base rate dominates despite high sensitivity. (2)


Question 2 (12)

(a) L(λ)=i=1nλeλxi=λneλxiL(\lambda)=\prod_{i=1}^n \lambda e^{-\lambda x_i}=\lambda^n e^{-\lambda\sum x_i}. (2) (λ)=nlnλλxi\ell(\lambda)=n\ln\lambda-\lambda\sum x_i. (1)

(b) ddλ=nλxi=0λ^=nxi=1xˉ\dfrac{d\ell}{d\lambda}=\dfrac{n}{\lambda}-\sum x_i=0 \Rightarrow \hat\lambda=\dfrac{n}{\sum x_i}=\dfrac{1}{\bar x}. (3) Second derivative d2dλ2=nλ2<0\dfrac{d^2\ell}{d\lambda^2}=-\dfrac{n}{\lambda^2}<0, confirming a maximum. (2)

(c) xi=20\sum x_i=20, n=4n=4, λ^=4/20=0.2\hat\lambda=4/20=0.2. (2)

(d) MAP maximizes posterior \propto likelihood ×\times prior, adding prior information; MLE only maximizes likelihood. They coincide when the prior is uniform (flat) over the parameter. (2)


Question 3 (10)

(a) Write X=i=1nBiX=\sum_{i=1}^n B_i, BiBernoulli(p)B_i\sim\text{Bernoulli}(p) independent. (1) E[Bi]=p\mathbb{E}[B_i]=p, so E[X]=E[Bi]=np\mathbb{E}[X]=\sum \mathbb{E}[B_i]=np. (2) Var(Bi)=p(1p)\text{Var}(B_i)=p(1-p) (since E[Bi2]=p\mathbb{E}[B_i^2]=p, subtract p2p^2). (1) By independence Var(X)=Var(Bi)=np(1p)\text{Var}(X)=\sum\text{Var}(B_i)=np(1-p). (2)

(b) Var(X)=E[(Xμ)2]=E[X22μX+μ2]\text{Var}(X)=\mathbb{E}[(X-\mu)^2]=\mathbb{E}[X^2-2\mu X+\mu^2]. (1) =E[X2]2μE[X]+μ2=\mathbb{E}[X^2]-2\mu\mathbb{E}[X]+\mu^2 (linearity). (1) =E[X2]2μ2+μ2=E[X2]μ2=E[X2](E[X])2=\mathbb{E}[X^2]-2\mu^2+\mu^2=\mathbb{E}[X^2]-\mu^2=\mathbb{E}[X^2]-(\mathbb{E}[X])^2. (2)


Question 4 (10)

(a)

import numpy as np
def clt_demo(n, m):
    samples = np.random.uniform(0, 1, size=(m, n))
    return samples.mean(axis=1)

(4) (correct shape/averaging axis and return).

(b) For UUniform(0,1)U\sim\text{Uniform}(0,1): E[U]=12\mathbb{E}[U]=\tfrac12, Var(U)=112\text{Var}(U)=\tfrac{1}{12}. (2) Sample mean of nn: mean =12=\tfrac12, variance =112n=\dfrac{1}{12n}. (2)

(c) As nn grows the histogram of sample means approaches a Normal (bell) shape centered at 0.50.5 with shrinking spread σ=1/12n\sigma=1/\sqrt{12n}, regardless of the underlying uniform shape. (2)


Question 5 (10)

(a) H(p)=ipilogpiH(p)=-\sum_i p_i\log p_i; H(p,q)=ipilogqiH(p,q)=-\sum_i p_i\log q_i; DKL(pq)=ipilogpiqiD_{KL}(p\Vert q)=\sum_i p_i\log\dfrac{p_i}{q_i}. (3) Relationship: H(p,q)=H(p)+DKL(pq)H(p,q)=H(p)+D_{KL}(p\Vert q). (1)

(b) H(p)=2(0.5log20.5)=1H(p)=-2(0.5\log_2 0.5)=1 bit. (1) H(p,q)=[0.5log20.25+0.5log20.75]=[0.5(2)+0.5(0.415)]=1+0.2075=1.2075H(p,q)=-[0.5\log_2 0.25+0.5\log_2 0.75]=-[0.5(-2)+0.5(-0.415)]=1+0.2075=1.2075 bits. (2) DKL=H(p,q)H(p)=0.2075D_{KL}=H(p,q)-H(p)=0.2075 bits. (1)

(c) By Gibbs/Jensen's inequality DKL0D_{KL}\ge0 with equality iff p=qp=q; asymmetric because it weights the log-ratio by pp (the "true" distribution), so swapping pp and qq changes the weighting. (2)


Question 6 (8)

(a) By CLT XˉN(μ,σ2/n)\bar X\approx N(\mu,\sigma^2/n), so Xˉμσ/nN(0,1)\dfrac{\bar X-\mu}{\sigma/\sqrt n}\approx N(0,1). (1) CI: xˉ±z0.975σn\bar x\pm z_{0.975}\dfrac{\sigma}{\sqrt n}. (1) σn=88=1\dfrac{\sigma}{\sqrt n}=\dfrac{8}{8}=1. Margin =1.96×1=1.96=1.96\times1=1.96. (2) CI =[48.04, 51.96]=[48.04,\ 51.96]. (1)

(b) It means: if we repeated the sampling procedure many times, about 95% of the constructed intervals would contain the true fixed μ\mu. It does not mean there is a 95% probability that this particular interval contains μ\mu (the true value is fixed, not random). (3)


[
  {"claim":"P(D|+) ≈ 0.0876 for the disease test", "code":"pD=Rational(1,100); sens=Rational(95,100); spec=Rational(90,100); num=sens*pD; den=sens*pD+(1-spec)*(1-pD); val=float(num/den); result = abs(val-0.0876)<0.001"},
  {"claim":"MLE lambda = 0.2 for data sum 20, n=4", "code":"n=4; s=20; lam=n/s; result = abs(lam-0.2)<1e-9"},
  {"claim":"Binomial variance sum equals np(1-p)", "code":"n,p=symbols('n p'); result = simplify(n*p*(1-p) - (n*p*(1-p)))==0"},
  {"claim":"H(p)=1, H(p,q)=1.2075, KL=0.2075 bits", "code":"import math; Hp=-(0.5*math.log2(0.5)+0.5*math.log2(0.5)); Hpq=-(0.5*math.log2(0.25)+0.5*math.log2(0.75)); KL=Hpq-Hp; result = abs(Hp-1)<1e-6 and abs(Hpq-1.2075)<1e-3 and abs(KL-0.2075)<1e-3"},
  {"claim":"95% CI is [48.04,51.96]", "code":"xbar=50; se=8/ (64**0.5); lo=xbar-1.96*se; hi=xbar+1.96*se; result = abs(lo-48.04)<0.01 and abs(hi-51.96)<0.01"}
]