Probability, Betting Edge, and Arrival Metrics

Medium · Statistics & Math · Software Engineer · Technical Screen

Problem Statement

Implement compute_probability_statistics to evaluate five independent probability and quantitative-finance scenarios from their parameterized inputs and return a Dict[str, float] containing nine metrics:

def compute_probability_statistics(
    win_payoff: float,
    win_probability: float,
    lose_payoff: float,
    dice_sum_threshold: int,
    target_face: int,
    arrival_rate_per_minute: float,
    interval_seconds: float,
    decimal_odds: float,
    true_probability: float,
    deck_size: int,
    ace_count: int,
    draws: int,
) -> Dict[str, float]:
  1. Binary wager moments (win_payoff, win_probability, lose_payoff): A single play yields win_payoff with probability win_probability and lose_payoff with probability 1 - win_probability. Compute its mean ("expected_value") and population variance ("variance").
  2. Conditioned two-dice roll (dice_sum_threshold, target_face): Two independent, fair six-sided dice (1 through 6) are rolled. Conditioned on their total being at least dice_sum_threshold, compute the probability that at least one die shows target_face ("conditional_probability"). If no roll meets the sum threshold, return float("nan").
  3. Poisson window count (arrival_rate_per_minute, interval_seconds): Independent events arrive according to a homogeneous Poisson process with mean rate arrival_rate_per_minute per minute. Over an observation window of interval_seconds seconds, compute the probability of observing 2 or more arrivals ("poisson_probability_at_least_two").
  4. Wagering edge and Kelly stake (decimal_odds, true_probability): A wager with true win chance true_probability pays decimal_odds total units (original 1-unit stake plus decimal_odds - 1 net profit) upon winning and loses the 1-unit stake otherwise. Compute:
    • "implied_probability": break-even win rate 1 / decimal_odds,
    • "edge": true_probability - implied_probability,
    • "expected_return": expected net profit per unit staked (true_probability * decimal_odds - 1),
    • "kelly_fraction": optimal fraction of bankroll to stake to maximize expected logarithmic wealth when non-negative bets are allowed (expected_return / (decimal_odds - 1) when expected_return > 0 and decimal_odds > 1, else 0.0).
  5. Hypergeometric complement (deck_size, ace_count, draws): From a finite deck of deck_size cards containing ace_count Aces, draws cards are dealt uniformly without replacement. Compute the probability that the hand contains at least one Ace ("ace_probability").

Examples

Example 1

Input:
  win_payoff = 15.0, win_probability = 0.3, lose_payoff = -5.0
  dice_sum_threshold = 10, target_face = 5
  arrival_rate_per_minute = 4.0, interval_seconds = 30.0
  decimal_odds = 2.5, true_probability = 0.5
  deck_size = 20, ace_count = 5, draws = 2

Output:
  {
    "expected_value": 1.0,
    "variance": 84.0,
    "conditional_probability": 0.5,
    "poisson_probability_at_least_two": 0.5939941502901619,
    "implied_probability": 0.4,
    "edge": 0.1,
    "expected_return": 0.25,
    "kelly_fraction": 0.16666666666666666,
    "ace_probability": 0.4473684210526315
  }
  • Wager: 0.3 * 15 + 0.7 * (-5) = 1.0, and 0.3 * (15 - 1)^2 + 0.7 * (-5 - 1)^2 = 84.0.
  • Dice: Six ordered rolls sum to >= 10 ((4,6), (5,5), (5,6), (6,4), (6,5), (6,6)), and three of them include a 5 (3 / 6 = 0.5).
  • Poisson: 4.0 per minute over 30 seconds gives mean 2.0, so P(X >= 2) = 1 - 3 * e^(-2) ≈ 0.5940.
  • Odds: Break-even rate is 1 / 2.5 = 0.4, edge is 0.1, expected return is 0.5 * 2.5 - 1 = 0.25, and Kelly stakes 0.25 / 1.5 = 1/6.
  • Cards: 1 - (15 / 20) * (14 / 19) = 17 / 38 ≈ 0.4474.

Example 2

Input:
  win_payoff = 8.0, win_probability = 0.25, lose_payoff = -4.0
  dice_sum_threshold = 8, target_face = 4
  arrival_rate_per_minute = 2.0, interval_seconds = 15.0
  decimal_odds = 2.0, true_probability = 0.4
  deck_size = 40, ace_count = 4, draws = 3

Output:
  {
    "expected_value": -1.0,
    "variance": 27.0,
    "conditional_probability": 0.3333333333333333,
    "poisson_probability_at_least_two": 0.09020401043104986,
    "implied_probability": 0.5,
    "edge": -0.1,
    "expected_return": -0.2,
    "kelly_fraction": 0.0,
    "ace_probability": 0.2773279352226721
  }
  • Wager: Mean is 0.25 * 8 + 0.75 * (-4) = -1.0; variance is 0.25 * 9^2 + 0.75 * (-3)^2 = 27.0.
  • Dice: 15 ordered pairs sum to >= 8, and 5 of them ((4,4), (4,5), (5,4), (4,6), (6,4)) show at least one 4 (5 / 15 = 1/3).
  • Poisson: Mean arrivals over 15 seconds at 2.0/min is 0.5, giving 1 - 1.5 * e^(-0.5) ≈ 0.0902.
  • Odds: Expected return is 0.4 * 2.0 - 1 = -0.2 <= 0, so kelly_fraction clamps to 0.0.
  • Cards: 1 - (36 / 40) * (35 / 39) * (34 / 38) = 137 / 494 ≈ 0.2773.

Constraints

  • 0.0 <= win_probability <= 1.0, -10^6 <= lose_payoff, win_payoff <= 10^6
  • target_face is an integer in [1, 6]; dice_sum_threshold is an integer (float("nan") if dice_sum_threshold > 12)
  • arrival_rate_per_minute >= 0.0, interval_seconds >= 0.0
  • decimal_odds >= 1.0, 0.0 <= true_probability <= 1.0
  • 1 <= deck_size <= 10^4, 0 <= ace_count <= deck_size, 0 <= draws <= deck_size
  • Floating-point outputs are compared within relative/absolute tolerance 10^-6.

Step-by-step walkthrough

  1. Map the five sub-problems to parameters and output keys

    This function evaluates five self-contained probability and quantitative-finance tasks and bundles their answers into a nine-key dictionary:

    Part Input parameters Output dictionary keys
    1. Binary wager win_payoff, win_probability, lose_payoff expected_value, variance
    2. Conditional dice roll dice_sum_threshold, target_face conditional_probability
    3. Arrival counts arrival_rate_per_minute, interval_seconds poisson_probability_at_least_two
    4. Wagering odds & Kelly decimal_odds, true_probability implied_probability, edge, expected_return, kelly_fraction
    5. Card draws (no replacement) deck_size, ace_count, draws ace_probability

    We will work through each calculation on Example 1 first (showing quick mental-math checks along the way), and encapsulate each group in a dedicated helper function. Two boundary conventions apply: if no two-dice outcome reaches dice_sum_threshold (such as dice_sum_threshold > 12), conditional probability is undefined so we return float("nan"); if a wager has non-positive expected return (<= 0), we pass on the bet and return kelly_fraction = 0.0.

  2. Part 1: Expected value and variance of a binary wager

    In Example 1, a single play pays win_payoff = +15.0 with probability p = 0.3, and lose_payoff = -5.0 with probability 1 - p = 0.7.

    • Expected value weights each payoff by how often it occurs: 0.3 * 15.0 + 0.7 * (-5.0) = 4.5 - 3.5 = 1.0.
    • Variance weights the squared deviation from that expected value: 0.3 * (15.0 - 1.0)^2 + 0.7 * (-5.0 - 1.0)^2 = 0.3 * 196 + 0.7 * 36 = 58.8 + 25.2 = 84.0.

    Mental-math check for any two-point random variable jumping between a and b: its variance always equals p * (1 - p) * (a - b)^2. Here 15 - (-5) = 20, so 0.3 * 0.7 * 20^2 = 0.21 * 400 = 84.0 immediately! In code, writing the definition directly keeps every step explicit:

    from typing import Tuple
    
    
    def gamble_mean_and_variance(
        win_payoff: float, win_probability: float, lose_payoff: float
    ) -> Tuple[float, float]:
        """Compute expected value and variance for a two-outcome bet."""
        lose_probability = 1.0 - win_probability
        # Expected value weights each payoff by its probability of occurring.
        expected_value = win_probability * win_payoff + lose_probability * lose_payoff
        # Variance averages the squared gap between each payoff and expected_value.
        variance = (
            win_probability * (win_payoff - expected_value) ** 2
            + lose_probability * (lose_payoff - expected_value) ** 2
        )
        return expected_value, variance
    
  3. Part 2: Conditional probability on two fair dice

    Conditional probability P(A | B) means: discard every outcome where B fails, then find the fraction of surviving outcomes where A holds. Because all 6 * 6 = 36 ordered pairs (first_die, second_die) are equally likely, we can count cells directly in a 6x6 table.

    In Example 1, B is sum >= 10 and A is at least one die shows 5:

          second_die ->
           1    2    3    4    5    6
      1 [  .    .    .    .    .    . ]
      2 [  .    .    .    .    .    . ]
      3 [  .    .    .    .    .    . ]   "."   = sum < 10 (discarded)
      4 [  .    .    .    .    .   10 ]   "10"  = sum >= 10, no 5
      5 [  .    .    .    .  10*  11* ]   "10*" = sum >= 10 AND has a 5
      6 [  .    .    .   10  11*   12 ]
    

    Exactly 3 + 2 + 1 = 6 cells satisfy sum >= 10. Among those 6 cells, 3 are starred ((5, 5), (5, 6), (6, 5)—notice (5, 5) is counted once, never twice!), giving P(has 5 | sum >= 10) = 3 / 6 = 0.5.

    def dice_conditional_probability(sum_threshold: int, target_face: int) -> float:
        """Compute P(at least one die == target_face | first_die + second_die >= sum_threshold)."""
        # Enumerate all 36 equally likely ordered pairs (first_die, second_die) in {1..6} x {1..6}.
        condition_count = 0
        favorable_count = 0
        for first_die in range(1, 7):
            for second_die in range(1, 7):
                if first_die + second_die >= sum_threshold:
                    condition_count += 1
                    # Using `or` counts (target_face, target_face) once instead of double-counting.
                    if first_die == target_face or second_die == target_face:
                        favorable_count += 1
        if condition_count == 0:
            # Conditioning on an impossible event (e.g. sum >= 13) is mathematically undefined.
            return float("nan")
        return favorable_count / condition_count
    
  4. Part 3: Arrival counts with the Poisson distribution

    When independent events arrive at a constant average rate, the count X inside a fixed window follows a Poisson distribution with parameter lam equal to the expected number of arrivals during that window:

    P(X = k) = e^(-lam) * lam^k / k!    for k = 0, 1, 2, ...
    

    Align time units first: in Example 1, 4.0 arrivals per minute across 30.0 seconds (30 / 60 = 0.5 minutes) gives lam = 4.0 * 0.5 = 2.0 expected arrivals.

    Rather than summing k = 2, 3, 4, ... indefinitely, subtract k = 0 and k = 1 from 1:

    P(X >= 2) = 1 - P(X = 0) - P(X = 1)
              = 1 - e^(-lam) - lam * e^(-lam)
              = 1 - e^(-lam) * (1 + lam)
    

    At lam = 2.0, this evaluates to 1 - 3 * e^(-2) ≈ 1 - 3 * 0.1353 = 0.5940.

    import math
    
    
    def poisson_at_least_two(rate_per_minute: float, interval_seconds: float) -> float:
        """Compute P(X >= 2) for X ~ Poisson(expected_arrivals) over interval_seconds."""
        # Convert seconds to minutes first so the rate and time window share the same unit.
        expected_arrivals = rate_per_minute * (interval_seconds / 60.0)
        # Complement rule: P(X >= 2) = 1 - P(X = 0) - P(X = 1) = 1 - e^(-lam) * (1 + lam).
        prob_zero_or_one = math.exp(-expected_arrivals) * (1.0 + expected_arrivals)
        return 1.0 - prob_zero_or_one
    
  5. Part 4: Decimal odds, edge, expected return, and Kelly sizing

    Decimal odds o = 2.5 state the gross payout returned per 1 unit risked when the wager wins: your 1-unit stake comes back plus b = o - 1 = 1.5 units of net profit, while losing forfeits the 1-unit stake.

    Tracing Example 1 (o = 2.5, p = 0.5):

    • Implied probability (1 / o): Break-even win frequency (p * o - 1 = 0), here 1 / 2.5 = 0.4.
    • Edge (p - 1 / o): Excess win probability over break-even: 0.5 - 0.4 = 0.1.
    • Expected return (p * o - 1): Mean net profit per unit staked: 0.5 * 2.5 - 1.0 = +0.25.
    • Kelly fraction: The bankroll share f maximizing expected log-growth p * ln(1 + b*f) + (1 - p) * ln(1 - f) solves f* = (b * p - (1 - p)) / b = expected_return / net_odds. For Example 1, 0.25 / 1.5 = 1 / 6 ≈ 0.1667. Whenever expected_return <= 0 (as in Example 2, where o = 2.0 and p = 0.4 give expected_return = -0.2), you cannot bet a negative stake, so kelly_fraction clamps to 0.0 (which also guards against dividing by zero at decimal_odds == 1.0).
    from typing import Tuple
    
    
    def betting_numbers(
        decimal_odds: float, true_probability: float
    ) -> Tuple[float, float, float, float]:
        """Compute implied probability, probability edge, expected return per unit, and Kelly stake."""
        # Break-even win rate implied by decimal_odds (where 1 staked returns decimal_odds total).
        implied_probability = 1.0 / decimal_odds
        edge = true_probability - implied_probability
        expected_return = true_probability * decimal_odds - 1.0
    
        # net_odds is the net profit per 1 unit staked when the bet wins (stake is returned plus profit).
        net_odds = decimal_odds - 1.0
        if expected_return <= 0.0 or net_odds <= 0.0:
            # Never bet when expected return is zero or negative (also avoids dividing by 0 at odds 1.0).
            kelly_fraction = 0.0
        else:
            # Kelly fraction f* = (net_odds * p - (1 - p)) / net_odds == expected_return / net_odds.
            kelly_fraction = expected_return / net_odds
        return implied_probability, edge, expected_return, kelly_fraction
    
  6. Part 5: Drawing at least one Ace without replacement

    "At least one Ace in draws cards" splits into 1, 2, ..., draws Aces, whereas its complement is a single chain: every dealt card is a non-Ace. Because cards are dealt without replacement, each non-Ace draw reduces both remaining non-Aces and remaining total cards by 1.

    Tracing Example 1 (deck_size = 20, ace_count = 5, non_ace_count = 15, draws = 2):

    Draw 1 (20 cards, 15 non-Aces) --15/20--> Draw 2 (19 cards, 14 non-Aces)
    
    P(zero Aces)      = (15 / 20) * (14 / 19) = (3 / 4) * (14 / 19) = 21 / 38
    P(at least 1 Ace) = 1 - 21 / 38                                 = 17 / 38 ≈ 0.4474
    

    We also guard draws > deck_size - ace_count: if more cards are dealt than the total supply of non-Aces, the pigeonhole principle guarantees at least one Ace (1.0).

    def at_least_one_ace(deck_size: int, ace_count: int, draws: int) -> float:
        """Compute P(at least one Ace) when drawing `draws` cards without replacement."""
        non_ace_count = deck_size - ace_count
        if draws > non_ace_count:
            # Pigeonhole principle: there are fewer non-aces than draws, so an Ace is guaranteed.
            return 1.0
    
        # Multiply P(draw i is a non-ace | all previous draws were non-aces) across each draw.
        no_ace_probability = 1.0
        for draw_index in range(draws):
            remaining_non_aces = non_ace_count - draw_index
            remaining_cards = deck_size - draw_index
            no_ace_probability *= remaining_non_aces / remaining_cards
        return 1.0 - no_ace_probability
    
  7. Complete solution

    Putting all five helpers together behind compute_probability_statistics:

    from typing import Any, Dict, List, Optional, Set, Tuple
    import math
    
    
    def gamble_mean_and_variance(
        win_payoff: float, win_probability: float, lose_payoff: float
    ) -> Tuple[float, float]:
        """Compute expected value and variance for a two-outcome bet."""
        lose_probability = 1.0 - win_probability
        # Expected value weights each payoff by its probability of occurring.
        expected_value = win_probability * win_payoff + lose_probability * lose_payoff
        # Variance averages the squared gap between each payoff and expected_value.
        variance = (
            win_probability * (win_payoff - expected_value) ** 2
            + lose_probability * (lose_payoff - expected_value) ** 2
        )
        return expected_value, variance
    
    
    def dice_conditional_probability(sum_threshold: int, target_face: int) -> float:
        """Compute P(at least one die == target_face | first_die + second_die >= sum_threshold)."""
        # Enumerate all 36 equally likely ordered pairs (first_die, second_die) in {1..6} x {1..6}.
        condition_count = 0
        favorable_count = 0
        for first_die in range(1, 7):
            for second_die in range(1, 7):
                if first_die + second_die >= sum_threshold:
                    condition_count += 1
                    # Using `or` counts (target_face, target_face) once instead of double-counting.
                    if first_die == target_face or second_die == target_face:
                        favorable_count += 1
        if condition_count == 0:
            # Conditioning on an impossible event (e.g. sum >= 13) is mathematically undefined.
            return float("nan")
        return favorable_count / condition_count
    
    
    def poisson_at_least_two(rate_per_minute: float, interval_seconds: float) -> float:
        """Compute P(X >= 2) for X ~ Poisson(expected_arrivals) over interval_seconds."""
        # Convert seconds to minutes first so the rate and time window share the same unit.
        expected_arrivals = rate_per_minute * (interval_seconds / 60.0)
        # Complement rule: P(X >= 2) = 1 - P(X = 0) - P(X = 1) = 1 - e^(-lam) * (1 + lam).
        prob_zero_or_one = math.exp(-expected_arrivals) * (1.0 + expected_arrivals)
        return 1.0 - prob_zero_or_one
    
    
    def betting_numbers(
        decimal_odds: float, true_probability: float
    ) -> Tuple[float, float, float, float]:
        """Compute implied probability, probability edge, expected return per unit, and Kelly stake."""
        # Break-even win rate implied by decimal_odds (where 1 staked returns decimal_odds total).
        implied_probability = 1.0 / decimal_odds
        edge = true_probability - implied_probability
        expected_return = true_probability * decimal_odds - 1.0
    
        # net_odds is the net profit per 1 unit staked when the bet wins (stake is returned plus profit).
        net_odds = decimal_odds - 1.0
        if expected_return <= 0.0 or net_odds <= 0.0:
            # Never bet when expected return is zero or negative (also avoids dividing by 0 at odds 1.0).
            kelly_fraction = 0.0
        else:
            # Kelly fraction f* = (net_odds * p - (1 - p)) / net_odds == expected_return / net_odds.
            kelly_fraction = expected_return / net_odds
        return implied_probability, edge, expected_return, kelly_fraction
    
    
    def at_least_one_ace(deck_size: int, ace_count: int, draws: int) -> float:
        """Compute P(at least one Ace) when drawing `draws` cards without replacement."""
        non_ace_count = deck_size - ace_count
        if draws > non_ace_count:
            # Pigeonhole principle: there are fewer non-aces than draws, so an Ace is guaranteed.
            return 1.0
    
        # Multiply P(draw i is a non-ace | all previous draws were non-aces) across each draw.
        no_ace_probability = 1.0
        for draw_index in range(draws):
            remaining_non_aces = non_ace_count - draw_index
            remaining_cards = deck_size - draw_index
            no_ace_probability *= remaining_non_aces / remaining_cards
        return 1.0 - no_ace_probability
    
    
    def compute_probability_statistics(win_payoff: float, win_probability: float, lose_payoff: float, dice_sum_threshold: int, target_face: int, arrival_rate_per_minute: float, interval_seconds: float, decimal_odds: float, true_probability: float, deck_size: int, ace_count: int, draws: int) -> Dict[str, float]:
        # Compute each independent probability / statistics group via its dedicated helper.
        expected_value, variance = gamble_mean_and_variance(
            win_payoff, win_probability, lose_payoff
        )
        implied_probability, edge, expected_return, kelly_fraction = betting_numbers(
            decimal_odds, true_probability
        )
        return {
            "expected_value": expected_value,
            "variance": variance,
            "conditional_probability": dice_conditional_probability(
                dice_sum_threshold, target_face
            ),
            "poisson_probability_at_least_two": poisson_at_least_two(
                arrival_rate_per_minute, interval_seconds
            ),
            "implied_probability": implied_probability,
            "edge": edge,
            "expected_return": expected_return,
            "kelly_fraction": kelly_fraction,
            "ace_probability": at_least_one_ace(deck_size, ace_count, draws),
        }
    

    ✅ Submit now — all tests should pass.

Intuition & how it works

Think of quantitative probability drills as applying three reliable mental habits: weighing outcomes by how often they occur (to measure average payoff and spread), filtering down to only the outcomes compatible with a clue (to answer "conditioned on..." questions), and evaluating the complement event (1 - P(none)) whenever "at least one" or "at least two" splits into multiple overlapping cases. Each of the five sub-problems exercises one of these building blocks:

  • Expected value is the long-run average payoff per play over infinitely many repetitions, and variance is the probability-weighted average squared distance from that mean (measuring payoff dispersion).
  • Conditional probability (P(A | B)) is the share of outcomes satisfying A after restricting the sample space strictly to outcomes satisfying B.
  • A Poisson distribution models the count of independent events inside a fixed window when arrivals occur at a constant mean rate.
  • Decimal odds quote the gross payout returned per 1 unit staked on a win; implied probability (1 / decimal_odds) is the break-even win rate, edge is true win probability minus that break-even rate, and the Kelly criterion (or Kelly fraction) is the bankroll fraction maximizing expected logarithmic wealth growth across repeated wagers.
  • Sampling without replacement means dealt cards are not returned to the deck, so each draw shrinks both numerator and denominator for subsequent draws.

Naive calculations fail whenever they ignore state changes across draws or mismatch units: treating card draws as independent (1 - (15/20)^2 instead of 1 - (15/20)*(14/19)), forgetting to convert 30 seconds into 0.5 minutes before multiplying by a per-minute rate, double-counting (5, 5) when checking whether either die shows 5, or dividing by gross odds o instead of net profit o - 1 in the Kelly formula.

Here are two quick Example 1 walk-throughs showing those ideas in action:

  • Conditioned dice (sum >= 10, target 5): Out of 36 equally likely ordered pairs, 6 reach at least 10: (4, 6), (5, 5), (5, 6), (6, 4), (6, 5), (6, 6). Exactly 3 of those 6 include a 5, giving 3 / 6 = 0.5.
  • Complement card draw (20 cards, 5 Aces, 2 draws): Instead of summing 1 Ace plus 2 Aces, compute P(no Ace) = (15 / 20) * (14 / 19) = 21 / 38, then subtract from 1 to get 17 / 38 ≈ 0.4474.

Complexity

  • Time: O(draws) (effectively O(1) for standard deck sizes). Parts 1, 3, and 4 execute a constant number of arithmetic operations in O(1) time; Part 2 always inspects the 6 * 6 = 36 ordered two-dice pairs in O(1) time; and Part 5 multiplies at most min(draws, deck_size - ace_count) fractions at O(1) cost per draw.
  • Space: O(1) auxiliary space. Every helper allocates only scalar numeric variables, and the wrapper constructs one fixed nine-key dictionary.

Edge cases

  • Deterministic wager (win_probability == 0 or 1): Expected value equals the guaranteed payoff and variance is 0.0.
  • Dice threshold <= 2: Every two-dice roll sums to at least 2, so condition_count == 36 and the answer reduces to the unconditional probability 11 / 36.
  • Dice threshold == 12 or > 12: At 12, only (6, 6) survives (1.0 for target_face == 6, 0.0 otherwise). Above 12, condition_count == 0, returning float("nan").
  • Zero arrival rate or zero-second window: expected_arrivals == 0.0, producing 1 - exp(0) * 1 = 0.0.
  • Break-even or negative-EV wagers (expected_return <= 0) and decimal_odds == 1.0: Fair odds (p == 1 / o) produce 0.0 edge, 0.0 expected return, and 0.0 Kelly stake. At decimal_odds == 1.0 (net_odds == 0), checking expected_return <= 0 or net_odds <= 0 safely returns 0.0 without dividing by zero.
  • Card draw boundaries: draws == 0 or ace_count == 0 yields 0.0; draws > deck_size - ace_count immediately yields 1.0 by the pigeonhole principle (dealing more cards than the entire supply of non-Aces guarantees hitting at least one Ace).

Common mistakes

  • Omitting - E[X]^2 when computing variance via E[X^2] - E[X]^2: On Example 1, 0.3 * 225 + 0.7 * 25 = 85.0, which forgets to subtract 1.0^2 = 1.0 to get 84.0. Evaluating E[(X - mean)^2] directly avoids dropping that term.
  • Dividing by 36 instead of condition_count: Returning 3 / 36 on Example 1 computes the joint probability P(sum >= 10 and has 5) rather than the conditional probability 3 / 6.
  • Double-counting (target_face, target_face): Adding rolls with first_die == 5 ((5, 5), (5, 6)) to rolls with second_die == 5 ((5, 5), (6, 5)) yields 4 / 6 unless the overlap (5, 5) is counted only once.
  • Skipping seconds-to-minutes conversion: Plugging lam = 4.0 directly instead of 4.0 * (30 / 60) = 2.0 inflates Example 1's Poisson answer from 0.5940 to 0.9084.
  • Dividing by gross decimal_odds instead of net profit decimal_odds - 1 in Kelly: On Example 1, 0.25 / 2.5 = 0.10 under-bets compared to the true Kelly fraction 0.25 / 1.5 = 1/6.
  • Returning a negative Kelly fraction on unfavorable odds: On Example 2 (expected_return = -0.2), the unconstrained formula gives -0.2, which must be clamped to 0.0 when you cannot take the bookmaker's side of the bet.

Follow-up questions

  • If you repeat the Example 1 wager (+15 with probability 0.3, -5 with probability 0.7) independently 100 times, what are the total expected profit, standard deviation, and approximate chance of finishing below break-even? Because both expectations and variances add across independent plays, 100 repetitions have total mean 100 * 1.0 = +100.0 and total variance 100 * 84.0 = 8400.0, giving a standard deviation (the square root of variance, measuring typical dollar spread) of sqrt(8400) ≈ 91.65. By the Central Limit Theorem (which states that the sum of many independent identically distributed variables is approximately normal), break-even (0) sits at z = (0 - 100) / 91.65 ≈ -1.09 standard deviations, giving a continuous normal approximation around 13.8% (with exact binomial P(wins <= 24) ≈ 11.4% for strictly negative profit 20 * wins - 500 < 0). Because profit grows linearly with n while standard deviation grows as sqrt(n), even a favorable wager still has a meaningful chance of being underwater after 100 plays.

  • Where do the assumptions behind a homogeneous Poisson arrival model fail on real traffic, and which models replace it? A homogeneous Poisson process assumes events arrive independently at a constant rate, forcing the count's variance to equal its mean (Var(X) = E[X]). Production request streams and market order flow violate both assumptions via daily seasonality and clustered bursts (where variance far exceeds the mean, known as overdispersion): time-varying rates are modeled with an inhomogeneous Poisson process (integrating rate(t) across the interval to obtain lam), while bursty, self-exciting, or overdispersed counts are modeled with a negative binomial distribution (a count distribution with an extra dispersion parameter letting variance exceed the mean) or a Hawkes process.

  • Why do quantitative traders often size positions at fractional Kelly (such as half-Kelly) even when a bet has positive expected return? In live markets true_probability is estimated with error rather than known exactly, and expected logarithmic growth is sharply asymmetric around f*: over-betting at 2 * f* reduces long-run compound growth all the way to zero (and exceeding 2 * f* drives long-run wealth toward ruin). Staking half-Kelly (0.5 * f*) sacrifices only 25% of the theoretical maximum compound growth rate while cutting portfolio variance by 75% (halving volatility) and providing a wide safety buffer against misestimated probabilities.

Follow-up questions

Similar questions

More Optiver interview questions

All 76 Optiver questions →