-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
210 lines (153 loc) · 4.99 KB
/
utils.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
import numpy as np
import pandas as pd
from numba import njit
__all__ = (
'get_exp_weights', 'RLS', 'weighted_mean', 'weighted_var',
'weighted_zscore', 'weighted_cov', 'weighted_corr',
)
EPS = 1e-16
def get_exp_weights(tau, N):
"""get exponentially decay weights
Args:
tau (int): decay halflife
N (int): sequence length
Returns:
numpy.ndarray: decay weights (sum to 1)
"""
lambd = 0.5**(1. / tau)
w = lambd**np.arange(N, 0, -1)
w /= w.sum()
return w
def RLS(y, X, R=None, r=None, w=None):
"""Constrained (Weighted) Least Square
Args:
y (numpy.ndarray): target values, [#obs]
X (numpy.ndarray): variable matrix, [#obs, #var]
R (numpy.ndarray, optional): constraints, [#var, #cons] or [#var]
r (numpy.ndarray, optional): target value for constraints, [#cons]
w (numpy.ndarray, optional): sample weights, [#obs]
Returns:
b (numpy.ndarray): regression coefficients
resid (numpy.ndarray): regression residual
tvalues (numpy.ndarray): T-stats for b
r2 (float): R-Squared
Reference:
- [William, 1991] (https://www.jstor.org/stable/2109587)
"""
# weights
if w is not None:
X = X * np.atleast_2d(np.sqrt(w)).T
y = y * np.sqrt(w)
# solve
if R is not None:
R = np.atleast_2d(R)
if r is None:
r = np.zeros(R.shape[0])
z = np.zeros((len(R), len(R)))
W = np.block([[X.T @ X, R.T], [R, z]])
p = np.r_[X.T @ y, r]
W_inv = np.linalg.pinv(W)
m = X.shape[1]
b = W_inv[:m] @ p
X_inv = W_inv[:m, :m] @ X.T
else:
W_inv = np.linalg.pinv(X.T @ X)
X_inv = W_inv @ X.T
b = X_inv @ y
# calc t-value
resid = y - X @ b
ss = (resid**2).sum() / (len(resid) - len(b))
b_var = ss * X_inv @ X_inv.T
tvalues = b / np.sqrt(np.diag(b_var))
# calc r2
r2 = 1 - (resid**2).sum() / (y**2).sum()
# restore resid
if w is not None:
resid /= np.sqrt(w)
return b, resid, tvalues, r2
def weighted_mean(X, w):
"""weighted mean
Args:
X (numpy.ndarray): variable matrix, [#obs, #var]
w (numpy.ndarray): sample weights, [#obs]
Returns:
numpy.ndarray: weighted mean, [#var]
"""
if X.shape != w.shape:
w = w.reshape(len(X), -1)
X = X * w
mask = ~np.isnan(X)
return np.nansum(X, axis=0) / \
(np.nansum(mask * w, axis=0) + EPS)
def weighted_var(X, w, lags=0):
"""weighted variance
Args:
X (numpy.ndarray): variable matrix, [#obs, #var]
w (numpy.ndarray): sample weights, [#obs]
lags (int): delay lags for Newey-West correction
Returns:
numpy.ndarray: weighted variance, [#var]
"""
mean = weighted_mean(X, w)
X = X - mean # demean
return weighted_mean(X[:len(X)-lags] * X[lags:], w[lags:])
def weighted_zscore(X, w):
"""weighted zscore
This method use weighted mean and *normal std* for zscore.
Args:
X (numpy.ndarray): variable matrix, [#obs, #var]
w (numpy.ndarray): sample weights, [#obs]
Returns:
numpy.ndarray: zscored variable matrix, [#obs, #var]
"""
mean = weighted_mean(X, w)
std = np.sqrt(weighted_var(X, w))
return (X - mean) / (std + EPS)
@njit
def _weighted_nancov(X, w, lags=0):
"""weighted covariance with missing value and delay lags
Args:
X (numpy.ndarray): variable matrix as `numpy.cov`, [#var, #obs]
w (numpy.ndarray): sample weights, [#obs]
Returns:
numpy.ndarray: weighted covariance
"""
n, m = X.shape
cov = np.zeros((n, n))
for i in range(n):
for j in range(n):
if lags == 0 and j < i:
cov[i, j] = cov[j, i]
else:
vals = X[i][:m-lags] * X[j][lags:]
mask = ~np.isnan(vals)
w_mask = w[lags:][mask]
cov[i, j] = np.sum(vals[mask] * w_mask) / (w_mask.sum() + EPS)
return cov
def weighted_cov(X, w, lags=0):
"""weighted covariance
cov(x, y, w) = sum_i (w_i * x_i * y_i) / sum_i w_i
where x_i, y_i are assumed to be centered.
Args:
X (numpy.ndarray): variable matrix, [#obs, #var]
w (numpy.ndarray): sample weights, [#obs]
lags (int): delay lags for Newey-West correction
Returns:
numpy.ndarray: weighted covariance, [#var]
"""
mean = weighted_mean(X, w)
X = X - mean # demean
return _weighted_nancov(X.T, w, lags)
def weighted_corr(X, w, lags=0):
"""weighted pearson correlation
corr(x, y, w) = cov(x, y, w) / \sqrt(cov(x, x, w) * cov(y, y, w))
Args:
X (numpy.ndarray): variable matrix, [#obs, #var]
w (numpy.ndarray): sample weights, [#obs]
lags (int): delay lags for Newey-West correction
Returns:
numpy.ndarray: weighted correlation, [#var]
"""
cov = weighted_cov(X, w, lags)
sigma = np.sqrt(np.diag(cov))
return cov / np.outer(sigma, sigma)