O2Medium★ Metric Drop & Causal AttributionPart O · BA-to-Python Data Science & Stats Kernels

Simpson's Paradox Detector & Kitagawa Funnel Decomposition

⚡Flashcard Drill➔

Decompose aggregate KPI shifts exactly into pure within-segment Rate Effect and traffic Mix-Shift Effect using symmetric Kitagawa-Blinder weights.

⏱️ Time: O(K)
💾 Space: O(K)
🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
def kitagawa_blinder_decomposition(conv_a: np.ndarray, total_a: np.ndarray, conv_b: np.ndarray, total_b: np.ndarray) -> dict:
n_a, n_b = float(total_a.sum()), float(total_b.sum())
w_a, w_b = total_a / max(n_a, 1e-12), total_b / max(n_b, 1e-12)
p_a =
p_b = np.where(total_b > 0, conv_b / total_b, 0.0)
total_diff = float(conv_b.sum() / n_b - conv_a.sum() / n_a)
rate_effect = float(np.sum(0.5 * (w_a + w_b) * (p_b - p_a)))
mix_effect = float(np.sum((w_b - w_a) * 0.5 * (p_a + p_b)))
paradox =
return {"total_diff": total_diff, "rate_effect": rate_effect, "mix_effect": mix_effect, "simpsons_paradox": paradox}
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: How do you explain overall CVR dropping while both iOS and Android CVR improved?
Q2: How does Kitagawa scale when investigating 20 candidate slicing dimensions?
Next: CUPED A/B Variance Reduction with Pooled Covariate Control →
💡

Core Mental Anchor / Mnemonic

Midpoint weights yield Rate, midpoint rates yield Mix, zero residual

📐 Mathematical Derivation & Core Formula

$$\Delta R_{\text{total}} = \sum_k \bar{w}_k (p_{B,k} - p_{A,k}) + \sum_k \Delta w_k \left(\frac{p_{A,k}+p_{B,k}}{2}\right)$$
Using symmetric midpoint weights wˉk=(wA,k+wB,k)/2\bar{w}_k = (w_{A,k}+w_{B,k})/2 and pˉk=(pA,k+pB,k)/2\bar{p}_k = (p_{A,k}+p_{B,k})/2, any aggregate rate change satisfies ΔR=∑wˉkΔpk+∑Δwkpˉk\Delta R = \sum \bar{w}_k \Delta p_k + \sum \Delta w_k \bar{p}_k identically with zero interaction remainder.

🔄 Tensor Dimensions & Shape Flow

conv_a/b, total_a/b: (K segments,) -> exact additive scalars

🛡️ Numerical Stability & Pitfalls Checklist

  • Assert rate_effect + mix_effect equals total_diff within floating tolerance 1e-9
  • Prefer symmetric Kitagawa midpoint weights over one-sided Oaxaca decomposition to stay order-invariant
  • Flag Simpson's paradox whenever every subgroup moves opposite to the aggregate headline metric

💻 Reference Code Implementation

import numpy as np

def kitagawa_blinder_decomposition(conv_a: np.ndarray, total_a: np.ndarray, conv_b: np.ndarray, total_b: np.ndarray) -> dict:
    n_a, n_b = float(total_a.sum()), float(total_b.sum())
    w_a, w_b = total_a / max(n_a, 1e-12), total_b / max(n_b, 1e-12)
    p_a = np.where(total_a > 0, conv_a / total_a, 0.0)
    p_b = np.where(total_b > 0, conv_b / total_b, 0.0)
    total_diff = float(conv_b.sum() / n_b - conv_a.sum() / n_a)
    rate_effect = float(np.sum(0.5 * (w_a + w_b) * (p_b - p_a)))
    mix_effect = float(np.sum((w_b - w_a) * 0.5 * (p_a + p_b)))
    paradox = bool(total_diff * rate_effect < 0 and np.all((p_b - p_a) * total_diff <= 0))
    return {"total_diff": total_diff, "rate_effect": rate_effect, "mix_effect": mix_effect, "simpsons_paradox": paradox}

🧪 Runnable Assertions & Validation

Copy and run directly in Python / Jupyter:

ca = np.array([90.0, 100.0]); ta = np.array([100.0, 1000.0])
cb = np.array([85.0, 880.0]); tb = np.array([100.0, 1000.0])
ca2 = np.array([900.0, 10.0]); ta2 = np.array([1000.0, 100.0])
cb2 = np.array([92.0, 200.0]); tb2 = np.array([100.0, 1000.0])
res = kitagawa_blinder_decomposition(ca2, ta2, cb2, tb2)
assert abs((res['rate_effect'] + res['mix_effect']) - res['total_diff']) < 1e-9
assert res['rate_effect'] > 0 and res['total_diff'] < 0 and res['simpsons_paradox'] is True
print('O2 PASS')

🎯 Core Follow-up Inquiries & Reference Answers

Q1:How do you explain overall CVR dropping while both iOS and Android CVR improved?
Traffic composition shifted heavily toward the lower-converting segment (Mix Effect < 0), overpowering genuine per-platform conversion gains (Rate Effect > 0).
Q2:How does Kitagawa scale when investigating 20 candidate slicing dimensions?
Score each dimension by |Mix| vs |Rate| contribution magnitude and inspect the top segments explaining >80% of aggregate variance.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
← Prev: Vectorized Cumulative Cohort ARPU Matrix (SQL to NumPy)Back to Part O & All 105Next: CUPED A/B Variance Reduction with Pooled Covariate Control →