5/36
Part 1 of 6 · No coin flipLearn and do

Four means hold everything the estimate needs

The weekly series boil down to four numbers.

def mean(t, p):
    return y[(treated == t) & (post == p)].mean()

T0, T1, C0, C1 = mean(1, 0), mean(1, 1), mean(0, 0), mean(0, 1)
rows = [("treated", T0, T1), ("control", C0, C1),
        ("gap", T0 - C0, T1 - C1)]
print("           before   after   change")
for name, b, a in rows:
    print(f"{name:<9} {b:7.2f} {a:7.2f}  {a - b:7.2f}")
           before   after   change
treated     53.75   63.50     9.75
control     41.75   47.50     5.75
gap         12.00   16.00     4.00

mean(t, p) averages the rows of one cell. (treated == t) & (post == p) is True on that cell's rows, since & is NumPy's elementwise and.

These are the practice data's four cells, T0, T1, C0 and C1, filled in for the worked example: T0=53.75T_0 = 53.75 and T1=63.5T_1 = 63.5 for the treated cities before and after, and C0=41.75C_0 = 41.75 and C1=47.5C_1 = 47.5 for the control cities.

The last number in the gap row, 4, is the answer the rest of this lesson builds. The gap grew from 12 to 16.

Each mean runs over every row in its cell. In a balanced panel, where every city has every week, that's the same as averaging the city averages.

In the editor, write cell_means(y, treated, post): the 2 by 2 table of means.