KatInterview

Calculate Welch's T-Test P-Value for Experiment Rides

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashingstatisticsdata-processinghypothesis-testing

Last updated

Process user experiment assignments and ride telemetry data to calculate a Welch's two-sample t-test p-value under strict filtering rules.

You are tasked with evaluating an A/B test for a new dispatch routing algorithm designed to reduce wait times (time to pickup) for riders.

Given a list of user variant assignments (users) and a list of ride events (rides), write a function welch_t_test_ttp_p_value(users, rides) to process the data and compute the two-sided Welch's t-test p-value comparing the treatment and control groups' pickup times.

Data Cleaning and Filtering Rules

  1. User Assignment:

    • Each dictionary in users contains user_id and variant ("treatment" or "control").
    • Rides belonging to users without a valid assignment in users must be ignored.
  2. Ride Filtering:

    • City Filter: Keep only rides where city is exactly "San Francisco".
    • Date Filter: Keep only rides where ride_date falls in July 2024 (inclusive of 2024-07-01 up to but not including 2024-08-01). The ride_date value can be a string formatted as "YYYY-MM-DD...", a datetime.date, or a datetime.datetime object.
    • Value Cleaning: Ignore rides where time_to_pickup is None or not a finite number (NaN or infinite). Convert valid numeric values to standard floats.
  3. Welch's Two-Sample T-Test:

    • Let $a$ be the sequence of pickup times for the treatment group and $b$ for the control group.
    • If either group has fewer than 2 valid data points ($\text{len}(a) < 2$ or $\text{len}(b) < 2$), return 1.0.
    • Calculate group sample means ($\bar{x}_a, \bar{x}_b$) and sample variances ($s_a^2, s_b^2$) with sample size $N_a - 1$ and $N_b - 1$ degrees of freedom.
    • Compute standard error squared: $SE^2 = \frac{s_a^2}{N_a} + \frac{s_b^2}{N_b}$.
    • If $SE^2 == 0$: return 1.0 if $\bar{x}_a == \bar{x}_b$, otherwise 0.0.
    • The t-statistic magnitude is $t = \frac{|\bar{x}_a - \bar{x}_b|}{\sqrt{SE^2}}$.
    • The Welch-Satterthwaite degrees of freedom $\nu$ is: $$\nu = \frac{(SE^2)^2}{\frac{(s_a^2 / N_a)^2}{N_a - 1} + \frac{(s_b^2 / N_b)^2}{N_b - 1}}$$
    • Return the two-tailed p-value corresponding to $t$ and $\nu$.

Constraints

  • $0 \le \text{len}(users) \le 10,000$
  • $0 \le \text{len}(rides) \le 50,000$
  • Floating point results should be computed using standard double-precision arithmetic.

Examples

Example 1

Input: welch_t_test_ttp_p_value([], [])

Output: 1

Example 2

Input: welch_t_test_ttp_p_value([{'user_id': 'u1', 'variant': 'control'}, {'user_id': 'u2', 'variant': 'control'}, {'user_id': 'u3', 'variant': 'treatment'}, {'user_id': 'u4', 'variant': 'treatment'}], [{'ride_id': 'r1', 'user_id': 'u1', 'ride_date': '2024-07-03', 'city': 'San Francisco', 'time_to_pickup': 8}, {'ride_id': 'r2', 'user_id': 'u2', 'ride_date': '2024-07-05', 'city': 'San Francisco', 'time_to_pickup': 10}, {'ride_id': 'r3', 'user_id': 'u3', 'ride_date': '2024-07-04', 'city': 'San Francisco', 'time_to_pickup': 5}, {'ride_id': 'r4', 'user_id': 'u4', 'ride_date': '2024-07-06', 'city': 'San Francisco', 'time_to_pickup': 7}])

Output: 0.16794970566215622

Hints

Hint 1

Map each user_id to their respective variant first so ride filtering can quickly check user eligibility.

Hint 2

Be careful when parsing dates: handle string dates, datetime.date, and datetime.datetime objects safely.

Hint 3

Remember that sample variance divides by (n - 1). Check edge cases where variances or standard errors equal zero.