Imports¶
%load_ext autoreload
%autoreload 2
import logging
import matplotlib.pyplot as plt
import seaborn as sns
import helpers.hintrospection as hintros
import L05_01_02_bin_analogy_ml_utils as utils
# Set plotting style.
sns.set_style("whitegrid")
plt.rcParams["figure.figsize"] = (12, 6)WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
import helpers.htutorial as ut
ut.config_notebook()
# Initialize logger.
logging.basicConfig(level=logging.INFO)
_LOG = logging.getLogger(__name__)vim support installed: restart the notebook, if needed
Python 3.12.3
Linux 589569fe8102 6.12.67-linuxkit #1 SMP Sun Jan 25 02:26:28 UTC 2026 aarch64 aarch64 aarch64 GNU/Linux
Part 1: Bin Analogy for Machine Learning¶
Cell 1.1: Visual bin: population of marbles¶
Goal:
- Visualize an unknown population of marbles in a bin, and introduce as the population’s true, unknown proportion of red marbles
Implementation: cell1_draw_bin_with_marbles_interactive()
- Places a grid of marbles, colors a
mufraction of them red with_draw_bin_with_marbles(), and shuffles the colors withseedso the arrangement looks random while the proportion stays fixed
hintros.print_obj_info(utils.cell1_draw_bin_with_marbles_interactive)Usage
Inputs
seed: random seed for the spatial arrangement of marblesmu: true proportion of red marbles in the population, 0-1
Panels
Bin with marbles: the full population, red and green marbles laid out on a grid, withmuand the red count in the title
utils.cell1_draw_bin_with_marbles_interactive()Loading...
Guided usage
- Change
seedseveral times, leavingmufixed- Observe the marbles reshuffle into a new arrangement, but the total red count stays exactly total marbles every time
- Raise
mufrom 0 toward 1- Observe the bin fill up with more red marbles, since is the fixed population proportion, not something observed by sampling
- In real-world scenarios, is never known directly, only estimated from samples: the rest of this notebook builds that estimation problem
Cell 1.2: Single experiment: is close to ?¶
Goal:
- Draw one sample of
Nmarbles from the bin, compute the sample proportion , and see how close a single estimate lands to
Implementation: cell2_plot_single_experiment_interactive()
- Draws
NBernoulli(mu) samples with_plot_single_experiment(), and computes - Colors the comparison bar chart green, yellow, or red by how far landed from
hintros.print_obj_info(utils.cell2_plot_single_experiment_interactive)Usage
Inputs
seed: random seed for the sample drawnmu: true proportion of red marbles in the population, 0-1N: number of marbles sampled, 10-1000
Panels
Population vs sample: bar chart comparing against , colored by how close the two areInterpretation: current parameters, the sampled , and the error
utils.cell2_plot_single_experiment_interactive()Loading...
Guided usage
- Change
seedseveral times, leavingmuandNfixed- Observe land in a different place each time, and the bar color flip between green, yellow, and red: one experiment is not reliable on its own
- Raise
Nfrom 10 toward 1000, leavingseedfixed- Observe settle closer to and the bar turn green more often
- A single experiment only gives a point estimate, never a sense of how
reliable that estimate is
- What is really needed is : the probability that an estimate this far off would happen at all
- Repeating the experiment many times, next, is how that probability gets measured
Cell 1.3: Monte Carlo simulation: distribution of ¶
Goal:
- Repeat the sampling experiment many times to see the full distribution of , and empirically estimate
Implementation: cell3_monte_carlo_simulation_interactive()
- Runs
n_experimentsindependent trials ofNBernoulli(mu) samples each in_plot_monte_carlo_simulation(), recording per trial - Overlays a KDE on the histogram, shades the region where , and reports the fraction of trials landing there
hintros.print_obj_info(utils.cell3_monte_carlo_simulation_interactive)Usage
Inputs
seed: random seed for the experimentsmu: true proportion of red marbles in the population, 0-1N: samples per experiment, 10-500n_experiments: number of repeated experiments, 100-10000eps: tolerance defining “far from ”
Panels
Distribution of nu: histogram plus KDE of across experiments, truemumarked, and the tails shaded redKey insights: current parameters, , , and
utils.cell3_monte_carlo_simulation_interactive()Loading...
Guided usage
- Raise
Nfrom 10 toward 500, leavingn_experimentsfixed- Observe the histogram narrow around
muand drop: this is the Law of Large Numbers, as
- Observe the histogram narrow around
- Raise
n_experimentstoward 10000- Observe the histogram fill in and its shape approach the smooth normal curve predicted by the Central Limit Theorem
- Replace “marbles in a bin” with “data points”, with
generalization error, and with training error on
Nsamples- The same result then reads: with enough training samples, training error is close to generalization error, the connection this bin analogy sets up for the rest of the course