ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.59k stars 809 forks source link

Fix kelly_criterion formula for investment scenario #340

Open gyusu opened 3 months ago

gyusu commented 3 months ago

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.

https://github.com/ranaroussi/quantstats/blob/fa0a91a42400978bcc95f5f2fb2cf20e6f7af56c/quantstats/stats.py#L870-L882

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