INNER CODE UNIT · Python
_monthly_return_stats
Fincept-Corporation/FinceptTerminal · fincept-qt/scripts/Analytics/alternateInvestment/cli.py:368
def _monthly_return_stats(returns: List[float], rf_annual: float) -> Dict[str, Any]:
"""Annualised statistics of a MONTHLY return series given as decimals.
Cumulative return and CAGR compound the series; volatility, Sharpe and Sortino
are annualised with sqrt(12); Sharpe/Sortino use the monthly equivalent of the
annual risk-free rate (Sortino's downside deviation is measured below it).
"""
n = len(returns)
growth, peak, max_dd = 1.0, 1.0, 0.0
for r in returns:
growth *= (1.0 + r)
peak = max(peak, growth)
max_dd = max(max_dd, 1.0 - growth / peak)
years = n / 12.0
cagr = growth ** (1.0 / years) - 1.0 if growth > 0 and years > 0 else None
mean = sum(returns) / n
vol_m = math.sqrt(sum((r - mean) ** 2 for r in returns) / (n - 1)) if n > 1 else 0.0
rf_m = (1.0 + rf_annual) ** (1.0 / 12.0) - 1.0