forked from dcajasn/Riskfolio-Lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwaWeights.py
257 lines (199 loc) · 6.04 KB
/
OwaWeights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""""" #
"""
Copyright (c) 2020-2022, Dany Cajas
All rights reserved.
This work is licensed under BSD 3-Clause "New" or "Revised" License.
License available at https://github.com/dcajasn/Riskfolio-Lib/blob/master/LICENSE.txt
"""
import numpy as np
def owa_gmd(T):
r"""
Calculate the OWA weights to calculate the Gini mean difference (GMD)
of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
Returns
-------
value : 1d-array
An OWA weights vector of size Tx1.
"""
w_ = []
for i in range(1, T + 1):
w_.append(2 * i - 1 - T)
w_ = 2 * np.array(w_) / (T * (T - 1))
w_ = w_.reshape(-1, 1)
return w_
def owa_cvar(T, alpha=0.05):
r"""
Calculate the OWA weights to calculate the Conditional Value at Risk (CVaR)
of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
alpha : float, optional
Significance level of CVaR. The default is 0.05.
Returns
-------
value : 1d-array
An OWA weights vector of size Tx1.
"""
k = int(np.ceil(T * alpha)) - 1
w_ = np.zeros((T, 1))
w_[:k, :] = -1 / (T * alpha)
w_[k, :] = -1 - np.sum(w_[:k, :])
return w_
def owa_wcvar(T, alphas, weights):
r"""
Calculate the OWA weights to calculate the Weighted Conditional Value at
Risk (WCVaR) of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
alphas : list
List of significance levels of each CVaR model.
weights : list
List of weights of each CVaR model.
Returns
-------
value : 1d-array
An OWA weights vector of size Tx1.
"""
w_ = 0
for i, j in zip(alphas, weights):
w_ += owa_cvar(T, i) * j
return w_
def owa_tg(T, alpha=0.05, a_sim=100):
r"""
Calculate the OWA weights to calculate the Tail Gini of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
alpha : float, optional
Significance level of TaiL Gini. The default is 0.05.
a_sim : float, optional
Number of CVaRs used to approximate the Tail Gini. The default is 100.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
alphas = np.linspace(alpha, 0.0001, a_sim)[::-1]
w_ = [(alphas[1] - 0) * alphas[0] / alphas[-1] ** 2]
for i in range(1, len(alphas) - 1):
w_.append((alphas[i + 1] - alphas[i - 1]) * alphas[i] / alphas[-1] ** 2)
w_.append((alphas[-1] - alphas[-2]) / alphas[-1])
w_ = owa_wcvar(T, alphas, w_)
return w_
def owa_wr(T):
r"""
Calculate the OWA weights to calculate the Worst realization (minimum) of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
w_ = np.zeros((T, 1))
w_[0, :] = -1
return w_
def owa_rg(T):
r"""
Calculate the OWA weights to calculate the range of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
w_ = np.zeros((T, 1))
w_[0, :] = -1
w_[-1, :] = 1
return w_
def owa_cvrg(T, alpha=0.05, beta=None):
r"""
Calculate the OWA weights to calculate the CVaR range of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
alpha : float, optional
Significance level of CVaR of losses. The default is 0.05.
beta : float, optional
Significance level of CVaR of gains. If None it duplicates alpha.
The default is None.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
if beta is None:
beta = alpha
w_ = owa_cvar(T, alpha) - owa_cvar(T, beta)[::-1]
return w_
def owa_wcvrg(T, alphas, weights_a, betas=None, weights_b=None):
r"""
Calculate the OWA weights to calculate the WCVaR range of a returns series.
Parameters
----------
T : int
Number of observations of the returns series.
alphas : list
List of significance levels of each CVaR of losses model.
weights_a : list
List of weights of each CVaR of losses model.
betas : list, optional
List of significance levels of each CVaR of gains model. If None it duplicates alpha.
The default is None.
weights_b : list, optional
List of weights of each CVaR of gains model. If None it duplicates weights_a.
The default is None.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
if betas is None or weights_b is None:
betas = alphas
weights_b = weights_a
w_ = owa_wcvar(T, alphas, weights_a) - owa_wcvar(T, betas, weights_b)[::-1]
return w_
def owa_tgrg(T, alpha=0.05, a_sim=100, beta=None, b_sim=None):
r"""
Calculate the OWA weights to calculate the Tail Gini range of a returns
series.
Parameters
----------
T : int
Number of observations of the returns series.
alpha : float, optional
Significance level of Tail Gini of losses. The default is 0.05.
a_sim : float, optional
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
beta : float, optional
Significance level of Tail Gini of gains. If None it duplicates alpha value.
The default is None.
b_sim : float, optional
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value.
The default is None.
Returns
-------
value : 1d-array
A OWA weights vector of size Tx1.
"""
if beta is None:
beta = alpha
if b_sim is None:
b_sim = a_sim
w_ = owa_tg(T, alpha, a_sim) - owa_tg(T, beta, b_sim)[::-1]
return w_