-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
251 lines (204 loc) · 8.87 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
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
"""
Credit: `ICascha/QuantGANs-replication` and Greg Ver Steeg
"""
from typing import Text, List, Union
import numpy as np
import numpy as np
from scipy import special
from scipy.stats import kurtosis, norm, rankdata, boxcox
from scipy import optimize # TODO: Explore efficacy of other opt. methods
import sklearn
from sklearn.preprocessing import StandardScaler
from matplotlib import pylab as plt
from scipy import stats
import warnings
import os
plt.rcParams.update({'font.size': 20})
np.seterr(all='warn')
_EPS = 1e-6
def rolling_window(x, k, sparse=True):
"""compute rolling windows from timeseries
Args:
x ([2d array]): x contains the time series in the shape (timestep, sample).
k ([int]): window length.
sparse (bool): Cut off the final windows containing NA. Defaults to True.
Returns:
[3d array]: array of rolling windows in the shape (window, timestep, sample).
"""
out = np.full([k, *x.shape], np.nan)
N = len(x)
for i in range(k):
out[i, :N-i] = x[i:]
if not sparse:
return out
return out[:, :-(k-1)]
def acf(x, k, le=False):
arr = rolling_window(x, k, sparse=False)
a = (arr[0] - np.nanmean(arr[0], axis=0))
if le:
arr **=2
b = (arr - np.nanmean(arr, axis=1, keepdims=True))
return np.nansum((a * b), axis=1) / np.sqrt(np.nansum(a**2, axis=0) * np.nansum(b**2, axis=1))
def cross_acf(x, y, k, le=False):
arr = rolling_window(y, k, sparse=False)
a = (x - x.mean(axis=0))
if le:
arr **=2
b = (arr - np.nanmean(arr, axis=1, keepdims=True))
return np.nansum((a * b), axis=1) / np.sqrt(np.nansum(a**2, axis=0) * np.nansum(b**2, axis=1))
def _update_x(x: Union[np.ndarray, List]) -> np.ndarray:
x = np.asarray(x)
if len(x.shape) == 1:
x = x[:, np.newaxis]
elif len(x.shape) != 2:
raise ValueError("Data should be a 1-d list of samples to transform or a 2d array with samples as rows.")
return x
class Gaussianize(sklearn.base.TransformerMixin):
"""
Gaussianize data using various methods.
Conventions
----------
This class is a wrapper that follows sklearn naming/style (e.g. fit(X) to train).
In this code, x is the input, y is the output. But in the functions outside the class, I follow
Georg's convention that Y is the input and X is the output (Gaussianized) data.
Parameters
----------
strategy : str, default='lambert'. Possibilities are 'lambert'[1], 'brute'[2] and 'boxcox'[3].
tol : float, default = 1e-4
max_iter : int, default = 100
Maximum number of iterations to search for correct parameters of Lambert transform.
Attributes
----------
coefs_ : list of tuples
For each variable, we have transformation parameters.
For Lambert, e.g., a tuple consisting of (mu, sigma, delta), corresponding to the parameters of the
appropriate Lambert transform. Eq. 6 and 8 in the paper below.
References
----------
[1] Georg M Goerg. The Lambert Way to Gaussianize heavy tailed data with
the inverse of Tukey's h transformation as a special case
Author generously provides code in R: https://cran.r-project.org/web/packages/LambertW/
[2] Valero Laparra, Gustavo Camps-Valls, and Jesus Malo. Iterative Gaussianization: From ICA to Random Rotations
[3] Box cox transformation and references: https://en.wikipedia.org/wiki/Power_transform
"""
def __init__(self, strategy: Text = 'lambert',
tol: float = 1e-5,
max_iter: int = 100,
verbose: bool = False):
self.tol = tol
self.max_iter = max_iter
self.strategy = strategy
self.coefs_ = [] # Store tau for each transformed variable
self.verbose = verbose
def fit(self, x: np.ndarray, y=None):
"""Fit a Gaussianizing transformation to each variable/column in x."""
# Initialize coefficients again with an empty list. Otherwise
# calling .fit() repeatedly will augment previous .coefs_ list.
self.coefs_ = []
x = _update_x(x)
if self.verbose:
print("Gaussianizing with strategy='%s'" % self.strategy)
if self.strategy == "lambert":
_get_coef = lambda vec: igmm(vec, self.tol, max_iter=self.max_iter)
elif self.strategy == "brute":
_get_coef = lambda vec: None # TODO: In principle, we could store parameters to do a quasi-invert
elif self.strategy == "boxcox":
_get_coef = lambda vec: boxcox(vec)[1]
else:
raise NotImplementedError("stategy='%s' not implemented." % self.strategy)
for x_i in x.T:
self.coefs_.append(_get_coef(x_i))
return self
def transform(self, x: np.ndarray) -> np.ndarray:
"""Transform new data using a previously learned Gaussianization model."""
x = _update_x(x)
if x.shape[1] != len(self.coefs_):
raise ValueError("%d variables in test data, but %d variables were in training data." % (x.shape[1], len(self.coefs_)))
if self.strategy == 'lambert':
return np.array([w_t(x_i, tau_i) for x_i, tau_i in zip(x.T, self.coefs_)]).T
elif self.strategy == 'brute':
return np.array([norm.ppf((rankdata(x_i) - 0.5) / len(x_i)) for x_i in x.T]).T
elif self.strategy == 'boxcox':
return np.array([boxcox(x_i, lmbda=lmbda_i) for x_i, lmbda_i in zip(x.T, self.coefs_)]).T
else:
raise NotImplementedError("stategy='%s' not implemented." % self.strategy)
def inverse_transform(self, y: np.ndarray) -> np.ndarray:
"""Recover original data from Gaussianized data."""
if self.strategy == 'lambert':
return np.array([inverse(y_i, tau_i) for y_i, tau_i in zip(y.T, self.coefs_)]).T
elif self.strategy == 'boxcox':
return np.array([(1. + lmbda_i * y_i) ** (1./lmbda_i) for y_i, lmbda_i in zip(y.T, self.coefs_)]).T
else:
raise NotImplementedError("Inversion not supported for gaussianization transform '%s'" % self.strategy)
def qqplot(self, x: np.ndarray, prefix: Text = 'qq', output_dir: Text = "/tmp/"):
"""Show qq plots compared to normal before and after the transform."""
x = _update_x(x)
y = self.transform(x)
n_dim = y.shape[1]
for i in range(n_dim):
stats.probplot(x[:, i], dist="norm", plot=plt)
plt.savefig(os.path.join(output_dir, prefix + '_%d_before.png' % i))
plt.clf()
stats.probplot(y[:, i], dist="norm", plot=plt)
plt.savefig(os.path.join(output_dir, prefix + '_%d_after.png' % i))
plt.clf()
def w_d(z, delta):
# Eq. 9
if delta < _EPS:
return z
return np.sign(z) * np.sqrt(np.real(special.lambertw(delta * z ** 2)) / delta)
def w_t(y, tau):
# Eq. 8
return tau[0] + tau[1] * w_d((y - tau[0]) / tau[1], tau[2])
def inverse(x, tau):
# Eq. 6
u = (x - tau[0]) / tau[1]
return tau[0] + tau[1] * (u * np.exp(u * u * (tau[2] * 0.5)))
def igmm(y: np.ndarray, tol: float = 1e-6, max_iter: int = 100):
# Infer mu, sigma, delta using IGMM in Alg.2, Appendix C
if np.std(y) < _EPS:
return np.mean(y), np.std(y).clip(_EPS), 0
delta0 = delta_init(y)
tau1 = (np.median(y), np.std(y) * (1. - 2. * delta0) ** 0.75, delta0)
for k in range(max_iter):
tau0 = tau1
z = (y - tau1[0]) / tau1[1]
delta1 = delta_gmm(z)
x = tau0[0] + tau1[1] * w_d(z, delta1)
mu1, sigma1 = np.mean(x), np.std(x)
tau1 = (mu1, sigma1, delta1)
if np.linalg.norm(np.array(tau1) - np.array(tau0)) < tol:
break
else:
if k == max_iter - 1:
warnings.warn("Warning: No convergence after %d iterations. Increase max_iter." % max_iter)
return tau1
def delta_gmm(z):
# Alg. 1, Appendix C
delta0 = delta_init(z)
def func(q):
u = w_d(z, np.exp(q))
if not np.all(np.isfinite(u)):
return 0.
else:
k = kurtosis(u, fisher=True, bias=False)**2
if not np.isfinite(k) or k > 1e10:
return 1e10
else:
return k
res = optimize.fmin(func, np.log(delta0), disp=0)
return np.around(np.exp(res[-1]), 6)
def delta_init(z):
gamma = kurtosis(z, fisher=False, bias=False)
with np.errstate(all='ignore'):
delta0 = np.clip(1. / 66 * (np.sqrt(66 * gamma - 162.) - 6.), 0.01, 0.48)
if not np.isfinite(delta0):
delta0 = 0.01
return delta0
def plot_loss(history, fp):
_, ax = plt.subplots(figsize=(16, 9))
# ax2 = ax.twinx()
p1 = ax.plot(history['gen_loss'], label='Generator Loss', color='tab:blue')
p2 = ax.plot(history['disc_loss'], label='Discriminator Loss', color='tab:orange')
ax.legend(handles=p1+p2)
plt.savefig(fp, bbox_inches='tight')