Estimate the Interaction Effect in a 2x2 Factorial Experiment With OLS
Quick Overview
Implement a Python function that fits a 2x2 factorial OLS model with statsmodels and reports the two factor coefficients, the interaction and a synergy flag. It tests how dummy-coded coefficients map to cell means, how to read an interaction p-value, and why factorial designs beat separate A/B tests for combined changes.
Estimate the Interaction Effect in a 2x2 Factorial Experiment With OLS
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `factorial_interaction(df, alpha=0.05)` for a 2x2 factorial experiment. `df` has columns `A` and `B` (integer 0/1 factors) and `y` (a float outcome). Fit the full factorial OLS model and return the main effects, the interaction, and whether the two features are synergistic.
Requirements:
- Fit with `statsmodels.formula.api.ols("y ~ A * B", df).fit()` (intercept, `A`, `B` and the interaction).
- `main_a` and `main_b` (float) are the coefficients on `A` and `B`.
- `interaction` (float) is `.params["A:B"]`.
- `interaction_p` (float) is the two-sided p-value from `.pvalues["A:B"]`.
- `synergy` (bool) is `interaction_p <= alpha and interaction > 0`.
Return a dict with exactly these five keys.
Example, for data whose cell means of `y` are 10.0 at (A=0, B=0), 11.2 at (A=1, B=0), 10.8 at (A=0, B=1) and 12.9 at (A=1, B=1):
```python
res = factorial_interaction(df, alpha=0.05)
# {
# 'main_a': 1.2,
# 'main_b': 0.8,
# 'interaction': 0.9,
# 'interaction_p': 0.01,
# 'synergy': True
# }
```
The p-value depends on the noise within each cell and on the sample size. Here 0.01 stands in for a significant result.
```hint Write out the cell means
With A and B coded 0/1, write each of the four cell means in terms of the model's coefficients. That tells you what the `A` coefficient measures once `A:B` is in the model.
```
```hint Check the types
Values read from `.params` and `.pvalues` are NumPy scalars, and comparisons on them return NumPy booleans. The contract asks for plain `float` and `bool`.
```
### Constraints & Assumptions
- `df` is well-formed with all four cells populated; input validation is out of scope.
- `A` and `B` are stored as the integers 0 and 1, so the formula treats them as numeric columns.
### What a Strong Answer Covers
- The terms `A * B` expands to and how each coefficient maps to the four cell means
- What the `A` and `B` coefficients measure under 0/1 coding once `A:B` is in the model, and how that differs from an average effect
- The synergy rule, including the sign check on top of the two-sided p-value
- Returning exactly the five keys as plain Python types
- The assumptions behind the OLS p-value
### Follow-up Questions
- How would you extend this to a 2x2x2 design with three factors?
- Why not just run two separate one-factor A/B tests instead of a factorial model?
- How would you get the average effect of A across both levels of B from a single fit?
- Why does detecting an interaction need much more traffic than detecting a main effect of the same size?
Overview: Implement a Python function that fits a 2x2 factorial OLS model with statsmodels and reports the two factor coefficients, the interaction and a synergy flag. It tests how dummy-coded coefficients map to cell means, how to read an interaction p-value, and why factorial designs beat separate A/B tests for combined changes.
Implement factorial_interaction(df, alpha=0.05) for a 2x2 factorial experiment. df has columns A and B (integer 0/1 factors) and y (a float outcome). Fit the full factorial OLS model and return the main effects, the interaction, and whether the two features are synergistic.
Requirements:
Fit with
statsmodels.formula.api.ols("y ~ A * B", df).fit()
(intercept,
A
,
B
and the interaction).
main_a
and
main_b
(float) are the coefficients on
A
and
B
.
interaction
(float) is
.params["A:B"]
.
interaction_p
(float) is the two-sided p-value from
.pvalues["A:B"]
.
synergy
(bool) is
interaction_p <= alpha and interaction > 0
.
Return a dict with exactly these five keys.
Example, for data whose cell means of y are 10.0 at (A=0, B=0), 11.2 at (A=1, B=0), 10.8 at (A=0, B=1) and 12.9 at (A=1, B=1):