Two-Segment Rider Model: Weekly Activity, Posterior, Forecast
Math & Bit ManipulationAuto-graded · 21 test casesLast 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_dedicatedof all new commuters. On any weekday, a Dedicated commuter takes a morning ride with probabilityp_ride_dedicated. - Occasional commuters make up the remaining fraction
1 - p_dedicated. On any weekday, an Occasional commuter takes a morning ride with probabilityp_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:
-
Weekly activity probability. The probability that a randomly chosen new commuter takes at least one ride during a workweek of
workweek_daysdays. Compute it per group using the complement (no rides at all), then combine the groups weighted by their population shares. -
Posterior for the Dedicated group. Suppose a new commuter is observed for
observed_daysdays 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 exactly0.0, return0.0for this value. -
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 <= 10 <= p_ride_dedicated <= 1and0 <= p_ride_occasional <= 1workweek_days >= 0andobserved_days >= 0are 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a two-component mixture. The hidden group fixes the daily ride probability, and days are independent only once the group is known. So each part is solved inside each group first, and then the groups are combined.
1. Weekly activity. For a group with daily ride probability p, the chance of at least one ride in n days is 1 - (1 - p)^n. Averaging over groups with the law of total probability gives
1 - (share_D * (1 - p_D)^n + share_O * (1 - p_O)^n).
Using a single blended daily rate instead would be wrong, since days are not independent across the whole population.
2. Posterior. Observing a ride on all k days has likelihood p^k in each group. Bayes' theorem gives
P(D | all rides) = share_D * p_D^k / (share_D * p_D^k + share_O * p_O^k).
If the denominator is exactly zero, the function returns 0.0.
3. Next-day forecast. Weight each group's daily ride probability by its posterior probability:
posterior * p_D + (1 - posterior) * p_O.
Why it is correct
Conditioning on the group restores independence, so every within-group quantity is a simple power. The law of total probability and Bayes' theorem then combine or invert those group-level quantities exactly. The forecast uses the updated beliefs rather than the prior, because the observed rides are evidence about the group.
Complexity
Time is O(log n) for the exponentiations (effectively constant), and space is O(1).
Reference solution
from typing import List
def rider_segment_probabilities(p_dedicated: float, p_ride_dedicated: float, p_ride_occasional: float, workweek_days: int, observed_days: int) -> List[float]:
p_occasional = 1.0 - p_dedicated
# P(at least one ride) = 1 - P(no rides), averaged over groups.
weekly_activity = 1.0 - (
p_dedicated * (1.0 - p_ride_dedicated) ** workweek_days
+ p_occasional * (1.0 - p_ride_occasional) ** workweek_days
)
# Bayes: posterior of Dedicated after riding on every observed day.
lik_dedicated = p_ride_dedicated ** observed_days
lik_occasional = p_ride_occasional ** observed_days
evidence = p_dedicated * lik_dedicated + p_occasional * lik_occasional
if evidence != 0.0:
posterior = p_dedicated * lik_dedicated / evidence
else:
posterior = 0.0
# Posterior predictive for the next day.
next_day = posterior * p_ride_dedicated + (1.0 - posterior) * p_ride_occasional
return [weekly_activity, posterior, next_day]