ranaroussi/quantstats

Fix kelly_criterion formula for investment scenario

gyusu opened this issue · 1 comments

According to https://en.wikipedia.org/wiki/Kelly_criterion,
current code is for a gambling scenario where you lose all your money when you lose.

def kelly_criterion(returns, prepare_returns=True):
"""
Calculates the recommended maximum amount of capital that
should be allocated to the given strategy, based on the
Kelly Criterion (http://en.wikipedia.org/wiki/Kelly_criterion)
"""
if prepare_returns:
returns = _utils._prepare_returns(returns)
win_loss_ratio = payoff_ratio(returns)
win_prob = win_rate(returns)
lose_prob = 1 - win_prob
return ((win_loss_ratio * win_prob) - lose_prob) / win_loss_ratio

Need to change like below to calcuate it for investment scenario which allows partial losses.

def kelly_criterion(returns, prepare_returns=True):
    """
    Calculates the recommended maximum amount of capital that
    should be allocated to the given strategy, based on the
    Kelly Criterion (http://en.wikipedia.org/wiki/Kelly_criterion)
    """
    if prepare_returns:
        returns = _utils._prepare_returns(returns)

    win_avg = avg_win(returns)
    lose_avg = -avg_lose(returns)

    win_prob = win_rate(returns)
    lose_prob = 1 - win_prob

    return win_prob / lose_avg - lose_prob / win_avg

Check out https://github.com/Lumiwealth/quantstats_lumi, which is being updated regularly. We are a fork of this library that is being maintained by Lumiwealth