Skip to content

Commit 8debd04

Browse files
committed
improved tests
1 parent 9f4aef1 commit 8debd04

File tree

5 files changed

+17
-35
lines changed

5 files changed

+17
-35
lines changed

docs/ExpectedReturns.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ superior models and feed them into the optimizer.
5050

5151
.. autofunction:: returns_from_prices
5252

53-
.. autofunction:: log_returns_from_prices
54-
5553
.. autofunction:: prices_from_returns
5654

5755

pypfopt/expected_returns.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,6 @@ def returns_from_prices(prices, log_returns=False):
4343
return prices.pct_change().dropna(how="all")
4444

4545

46-
def log_returns_from_prices(prices):
47-
"""
48-
Calculate the log returns given prices.
49-
50-
:param prices: adjusted (daily) closing prices of the asset, each row is a
51-
date and each column is a ticker/id.
52-
:type prices: pd.DataFrame
53-
:return: (daily) returns
54-
:rtype: pd.DataFrame
55-
"""
56-
warnings.warn(
57-
"log_returns_from_prices is deprecated. Please use returns_from_prices(prices, log_returns=True)"
58-
)
59-
return np.log(1 + prices.pct_change()).dropna(how="all")
60-
61-
6246
def prices_from_returns(returns, log_returns=False):
6347
"""
6448
Calculate the pseudo-prices given returns. These are not true prices because

tests/test_discrete_allocation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_get_latest_prices_error():
2525
def test_remove_zero_positions():
2626
raw = {"MA": 14, "FB": 12, "XOM": 0, "PFE": 51, "BABA": 5, "GOOG": 0}
2727

28-
da = DiscreteAllocation({}, pd.Series())
28+
da = DiscreteAllocation({}, pd.Series(dtype=float))
2929
assert da._remove_zero_positions(raw) == {"MA": 14, "FB": 12, "PFE": 51, "BABA": 5}
3030

3131

tests/test_efficient_frontier.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,10 @@ def test_max_sharpe_risk_free_portfolio_performance():
582582
# max_sharpe
583583
ef = setup_efficient_frontier()
584584
ef.max_sharpe(risk_free_rate=0.05)
585-
res = ef.portfolio_performance()
586-
res2 = ef.portfolio_performance(risk_free_rate=0.05)
587-
np.testing.assert_allclose(res, res2)
585+
with pytest.warns(UserWarning):
586+
res = ef.portfolio_performance()
587+
res2 = ef.portfolio_performance(risk_free_rate=0.05)
588+
np.testing.assert_allclose(res, res2)
588589

589590

590591
def test_min_vol_pair_constraint():

tests/test_expected_returns.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ def test_log_returns_from_prices():
5757
new_nan = log_rets.isnull().sum(axis=1).sum()
5858
assert new_nan == old_nan
5959
np.testing.assert_almost_equal(log_rets.iloc[-1, -1], 0.0001682740081102576)
60-
# Test the deprecated function, until it is removed.
61-
deprecated_log_rets = expected_returns.log_returns_from_prices(df)
62-
np.testing.assert_allclose(deprecated_log_rets, log_rets)
6360

6461

6562
def test_mean_historical_returns_dummy():
@@ -74,11 +71,11 @@ def test_mean_historical_returns_dummy():
7471
)
7572
mean = expected_returns.mean_historical_return(data, frequency=1)
7673
test_answer = pd.Series([0.0061922, 0.0241137, 0.0122722, -0.0421775])
77-
pd.testing.assert_series_equal(mean, test_answer, check_less_precise=4)
74+
pd.testing.assert_series_equal(mean, test_answer, rtol=1e-3)
7875

7976
mean = expected_returns.mean_historical_return(data, compounding=False, frequency=1)
8077
test_answer = pd.Series([0.0086560, 0.0250000, 0.0128697, -0.03632333])
81-
pd.testing.assert_series_equal(mean, test_answer, check_less_precise=4)
78+
pd.testing.assert_series_equal(mean, test_answer, rtol=1e-3)
8279

8380

8481
def test_mean_historical_returns():
@@ -142,10 +139,11 @@ def test_ema_historical_return():
142139
assert mean.notnull().all()
143140
assert mean.dtype == "float64"
144141
# Test the (warning triggering) case that input is not a dataFrame
145-
mean_np = expected_returns.ema_historical_return(df.to_numpy())
146-
mean_np.name = mean.name # These will differ.
147-
reset_mean = mean.reset_index(drop=True) # Index labels would be tickers.
148-
pd.testing.assert_series_equal(mean_np, reset_mean)
142+
with pytest.warns(RuntimeWarning):
143+
mean_np = expected_returns.ema_historical_return(df.to_numpy())
144+
mean_np.name = mean.name # These will differ.
145+
reset_mean = mean.reset_index(drop=True) # Index labels would be tickers.
146+
pd.testing.assert_series_equal(mean_np, reset_mean)
149147

150148

151149
def test_ema_historical_return_frequency():
@@ -195,10 +193,11 @@ def test_capm_no_benchmark():
195193
)
196194
np.testing.assert_array_almost_equal(mu.values, correct_mu)
197195
# Test the (warning triggering) case that input is not a dataFrame
198-
mu_np = expected_returns.capm_return(df.to_numpy())
199-
mu_np.name = mu.name # These will differ.
200-
mu_np.index = mu.index # Index labels would be tickers.
201-
pd.testing.assert_series_equal(mu_np, mu)
196+
with pytest.warns(RuntimeWarning):
197+
mu_np = expected_returns.capm_return(df.to_numpy())
198+
mu_np.name = mu.name # These will differ.
199+
mu_np.index = mu.index # Index labels would be tickers.
200+
pd.testing.assert_series_equal(mu_np, mu)
202201

203202

204203
def test_capm_with_benchmark():

0 commit comments

Comments
 (0)