%matplotlib inline
from d2l import mxnet as d2l
from mxnet import np, npx
from mxnet.numpy.random import multinomial
import random
npx.set_np()Most of machine learning is inference under uncertainty:
The chapter’s running example: tossing a fair coin. As the sample count grows, empirical frequencies converge to the true P = 0.5:
The standard d2l prelude (plus a multinomial distribution we’ll use shortly):
A cleaner abstraction: a Multinomial over the categories {heads, tails} with probabilities [0.5, 0.5]. One call returns the count vector for 100 tosses:
With 10 000 tosses, the empirical frequencies sit much closer to 0.5:
This is the law of large numbers: as n \to \infty the empirical mean converges to the true mean.
Plot the running estimate of P(\text{heads}) and P(\text{tails}) vs. sample count — the curves zigzag toward 0.5:
The variance of the estimate shrinks like 1/\sqrt{n} — doubling accuracy means quadrupling the sample budget.