Skip to content

Commit

Permalink
More testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Divasco committed Oct 9, 2024
1 parent 43a9161 commit 1543f11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions garpar/optimize/mean_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def _coerce_target_return(self, pf):

def _coerce_target_volatility(self, pf):
if self.target_risk is None:
return np.min(np.std(pf.as_prices()))
return np.min(np.std(pf.as_prices(), axis=0))
return self.target_risk

def _calculate_weights(self, pf):
optimizer = self._get_optimizer(pf)
method = self.method

optimization_methods = {
"min_volatility": self._min_volatility,
"max_sharpe": self._max_sharpe,
Expand All @@ -77,10 +77,10 @@ def _calculate_weights(self, pf):
"efficient_return": self._efficient_return,
"portfolio_performance": self._portfolio_performance,
}

if method not in optimization_methods:
raise ValueError(f"Unknown optimization method: {method}")

return optimization_methods[method](optimizer, pf)

def _min_volatility(self, optimizer, pf):
Expand Down
22 changes: 22 additions & 0 deletions tests/optimize/test_mean_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from garpar.optimize.mean_variance import MVOptimizer, Markowitz

import pypfopt

from garpar import datasets

import numpy as np
Expand Down Expand Up @@ -69,6 +71,26 @@ def test_MVOptimizer_coerce_target_return(risso_portfolio, price_distribution):
coerced_return = optimizer._coerce_target_return(pf)
assert coerced_return == 0.05 # The minimum absolute return from mock portfolio

@pytest.mark.parametrize("price_distribution", pytest.DISTRIBUTIONS)
def test_MVOptimizer_get_optimizer(risso_portfolio, price_distribution):
pf = risso_portfolio(random_state=42, distribution=price_distribution)
optimizer = MVOptimizer()
assert type(optimizer._get_optimizer(pf)) == pypfopt.efficient_frontier.EfficientFrontier

@pytest.mark.parametrize(
"volatiliy, price_distribution",
[
(0.03313144315467211, pytest.DISTRIBUTIONS['levy-stable']),
(0.8509377578214843, pytest.DISTRIBUTIONS['normal']),
(13.383798092382262, pytest.DISTRIBUTIONS['uniform'])
]
)
def test_MVOptimizer_coerce_volatiliy(volatiliy, price_distribution):
pf = price_distribution(random_state=43)
optimizer = MVOptimizer(method="max-sharpe")
coerced_volatility = optimizer._coerce_target_volatility(pf)
assert coerced_volatility == pytest.approx(volatiliy, 1e-9)

# =============================================================================
# MARKOWITZ TEST
# =============================================================================
Expand Down

0 comments on commit 1543f11

Please sign in to comment.