KatInterview

Two-Segment Rider Model: Weekly Activity, Posterior, Forecast

Math & Bit ManipulationAuto-graded · 21 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitybayesmixture-model

Last updated

Compute three probabilities from a two-group mixture model: at-least-one-success over n days, a Bayesian posterior after all-success streaks, and a posterior predictive forecast.

Two-Segment Rider Model

A ride-hailing service sorts every new commuter into one of two hidden groups:

  • Dedicated commuters make up a fraction p_dedicated of all new commuters. On any weekday, a Dedicated commuter takes a morning ride with probability p_ride_dedicated.
  • Occasional commuters make up the remaining fraction 1 - p_dedicated. On any weekday, an Occasional commuter takes a morning ride with probability p_ride_occasional.

Once the group of a commuter is fixed, that commuter's decisions on different days are independent and identically distributed. Across the whole population, days are not independent, because a ride on one day carries information about which group the commuter is in.

Task

Implement a function that returns a list of three floats, in this order:

  1. Weekly activity probability. The probability that a randomly chosen new commuter takes at least one ride during a workweek of workweek_days days. Compute it per group using the complement (no rides at all), then combine the groups weighted by their population shares.

  2. Posterior for the Dedicated group. Suppose a new commuter is observed for observed_days days and rode on every one of them. Return the probability, given this observation, that the commuter is Dedicated (Bayes' theorem). If the overall probability of the observation (the normalizing denominator) is exactly 0.0, return 0.0 for this value.

  3. Next-day ride probability. For the same commuter, return the probability of riding on the following day, using the posterior group probabilities from item 2 as the weights for the two groups' daily ride probabilities.

Return [weekly_activity, posterior_dedicated, next_day_probability].

Do not round the results. Compute them with ordinary floating-point arithmetic, using the formulas in the order described above (for example, the weekly activity is 1 - (share_D * (1 - p_D)^n + share_O * (1 - p_O)^n)), so that results match the expected values.

Constraints

  • 0 <= p_dedicated <= 1
  • 0 <= p_ride_dedicated <= 1 and 0 <= p_ride_occasional <= 1
  • workweek_days >= 0 and observed_days >= 0 are integers (small, such as at most a few dozen)
  • All inputs are plain Python numbers; the output is a list of three floats
  • Results are compared against expected values with floating-point tolerance

Examples

Example 1

Input: rider_segment_probabilities(0.25, 0.8, 0.2, 5, 3)

Output: [0.7541599999999999, 0.9552238805970149, 0.7731343283582089]

Example 2

Input: rider_segment_probabilities(0.5, 0.6, 0.4, 5, 3)

Output: [0.956, 0.7714285714285714, 0.5542857142857143]

Example 3

Input: rider_segment_probabilities(0.1, 0.9, 0.1, 5, 3)

Output: [0.4685579999999999, 0.9878048780487805, 0.8902439024390244]

Hints

Hint 1

Within a single group the days are independent, so 'at least one ride in n days' is one minus the chance of zero rides, which is (1 - p)^n for that group. Then average over groups with the population shares.

Hint 2

Do not blend the two ride probabilities into one population-wide rate and then use the complement; that wrongly treats days as independent across the whole population.

Hint 3

For the posterior, the likelihood of riding every observed day is p^k per group. Multiply by the group share, then divide by the sum of both products. For the forecast, reuse that posterior as the group weights.