Sampling and independence
Quick answer
Sampling generates observations from a population or probability model. Independence means knowing one outcome does not change the probabilities of another. Many ML experiments assume independent observations, but repeated measurements, shared sources and time dependence can violate that assumption. A simulation estimates model behavior; it does not prove that model describes the real world.
Replacement changes conditional probabilities
A bag contains two red and two blue tokens, selected uniformly. With replacement and independent draws, P(two red)=(2/4)(2/4)=1/4. Without replacement, P(second red | first red)=1/3 and P(two red)=1/6. Events A and B are independent if P(A and B)=P(A)P(B), including zero-probability cases. For many events, pairwise independence alone does not establish mutual independence.
from fractions import Fractionprint(Fraction(2,4)*Fraction(2,4))print(Fraction(2,4)*Fraction(1,3))Expected output (Python 3.13.5):
1/41/6Removing a red token changes the composition of the bag, so the second draw depends on the first.
Simulate repeated trials with a local seed
A pseudo-random generator produces deterministic sequences for a given initial state in a defined environment. Use a local Random instance to avoid sharing state with unrelated code. Here one trial contains four independent-model Bernoulli draws with p=0.5; the count has expectation 2. Repeat 1000 trials and compare the mean with theory. The law of large numbers says averages converge in the appropriate sense under conditions such as independent identically distributed draws with finite mean; it does not require each larger sample to be closer.
from random import Randomrng = Random(17)counts = [sum(rng.random()<0.5 for _ in range(4)) for _ in range(1000)]mean = sum(counts)/len(counts)print(len(counts), round(mean,3))print(abs(mean-2)<0.15)Expected output (Python 3.13.5):
1000 2.02TrueThe result is a finite simulation, not an exact theoretical mean. The tolerance is a teaching check for this fixed fixture, not a confidence interval or universal bound.
Common mistakes
Do not confuse a reproducible seed with statistical independence. Reusing the same seed for separate experiments can duplicate draws. Sampling rows independently is inappropriate for evaluating related records from one machine or time sequence. Plan group or time-aware splits when generalization requires them. Random.sample selects positions without replacement; repeated values may still appear. Random.choices samples with replacement.
Practice and next steps
Calculate P(two blue) for both bag mechanisms. Change the simulation seed and trial count separately, record the mean, and explain why an individual estimate can move farther from theory. Next chapter covers statistical estimation and uncertainty.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sampling and independence — knowledge check
Apply probability concepts to original finite and simulated examples.
Which equality defines independence of two events?
Two red and two blue tokens: P(two red without replacement)?
What does fixing a pseudo-random seed accomplish?
What does the law of large numbers imply under suitable assumptions?
Why avoid random row splits for multiple related readings from one machine?