{"schemaVersion":2,"courseId":"did","module":{"id":"did-idea","title":"1 · The DiD idea","summary":"Why comparing one group's before/after change against a control group's change isolates a causal effect.","stages":[{"type":"read","title":"Two comparisons are better than one","html":"<h2>The problem DiD solves</h2><p>Suppose a state raises its minimum wage and we want the effect on employment. Two naive strategies both fail:</p><ul><li><strong>Before vs. after</strong> in the treated state alone: employment also moves because of the economy, seasons, trends — not just the policy.</li><li><strong>Treated vs. control</strong> at one point in time: the two states differ for many baseline reasons (industry mix, demographics) that have nothing to do with the policy.</li></ul><p>Each single comparison is contaminated. <strong>Difference-in-Differences (DiD)</strong> combines them: take the <em>change</em> in the treated group and subtract the <em>change</em> in a control group over the same window.</p><div class=\"callout\"><b>Key idea.</b> The control group's change estimates what <em>would have happened</em> to the treated group anyway. Subtracting it strips out common trends and common shocks, leaving the causal effect.</div><h3>Four cells</h3><p>DiD lives on a 2×2 grid: {treated, control} × {before, after}. We need the average outcome in each of the four cells.</p>","id":"did-idea-01","skillIds":["did.counterfactual-trend"],"estimatedMinutes":6,"difficulty":"applied","prerequisiteStageIds":[]},{"type":"read","title":"The estimator in words and symbols","html":"<h3>Differencing twice</h3><p>Let ȳ denote a group-period average outcome. The treated group's raw change over time is:</p><p class=\"katexish\">Δtreated = ȳ(treated, after) − ȳ(treated, before)</p><p>But part of that is just the trend everyone experienced. The control group reveals that trend:</p><p class=\"katexish\">Δcontrol = ȳ(control, after) − ȳ(control, before)</p><p>The DiD estimate is the <em>difference of these differences</em>:</p><p class=\"katexish\">ATT̂ = Δtreated − Δcontrol</p><p>This number estimates the <strong>Average Treatment effect on the Treated (ATT)</strong> — how much the policy changed outcomes for the units that actually got it.</p><div class=\"callout\"><b>Key idea.</b> The control group is a <em>counterfactual machine</em>: it tells us the change the treated group would have had with no treatment. The leftover is the effect.</div>","id":"did-idea-02","skillIds":["did.two-by-two-did"],"estimatedMinutes":6,"difficulty":"applied","prerequisiteStageIds":["did-idea-01"]},{"type":"code","title":"Compute DiD from four cell means","note":"Simulate a treated and a control group, before and after. True effect (ATT) is 4. We recover it by hand from the four averages.","code":"import numpy as np, pandas as pd\nrng = np.random.default_rng(0)\nn = 500\ntreated = rng.integers(0, 2, n)        # 1 = treated group, 0 = control\npost    = rng.integers(0, 2, n)        # 1 = after, 0 = before\nATT = 4.0\n# baseline gap 3 (treated higher), common time trend 2, true effect 4 only when treated & post\ny = 10 + 3*treated + 2*post + ATT*treated*post + rng.normal(0, 2, n)\ndf = pd.DataFrame(dict(y=y, treated=treated, post=post))\n\ncell = df.groupby(['treated','post'])['y'].mean().unstack()\nprint('Cell means (rows=treated, cols=post):')\nprint(cell.round(3))\n\nd_treated = cell.loc[1,1] - cell.loc[1,0]\nd_control = cell.loc[0,1] - cell.loc[0,0]\ndid = d_treated - d_control\nprint(f'\\nchange in treated group : {d_treated:.3f}')\nprint(f'change in control group: {d_control:.3f}')\nprint(f'DiD estimate (ATT)     : {did:.3f}   (true = {ATT})')","id":"did-idea-03","skillIds":["did.counterfactual-trend"],"estimatedMinutes":10,"difficulty":"applied","prerequisiteStageIds":["did-idea-02"]},{"type":"interactive","title":"See the four cells move","note":"Slide the true ATT and the common time trend. The DiD estimate should track the ATT and ignore the trend.","params":[{"name":"att","label":"True ATT","min":-6,"max":8,"step":0.5,"value":4},{"name":"trend","label":"Common time trend","min":-4,"max":6,"step":0.5,"value":2}],"template":"import numpy as np, pandas as pd, matplotlib.pyplot as plt\nrng = np.random.default_rng(0)\nn = 600\ntreated = rng.integers(0,2,n); post = rng.integers(0,2,n)\ny = 10 + 3*treated + {{trend}}*post + {{att}}*treated*post + rng.normal(0,2,n)\ndf = pd.DataFrame(dict(y=y, treated=treated, post=post))\nc = df.groupby(['treated','post'])['y'].mean().unstack()\ndid = (c.loc[1,1]-c.loc[1,0]) - (c.loc[0,1]-c.loc[0,0])\nplt.figure(figsize=(6,3.7))\nplt.plot([0,1],[c.loc[1,0],c.loc[1,1]],'o-',color='#da9100',lw=2,label='treated')\nplt.plot([0,1],[c.loc[0,0],c.loc[0,1]],'o-',color='#c9c6ac',lw=2,label='control')\n# counterfactual: treated baseline + control's change\ncf = c.loc[1,0] + (c.loc[0,1]-c.loc[0,0])\nplt.plot([0,1],[c.loc[1,0],cf],'--',color='#af983f',lw=2,label='treated counterfactual')\nplt.xticks([0,1],['before','after']); plt.legend(fontsize=7)\nplt.title(f'DiD (ATT) estimate = {did:.2f}')","id":"did-idea-04","skillIds":["did.two-by-two-did"],"estimatedMinutes":8,"difficulty":"applied","prerequisiteStageIds":["did-idea-03"]},{"type":"quiz","title":"Check","prompt":"A common economy-wide shock makes BOTH groups' outcomes rise by 5 between before and after. What happens to the DiD estimate?","choices":["It rises by 5","It rises by 10","It is unchanged","It falls by 5"],"answer":2,"hint":"DiD subtracts the control group's change, which now contains that +5.","explain":"A shock common to both groups enters both Δtreated and Δcontrol, so it cancels in the difference of differences. DiD is designed to be immune to such common shocks.","id":"did-idea-05","skillIds":["did.counterfactual-trend"],"estimatedMinutes":5,"difficulty":"applied","prerequisiteStageIds":["did-idea-04"],"variantId":"did-idea-05-core"},{"type":"fillblank","title":"The control group is a what?","prompt":"In DiD, the control group's before-to-after change is used as an estimate of the treated group's ___ change — what would have happened to the treated units absent treatment.","lead":"You just saw that subtracting the control group's change strips out common trends and shocks, leaving the causal effect.","accept":["counterfactual","counter-factual","counter factual"],"hint":"It is the change the treated group *would* have had with no policy — a word starting with 'counter'.","explain":"The 'control is just another treated group' slip. The control group is a <em>counterfactual</em> machine: its change estimates the trend/shocks the treated group would also have felt, so subtracting it nets those out and leaves the treatment effect. The tempting reading — that the control is simply a second observation of the effect — fails because the control is never treated; its only job is to reveal the common trend. This holds <b>only if</b> that common trend really is shared (parallel trends); if the groups' trends diverge, the control is the wrong counterfactual.","points":15,"id":"did-idea-06","skillIds":["did.two-by-two-did"],"estimatedMinutes":4,"difficulty":"applied","prerequisiteStageIds":["did-idea-05"],"variantId":"did-idea-06-core"}]}}
