riskfolio-lib 7.2.0__cp313-cp313-macosx_10_13_x86_64.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.
riskfolio/__init__.py ADDED
@@ -0,0 +1,14 @@
1
+ """""" #
2
+
3
+ """
4
+ Copyright (c) 2020-2026, Dany Cajas
5
+ All rights reserved.
6
+ This work is licensed under BSD 3-Clause "New" or "Revised" License.
7
+ License available at https://github.com/dcajasn/Riskfolio-Lib/blob/master/LICENSE.txt
8
+ """
9
+
10
+ from riskfolio.src import *
11
+ from riskfolio.external import *
12
+ from riskfolio.version import (
13
+ version as __version__,
14
+ ) # riskfolio/version.py is auto-generated
@@ -0,0 +1,10 @@
1
+ """""" #
2
+
3
+ """
4
+ Copyright (c) 2020-2026, Dany Cajas
5
+ All rights reserved.
6
+ This work is licensed under BSD 3-Clause "New" or "Revised" License.
7
+ License available at https://github.com/dcajasn/Riskfolio-Lib/blob/master/LICENSE.txt
8
+ """
9
+
10
+ from .cppfunctions import *
@@ -0,0 +1,376 @@
1
+ """""" #
2
+
3
+ """
4
+ Copyright (c) 2020-2026, Dany Cajas
5
+ All rights reserved.
6
+ This work is licensed under BSD 3-Clause "New" or "Revised" License.
7
+ License available at https://github.com/dcajasn/Riskfolio-Lib/blob/master/LICENSE.txt
8
+ """
9
+
10
+ import numpy as np
11
+ import pandas as pd
12
+ from riskfolio.external.functions import *
13
+ from itertools import product
14
+
15
+ __all__ = [
16
+ "duplication_matrix",
17
+ "duplication_elimination_matrix",
18
+ "duplication_summation_matrix",
19
+ "commutation_matrix",
20
+ "coskewness_matrix",
21
+ "semi_coskewness_matrix",
22
+ "cokurtosis_matrix",
23
+ "semi_cokurtosis_matrix",
24
+ "k_eigh",
25
+ "d_corr",
26
+ "d_corr_matrix",
27
+ "residuals_coskewness_fm",
28
+ "residuals_cokurtosis_fm",
29
+ ]
30
+
31
+
32
+ def duplication_matrix(n: int, diag: bool = True):
33
+ r"""
34
+ Calculate duplication matrix of size "n" as shown in :cite:`d-Magnus1980`.
35
+
36
+ Parameters
37
+ ----------
38
+ n : int
39
+ Number of assets.
40
+
41
+ Returns
42
+ -------
43
+ D: np.ndarray
44
+ Duplication matrix
45
+ """
46
+ return cpp_duplication_matrix(n, diag)
47
+
48
+
49
+ def duplication_elimination_matrix(n: int, diag: bool = True):
50
+ r"""
51
+ Calculate duplication elimination matrix of size "n" as shown in :cite:`d-Magnus1980`.
52
+
53
+ Parameters
54
+ ----------
55
+ n : int
56
+ Number of assets.
57
+
58
+ Returns
59
+ -------
60
+ L: np.ndarray
61
+ Duplication matrix
62
+ """
63
+ return cpp_duplication_elimination_matrix(n, diag)
64
+
65
+
66
+ def duplication_summation_matrix(n: int, diag: bool = True):
67
+ r"""
68
+ Calculate duplication summation matrix of size "n" as shown in :cite:`d-Cajas4`.
69
+
70
+ Parameters
71
+ ----------
72
+ n : int
73
+ Number of assets.
74
+
75
+ Returns
76
+ -------
77
+ S: np.ndarray
78
+ Duplication summation matrix.
79
+ """
80
+ return cpp_duplication_summation_matrix(n, diag)
81
+
82
+
83
+ def commutation_matrix(T: int, n: int):
84
+ r"""
85
+ Calculate commutation matrix of size T x n.
86
+
87
+ Parameters
88
+ ----------
89
+ T : int
90
+ Number of rows.
91
+
92
+ n : int
93
+ Number of columns.
94
+
95
+ Returns
96
+ -------
97
+ K: np.ndarray
98
+ Duplication summation matrix.
99
+ """
100
+ return cpp_commutation_matrix(T, n)
101
+
102
+
103
+ def coskewness_matrix(Y: np.ndarray):
104
+ r"""
105
+ Calculates coskewness rectangular matrix as shown in :cite:`d-Cajas4`.
106
+
107
+ Parameters
108
+ ----------
109
+ Y : ndarray or dataframe
110
+ Returns series of shape n_sample x n_features.
111
+
112
+ Returns
113
+ -------
114
+ M3 : ndarray
115
+ The lower semi coskewness rectangular matrix.
116
+
117
+ Raises
118
+ ------
119
+ ValueError when the value cannot be calculated.
120
+
121
+ """
122
+ flag = False
123
+ if isinstance(Y, pd.DataFrame):
124
+ assets = Y.columns.tolist()
125
+ cols = list(product(assets, assets))
126
+ cols = [str(y) + " - " + str(x) for x, y in cols]
127
+ flag = True
128
+
129
+ Y_ = np.array(Y, ndmin=2)
130
+ M3 = cpp_coskewness_matrix(Y_, semi=False)
131
+
132
+ if flag:
133
+ M3 = pd.DataFrame(M3, index=assets, columns=cols)
134
+
135
+ return M3
136
+
137
+
138
+ def semi_coskewness_matrix(Y: np.ndarray):
139
+ r"""
140
+ Calculates lower semi coskewness rectangular matrix as shown in :cite:`d-Cajas4`.
141
+
142
+ Parameters
143
+ ----------
144
+ Y : ndarray or dataframe
145
+ Returns series of shape n_samples x n_features.
146
+
147
+ Returns
148
+ -------
149
+ s_M3 : ndarray
150
+ The lower semi coskewness rectangular matrix.
151
+
152
+ Raises
153
+ ------
154
+ ValueError when the value cannot be calculated.
155
+
156
+ """
157
+ flag = False
158
+ if isinstance(Y, pd.DataFrame):
159
+ assets = Y.columns.tolist()
160
+ cols = list(product(assets, assets))
161
+ cols = [str(y) + " - " + str(x) for x, y in cols]
162
+ flag = True
163
+
164
+ Y_ = np.array(Y, ndmin=2)
165
+ s_M3 = cpp_coskewness_matrix(Y_, semi=True)
166
+
167
+ if flag:
168
+ s_M3 = pd.DataFrame(s_M3, index=assets, columns=cols)
169
+
170
+ return s_M3
171
+
172
+
173
+ def cokurtosis_matrix(Y: np.ndarray):
174
+ r"""
175
+ Calculates cokurtosis square matrix as shown in :cite:`d-Cajas4`.
176
+
177
+ Parameters
178
+ ----------
179
+ Y : ndarray or dataframe
180
+ Returns series of shape n_samples x n_features.
181
+
182
+ Returns
183
+ -------
184
+ S4 : ndarray
185
+ The cokurtosis square matrix.
186
+
187
+ Raises
188
+ ------
189
+ ValueError when the value cannot be calculated.
190
+
191
+ """
192
+ flag = False
193
+ if isinstance(Y, pd.DataFrame):
194
+ assets = Y.columns.tolist()
195
+ cols = list(product(assets, assets))
196
+ cols = [str(y) + " - " + str(x) for x, y in cols]
197
+ flag = True
198
+
199
+ Y_ = np.array(Y, ndmin=2)
200
+ S4 = cpp_cokurtosis_matrix(Y_, semi=False)
201
+
202
+ if flag:
203
+ S4 = pd.DataFrame(S4, index=cols, columns=cols)
204
+
205
+ return S4
206
+
207
+
208
+ def semi_cokurtosis_matrix(Y: np.ndarray):
209
+ r"""
210
+ Calculates lower semi cokurtosis square matrix as shown in :cite:`d-Cajas4`.
211
+
212
+ Parameters
213
+ ----------
214
+ Y : ndarray or dataframe
215
+ Returns series of shape n_sample x n_features.
216
+
217
+ Returns
218
+ -------
219
+ s_S4 : ndarray
220
+ The lower semi cokurtosis square matrix.
221
+
222
+ Raises
223
+ ------
224
+ ValueError when the value cannot be calculated.
225
+
226
+ """
227
+ flag = False
228
+ if isinstance(Y, pd.DataFrame):
229
+ assets = Y.columns.tolist()
230
+ cols = list(product(assets, assets))
231
+ cols = [str(y) + " - " + str(x) for x, y in cols]
232
+ flag = True
233
+
234
+ Y_ = np.array(Y, ndmin=2)
235
+ s_S4 = cpp_cokurtosis_matrix(Y_, semi=True)
236
+
237
+ if flag:
238
+ s_S4 = pd.DataFrame(s_S4, index=cols, columns=cols)
239
+
240
+ return s_S4
241
+
242
+
243
+ def k_eigh(Y: np.ndarray, k: int):
244
+ r"""
245
+ Calculates lower semi cokurtosis square matrix as shown in :cite:`d-Cajas4`.
246
+
247
+ Parameters
248
+ ----------
249
+ Y : ndarray or dataframe
250
+ Returns series of shape n_sample x n_features.
251
+
252
+ Returns
253
+ -------
254
+ s_S4 : ndarray
255
+ The lower semi cokurtosis square matrix.
256
+
257
+ Raises
258
+ ------
259
+ ValueError when the value cannot be calculated.
260
+ """
261
+ Y_ = np.array(Y, ndmin=2)
262
+ eigvalues, eigvectors = cpp_k_eigh(Y_, k)
263
+
264
+ return eigvalues, eigvectors
265
+
266
+
267
+ def d_corr(X: np.ndarray, Y: np.ndarray):
268
+ r"""
269
+ Calculates the distance correlation of X and Y.
270
+
271
+ Parameters
272
+ ----------
273
+ X : ndarray or dataframe
274
+ Returns series of shape n_sample x n_features.
275
+ Y : ndarray or dataframe
276
+ Returns series of shape n_sample x n_features.
277
+
278
+ Returns
279
+ -------
280
+ value : float
281
+ Distance correlation.
282
+
283
+ Raises
284
+ ------
285
+ ValueError when the value cannot be calculated.
286
+ """
287
+ X_ = np.array(X, ndmin=2)
288
+ Y_ = np.array(Y, ndmin=2)
289
+ value = cpp_dcorr(X_, Y_)
290
+
291
+ return value
292
+
293
+
294
+ def d_corr_matrix(Y: np.ndarray):
295
+ r"""
296
+ Calculates the distance correlation matrix of matrix of variables Y.
297
+
298
+ Parameters
299
+ ----------
300
+ Y : ndarray or dataframe
301
+ Returns series of shape n_sample x n_features.
302
+
303
+ Returns
304
+ -------
305
+ value : float
306
+ Distance correlation.
307
+
308
+ Raises
309
+ ------
310
+ ValueError when the value cannot be calculated.
311
+
312
+ """
313
+ Y_ = np.array(Y, ndmin=2)
314
+ value = cpp_dcorr_matrix(Y_)
315
+
316
+ return value
317
+
318
+
319
+ def residuals_coskewness_fm(residuals: np.ndarray):
320
+ r"""
321
+ Calculates the coskewness tensor of residuals of a risk factors model.
322
+
323
+ Parameters
324
+ ----------
325
+ residuals : ndarray or dataframe
326
+ Ndarray or DataFrame of residuals of the risk factors model of shape n_samples x 1.
327
+
328
+ Returns
329
+ -------
330
+ value : float
331
+ Coskewness tensor of residuals of a risk factors model.
332
+
333
+ Raises
334
+ ------
335
+ ValueError when the value cannot be calculated.
336
+
337
+ """
338
+ residuals_ = np.array(residuals, ndmin=2)
339
+
340
+ value = cpp_residuals_coskewness_fm(residuals_)
341
+
342
+ return value
343
+
344
+
345
+ def residuals_cokurtosis_fm(B: np.ndarray, S_f: np.ndarray, residuals: np.ndarray):
346
+ r"""
347
+ Calculates the cokurtosis square matrix of residuals of a risk factors model.
348
+
349
+ Parameters
350
+ ----------
351
+ B : ndarray or dataframe
352
+ The loadings matrix.
353
+
354
+ S_f : ndarray or dataframe
355
+ Covariance matrix of the risk factors.
356
+
357
+ residuals : ndarray or dataframe
358
+ Ndarray or DataFrame of residuals of the risk factors model of shape n_samples x 1.
359
+
360
+ Returns
361
+ -------
362
+ value : float
363
+ Cokurtosis square matrix of residuals of a risk factors model.
364
+
365
+ Raises
366
+ ------
367
+ ValueError when the value cannot be calculated.
368
+
369
+ """
370
+ B_ = np.array(B, ndmin=2)
371
+ S_f_ = np.array(S_f, ndmin=2)
372
+ residuals_ = np.array(residuals, ndmin=2)
373
+
374
+ value = cpp_residuals_cokurtosis_fm(B_, S_f_, residuals_)
375
+
376
+ return value