Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pypfopt/expected_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/test_expected_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)