ranaroussi / quantstats

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

use cumulative multiplication calculation in stats.py is bug #209

Open sxczlhq opened 2 years ago

sxczlhq commented 2 years ago

In stats.py,about as

def compsum(returns): """Calculates rolling compounded returns""" return returns.add(1).cumprod() - 1

def comp(returns): """Calculates total compounded returns""" return returns.add(1).prod() - 1

first,The function name is compsum, but cumprod() is used in the calculation process. second,It is not appropriate to use cumulative multiplication calculation in an investment cycle. so ,I suggest the following formula:

def compsum(returns): """Calculates rolling compounded returns""" return returns.cumsum()

def comp(returns): """Calculates total compounded returns""" return returns.sum()

sxczlhq commented 2 years ago

ps: returns.add(1).cumprod() - 1 ,leads to issues 178.

sxczlhq commented 2 years ago

The following is the revised code after testing, please refer to: ++++++++ stats.py +++++++++++++ def compsum(returns): """Calculates rolling compounded returns""" return returns.cumsum()

def comp(returns): """Calculates total compounded returns""" return returns.sum()

def comprod(returns): """Calculates rolling compounded returns""" return returns.add(1).cumprod() - 1

def prod(returns): """Calculates total compounded returns""" return returns.add(1).prod() - 1

+++++++++++++++ utils.py ++++++++++++++++ def to_prices(returns, base=1e5): """Converts returns series to price data""" returns = returns.copy().fillna(0).replace( [_np.inf, -_np.inf], float('NaN')) return base + base * _stats.comprod(returns)