Skip to content

Commit 210c058

Browse files
committed
add expected_returns and risk_model as parameters
1 parent 211b8d4 commit 210c058

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/empyrial/main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def __init__(
6565
optimizer=None,
6666
max_vol=0.15,
6767
diversification=1,
68+
expected_returns=None,
69+
risk_model=None,
6870
# confidences=None,
6971
# view=None,
7072
min_weights=None,
@@ -84,6 +86,12 @@ def __init__(
8486
self.rebalance = rebalance
8587
self.max_vol = max_vol
8688
self.diversification = diversification
89+
self.expected_returns = expected_returns
90+
if expected_returns is not None:
91+
assert expected_returns in ["mean_historical_return", "ema_historical_return", "capm_return"], f"Expected return method: {expected_returns} not supported yet!"
92+
self.risk_model = risk_model
93+
if risk_model is not None:
94+
assert risk_model in ["sample_cov", "semicovariance", "exp_cov", "ledoit_wolf", "ledoit_wolf_constant_variance", "ledoit_wolf_single_factor", "ledoit_wolf_constant_correlation", "oracle_approximating"], f"Risk model: {risk_model} not supported yet!"
8795
self.max_weights = max_weights
8896
self.min_weights = min_weights
8997
self.risk_manager = risk_manager
@@ -541,12 +549,12 @@ def efficient_frontier(my_portfolio, perf=True) -> list:
541549
# sometimes we will pick a date range where company isn't public we can't set price to 0 so it has to go to 1
542550
df = df.fillna(1)
543551

544-
mu = expected_returns.mean_historical_return(df)
545-
S = risk_models.sample_cov(df)
552+
mu = expected_returns.return_model(df, method=my_portfolio.expected_returns)
553+
S = risk_models.risk_matrix(df, method=my_portfolio.risk_model)
546554

547555
# optimize for max sharpe ratio
548556
ef = EfficientFrontier(mu, S)
549-
557+
ef.add_objective(objective_functions.L2_reg, gamma=my_portfolio.diversification)
550558
if my_portfolio.min_weights is not None:
551559
ef.add_constraint(lambda x: x >= my_portfolio.min_weights)
552560
if my_portfolio.max_weights is not None:
@@ -614,8 +622,8 @@ def mean_var(my_portfolio, vol_max=0.15, perf=True) -> list:
614622
# sometimes we will pick a date range where company isn't public we can't set price to 0 so it has to go to 1
615623
prices = prices.fillna(1)
616624

617-
mu = expected_returns.capm_return(prices)
618-
S = risk_models.CovarianceShrinkage(prices).ledoit_wolf()
625+
mu = expected_returns.return_model(prices, method=my_portfolio.expected_returns)
626+
S = risk_models.risk_matrix(prices, method=my_portfolio.risk_model)
619627

620628
ef = EfficientFrontier(mu, S)
621629
ef.add_objective(objective_functions.L2_reg, gamma=my_portfolio.diversification)
@@ -649,8 +657,8 @@ def min_var(my_portfolio, perf=True) -> list:
649657
prices = ohlc["Adj Close"].dropna(how="all")
650658
prices = prices.filter(my_portfolio.portfolio)
651659

652-
mu = expected_returns.capm_return(prices)
653-
S = risk_models.CovarianceShrinkage(prices).ledoit_wolf()
660+
mu = expected_returns.return_model(prices, method=my_portfolio.expected_returns)
661+
S = risk_models.risk_matrix(prices, method=my_portfolio.risk_model)
654662

655663
ef = EfficientFrontier(mu, S)
656664
ef.add_objective(objective_functions.L2_reg, gamma=my_portfolio.diversification)

0 commit comments

Comments
 (0)