Decompose a change in an overall mean into per-segment mix and rate effects
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `mix_shift_decomposition(df, seg, metric, period, base_period, curr_period)`. It filters `df` on the `period` column to the rows of `base_period` and `curr_period`, then decomposes the change in the plain (unweighted, one value per row) overall mean of `metric` into a MIX effect and a RATE effect for each segment. Return one row per value of the `seg` column.
The output must follow these rules:
- Columns, in this order: `segment` (object), then `base_share`, `curr_share`, `base_mean`, `curr_mean`, `mix_effect`, `rate_effect`, `total_effect` (all float64).
- `base_share` and `curr_share` are the segment's share of rows within that period. `base_mean` and `curr_mean` are the segment's mean of `metric` in that period.
- `mix_effect = (curr_share - base_share) * base_mean`, `rate_effect = curr_share * (curr_mean - base_mean)` and `total_effect = mix_effect + rate_effect`.
- The `total_effect` column must sum to `curr_overall_mean - base_overall_mean` (checked with `np.isclose`).
- Sort by `total_effect` ascending, then `segment` ascending, then call `reset_index(drop=True)`.
- Every segment appears in both periods, so no fill or reindex is needed.
- The result is compared with `assert_frame_equal`, so column order, dtypes and the index must match exactly.
For example:
```python
df = pd.DataFrame({
"quarter": ["Q1"] * 10 + ["Q2"] * 10,
"device": ["desktop"] * 6 + ["mobile"] * 4 + ["desktop"] * 2 + ["mobile"] * 8,
"order_value": [8, 12, 9, 11, 10, 10, 4, 6, 5, 5,
10, 12, 5, 7, 6, 6, 4, 8, 6, 6],
})
mix_shift_decomposition(df, "device", "order_value", "quarter", "Q1", "Q2")
```
returns (floats shown rounded):
```text
segment base_share curr_share base_mean curr_mean mix_effect rate_effect total_effect
0 desktop 0.6 0.2 10.0 11.0 -4.0 0.2 -3.8
1 mobile 0.4 0.8 5.0 6.0 2.0 0.8 2.8
```
The overall mean falls from 8.0 in Q1 to 7.0 in Q2, and the two `total_effect` values sum to that change of -1.0. Both devices raised their mean order value by 1, so the drop comes entirely from rows moving from desktop to the cheaper mobile segment.
```hint Rewrite the overall mean
The plain mean of a period can be written as a weighted sum of the segment means. Work out what the weights are, and the identity follows from there.
```
```hint Shares come from each period
Each share uses the row count of its own period as the denominator. Check that your base shares and current shares each sum to 1.
```
### Clarifying Questions to Ask
- Can `metric` contain missing values, and if so should those rows still count toward a segment's share?
- What should the function do if one of the two periods has no rows?
- Should segment labels keep their original values in the output, or be converted to strings?
### What a Strong Answer Covers
- Filters to the two periods and computes each segment's share and mean within its own period.
- Applies the given mix, rate and total formulas with the segments aligned across periods.
- Shows why the total effects sum exactly to the change in the overall mean.
- Returns the exact column order, dtypes, sort order and index.
- Interprets the output, including the case where every segment improves while the overall mean falls.
### Follow-up Questions
- Why does the identity `sum(total_effect) == curr_overall_mean - base_overall_mean` hold?
- This split depends on which period anchors each term. What is a symmetric alternative?
- How would you extend this to segments that appear in only one period?
- A segment that gains share always gets a positive mix effect when its mean is positive, even if it is the weakest segment. How would you make the per-segment mix effect easier to read?
Overview: Write a pandas function that splits the change in an overall mean between two periods into per-segment mix and rate effects that add up to the total change. It tests groupby aggregation, share-weighted means, Simpson's paradox and exact output formatting.