"""Chapter 14 exercises: decomposition (constructed data, standard library only). Exercise 14.1 (reference answer): a 3-page study of a revision app. 200 students are randomly split into two groups of 100. The paper's Table 1 reports only the 60 app-group students who used the app for the full four weeks; the 40 who gave up are left out, though they took the exam too. Every sub-question checked against its own input is answered correctly, yet the paper's overall conclusion ("the app adds 8 points") does not hold. Exercise 14.2: error accumulation across the levels of a summary tree, under the stated simplifying assumptions (each level keeps a given key fact with the same probability, levels independent). """ from math import comb # ---------- Exercise 14.1: the revision-app study (constructed data) ---------- control_n, control_gain = 100, 4.0 # control group: mean improvement in points completers_n, completers_gain = 60, 12.0 # app group, used the app for 4 weeks dropouts_n, dropouts_gain = 40, 3.0 # app group, gave up early but took the exam assert completers_n + dropouts_n == control_n == 100 table1_diff = completers_gain - control_gain app_group_gain = (completers_n * completers_gain + dropouts_n * dropouts_gain) / (completers_n + dropouts_n) itt_diff = app_group_gain - control_gain print("Exercise 14.1") print(f" Table 1 (completers only): {completers_gain} - {control_gain} = {table1_diff:.1f} points") print(f" whole randomized app group: ({completers_n}*{completers_gain} + {dropouts_n}*{dropouts_gain})/100" f" = {app_group_gain:.1f}") print(f" difference by randomized groups: {app_group_gain:.1f} - {control_gain} = {itt_diff:.1f} points") print(f" paper's completers-only difference minus the randomized one: {table1_diff - itt_diff:.1f} points") # ---------- Exercise 14.2: error accumulation in a summary tree ---------- print("\nExercise 14.2") p_keep = 0.9 # probability that one level keeps a given key fact levels = 3 # chapter text -> chapter summary -> part summary -> book summary facts = 10 # key facts in the book, one per chapter p_survive = p_keep ** levels print(f" one fact survives {levels} levels: {p_keep}^{levels} = {p_survive:.3f}") print(f" expected surviving facts out of {facts}: {facts * p_survive:.2f}") need = 3 # a conclusion that needs three specific facts together p_all = p_survive ** need print(f" all {need} facts needed for one conclusion survive: {p_survive:.3f}^{need} = {p_all:.3f}") # probability that at least 8 of 10 facts survive (binomial); not used in the text p_at_least_8 = sum(comb(facts, k) * p_survive**k * (1 - p_survive)**(facts - k) for k in range(8, facts + 1)) print(f" at least 8 of {facts} facts survive: {p_at_least_8:.3f}") # what per-level keep rate would give 0.9 survival over 3 levels? target = 0.9 needed_p = target ** (1 / levels) print(f" per-level keep rate needed for {target} survival over {levels} levels: {needed_p:.3f}")