Implement a pandas function that turns per-stratum treatment and control means and counts into a post-stratified average treatment effect. It tests reshaping arm-level rows, building population-share weights, and knowing when a pooled difference in means gives the wrong answer.
Implement `def post_stratified_ate(df: pd.DataFrame) -> float` in `solution.py`. Each row is one arm-stratum cell with columns `'stratum'`, `'arm'` (`'control'` or `'treatment'`), `'mean_y'` and `'n'`. Return $\sum_s w_s d_s$, where $d_s$ is `mean_y[treatment] - mean_y[control]` within stratum $s$.
Requirements:
- $w_s$ is `(n_control + n_treatment in s) / (grand total units across all strata)`, so the weights sum to 1.
- The effect sign is treatment minus control.
- Return an unrounded `float`.
Example:
```text
A: control mean 10, treatment mean 12, n = 100 in each arm
B: control mean 20, treatment mean 23, n = 200 in each arm
C: control mean 5, treatment mean 6, n = 50 in each arm
weights: A = 2/7, B = 4/7, C = 1/7
effects: A = 2, B = 3, C = 1
ATE = 2/7 * 2 + 4/7 * 3 + 1/7 * 1
Output: 2.4285714285714284
```
```hint Line up the arms
Reshape the cells so each stratum has its treatment and control values side by side. Then the effect and the weight are both indexed by stratum and combine directly.
```
### Constraints & Assumptions
- Each stratum has exactly one `'control'` row and one `'treatment'` row.
- `n` is the positive number of units in that arm-stratum cell.
- `mean_y` is the outcome mean over those units.
### What a Strong Answer Covers
- Aligns the treatment and control means within each stratum before subtracting, with the sign treatment minus control.
- Builds each stratum's weight from both arms' counts over the grand total, so the weights sum to 1.
- Combines effects and weights on matching stratum labels and returns an unrounded float.
- Handles or flags malformed input, such as a stratum missing an arm or a duplicated cell.
- Explains when post-stratification gives a different answer from the pooled difference in means.
### Follow-up Questions
- How does post-stratification differ from a simple pooled difference in means, and when do the two disagree?
- How would you also return a standard error for the estimate?
- Should the weights come from the experiment's own sample or from known population shares, and what does each choice estimate?
Overview: Implement a pandas function that turns per-stratum treatment and control means and counts into a post-stratified average treatment effect. It tests reshaping arm-level rows, building population-share weights, and knowing when a pooled difference in means gives the wrong answer.
Implement def post_stratified_ate(df: pd.DataFrame) -> float in solution.py. Each row is one arm-stratum cell with columns 'stratum', 'arm' ('control' or 'treatment'), 'mean_y' and 'n'. Return ∑swsds, where ds is mean_y[treatment] - mean_y[control] within stratum s.
Requirements:
ws
is
(n_control + n_treatment in s) / (grand total units across all strata)
, so the weights sum to 1.
The effect sign is treatment minus control.
Return an unrounded
float
.
Example:
A: control mean 10, treatment mean 12, n = 100 in each arm
B: control mean 20, treatment mean 23, n = 200 in each arm
C: control mean 5, treatment mean 6, n = 50 in each arm
weights: A = 2/7, B = 4/7, C = 1/7
effects: A = 2, B = 3, C = 1
ATE = 2/7 * 2 + 4/7 * 3 + 1/7 * 1
Output: 2.4285714285714284
Constraints & Assumptions
Each stratum has exactly one
'control'
row and one
'treatment'
row.
n
is the positive number of units in that arm-stratum cell.
mean_y
is the outcome mean over those units.
What a Strong Answer Covers Guidance
Aligns the treatment and control means within each stratum before subtracting, with the sign treatment minus control.
Builds each stratum's weight from both arms' counts over the grand total, so the weights sum to 1.
Combines effects and weights on matching stratum labels and returns an unrounded float.
Handles or flags malformed input, such as a stratum missing an arm or a duplicated cell.
Explains when post-stratification gives a different answer from the pooled difference in means.
Follow-up Questions Guidance
How does post-stratification differ from a simple pooled difference in means, and when do the two disagree?
How would you also return a standard error for the estimate?
Should the weights come from the experiment's own sample or from known population shares, and what does each choice estimate?