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
2 changes: 1 addition & 1 deletion pypfopt/expected_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def capm_return(
if returns_data:
returns = prices.copy()
if market_prices is not None:
market_returns = market_prices
market_returns = market_prices.copy()
else:
returns = returns_from_prices(prices, log_returns)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_expected_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ def test_capm_with_benchmark():
assert (mu2 >= mu).all()


@pytest.mark.parametrize("compounding", [True, False])
def test_capm_returns_data_preserves_benchmark(compounding):
prices = get_data()
benchmark_prices = get_benchmark_data()
returns = expected_returns.returns_from_prices(prices)
market_returns = expected_returns.returns_from_prices(benchmark_prices)
original_returns = returns.copy(deep=True)
original_market_returns = market_returns.copy(deep=True)

expected = expected_returns.capm_return(
prices, market_prices=benchmark_prices, compounding=compounding
)
actual = expected_returns.capm_return(
returns,
market_prices=market_returns,
returns_data=True,
compounding=compounding,
)

pd.testing.assert_series_equal(actual, expected)
pd.testing.assert_frame_equal(returns, original_returns)
pd.testing.assert_frame_equal(market_returns, original_market_returns)


def test_risk_matrix_and_returns_data():
# Test the switcher method for simple calls
df = get_data()
Expand Down