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
- Write
growth_rate(f, outcomes, probs): the expected log growth of wealth per bet, , when a fraction of wealth is staked. Here is the profit on $1 staked. Return-math.infwhen an outcome with a positive chance leaves no wealth, that is . - Write
kelly_fraction(outcomes, probs, tol=1e-12): the fraction that makes largest, the Kelly fraction. Stop once is belowtol. Return 0.0 when the expected profit is 0 or less, and raiseValueErrorfor a bet that never loses. - Write
simulate_wealth(f, outcomes, probs, n_bets, n_paths, seed): the final wealth ofn_pathsruns ofn_betsbets, each run starting from 1. Draw the outcomes withnp.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 negativefraisesValueError.
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
is the expected value of a function of , 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 of lesson 4.4.1, and , so there is only one.
Hint: Keeping Newton safe
In one variable, Newton's step is (lesson 4.4.3). From it can jump past the fraction at which one bad week ruins the desk, where 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 , but a typical run grows like over bets. They differ because the mean of is not the log of the mean of . Lesson 5.3.1 shows that the mean of a function is not the function of the mean.