5/36
Part 1 of 6 · Two unitsLearn

Each user's metric is a city shock plus the user's own noise

The practice data is built from that same picture.

import numpy as np

def practice_data(seed, city_sd):
    rng = np.random.default_rng(seed)
    cluster = np.repeat(np.arange(40), 50)
    treat = rng.permutation(np.r_[np.ones(20), np.zeros(20)])[cluster]
    shock = rng.normal(0, city_sd, 40)[cluster]
    y = 10 + 0.5 * treat + shock + rng.normal(0, 2, 2000)
    return y, treat, cluster

y, treat, cluster = practice_data(0, 1.5)
means = np.array([y[cluster == g].mean() for g in range(40)])
inside = np.array([y[cluster == g].std() for g in range(40)])
print(f"users inside one city spread by {inside.mean():.2f}")
print(f"the 40 city means spread by     {means.std():.2f}")
users inside one city spread by 1.97
the 40 city means spread by     1.39

This is how the practice data is made. cluster holds each user's city id, 0 to 39, fifty users each. rng.normal(0, city_sd, 40) draws one shock per city, and indexing with [cluster] copies it to the city's 50 users. The coin flips are copied the same way.

The spread here is the standard deviation. Users inside a city spread by about 2, their own noise. If users were independent, a mean of 50 of them would spread by only 2/50=0.282/\sqrt{50} = 0.28. The city means spread by 1.39. Most of that is the city shock, which every user in the city shares.

In symbols:

yig=μ+τ tg+bg+eigy_{ig} = \mu + \tau\, t_g + b_g + e_{ig}

In words: user ii in city gg gets the base level μ\mu (10 in the code), plus the effect τ\tau (0.5) when the city's flag tgt_g is 1, plus the city's shock bgb_g, plus the user's own noise eige_{ig}. The shocks have variance σb2\sigma_b^2, 1.5 squared in the code, and the users' own noise has variance σw2\sigma_w^2, 2 squared. There are GG cities of mm users, so N=GmN = Gm users in all.