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,368 @@
1
+ """Hierarchical Risk Parity 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
+ import pandas as pd
9
+ import scipy.cluster.hierarchy as sch
10
+
11
+ import skfolio.typing as skt
12
+ from skfolio.cluster import HierarchicalClustering
13
+ from skfolio.distance import BaseDistance, PearsonDistance
14
+ from skfolio.measures import ExtraRiskMeasure, RiskMeasure
15
+ from skfolio.optimization.cluster.hierarchical._base import (
16
+ BaseHierarchicalOptimization,
17
+ )
18
+ from skfolio.prior import BasePrior, EmpiricalPrior
19
+ from skfolio.utils.tools import bisection, check_estimator
20
+
21
+
22
+ class HierarchicalRiskParity(BaseHierarchicalOptimization):
23
+ r"""Hierarchical Risk Parity estimator.
24
+
25
+ Hierarchical Risk Parity is a portfolio optimization method developed by Marcos
26
+ Lopez de Prado [2]_.
27
+
28
+ This algorithm uses a distance matrix to compute hierarchical clusters using the
29
+ Hierarchical Tree Clustering algorithm then uses seriation to rearrange the assets
30
+ in the dendrogram minimizing the distance between leafs.
31
+ The final step is the recursive bisection where each cluster is split between two
32
+ sub-clusters by starting with the topmost cluster and traversing in a top-down
33
+ manner. For each sub-cluster we compute the total cluster risk of an inverse-risk
34
+ allocation. A weighting factor is then computed from these two sub-cluster risks
35
+ which is used to update the cluster weight.
36
+
37
+ .. note ::
38
+ The original paper uses the variance as the risk measure and the single-linkage
39
+ method for the Hierarchical Tree Clustering algorithm. Here we generalize it to
40
+ multiple risk measures and linkage methods.
41
+ The default linkage method is set to the Ward
42
+ variance minimization algorithm which is more stable and have better properties
43
+ than the single-linkage method [4]_.
44
+
45
+ Parameters
46
+ ----------
47
+ risk_measure : RiskMeasure or ExtraRiskMeasure, default=RiskMeasure.VARIANCE
48
+ :class:`~skfolio.meta.RiskMeasure` or :class:`~skfolio.meta.ExtraRiskMeasure`
49
+ of the optimization.
50
+ Can be any of:
51
+
52
+ * MEAN_ABSOLUTE_DEVIATION
53
+ * FIRST_LOWER_PARTIAL_MOMENT
54
+ * VARIANCE
55
+ * SEMI_VARIANCE
56
+ * CVAR
57
+ * EVAR
58
+ * WORST_REALIZATION
59
+ * CDAR
60
+ * MAX_DRAWDOWN
61
+ * AVERAGE_DRAWDOWN
62
+ * EDAR
63
+ * ULCER_INDEX
64
+ * GINI_MEAN_DIFFERENCE_RATIO
65
+ * VALUE_AT_RISK
66
+ * DRAWDOWN_AT_RISK
67
+ * ENTROPIC_RISK_MEASURE
68
+ * FOURTH_CENTRAL_MOMENT
69
+ * FOURTH_LOWER_PARTIAL_MOMENT
70
+ * SKEW
71
+ * KURTOSIS
72
+
73
+ The default is `RiskMeasure.VARIANCE`.
74
+
75
+ prior_estimator : BasePrior, optional
76
+ :ref:`Prior estimator <prior>`.
77
+ The prior estimator is used to estimate the :class:`~skfolio.prior.PriorModel`
78
+ containing the estimation of assets expected returns, covariance matrix and
79
+ returns. The moments and returns estimations are used for the risk computation
80
+ and the returns estimation are used by the distance matrix estimator.
81
+ The default (`None`) is to use :class:`~skfolio.prior.EmpiricalPrior`.
82
+
83
+ distance_estimator : BaseDistance, optional
84
+ :ref:`Distance estimator <distance>`.
85
+ The distance estimator is used to estimate the codependence and the distance
86
+ matrix needed for the computation of the linkage matrix.
87
+ The default (`None`) is to use :class:`~skfolio.distance.PearsonDistance`.
88
+
89
+ hierarchical_clustering_estimator : HierarchicalClustering, optional
90
+ :ref:`Hierarchical Clustering estimator <hierarchical_clustering>`.
91
+ The hierarchical clustering estimator is used to compute the linkage matrix
92
+ and the hierarchical clustering of the assets based on the distance matrix.
93
+ The default (`None`) is to use
94
+ :class:`~skfolio.cluster.HierarchicalClustering`.
95
+
96
+ min_weights : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
97
+ Minimum assets weights (weights lower bounds). Negative weights are not allowed.
98
+ If a float is provided, it is applied to each asset. `None` is equivalent to
99
+ `-np.Inf` (no lower bound). If a dictionary is provided, its (key/value) pair
100
+ must be the (asset name/asset minium weight) and the input `X` of the `fit`
101
+ methods must be a DataFrame with the assets names in columns. When using a
102
+ dictionary, assets values that are not provided are assigned a minimum weight
103
+ of `0.0`. The default is 0.0 (no short selling).
104
+
105
+ Example:
106
+
107
+ * min_weights = 0 --> long only portfolio (no short selling).
108
+ * min_weights = None --> no lower bound (same as `-np.Inf`).
109
+ * min_weights = {"SX5E": 0, "SPX": 0.1}
110
+ * min_weights = [0, 0.1]
111
+
112
+ max_weights : float | dict[str, float] | array-like of shape (n_assets, ), default=1.0
113
+ Maximum assets weights (weights upper bounds). Weights above 1.0 are not
114
+ allowed. If a float is provided, it is applied to each asset. `None` is
115
+ equivalent to `+np.Inf` (no upper bound). If a dictionary is provided, its
116
+ (key/value) pair must be the (asset name/asset maximum weight) and the input `X`
117
+ of the `fit` methods must be a DataFrame with the assets names in columns. When
118
+ using a dictionary, assets values that are not provided are assigned a minimum
119
+ weight of `1.0`. The default is 1.0 (each asset is below 100%).
120
+
121
+ Example:
122
+
123
+ * max_weights = 0 --> no long position (short only portfolio).
124
+ * max_weights = 0.5 --> each weight must be below 50%.
125
+ * max_weights = {"SX5E": 1, "SPX": 0.25}
126
+ * max_weights = [1, 0.25]
127
+
128
+ transaction_costs : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
129
+ Transaction costs of the assets. It is used to add linear transaction costs to
130
+ the optimization problem:
131
+
132
+ .. math:: total\_cost = \sum_{i=1}^{N} c_{i} \times |w_{i} - w\_prev_{i}|
133
+
134
+ with :math:`c_{i}` the transaction cost of asset i, :math:`w_{i}` its weight
135
+ and :math:`w\_prev_{i}` its previous weight (defined in `previous_weights`).
136
+ The float :math:`total\_cost` is used in the portfolio expected return:
137
+
138
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_cost
139
+
140
+ with :math:`\mu` the vector af assets' expected returns and :math:`w` the
141
+ vector of assets weights.
142
+
143
+ If a float is provided, it is applied to each asset.
144
+ If a dictionary is provided, its (key/value) pair must be the
145
+ (asset name/asset cost) and the input `X` of the `fit` methods must be a
146
+ DataFrame with the assets names in columns.
147
+ The default value is `0.0`.
148
+
149
+ .. warning::
150
+
151
+ Based on the above formula, the periodicity of the transaction costs
152
+ needs to be homogenous to the periodicity of :math:`\mu`. For example, if
153
+ the input `X` is composed of **daily** returns, the `transaction_costs` need
154
+ to be expressed in **daily** costs.
155
+ (See :ref:`sphx_glr_auto_examples_1_mean_risk_plot_6_transaction_costs.py`)
156
+
157
+ management_fees : float | dict[str, float] | array-like of shape (n_assets, ), default=0.0
158
+ Management fees of the assets. It is used to add linear management fees to the
159
+ optimization problem:
160
+
161
+ .. math:: total\_fee = \sum_{i=1}^{N} f_{i} \times w_{i}
162
+
163
+ with :math:`f_{i}` the management fee of asset i and :math:`w_{i}` its weight.
164
+ The float :math:`total\_fee` is used in the portfolio expected return:
165
+
166
+ .. math:: expected\_return = \mu^{T} \cdot w - total\_fee
167
+
168
+ with :math:`\mu` the vector af assets expected returns and :math:`w` the vector
169
+ of assets weights.
170
+
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 fee) and the input `X` of the `fit` methods must be a
174
+ DataFrame with the assets names in columns.
175
+ The default value is `0.0`.
176
+
177
+ .. warning::
178
+
179
+ Based on the above formula, the periodicity of the management fees needs to
180
+ be homogenous to the periodicity of :math:`\mu`. For example, if the input
181
+ `X` is composed of **daily** returns, the `management_fees` need to be
182
+ expressed in **daily** fees.
183
+
184
+ .. note::
185
+
186
+ Another approach is to directly impact the management fees to the input `X`
187
+ in order to express the returns net of fees. However, when estimating the
188
+ :math:`\mu` parameter using for example Shrinkage estimators, this approach
189
+ would mix a deterministic value with an uncertain one leading to unwanted
190
+ bias in the management fees.
191
+
192
+ previous_weights : float | dict[str, float] | array-like of shape (n_assets, ), optional
193
+ Previous weights of the assets. Previous weights are used to compute the
194
+ portfolio total cost. If a float is provided, it is applied to each asset.
195
+ If a dictionary is provided, its (key/value) pair must be the
196
+ (asset name/asset previous weight) and the input `X` of the `fit` methods must
197
+ be a DataFrame with the assets names in columns.
198
+ The default (`None`) means no previous weights.
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,)
209
+ Weights of the assets.
210
+
211
+ distance_estimator_ : BaseDistance
212
+ Fitted `distance_estimator`.
213
+
214
+ hierarchical_clustering_estimator_ : HierarchicalClustering
215
+ Fitted `hierarchical_clustering_estimator`.
216
+
217
+ n_features_in_ : int
218
+ Number of assets seen during `fit`.
219
+
220
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
221
+ Names of assets seen during `fit`. Defined only when `X`
222
+ has assets names that are all strings.
223
+
224
+ References
225
+ ----------
226
+ .. [1] "Building diversified portfolios that outperform out of sample",
227
+ The Journal of Portfolio Management,
228
+ Marcos López de Prado (2016).
229
+
230
+ .. [2] "A robust estimator of the efficient frontier",
231
+ SSRN Electronic Journal,
232
+ Marcos López de Prado (2019).
233
+
234
+ .. [3] "Machine Learning for Asset Managers",
235
+ Elements in Quantitative Finance. Cambridge University Press,
236
+ Marcos López de Prado (2020).
237
+
238
+ .. [4] "A review of two decades of correlations, hierarchies, networks and
239
+ clustering in financial markets",
240
+ Gautier Marti, Frank Nielsen, Mikołaj Bińkowski, Philippe Donnat (2020).
241
+ """
242
+
243
+ def __init__(
244
+ self,
245
+ risk_measure: RiskMeasure | ExtraRiskMeasure = RiskMeasure.VARIANCE,
246
+ prior_estimator: BasePrior | None = None,
247
+ distance_estimator: BaseDistance | None = None,
248
+ hierarchical_clustering_estimator: HierarchicalClustering | None = None,
249
+ min_weights: skt.MultiInput | None = 0.0,
250
+ max_weights: skt.MultiInput | None = 1.0,
251
+ transaction_costs: skt.MultiInput = 0.0,
252
+ management_fees: skt.MultiInput = 0.0,
253
+ previous_weights: skt.MultiInput | None = None,
254
+ portfolio_params: dict | None = None,
255
+ ):
256
+ super().__init__(
257
+ risk_measure=risk_measure,
258
+ prior_estimator=prior_estimator,
259
+ distance_estimator=distance_estimator,
260
+ hierarchical_clustering_estimator=hierarchical_clustering_estimator,
261
+ min_weights=min_weights,
262
+ max_weights=max_weights,
263
+ transaction_costs=transaction_costs,
264
+ management_fees=management_fees,
265
+ previous_weights=previous_weights,
266
+ portfolio_params=portfolio_params,
267
+ )
268
+
269
+ def fit(self, X: npt.ArrayLike, y: None = None) -> "HierarchicalRiskParity":
270
+ """Fit the Hierarchical Risk Parity Optimization estimator.
271
+
272
+ Parameters
273
+ ----------
274
+ X : array-like of shape (n_observations, n_assets)
275
+ Price returns of the assets.
276
+
277
+ y : Ignored
278
+ Not used, present for API consistency by convention.
279
+
280
+ Returns
281
+ -------
282
+ self : HierarchicalRiskParity
283
+ Fitted estimator.
284
+ """
285
+ # Validate
286
+ if not isinstance(self.risk_measure, RiskMeasure | ExtraRiskMeasure):
287
+ raise TypeError(
288
+ "`risk_measure` must be of type `RiskMeasure` or `ExtraRiskMeasure`"
289
+ )
290
+ self.prior_estimator_ = check_estimator(
291
+ self.prior_estimator,
292
+ default=EmpiricalPrior(),
293
+ check_type=BasePrior,
294
+ )
295
+ self.distance_estimator_ = check_estimator(
296
+ self.distance_estimator,
297
+ default=PearsonDistance(),
298
+ check_type=BaseDistance,
299
+ )
300
+ self.hierarchical_clustering_estimator_ = check_estimator(
301
+ self.hierarchical_clustering_estimator,
302
+ default=HierarchicalClustering(),
303
+ check_type=HierarchicalClustering,
304
+ )
305
+
306
+ # Fit the estimators
307
+ self.prior_estimator_.fit(X, y)
308
+ prior_model = self.prior_estimator_.prior_model_
309
+ returns = prior_model.returns
310
+
311
+ # To keep the asset_names
312
+ if isinstance(X, pd.DataFrame):
313
+ returns = pd.DataFrame(returns, columns=X.columns)
314
+
315
+ self.distance_estimator_.fit(returns)
316
+ distance = self.distance_estimator_.distance_
317
+
318
+ # To keep the asset_names
319
+ if isinstance(X, pd.DataFrame):
320
+ distance = pd.DataFrame(distance, columns=X.columns)
321
+
322
+ self.hierarchical_clustering_estimator_.fit(distance)
323
+
324
+ X = self._validate_data(X)
325
+ n_assets = X.shape[1]
326
+
327
+ min_weights, max_weights = self._convert_weights_bounds(n_assets=n_assets)
328
+ assets_risks = self._unitary_risks(prior_model=prior_model)
329
+
330
+ ordered_linkage_matrix = sch.optimal_leaf_ordering(
331
+ self.hierarchical_clustering_estimator_.linkage_matrix_,
332
+ self.hierarchical_clustering_estimator_.condensed_distance_,
333
+ )
334
+ sorted_assets = sch.leaves_list(ordered_linkage_matrix)
335
+
336
+ weights = np.ones(n_assets)
337
+ items = [sorted_assets]
338
+
339
+ while len(items) > 0:
340
+ new_items = []
341
+ for clusters_ids in bisection(items):
342
+ new_items += clusters_ids
343
+ risks = []
344
+ for ids in clusters_ids:
345
+ inv_risk_w = np.zeros(n_assets)
346
+ inv_risk_w[ids] = 1 / assets_risks[ids]
347
+ inv_risk_w /= inv_risk_w.sum()
348
+ risks.append(
349
+ self._risk(weights=inv_risk_w, prior_model=prior_model)
350
+ )
351
+ left_risk, right_risk = risks
352
+ left_cluster, right_cluster = clusters_ids
353
+ alpha = 1 - left_risk / (left_risk + right_risk)
354
+ # Weights constraints
355
+ alpha = self._apply_weight_constraints_to_alpha(
356
+ alpha=alpha,
357
+ weights=weights,
358
+ max_weights=max_weights,
359
+ min_weights=min_weights,
360
+ left_cluster=left_cluster,
361
+ right_cluster=right_cluster,
362
+ )
363
+ weights[left_cluster] *= alpha
364
+ weights[right_cluster] *= 1 - alpha
365
+ items = new_items
366
+
367
+ self.weights_ = weights
368
+ return self
@@ -0,0 +1,16 @@
1
+ from skfolio.optimization.convex._base import ConvexOptimization, ObjectiveFunction
2
+ from skfolio.optimization.convex._distributionally_robust import (
3
+ DistributionallyRobustCVaR,
4
+ )
5
+ from skfolio.optimization.convex._maximum_diversification import MaximumDiversification
6
+ from skfolio.optimization.convex._mean_risk import MeanRisk
7
+ from skfolio.optimization.convex._risk_budgeting import RiskBudgeting
8
+
9
+ __all__ = [
10
+ "ObjectiveFunction",
11
+ "ConvexOptimization",
12
+ "MeanRisk",
13
+ "RiskBudgeting",
14
+ "DistributionallyRobustCVaR",
15
+ "MaximumDiversification",
16
+ ]