KatInterview

Probability Toolkit: Gamble Moments, Dice, Arrivals, Odds, Draws

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashingprobabilityexpected-valueconditional-probability

Last 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_payoff
  • variance = 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 / d
  • edge = p - implied_probability
  • expected_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) when b > 0, otherwise 0.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 is 0.0.
  • If draws == deck_size, the value is 1.0 if ace_count > 0, else 0.0.
  • Otherwise, ace_probability = 1 - C(deck_size - ace_count, draws) / C(deck_size, draws), where C is 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 <= 1 and 0 <= true_probability <= 1
  • win_payoff, lose_payoff are real numbers (may be negative)
  • 2 <= dice_sum_threshold is an integer; values above 12 simply produce no qualifying outcomes
  • target_face is an integer in 1..6
  • arrival_rate_per_minute >= 0, interval_seconds >= 0
  • decimal_odds > 0 (decimal odds of 1 or less give kelly_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.