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,974 @@
1
+ """Mean Risk 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
+ import pandas as pd
10
+ import sklearn as sk
11
+
12
+ import skfolio.typing as skt
13
+ from skfolio.measures import RiskMeasure
14
+ from skfolio.optimization.convex._base import ConvexOptimization, ObjectiveFunction
15
+ from skfolio.prior import BasePrior, EmpiricalPrior
16
+ from skfolio.uncertainty_set import BaseCovarianceUncertaintySet, BaseMuUncertaintySet
17
+ from skfolio.utils.tools import args_names, check_estimator
18
+
19
+ # noinspection PyUnresolvedReferences
20
+ _NON_ANNUALIZED_RISK_MEASURES = [rm for rm in RiskMeasure if not rm.is_annualized]
21
+
22
+
23
+ class MeanRisk(ConvexOptimization):
24
+ r"""Mean-Risk Optimization estimator.
25
+
26
+ The below 4 objective functions can be optimized:
27
+
28
+ * Minimize Risk:
29
+
30
+ .. math:: \begin{cases}
31
+ \begin{aligned}
32
+ &\min_{w} & & risk_{i}(w) \\
33
+ &\text{s.t.} & & w^T \cdot \mu \ge min\_return \\
34
+ & & & A \cdot w \ge b \\
35
+ & & & risk_{j}(w) \le max\_risk_{j} \quad \forall \; j \ne i
36
+ \end{aligned}
37
+ \end{cases}
38
+
39
+ * Maximize Expected Return:
40
+
41
+ .. math:: \begin{cases}
42
+ \begin{aligned}
43
+ &\max_{w} & & w^T \cdot \mu \\
44
+ &\text{s.t.} & & risk_{i}(w) \le max\_risk_{i} \\
45
+ & & & A \cdot w \ge b \\
46
+ & & & risk_{j}(w) \le max\_risk_{j} \quad \forall \; j \ne i
47
+ \end{aligned}
48
+ \end{cases}
49
+
50
+ * Maximize Utility:
51
+
52
+ .. math:: \begin{cases}
53
+ \begin{aligned}
54
+ &\max_{w} & & w^T \cdot \mu - \lambda \times risk_{i}(w)\\
55
+ &\text{s.t.} & & risk_{i}(w) \le max\_risk_{i} \\
56
+ & & & w^T \cdot \mu \ge min\_return \\
57
+ & & & A \cdot w \ge b \\
58
+ & & & risk_{j}(w) \le max\_risk_{j} \quad \forall \; j \ne i
59
+ \end{aligned}
60
+ \end{cases}
61
+
62
+ * Maximize Ratio:
63
+
64
+ .. math:: \begin{cases}
65
+ \begin{aligned}
66
+ &\max_{w} & & \frac{w^T \cdot \mu - r_{f}}{risk_{i}(w)}\\
67
+ &\text{s.t.} & & risk_{i}(w) \le max\_risk_{i} \\
68
+ & & & w^T \cdot \mu \ge min\_return \\
69
+ & & & A \cdot w \ge b \\
70
+ & & & risk_{j}(w) \le max\_risk_{j} \quad \forall \; j \ne i
71
+ \end{aligned}
72
+ \end{cases}
73
+
74
+ With :math:`risk_{i}` a risk measure among:
75
+
76
+ * Mean Absolute Deviation
77
+ * First Lower Partial Moment
78
+ * Variance
79
+ * Semi-Variance
80
+ * CVaR (Conditional Value at Risk)
81
+ * EVaR (Entropic Value at Risk)
82
+ * Worst Realization (worst return)
83
+ * CDaR (Conditional Drawdown at Risk)
84
+ * Maximum Drawdown
85
+ * Average Drawdown
86
+ * EDaR (Entropic Drawdown at Risk)
87
+ * Ulcer Index
88
+ * Gini Mean Difference
89
+
90
+ Cost, regularization, uncertainty set, and additional constraints can also be added
91
+ to the optimization problem (see the parameters description).
92
+
93
+ The assets expected returns, covariance matrix and returns are estimated from the
94
+ :ref:`prior estimator <prior>`.
95
+
96
+ Parameters
97
+ ----------
98
+ objective_function : ObjectiveFunction, default=ObjectiveFunction.MINIMIZE_RISK
99
+ :class:`~skfolio.optimization.ObjectiveFunction` of the optimization.
100
+ Can be any of:
101
+
102
+ * MINIMIZE_RISK
103
+ * MAXIMIZE_RETURN
104
+ * MAXIMIZE_UTILITY
105
+ * MAXIMIZE_RATIO
106
+
107
+ The default is `ObjectiveFunction.MINIMIZE_RISK`.
108
+
109
+ risk_measure : RiskMeasure, default=RiskMeasure.VARIANCE
110
+ :class:`~skfolio.meta.RiskMeasure` of the optimization.
111
+ Can be any of:
112
+
113
+ * VARIANCE
114
+ * SEMI_VARIANCE
115
+ * STANDARD_DEVIATION
116
+ * SEMI_DEVIATION
117
+ * MEAN_ABSOLUTE_DEVIATION
118
+ * FIRST_LOWER_PARTIAL_MOMENT
119
+ * CVAR
120
+ * EVAR
121
+ * WORST_REALIZATION
122
+ * CDAR
123
+ * MAX_DRAWDOWN
124
+ * AVERAGE_DRAWDOWN
125
+ * EDAR
126
+ * ULCER_INDEX
127
+ * GINI_MEAN_DIFFERENCE_RATIO
128
+
129
+ The default is `RiskMeasure.VARIANCE`.
130
+
131
+ risk_aversion : float, default=1.0
132
+ Risk aversion factor :math:`\lambda` of the utility function. Only used for
133
+ `objective_function=ObjectiveFunction.MAXIMIZE_UTILITY`.
134
+ The default value is `1.0`.
135
+
136
+ prior_estimator : BasePrior, optional
137
+ :ref:`Prior estimator <prior>`.
138
+ The prior estimator is used to estimate the :class:`~skfolio.prior.PriorModel`
139
+ containing the estimation of assets expected returns, covariance matrix,
140
+ returns and Cholesky decomposition of the covariance.
141
+ The default (`None`) is to use :class:`~skfolio.prior.EmpiricalPrior`.
142
+
143
+ min_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=0.0
144
+ Minimum assets weights (weights lower bounds).
145
+ If a float is provided, it is applied to each asset.
146
+ `None` is equivalent to `-np.Inf` (no lower bound).
147
+ If a dictionary is provided, its (key/value) pair must be the
148
+ (asset name/asset minium weight) and the input `X` of the `fit` methods must
149
+ be a DataFrame with the assets names in columns.
150
+ When using a dictionary, assets values that are not provided are assigned
151
+ a minimum weight of `0.0`.
152
+ The default value is `0.0` (no short selling).
153
+
154
+ Example:
155
+
156
+ * `min_weights = 0` --> long only portfolio (no short selling).
157
+ * `min_weights = None` --> no lower bound (same as `-np.Inf`).
158
+ * `min_weights = -2` --> each weight must be above -200%.
159
+ * `min_weights = {"SX5E": 0, "SPX": -2}`
160
+ * `min_weights = [0, -2]`
161
+
162
+ max_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=1.0
163
+ Maximum assets weights (weights upper bounds).
164
+ If a float is provided, it is applied to each asset.
165
+ `None` is equivalent to `+np.Inf` (no upper bound).
166
+ If a dictionary is provided, its (key/value) pair must be the
167
+ (asset name/asset maximum weight) and the input `X` of the `fit` methods must
168
+ be a DataFrame with the assets names in columns.
169
+ When using a dictionary, assets values that are not provided are assigned
170
+ a minimum weight of `1.0`.
171
+ The default value is `1.0` (each asset is below 100%).
172
+
173
+ Example:
174
+
175
+ * `max_weights = 0` --> no long position (short only portfolio).
176
+ * `max_weights = None` --> no upper bound.
177
+ * `max_weights = 2` --> each weight must be below 200%.
178
+ * `max_weights = {"SX5E": 1, "SPX": 2}`
179
+ * `max_weights = [1, 2]`
180
+
181
+ budget : float | None, default=1.0
182
+ Investment budget. It is the sum of long positions and short positions (sum of
183
+ all weights). `None` means no budget constraints.
184
+ The default value is `1.0` (fully invested portfolio).
185
+
186
+ Examples:
187
+
188
+ * `budget = 1` --> fully invested portfolio.
189
+ * `budget = 0` --> market neutral portfolio.
190
+ * `budget = None` --> no constraints on the sum of weights.
191
+
192
+ min_budget : float, optional
193
+ Minimum budget. It is the lower bound of the sum of long and short positions
194
+ (sum of all weights). If provided, you must set `budget=None`.
195
+ The default (`None`) means no minimum budget constraint.
196
+
197
+ max_budget : float, optional
198
+ Maximum budget. It is the upper bound of the sum of long and short positions
199
+ (sum of all weights). If provided, you must set `budget=None`.
200
+ The default (`None`) means no maximum budget constraint.
201
+
202
+ max_short : float, optional
203
+ Maximum short position. The short position is defined as the sum of negative
204
+ weights (in absolute term).
205
+ The default (`None`) means no maximum short position.
206
+
207
+ max_long : float, optional
208
+ Maximum long position. The long position is defined as the sum of positive
209
+ weights.
210
+ The default (`None`) means no maximum long position.
211
+
212
+ transaction_costs : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
213
+ Transaction costs of the assets. It is used to add linear transaction costs to
214
+ the optimization problem:
215
+
216
+ .. math:: total\_cost = \sum_{i=1}^{N} c_{i} \times |w_{i} - w\_prev_{i}|
217
+
218
+ with :math:`c_{i}` the transaction cost of asset i, :math:`w_{i}` its weight
219
+ and :math:`w\_prev_{i}` its previous weight (defined in `previous_weights`).
220
+ The float :math:`total\_cost` is used in the portfolio expected return:
221
+
222
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_cost
223
+
224
+ with :math:`\mu` the vector af assets' expected returns and :math:`w` the
225
+ vector of assets weights.
226
+
227
+ If a float is provided, it is applied to each asset.
228
+ If a dictionary is provided, its (key/value) pair must be the
229
+ (asset name/asset cost) and the input `X` of the `fit` methods must be a
230
+ DataFrame with the assets names in columns.
231
+ The default value is `0.0`.
232
+
233
+ .. warning::
234
+
235
+ Based on the above formula, the periodicity of the transaction costs
236
+ needs to be homogenous to the periodicity of :math:`\mu`. For example, if
237
+ the input `X` is composed of **daily** returns, the `transaction_costs` need
238
+ to be expressed in **daily** costs.
239
+ (See :ref:`sphx_glr_auto_examples_1_mean_risk_plot_6_transaction_costs.py`)
240
+
241
+ management_fees : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
242
+ Management fees of the assets. It is used to add linear management fees to the
243
+ optimization problem:
244
+
245
+ .. math:: total\_fee = \sum_{i=1}^{N} f_{i} \times w_{i}
246
+
247
+ with :math:`f_{i}` the management fee of asset i and :math:`w_{i}` its weight.
248
+ The float :math:`total\_fee` is used in the portfolio expected return:
249
+
250
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_fee
251
+
252
+ with :math:`\mu` the vector af assets expected returns and :math:`w` the vector
253
+ of assets weights.
254
+
255
+ If a float is provided, it is applied to each asset.
256
+ If a dictionary is provided, its (key/value) pair must be the
257
+ (asset name/asset fee) and the input `X` of the `fit` methods must be a
258
+ DataFrame with the assets names in columns.
259
+ The default value is `0.0`.
260
+
261
+ .. warning::
262
+
263
+ Based on the above formula, the periodicity of the management fees needs to
264
+ be homogenous to the periodicity of :math:`\mu`. For example, if the input
265
+ `X` is composed of **daily** returns, the `management_fees` need to be
266
+ expressed in **daily** fees.
267
+
268
+ .. note::
269
+
270
+ Another approach is to directly impact the management fees to the input `X`
271
+ in order to express the returns net of fees. However, when estimating the
272
+ :math:`\mu` parameter using for example Shrinkage estimators, this approach
273
+ would mix a deterministic value with an uncertain one leading to unwanted
274
+ bias in the management fees.
275
+
276
+ previous_weights : float | dict[str, float] | array-like of shape (n_assets, ), optional
277
+ Previous weights of the assets. Previous weights are used to compute the
278
+ portfolio cost and the portfolio turnover.
279
+ If a float is provided, it is applied to each asset.
280
+ If a dictionary is provided, its (key/value) pair must be the
281
+ (asset name/asset previous weight) and the input `X` of the `fit` methods must
282
+ be a DataFrame with the assets names in columns.
283
+ The default (`None`) means no previous weights.
284
+
285
+ l1_coef : float, default=0.0
286
+ L1 regularization coefficient.
287
+ It is used to penalize the objective function by the L1 norm:
288
+
289
+ .. math:: l1\_coef \times \Vert w \Vert_{1} = l1\_coef \times \sum_{i=1}^{N} |w_{i}|
290
+
291
+ Increasing this coefficient will reduce the number of non-zero weights
292
+ (cardinality). It tends to increase robustness (out-of-sample stability) but
293
+ reduces diversification.
294
+ The default value is `0.0`.
295
+
296
+ l2_coef : float, default=0.0
297
+ L2 regularization coefficient.
298
+ It is used to penalize the objective function by the L2 norm:
299
+
300
+ .. math:: l2\_coef \times \Vert w \Vert_{2}^{2} = l2\_coef \times \sum_{i=1}^{N} w_{i}^2
301
+
302
+ It tends to increase robustness (out-of-sample stability).
303
+ The default value is `0.0`.
304
+
305
+ mu_uncertainty_set_estimator : BaseMuUncertaintySet, optional
306
+ :ref:`Mu Uncertainty set estimator <uncertainty_set_estimator>`.
307
+ If provided, the assets expected returns are modelled with an ellipsoidal
308
+ uncertainty set. It is called worst-case optimization and is a class of robust
309
+ optimization. It reduces the instability that arises from the estimation errors
310
+ of the expected returns.
311
+ The worst case portfolio expect return is:
312
+
313
+ .. math:: w^T \cdot \hat{\mu} - \kappa_{\mu} \lVert S_{\mu}^\frac{1}{2} \cdot w \rVert_{2}
314
+
315
+ with :math:`\kappa` the size of the ellipsoid (confidence region) and
316
+ :math:`S` its shape.
317
+ The default (`None`) means that no uncertainty set is used.
318
+
319
+ covariance_uncertainty_set_estimator : BaseCovarianceUncertaintySet, optional
320
+ :ref:`Covariance Uncertainty set estimator <uncertainty_set_estimator>`.
321
+ If provided, the assets covariance matrix is modelled with an ellipsoidal
322
+ uncertainty set. It is called worst-case optimization and is a class of robust
323
+ optimization. It reduces the instability that arises from the estimation errors
324
+ of the covariance matrix.
325
+ The default (`None`) means that no uncertainty set is used.
326
+
327
+ linear_constraints : array-like of shape (n_constraints,), optional
328
+ Linear constraints.
329
+ The linear constraints must match any of following patterns:
330
+
331
+ * "2.5 * ref1 + 0.10 * ref2 + 0.0013 <= 2.5 * ref3"
332
+ * "ref1 >= 2.9 * ref2"
333
+ * "ref1 <= ref2"
334
+ * "ref1 >= ref1"
335
+
336
+ With "ref1", "ref2" ... the assets names or the groups names provided
337
+ in the parameter `groups`. Assets names can be referenced without the need of
338
+ `groups` if the input `X` of the `fit` methods is a DataFrame with these
339
+ assets names in columns.
340
+
341
+ Examples:
342
+
343
+ * "SPX >= 0.10" --> SPX weight must be greater than 10% (note that you can also use `min_weights`)
344
+ * "SX5E + TLT >= 0.2" --> the sum of SX5E and TLT weights must be greater than 20%
345
+ * "US >= 0.7" --> the sum of all US weights must be greater than 70%
346
+ * "Equity <= 3 * Bond" --> the sum of all Equity weights must be less or equal to 3 times the sum of all Bond weights.
347
+ * "2*SPX + 3*Europe <= Bond + 0.05" --> mixing assets and group constraints
348
+
349
+ groups : dict[str, list[str]] or array-like of shape (n_groups, n_assets), optional
350
+ The assets groups referenced in `linear_constraints`.
351
+ If a dictionary is provided, its (key/value) pair must be the
352
+ (asset name/asset groups) and the input `X` of the `fit` methods must be a
353
+ DataFrame with the assets names in columns.
354
+
355
+ Examples:
356
+
357
+ * groups = {"SX5E": ["Equity", "Europe"], "SPX": ["Equity", "US"], "TLT": ["Bond", "US"]}
358
+ * groups = [["Equity", "Equity", "Bond"], ["Europe", "US", "US"]]
359
+
360
+ left_inequality : array-like of shape (n_constraints, n_assets), optional
361
+ Left inequality matrix :math:`A` of the linear
362
+ constraint :math:`A \cdot w \leq b`.
363
+
364
+ right_inequality : array-like of shape (n_constraints, ), optional
365
+ Right inequality vector :math:`b` of the linear
366
+ constraint :math:`A \cdot w \leq b`.
367
+
368
+ risk_free_rate : float, default=0.0
369
+ Risk-free interest rate.
370
+ The default value is `0.0`.
371
+
372
+ max_tracking_error : float, optional
373
+ Upper bound constraint on the tracking error.
374
+ The tracking error is defined as the RMSE (root-mean-square error) of the
375
+ portfolio returns compared to a target returns. If `max_tracking_error` is
376
+ provided, the target returns `y` must be provided in the `fit` method.
377
+
378
+ max_turnover : float, optional
379
+ Upper bound constraint of the turnover.
380
+ The turnover is defined as the absolute difference between the portfolio weights
381
+ and the `previous_weights`. Note that another way to control for turnover is by
382
+ using the `transaction_costs` parameter.
383
+
384
+ max_mean_absolute_deviation : float | array-like of shape (n_optimization), optional
385
+ Upper bound constraint on the Mean Absolute Deviation.
386
+
387
+ max_first_lower_partial_moment : float | array-like of shape (n_optimization), optional
388
+ Upper bound constraint on the First Lower Partial Moment.
389
+
390
+ max_variance : float | array-like of shape (n_optimization), optional
391
+ Upper bound constraint on the Variance.
392
+
393
+ max_standard_deviation : float | array-like of shape (n_optimization), optional
394
+ Upper bound constraint on the Standard deviation.
395
+
396
+ max_semi_variance : float | array-like of shape (n_optimization), optional
397
+ Upper bound constraint on the Semi-Variance (Second Lower Partial Moment or
398
+ Downside Variance).
399
+
400
+ max_semi_deviation : float | array-like of shape (n_optimization), optional
401
+ Upper bound constraint on the Semi-Standard deviation.
402
+
403
+ max_worst_realization : float | array-like of shape (n_optimization), optional
404
+ Upper bound constraint on the Worst Realization (Worst Return).
405
+
406
+ max_cvar : float | array-like of shape (n_optimization), optional
407
+ Upper bound constraint on the CVaR (Conditional Value-at-Risk or Expected
408
+ Shortfall).
409
+
410
+ max_evar : float | array-like of shape (n_optimization), optional
411
+ Upper bound constraint on the EVaR (Entropic Value at Risk).
412
+
413
+ max_max_drawdown : float | array-like of shape (n_optimization), optional
414
+ Upper bound constraint on the Maximum Drawdown.
415
+
416
+ max_average_drawdown : float | array-like of shape (n_optimization), optional
417
+ Upper bound constraint on the Average Drawdown.
418
+
419
+ max_cdar : float | array-like of shape (n_optimization), optional
420
+ Upper bound constraint on the CDaR (Conditional Drawdown at Risk).
421
+
422
+ max_edar : float | array-like of shape (n_optimization), optional
423
+ Upper bound constraint on the EDaR (Entropic Drawdown at Risk).
424
+
425
+ max_ulcer_index : float | array-like of shape (n_optimization), optional
426
+ Upper bound constraint on the Ulcer Index.
427
+
428
+ max_gini_mean_difference : float | array-like of shape (n_optimization), optional
429
+ Upper bound constraint on the Gini Mean Difference.
430
+
431
+ min_return : float | array-like of shape (n_optimization), optional
432
+ Lower bound constraint on the expected return.
433
+
434
+ min_acceptable_return : float, optional
435
+ The minimum acceptable return used to distinguish "downside" and "upside"
436
+ returns for the computation of lower partial moments:
437
+
438
+ * First Lower Partial Moment
439
+ * Semi-Variance
440
+ * Semi-Deviation
441
+
442
+ The default (`None`) is to use the mean.
443
+
444
+ cvar_beta : float, default=0.95
445
+ CVaR (Conditional Value at Risk) confidence level.
446
+ The default value is `0.95`.
447
+
448
+ evar_beta : float, default=0
449
+ EVaR (Entropic Value at Risk) confidence level.
450
+ The default value is `0.95`.
451
+
452
+ cdar_beta : float, default=0.95
453
+ CDaR (Conditional Drawdown at Risk) confidence level.
454
+ The default value is `0.95`.
455
+
456
+ edar_beta : float, default=0.95
457
+ EDaR (Entropic Drawdown at Risk) confidence level.
458
+ The default value is `0.95`.
459
+
460
+ add_objective : Callable[[cp.Variable], cp.Expression], optional
461
+ Add a custom objective to the existing objective expression.
462
+ It is a function that must take as argument the weights `w` and returns a
463
+ CVXPY expression.
464
+
465
+ add_constraints : Callable[[cp.Variable], cp.Expression|list[cp.Expression]], optional
466
+ Add a custom constraint or a list of constraints to the existing constraints.
467
+ It is a function that must take as argument the weights `w` and returns a
468
+ CVPXY expression or a list of CVPXY expressions.
469
+
470
+ overwrite_expected_return : Callable[[cp.Variable], cp.Expression], optional
471
+ Overwrite the expected return :math:`\mu \cdot w` with a custom expression.
472
+ It is a function that must take as argument the weights `w` and returns a
473
+ CVPXY expression.
474
+
475
+ solver : str, optional
476
+ The solver to use. For example, "ECOS", "SCS", or "OSQP".
477
+ The default (`None`) is set depending on the problem.
478
+ For more details about available solvers, check the CVXPY documentation:
479
+ https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver
480
+
481
+ solver_params : dict, optional
482
+ Solver parameters. For example, `solver_params=dict(verbose=True)`.
483
+ For more details about solver arguments, check the CVXPY documentation:
484
+ https://www.cvxpy.org/tutorial/advanced/index.html#setting-solver-options
485
+
486
+ scale_objective : float, optional
487
+ Scale each objective element by this value.
488
+ It can be used to increase the optimization accuracies in specific cases.
489
+ The default (`None`) is set depending on the problem.
490
+
491
+ scale_constraints : float, optional
492
+ Scale each constraint element by this value.
493
+ It can be used to increase the optimization accuracies in specific cases.
494
+ The default (`None`) is set depending on the problem.
495
+
496
+ raise_on_failure : bool, default=True
497
+ If this is set to True, an error is raised when the optimization fail otherwise
498
+ it passes with a warning.
499
+
500
+ portfolio_params : dict, optional
501
+ Portfolio parameters passed to the portfolio evaluated by the `predict` and
502
+ `score` methods. If not provided, the `name`, `transaction_costs`,
503
+ `management_fees` and `previous_weights` are copied from the optimization
504
+ model and systematically passed to the portfolio.
505
+
506
+ Attributes
507
+ ----------
508
+ weights_ : ndarray of shape (n_assets,) or (n_optimizations, n_assets)
509
+ Weights of the assets.
510
+
511
+ problem_: cvxpy.Problem
512
+ CVXPY problem used for the optimization.
513
+
514
+ problem_values_ : dict[str, float] | list[dict[str, float]] of size n_optimizations
515
+ Expression values retrieved from the CVXPY problem.
516
+
517
+ prior_estimator_ : BasePrior
518
+ Fitted `prior_estimator`.
519
+
520
+ mu_uncertainty_set_estimator_ : BaseMuUncertaintySet
521
+ Fitted `mu_uncertainty_set_estimator` if provided.
522
+
523
+ covariance_uncertainty_set_estimator_ : BaseCovarianceUncertaintySet
524
+ Fitted `covariance_uncertainty_set_estimator` if provided.
525
+
526
+ n_features_in_ : int
527
+ Number of assets seen during `fit`.
528
+
529
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
530
+ Names of assets seen during `fit`. Defined only when `X`
531
+ has assets names that are all strings.
532
+ """
533
+
534
+ def __init__(
535
+ self,
536
+ objective_function: ObjectiveFunction = ObjectiveFunction.MINIMIZE_RISK,
537
+ risk_measure: RiskMeasure = RiskMeasure.VARIANCE,
538
+ risk_aversion: float = 1.0,
539
+ efficient_frontier_size: int | None = None,
540
+ prior_estimator: BasePrior | None = None,
541
+ min_weights: skt.MultiInput | None = 0.0,
542
+ max_weights: skt.MultiInput | None = 1.0,
543
+ budget: float | None = 1.0,
544
+ min_budget: float | None = None,
545
+ max_budget: float | None = None,
546
+ max_short: float | None = None,
547
+ max_long: float | None = None,
548
+ transaction_costs: skt.MultiInput = 0.0,
549
+ management_fees: skt.MultiInput = 0.0,
550
+ previous_weights: skt.MultiInput | None = None,
551
+ groups: skt.Groups | None = None,
552
+ linear_constraints: skt.LinearConstraints | None = None,
553
+ left_inequality: skt.Inequality | None = None,
554
+ right_inequality: skt.Inequality | None = None,
555
+ l1_coef: float = 0.0,
556
+ l2_coef: float = 0.0,
557
+ mu_uncertainty_set_estimator: BaseMuUncertaintySet | None = None,
558
+ covariance_uncertainty_set_estimator: BaseCovarianceUncertaintySet
559
+ | None = None,
560
+ risk_free_rate: float = 0.0,
561
+ min_return: skt.Target | None = None,
562
+ max_tracking_error: skt.Target | None = None,
563
+ max_turnover: skt.Target | None = None,
564
+ max_mean_absolute_deviation: skt.Target | None = None,
565
+ max_first_lower_partial_moment: skt.Target | None = None,
566
+ max_variance: skt.Target | None = None,
567
+ max_standard_deviation: skt.Target | None = None,
568
+ max_semi_variance: skt.Target | None = None,
569
+ max_semi_deviation: skt.Target | None = None,
570
+ max_worst_realization: skt.Target | None = None,
571
+ max_cvar: skt.Target | None = None,
572
+ max_evar: skt.Target | None = None,
573
+ max_max_drawdown: skt.Target | None = None,
574
+ max_average_drawdown: skt.Target | None = None,
575
+ max_cdar: skt.Target | None = None,
576
+ max_edar: skt.Target | None = None,
577
+ max_ulcer_index: skt.Target | None = None,
578
+ max_gini_mean_difference: skt.Target | None = None,
579
+ min_acceptable_return: skt.Target | None = None,
580
+ cvar_beta: float = 0.95,
581
+ evar_beta: float = 0.95,
582
+ cdar_beta: float = 0.95,
583
+ edar_beta: float = 0.95,
584
+ solver: str | None = None,
585
+ solver_params: dict | None = None,
586
+ scale_objective: float | None = None,
587
+ scale_constraints: float | None = None,
588
+ raise_on_failure: bool = True,
589
+ add_objective: skt.ExpressionFunction | None = None,
590
+ add_constraints: skt.ExpressionFunction | None = None,
591
+ overwrite_expected_return: skt.ExpressionFunction | None = None,
592
+ portfolio_params: dict | None = None,
593
+ ):
594
+ super().__init__(
595
+ risk_measure=risk_measure,
596
+ prior_estimator=prior_estimator,
597
+ mu_uncertainty_set_estimator=mu_uncertainty_set_estimator,
598
+ covariance_uncertainty_set_estimator=covariance_uncertainty_set_estimator,
599
+ min_weights=min_weights,
600
+ max_weights=max_weights,
601
+ budget=budget,
602
+ min_budget=min_budget,
603
+ max_budget=max_budget,
604
+ max_short=max_short,
605
+ max_long=max_long,
606
+ transaction_costs=transaction_costs,
607
+ management_fees=management_fees,
608
+ previous_weights=previous_weights,
609
+ groups=groups,
610
+ linear_constraints=linear_constraints,
611
+ left_inequality=left_inequality,
612
+ right_inequality=right_inequality,
613
+ l1_coef=l1_coef,
614
+ l2_coef=l2_coef,
615
+ risk_free_rate=risk_free_rate,
616
+ min_acceptable_return=min_acceptable_return,
617
+ cvar_beta=cvar_beta,
618
+ evar_beta=evar_beta,
619
+ cdar_beta=cdar_beta,
620
+ edar_beta=edar_beta,
621
+ solver=solver,
622
+ solver_params=solver_params,
623
+ scale_objective=scale_objective,
624
+ scale_constraints=scale_constraints,
625
+ raise_on_failure=raise_on_failure,
626
+ add_objective=add_objective,
627
+ add_constraints=add_constraints,
628
+ overwrite_expected_return=overwrite_expected_return,
629
+ portfolio_params=portfolio_params,
630
+ )
631
+ self.objective_function = objective_function
632
+ self.risk_aversion = risk_aversion
633
+ self.efficient_frontier_size = efficient_frontier_size
634
+ self.min_return = min_return
635
+ self.max_tracking_error = max_tracking_error
636
+ self.max_turnover = max_turnover
637
+ self.max_mean_absolute_deviation = max_mean_absolute_deviation
638
+ self.max_first_lower_partial_moment = max_first_lower_partial_moment
639
+ self.max_variance = max_variance
640
+ self.max_standard_deviation = max_standard_deviation
641
+ self.max_semi_variance = max_semi_variance
642
+ self.max_semi_deviation = max_semi_deviation
643
+ self.max_worst_realization = max_worst_realization
644
+ self.max_cvar = max_cvar
645
+ self.max_evar = max_evar
646
+ self.max_max_drawdown = max_max_drawdown
647
+ self.max_average_drawdown = max_average_drawdown
648
+ self.max_cdar = max_cdar
649
+ self.max_edar = max_edar
650
+ self.max_ulcer_index = max_ulcer_index
651
+ self.max_gini_mean_difference = max_gini_mean_difference
652
+
653
+ def _validation(self) -> None:
654
+ """Validate the input parameters"""
655
+ if not isinstance(self.risk_measure, RiskMeasure):
656
+ raise TypeError("risk_measure must be of type `RiskMeasure`")
657
+ if not isinstance(self.objective_function, ObjectiveFunction):
658
+ raise TypeError("objective_function must be of type `ObjectiveFunction`")
659
+ if self.efficient_frontier_size is not None:
660
+ if self.efficient_frontier_size <= 1:
661
+ raise ValueError(
662
+ "`efficient_frontier_size` must be strictly greater than one"
663
+ )
664
+ if self.objective_function != ObjectiveFunction.MINIMIZE_RISK:
665
+ raise ValueError(
666
+ "`efficient_frontier_size` must be used only with "
667
+ "`objective_function = ObjectiveFunction.MINIMIZE_RISK`"
668
+ )
669
+
670
+ def fit(self, X: npt.ArrayLike, y: npt.ArrayLike | None = None) -> "MeanRisk":
671
+ """Fit the Mean-Risk Optimization estimator.
672
+
673
+ Parameters
674
+ ----------
675
+ X : array-like of shape (n_observations, n_assets)
676
+ Price returns of the assets.
677
+
678
+ y : array-like of shape (n_observations, n_targets), optional
679
+ Price returns of factors or a target benchmark.
680
+ The default is `None`.
681
+
682
+ Returns
683
+ -------
684
+ self : MeanRisk
685
+ Fitted estimator.
686
+ """
687
+ self._check_feature_names(X, reset=True)
688
+ # Validate
689
+ self._validation()
690
+ # Used to avoid adding multiple times similar constrains linked to identical
691
+ # risk models
692
+ self._clear_models_cache()
693
+ self.prior_estimator_ = check_estimator(
694
+ self.prior_estimator,
695
+ default=EmpiricalPrior(),
696
+ check_type=BasePrior,
697
+ )
698
+ self.prior_estimator_.fit(X, y)
699
+ prior_model = self.prior_estimator_.prior_model_
700
+ n_observations, n_assets = prior_model.returns.shape
701
+
702
+ # set solvers
703
+ self._set_solver(default="ECOS")
704
+
705
+ # set scales
706
+ if self.objective_function == ObjectiveFunction.MAXIMIZE_RATIO:
707
+ self._set_scale_objective(default=1)
708
+ self._set_scale_constraints(default=1)
709
+ else:
710
+ match self.risk_measure:
711
+ case (
712
+ RiskMeasure.MEAN_ABSOLUTE_DEVIATION
713
+ | RiskMeasure.FIRST_LOWER_PARTIAL_MOMENT
714
+ | RiskMeasure.CVAR
715
+ | RiskMeasure.WORST_REALIZATION
716
+ | RiskMeasure.AVERAGE_DRAWDOWN
717
+ | RiskMeasure.MAX_DRAWDOWN
718
+ | RiskMeasure.CDAR
719
+ | RiskMeasure.ULCER_INDEX
720
+ ):
721
+ self._set_scale_objective(default=1e-1)
722
+ self._set_scale_constraints(default=1e2)
723
+
724
+ case RiskMeasure.EVAR:
725
+ self._set_scale_objective(default=1)
726
+ self._set_scale_constraints(default=1e-2)
727
+
728
+ case RiskMeasure.EDAR:
729
+ self._set_scale_objective(default=1)
730
+ self._set_scale_constraints(default=1e2)
731
+
732
+ case _:
733
+ self._set_scale_objective(default=1)
734
+ self._set_scale_constraints(default=1)
735
+
736
+ # Init weight variable and constraints
737
+ w = cp.Variable(n_assets)
738
+ constraints = []
739
+
740
+ if self.objective_function == ObjectiveFunction.MAXIMIZE_RATIO:
741
+ factor = cp.Variable()
742
+ else:
743
+ factor = cp.Constant(1)
744
+
745
+ # Mu uncertainty set
746
+ if self.mu_uncertainty_set_estimator is None:
747
+ mu_uncertainty_set = cp.Constant(0)
748
+ else:
749
+ # noinspection PyTypeChecker
750
+ self.mu_uncertainty_set_estimator_ = sk.clone(
751
+ self.mu_uncertainty_set_estimator
752
+ )
753
+ self.mu_uncertainty_set_estimator_.fit(X, y)
754
+ mu_uncertainty_set = self._cvx_mu_uncertainty_set(
755
+ mu_uncertainty_set=self.mu_uncertainty_set_estimator_.uncertainty_set_,
756
+ w=w,
757
+ )
758
+
759
+ # Expected returns
760
+ expected_return = (
761
+ self._cvx_expected_return(prior_model=prior_model, w=w)
762
+ - self._cvx_transaction_cost(prior_model=prior_model, w=w, factor=factor)
763
+ - self._cvx_management_fee(prior_model=prior_model, w=w)
764
+ - mu_uncertainty_set
765
+ )
766
+
767
+ # Regularization
768
+ regularization = self._cvx_regularization(w=w)
769
+
770
+ # Tracking error
771
+ if self.max_tracking_error is not None:
772
+ if y is None:
773
+ raise ValueError(
774
+ "If `max_tracking_error` is provided, `y` must also be provided"
775
+ )
776
+ if isinstance(y, pd.DataFrame):
777
+ if y.shape[1] > 1:
778
+ raise ValueError(
779
+ "If `max_tracking_error` is provided, `y` must be a"
780
+ " 1d-array, a single-column DataFrame or a Series"
781
+ )
782
+ y = y[y.columns[0]]
783
+ _, y = self._validate_data(X, y)
784
+ tracking_error = self._tracking_error(
785
+ prior_model=prior_model, w=w, y=y, factor=factor
786
+ )
787
+ constraints += [
788
+ tracking_error * self._scale_constraints
789
+ <= self.max_tracking_error * self._scale_constraints
790
+ ]
791
+
792
+ # Turnover
793
+ if self.max_turnover is not None:
794
+ turnover = self._turnover(n_assets=n_assets, w=w, factor=factor)
795
+ constraints += [
796
+ turnover * self._scale_constraints
797
+ <= self.max_turnover * factor * self._scale_constraints
798
+ ]
799
+
800
+ # weight constraints
801
+ constraints += self._get_weight_constraints(
802
+ n_assets=n_assets, w=w, factor=factor
803
+ )
804
+
805
+ parameters_values = []
806
+
807
+ # Efficient frontier
808
+ if self.efficient_frontier_size is not None:
809
+ # We find the lower and upper bounds of the expected returns.
810
+ # noinspection PyTypeChecker
811
+ model: MeanRisk = sk.clone(self)
812
+ # noinspection PyTypeChecker
813
+ model.set_params(
814
+ objective_function=ObjectiveFunction.MINIMIZE_RISK,
815
+ efficient_frontier_size=None,
816
+ portfolio_params=dict(annualized_factor=1),
817
+ )
818
+ model.fit(X)
819
+ min_return = model.problem_values_["expected_return"]
820
+ # noinspection PyTypeChecker
821
+ model.set_params(objective_function=ObjectiveFunction.MAXIMIZE_RETURN)
822
+ model.fit(X)
823
+ max_return = model.problem_values_["expected_return"]
824
+ if max_return <= 0:
825
+ raise ValueError(
826
+ "Unable to compute the Efficient Frontier with only negative"
827
+ " expected returns"
828
+ )
829
+ targets = np.linspace(
830
+ max(min_return, 1e-10) * 1.01,
831
+ max_return,
832
+ num=self.efficient_frontier_size,
833
+ )
834
+ parameter = cp.Parameter(nonneg=False)
835
+ constraints += [expected_return >= parameter * factor]
836
+ parameters_values.append((parameter, targets))
837
+
838
+ # min_return constraint
839
+ if self.min_return is not None:
840
+ parameter = cp.Parameter(nonneg=False)
841
+ constraints += [
842
+ expected_return * self._scale_constraints
843
+ >= parameter * factor * self._scale_constraints
844
+ ]
845
+ parameters_values.append((parameter, self.min_return))
846
+
847
+ # risk and risk constraints
848
+ risk = None
849
+ for r_m in _NON_ANNUALIZED_RISK_MEASURES:
850
+ risk_limit = getattr(self, f"max_{r_m.value}")
851
+
852
+ if self.risk_measure == r_m or risk_limit is not None:
853
+ # Add covariance uncertainty set if provided
854
+ if (
855
+ r_m == RiskMeasure.VARIANCE
856
+ and self.covariance_uncertainty_set_estimator is not None
857
+ ):
858
+ risk_func = self._worst_case_variance_risk
859
+ else:
860
+ risk_func = getattr(self, f"_{r_m.value}_risk")
861
+
862
+ args = {}
863
+ for arg_name in args_names(risk_func):
864
+ if arg_name == "prior_model":
865
+ args[arg_name] = prior_model
866
+ elif arg_name == "w":
867
+ args[arg_name] = w
868
+ elif arg_name == "factor":
869
+ args[arg_name] = factor
870
+ elif arg_name == "covariance_uncertainty_set":
871
+ self._set_solver(default="CVXOPT")
872
+ # noinspection PyTypeChecker
873
+ self.covariance_uncertainty_set_estimator_ = sk.clone(
874
+ self.covariance_uncertainty_set_estimator
875
+ )
876
+ self.covariance_uncertainty_set_estimator_.fit(X, y)
877
+ args[arg_name] = (
878
+ self.covariance_uncertainty_set_estimator_.uncertainty_set_
879
+ )
880
+ else:
881
+ args[arg_name] = getattr(self, arg_name)
882
+
883
+ risk_i, constraints_i = risk_func(**args)
884
+ constraints += constraints_i
885
+ if risk_limit is not None:
886
+ parameter = cp.Parameter(nonneg=True)
887
+ constraints += [
888
+ risk_i * self._scale_constraints
889
+ <= parameter * factor * self._scale_constraints
890
+ ]
891
+ parameters_values.append((parameter, risk_limit))
892
+ if self.risk_measure == r_m:
893
+ risk = risk_i
894
+
895
+ # custom objectives and constraints
896
+ custom_objective = self._get_custom_objective(w=w)
897
+ constraints += self._get_custom_constraints(w=w)
898
+
899
+ match self.objective_function:
900
+ case ObjectiveFunction.MAXIMIZE_RETURN:
901
+ objective = cp.Maximize(
902
+ expected_return * self._scale_objective
903
+ - regularization * self._scale_objective
904
+ + custom_objective * self._scale_objective
905
+ )
906
+ case ObjectiveFunction.MINIMIZE_RISK:
907
+ objective = cp.Minimize(
908
+ risk * self._scale_objective
909
+ + regularization * self._scale_objective
910
+ + custom_objective * self._scale_objective
911
+ )
912
+ case ObjectiveFunction.MAXIMIZE_UTILITY:
913
+ objective = cp.Maximize(
914
+ expected_return * self._scale_objective
915
+ - self.risk_aversion * risk * self._scale_objective
916
+ - regularization * self._scale_objective
917
+ + custom_objective * self._scale_objective
918
+ )
919
+ case ObjectiveFunction.MAXIMIZE_RATIO:
920
+ if expected_return.is_affine():
921
+ # Charnes-Cooper's variable transformation for Fractional
922
+ # Programing problem :Max(f1/f2) with f2 linear
923
+ constraints += [
924
+ expected_return * self._scale_constraints
925
+ - cp.Constant(self.risk_free_rate)
926
+ * factor
927
+ * self._scale_constraints
928
+ == cp.Constant(1) * self._scale_constraints
929
+ ]
930
+ else:
931
+ # Schaible's generalization of Charnes-Cooper's variable
932
+ # transformation for Fractional Programing problem :Max(f1/f2)
933
+ # with f1 concave instead of linear: Schaible,"Parameter-free
934
+ # Convex Equivalent and Dual Programs of Fractional Programming
935
+ # Problems".
936
+ # The condition to work is f1 >= 0, so we need to raise an user
937
+ # warning when it's not the case.
938
+ # TODO: raise user warning when f1<0
939
+ constraints += [
940
+ expected_return * self._scale_constraints
941
+ - cp.Constant(self.risk_free_rate)
942
+ * factor
943
+ * self._scale_constraints
944
+ >= cp.Constant(1) * self._scale_constraints
945
+ ]
946
+ objective = cp.Minimize(
947
+ risk * self._scale_objective
948
+ + regularization * self._scale_objective
949
+ + custom_objective * self._scale_objective
950
+ )
951
+ case _:
952
+ raise ValueError(
953
+ f"objective_function {self.objective_function} is not valid"
954
+ )
955
+
956
+ # problem
957
+ problem = cp.Problem(objective, constraints)
958
+
959
+ # results
960
+ self._solve_problem(
961
+ problem=problem,
962
+ w=w,
963
+ factor=factor,
964
+ parameters_values=parameters_values,
965
+ expressions={
966
+ "expected_return": expected_return,
967
+ "risk": risk,
968
+ "mu_uncertainty_set": mu_uncertainty_set,
969
+ "regularization": regularization,
970
+ "factor": factor,
971
+ },
972
+ )
973
+
974
+ return self