SigmaField

Practice Trading

Size a repeated bet with the Kelly criterion

Medium+25 pointsAbout 35 minutesTrading

The brief

A trading desk runs the same short-dated options trade every week. Each $1 staked ends the week in one of five ways, from a total loss to a profit of $1.20, with known chances. On average each $1 staked makes 9 cents.

The head of the desk must choose what fraction of capital to stake each week. A trader argues for staking more, since every trade has a positive expected profit. The desk wants the fraction that makes capital grow fastest, and a simulation that shows what staking more does.

Your task

  1. Write growth_rate(f, outcomes, probs): the expected log growth of wealth per bet, g(f)=E[ln⁡(1+fR)]g(f) = \mathbb{E}[\ln(1 + fR)], when a fraction ff of wealth is staked. Here RR is the profit on $1 staked. Return -math.inf when an outcome with a positive chance leaves no wealth, that is 1+fR≤01 + fR \le 0.
  2. Write kelly_fraction(outcomes, probs, tol=1e-12): the fraction f∗≥0f^* \ge 0 that makes gg largest, the Kelly fraction. Stop once ∣g′(f)∣\lvert g'(f) \rvert is below tol. Return 0.0 when the expected profit is 0 or less, and raise ValueError for a bet that never loses.
  3. Write simulate_wealth(f, outcomes, probs, n_bets, n_paths, seed): the final wealth of n_paths runs of n_bets bets, each run starting from 1. Draw the outcomes with np.random.default_rng(seed).choice(len(outcomes), size=(n_paths, n_bets), p=probs). A run whose wealth reaches 0 is ruined and stays at 0; a negative f raises ValueError.

All three raise ValueError unless there is one probability per outcome, none negative, and they add up to 1.

The data

OUTCOMES holds the profit on each $1 staked for the five results, and PROBS their chances. The tests simulate 260 weeks, five years of trading.

Hint: Where to start

g(f)g(f) is the expected value of a function of RR, so LOTUS (lesson 5.3.1) writes it as a weighted sum. Differentiate it term by term. The best fraction solves the first-order condition g′(f)=0g'(f) = 0 of lesson 4.4.1, and g′′(f)<0g''(f) < 0, so there is only one.

Hint: Keeping Newton safe

In one variable, Newton's step is −g′(f)/g′′(f)-g'(f)/g''(f) (lesson 4.4.3). From f=0f = 0 it can jump past the fraction at which one bad week ruins the desk, where gg is not defined. Keep a bracket around the answer and bisect when a step would leave it, or halve the step as in lesson 4.4.3.

Hint: Mean against median

The average final wealth rises with ff, but a typical run grows like en g(f)e^{n\,g(f)} over nn bets. They differ because the mean of ln⁡W\ln W is not the log of the mean of WW. Lesson 5.3.1 shows that the mean of a function is not the function of the mean.