@@ -65,6 +65,8 @@ def __init__(
65
65
optimizer = None ,
66
66
max_vol = 0.15 ,
67
67
diversification = 1 ,
68
+ expected_returns = None ,
69
+ risk_model = None ,
68
70
# confidences=None,
69
71
# view=None,
70
72
min_weights = None ,
@@ -84,6 +86,12 @@ def __init__(
84
86
self .rebalance = rebalance
85
87
self .max_vol = max_vol
86
88
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!"
87
95
self .max_weights = max_weights
88
96
self .min_weights = min_weights
89
97
self .risk_manager = risk_manager
@@ -541,12 +549,12 @@ def efficient_frontier(my_portfolio, perf=True) -> list:
541
549
# 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
542
550
df = df .fillna (1 )
543
551
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 )
546
554
547
555
# optimize for max sharpe ratio
548
556
ef = EfficientFrontier (mu , S )
549
-
557
+ ef . add_objective ( objective_functions . L2_reg , gamma = my_portfolio . diversification )
550
558
if my_portfolio .min_weights is not None :
551
559
ef .add_constraint (lambda x : x >= my_portfolio .min_weights )
552
560
if my_portfolio .max_weights is not None :
@@ -614,8 +622,8 @@ def mean_var(my_portfolio, vol_max=0.15, perf=True) -> list:
614
622
# 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
615
623
prices = prices .fillna (1 )
616
624
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 )
619
627
620
628
ef = EfficientFrontier (mu , S )
621
629
ef .add_objective (objective_functions .L2_reg , gamma = my_portfolio .diversification )
@@ -649,8 +657,8 @@ def min_var(my_portfolio, perf=True) -> list:
649
657
prices = ohlc ["Adj Close" ].dropna (how = "all" )
650
658
prices = prices .filter (my_portfolio .portfolio )
651
659
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 )
654
662
655
663
ef = EfficientFrontier (mu , S )
656
664
ef .add_objective (objective_functions .L2_reg , gamma = my_portfolio .diversification )
0 commit comments