Factor_prices = load_factors_dataset() X_full, F_full = prices_to_returns(worth, factors_price) X_tr, X_te, F_tr, F_te = train_test_split( risk_measure=RiskMeasure.VARIANCE, prior_estimator=FactorModel(), ) fm.match(X_tr, F_tr) ptf_fm = fm.predict(X_te); ptf_fm.title = “Issue Mannequin” print(f”nFactor Mannequin Sharp: {ptf_fm.annualized_sharpe_ratio:.3f}”) Pipe = Pipeline([
(“preselect”, SelectKExtremes(k=8, highest=True)),
(“optimize”, MeanRisk(
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
risk_measure=RiskMeasure.VARIANCE)),
]) Pipe.match ( cv=WalkForward(train_size=252*2, test_size=63), n_jobs=-1, ) mp_portfolio.title = “WalkForward Max Sharpe” print(f”nWalkForward Portfolio Sharpe={mp_portfolio.annualized_sharpe_ratio:.3f} ” f”CalmarRatio={mp_portfolio.calmar_ratio:.3f}”) mp_portfolio.plot_cumulative_returns().present()tuned = MeanRisk(objective_function=ObjectiveFunction.MAXIMIZE_RATIO,risk_measure=RiskMeasure.VARIANCE,prior_estimator=EmpiricalPrior(mu_estimator=EWMu(alpha=0.1)), )grid = GridSearchCV( estimator=tuned, cv=WalkForward(train_size=252*2, test_size=63), n_jobs=-1, param_grid={ “l2_coef”: [0.0, 0.01, 0.1]”prior_estimator__mu_estimator__alpha”: [0.05, 0.1, 0.2, 0.5]}, ) Grid.match(X_train) print(“nBest params:”, Grid.best_params_) print(f”Finest CV Rating (Sharpe): {grid.best_score_:.3f}”) ptf_tuned = Grid.best_estimator_.predict(X_test); ptf_tuned.title = “Adjusted Max Sharp” Remaining = Inhabitants([
*baseline_population,
ptf_min_var, ptf_max_sharpe,
ptf_rb_var, ptf_rb_cvar,
ptf_hrp, ptf_nco,
ptf_robust, ptf_gerber,
ptf_constr, ptf_bl, ptf_fm,
ptf_pipe, ptf_tuned,
]) _full = Remaining.abstract() _wanted_final = [
“Annualized Mean”, “Annualized Standard Deviation”,
“Annualized Sharpe Ratio”, “Annualized Sortino Ratio”,
“CVaR at 95%”, “Maximum Drawdown”, “Max Drawdown”,
]
_have_final = [r for r in _wanted_final if r in _full.index]
Abstract = _full.loc[_have_final].T.sort_values( “Annualized Sharpe Ratio”, ascending=False ) print(“n” + “= * 80) print(“Remaining Horse Racing — Type by Sharpe (out-of-sample check set)”) print(“=” * 80) print(abstract.to_string()) Remaining.plot_cumulative_returns().present() Remaining.plot_composition().present() ptf_rb_var.plot_contribution(measure=RiskMeasure.VARIANCE).present() print(“nDone. Attempt exchanging threat indicators, including constraints, or connecting.”) print(“Your personal information body might be returned. All estimators observe the sklearn API.”)
(“preselect”, SelectKExtremes(k=8, highest=True)),
(“optimize”, MeanRisk(
objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
risk_measure=RiskMeasure.VARIANCE)),
]) Pipe.match ( cv=WalkForward(train_size=252*2, test_size=63), n_jobs=-1, ) mp_portfolio.title = “WalkForward Max Sharpe” print(f”nWalkForward Portfolio Sharpe={mp_portfolio.annualized_sharpe_ratio:.3f} ” f”CalmarRatio={mp_portfolio.calmar_ratio:.3f}”) mp_portfolio.plot_cumulative_returns().present()tuned = MeanRisk(objective_function=ObjectiveFunction.MAXIMIZE_RATIO,risk_measure=RiskMeasure.VARIANCE,prior_estimator=EmpiricalPrior(mu_estimator=EWMu(alpha=0.1)), )grid = GridSearchCV( estimator=tuned, cv=WalkForward(train_size=252*2, test_size=63), n_jobs=-1, param_grid={ “l2_coef”: [0.0, 0.01, 0.1]”prior_estimator__mu_estimator__alpha”: [0.05, 0.1, 0.2, 0.5]}, ) Grid.match(X_train) print(“nBest params:”, Grid.best_params_) print(f”Finest CV Rating (Sharpe): {grid.best_score_:.3f}”) ptf_tuned = Grid.best_estimator_.predict(X_test); ptf_tuned.title = “Adjusted Max Sharp” Remaining = Inhabitants([
*baseline_population,
ptf_min_var, ptf_max_sharpe,
ptf_rb_var, ptf_rb_cvar,
ptf_hrp, ptf_nco,
ptf_robust, ptf_gerber,
ptf_constr, ptf_bl, ptf_fm,
ptf_pipe, ptf_tuned,
]) _full = Remaining.abstract() _wanted_final = [
“Annualized Mean”, “Annualized Standard Deviation”,
“Annualized Sharpe Ratio”, “Annualized Sortino Ratio”,
“CVaR at 95%”, “Maximum Drawdown”, “Max Drawdown”,
]
_have_final = [r for r in _wanted_final if r in _full.index]
Abstract = _full.loc[_have_final].T.sort_values( “Annualized Sharpe Ratio”, ascending=False ) print(“n” + “= * 80) print(“Remaining Horse Racing — Type by Sharpe (out-of-sample check set)”) print(“=” * 80) print(abstract.to_string()) Remaining.plot_cumulative_returns().present() Remaining.plot_composition().present() ptf_rb_var.plot_contribution(measure=RiskMeasure.VARIANCE).present() print(“nDone. Attempt exchanging threat indicators, including constraints, or connecting.”) print(“Your personal information body might be returned. All estimators observe the sklearn API.”)


