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.

github ranaroussi / quantstats / quantstats / stats.py View on Github external
def kurtosis(returns):
    """
    calculates returns' kurtosis
    (the degree to which a distribution peak compared to a normal distribution)
    """
    return _utils._prepare_returns(returns).kurtosis()
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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.
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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)
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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))
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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)
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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))
github ranaroussi / quantstats / quantstats / stats.py View on Github external
def skew(returns):
    """
    calculates returns' skewness
    (the degree of asymmetry of a distribution around its mean)
    """
    return _utils._prepare_returns(returns).skew()
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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)
github ranaroussi / quantstats / quantstats / stats.py View on Github external
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()