skfolio 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. skfolio/__init__.py +29 -0
  2. skfolio/cluster/__init__.py +8 -0
  3. skfolio/cluster/_hierarchical.py +387 -0
  4. skfolio/datasets/__init__.py +20 -0
  5. skfolio/datasets/_base.py +389 -0
  6. skfolio/datasets/data/__init__.py +0 -0
  7. skfolio/datasets/data/factors_dataset.csv.gz +0 -0
  8. skfolio/datasets/data/sp500_dataset.csv.gz +0 -0
  9. skfolio/datasets/data/sp500_index.csv.gz +0 -0
  10. skfolio/distance/__init__.py +26 -0
  11. skfolio/distance/_base.py +55 -0
  12. skfolio/distance/_distance.py +574 -0
  13. skfolio/exceptions.py +30 -0
  14. skfolio/measures/__init__.py +76 -0
  15. skfolio/measures/_enums.py +355 -0
  16. skfolio/measures/_measures.py +607 -0
  17. skfolio/metrics/__init__.py +3 -0
  18. skfolio/metrics/_scorer.py +121 -0
  19. skfolio/model_selection/__init__.py +18 -0
  20. skfolio/model_selection/_combinatorial.py +407 -0
  21. skfolio/model_selection/_validation.py +194 -0
  22. skfolio/model_selection/_walk_forward.py +221 -0
  23. skfolio/moments/__init__.py +41 -0
  24. skfolio/moments/covariance/__init__.py +29 -0
  25. skfolio/moments/covariance/_base.py +101 -0
  26. skfolio/moments/covariance/_covariance.py +1108 -0
  27. skfolio/moments/expected_returns/__init__.py +21 -0
  28. skfolio/moments/expected_returns/_base.py +31 -0
  29. skfolio/moments/expected_returns/_expected_returns.py +415 -0
  30. skfolio/optimization/__init__.py +36 -0
  31. skfolio/optimization/_base.py +147 -0
  32. skfolio/optimization/cluster/__init__.py +13 -0
  33. skfolio/optimization/cluster/_nco.py +348 -0
  34. skfolio/optimization/cluster/hierarchical/__init__.py +13 -0
  35. skfolio/optimization/cluster/hierarchical/_base.py +440 -0
  36. skfolio/optimization/cluster/hierarchical/_herc.py +406 -0
  37. skfolio/optimization/cluster/hierarchical/_hrp.py +368 -0
  38. skfolio/optimization/convex/__init__.py +16 -0
  39. skfolio/optimization/convex/_base.py +1944 -0
  40. skfolio/optimization/convex/_distributionally_robust.py +392 -0
  41. skfolio/optimization/convex/_maximum_diversification.py +417 -0
  42. skfolio/optimization/convex/_mean_risk.py +974 -0
  43. skfolio/optimization/convex/_risk_budgeting.py +560 -0
  44. skfolio/optimization/ensemble/__init__.py +6 -0
  45. skfolio/optimization/ensemble/_base.py +87 -0
  46. skfolio/optimization/ensemble/_stacking.py +326 -0
  47. skfolio/optimization/naive/__init__.py +3 -0
  48. skfolio/optimization/naive/_naive.py +173 -0
  49. skfolio/population/__init__.py +3 -0
  50. skfolio/population/_population.py +883 -0
  51. skfolio/portfolio/__init__.py +13 -0
  52. skfolio/portfolio/_base.py +1096 -0
  53. skfolio/portfolio/_multi_period_portfolio.py +610 -0
  54. skfolio/portfolio/_portfolio.py +842 -0
  55. skfolio/pre_selection/__init__.py +7 -0
  56. skfolio/pre_selection/_pre_selection.py +342 -0
  57. skfolio/preprocessing/__init__.py +3 -0
  58. skfolio/preprocessing/_returns.py +114 -0
  59. skfolio/prior/__init__.py +18 -0
  60. skfolio/prior/_base.py +63 -0
  61. skfolio/prior/_black_litterman.py +238 -0
  62. skfolio/prior/_empirical.py +163 -0
  63. skfolio/prior/_factor_model.py +268 -0
  64. skfolio/typing.py +50 -0
  65. skfolio/uncertainty_set/__init__.py +23 -0
  66. skfolio/uncertainty_set/_base.py +108 -0
  67. skfolio/uncertainty_set/_bootstrap.py +281 -0
  68. skfolio/uncertainty_set/_empirical.py +237 -0
  69. skfolio/utils/__init__.py +0 -0
  70. skfolio/utils/bootstrap.py +115 -0
  71. skfolio/utils/equations.py +350 -0
  72. skfolio/utils/sorting.py +117 -0
  73. skfolio/utils/stats.py +466 -0
  74. skfolio/utils/tools.py +567 -0
  75. skfolio-0.0.1.dist-info/LICENSE +29 -0
  76. skfolio-0.0.1.dist-info/METADATA +568 -0
  77. skfolio-0.0.1.dist-info/RECORD +79 -0
  78. skfolio-0.0.1.dist-info/WHEEL +5 -0
  79. skfolio-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,560 @@
1
+ """Risk Budgeting Optimization estimator."""
2
+
3
+ # Author: Hugo Delatte <delatte.hugo@gmail.com>
4
+ # License: BSD 3 clause
5
+
6
+ import cvxpy as cp
7
+ import numpy as np
8
+ import numpy.typing as npt
9
+
10
+ import skfolio.typing as skt
11
+ from skfolio.measures import RiskMeasure
12
+ from skfolio.optimization.convex._base import ConvexOptimization
13
+ from skfolio.prior import BasePrior, EmpiricalPrior
14
+ from skfolio.utils.tools import args_names, check_estimator
15
+
16
+
17
+ class RiskBudgeting(ConvexOptimization):
18
+ r"""Risk Budgeting Optimization estimator.
19
+
20
+ The Risk Budgeting estimator solves the below convex problem:
21
+
22
+ .. math:: \begin{cases}
23
+ \begin{aligned}
24
+ &\min_{w} & & risk_{i}(w) \\
25
+ &\text{s.t.} & & b^T \cdot log(w) \ge c \\
26
+ & & & w^T \cdot \mu \ge min\_return \\
27
+ & & & A \cdot w \ge b \\
28
+ & & & w \ge 0
29
+ \end{aligned}
30
+ \end{cases}
31
+
32
+ with :math:`b` the risk budget vector and :math:`c` an auxiliary variable of
33
+ the log barrier.
34
+
35
+ And :math:`risk_{i}` a risk measure among:
36
+
37
+ * Mean Absolute Deviation
38
+ * First Lower Partial Moment
39
+ * Variance
40
+ * Semi-Variance
41
+ * CVaR (Conditional Value at Risk)
42
+ * EVaR (Entropic Value at Risk)
43
+ * Worst Realization (worst return)
44
+ * CDaR (Conditional Drawdown at Risk)
45
+ * Maximum Drawdown
46
+ * Average Drawdown
47
+ * EDaR (Entropic Drawdown at Risk)
48
+ * Ulcer Index
49
+ * Gini Mean Difference
50
+
51
+ Cost and additional constraints can also be added to the optimization problem (see
52
+ the parameters description).
53
+
54
+ Limitations are imposed on some constraints including long only weights to ensure
55
+ convexity.
56
+
57
+ The assets expected returns, covariance matrix and returns are estimated from the
58
+ :ref:`prior estimator <prior>`.
59
+
60
+ Parameters
61
+ ----------
62
+ risk_measure : RiskMeasure, default=RiskMeasure.VARIANCE
63
+ :class:`~skfolio.meta.RiskMeasure` of the optimization.
64
+ Can be any of:
65
+
66
+ * VARIANCE
67
+ * SEMI_VARIANCE
68
+ * STANDARD_DEVIATION
69
+ * SEMI_DEVIATION
70
+ * MEAN_ABSOLUTE_DEVIATION
71
+ * FIRST_LOWER_PARTIAL_MOMENT
72
+ * CVAR
73
+ * EVAR
74
+ * WORST_REALIZATION
75
+ * CDAR
76
+ * MAX_DRAWDOWN
77
+ * AVERAGE_DRAWDOWN
78
+ * EDAR
79
+ * ULCER_INDEX
80
+ * GINI_MEAN_DIFFERENCE_RATIO
81
+
82
+ The default is `RiskMeasure.VARIANCE`.
83
+
84
+ risk_budget : dict[str, float] | array-like of shape (n_assets,), optional
85
+ Risk budget allocated to each asset.
86
+ If a dictionary is provided, its (key/value) pair must be the
87
+ (asset name/asset risk budget) and the input `X` of the `fit` methods must be a
88
+ DataFrame with the assets names in columns.
89
+ The default (`None`) is to use the identity vector, reducing the risk
90
+ budgeting to a risk-parity (each asset contributing equally to the total risk).
91
+
92
+ prior_estimator : BasePrior, optional
93
+ :ref:`Prior estimator <prior>`.
94
+ The prior estimator is used to estimate the :class:`~skfolio.prior.PriorModel`
95
+ containing the estimation of assets expected returns, covariance matrix,
96
+ returns and Cholesky decomposition of the covariance.
97
+ The default (`None`) is to use :class:`~skfolio.prior.EmpiricalPrior`.
98
+
99
+ min_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=0.0
100
+ Minimum assets weights (weights lower bounds).
101
+ If a float is provided, it is applied to each asset.
102
+ `None` is equivalent to `-np.Inf` (no lower bound).
103
+ If a dictionary is provided, its (key/value) pair must be the
104
+ (asset name/asset minium weight) and the input `X` of the `fit` methods must
105
+ be a DataFrame with the assets names in columns.
106
+ When using a dictionary, assets values that are not provided are assigned
107
+ a minimum weight of `0.0`.
108
+ The default value is `0.0` (no short selling).
109
+
110
+ Example:
111
+
112
+ * `min_weights = 0` --> long only portfolio (no short selling).
113
+ * `min_weights = None` --> no lower bound (same as `-np.Inf`).
114
+ * `min_weights = -2` --> each weight must be above -200%.
115
+ * `min_weights = {"SX5E": 0, "SPX": -2}`
116
+ * `min_weights = [0, -2]`
117
+
118
+ max_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=1.0
119
+ Maximum assets weights (weights upper bounds).
120
+ If a float is provided, it is applied to each asset.
121
+ `None` is equivalent to `+np.Inf` (no upper bound).
122
+ If a dictionary is provided, its (key/value) pair must be the
123
+ (asset name/asset maximum weight) and the input `X` of the `fit` methods must
124
+ be a DataFrame with the assets names in columns.
125
+ When using a dictionary, assets values that are not provided are assigned
126
+ a minimum weight of `1.0`.
127
+ The default value is `1.0` (each asset is below 100%).
128
+
129
+ Example:
130
+
131
+ * `max_weights = 0` --> no long position (short only portfolio).
132
+ * `max_weights = None` --> no upper bound.
133
+ * `max_weights = 2` --> each weight must be below 200%.
134
+ * `max_weights = {"SX5E": 1, "SPX": 2}`
135
+ * `max_weights = [1, 2]`
136
+
137
+ transaction_costs : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
138
+ Transaction costs of the assets. It is used to add linear transaction costs to
139
+ the optimization problem:
140
+
141
+ .. math:: total\_cost = \sum_{i=1}^{N} c_{i} \times |w_{i} - w\_prev_{i}|
142
+
143
+ with :math:`c_{i}` the transaction cost of asset i, :math:`w_{i}` its weight
144
+ and :math:`w\_prev_{i}` its previous weight (defined in `previous_weights`).
145
+ The float :math:`total\_cost` is used in the portfolio expected return:
146
+
147
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_cost
148
+
149
+ with :math:`\mu` the vector af assets' expected returns and :math:`w` the
150
+ vector of assets weights.
151
+
152
+ If a float is provided, it is applied to each asset.
153
+ If a dictionary is provided, its (key/value) pair must be the
154
+ (asset name/asset cost) and the input `X` of the `fit` methods must be a
155
+ DataFrame with the assets names in columns.
156
+ The default value is `0.0`.
157
+
158
+ .. warning::
159
+
160
+ Based on the above formula, the periodicity of the transaction costs
161
+ needs to be homogenous to the periodicity of :math:`\mu`. For example, if
162
+ the input `X` is composed of **daily** returns, the `transaction_costs` need
163
+ to be expressed in **daily** costs.
164
+ (See :ref:`sphx_glr_auto_examples_1_mean_risk_plot_6_transaction_costs.py`)
165
+
166
+ management_fees : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
167
+ Management fees of the assets. It is used to add linear management fees to the
168
+ optimization problem:
169
+
170
+ .. math:: total\_fee = \sum_{i=1}^{N} f_{i} \times w_{i}
171
+
172
+ with :math:`f_{i}` the management fee of asset i and :math:`w_{i}` its weight.
173
+ The float :math:`total\_fee` is used in the portfolio expected return:
174
+
175
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_fee
176
+
177
+ with :math:`\mu` the vector af assets expected returns and :math:`w` the vector
178
+ of assets weights.
179
+
180
+ If a float is provided, it is applied to each asset.
181
+ If a dictionary is provided, its (key/value) pair must be the
182
+ (asset name/asset fee) and the input `X` of the `fit` methods must be a
183
+ DataFrame with the assets names in columns.
184
+ The default value is `0.0`.
185
+
186
+ .. warning::
187
+
188
+ Based on the above formula, the periodicity of the management fees needs to
189
+ be homogenous to the periodicity of :math:`\mu`. For example, if the input
190
+ `X` is composed of **daily** returns, the `management_fees` need to be
191
+ expressed in **daily** fees.
192
+
193
+ .. note::
194
+
195
+ Another approach is to directly impact the management fees to the input `X`
196
+ in order to express the returns net of fees. However, when estimating the
197
+ :math:`\mu` parameter using for example Shrinkage estimators, this approach
198
+ would mix a deterministic value with an uncertain one leading to unwanted
199
+ bias in the management fees.
200
+
201
+ previous_weights : float | dict[str, float] | array-like of shape (n_assets, ), optional
202
+ Previous weights of the assets. Previous weights are used to compute the
203
+ portfolio cost and the portfolio turnover.
204
+ If a float is provided, it is applied to each asset.
205
+ If a dictionary is provided, its (key/value) pair must be the
206
+ (asset name/asset previous weight) and the input `X` of the `fit` methods must
207
+ be a DataFrame with the assets names in columns.
208
+ The default (`None`) means no previous weights.
209
+
210
+ linear_constraints : array-like of shape (n_constraints,), optional
211
+ Linear constraints.
212
+ The linear constraints must match any of following patterns:
213
+
214
+ * "2.5 * ref1 + 0.10 * ref2 + 0.0013 <= 2.5 * ref3"
215
+ * "ref1 >= 2.9 * ref2"
216
+ * "ref1 <= ref2"
217
+ * "ref1 >= ref1"
218
+
219
+ With "ref1", "ref2" ... the assets names or the groups names provided
220
+ in the parameter `groups`. Assets names can be referenced without the need of
221
+ `groups` if the input `X` of the `fit` methods is a DataFrame with these
222
+ assets names in columns.
223
+
224
+ Examples:
225
+
226
+ * "SPX >= 0.10" --> SPX weight must be greater than 10% (note that you can also use `min_weights`)
227
+ * "SX5E + TLT >= 0.2" --> the sum of SX5E and TLT weights must be greater than 20%
228
+ * "US >= 0.7" --> the sum of all US weights must be greater than 70%
229
+ * "Equity <= 3 * Bond" --> the sum of all Equity weights must be less or equal to 3 times the sum of all Bond weights.
230
+ * "2*SPX + 3*Europe <= Bond + 0.05" --> mixing assets and group constraints
231
+
232
+ groups : dict[str, list[str]] or array-like of shape (n_groups, n_assets), optional
233
+ The assets groups referenced in `linear_constraints`.
234
+ If a dictionary is provided, its (key/value) pair must be the
235
+ (asset name/asset groups) and the input `X` of the `fit` methods must be a
236
+ DataFrame with the assets names in columns.
237
+
238
+ Examples:
239
+
240
+ * groups = {"SX5E": ["Equity", "Europe"], "SPX": ["Equity", "US"], "TLT": ["Bond", "US"]}
241
+ * groups = [["Equity", "Equity", "Bond"], ["Europe", "US", "US"]]
242
+
243
+ left_inequality : array-like of shape (n_constraints, n_assets), optional
244
+ Left inequality matrix :math:`A` of the linear
245
+ constraint :math:`A \cdot w \leq b`.
246
+
247
+ right_inequality : array-like of shape (n_constraints, ), optional
248
+ Right inequality vector :math:`b` of the linear
249
+ constraint :math:`A \cdot w \leq b`.
250
+
251
+ risk_free_rate : float, default=0.0
252
+ Risk-free interest rate.
253
+ The default value is `0.0`.
254
+
255
+ min_return : float | array-like of shape (n_optimization), optional
256
+ Lower bound constraint on the expected return.
257
+
258
+ min_acceptable_return : float, optional
259
+ The minimum acceptable return used to distinguish "downside" and "upside"
260
+ returns for the computation of lower partial moments:
261
+
262
+ * First Lower Partial Moment
263
+ * Semi-Variance
264
+ * Semi-Deviation
265
+
266
+ The default (`None`) is to use the mean.
267
+
268
+ cvar_beta : float, default=0.95
269
+ CVaR (Conditional Value at Risk) confidence level.
270
+
271
+ evar_beta : float, default=0
272
+ EVaR (Entropic Value at Risk) confidence level.
273
+
274
+ cvar_beta : float, default=0.95
275
+ CVaR (Conditional Value at Risk) confidence level.
276
+ The default value is `0.95`.
277
+
278
+ evar_beta : float, default=0
279
+ EVaR (Entropic Value at Risk) confidence level.
280
+ The default value is `0.95`.
281
+
282
+ cdar_beta : float, default=0.95
283
+ CDaR (Conditional Drawdown at Risk) confidence level.
284
+ The default value is `0.95`.
285
+
286
+ edar_beta : float, default=0.95
287
+ EDaR (Entropic Drawdown at Risk) confidence level.
288
+ The default value is `0.95`.
289
+
290
+ add_objective : Callable[[cp.Variable], cp.Expression], optional
291
+ Add a custom objective to the existing objective expression.
292
+ It is a function that must take as argument the weights `w` and returns a
293
+ CVXPY expression.
294
+
295
+ add_constraints : Callable[[cp.Variable], cp.Expression|list[cp.Expression]], optional
296
+ Add a custom constraint or a list of constraints to the existing constraints.
297
+ It is a function that must take as argument the weights `w` and returns a
298
+ CVPXY expression or a list of CVPXY expressions.
299
+
300
+ overwrite_expected_return : Callable[[cp.Variable], cp.Expression], optional
301
+ Overwrite the expected return :math:`\mu \cdot w` with a custom expression.
302
+ It is a function that must take as argument the weights `w` and returns a
303
+ CVPXY expression.
304
+
305
+ solver : str, optional
306
+ The solver to use. For example, "ECOS", "SCS", or "OSQP".
307
+ The default (`None`) is set depending on the problem.
308
+ For more details about available solvers, check the CVXPY documentation:
309
+ https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver
310
+
311
+ solver_params : dict, optional
312
+ Solver parameters. For example, `solver_params=dict(verbose=True)`.
313
+ For more details about solver arguments, check the CVXPY documentation:
314
+ https://www.cvxpy.org/tutorial/advanced/index.html#setting-solver-options
315
+
316
+ scale_objective : float, optional
317
+ Scale each objective element by this value.
318
+ It can be used to increase the optimization accuracies in specific cases.
319
+ The default (`None`) is set depending on the problem.
320
+
321
+ scale_constraints : float, optional
322
+ Scale each constraint element by this value.
323
+ It can be used to increase the optimization accuracies in specific cases.
324
+ The default (`None`) is set depending on the problem.
325
+
326
+ raise_on_failure : bool, default=True
327
+ If this is set to True, an error is raised when the optimization fail otherwise
328
+ it passes with a warning.
329
+
330
+ portfolio_params : dict, optional
331
+ Portfolio parameters passed to the portfolio evaluated by the `predict` and
332
+ `score` methods. If not provided, the `name`, `transaction_costs`,
333
+ `management_fees` and `previous_weights` are copied from the optimization
334
+ model and systematically passed to the portfolio.
335
+
336
+ Attributes
337
+ ----------
338
+ weights_ : ndarray of shape (n_assets,) or (n_optimizations, n_assets)
339
+ Weights of the assets.
340
+
341
+ problem_: cvxpy.Problem
342
+ CVXPY problem used for the optimization.
343
+
344
+ problem_values_ : dict[str, float] | list[dict[str, float]] of size n_optimizations
345
+ Expression values retrieved from the CVXPY problem.
346
+
347
+ prior_estimator_ : BasePrior
348
+ Fitted `prior_estimator`.
349
+
350
+ n_features_in_ : int
351
+ Number of assets seen during `fit`.
352
+
353
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
354
+ Names of assets seen during `fit`. Defined only when `X`
355
+ has assets names that are all strings.
356
+ """
357
+
358
+ def __init__(
359
+ self,
360
+ risk_measure: RiskMeasure = RiskMeasure.VARIANCE,
361
+ risk_budget: np.ndarray | None = None,
362
+ prior_estimator: BasePrior | None = None,
363
+ min_weights: skt.MultiInput | None = 0.0,
364
+ max_weights: skt.MultiInput | None = 1.0,
365
+ transaction_costs: skt.MultiInput = 0.0,
366
+ management_fees: skt.MultiInput = 0.0,
367
+ previous_weights: skt.MultiInput | None = None,
368
+ groups: skt.Groups | None = None,
369
+ linear_constraints: skt.LinearConstraints | None = None,
370
+ left_inequality: skt.Inequality | None = None,
371
+ right_inequality: skt.Inequality | None = None,
372
+ risk_free_rate: float = 0.0,
373
+ min_return: skt.Target | None = None,
374
+ min_acceptable_return: skt.Target | None = None,
375
+ cvar_beta: float = 0.95,
376
+ evar_beta: float = 0.95,
377
+ cdar_beta: float = 0.95,
378
+ edar_beta: float = 0.95,
379
+ solver: str | None = None,
380
+ solver_params: dict | None = None,
381
+ scale_objective: float | None = None,
382
+ scale_constraints: float | None = None,
383
+ raise_on_failure: bool = True,
384
+ add_objective: skt.ExpressionFunction | None = None,
385
+ add_constraints: skt.ExpressionFunction | None = None,
386
+ overwrite_expected_return: skt.ExpressionFunction | None = None,
387
+ portfolio_params: dict | None = None,
388
+ ):
389
+ super().__init__(
390
+ risk_measure=risk_measure,
391
+ prior_estimator=prior_estimator,
392
+ min_weights=min_weights,
393
+ max_weights=max_weights,
394
+ budget=1,
395
+ transaction_costs=transaction_costs,
396
+ management_fees=management_fees,
397
+ previous_weights=previous_weights,
398
+ groups=groups,
399
+ linear_constraints=linear_constraints,
400
+ left_inequality=left_inequality,
401
+ right_inequality=right_inequality,
402
+ risk_free_rate=risk_free_rate,
403
+ min_acceptable_return=min_acceptable_return,
404
+ cvar_beta=cvar_beta,
405
+ evar_beta=evar_beta,
406
+ cdar_beta=cdar_beta,
407
+ edar_beta=edar_beta,
408
+ solver=solver,
409
+ solver_params=solver_params,
410
+ scale_objective=scale_objective,
411
+ scale_constraints=scale_constraints,
412
+ raise_on_failure=raise_on_failure,
413
+ add_objective=add_objective,
414
+ add_constraints=add_constraints,
415
+ overwrite_expected_return=overwrite_expected_return,
416
+ portfolio_params=portfolio_params,
417
+ )
418
+ self.min_return = min_return
419
+ self.risk_budget = risk_budget
420
+
421
+ def _validation(self) -> None:
422
+ if not isinstance(self.risk_measure, RiskMeasure):
423
+ raise TypeError("risk_measure must be of type `RiskMeasure`")
424
+ if self.min_weights < 0:
425
+ raise ValueError(
426
+ "Risk Budgeting must have non negative `min_weights` constraint"
427
+ " otherwise the problem becomes non-convex."
428
+ )
429
+
430
+ def fit(self, X: npt.ArrayLike, y=None) -> "RiskBudgeting":
431
+ """Fit the Risk Budgeting Optimization estimator.
432
+
433
+ Parameters
434
+ ----------
435
+ X : array-like of shape (n_observations, n_assets)
436
+ Price returns of the assets.
437
+
438
+ y : array-like of shape (n_observations, n_factors), optional
439
+ Price returns of factors.
440
+ The default is `None`.
441
+
442
+
443
+ Returns
444
+ -------
445
+ self : RiskBudgeting
446
+ Fitted estimator.
447
+ """
448
+ self._check_feature_names(X, reset=True)
449
+ # Validate
450
+ self._validation()
451
+ # Used to avoid adding multiple times similar constrains linked to identical
452
+ # risk models
453
+ self._clear_models_cache()
454
+ self.prior_estimator_ = check_estimator(
455
+ self.prior_estimator,
456
+ default=EmpiricalPrior(),
457
+ check_type=BasePrior,
458
+ )
459
+ self.prior_estimator_.fit(X, y)
460
+ prior_model = self.prior_estimator_.prior_model_
461
+ n_observations, n_assets = prior_model.returns.shape
462
+
463
+ # set solvers
464
+ self._set_solver(default="SCS")
465
+
466
+ # set scale
467
+ self._set_scale_objective(default=1)
468
+ self._set_scale_constraints(default=1)
469
+
470
+ # Risk budget
471
+ risk_budget = self.risk_budget
472
+ if risk_budget is None:
473
+ risk_budget = np.ones(n_assets)
474
+ else:
475
+ risk_budget = self._clean_input(
476
+ self.risk_budget,
477
+ n_assets=n_assets,
478
+ fill_value=1e-10,
479
+ name="risk_budget",
480
+ )
481
+ risk_budget[risk_budget == 0] = 1e-10
482
+
483
+ # Variables
484
+ w = cp.Variable(n_assets)
485
+ factor = cp.Variable()
486
+ c = cp.Variable(nonneg=True)
487
+
488
+ # Expected returns
489
+ expected_return = (
490
+ self._cvx_expected_return(prior_model=prior_model, w=w)
491
+ - self._cvx_transaction_cost(prior_model=prior_model, w=w, factor=factor)
492
+ - self._cvx_management_fee(prior_model=prior_model, w=w)
493
+ )
494
+
495
+ # risk budgeting constraint
496
+ constraints = [
497
+ risk_budget @ cp.log(w) * self._scale_constraints
498
+ >= c * self._scale_constraints
499
+ ]
500
+
501
+ # weight constraints
502
+ constraints += self._get_weight_constraints(
503
+ n_assets=n_assets, w=w, factor=factor
504
+ )
505
+
506
+ parameters_values = []
507
+
508
+ # min_return constraint
509
+ if self.min_return is not None:
510
+ parameter = cp.Parameter(nonneg=False)
511
+ constraints += [
512
+ expected_return * self._scale_constraints
513
+ >= parameter * factor * self._scale_constraints
514
+ ]
515
+ parameters_values.append((parameter, self.min_return))
516
+
517
+ # risk and risk constraints
518
+ risk_func = getattr(self, f"_{self.risk_measure.value}_risk")
519
+ args = {}
520
+ for arg_name in args_names(risk_func):
521
+ if arg_name == "prior_model":
522
+ args[arg_name] = prior_model
523
+ elif arg_name == "w":
524
+ args[arg_name] = w
525
+ elif arg_name == "factor":
526
+ if self.risk_measure in [RiskMeasure.FIRST_LOWER_PARTIAL_MOMENT]:
527
+ args[arg_name] = factor
528
+ else:
529
+ args[arg_name] = cp.Constant(1)
530
+ else:
531
+ args[arg_name] = getattr(self, arg_name)
532
+ risk, constraints_i = risk_func(**args)
533
+ constraints += constraints_i
534
+
535
+ # custom objectives and constraints
536
+ custom_objective = self._get_custom_objective(w=w)
537
+ constraints += self._get_custom_constraints(w=w)
538
+
539
+ objective = cp.Minimize(
540
+ risk * self._scale_objective + custom_objective * self._scale_objective
541
+ )
542
+
543
+ # problem
544
+ # noinspection PyTypeChecker
545
+ problem = cp.Problem(objective, constraints)
546
+
547
+ # results
548
+ self._solve_problem(
549
+ problem=problem,
550
+ w=w,
551
+ factor=factor,
552
+ parameters_values=parameters_values,
553
+ expressions={
554
+ "expected_return": expected_return,
555
+ "risk": risk,
556
+ "factor": factor,
557
+ },
558
+ )
559
+
560
+ return self
@@ -0,0 +1,6 @@
1
+ from skfolio.optimization.ensemble._stacking import (
2
+ BaseComposition,
3
+ StackingOptimization,
4
+ )
5
+
6
+ __all__ = ["BaseComposition", "StackingOptimization"]
@@ -0,0 +1,87 @@
1
+ """Base Composition estimator.
2
+ Follow same implementation as Base composition from sklearn
3
+ """
4
+
5
+ # Author: Hugo Delatte <delatte.hugo@gmail.com>
6
+ # License: BSD 3 clause
7
+
8
+ from abc import ABC, abstractmethod
9
+ from contextlib import suppress
10
+
11
+ import sklearn.base as skb
12
+
13
+
14
+ class BaseComposition(skb.BaseEstimator, ABC):
15
+ """Handles parameter management for ensemble estimators."""
16
+
17
+ @abstractmethod
18
+ def __init__(self):
19
+ pass
20
+
21
+ def _get_params(self, attr, deep=True):
22
+ out = super().get_params(deep=deep)
23
+ if not deep:
24
+ return out
25
+
26
+ estimators = getattr(self, attr)
27
+ try:
28
+ out.update(estimators)
29
+ except (TypeError, ValueError):
30
+ # Ignore TypeError for cases where estimators is not a list of
31
+ # (name, estimator) and ignore ValueError when the list is not
32
+ # formatted correctly. This is to prevent errors when calling
33
+ # `set_params`. `BaseEstimator.set_params` calls `get_params` which
34
+ # can error for invalid values for `estimators`.
35
+ return out
36
+
37
+ for name, estimator in estimators:
38
+ if hasattr(estimator, "get_params"):
39
+ for key, value in estimator.get_params(deep=True).items():
40
+ out[f"{name}__{key}"] = value
41
+ return out
42
+
43
+ def _set_params(self, attr, **params):
44
+ # Ensure strict ordering of parameter setting:
45
+ # 1. All steps
46
+ if attr in params:
47
+ setattr(self, attr, params.pop(attr))
48
+ # 2. Replace items with estimators in params
49
+ items = getattr(self, attr)
50
+ if isinstance(items, list) and items:
51
+ # Get item names used to identify valid names in params
52
+ # `zip` raises a TypeError when `items` does not contains
53
+ # elements of length 2
54
+ with suppress(TypeError):
55
+ item_names, _ = zip(*items, strict=True)
56
+ for name in params:
57
+ if "__" not in name and name in item_names:
58
+ self._replace_estimator(attr, name, params.pop(name))
59
+
60
+ # 3. Step parameters and other initialisation arguments
61
+ super().set_params(**params)
62
+ return self
63
+
64
+ def _replace_estimator(self, attr, name, new_val):
65
+ # assumes `name` is a valid estimator name
66
+ new_estimators = list(getattr(self, attr))
67
+ for i, (estimator_name, _) in enumerate(new_estimators):
68
+ if estimator_name == name:
69
+ new_estimators[i] = (name, new_val)
70
+ break
71
+ setattr(self, attr, new_estimators)
72
+
73
+ def _validate_names(self, names):
74
+ if len(set(names)) != len(names):
75
+ raise ValueError(f"Names provided are not unique: {list(names)!r}")
76
+ invalid_names = set(names).intersection(self.get_params(deep=False))
77
+ if invalid_names:
78
+ raise ValueError(
79
+ "Estimator names conflict with constructor arguments: {!r}".format(
80
+ sorted(invalid_names)
81
+ )
82
+ )
83
+ invalid_names = [name for name in names if "__" in name]
84
+ if invalid_names:
85
+ raise ValueError(
86
+ f"Estimator names must not contain __: got {invalid_names!r}"
87
+ )