/
zscore.py
319 lines (253 loc) · 10 KB
/
zscore.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import numpy as np
import pandas as pd
import xarray as xr
from sklearn.utils.validation import check_is_fitted
from .base import AbstractDownscaler
class ZScoreRegressor(AbstractDownscaler):
""" Z Score Regressor bias correction model wrapper
Apply a scikit-learn model (e.g. Pipeline) point-by-point. The pipeline
must implement the fit and predict methods.
Parameters
----------
window_width : int
The size of the moving window for statistical analysis. Default is 31
days.
"""
_fit_attributes = ["shift_", "scale_"]
def __init__(self, window_width=31):
assert window_width > 0, window_width
self.window_width = window_width
def fit(self, X, y):
""" Fit Z-Score Model finds the shift and scale parameters
to inform bias correction.
Parameters
----------
X : pd.Series or pd.DataFrame, shape (n_samples, 1)
Training historical model data.
y : pd.Series or pd.DataFrame, shape (n_samples, 1)
Target measured values.
Returns
-------
self : returns an instance of self.
"""
assert isinstance(X.squeeze(), pd.Series)
assert isinstance(y.squeeze(), pd.Series)
X_mean, X_std = _calc_stats(X.squeeze(), self.window_width)
y_mean, y_std = _calc_stats(y.squeeze(), self.window_width)
self.fit_stats_dict_ = {
"X_mean": X_mean,
"X_std": X_std,
"y_mean": y_mean,
"y_std": y_std,
}
shift, scale = _get_params(X_mean, X_std, y_mean, y_std)
self.shift_ = shift
self.scale_ = scale
return self
def predict(self, X):
""" Predict performs the z-score bias correction
on the future model dataset, X.
Parameters
----------
X : pd.Series or pd.DataFrame, shape (n_samples, 1)
Training future model data.
Returns
-------
fut_corrected : pd.DataFrame, shape (n_samples, 1)
Returns corrected values.
"""
check_is_fitted(self, self._fit_attributes)
assert isinstance(X, pd.DataFrame)
assert X.shape[1] == 1
name = list(X.keys())[0]
fut_mean, fut_std, fut_zscore = _get_fut_stats(X.squeeze(), self.window_width)
shift_expanded, scale_expanded = _expand_params(X.squeeze(), self.shift_, self.scale_)
fut_mean_corrected, fut_std_corrected = _correct_fut_stats(
fut_mean, fut_std, shift_expanded, scale_expanded
)
self.predict_stats_dict_ = {
"meani": fut_mean,
"stdi": fut_std,
"meanf": fut_mean_corrected,
"stdf": fut_std_corrected,
}
fut_corrected = (fut_zscore * fut_std_corrected) + fut_mean_corrected
return fut_corrected.to_frame(name)
def _reshape(da, window_width):
"""
Helper function for `fit` that splits the year and day
dimensions of the time-coordinate and bookends the years
e.g. (Dec15:31 + whole year + Jan1:15) if window_width is 31 days.
Parameters
----------
da : xr.DataArray, shape (n_samples, )
Samples
window_width : int
The size of the rolling window.
Returns
-------
ds_rsh : xr.Dataset, shape(day: 364 + n_bookend_days, year: n_years)
Reshaped xr.Dataset
"""
assert da.ndim == 1
if "time" not in da.coords and "index" in da.coords:
da = da.rename({"index": "time"})
assert "time" in da.coords
def split(g):
return g.rename({"time": "day"}).assign_coords(day=g.time.dt.dayofyear.values)
da_split = da.groupby("time.year").map(split)
early_jans = da_split.isel(day=slice(None, window_width // 2))
late_decs = da_split.isel(day=slice(-window_width // 2, None))
da_rsh = xr.concat([late_decs, da_split, early_jans], dim="day")
return da_rsh
def _calc_stats(series, window_width):
"""
Helper function for `fit` that calculates the rolling mean and
standard deviation for each day of the year across all years.
Parameters
----------
series : pd.Series, shape (n_samples, )
Samples.
window_width : int
The size of the rolling window.
Returns
-------
mean : pd.Series, shape (364, )
Means for each day of year across all years
std: pd.Series, shape (364, )
Standard deviations for each day of year across all years
"""
da = series.to_xarray()
da_rsh = _reshape(da, window_width)
ds_rolled = da_rsh.rolling(day=window_width, center=True).construct("win_day")
n = window_width // 2 + 1
ds_mean = ds_rolled.mean(dim=["year", "win_day"]).isel(day=slice(n, -n))
ds_std = ds_rolled.std(dim=["year", "win_day"]).isel(day=slice(n, -n))
mean = ds_mean.to_series()
std = ds_std.to_series()
return mean, std
def _get_params(hist_mean, hist_std, meas_mean, meas_std):
"""
Helper function for `fit` that calculates the shift and scale parameters
for z-score correction by comparing the historical and measured
daily means and standard deviations.
Parameters
----------
hist_mean : pd.Series, shape (364, )
Mean calculated using the moving window for each day on an average
year from the historical model.
hist_std : pd.Series, shape (364, )
Standard deviation calculated using the moving window for each day on
an average year from the historical model.
meas_mean : pd.Series, shape (364, )
Mean calculated using the moving window for each day on an average year
from the measurements.
meas_std : pd.Series, shape (364, )
Standard deviation calculated using the moving window for each day on
an average year from the measurements.
Returns
-------
shift : pd.Series, shape (364, )
The value by which to adjust the future mean.
scale : pd.Series, shape (364, )
The value by which to adjust the future standard deviation.
"""
# TODO: Update docstring to relax the assumption that the year is 364 days long
# assert len(hist_mean) == 364, len(hist_mean)
# assert len(hist_std) == 364, len(hist_std)
# assert len(meas_mean) == 364, len(meas_mean)
# assert len(meas_std) == 364, len(meas_std)
assert all([s.ndim for s in [hist_mean, hist_std, meas_mean, meas_std]])
shift = meas_mean - hist_mean
scale = meas_std / hist_std
return shift, scale
# Helpers for Predict
def _get_fut_stats(series, window_width):
"""
Helper function for `predict` that calculates statistics
for the future dataset
Parameters
----------
series : pd.Series, shape (n_samples, )
Samples
window_width: int
The size of the rolling window.
Returns
-------
fut_mean : pd.Series, shape (n_samples, )
Mean calculated using the moving window for each day of the future
model.
fut_std : pd.Series, shape (n_samples, )
Standard deviation calculated using the moving window for each day of
the future model.
fut_zscore: pd.Series, shape (n_samples, )
Z-Score coefficient calculated by comparing the series values, the
means, and standared deviations.
"""
fut_mean = series.rolling(window_width, center=True).mean()
fut_std = series.rolling(window_width, center=True).std()
fut_zscore = (series - fut_mean) / fut_std
return fut_mean, fut_std, fut_zscore
def _expand_params(series, shift, scale):
"""
Helper function for `predict` that expands the shift and scale parameters
from a 365-day average year, to the length of the future series.
Parameters
----------
series : pd.Series, shape (n_samples, )
Samples.
shift : pd.Series, shape (364, )
The value by which to adjust the future mean.
scale : pd.Series, shape (364, )
The value by which to adjust the future standard deviation.
Returns
-------
shift_expanded : pd.Series, shape (n_samples, )
The value by which to adjust the future mean, repeated over the length
of the series.
scale_expanded : pd.Series, shape (n_samples, )
The value by which to adjust the future standard deviation, repeated
over the length of the series.
"""
n_samples = len(series)
len_avgyr = 364 if n_samples > 364 else n_samples
# TODO: update doc string
# assert len(shift) == len_avgyr, len(shift)
# assert len(scale) == len_avgyr, len(scale)
repeats = int(n_samples / len_avgyr)
remainder = n_samples % len_avgyr
inds = np.concatenate([np.tile(np.arange(len_avgyr), repeats), np.arange(remainder)])
assert len(inds) == n_samples, (len(inds), n_samples)
shift_expanded = shift.iloc[inds]
shift_expanded.index = series.index
scale_expanded = scale.iloc[inds]
scale_expanded.index = series.index
return shift_expanded, scale_expanded
def _correct_fut_stats(fut_mean, fut_std, shift_expanded, scale_expanded):
"""
Helper function for `predict` that adjusts future statistics by shift and
scale parameters.
Parameters
----------
fut_mean : pd.Series, shape (n_samples, )
Mean calculated using the moving window for each day of the future
model.
fut_std : pd.Series, shape (n_samples, )
Standard deviation calculated using the moving window for each day
of the future model.
shift_expanded : pd.Series, shape (n_samples, )
The value by which to adjust the future mean, repeated over the
length of the Series.
scale_expanded : pd.Series, shape (n_samples, )
The value by which to adjust the future standard deviation, repeated over the
length of the Series.
Returns
-------
fut_mean_corrected : pd.Series, shape (n_samples, )
Corrected mean for each day of the future model.
fut_std_corrected : pd.Series, shape (n_samples, )
Corrected standard deviation for each day of the future model.
"""
fut_mean_corrected = fut_mean + shift_expanded
fut_std_corrected = fut_std * scale_expanded
return fut_mean_corrected, fut_std_corrected