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,417 @@
1
+ """Maximum Diversification Optimization estimator."""
2
+
3
+ # Author: Hugo Delatte <delatte.hugo@gmail.com>
4
+ # License: BSD 3 clause
5
+
6
+ import numpy as np
7
+ import numpy.typing as npt
8
+
9
+ import skfolio.typing as skt
10
+ from skfolio.measures import RiskMeasure
11
+ from skfolio.optimization.convex._base import ObjectiveFunction
12
+ from skfolio.optimization.convex._mean_risk import MeanRisk
13
+ from skfolio.prior import BasePrior
14
+
15
+
16
+ class MaximumDiversification(MeanRisk):
17
+ r"""Maximum Diversification Optimization estimator.
18
+
19
+ Maximizes the diversification ratio which is the ratio of the weighted volatilities
20
+ over the total volatility.
21
+
22
+ It is a special case of the :class:`~skfolio.optimization.MeanRisk` estimator where
23
+ the expected return from the objective function is replaced by the weighted
24
+ volatilies.
25
+
26
+ Parameters
27
+ ----------
28
+ prior_estimator : BasePrior, optional
29
+ :ref:`Prior estimator <prior>`.
30
+ The prior estimator is used to estimate the :class:`~skfolio.prior.PriorModel`
31
+ containing the estimation of assets expected returns, covariance matrix,
32
+ returns and Cholesky decomposition of the covariance.
33
+ The default (`None`) is to use :class:`~skfolio.prior.EmpiricalPrior`.
34
+
35
+ min_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=0.0
36
+ Minimum assets weights (weights lower bounds).
37
+ If a float is provided, it is applied to each asset.
38
+ `None` is equivalent to `-np.Inf` (no lower bound).
39
+ If a dictionary is provided, its (key/value) pair must be the
40
+ (asset name/asset minium weight) and the input `X` of the `fit` methods must
41
+ be a DataFrame with the assets names in columns.
42
+ When using a dictionary, assets values that are not provided are assigned
43
+ a minimum weight of `0.0`.
44
+ The default value is `0.0` (no short selling).
45
+
46
+ Example:
47
+
48
+ * `min_weights = 0` --> long only portfolio (no short selling).
49
+ * `min_weights = None` --> no lower bound (same as `-np.Inf`).
50
+ * `min_weights = -2` --> each weight must be above -200%.
51
+ * `min_weights = {"SX5E": 0, "SPX": -2}`
52
+ * `min_weights = [0, -2]`
53
+
54
+ max_weights : float | dict[str, float] | array-like of shape (n_assets, ) | None, default=1.0
55
+ Maximum assets weights (weights upper bounds).
56
+ If a float is provided, it is applied to each asset.
57
+ `None` is equivalent to `+np.Inf` (no upper bound).
58
+ If a dictionary is provided, its (key/value) pair must be the
59
+ (asset name/asset maximum weight) and the input `X` of the `fit` methods must
60
+ be a DataFrame with the assets names in columns.
61
+ When using a dictionary, assets values that are not provided are assigned
62
+ a minimum weight of `1.0`.
63
+ The default value is `1.0` (each asset is below 100%).
64
+
65
+ Example:
66
+
67
+ * `max_weights = 0` --> no long position (short only portfolio).
68
+ * `max_weights = None` --> no upper bound.
69
+ * `max_weights = 2` --> each weight must be below 200%.
70
+ * `max_weights = {"SX5E": 1, "SPX": 2}`
71
+ * `max_weights = [1, 2]`
72
+
73
+ budget : float | None, default=1.0
74
+ Investment budget. It is the sum of long positions and short positions (sum of
75
+ all weights). `None` means no budget constraints.
76
+ The default value is `1.0` (fully invested portfolio).
77
+
78
+ Examples:
79
+
80
+ * `budget = 1` --> fully invested portfolio.
81
+ * `budget = 0` --> market neutral portfolio.
82
+ * `budget = None` --> no constraints on the sum of weights.
83
+
84
+ min_budget : float, optional
85
+ Minimum budget. It is the lower bound of the sum of long and short positions
86
+ (sum of all weights). If provided, you must set `budget=None`.
87
+ The default (`None`) means no minimum budget constraint.
88
+
89
+ max_budget : float, optional
90
+ Maximum budget. It is the upper bound of the sum of long and short positions
91
+ (sum of all weights). If provided, you must set `budget=None`.
92
+ The default (`None`) means no maximum budget constraint.
93
+
94
+ max_short : float, optional
95
+ Maximum short position. The short position is defined as the sum of negative
96
+ weights (in absolute term).
97
+ The default (`None`) means no maximum short position.
98
+
99
+ max_long : float, optional
100
+ Maximum long position. The long position is defined as the sum of positive
101
+ weights.
102
+ The default (`None`) means no maximum long position.
103
+
104
+ transaction_costs : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
105
+ Transaction costs of the assets. It is used to add linear transaction costs to
106
+ the optimization problem:
107
+
108
+ .. math:: total\_cost = \sum_{i=1}^{N} c_{i} \times |w_{i} - w\_prev_{i}|
109
+
110
+ with :math:`c_{i}` the transaction cost of asset i, :math:`w_{i}` its weight
111
+ and :math:`w\_prev_{i}` its previous weight (defined in `previous_weights`).
112
+ The float :math:`total\_cost` is used in the portfolio expected return:
113
+
114
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_cost
115
+
116
+ with :math:`\mu` the vector af assets' expected returns and :math:`w` the
117
+ vector of assets weights.
118
+
119
+ If a float is provided, it is applied to each asset.
120
+ If a dictionary is provided, its (key/value) pair must be the
121
+ (asset name/asset cost) and the input `X` of the `fit` methods must be a
122
+ DataFrame with the assets names in columns.
123
+ The default value is `0.0`.
124
+
125
+ .. warning::
126
+
127
+ Based on the above formula, the periodicity of the transaction costs
128
+ needs to be homogenous to the periodicity of :math:`\mu`. For example, if
129
+ the input `X` is composed of **daily** returns, the `transaction_costs` need
130
+ to be expressed in **daily** costs.
131
+ (See :ref:`sphx_glr_auto_examples_1_mean_risk_plot_6_transaction_costs.py`)
132
+
133
+ management_fees : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
134
+ Management fees of the assets. It is used to add linear management fees to the
135
+ optimization problem:
136
+
137
+ .. math:: total\_fee = \sum_{i=1}^{N} f_{i} \times w_{i}
138
+
139
+ with :math:`f_{i}` the management fee of asset i and :math:`w_{i}` its weight.
140
+ The float :math:`total\_fee` is used in the portfolio expected return:
141
+
142
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_fee
143
+
144
+ with :math:`\mu` the vector af assets expected returns and :math:`w` the vector
145
+ of assets weights.
146
+
147
+ If a float is provided, it is applied to each asset.
148
+ If a dictionary is provided, its (key/value) pair must be the
149
+ (asset name/asset fee) and the input `X` of the `fit` methods must be a
150
+ DataFrame with the assets names in columns.
151
+ The default value is `0.0`.
152
+
153
+ .. warning::
154
+
155
+ Based on the above formula, the periodicity of the management fees needs to
156
+ be homogenous to the periodicity of :math:`\mu`. For example, if the input
157
+ `X` is composed of **daily** returns, the `management_fees` need to be
158
+ expressed in **daily** fees.
159
+
160
+ .. note::
161
+
162
+ Another approach is to directly impact the management fees to the input `X`
163
+ in order to express the returns net of fees. However, when estimating the
164
+ :math:`\mu` parameter using for example Shrinkage estimators, this approach
165
+ would mix a deterministic value with an uncertain one leading to unwanted
166
+ bias in the management fees.
167
+
168
+ previous_weights : float | dict[str, float] | array-like of shape (n_assets, ), optional
169
+ Previous weights of the assets. Previous weights are used to compute the
170
+ portfolio cost and the portfolio turnover.
171
+ If a float is provided, it is applied to each asset.
172
+ If a dictionary is provided, its (key/value) pair must be the
173
+ (asset name/asset previous weight) and the input `X` of the `fit` methods must
174
+ be a DataFrame with the assets names in columns.
175
+ The default (`None`) means no previous weights.
176
+
177
+ l1_coef : float, default=0.0
178
+ L1 regularization coefficient.
179
+ It is used to penalize the objective function by the L1 norm:
180
+
181
+ .. math:: l1\_coef \times \Vert w \Vert_{1} = l1\_coef \times \sum_{i=1}^{N} |w_{i}|
182
+
183
+ Increasing this coefficient will reduce the number of non-zero weights
184
+ (cardinality). It tends to increase robustness (out-of-sample stability) but
185
+ reduces diversification.
186
+ The default value is `0.0`.
187
+
188
+ l2_coef : float, default=0.0
189
+ L2 regularization coefficient.
190
+ It is used to penalize the objective function by the L2 norm:
191
+
192
+ .. math:: l2\_coef \times \Vert w \Vert_{2}^{2} = l2\_coef \times \sum_{i=1}^{N} w_{i}^2
193
+
194
+ It tends to increase robustness (out-of-sample stability).
195
+ The default value is `0.0`.
196
+
197
+ linear_constraints : array-like of shape (n_constraints,), optional
198
+ Linear constraints.
199
+ The linear constraints must match any of following patterns:
200
+
201
+ * "2.5 * ref1 + 0.10 * ref2 + 0.0013 <= 2.5 * ref3"
202
+ * "ref1 >= 2.9 * ref2"
203
+ * "ref1 <= ref2"
204
+ * "ref1 >= ref1"
205
+
206
+ With "ref1", "ref2" ... the assets names or the groups names provided
207
+ in the parameter `groups`. Assets names can be referenced without the need of
208
+ `groups` if the input `X` of the `fit` methods is a DataFrame with these
209
+ assets names in columns.
210
+
211
+ Examples:
212
+
213
+ * "SPX >= 0.10" --> SPX weight must be greater than 10% (note that you can also use `min_weights`)
214
+ * "SX5E + TLT >= 0.2" --> the sum of SX5E and TLT weights must be greater than 20%
215
+ * "US >= 0.7" --> the sum of all US weights must be greater than 70%
216
+ * "Equity <= 3 * Bond" --> the sum of all Equity weights must be less or equal to 3 times the sum of all Bond weights.
217
+ * "2*SPX + 3*Europe <= Bond + 0.05" --> mixing assets and group constraints
218
+
219
+ groups : dict[str, list[str]] or array-like of shape (n_groups, n_assets), optional
220
+ The assets groups referenced in `linear_constraints`.
221
+ If a dictionary is provided, its (key/value) pair must be the
222
+ (asset name/asset groups) and the input `X` of the `fit` methods must be a
223
+ DataFrame with the assets names in columns.
224
+
225
+ Examples:
226
+
227
+ * groups = {"SX5E": ["Equity", "Europe"], "SPX": ["Equity", "US"], "TLT": ["Bond", "US"]}
228
+ * groups = [["Equity", "Equity", "Bond"], ["Europe", "US", "US"]]
229
+
230
+ left_inequality : array-like of shape (n_constraints, n_assets), optional
231
+ Left inequality matrix :math:`A` of the linear
232
+ constraint :math:`A \cdot w \leq b`.
233
+
234
+ right_inequality : array-like of shape (n_constraints, ), optional
235
+ Right inequality vector :math:`b` of the linear
236
+ constraint :math:`A \cdot w \leq b`.
237
+
238
+ risk_free_rate : float, default=0.0
239
+ Risk-free interest rate.
240
+ The default value is `0.0`.
241
+
242
+ max_tracking_error : float, optional
243
+ Upper bound constraint on the tracking error.
244
+ The tracking error is defined as the RMSE (root-mean-square error) of the
245
+ portfolio returns compared to a target returns. If `max_tracking_error` is
246
+ provided, the target returns `y` must be provided in the `fit` method.
247
+
248
+ max_turnover : float, optional
249
+ Upper bound constraint of the turnover.
250
+ The turnover is defined as the absolute difference between the portfolio weights
251
+ and the `previous_weights`. Note that another way to control for turnover is by
252
+ using the `transaction_costs` parameter.
253
+
254
+ min_return : float | array-like of shape (n_optimization), optional
255
+ Lower bound constraint on the expected return.
256
+
257
+ min_return : float | array-like of shape (n_optimization), optional
258
+ Lower bound constraint on the expected return.
259
+
260
+ add_objective : Callable[[cp.Variable], cp.Expression], optional
261
+ Add a custom objective to the existing objective expression.
262
+ It is a function that must take as argument the weights `w` and returns a
263
+ CVXPY expression.
264
+
265
+ add_constraints : Callable[[cp.Variable], cp.Expression|list[cp.Expression]], optional
266
+ Add a custom constraint or a list of constraints to the existing constraints.
267
+ It is a function that must take as argument the weights `w` and returns a
268
+ CVPXY expression or a list of CVPXY expressions.
269
+
270
+ solver : str, optional
271
+ The solver to use. For example, "ECOS", "SCS", or "OSQP".
272
+ The default (`None`) is set depending on the problem.
273
+ For more details about available solvers, check the CVXPY documentation:
274
+ https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver
275
+
276
+ solver_params : dict, optional
277
+ Solver parameters. For example, `solver_params=dict(verbose=True)`.
278
+ For more details about solver arguments, check the CVXPY documentation:
279
+ https://www.cvxpy.org/tutorial/advanced/index.html#setting-solver-options
280
+
281
+ scale_objective : float, optional
282
+ Scale each objective element by this value.
283
+ It can be used to increase the optimization accuracies in specific cases.
284
+ The default (`None`) is set depending on the problem.
285
+
286
+ scale_constraints : float, optional
287
+ Scale each constraint element by this value.
288
+ It can be used to increase the optimization accuracies in specific cases.
289
+ The default (`None`) is set depending on the problem.
290
+
291
+ raise_on_failure : bool, default=True
292
+ If this is set to True, an error is raised when the optimization fail otherwise
293
+ it passes with a warning.
294
+
295
+ portfolio_params : dict, optional
296
+ Portfolio parameters passed to the portfolio evaluated by the `predict` and
297
+ `score` methods. If not provided, the `name`, `transaction_costs`,
298
+ `management_fees` and `previous_weights` are copied from the optimization
299
+ model and systematically passed to the portfolio.
300
+
301
+ Attributes
302
+ ----------
303
+ weights_ : ndarray of shape (n_assets,) or (n_optimizations, n_assets)
304
+ Weights of the assets.
305
+
306
+ problem_: cvxpy.Problem
307
+ CVXPY problem used for the optimization.
308
+
309
+ problem_values_ : dict[str, float] | list[dict[str, float]] of size n_optimizations
310
+ Expression values retrieved from the CVXPY problem.
311
+
312
+ prior_estimator_ : BasePrior
313
+ Fitted `prior_estimator`.
314
+
315
+ n_features_in_ : int
316
+ Number of assets seen during `fit`.
317
+
318
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
319
+ Names of assets seen during `fit`. Defined only when `X`
320
+ has assets names that are all strings.
321
+ """
322
+
323
+ def __init__(
324
+ self,
325
+ prior_estimator: BasePrior | None = None,
326
+ min_weights: skt.MultiInput | None = 0.0,
327
+ max_weights: skt.MultiInput | None = 1.0,
328
+ budget: float | None = 1.0,
329
+ min_budget: float | None = None,
330
+ max_budget: float | None = None,
331
+ max_short: float | None = None,
332
+ max_long: float | None = None,
333
+ transaction_costs: skt.MultiInput = 0.0,
334
+ management_fees: skt.MultiInput = 0.0,
335
+ previous_weights: skt.MultiInput | None = None,
336
+ groups: skt.Groups | None = None,
337
+ linear_constraints: skt.LinearConstraints | None = None,
338
+ left_inequality: skt.Inequality | None = None,
339
+ right_inequality: skt.Inequality | None = None,
340
+ l1_coef: float = 0.0,
341
+ l2_coef: float = 0.0,
342
+ risk_free_rate: float = 0.0,
343
+ min_return: skt.Target | None = None,
344
+ max_tracking_error: skt.Target | None = None,
345
+ max_turnover: skt.Target | None = None,
346
+ solver: str | None = None,
347
+ solver_params: dict | None = None,
348
+ scale_objective: float | None = None,
349
+ scale_constraints: float | None = None,
350
+ raise_on_failure: bool = True,
351
+ add_objective: skt.ExpressionFunction | None = None,
352
+ add_constraints: skt.ExpressionFunction | None = None,
353
+ portfolio_params: dict | None = None,
354
+ ):
355
+ super().__init__(
356
+ objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
357
+ risk_measure=RiskMeasure.VARIANCE,
358
+ prior_estimator=prior_estimator,
359
+ min_weights=min_weights,
360
+ max_weights=max_weights,
361
+ budget=budget,
362
+ min_budget=min_budget,
363
+ max_budget=max_budget,
364
+ max_short=max_short,
365
+ max_long=max_long,
366
+ transaction_costs=transaction_costs,
367
+ management_fees=management_fees,
368
+ previous_weights=previous_weights,
369
+ groups=groups,
370
+ linear_constraints=linear_constraints,
371
+ left_inequality=left_inequality,
372
+ right_inequality=right_inequality,
373
+ l1_coef=l1_coef,
374
+ l2_coef=l2_coef,
375
+ risk_free_rate=risk_free_rate,
376
+ min_return=min_return,
377
+ max_tracking_error=max_tracking_error,
378
+ max_turnover=max_turnover,
379
+ solver=solver,
380
+ solver_params=solver_params,
381
+ scale_objective=scale_objective,
382
+ scale_constraints=scale_constraints,
383
+ raise_on_failure=raise_on_failure,
384
+ add_objective=add_objective,
385
+ add_constraints=add_constraints,
386
+ portfolio_params=portfolio_params,
387
+ )
388
+
389
+ def fit(
390
+ self, X: npt.ArrayLike, y: npt.ArrayLike | None = None
391
+ ) -> "MaximumDiversification":
392
+ """Fit the Maximum Diversification Optimization estimator.
393
+
394
+ Parameters
395
+ ----------
396
+ X : array-like of shape (n_observations, n_assets)
397
+ Price returns of the assets.
398
+
399
+ y : array-like of shape (n_observations, n_targets), optional
400
+ Price returns of factors or a target benchmark.
401
+ The default is `None`.
402
+
403
+ Returns
404
+ -------
405
+ self : MaximumDiversification
406
+ Fitted estimator.
407
+ """
408
+ self._check_feature_names(X, reset=True)
409
+
410
+ def func(w, obj):
411
+ """weighted volatilities"""
412
+ covariance = obj.prior_estimator_.prior_model_.covariance
413
+ return np.sqrt(np.diag(covariance)) @ w
414
+
415
+ self.overwrite_expected_return = func
416
+ super().fit(X, y)
417
+ return self