From 3abed5ff2b51ff2d14ddc2c938348e7ddf972aee Mon Sep 17 00:00:00 2001 From: Abhinav Jain <156959147+abhnxvj@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:10:51 +0530 Subject: [PATCH 1/2] [BUG] Compound log returns with exp in expected return estimators --- pypfopt/expected_returns.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pypfopt/expected_returns.py b/pypfopt/expected_returns.py index bbbf69c6..c654c9e9 100644 --- a/pypfopt/expected_returns.py +++ b/pypfopt/expected_returns.py @@ -166,6 +166,9 @@ def mean_historical_return( _check_returns(returns) if compounding: + if log_returns and not returns_data: + # log returns add up over time, so they compound with exp, not (1 + r) + return np.exp(returns.mean() * frequency) - 1 return (1 + returns).prod() ** (frequency / returns.count()) - 1 else: return returns.mean() * frequency @@ -218,6 +221,8 @@ def ema_historical_return( _check_returns(returns) if compounding: + if log_returns and not returns_data: + return np.exp(returns.ewm(span=span).mean().iloc[-1] * frequency) - 1 return (1 + returns.ewm(span=span).mean().iloc[-1]) ** frequency - 1 else: return returns.ewm(span=span).mean().iloc[-1] * frequency @@ -304,7 +309,9 @@ def capm_return( betas = cov["mkt"] / cov.loc["mkt", "mkt"] betas = betas.drop("mkt") # Find mean market return on a given time period - if compounding: + if compounding and log_returns and not returns_data: + mkt_mean_ret = np.exp(returns["mkt"].mean() * frequency) - 1 + elif compounding: mkt_mean_ret = (1 + returns["mkt"]).prod() ** ( frequency / returns["mkt"].count() ) - 1 From c132ced37cd5ec8d78b58853a5ad65b0b6292f2b Mon Sep 17 00:00:00 2001 From: Abhinav Jain <156959147+abhnxvj@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:11:30 +0530 Subject: [PATCH 2/2] Add tests for log return compounding --- tests/test_expected_returns.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_expected_returns.py b/tests/test_expected_returns.py index 4e16583b..7c0ad6f6 100644 --- a/tests/test_expected_returns.py +++ b/tests/test_expected_returns.py @@ -299,3 +299,36 @@ def test_log_return_passthrough(): except AssertionError: return assert False + + +def test_mean_historical_return_log_returns_compounding(): + # log returns add up over time, so compounding them with (1 + r) understates + # the growth. The compounded log return should match the CAGR from simple returns. + df = get_data() + mu_simple = expected_returns.mean_historical_return(df) + mu_log = expected_returns.mean_historical_return(df, log_returns=True) + pd.testing.assert_series_equal(mu_simple, mu_log) + + # a price that doubles over one year has a CAGR of 100% either way + prices = pd.DataFrame({"A": np.linspace(100, 200, 253)}) + mu = expected_returns.mean_historical_return(prices, log_returns=True) + np.testing.assert_almost_equal(mu["A"], 1.0) + + +def test_ema_historical_return_log_returns_compounding(): + df = get_data() + log_rets = expected_returns.returns_from_prices(df, log_returns=True) + expected = np.exp(log_rets.ewm(span=500).mean().iloc[-1] * 252) - 1 + mu = expected_returns.ema_historical_return(df, log_returns=True) + pd.testing.assert_series_equal(mu, expected) + + +def test_capm_return_log_returns_compounding(): + df = get_data() + log_rets = expected_returns.returns_from_prices(df, log_returns=True) + mkt = log_rets.mean(axis=1) + cov = log_rets.assign(mkt=mkt).cov() + betas = (cov["mkt"] / cov.loc["mkt", "mkt"]).drop("mkt") + expected = betas * (np.exp(mkt.mean() * 252) - 1) + mu = expected_returns.capm_return(df, log_returns=True) + pd.testing.assert_series_equal(mu, expected, check_names=False)