skfolio.metrics.make_scorer#
- skfolio.metrics.make_scorer(score_func, greater_is_better=None, response_method='predict', **kwargs)[source]#
Make a scorer from a measure, a portfolio score function, or a non-predictor estimator score function.
This function wraps scoring functions for use in model selection:
response_method="predict"(default): for portfolio optimization estimators (e.g.MeanRisk). Compatible withGridSearchCVandcross_val_score.response_method=None: for non-predictor estimators (covariance, expected returns, prior) that implementfitbut notpredict. Compatible with both sklearn cross-validation utilities and skfolio online utilities (OnlineGridSearch,online_score).
Note
For online evaluation of portfolio optimization estimators, pass a measure directly to the
scoringparameter instead of usingmake_scorer. Online evaluation scores the full aggregatedMultiPeriodPortfoliorather than averaging per-fold scores.- Parameters:
- score_funcMeasure | callable
If
score_funcis a measure, we return the measure of the predictedPortfoliotimes1or-1depending ongreater_is_better.response_methodmust be"predict"in this case.If
response_method="predict",score_funcmust be a score function (or loss function) with signaturescore_func(pred, **kwargs)wherepredis the predictedPortfolio.If
response_method=None,score_funcmust be a score function (or loss function) with signaturescore_func(estimator, X_test, **kwargs)whereestimatoris the fitted non-predictor estimator andX_testthe realized returns.- greater_is_betterbool, optional
Whether
score_funcis a score function (high is good) or a loss function (low is good). In the latter case the scorer sign-flips the outcome so that higher values always indicate a better model. The default (None) is:If
score_funcis a measure:TrueforPerfMeasureandRatioMeasure.FalseforRiskMeasureandExtraRiskMeasure.
Otherwise,
True.
- response_methodstr or None, default=”predict”
Determines how the scorer obtains predictions. Only
"predict"andNoneare supported:"predict": callestimator.predict(X)and pass the resultingPortfoliotoscore_func. Use for portfolio optimization estimators (e.g.MeanRisk).None: pass(estimator, X_test)directly toscore_funcwithout calling any response method. Use for non-predictor estimators (covariance, expected returns, prior).
- **kwargsadditional arguments
Additional parameters to be passed to
score_func.
- Returns:
- scorercallable
Callable object with signature
scorer(estimator, X, y=None)that returns a scalar score (higher is better).
Examples
Portfolio scorer from a measure:
>>> from skfolio.measures import RatioMeasure >>> scorer = make_scorer(RatioMeasure.SHARPE_RATIO)
Portfolio scorer from a custom function:
>>> def custom(pred): ... return pred.mean - 2 * pred.variance >>> scorer = make_scorer(custom)
Non-predictor estimator scorer for covariance evaluation:
>>> from skfolio.metrics import portfolio_variance_qlike_loss >>> import numpy as np >>> scorer = make_scorer( ... portfolio_variance_qlike_loss, ... greater_is_better=False, ... response_method=None, ... portfolio_weights=np.ones(20) / 20, ... )