Skip to content

Commit f655948

Browse files
author
88d52bdba0366127fffca9dfa93895
committed
enhance tests
1 parent a6e6865 commit f655948

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

.github/workflows/codecov.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ jobs:
2727
pip install -r requirements.txt
2828
- name: Generate coverage report
2929
run: |
30-
pip install pytest
31-
pip install pytest-cov
30+
pip install pytest pytest-cov
3231
pytest --cov=./ --cov-report=xml
3332
- name: Upload coverage to Codecov
3433
uses: codecov/codecov-action@v3

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ jobs:
3030
pip install -r requirements.txt
3131
- name: Test with pytest
3232
run: |
33-
pip install pytest pytest-cov
34-
pytest -s ./tests --doctest-modules --cov-report=html
33+
pip install pytest
34+
pytest ./tests

pypfopt/discrete_allocation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
offers multiple methods to generate a discrete portfolio allocation from continuous weights.
44
"""
55
import collections
6+
7+
import cvxpy as cp
68
import numpy as np
79
import pandas as pd
8-
import cvxpy as cp
10+
911
from . import exceptions
1012

1113

@@ -249,7 +251,7 @@ def greedy_portfolio(self, reinvest=False, verbose=False):
249251
self._allocation_rmse_error(verbose)
250252
return self.allocation, available_funds
251253

252-
def lp_portfolio(self, reinvest=False, verbose=False, solver="ECOS_BB"):
254+
def lp_portfolio(self, reinvest=False, verbose=False, solver=None):
253255
"""
254256
Convert continuous weights into a discrete portfolio allocation
255257
using integer programming.
@@ -319,9 +321,6 @@ def lp_portfolio(self, reinvest=False, verbose=False, solver="ECOS_BB"):
319321
objective = cp.sum(u) + r
320322

321323
opt = cp.Problem(cp.Minimize(objective), constraints)
322-
323-
if solver is not None and solver not in cp.installed_solvers():
324-
raise NameError("Solver {} is not installed. ".format(solver))
325324
opt.solve(solver=solver)
326325

327326
if opt.status not in {"optimal", "optimal_inaccurate"}: # pragma: no cover

tests/test_discrete_allocation.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import numpy as np
22
import pandas as pd
33
import pytest
4+
from cvxpy.error import SolverError
45

56
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
97
from tests.utilities_for_tests import get_data, setup_efficient_frontier
108

119

@@ -71,7 +69,7 @@ def test_greedy_allocation_rmse_error():
7169
da.greedy_portfolio()
7270

7371
np.testing.assert_almost_equal(
74-
da._allocation_rmse_error(verbose=False), 0.017086185150415774
72+
da._allocation_rmse_error(verbose=True), 0.017086185150415774
7573
)
7674

7775

@@ -132,7 +130,7 @@ def test_greedy_allocation_rmse_error_short():
132130
da.greedy_portfolio()
133131

134132
np.testing.assert_almost_equal(
135-
da._allocation_rmse_error(verbose=False), 0.06063511265243106
133+
da._allocation_rmse_error(verbose=True), 0.06063511265243106
136134
)
137135

138136

@@ -275,7 +273,7 @@ def test_lp_allocation_rmse_error():
275273
latest_prices = get_latest_prices(df)
276274
da = DiscreteAllocation(w, latest_prices, short_ratio=0.3)
277275
da.lp_portfolio()
278-
assert da._allocation_rmse_error(verbose=False) < 0.02
276+
assert da._allocation_rmse_error(verbose=True) < 0.02
279277

280278

281279
def test_lp_portfolio_allocation_short():
@@ -378,7 +376,7 @@ def test_lp_allocation_rmse_error_short():
378376
latest_prices = get_latest_prices(df)
379377
da = DiscreteAllocation(w, latest_prices, short_ratio=0.3)
380378
da.lp_portfolio()
381-
assert da._allocation_rmse_error(verbose=False) < 0.1
379+
assert da._allocation_rmse_error(verbose=True) < 0.1
382380

383381

384382
def test_lp_portfolio_allocation_different_params():
@@ -420,11 +418,11 @@ def test_rmse_decreases_with_value():
420418

421419
da1 = DiscreteAllocation(w, latest_prices, total_portfolio_value=10000)
422420
da1.greedy_portfolio()
423-
rmse1 = da1._allocation_rmse_error(verbose=False)
421+
rmse1 = da1._allocation_rmse_error(verbose=True)
424422

425423
da2 = DiscreteAllocation(w, latest_prices, total_portfolio_value=100000)
426424
da2.greedy_portfolio()
427-
rmse2 = da2._allocation_rmse_error(verbose=False)
425+
rmse2 = da2._allocation_rmse_error(verbose=True)
428426

429427
assert rmse2 < rmse1
430428

@@ -445,7 +443,7 @@ def test_allocation_errors():
445443
DiscreteAllocation(w, latest_prices, total_portfolio_value=0)
446444
with pytest.raises(ValueError):
447445
DiscreteAllocation(w, latest_prices, short_ratio=-0.4)
448-
with pytest.raises(NameError):
446+
with pytest.raises(SolverError):
449447
da = DiscreteAllocation(w, latest_prices)
450448
da.lp_portfolio(solver="ABCDEF")
451449

0 commit comments

Comments
 (0)