Probability Toolkit: Gamble Moments, Dice, Arrivals, Odds, Draws
Arrays & HashingAuto-graded · 20 test casesLast updated
Compute expected value and variance, a dice conditional probability, a Poisson tail, betting edge with Kelly sizing, and a without-replacement card probability in one function.
Problem
Write a function that bundles five standard probability calculations and returns them in a dictionary. Each part is independent of the others, and every result must be a float.
1. Gamble moments. A gamble pays win_payoff with probability win_probability, and pays lose_payoff otherwise. Let lose_probability = 1.0 - win_probability.
expected_value = win_probability * win_payoff + lose_probability * lose_payoffvariance = win_probability * (win_payoff - expected_value)**2 + lose_probability * (lose_payoff - expected_value)**2
2. Conditional dice probability. Two fair six-sided dice are rolled. Consider only the 36 ordered outcomes (first, second) whose total is at least dice_sum_threshold. Let conditional_probability be the fraction of those qualifying outcomes in which at least one die shows target_face. If no outcome qualifies, the value is 0.0.
3. Poisson tail. Arrivals occur at an average rate of arrival_rate_per_minute per minute. Over a window of interval_seconds seconds, the count is Poisson with mean mu = arrival_rate_per_minute * interval_seconds / 60.0. Return poisson_probability_at_least_two = P(X >= 2).
4. Betting odds. Let d = decimal_odds and p = true_probability.
implied_probability = 1 / dedge = p - implied_probabilityexpected_return = p * d - 1(expected profit per unit staked)b = d - 1(net profit per unit staked on a win)kelly_fraction = max(0, (b * p - (1 - p)) / b)whenb > 0, otherwise0.0
5. Draws without replacement. A deck has deck_size cards, of which ace_count are special "ace" cards. Draw draws cards without replacement. Let ace_probability be the probability that at least one ace is drawn:
- If
draws == 0, the value is0.0. - If
draws == deck_size, the value is1.0iface_count > 0, else0.0. - Otherwise,
ace_probability = 1 - C(deck_size - ace_count, draws) / C(deck_size, draws), whereCis the binomial coefficient.
Return a dictionary with exactly these keys, mapped to float values:
expected_value, variance, conditional_probability, poisson_probability_at_least_two, implied_probability, edge, expected_return, kelly_fraction, ace_probability.
Constraints
0 <= win_probability <= 1and0 <= true_probability <= 1win_payoff,lose_payoffare real numbers (may be negative)2 <= dice_sum_thresholdis an integer; values above 12 simply produce no qualifying outcomestarget_faceis an integer in1..6arrival_rate_per_minute >= 0,interval_seconds >= 0decimal_odds > 0(decimal odds of 1 or less givekelly_fraction = 0.0)0 <= draws <= deck_size,0 <= ace_count <= deck_size,deck_size >= 1- Floating-point answers are compared with a small absolute tolerance.
Examples
Example 1
Input: probability_toolkit(10, 0.4, -6, 9, 6, 3, 20, 2.6, 0.35, 52, 4, 3)
Output: {'expected_value': 0.40000000000000036, 'variance': 61.44, 'conditional_probability': 0.7, 'poisson_probability_at_least_two': 0.26424111765711533, 'implied_probability': 0.3846153846153846, 'edge': -0.0346153846153846, 'expected_return': -0.09000000000000008, 'kelly_fraction': 0, 'ace_probability': 0.2173755656108597}
Hints
Hint 1
For the dice part, enumerate all 36 ordered pairs and count the qualifying ones separately from those that also contain the target face. Do not count unordered pairs.
Hint 2
For the card part, compute the complement: the probability that no ace is drawn is C(deck_size - ace_count, draws) / C(deck_size, draws). Handle draws == 0 explicitly.
Hint 3
For Kelly, use the net profit b = decimal_odds - 1, not the decimal odds themselves, and clamp the result at zero when it is negative or when b is not positive.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each of the five parts is a direct formula, so the function computes them one at a time and collects the results in a dictionary.
-
Gamble moments. The expected value is the probability-weighted sum of the two payoffs. The variance is the probability-weighted sum of squared deviations from that expected value. This is the definition of variance, so no shortcut is needed.
-
Dice conditional. Enumerate all 36 ordered outcomes. Count the outcomes whose sum meets the threshold, and among those count the ones containing the target face. The ratio is the conditional probability. Ordered pairs are required, because (3,6) and (6,3) are different outcomes. If nothing qualifies, return 0.0 to avoid dividing by zero.
-
Poisson tail. Convert the rate to the window length to get the mean
mu. Then P(X >= 2) = 1 - e^(-mu)(1 + mu), since the probability of 0 arrivals is e^(-mu) and of exactly 1 arrival is mu·e^(-mu). -
Betting. The implied probability is 1/d. The edge is the true probability minus the implied one. The expected return per unit is p·d - 1. For Kelly, the net profit b = d - 1 appears in the formula f* = (b·p - q)/b. A negative result means "do not bet," so clamp it to zero. When b ≤ 0 there is no meaningful bet, so return 0.0.
-
Cards. The complement is easier. The probability that no ace appears in
drawscards is C(non-aces, draws) / C(deck, draws). Subtract that from 1. The special cases aredraws == 0, which gives 0.0, anddraws == deck_size, where the answer is certain if any aces exist.
Why it is correct
Every formula is the standard definition or a direct consequence of it. The complement identity in part 5 is exact, because "at least one ace" is the negation of "no aces." The Poisson tail follows from the Poisson PMF. The Kelly formula is the standard result for a binary bet, and clamping at zero gives the correct decision when the edge is negative.
Complexity
- Time: O(1). The dice enumeration is over a fixed 36 outcomes, and the binomial coefficients involve only the given integers.
- Space: O(1) beyond the returned dictionary.