Imports¶
%load_ext autoreload
%autoreload 2
import logging
import matplotlib.pyplot as plt
import seaborn as sns
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 msml610.tutorials.msml610_utils 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
Cell 1: Visual Bin: Population of Marbles¶
Goal:
- Visualize the concept of an unknown population of marbles in a bin
- Introduce the parameter as the true (unknown) proportion of red marbles
- Build intuition for how we have a fixed population with a fixed proportion
Plots:
- Display a single plot:
- Bin with Marbles: A 2D grid showing red and green marbles arranged in the bin
- The visual distribution reflects the true proportion
Parameters:
mu(): True proportion of red marbles in the population (0 to 1)seed: Random seed controlling the spatial arrangement of marbles (for reproducibility)
Key observations:
- The bin represents a population with a fixed but “unknown” parameter
- In real-world scenarios, we don’t know the true - we can only sample from the population
- The spatial arrangement of marbles is random, but the overall proportion is always
- This sets up the fundamental problem: How do we estimate from samples?
utils.cell1_draw_bin_with_marbles_interactive()Loading...
Cell 2: Single Experiment: Is Close to ?¶
Goal:
- Demonstrate a single sampling experiment to estimate the population parameter
- Show how we compute the sample proportion from random draws
- Examine the relationship between (sample statistic) and (population parameter)
- Understand that a single experiment gives us one estimate, which may or may not be close to
Plots:
- Display two panels:
- Population vs Sample: Bar chart comparing (population) and (sample)
- Color-coded bars indicate how close is to (green = close, yellow = medium, red = far)
- Interpretation: Text box showing parameters, results, and assessment of the single experiment
- Population vs Sample: Bar chart comparing (population) and (sample)
Parameters:
mu(): True proportion of red marbles in the population (0 to 1)N(): Number of marbles to sample (sample size)seed: Random seed for reproducibility of sampling
Key observations:
- A single experiment produces one sample proportion
- The error varies depending on the random sample drawn
- Sometimes is close to , sometimes it’s not - we see natural sampling variability
- Try different seeds to observe how changes across different random samples
- This demonstrates why a single experiment is insufficient - we need to understand the distribution of
utils.cell2_plot_single_experiment_interactive()Loading...
Cell 2.1: Limitations of Single Experiments¶
- Single experiments don’t tell the full story:
- We saw that can vary significantly across different random samples
- One experiment gives us a point estimate, but no information about reliability
- What we really need to know: - the probability that our estimate is “far” from truth
- Critical questions to consider:
- What if we got unlucky in our sample? How would we know?
- How confident can we be that based on a single observation?
- Does sample size matter? How much does it help?
- Can we quantify the uncertainty in our estimate?
- The solution: “Let’s repeat this many times...”
- By running many experiments, we can:
- Observe the distribution of values
- Estimate empirically
- Understand how sample size affects the accuracy of
- By running many experiments, we can:
Cell 3: Monte Carlo Simulation: Distribution of ¶
Goal:
- Run many repeated sampling experiments to understand the distribution of
- Empirically estimate - the probability that is far from
- Observe how the distribution of concentrates around as we increase sample size
- Build intuition for the Law of Large Numbers and Central Limit Theorem
Plots:
- Display two panels:
- Distribution of nu: Histogram with KDE overlay showing the distribution of sample proportions across experiments
- Green dashed line: True parameter
- Red shaded regions: Areas where
- Statistics box: Mean, standard deviation, and empirical probability
- Key Insights: Text box summarizing parameters, results, and observations
- Distribution of nu: Histogram with KDE overlay showing the distribution of sample proportions across experiments
Parameters:
mu(): True proportion of red marbles in the population (0 to 1)N(): Number of samples per experiment (sample size)n_experiments: Number of repeated experiments to runeps(): Tolerance threshold for defining “far from ”seed: Random seed for reproducibility
Key observations:
- The distribution of values clusters around the true - demonstrating unbiasedness
- As increases, the distribution becomes tighter (smaller variance) - Law of Large Numbers
- The empirical probability decreases with larger
- The distribution approaches a normal shape - Central Limit Theorem
- We can now quantify: “What fraction of experiments produce estimates within of ?”
- This connects to confidence intervals and the precision of statistical estimates
utils.cell3_monte_carlo_simulation_interactive()Loading...
Cell 3.1: Observations from Monte Carlo Simulation¶
- Distribution properties:
- The distribution of values is centered around the true - this shows that is an unbiased estimator
- The shape of the distribution approximates a normal (Gaussian) distribution - illustrating the Central Limit Theorem
- Most values fall close to , with fewer extreme outliers
- Effect of sample size :
- As increases, the distribution becomes tighter (smaller variance: )
- Larger means more precise estimates - the “spread” of possible values shrinks
- This is the Law of Large Numbers in action: as
- Probabilistic guarantees:
- The empirical probability decreases with larger
- We can quantify confidence: “With sample size , approximately X% of experiments yield within of ”
- This connects to Hoeffding’s inequality and other concentration bounds
- Connection to machine learning:
- Replace “marbles in bin” with “data points” and “” with “expected error”
- Sample proportion represents training error on samples
- True represents generalization error on the full population
- Key insight: With enough samples ( large), training error is close to generalization error