How to use QuantStats - 10 common examples

To help you get started, we’ve selected a few QuantStats examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

ranaroussi / quantstats / quantstats / stats.py View on Github
def kurtosis(returns):
"""
calculates returns' kurtosis
(the degree to which a distribution peak compared to a normal distribution)
"""
return _utils._prepare_returns(returns).kurtosis()
ranaroussi / quantstats / quantstats / stats.py View on Github
def profit_ratio(returns):
""" measures the profit ratio (win ratio / loss ratio) """
returns = _utils._prepare_returns(returns)
wins = returns[returns >= 0]
loss = returns[returns < 0]

win_ratio = abs(wins.mean() / wins.count())
loss_ratio = abs(loss.mean() / loss.count())
try:
return win_ratio / loss_ratio
except Exception:
return 0.
ranaroussi / quantstats / quantstats / stats.py View on Github
def expected_return(returns, aggregate=None, compounded=True):
"""
returns the expected return for a given period
by calculating the geometric holding period return
"""
returns = _utils._prepare_returns(returns)
returns = _utils.aggregate_returns(returns, aggregate, compounded)
return _np.product(1 + returns) ** (1 / len(returns)) - 1
ranaroussi / quantstats / quantstats / stats.py View on Github
def exposure(returns):
""" returns the market exposure time (returns != 0) """
returns = _utils._prepare_returns(returns)

def _exposure(ret):
ex = len(ret[(~_np.isnan(ret)) & (ret != 0)]) / len(ret)
return _ceil(ex * 100) / 100

if isinstance(returns, _pd.DataFrame):
_df = {}
for col in returns.columns:
_df[col] = _exposure(returns[col])
return _pd.Series(_df)
return _exposure(returns)
ranaroussi / quantstats / quantstats / stats.py View on Github
def tail_ratio(returns, cutoff=0.95):
"""
measures the ratio between the right
(95%) and left tail (5%).
"""
returns = _utils._prepare_returns(returns)
return abs(returns.quantile(cutoff) / returns.quantile(1-cutoff))
ranaroussi / quantstats / quantstats / stats.py View on Github
def value_at_risk(returns, sigma=1, confidence=0.95):
"""
calculats the daily value-at-risk
(variance-covariance calculation with confidence n)
"""
returns = _utils._prepare_returns(returns)
mu = returns.mean()
sigma *= returns.std()

if confidence > 1:
confidence = confidence/100

return _norm.ppf(1-confidence, mu, sigma)
ranaroussi / quantstats / quantstats / stats.py View on Github
def payoff_ratio(returns):
""" measures the payoff ratio (average win/average loss) """
returns = _utils._prepare_returns(returns)
return avg_win(returns) / abs(avg_loss(returns))
ranaroussi / quantstats / quantstats / stats.py View on Github
def skew(returns):
"""
calculates returns' skewness
(the degree of asymmetry of a distribution around its mean)
"""
return _utils._prepare_returns(returns).skew()
ranaroussi / quantstats / quantstats / stats.py View on Github
def win_rate(returns, aggregate=None, compounded=True):
""" calculates the win ratio for a period """
def _win_rate(series):
try:
return len(series[series > 0]) / len(series[series != 0])
except Exception:
return 0.

returns = _utils._prepare_returns(returns)
if aggregate:
returns = _utils.aggregate_returns(returns, aggregate, compounded)

if isinstance(returns, _pd.DataFrame):
_df = {}
for col in returns.columns:
_df[col] = _win_rate(returns[col])

return _pd.Series(_df)

return _win_rate(returns)
ranaroussi / quantstats / quantstats / stats.py View on Github
def outlier_loss_ratio(returns, quantile=.01):
"""
calculates the outlier losers ratio
1st percentile of returns / mean negative return
"""
returns = _utils._prepare_returns(returns)
return returns.quantile(quantile).mean() / returns[returns < 0].mean()

QuantStats

Portfolio analytics for quants

Apache-2.0