skfolio.optimization
.Random#
- class skfolio.optimization.Random(portfolio_params=None)[source]#
Random weight estimator.
The assets weight are drawn from a Dirichlet distribution and sum to one.
- Parameters:
- portfolio_paramsdict, optional
Portfolio parameters passed to the portfolio evaluated by the
predict
andscore
methods. If not provided, thename
,transaction_costs
,management_fees
,previous_weights
andrisk_free_rate
are copied from the optimization model and passed to the portfolio.
- Attributes:
- weights_ndarray of shape (n_assets,) or (n_optimizations, n_assets)
Weights of the assets.
Methods
fit
(X[, y])Fit the Random Weighted estimator.
fit_predict
(X)Perform
fit
onX
and returns the predictedPortfolio
orPopulation
ofPortfolio
onX
based on the fittedweights
.Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Predict the
Portfolio
orPopulation
ofPortfolio
onX
based on the fitted weights.score
(X[, y])Prediction score.
set_params
(**params)Set the parameters of this estimator.
- fit(X, y=None)[source]#
Fit the Random Weighted estimator.
- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- yIgnored
Not used, present for API consistency by convention.
- Returns:
- selfEqualWeighted
Fitted estimator.
- fit_predict(X)#
Perform
fit
onX
and returns the predictedPortfolio
orPopulation
ofPortfolio
onX
based on the fittedweights
. For factor models, usefit(X, y)
thenpredict(X)
separately.- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- Returns:
- predictionPortfolio | Population
Portfolio
orPopulation
ofPortfolio
estimated onX
based on the fittedweights
.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X)#
Predict the
Portfolio
orPopulation
ofPortfolio
onX
based on the fitted weights.Optimization estimators can return a 1D or a 2D array of
weights
. For a 1D array, the prediction returns aPortfolio
. For a 2D array, the prediction returns aPopulation
ofPortfolio
.If
name
is not provided in the portfolio arguments, we use the first 500 characters of the estimator name.- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- Returns:
- predictionPortfolio | Population
Portfolio
orPopulation
ofPortfolio
estimated onX
based on the fittedweights
.
- score(X, y=None)#
Prediction score. If the prediction is a single
Portfolio
, the score is the Sharpe Ratio. If the prediction is aPopulation
ofPortfolio
, the score is the mean of all the portfolios Sharpe Ratios in the population.- Parameters:
- Xarray-like of shape (n_observations, n_assets)
Price returns of the assets.
- yIgnored
Not used, present here for API consistency by convention.
- Returns:
- scorefloat
The Sharpe Ratio of the portfolio if the prediction is a single
Portfolio
or the mean of all the portfolios Sharpe Ratios if the prediction is aPopulation
ofPortfolio
.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.