Ride Service Demand Under Weather Conditions
Arrays & HashingAuto-graded · 19 test casesLast updated
Apply the Law of Total Probability and Bayes' Theorem to compute ride request likelihoods and conditional weather probabilities.
Problem
A transportation hub serves two types of commuters: those who typically use ride services and those who typically use public transit. Weather conditions influence whether commuters will actually request a ride on any given day.
You are given:
- The probability that any day is rainy versus clear
- The proportion of commuters who prefer ride services versus public transit
- For rainy days: the fraction of ride-preferring commuters who request a ride, and the fraction of transit-preferring commuters who switch to requesting a ride
- For clear days: the fraction of ride-preferring commuters who request a ride, and the fraction of transit-preferring commuters who switch to requesting a ride
Your task is to compute three quantities:
- Total request probability: The overall likelihood that a randomly selected commuter will request a ride on an arbitrary day
- Weather posterior: Given that a commuter requested a ride, the probability that the day is rainy
- Expected indicator: The expected value of an indicator variable X where X=1 if a commuter requests a ride and X=0 otherwise
Assume that commuter preference (ride service vs. transit) is independent of weather conditions.
Return a dictionary with keys "total_probability", "rain_given_lyft", and "expected_value", each rounded to four decimal places.
Constraints
- 0 ≤ rain_prob ≤ 1
- 0 ≤ lyft_pref_prob ≤ 1
- 0 ≤ all conditional request/switch probabilities ≤ 1
- If the total request probability is zero, return 0.0 for the weather posterior
- All output values must be rounded to exactly 4 decimal places
Examples
Example 1
Input: calculate_ride_request_probabilities(0.25, 0.6, 0.8, 0.2, 0.3, 0.05)
Output: {'total_probability': 0.29, 'rain_given_lyft': 0.4828, 'expected_value': 0.29}
Example 2
Input: calculate_ride_request_probabilities(0, 0.6, 0.8, 0.2, 0.3, 0.05)
Output: {'total_probability': 0.2, 'rain_given_lyft': 0, 'expected_value': 0.2}
Example 3
Input: calculate_ride_request_probabilities(1, 0.6, 0.8, 0.2, 0.3, 0.05)
Output: {'total_probability': 0.56, 'rain_given_lyft': 1, 'expected_value': 0.56}
Hints
Hint 1
Use the Law of Total Probability by partitioning over both weather conditions and commuter preferences to find the overall request rate
Hint 2
Apply Bayes' Theorem to invert the conditional: multiply the likelihood of a request given rain by the prior probability of rain, then divide by the total request probability
Hint 3
The expected value of a binary indicator variable equals the probability that the variable is 1
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires applying fundamental probability rules over a partitioned sample space.
Part 1: Total request probability
We partition the sample space by both weather (rainy or clear) and commuter preference (ride service or transit). The Law of Total Probability states that we sum the probability of a request across all four combinations, weighting each by the joint probability of that combination occurring.
For each weather-preference pair:
- Compute the joint probability (weather probability × preference probability, using independence)
- Multiply by the conditional request probability for that scenario
- Sum across all four scenarios
We can also compute this in two stages: first find the request probability conditional on each weather state by marginalizing over preferences, then marginalize over weather states.
Part 2: Weather posterior given a request
Once we know a request occurred, we use Bayes' Theorem to invert the conditional probability. The posterior probability that it's raining given a request equals:
P(rain | request) = P(request | rain) × P(rain) / P(request)
The numerator is the probability of both rain and a request occurring together. The denominator normalizes this to account for all ways a request can occur.
Part 3: Expected value of indicator
For a binary indicator variable X that equals 1 when an event occurs and 0 otherwise, the expected value is simply the probability of the event: E[X] = P(X=1). In this case, that's the total request probability we already computed.
Time Complexity: O(1) — fixed number of arithmetic operations
Space Complexity: O(1) — constant space for intermediate values
from typing import Dict
def calculate_ride_request_probabilities(
rain_prob: float,
lyft_pref_prob: float,
rainy_lyft_request_prob: float,
rainy_transit_switch_prob: float,
clear_lyft_request_prob: float,
clear_transit_switch_prob: float
) -> Dict[str, float]:
clear_prob = 1.0 - rain_prob
transit_pref_prob = 1.0 - lyft_pref_prob
rainy_request_prob = (
lyft_pref_prob * rainy_lyft_request_prob
+ transit_pref_prob * rainy_transit_switch_prob
)
clear_request_prob = (
lyft_pref_prob * clear_lyft_request_prob
+ transit_pref_prob * clear_transit_switch_prob
)
total_request_prob = (
rain_prob * rainy_request_prob
+ clear_prob * clear_request_prob
)
if total_request_prob == 0.0:
rain_given_request = 0.0
else:
rain_given_request = rain_prob * rainy_request_prob / total_request_prob
return {
"total_probability": round(total_request_prob, 4),
"rain_given_lyft": round(rain_given_request, 4),
"expected_value": round(total_request_prob, 4),
}