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,392 @@
1
+ """Distributionally Robust CVaR 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 check_estimator
15
+
16
+
17
+ class DistributionallyRobustCVaR(ConvexOptimization):
18
+ r"""Distributionally Robust CVaR.
19
+
20
+ The Distributionally Robust CVaR model constructs a Wasserstein ball in the space of
21
+ multivariate and non-discrete probability distributions centered at the uniform
22
+ distribution on the training samples, and find the allocation that minimize the CVaR
23
+ of the worst-case distribution within this Wasserstein ball.
24
+ Esfahani and Kuhn [1]_ proved that for piecewise linear objective functions,
25
+ which is the case of CVaR [2]_, the distributionally robust optimization problem
26
+ over a Wasserstein ball can be reformulated as finite convex programs.
27
+
28
+ Only piecewise linear function are supported which means that transaction costs and
29
+ regularization are not permitted.
30
+
31
+ A solver like `Mosek` that can handles a high number of constraints is preferred.
32
+
33
+ Parameters
34
+ ----------
35
+ cvar_beta : float, default=0.95
36
+ CVaR (Conditional Value at Risk) confidence level.
37
+
38
+ risk_aversion : float, default=1.0
39
+ Risk aversion factor of the utility function: return - risk_aversion * cvar.
40
+
41
+ wasserstein_ball_radius: float, default=0.02
42
+ Radius of the Wasserstein ball.
43
+
44
+ prior_estimator : BasePrior, optional
45
+ :ref:`Prior estimator <prior>`.
46
+ The prior estimator is used to estimate the :class:`~skfolio.prior.PriorModel`
47
+ containing the estimation of assets expected returns, covariance matrix,
48
+ returns and Cholesky decomposition of the covariance.
49
+ The default (`None`) is to use :class:`~skfolio.prior.EmpiricalPrior`.
50
+
51
+ min_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=0.0
52
+ Minimum assets weights (weights lower bounds).
53
+ If a float is provided, it is applied to each asset.
54
+ `None` is equivalent to `-np.Inf` (no lower bound).
55
+ If a dictionary is provided, its (key/value) pair must be the
56
+ (asset name/asset minium weight) and the input `X` of the `fit` methods must
57
+ be a DataFrame with the assets names in columns.
58
+ When using a dictionary, assets values that are not provided are assigned
59
+ a minimum weight of `0.0`.
60
+ The default value is `0.0` (no short selling).
61
+
62
+ Example:
63
+
64
+ * `min_weights = 0` --> long only portfolio (no short selling).
65
+ * `min_weights = None` --> no lower bound (same as `-np.Inf`).
66
+ * `min_weights = -2` --> each weight must be above -200%.
67
+ * `min_weights = {"SX5E": 0, "SPX": -2}`
68
+ * `min_weights = [0, -2]`
69
+
70
+ max_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=1.0
71
+ Maximum assets weights (weights upper bounds).
72
+ If a float is provided, it is applied to each asset.
73
+ `None` is equivalent to `+np.Inf` (no upper bound).
74
+ If a dictionary is provided, its (key/value) pair must be the
75
+ (asset name/asset maximum weight) and the input `X` of the `fit` methods must
76
+ be a DataFrame with the assets names in columns.
77
+ When using a dictionary, assets values that are not provided are assigned
78
+ a minimum weight of `1.0`.
79
+ The default value is `1.0` (each asset is below 100%).
80
+
81
+ Example:
82
+
83
+ * `max_weights = 0` --> no long position (short only portfolio).
84
+ * `max_weights = None` --> no upper bound.
85
+ * `max_weights = 2` --> each weight must be below 200%.
86
+ * `max_weights = {"SX5E": 1, "SPX": 2}`
87
+ * `max_weights = [1, 2]`
88
+
89
+ budget : float | None, default=1.0
90
+ Investment budget. It is the sum of long positions and short positions (sum of
91
+ all weights). `None` means no budget constraints.
92
+ The default value is `1.0` (fully invested portfolio).
93
+
94
+ Examples:
95
+
96
+ * `budget = 1` --> fully invested portfolio.
97
+ * `budget = 0` --> market neutral portfolio.
98
+ * `budget = None` --> no constraints on the sum of weights.
99
+
100
+ min_budget : float, optional
101
+ Minimum budget. It is the lower bound of the sum of long and short positions
102
+ (sum of all weights). If provided, you must set `budget=None`.
103
+ The default (`None`) means no minimum budget constraint.
104
+
105
+ max_short : float, optional
106
+ Maximum short position. The short position is defined as the sum of negative
107
+ weights (in absolute term).
108
+ The default (`None`) means no maximum short position.
109
+
110
+ max_long : float, optional
111
+ Maximum long position. The long position is defined as the sum of positive
112
+ weights.
113
+ The default (`None`) means no maximum long position.
114
+
115
+ max_budget : float, optional
116
+ Maximum budget. It is the upper bound of the sum of long and short positions
117
+ (sum of all weights). If provided, you must set `budget=None`.
118
+ The default (`None`) means no maximum budget constraint.
119
+
120
+ linear_constraints : array-like of shape (n_constraints,), optional
121
+ Linear constraints.
122
+ The linear constraints must match any of following patterns:
123
+
124
+ * "2.5 * ref1 + 0.10 * ref2 + 0.0013 <= 2.5 * ref3"
125
+ * "ref1 >= 2.9 * ref2"
126
+ * "ref1 <= ref2"
127
+ * "ref1 >= ref1"
128
+
129
+ With "ref1", "ref2" ... the assets names or the groups names provided
130
+ in the parameter `groups`. Assets names can be referenced without the need of
131
+ `groups` if the input `X` of the `fit` methods is a DataFrame with these
132
+ assets names in columns.
133
+
134
+ Examples:
135
+
136
+ * "SPX >= 0.10" --> SPX weight must be greater than 10% (note that you can also use `min_weights`)
137
+ * "SX5E + TLT >= 0.2" --> the sum of SX5E and TLT weights must be greater than 20%
138
+ * "US >= 0.7" --> the sum of all US weights must be greater than 70%
139
+ * "Equity <= 3 * Bond" --> the sum of all Equity weights must be less or equal to 3 times the sum of all Bond weights.
140
+ * "2*SPX + 3*Europe <= Bond + 0.05" --> mixing assets and group constraints
141
+
142
+ groups : dict[str, list[str]] or array-like of shape (n_groups, n_assets), optional
143
+ The assets groups referenced in `linear_constraints`.
144
+ If a dictionary is provided, its (key/value) pair must be the
145
+ (asset name/asset groups) and the input `X` of the `fit` methods must be a
146
+ DataFrame with the assets names in columns.
147
+
148
+ Examples:
149
+
150
+ * groups = {"SX5E": ["Equity", "Europe"], "SPX": ["Equity", "US"], "TLT": ["Bond", "US"]}
151
+ * groups = [["Equity", "Equity", "Bond"], ["Europe", "US", "US"]]
152
+
153
+ left_inequality : array-like of shape (n_constraints, n_assets), optional
154
+ Left inequality matrix :math:`A` of the linear
155
+ constraint :math:`A \cdot w \leq b`.
156
+
157
+ right_inequality : array-like of shape (n_constraints, ), optional
158
+ Right inequality vector :math:`b` of the linear
159
+ constraint :math:`A \cdot w \leq b`.
160
+
161
+ risk_free_rate : float, default=0.0
162
+ Risk-free interest rate.
163
+ The default value is `0.0`.
164
+
165
+ add_constraints : Callable[[cp.Variable], cp.Expression|list[cp.Expression]], optional
166
+ Add a custom constraint or a list of constraints to the existing constraints.
167
+ It is a function that must take as argument the weights `w` and returns a
168
+ CVPXY expression or a list of CVPXY expressions.
169
+
170
+ overwrite_expected_return : Callable[[cp.Variable], cp.Expression], optional
171
+ Overwrite the expected return :math:`\mu \cdot w` with a custom expression.
172
+ It is a function that must take as argument the weights `w` and returns a
173
+ CVPXY expression.
174
+
175
+ solver : str, optional
176
+ The solver to use. For example, "ECOS", "SCS", or "OSQP".
177
+ The default (`None`) is set depending on the problem.
178
+ For more details about available solvers, check the CVXPY documentation:
179
+ https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver
180
+
181
+ solver_params : dict, optional
182
+ Solver parameters. For example, `solver_params=dict(verbose=True)`.
183
+ For more details about solver arguments, check the CVXPY documentation:
184
+ https://www.cvxpy.org/tutorial/advanced/index.html#setting-solver-options
185
+
186
+ scale_objective : float, optional
187
+ Scale each objective element by this value.
188
+ It can be used to increase the optimization accuracies in specific cases.
189
+ The default (`None`) is set depending on the problem.
190
+
191
+ scale_constraints : float, optional
192
+ Scale each constraint element by this value.
193
+ It can be used to increase the optimization accuracies in specific cases.
194
+ The default (`None`) is set depending on the problem.
195
+
196
+ raise_on_failure : bool, default=True
197
+ If this is set to True, an error is raised when the optimization fail otherwise
198
+ it passes with a warning.
199
+
200
+ portfolio_params : dict, optional
201
+ Portfolio parameters passed to the portfolio evaluated by the `predict` and
202
+ `score` methods. If not provided, the `name`, `transaction_costs`,
203
+ `management_fees` and `previous_weights` are copied from the optimization
204
+ model and systematically passed to the portfolio.
205
+
206
+ Attributes
207
+ ----------
208
+ weights_ : ndarray of shape (n_assets,) or (n_optimizations, n_assets)
209
+ Weights of the assets.
210
+
211
+ problem_: cvxpy.Problem
212
+ CVXPY problem used for the optimization.
213
+
214
+ problem_values_ : dict[str, float] | list[dict[str, float]] of size n_optimizations
215
+ Expression values retrieved from the CVXPY problem.
216
+
217
+ prior_estimator_ : BasePrior
218
+ Fitted `prior_estimator`.
219
+
220
+ n_features_in_ : int
221
+ Number of assets seen during `fit`.
222
+
223
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
224
+ Names of assets seen during `fit`. Defined only when `X`
225
+ has assets names that are all strings.
226
+
227
+ References
228
+ ----------
229
+ .. [1] "Data-driven distributionally robust optimization using the Wasserstein
230
+ metric: performance guarantees and tractable reformulations".
231
+ Esfahani and Kuhn (2018).
232
+
233
+ .. [2] "Optimization of conditional value-at-risk".
234
+ Rockafellar and Uryasev (2000).
235
+ """
236
+
237
+ def __init__(
238
+ self,
239
+ risk_aversion: float = 1.0,
240
+ cvar_beta: float = 0.95,
241
+ wasserstein_ball_radius: float = 0.02,
242
+ prior_estimator: BasePrior | None = None,
243
+ min_weights: skt.MultiInput | None = 0.0,
244
+ max_weights: skt.MultiInput | None = 1.0,
245
+ budget: float | None = 1,
246
+ min_budget: float | None = None,
247
+ max_budget: float | None = None,
248
+ max_short: float | None = None,
249
+ max_long: float | None = None,
250
+ groups: skt.Groups | None = None,
251
+ linear_constraints: skt.LinearConstraints | None = None,
252
+ left_inequality: skt.Inequality | None = None,
253
+ right_inequality: skt.Inequality | None = None,
254
+ risk_free_rate: float = 0.0,
255
+ solver: str | None = None,
256
+ solver_params: dict | None = None,
257
+ scale_objective: float | None = None,
258
+ scale_constraints: float | None = None,
259
+ raise_on_failure: bool = True,
260
+ add_objective: skt.ExpressionFunction | None = None,
261
+ add_constraints: skt.ExpressionFunction | None = None,
262
+ overwrite_expected_return: skt.ExpressionFunction | None = None,
263
+ portfolio_params: dict | None = None,
264
+ ):
265
+ super().__init__(
266
+ risk_measure=RiskMeasure.CVAR,
267
+ prior_estimator=prior_estimator,
268
+ min_weights=min_weights,
269
+ max_weights=max_weights,
270
+ budget=budget,
271
+ min_budget=min_budget,
272
+ max_budget=max_budget,
273
+ max_short=max_short,
274
+ max_long=max_long,
275
+ groups=groups,
276
+ linear_constraints=linear_constraints,
277
+ left_inequality=left_inequality,
278
+ right_inequality=right_inequality,
279
+ risk_free_rate=risk_free_rate,
280
+ cvar_beta=cvar_beta,
281
+ solver=solver,
282
+ solver_params=solver_params,
283
+ scale_objective=scale_objective,
284
+ scale_constraints=scale_constraints,
285
+ raise_on_failure=raise_on_failure,
286
+ add_objective=add_objective,
287
+ add_constraints=add_constraints,
288
+ overwrite_expected_return=overwrite_expected_return,
289
+ portfolio_params=portfolio_params,
290
+ )
291
+ self.risk_aversion = risk_aversion
292
+ self.wasserstein_ball_radius = wasserstein_ball_radius
293
+
294
+ def fit(
295
+ self, X: npt.ArrayLike, y: npt.ArrayLike | None = None
296
+ ) -> "DistributionallyRobustCVaR":
297
+ """Fit the Distributionally Robust CVaR Optimization estimator.
298
+
299
+ Parameters
300
+ ----------
301
+ X : array-like of shape (n_observations, n_assets)
302
+ Price returns of the assets.
303
+
304
+ y : array-like of shape (n_observations, n_factors), optional
305
+ Price returns of factors.
306
+ The default is `None`.
307
+
308
+ Returns
309
+ -------
310
+ self : DistributionallyRobustCVaR
311
+ Fitted estimator.
312
+ """
313
+ self._check_feature_names(X, reset=True)
314
+ # Used to avoid adding multiple times similar constrains linked to identical
315
+ # risk models
316
+ self._clear_models_cache()
317
+ self.prior_estimator_ = check_estimator(
318
+ self.prior_estimator,
319
+ default=EmpiricalPrior(),
320
+ check_type=BasePrior,
321
+ )
322
+ self.prior_estimator_.fit(X, y)
323
+ prior_model = self.prior_estimator_.prior_model_
324
+ n_observations, n_assets = prior_model.returns.shape
325
+
326
+ # set solvers
327
+ self._set_solver(default="ECOS")
328
+
329
+ # set scale
330
+ self._set_scale_objective(default=1)
331
+ self._set_scale_constraints(default=1)
332
+
333
+ a1 = -1
334
+ b1 = cp.Constant(self.risk_aversion)
335
+ a2 = -1 - self.risk_aversion / (1 - self.cvar_beta)
336
+ b2 = cp.Constant(self.risk_aversion * (1 - 1 / (1 - self.cvar_beta)))
337
+ ones = np.ones(n_assets)
338
+ w = cp.Variable(n_assets)
339
+ u = cp.Variable((n_observations, n_assets))
340
+ v = cp.Variable((n_observations, n_assets))
341
+ lb = cp.Variable()
342
+ tau = cp.Variable()
343
+ s = cp.Variable(n_observations)
344
+
345
+ factor = cp.Constant(1)
346
+
347
+ # constraints
348
+ constraints = self._get_weight_constraints(
349
+ n_assets=n_assets, w=w, factor=factor
350
+ )
351
+ constraints += [
352
+ u * self._scale_constraints >= cp.Constant(0),
353
+ v * self._scale_constraints >= cp.Constant(0),
354
+ b1 * tau * self._scale_constraints
355
+ + a1 * (prior_model.returns @ w) * self._scale_constraints
356
+ + cp.multiply(u, (1 + prior_model.returns)) @ ones * self._scale_constraints
357
+ <= s * self._scale_constraints,
358
+ b2 * tau * self._scale_constraints
359
+ + a2 * (prior_model.returns @ w) * self._scale_constraints
360
+ + cp.multiply(v, (1 + prior_model.returns)) @ ones * self._scale_constraints
361
+ <= s * self._scale_constraints,
362
+ ]
363
+
364
+ for i in range(n_observations):
365
+ # noinspection PyTypeChecker
366
+ constraints.append(
367
+ cp.norm(-u[i] - a1 * w, np.inf) * self._scale_constraints
368
+ <= lb * self._scale_constraints
369
+ )
370
+ # noinspection PyTypeChecker
371
+ constraints.append(
372
+ cp.norm(-v[i] - a2 * w, np.inf) * self._scale_constraints
373
+ <= lb * self._scale_constraints
374
+ )
375
+
376
+ # custom objectives and constraints
377
+ custom_objective = self._get_custom_objective(w=w)
378
+ constraints += self._get_custom_constraints(w=w)
379
+
380
+ objective = cp.Minimize(
381
+ cp.Constant(self.wasserstein_ball_radius) * lb * self._scale_objective
382
+ + (1 / n_observations) * cp.sum(s) * self._scale_objective
383
+ + custom_objective * self._scale_objective
384
+ )
385
+
386
+ # problem
387
+ problem = cp.Problem(objective, constraints)
388
+
389
+ # results
390
+ self._solve_problem(problem=problem, w=w, factor=factor)
391
+
392
+ return self