"""Chapter 3, sections 3.1 to 3.3: numbers used in the text (constructed examples). Dice, a constructed question bank for conditional probability, the spread of sample accuracy when 200 questions are drawn from a bank with true accuracy 70%, the coverage of 95% intervals, and the half-width of the interval at several sample sizes. Standard library only. """ import math import random import statistics # 3.1 dice: expectation and variance faces = range(1, 7) e = sum(faces) / 6 var = sum((k - e) ** 2 for k in faces) / 6 print(f"dice: E = {e}, Var = {var:.4f} (= 35/12), SD = {math.sqrt(var):.3f}") mean_sq = sum(k * k for k in faces) / 6 print(f"dice: mean of squares = {mean_sq:.4f} (= 91/6); minus 3.5^2 = {mean_sq - e * e:.4f}") print(f"dice: P(6 | even) = 1/3 = {1/3:.3f}; P(two sixes) = 1/36 = {1/36:.4f}") # 3.1 constructed question bank: 400 math (240 correct), 600 reading (460 correct) math_n, math_ok, read_n, read_ok = 400, 240, 600, 460 print(f"bank: overall {(math_ok + read_ok) / (math_n + read_n):.3f}, " f"P(correct | math) = {math_ok / math_n:.3f}, P(correct | reading) = {read_ok / read_n:.3f}") # 3.2 variance of a 0/1 score for p in (0.5, 0.7, 0.9): print(f"0/1 score with p = {p}: variance p(1-p) = {p * (1 - p):.4f}, SD = {math.sqrt(p * (1 - p)):.3f}") # 3.2 ten samples of 200 questions from a bank whose true accuracy is 70% p_true, n = 0.70, 200 rng = random.Random(3) ten = [sum(rng.random() < p_true for _ in range(n)) / n for _ in range(10)] print("ten samples of 200:", " ".join(f"{a:.1%}" for a in ten)) print(f" min {min(ten):.1%}, max {max(ten):.1%}") # 3.2/3.3 many samples: spread, and coverage of 95% intervals rng = random.Random(4) reps = 10000 accs = [sum(rng.random() < p_true for _ in range(n)) / n for _ in range(reps)] sd_sim = statistics.pstdev(accs) sd_theory = math.sqrt(p_true * (1 - p_true) / n) print(f"{reps} samples of {n}: SD of sample accuracy = {sd_sim:.4f}; theory sqrt(p(1-p)/n) = {sd_theory:.4f}") within = sum(abs(a - p_true) <= 1.96 * sd_theory for a in accs) / reps print(f" share within 1.96 SD of 70%: {within:.1%}") covered = 0 for a in accs: se = math.sqrt(a * (1 - a) / n) covered += (a - 1.96 * se <= p_true <= a + 1.96 * se) print(f" share of 95% intervals (p_hat +- 1.96 SE) that contain 70%: {covered / reps:.1%}") # 3.3 model A: 71% on 200 questions pa = 0.71 se = math.sqrt(pa * (1 - pa) / n) print(f"A: 0.71*0.29 = {pa * (1 - pa):.4f}; /200 = {pa * (1 - pa) / n:.7f}; SE = {se:.4f}; " f"1.96*SE = {1.96 * se:.4f}; CI = [{pa - 1.96 * se:.1%}, {pa + 1.96 * se:.1%}]") # 3.3 half-width of the 95% interval at p_hat = 70% for m in (50, 100, 200, 500, 1000, 2000, 10000): se = math.sqrt(0.7 * 0.3 / m) print(f"n = {m:5d}: SE = {se:.2%}, half-width 1.96*SE = {1.96 * se:.1%}")