# The Problem with Optimal f

What does “optimal” mean, anyway? In the first part of this series, we discovered that the staked fraction of capital that yields the greatest compounded returns also yields a less-than-optimal level of drawdown. To realize the greatest return on capital, an investor in SPY since its inception should have used over 3x leverage to buy in. This would have yielded the greatest compounded rate of return, but would have induced a 97% (!!!) max drawdown along the way. Since a 20% retracement from peak equity causes most investors to start tossing in their sleep, this approach doesn’t seem very realistic. This post will help traders maximize their gains while still getting their beauty rest!

Read [Part 1: Beyond Kelly] here and feel free to follow along with provided notebook.

# Drawdown Curve

We left off with an example showing that investing in SPY at optimal f would cause some extreme discomfort along the way. Let’s see how increasing position size increases drawdown by constructing a curve similar to the one for GHPR for maximum drawdown. We use maximum drawdown as a proxy for risk because this is the number that investors “feel”, which tends to drive allocation decisions for emotional reasons.

An alternative metric is the Ulcer Index, a root-mean-squared calculation based on drawdown that takes into account both drawdown severity and duration. Source code for this value has been provided as well; feel free to substitute it in.

Python Code:

<br /> def equity_curve(returns):<br /> eq = (1 + returns).cumprod(axis=0)<br /> # Set the starting value of every curve to 1<br /> # normalized_eq = raw_eq / raw_eq[0]<br /> return eq</p> <p>def drawdown(equity_curve):<br /> eq_series = pd.Series(equity_curve)<br /> _drawdown = eq_series / eq_series.cummax() - 1<br /> return _drawdown</p> <p>def max_drawdown(equity_curve, percent=True):<br /> abs_drawdown = np.abs(drawdown(equity_curve))<br /> _max_drawdown = np.max(abs_drawdown)<br /> if percent == True:<br /> return _max_drawdown * 100<br /> else:<br /> return _max_drawdown</p> <p>def ulcer_index(equity_curve):<br /> _drawdown = drawdown(equity_curve)<br /> _ulcer_index = np.sqrt(np.mean(_drawdown**2)) * 100<br /> return _ulcer_index</p> <p>def twr(equity_curve):<br /> eq_arr = np.array(equity_curve)<br /> _twr = eq_arr[-1] / eq_arr[0]<br /> return _twr</p> <p>def ghpr(equity_curve):<br /> _twr = twr(equity_curve)<br /> _ghpr = _twr ** (1 / len(equity_curve)) - 1<br /> return _ghpr</p> <p>def get_f_dd(returns):<br /> f_values = np.linspace(0, 0.99, 100)<br /> max_loss = np.abs(np.min(returns))<br /> bounded_f = f_values / max_loss<br /> df = pd.DataFrame(columns=['ghpr', 'drawdown'])<br /> for f in bounded_f:<br /> eq = equity_curve(f * returns)<br /> _ghpr = ghpr(eq) - 1<br /> _max_drawdown = max_drawdown(eq)<br /> # _ulcer_index = ulcer_index(eq)<br /> df.loc[f, 'ghpr'] = _ghpr * 100<br /> df.loc[f, 'drawdown'] = _max_drawdown<br /> # df.loc[f, 'ulcer index'] = _ulcer_index<br /> optimal_f = df['ghpr'].idxmax()<br /> return {'f_curve':df, 'optimal_f':optimal_f, 'max_loss':max_loss}</p> <p>def f_dd_plot(f, title=''):<br /> f_curve = f['f_curve']<br /> optimal_f = f['optimal_f']<br /> optimal_f_ghpr = f_curve.loc[optimal_f, 'ghpr']<br /> limit_dd = f_curve.loc[2 * optimal_f, 'drawdown']</p> <p> fig, ax = plt.subplots(1, 1, figsize=(10, 7))<br /> f_curve.plot(secondary_y='drawdown', ax=ax)</p> <p> ax.set_xlim(0, optimal_f * 2)<br /> ax.set_ylim(0, 1.5 * optimal_f_ghpr)<br /> ax.right_ax.set_ylim(0)<br /> ax.set_title(title + ' GHPR vs Max Drawdown')<br /> ax.set_xlabel('Fraction Staked')<br /> ax.set_ylabel('GHPR (%)')<br /> ax.right_ax.set_ylabel('Drawdown (%)')<br /> ax.axvline(optimal_f, linestyle=':', color='red')</p> <p> plt.savefig(title + ' f Drawdown Curve.png')<br /> plt.show()</p> <p>def f_dd_results(f):<br /> f_curve = f['f_curve']<br /> optimal_f = f['optimal_f']<br /> ghpr = f_curve.loc[optimal_f, 'ghpr']<br /> drawdown = f_curve.loc[optimal_f, 'drawdown']</p> <p> print('Optimal f: {}'.format(np.round(optimal_f, 3)))<br /> print('Geometric Holding Period Return: {}%'.format(np.round(ghpr, 5)))<br /> print('Max Drawdown: {}%'.format(np.round(drawdown, 2)))</p> <p># SPY.csv file available in GitHub 'data' folder<br /> spy = pd.read_csv('SPY.csv', parse_dates=True, index_col=0)<br /> spy.sort_index(ascending=True, inplace=True)<br /> spy_returns = spy['Adj Close'].pct_change().dropna()</p> <p>spy_f = get_f_dd(spy_returns)</p> <p>f_dd_plot(spy_f, 'SPY')<br /> f_dd_results(spy_f)<br />

<br /> Optimal f: 3.149<br /> Geometric Holding Period Return: 0.06677%<br /> Max Drawdown: 97.23%<br />

We can see from the graph that as position size approaches optimal f, both rate of return and maximum drawdown increase, with drawdown at optimal f well over 90%. As we increase size further from this point, drawdown continues to approach 100%. However, returns begin to decrease. This reinforces the fact that increasing size to increase profits (*cough* 200x leverage on cryptocurrency futures) past optimal f is a fool’s errand. Doing so will only bring further pain.

However, many investors’ pain tolerance is much (MUCH) lower than 97%. Most people have a psychological limit where they’ll take their money off the table if exceeded. Be conservative with this limit; what you can handle when looking at a backtest is probably different than what you can when looking at your account PnL.

The total returns aren’t relevant if one can’t handle the risk. For this reason, we seek to bound the curve at the point where drawdown exceeds our limit. Effectively, the GHPR value for all fractions that exceed the risk limit becomes zero; it’s irrelevant. The peak of this new, bounded, curve occurs at the real optimal fraction. Deploying this fraction of our capital will yield the greatest return, while protecting from the psychological pain of our maximum drawdown.

For instance, let’s say we want to maximize our compounded returns in SPY without letting our drawdown exceed 25%.

<br /> def get_f_bounded(returns, drawdown_limit):<br /> f_values = np.linspace(0, 0.99, 199)<br /> max_loss = np.abs(np.min(returns))<br /> bounded_f = f_values / max_loss<br /> df = pd.DataFrame(columns=['ghpr', 'drawdown'])<br /> for f in bounded_f:<br /> eq = equity_curve(f * returns)<br /> _max_drawdown = max_drawdown(eq)<br /> if _max_drawdown &amp;amp;lt;= drawdown_limit:<br /> _ghpr = ghpr(eq)<br /> else:<br /> _ghpr = 0<br /> df.loc[f, 'ghpr'] = _ghpr * 100<br /> df.loc[f, 'drawdown'] = _max_drawdown<br /> optimal_f = df['ghpr'].idxmax()<br /> return {'f_curve':df, 'optimal_f':optimal_f, 'max_loss':max_loss}</p> <p>spy_f_bounded = get_f_bounded(spy_returns, drawdown_limit=25)<br /> f_dd_plot(spy_f_bounded, 'SPY Bounded')<br /> f_dd_results(spy_f_bounded)<br />

<br /> Optimal f: 0.356<br /> Geometric Holding Period Return: 0.01427%<br /> Max Drawdown: 23.04%<br />

In order to protect from the possibility of a 25% drawdown, we’d need to scale back the leverage from our optimal f value by almost 9 times!

# Drawdown Caveats

As noted above, drawdown is a good risk metric for practitioners because this it most directly impacts their emotions. In practice, few traders base their decisions whether to follow their strategy on the standard deviation of returns or similar measures of volatility. It’s a much more visceral experience to watch one’s account balance drop from $100,000 to $50,000; one that can de-rail trading strategies. However, there are limitations to using drawdown as a risk metric.

## Order of Returns

Unlike compounded returns, the value for maximum drawdown depends on the order in which returns are realized. We’ll demonstrate this below by shuffling the order of S&P returns and examining the resulting equity curves.

<br /> def reordered_curves(returns, n_curves):<br /> curves = pd.DataFrame(index=returns.index)<br /> for i in range(n_curves):<br /> reordered = np.random.permutation(returns)<br /> curves[i] = equity_curve(reordered)<br /> return curves</p> <p>spy_curves_10 = reordered_curves(spy_returns, 10)</p> <p>fig, ax = plt.subplots(1, 1, figsize=(10, 10))<br /> spy_curves_10.plot(ax=ax)<br /> ax.set_title('Equity Curves')<br /> plt.savefig('10 Equity Curves.png')<br /> plt.show()<br />

<br /> curve_results = pd.DataFrame()<br /> curve_results['GHPR'] = curves.apply(ghpr)<br /> curve_results['Max Drawdown'] = curves.apply(max_drawdown)<br /> curve_results<br />

We see that regardless of the order in which daily returns are realized, the GHPR for each curve is the same. Using optimal f as it’s traditionally defined would not discriminate between these curves. However, the difference between the largest and smallest drawdowns experienced is over 20%! And this is after generating only 10 curves; we’ll see below that the possible values can vary greatly.

<br /> spy_curves_5000 = reordered_curves(spy_returns, 5000)<br /> spy_drawdown_5000 = spy_curves_5000.apply(max_drawdown)<br /> spy_drawdown_5000.hist(bins=25)<br /> plt.title('Possible Max Drawdown Values')<br /> plt.show()<br /> print('Maximum Drawdown: {}%'.format(np.round(spy_drawdown_5000.max(), 3)))<br />

<br /> Maximum Drawdown: 73.686%<br />

The maximum drawdown varies from less than 30% to over 70%! This is a huge range of possible values. If we want to use drawdown as a risk metric, we need to deal with its inherent uncertainty. We won’t be able to totally constrain our results on expected drawdown, as the value for max drawdown could fall within a wide range. However, we can say with some level of confidence that the maximum drawdown experienced won’t exceed our threshold. To do this, we calculate the percentage of scenarios in which maximum drawdown is likely exceed our risk threshold. If this percentage is too great, we must use a smaller position size.

## Time Horizon

A second important characteristic of drawdown is that it is time-horizon dependent. In fact, it can be shown that maximum drawdown is proportional to the square root of time (I won’t prove that out here, but feel free to work investigate it yourself!) Essentially, the longer a given series of returns compounds on itself, the greater the maximum drawdown will be. Intuitively, an account’s maximum drawdown over the course of the next month will be less than its maximum over the next year. In order to accurately set a threshold for maximum drawdown, we’ll need to define our time horizon.

For example, we might specify that drawdown must not exceed 25% over the course of the next year (approx. 250 trading days). Building on our earlier findings, we’d like to state this constraint with a certain degree of certainty, or confidence. A common level for statistical significance is 5%. We could desire to be 95% sure that our drawdown over the coming year will be less than 25%.

To determine the ideal staking fraction under these conditions, we will use the following process for each f value between 0 and (1 / worst loss):

- Generate many possible equity curves (similar to Monte Carlo)
- Randomly sample a number of returns from the return distribution according to our desired time horizon
- Usually pick >1000 to get an accurate picture of drawdown distribution

- Calculate GHPR and maximum drawdown for each of these curves
- Determine the drawdown level at our specified confidence level (i.e. 95th percentile of drawdown distribution)
- Record the median GHPR value (you could choose to use a more favorable/conservative percentile for GHPR but we’ll use the median)
- If the drawdown value at our specified confidence level is lower than our risk threshold, assign the median GHPR as the GHPR value for that f
- If it is not, set GHPR = 0. It’s too risky to trade at this position size

- Find the point in this new f curve that maximizes the expected value of GHPR

We’ll call this point “ideal f” (I guess.. please someone suggest something better!) .

<br /> import time</p> <p>def ideal_f(returns, time_horizon, n_curves, drawdown_limit, certainty_level):<br /> """<br /> Calculates ideal fraction to stake on an investment with given return distribution </p> <p> Args:<br /> returns: (array-like) distribution that's representative of future returns<br /> time_horizon: (integer) the number of returns to sample for each curve<br /> n_curves: (integer) the number of equity curves to generate on each iteration of f<br /> drawdown_limit: (real) user-specified value for drawdown which must not be exceeded<br /> certainty_level: (real) the level of confidence that drawdown<br /> limit will not be exceeded</p> <p> Returns:<br /> 'f_curve': calculated drawdown and ghpr value at each value of f<br /> 'optimal_f': the ideal fraction of one's account to stake on an investment<br /> 'max_loss': the maximum loss sustained in the provided returns distribution</p> <p> """<br /> print('Calculating ideal f...')<br /> start = time.time()</p> <p> f_values = np.linspace(0, 0.99, 200)<br /> max_loss = np.abs(np.min(returns))<br /> bounded_f = f_values / max_loss<br /> f_curve = pd.DataFrame(columns=['ghpr', 'drawdown'])<br /> for f in bounded_f:<br /> # Generate n_curves number of random equity curves<br /> reordered_returns = np.random.choice(f * returns, size=(time_horizon, n_curves))<br /> curves = equity_curve(reordered_returns)<br /> curves_df = pd.DataFrame(curves)<br /> # Calculate GHPR and Maximum Drawdown for each equity curve<br /> curves_drawdown = max_drawdown(curves_df)<br /> curves_ghpr = ghpr(curves_df)<br /> # Calculate drawdown at our certainty level<br /> drawdown_percentile = np.percentile(curves_drawdown, certainty_level)<br /> # Calculate median ghpr value<br /> ghpr_median = np.median(curves_ghpr)<br /> if drawdown_percentile &lt;= drawdown_limit:<br /> _ghpr = ghpr_median<br /> else:<br /> _ghpr = 0<br /> f_curve.loc[f, 'ghpr'] = _ghpr * 100<br /> f_curve.loc[f, 'drawdown'] = drawdown_percentile<br /> optimal_f = f_curve['ghpr'].idxmax()</p> <p> elapsed = time.time() - start<br /> print('Ideal f calculated in {}s'.format(elapsed))</p> <p> return {'f_curve':f_curve, 'optimal_f':optimal_f, 'max_loss':max_loss}</p> <p>spy_ideal_f = ideal_f(spy_returns, time_horizon=250, n_curves=1000, drawdown_limit=25, certainty_level=95)<br />

<br /> Calculating ideal f...<br /> Ideal f calculated in 2.113638162612915s<br />

<br /> f_dd_plot(spy_ideal_f)<br /> f_dd_results(spy_ideal_f)<br />

<br /> Optimal f: 0.859<br /> Geometric Holding Period Return: 0.03295%<br /> Max Drawdown: 24.57%<br />

We see that the curves are a little more jagged using this approach. This is due to the inherent randomness involved when using a Monte Carlo-type simulation. However, we observe the same general relationship between GHPR and drawdown as before. Interestingly, this approach suggests using more than twice as much leverage as the determinisic method shown previously. This is for two reasons.

First, we shortened our time horizon considerably. Whereas the first attempt used the entire price history of the SPY (nearly 25 years), the second approach simulated curves one year out into the future. The shorter the time horizon, the less drawdown we expect. Second, financial time series such as equity index returns tend to exhibit both volatility clustering and autocorrelation. This means that the individual returns aren’t totally independent from previous returns, an assumption used in our modeling process. To combat this, the strategy used to input results into the position sizing algorithm should take these factors into account when making buying/selling decisions.

This might be a good time to mention that the returns used in these calculations **must be representative of expected future returns. **The ideal scenario would be to use real-money returns or results from paper trading. If one must use results from backtesting, it’s recommended to use out-of-sample returns if possible. Plugging over-optimized in-sample results into these formulas will underestimate risk and overestimate gain. If your strategy has a sharpe ratio of 8 in a backtest but in live trading it’s closer to 0.5, you will be massively over-leveraged and quickly exceed your risk tolerance.

Final Thoughts / Next Steps

We now have a method to determine our ideal position size as a percentage of our total account equity. By simulating a number of possible outcomes for a series of returns, we are able to say with some level of confidence that the amount of capital we are risking will yield the greatest compounded return while remaining within our psychological limit for drawdown.

However, this method only applies to single return streams. As investors, we want to leverage the benefits of diversification. By combining multiple assets and strategies into a portfolio, we gain access to downside protection and additional upside. We’ll incorporate this into our approach in the next part of this series, so check back for updates!

If you liked this post (or didn’t!) leave a comment below, and/or join the Quant Talk chat at https://t.me/joinchat/GrWzrxH7Z0X_65JD3NLGMw.

Thanks for Reading