Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 1 · Chance, repetition, and fair tests

Level: Levels 1–2 · Prerequisites: L1-02 (probability model), L1-03 (repeat and record), L1-05 (assign a fair comparison)

What you’ll learn:

  • A probability belongs to a model. A relative frequency is a measurement, and it can differ from the model.

  • Many independent trials usually bring the frequency close to the model probability. This is the law of large numbers. You will also see how big the typical gap is.

  • A 95 % band around the probability is about one fixed number of trials. It says nothing about a whole run.

  • A coin flip that assigns the treatment balances a hidden trait on average. Letting people choose does not.

Open in Colab

1 · Predict → Run → Explain: a coin and a die

Predict. You flip a fair coin 10 times. Will you see exactly 5 heads? After 10,000 flips, will the fraction of heads be exactly one half?

10 coin flips            hits=    7  frequency=0.7000  model p=0.5000  gap=+0.2000  gap/SE=+1.26
10,000 coin flips        hits= 4952  frequency=0.4952  model p=0.5000  gap=-0.0048  gap/SE=-0.96
6,000 die rolls (six)    hits=  982  frequency=0.1637  model p=0.1667  gap=-0.0030  gap/SE=-0.62
Large-n frequencies are within 3 standard errors of the model probability.

Explain. A probability belongs to the model. The relative frequency (hits ÷ trials) is a measurement Illowsky & Dean, 2023. The table prints the gap in units of the standard error p(1−p)/n\sqrt{p(1-p)/n}. With many trials the gap is small in absolute terms, because the standard error shrinks like 1/n1/\sqrt{n}. Yet the count of heads is not pushed toward an exact balance.

2 · The law of large numbers, many runs at once

Predict. Draw 40 independent runs of 10,000 coin flips. Around n=10,000n=10{,}000, roughly what fraction of the runs should sit inside the band p±1.96p(1−p)/np \pm 1.96\sqrt{p(1-p)/n}?

runs inside the band at n=10,000:            1.000
average pointwise coverage over n >= 30:     0.960
runs that leave the band at least once (n>=30): 0.400
Band checks passed.

Drag the slider to let the runs grow. Each thin line is one seeded run. The shaded region is the pointwise 95 % band.

Loading...

Explain. At any one fixed nn, about 95 % of runs should be inside the band. The printed coverage at n=10,000n=10{,}000 fits that for 40 runs. A larger share of runs leaves the band somewhere along the way. That is because the band is pointwise. It is not a guarantee for the whole path. The band’s width shrinks like 1/n1/\sqrt{n}. This rate comes from the variance p(1−p)/np(1-p)/n. It is what makes the law of large numbers (the frequency settles near pp) quantitative Illowsky & Dean, 2023.

3 · Predict → Run → Explain: who gets the treatment?

Imagine 2,000 people. Half of them have a hidden trait UU (say, high motivation) that you cannot measure. The treatment raises the chance of a good outcome by 0.10. The hidden trait raises it by 0.40:

P(Y=1∣T,U)=0.2+0.1 T+0.4 U.P(Y=1\mid T,U) = 0.2 + 0.1\,T + 0.4\,U .

Predict. Suppose motivated people are more likely to choose the treatment. Will “treated minus untreated” still be about 0.10? What if a coin decides?

strength=0.00: trait share treated=0.506, control=0.525, estimate=+0.128, population contrast (engine)=+0.100, true effect=+0.100
strength=0.30: trait share treated=0.803, control=0.215, estimate=+0.324, population contrast (engine)=+0.340, true effect=+0.100
Randomization checks passed.

Use the slider to change how strongly the hidden trait drives the choice of treatment. At strength 0 the assignment is a fair coin.

Loading...

Explain. When people choose, the treated group has more people with the hidden trait. So the contrast mixes the treatment effect with the trait’s effect. When a coin chooses, the trait is balanced on average. Then the simple contrast estimates the treatment effect Hernan & Robins, 2020. The “population contrast” bar is computed exactly by science.causal. The orange bar is one simulated sample of 2,000 people.

4 · Randomization balances on average, not in every sample

Predict. Take a small study of 200 people. Re-run the coin-flip assignment 2,000 times. Will the trait share always be equal in the two groups?

coin flip:      mean imbalance +0.0018, sd 0.0705 (approx. theory 0.0707)
self-selected:  mean imbalance +0.6048
share of coin-flip studies with |imbalance| > 0.10: 0.157
Repeated-randomization checks passed.

The static figure below sums up both halves of the lab.

<Figure size 1300x494 with 2 Axes>

Explain. Coin-flip assignment centers the imbalance at zero. But a single small study can still be unlucky. The printed share of studies with a gap above 0.10 is not zero. That is why reports give group sizes and uncertainty. Self-selection shifts the whole distribution away from zero. More data cannot fix that.

Going deeper

Law of large numbers

Take independent trials Xi∼Bernoulli(p)X_i\sim\mathrm{Bernoulli}(p) with p^n=1n∑i=1nXi\hat p_n=\frac1n\sum_{i=1}^n X_i. Then

E[p^n]=p,Var⁡(p^n)=p(1−p)n,P(∣p^n−p∣≥ε)≤p(1−p)nε2 (Chebyshev).\mathbb E[\hat p_n]=p,\qquad \operatorname{Var}(\hat p_n)=\frac{p(1-p)}{n},\qquad P\big(|\hat p_n-p|\ge\varepsilon\big)\le\frac{p(1-p)}{n\varepsilon^2}\ (\text{Chebyshev}).

The central limit theorem gives the approximate pointwise band p±1.96p(1−p)/np\pm1.96\sqrt{p(1-p)/n} for a fixed nn. A band that holds for the whole path at once must be wider. Such a band can come, for example, from the law of the iterated logarithm or from a union bound.

Randomization

Write YtY^t for the potential outcome under treatment tt. In the model of Section 3,

E[Y∣T=1]−E[Y∣T=0]=0.1⏟effect+0.4 (P(U=1∣T=1)−P(U=1∣T=0)).\mathbb E[Y\mid T=1]-\mathbb E[Y\mid T=0] =\underbrace{0.1}_{\text{effect}}+0.4\,\big(P(U=1\mid T=1)-P(U=1\mid T=0)\big).

Random assignment makes TT independent of UU (and so of (Y0,Y1)(Y^0,Y^1)). This is called exchangeability. The second term then vanishes in expectation Hernan & Robins, 2020. Let the self-selection strength be ss. Then P(U=1∣T=1)−P(U=1∣T=0)=2sP(U=1\mid T=1)-P(U=1\mid T=0)=2s when P(U=1)=12P(U=1)=\tfrac12, so the bias is 0.8 s0.8\,s.

Exercises

Solution

1.96(1/6)(5/6)/6000≈0.00941.96\sqrt{(1/6)(5/6)/6000}\approx 0.0094. The check cell below computes it.

Solution

The model predicts one half, because each flip is independent of the past. The check cell rebuilds the single flips from the running count. It confirms that the measured fraction is close to one half (within 0.03). Past heads do not make a tail “due”.

Solution

observed − ate =0.34−0.10=0.24=0.34-0.10=0.24. Here s=0.3s=0.3, and 0.8×0.3=0.240.8\times0.3=0.24.

Check yourself

flips right after five heads: 12394, fraction heads: 0.500
All checks passed.
References
  1. Illowsky, B., & Dean, S. (2023). Introductory Statistics 2e. https://openstax.org/books/introductory-statistics-2e/pages/1-introduction
  2. Hernan, M., & Robins, J. (2020). Causal Inference: What If. https://miguelhernan.org/whatifbook