riskfolio-lib 7.1.0__cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_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-2024, 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-2025, 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,300 @@
1
+ """""" #
2
+
3
+ """
4
+ Copyright (c) 2020-2025, 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
+
16
+ def duplication_matrix(n: int, diag: bool = True):
17
+ r"""
18
+ Calculate duplication matrix of size "n" as shown in :cite:`d-Magnus1980`.
19
+
20
+ Parameters
21
+ ----------
22
+ n : int
23
+ Number of assets.
24
+
25
+ Returns
26
+ -------
27
+ D: np.ndarray
28
+ Duplication matrix
29
+ """
30
+ return cpp_duplication_matrix(n, diag)
31
+
32
+
33
+ def duplication_elimination_matrix(n: int, diag: bool = True):
34
+ r"""
35
+ Calculate duplication elimination matrix of size "n" as shown in :cite:`d-Magnus1980`.
36
+
37
+ Parameters
38
+ ----------
39
+ n : int
40
+ Number of assets.
41
+
42
+ Returns
43
+ -------
44
+ L: np.ndarray
45
+ Duplication matrix
46
+ """
47
+ return cpp_duplication_elimination_matrix(n, diag)
48
+
49
+
50
+ def duplication_summation_matrix(n: int, diag: bool = True):
51
+ r"""
52
+ Calculate duplication summation matrix of size "n" as shown in :cite:`d-Cajas4`.
53
+
54
+ Parameters
55
+ ----------
56
+ n : int
57
+ Number of assets.
58
+
59
+ Returns
60
+ -------
61
+ S: np.ndarray
62
+ Duplication summation matrix.
63
+ """
64
+ return cpp_duplication_summation_matrix(n, diag)
65
+
66
+
67
+ def commutation_matrix(T: int, n: int):
68
+ r"""
69
+ Calculate commutation matrix of size T x n.
70
+
71
+ Parameters
72
+ ----------
73
+ T : int
74
+ Number of rows.
75
+
76
+ n : int
77
+ Number of columns.
78
+
79
+ Returns
80
+ -------
81
+ K: np.ndarray
82
+ Duplication summation matrix.
83
+ """
84
+ return cpp_commutation_matrix(T, n)
85
+
86
+
87
+ def coskewness_matrix(Y: np.ndarray):
88
+ r"""
89
+ Calculates coskewness rectangular matrix as shown in :cite:`d-Cajas4`.
90
+
91
+ Parameters
92
+ ----------
93
+ Y : ndarray or dataframe
94
+ Returns series of shape n_sample x n_features.
95
+
96
+ Returns
97
+ -------
98
+ M3 : ndarray
99
+ The lower semi coskewness rectangular matrix.
100
+
101
+ Raises
102
+ ------
103
+ ValueError when the value cannot be calculated.
104
+
105
+ """
106
+ flag = False
107
+ if isinstance(Y, pd.DataFrame):
108
+ assets = Y.columns.tolist()
109
+ cols = list(product(assets, assets))
110
+ cols = [str(y) + " - " + str(x) for x, y in cols]
111
+ flag = True
112
+
113
+ Y_ = np.array(Y, ndmin=2)
114
+ M3 = cpp_coskewness_matrix(Y_, semi=False)
115
+
116
+ if flag:
117
+ M3 = pd.DataFrame(M3, index=assets, columns=cols)
118
+
119
+ return M3
120
+
121
+
122
+ def semi_coskewness_matrix(Y: np.ndarray):
123
+ r"""
124
+ Calculates lower semi coskewness rectangular matrix as shown in :cite:`d-Cajas4`.
125
+
126
+ Parameters
127
+ ----------
128
+ Y : ndarray or dataframe
129
+ Returns series of shape n_samples x n_features.
130
+
131
+ Returns
132
+ -------
133
+ s_M3 : ndarray
134
+ The lower semi coskewness rectangular matrix.
135
+
136
+ Raises
137
+ ------
138
+ ValueError when the value cannot be calculated.
139
+
140
+ """
141
+ flag = False
142
+ if isinstance(Y, pd.DataFrame):
143
+ assets = Y.columns.tolist()
144
+ cols = list(product(assets, assets))
145
+ cols = [str(y) + " - " + str(x) for x, y in cols]
146
+ flag = True
147
+
148
+ Y_ = np.array(Y, ndmin=2)
149
+ s_M3 = cpp_coskewness_matrix(Y_, semi=True)
150
+
151
+ if flag:
152
+ s_M3 = pd.DataFrame(s_M3, index=assets, columns=cols)
153
+
154
+ return s_M3
155
+
156
+
157
+ def cokurtosis_matrix(Y: np.ndarray):
158
+ r"""
159
+ Calculates cokurtosis square matrix as shown in :cite:`d-Cajas4`.
160
+
161
+ Parameters
162
+ ----------
163
+ Y : ndarray or dataframe
164
+ Returns series of shape n_samples x n_features.
165
+
166
+ Returns
167
+ -------
168
+ S4 : ndarray
169
+ The cokurtosis square matrix.
170
+
171
+ Raises
172
+ ------
173
+ ValueError when the value cannot be calculated.
174
+
175
+ """
176
+ flag = False
177
+ if isinstance(Y, pd.DataFrame):
178
+ assets = Y.columns.tolist()
179
+ cols = list(product(assets, assets))
180
+ cols = [str(y) + " - " + str(x) for x, y in cols]
181
+ flag = True
182
+
183
+ Y_ = np.array(Y, ndmin=2)
184
+ S4 = cpp_cokurtosis_matrix(Y_, semi=False)
185
+
186
+ if flag:
187
+ S4 = pd.DataFrame(S4, index=cols, columns=cols)
188
+
189
+ return S4
190
+
191
+
192
+ def semi_cokurtosis_matrix(Y):
193
+ r"""
194
+ Calculates lower semi cokurtosis square matrix as shown in :cite:`d-Cajas4`.
195
+
196
+ Parameters
197
+ ----------
198
+ Y : ndarray or dataframe
199
+ Returns series of shape n_sample x n_features.
200
+
201
+ Returns
202
+ -------
203
+ s_S4 : ndarray
204
+ The lower semi cokurtosis square matrix.
205
+
206
+ Raises
207
+ ------
208
+ ValueError when the value cannot be calculated.
209
+
210
+ """
211
+ flag = False
212
+ if isinstance(Y, pd.DataFrame):
213
+ assets = Y.columns.tolist()
214
+ cols = list(product(assets, assets))
215
+ cols = [str(y) + " - " + str(x) for x, y in cols]
216
+ flag = True
217
+
218
+ Y_ = np.array(Y, ndmin=2)
219
+ s_S4 = cpp_cokurtosis_matrix(Y_, semi=True)
220
+
221
+ if flag:
222
+ s_S4 = pd.DataFrame(s_S4, index=cols, columns=cols)
223
+
224
+ return s_S4
225
+
226
+
227
+ def k_eigh(Y, k):
228
+ r"""
229
+ Calculates lower semi cokurtosis square matrix as shown in :cite:`d-Cajas4`.
230
+
231
+ Parameters
232
+ ----------
233
+ Y : ndarray or dataframe
234
+ Returns series of shape n_sample x n_features.
235
+
236
+ Returns
237
+ -------
238
+ s_S4 : ndarray
239
+ The lower semi cokurtosis square matrix.
240
+
241
+ Raises
242
+ ------
243
+ ValueError when the value cannot be calculated.
244
+ """
245
+ Y_ = np.array(Y, ndmin=2)
246
+ eigvalues, eigvectors = cpp_k_eigh(Y_, k)
247
+
248
+ return eigvalues, eigvectors
249
+
250
+
251
+ def d_corr(X, Y):
252
+ r"""
253
+ Calculates the distance correlation of X and Y.
254
+
255
+ Parameters
256
+ ----------
257
+ X : ndarray or dataframe
258
+ Returns series of shape n_sample x n_features.
259
+ Y : ndarray or dataframe
260
+ Returns series of shape n_sample x n_features.
261
+
262
+ Returns
263
+ -------
264
+ value : float
265
+ Distance correlation.
266
+
267
+ Raises
268
+ ------
269
+ ValueError when the value cannot be calculated.
270
+ """
271
+ X_ = np.array(X, ndmin=2)
272
+ Y_ = np.array(Y, ndmin=2)
273
+ value = cpp_dcorr(X_, Y_)
274
+
275
+ return value
276
+
277
+
278
+ def d_corr_matrix(Y):
279
+ r"""
280
+ Calculates the distance correlation matrix of matrix of variables Y.
281
+
282
+ Parameters
283
+ ----------
284
+ Y : ndarray or dataframe
285
+ Returns series of shape n_sample x n_features.
286
+
287
+ Returns
288
+ -------
289
+ value : float
290
+ Distance correlation.
291
+
292
+ Raises
293
+ ------
294
+ ValueError when the value cannot be calculated.
295
+
296
+ """
297
+ Y_ = np.array(Y, ndmin=2)
298
+ value = cpp_dcorr_matrix(Y_)
299
+
300
+ return value