1
1
import numpy as np
2
2
import pandas as pd
3
3
import pytest
4
+ from cvxpy .error import SolverError
4
5
5
6
from pypfopt .discrete_allocation import get_latest_prices , DiscreteAllocation
6
- from pypfopt .efficient_frontier import EfficientFrontier
7
- from pypfopt .expected_returns import mean_historical_return
8
- from pypfopt .risk_models import sample_cov
9
7
from tests .utilities_for_tests import get_data , setup_efficient_frontier
10
8
11
9
@@ -71,7 +69,7 @@ def test_greedy_allocation_rmse_error():
71
69
da .greedy_portfolio ()
72
70
73
71
np .testing .assert_almost_equal (
74
- da ._allocation_rmse_error (verbose = False ), 0.017086185150415774
72
+ da ._allocation_rmse_error (verbose = True ), 0.017086185150415774
75
73
)
76
74
77
75
@@ -132,7 +130,7 @@ def test_greedy_allocation_rmse_error_short():
132
130
da .greedy_portfolio ()
133
131
134
132
np .testing .assert_almost_equal (
135
- da ._allocation_rmse_error (verbose = False ), 0.06063511265243106
133
+ da ._allocation_rmse_error (verbose = True ), 0.06063511265243106
136
134
)
137
135
138
136
@@ -275,7 +273,7 @@ def test_lp_allocation_rmse_error():
275
273
latest_prices = get_latest_prices (df )
276
274
da = DiscreteAllocation (w , latest_prices , short_ratio = 0.3 )
277
275
da .lp_portfolio ()
278
- assert da ._allocation_rmse_error (verbose = False ) < 0.02
276
+ assert da ._allocation_rmse_error (verbose = True ) < 0.02
279
277
280
278
281
279
def test_lp_portfolio_allocation_short ():
@@ -378,7 +376,7 @@ def test_lp_allocation_rmse_error_short():
378
376
latest_prices = get_latest_prices (df )
379
377
da = DiscreteAllocation (w , latest_prices , short_ratio = 0.3 )
380
378
da .lp_portfolio ()
381
- assert da ._allocation_rmse_error (verbose = False ) < 0.1
379
+ assert da ._allocation_rmse_error (verbose = True ) < 0.1
382
380
383
381
384
382
def test_lp_portfolio_allocation_different_params ():
@@ -420,11 +418,11 @@ def test_rmse_decreases_with_value():
420
418
421
419
da1 = DiscreteAllocation (w , latest_prices , total_portfolio_value = 10000 )
422
420
da1 .greedy_portfolio ()
423
- rmse1 = da1 ._allocation_rmse_error (verbose = False )
421
+ rmse1 = da1 ._allocation_rmse_error (verbose = True )
424
422
425
423
da2 = DiscreteAllocation (w , latest_prices , total_portfolio_value = 100000 )
426
424
da2 .greedy_portfolio ()
427
- rmse2 = da2 ._allocation_rmse_error (verbose = False )
425
+ rmse2 = da2 ._allocation_rmse_error (verbose = True )
428
426
429
427
assert rmse2 < rmse1
430
428
@@ -445,7 +443,7 @@ def test_allocation_errors():
445
443
DiscreteAllocation (w , latest_prices , total_portfolio_value = 0 )
446
444
with pytest .raises (ValueError ):
447
445
DiscreteAllocation (w , latest_prices , short_ratio = - 0.4 )
448
- with pytest .raises (NameError ):
446
+ with pytest .raises (SolverError ):
449
447
da = DiscreteAllocation (w , latest_prices )
450
448
da .lp_portfolio (solver = "ABCDEF" )
451
449
0 commit comments