Skip to content

Commit

Permalink
Misc pd/np compatibility stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ranaroussi committed Oct 25, 2024
1 parent 8ce8784 commit f19d5b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
30 changes: 16 additions & 14 deletions quantstats/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,23 +972,25 @@ def metrics(
metrics["Outlier Win Ratio"] = _stats.outlier_win_ratio(df, prepare_returns=False)
metrics["Outlier Loss Ratio"] = _stats.outlier_loss_ratio(df, prepare_returns=False)

# returns
# # returns
metrics["~~"] = blank
comp_func = _stats.comp if compounded else _np.sum

today = df.index[-1] # _dt.today()
metrics["MTD %"] = comp_func(df[df.index >= _dt(today.year, today.month, 1)]) * pct

d = today - relativedelta(months=3)
metrics["3M %"] = comp_func(df[df.index >= d]) * pct

d = today - relativedelta(months=6)
metrics["6M %"] = comp_func(df[df.index >= d]) * pct

metrics["YTD %"] = comp_func(df[df.index >= _dt(today.year, 1, 1)]) * pct

d = today - relativedelta(years=1)
metrics["1Y %"] = comp_func(df[df.index >= d]) * pct
m3 = today - relativedelta(months=3)
m6 = today - relativedelta(months=6)
y1 = today - relativedelta(years=1)
if compounded:
metrics["MTD %"] = _stats.comp(df[df.index >= _dt(today.year, today.month, 1)]) * pct
metrics["3M %"] = _stats.comp(df[df.index >= m3]) * pct
metrics["6M %"] = _stats.comp(df[df.index >= m6]) * pct
metrics["YTD %"] = _stats.comp(df[df.index >= _dt(today.year, 1, 1)]) * pct
metrics["1Y %"] = _stats.comp(df[df.index >= y1]) * pct
else:
metrics["MTD %"] = _np.sum(df[df.index >= _dt(today.year, today.month, 1)], axis=0) * pct
metrics["3M %"] = _np.sum(df[df.index >= m3], axis=0) * pct
metrics["6M %"] = _np.sum(df[df.index >= m6], axis=0) * pct
metrics["YTD %"] = _np.sum(df[df.index >= _dt(today.year, 1, 1)], axis=0) * pct
metrics["1Y %"] = _np.sum(df[df.index >= y1], axis=0) * pct

d = today - relativedelta(months=35)
metrics["3Y (ann.) %"] = _stats.cagr(df[df.index >= d], 0.0, compounded) * pct
Expand Down
8 changes: 4 additions & 4 deletions quantstats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def pct_rank(prices, window=60):

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


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


def distribution(returns, compounded=True, prepare_returns=True):
Expand Down Expand Up @@ -93,7 +93,7 @@ def expected_return(returns, aggregate=None, compounded=True, prepare_returns=Tr
if prepare_returns:
returns = _utils._prepare_returns(returns)
returns = _utils.aggregate_returns(returns, aggregate, compounded)
return _np.prod(1 + returns) ** (1 / len(returns)) - 1
return _np.prod(1 + returns, axis=0) ** (1 / len(returns)) - 1


def geometric_mean(returns, aggregate=None, compounded=True):
Expand Down Expand Up @@ -519,7 +519,7 @@ def cagr(returns, rf=0.0, compounded=True, periods=252):
if compounded:
total = comp(total)
else:
total = _np.sum(total)
total = _np.sum(total, axis=0)

years = (returns.index[-1] - returns.index[0]).days / periods

Expand Down

0 comments on commit f19d5b2

Please sign in to comment.