Skip to content

Commit

Permalink
enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
88d52bdba0366127fffca9dfa93895 committed May 2, 2023
1 parent a6e6865 commit f655948
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jobs:
pip install -r requirements.txt
- name: Generate coverage report
run: |
pip install pytest
pip install pytest-cov
pip install pytest pytest-cov
pytest --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ jobs:
pip install -r requirements.txt
- name: Test with pytest
run: |
pip install pytest pytest-cov
pytest -s ./tests --doctest-modules --cov-report=html
pip install pytest
pytest ./tests
9 changes: 4 additions & 5 deletions pypfopt/discrete_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
offers multiple methods to generate a discrete portfolio allocation from continuous weights.
"""
import collections

import cvxpy as cp
import numpy as np
import pandas as pd
import cvxpy as cp

from . import exceptions


Expand Down Expand Up @@ -249,7 +251,7 @@ def greedy_portfolio(self, reinvest=False, verbose=False):
self._allocation_rmse_error(verbose)
return self.allocation, available_funds

def lp_portfolio(self, reinvest=False, verbose=False, solver="ECOS_BB"):
def lp_portfolio(self, reinvest=False, verbose=False, solver=None):
"""
Convert continuous weights into a discrete portfolio allocation
using integer programming.
Expand Down Expand Up @@ -319,9 +321,6 @@ def lp_portfolio(self, reinvest=False, verbose=False, solver="ECOS_BB"):
objective = cp.sum(u) + r

opt = cp.Problem(cp.Minimize(objective), constraints)

if solver is not None and solver not in cp.installed_solvers():
raise NameError("Solver {} is not installed. ".format(solver))
opt.solve(solver=solver)

if opt.status not in {"optimal", "optimal_inaccurate"}: # pragma: no cover
Expand Down
18 changes: 8 additions & 10 deletions tests/test_discrete_allocation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import numpy as np
import pandas as pd
import pytest
from cvxpy.error import SolverError

from pypfopt.discrete_allocation import get_latest_prices, DiscreteAllocation
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt.expected_returns import mean_historical_return
from pypfopt.risk_models import sample_cov
from tests.utilities_for_tests import get_data, setup_efficient_frontier


Expand Down Expand Up @@ -71,7 +69,7 @@ def test_greedy_allocation_rmse_error():
da.greedy_portfolio()

np.testing.assert_almost_equal(
da._allocation_rmse_error(verbose=False), 0.017086185150415774
da._allocation_rmse_error(verbose=True), 0.017086185150415774
)


Expand Down Expand Up @@ -132,7 +130,7 @@ def test_greedy_allocation_rmse_error_short():
da.greedy_portfolio()

np.testing.assert_almost_equal(
da._allocation_rmse_error(verbose=False), 0.06063511265243106
da._allocation_rmse_error(verbose=True), 0.06063511265243106
)


Expand Down Expand Up @@ -275,7 +273,7 @@ def test_lp_allocation_rmse_error():
latest_prices = get_latest_prices(df)
da = DiscreteAllocation(w, latest_prices, short_ratio=0.3)
da.lp_portfolio()
assert da._allocation_rmse_error(verbose=False) < 0.02
assert da._allocation_rmse_error(verbose=True) < 0.02


def test_lp_portfolio_allocation_short():
Expand Down Expand Up @@ -378,7 +376,7 @@ def test_lp_allocation_rmse_error_short():
latest_prices = get_latest_prices(df)
da = DiscreteAllocation(w, latest_prices, short_ratio=0.3)
da.lp_portfolio()
assert da._allocation_rmse_error(verbose=False) < 0.1
assert da._allocation_rmse_error(verbose=True) < 0.1


def test_lp_portfolio_allocation_different_params():
Expand Down Expand Up @@ -420,11 +418,11 @@ def test_rmse_decreases_with_value():

da1 = DiscreteAllocation(w, latest_prices, total_portfolio_value=10000)
da1.greedy_portfolio()
rmse1 = da1._allocation_rmse_error(verbose=False)
rmse1 = da1._allocation_rmse_error(verbose=True)

da2 = DiscreteAllocation(w, latest_prices, total_portfolio_value=100000)
da2.greedy_portfolio()
rmse2 = da2._allocation_rmse_error(verbose=False)
rmse2 = da2._allocation_rmse_error(verbose=True)

assert rmse2 < rmse1

Expand All @@ -445,7 +443,7 @@ def test_allocation_errors():
DiscreteAllocation(w, latest_prices, total_portfolio_value=0)
with pytest.raises(ValueError):
DiscreteAllocation(w, latest_prices, short_ratio=-0.4)
with pytest.raises(NameError):
with pytest.raises(SolverError):
da = DiscreteAllocation(w, latest_prices)
da.lp_portfolio(solver="ABCDEF")

Expand Down

0 comments on commit f655948

Please sign in to comment.