simons-forecasting 0.1.3__py3-none-any.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.
- simons/__init__.py +10 -0
- simons/config.py +82 -0
- simons/covariates.py +338 -0
- simons/forecast.py +578 -0
- simons/model.py +696 -0
- simons/periodicity.py +188 -0
- simons/predictor.py +56 -0
- simons/synth/__init__.py +5 -0
- simons/synth/augment.py +143 -0
- simons/synth/base.py +448 -0
- simons/synth/calendar.py +157 -0
- simons/synth/causal.py +193 -0
- simons/synth/covariates/__init__.py +1 -0
- simons/synth/covariates/classical.py +637 -0
- simons/synth/covariates/effects.py +155 -0
- simons/synth/covariates/energy.py +528 -0
- simons/synth/covariates/families.py +296 -0
- simons/synth/covariates/generic.py +342 -0
- simons/synth/heavy.py +234 -0
- simons/synth/mech.py +636 -0
- simons/synth/multi.py +274 -0
- simons/synth/nonstat.py +204 -0
- simons/synth/observe.py +117 -0
- simons/training/__init__.py +1 -0
- simons/training/__main__.py +77 -0
- simons/training/augment.py +125 -0
- simons/training/clusters.py +215 -0
- simons/training/config.py +321 -0
- simons/training/covariates/__init__.py +1 -0
- simons/training/covariates/roles.py +370 -0
- simons/training/covariates/stream.py +243 -0
- simons/training/covariates/windows.py +239 -0
- simons/training/data.py +319 -0
- simons/training/distributed.py +124 -0
- simons/training/mixture.py +140 -0
- simons/training/network.py +115 -0
- simons/training/objective.py +193 -0
- simons/training/optim.py +164 -0
- simons/training/packing.py +94 -0
- simons/training/schedule.py +117 -0
- simons/training/soup.py +93 -0
- simons/training/stitching.py +50 -0
- simons/training/trainer.py +571 -0
- simons/training/warp.py +144 -0
- simons_forecasting-0.1.3.dist-info/METADATA +156 -0
- simons_forecasting-0.1.3.dist-info/RECORD +49 -0
- simons_forecasting-0.1.3.dist-info/WHEEL +4 -0
- simons_forecasting-0.1.3.dist-info/licenses/LICENSE +202 -0
- simons_forecasting-0.1.3.dist-info/licenses/NOTICE +57 -0
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
"""Sample classical covariate effects on corpus or synthetic base channels."""
|
|
2
|
+
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from scipy.signal import lfilter
|
|
7
|
+
|
|
8
|
+
from simons.synth.covariates import effects as D
|
|
9
|
+
from simons.synth.covariates.effects import log_uniform as _lu
|
|
10
|
+
|
|
11
|
+
WEIGHTS = {"F0": 0.04, "F1": 0.14, "F2": 0.16, "F3": 0.10, "F4": 0.18, "F5": 0.08, "F6": 0.12,
|
|
12
|
+
"F7": 0.10, "F8": 0.08}
|
|
13
|
+
DEFAULTS = {"positive": 0.5, "count": 0.15, "null": 0.25, "weak_base": 0.25, "past_only": 0.0,
|
|
14
|
+
"gaps": 0.1, "units": 0.3, "decoys": 3, "fc_addon": 0.35}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _z(x):
|
|
19
|
+
m, s = np.nanmean(x), np.nanstd(x)
|
|
20
|
+
return (x - m) / (s if np.isfinite(s) and s > 1e-9 else 1.0)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _unit(g):
|
|
24
|
+
s = np.nanstd(g)
|
|
25
|
+
return g / s if np.isfinite(s) and s > 1e-9 else g * 0.0
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _ar1(L, rng, phi, sd=1.0, heavy=False):
|
|
29
|
+
e = rng.standard_t(3, L) / np.sqrt(3) if heavy else rng.normal(size=L)
|
|
30
|
+
x = lfilter([1.0], [1.0, -phi], e)
|
|
31
|
+
return x * sd * np.sqrt(max(1 - phi * phi, 1e-6))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _lowpass(L, rng, period):
|
|
35
|
+
"""Filter white noise through a two-pole low-pass with the given cutoff period."""
|
|
36
|
+
a = np.exp(-2 * np.pi / max(period, 2.0))
|
|
37
|
+
return _unit(lfilter([1.0], [1.0, -2 * a, a * a], rng.normal(size=L)))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _delay(x, d):
|
|
41
|
+
if d <= 0:
|
|
42
|
+
return x
|
|
43
|
+
d = min(d, len(x) - 1)
|
|
44
|
+
return np.concatenate([np.full(d, x[0]), x[:-d]])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _filt(b, a, x):
|
|
48
|
+
"""Start lfilter at steady state for x[0] to avoid a start-up transient."""
|
|
49
|
+
gain = np.sum(b) / np.sum(a)
|
|
50
|
+
return lfilter(b, a, x - x[0]) + x[0] * gain
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _holds(L, rng, mean_hold):
|
|
54
|
+
"""Return segment IDs [L] for piecewise-constant signals with geometric hold times."""
|
|
55
|
+
n = int(L / max(mean_hold, 1.0) * 3) + 4
|
|
56
|
+
ends = np.cumsum(rng.geometric(1.0 / max(mean_hold, 1.0), size=n))
|
|
57
|
+
return np.minimum(np.searchsorted(ends, np.arange(L), side="right"), n - 1)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Cal:
|
|
61
|
+
"""Daily (pd), weekly (pw) and annual (py) periods in steps; p1 is the shortest cycle.
|
|
62
|
+
pd/pw are None for daily/weekly or coarser data."""
|
|
63
|
+
|
|
64
|
+
PD: ClassVar[dict[str, int | None]] = {"H": 24, "30T": 48, "15T": 96, "10T": 144, "5T": 288,
|
|
65
|
+
"D": None, "W": None, "M": None}
|
|
66
|
+
|
|
67
|
+
def __init__(self, freq, L, rng):
|
|
68
|
+
self.pd = self.PD[freq]
|
|
69
|
+
if self.pd:
|
|
70
|
+
self.pw, self.py = 7 * self.pd, 365 * self.pd
|
|
71
|
+
elif freq == "D":
|
|
72
|
+
self.pw, self.py = 7, 365
|
|
73
|
+
elif freq == "W":
|
|
74
|
+
self.pw, self.py = None, 52
|
|
75
|
+
else:
|
|
76
|
+
self.pw, self.py = None, 12
|
|
77
|
+
self.p1 = self.pd or self.pw or self.py
|
|
78
|
+
self.t = np.arange(L, dtype="float64")
|
|
79
|
+
self.t0 = int(rng.integers(0, 10 ** 6))
|
|
80
|
+
self.phase = ((self.t + self.t0) % self.p1) / self.p1
|
|
81
|
+
if self.pw:
|
|
82
|
+
day = ((self.t + self.t0) // (self.pd or 1)) % 7
|
|
83
|
+
self.weekend = (day >= 5).astype(float)
|
|
84
|
+
else:
|
|
85
|
+
self.weekend = None
|
|
86
|
+
|
|
87
|
+
def profile(self, rng, harmonics=3):
|
|
88
|
+
"""Draw a unit-std smooth periodic profile over the shortest calendar cycle."""
|
|
89
|
+
g = np.zeros_like(self.t)
|
|
90
|
+
for k in range(1, harmonics + 1):
|
|
91
|
+
g += rng.normal() / k * np.sin(2 * np.pi * k * self.phase + rng.uniform(0, 6.3))
|
|
92
|
+
return _unit(g)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _issued(truth, hist, rng, rel_sd=None, lead=None):
|
|
96
|
+
"""Issue forecasts with autocorrelated error, optionally switching vintages at the origin."""
|
|
97
|
+
L = len(truth)
|
|
98
|
+
H = L - hist
|
|
99
|
+
sd = np.nanstd(truth)
|
|
100
|
+
sd = sd if np.isfinite(sd) and sd > 0 else 1.0
|
|
101
|
+
rel = _lu(rng, 0.03, 1.0) if rel_sd is None else rel_sd
|
|
102
|
+
phi = rng.uniform(0.0, 0.97)
|
|
103
|
+
heavy = rng.random() < 0.3
|
|
104
|
+
e = _ar1(L, rng, phi, rel * sd, heavy)
|
|
105
|
+
if lead is None:
|
|
106
|
+
lead = rng.random() < 0.5
|
|
107
|
+
if lead and H > 0:
|
|
108
|
+
fresh = _ar1(H, rng, phi, rel * sd, heavy)
|
|
109
|
+
k = rng.uniform(0.5, 4.0)
|
|
110
|
+
e[hist:] = fresh * np.sqrt(1 + k * np.arange(1, H + 1) / H)
|
|
111
|
+
f = np.nan_to_num(truth, nan=np.nanmean(truth) if np.isfinite(truth).any() else 0.0) + e
|
|
112
|
+
if rng.random() < 0.3:
|
|
113
|
+
f = f * rng.uniform(0.8, 1.2) + rng.normal() * 0.3 * sd
|
|
114
|
+
return f
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _driver(L, rng, cal):
|
|
118
|
+
k = rng.random()
|
|
119
|
+
if k < 0.4:
|
|
120
|
+
x = _lowpass(L, rng, _lu(rng, 4, 800))
|
|
121
|
+
elif k < 0.6:
|
|
122
|
+
x = _z(D.gen_covariate("smooth", L, rng))
|
|
123
|
+
elif k < 0.8:
|
|
124
|
+
x = _z(_lowpass(L, rng, _lu(rng, 20, 2000)) + 0.7 * cal.profile(rng))
|
|
125
|
+
else:
|
|
126
|
+
x = _z(D.gen_covariate("step", L, rng))
|
|
127
|
+
return x
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def f0_legacy(L, hist, rng, cal, lev):
|
|
133
|
+
n = int(rng.integers(1, 7))
|
|
134
|
+
kinds = rng.choice(["event", "smooth", "step", "calendar", "forecast"], size=n,
|
|
135
|
+
p=[0.3, 0.25, 0.15, 0.13, 0.17])
|
|
136
|
+
g, covs, nf = np.zeros(L), [], 0
|
|
137
|
+
for kind in kinds:
|
|
138
|
+
if kind == "forecast":
|
|
139
|
+
nf += 1
|
|
140
|
+
continue
|
|
141
|
+
x = D.gen_covariate(kind, L, rng)
|
|
142
|
+
if rng.random() < 0.6:
|
|
143
|
+
g += D.effect(x, rng) * rng.choice([-1, 1]) * _lu(rng, 0.05, 2.5)
|
|
144
|
+
covs.append(x)
|
|
145
|
+
return {"g": g, "covs": covs, "fc_target": nf}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _input(L, rng, cal):
|
|
149
|
+
k = rng.choice(["setpoint", "prbs", "steps", "band", "schedule"])
|
|
150
|
+
if k == "setpoint":
|
|
151
|
+
seg = _holds(L, rng, _lu(rng, 20, 2000))
|
|
152
|
+
return rng.normal(size=seg.max() + 1)[seg]
|
|
153
|
+
if k == "prbs":
|
|
154
|
+
seg = _holds(L, rng, _lu(rng, 2, 200))
|
|
155
|
+
return (rng.random(seg.max() + 1) < 0.5).astype(float)[seg]
|
|
156
|
+
if k == "steps":
|
|
157
|
+
return D.gen_covariate("step", L, rng)
|
|
158
|
+
if k == "band":
|
|
159
|
+
return _lowpass(L, rng, _lu(rng, 4, 500))
|
|
160
|
+
slots = int(rng.choice([2, 3, 4, 6]))
|
|
161
|
+
lv = rng.normal(size=slots)
|
|
162
|
+
x = lv[np.minimum((cal.phase * slots).astype(int), slots - 1)]
|
|
163
|
+
if rng.random() < 0.5:
|
|
164
|
+
x = x + D.gen_covariate("step", L, rng) * 0.5
|
|
165
|
+
return x
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _tf_coeffs(rng):
|
|
169
|
+
if rng.random() < 0.55:
|
|
170
|
+
tau = _lu(rng, 1, 300)
|
|
171
|
+
a1 = np.exp(-1 / tau)
|
|
172
|
+
a = np.array([1.0, -a1])
|
|
173
|
+
b = np.array([1.0 - a1])
|
|
174
|
+
if rng.random() < 0.3:
|
|
175
|
+
z = rng.uniform(-0.5, 0.8)
|
|
176
|
+
b = (1.0 - a1) * np.array([1.0, -z]) / (1.0 - z)
|
|
177
|
+
elif rng.random() < 0.6:
|
|
178
|
+
P = _lu(rng, 8, 400)
|
|
179
|
+
r = np.exp(-1 / (P * rng.uniform(0.15, 2.0)))
|
|
180
|
+
a = np.array([1.0, -2 * r * np.cos(2 * np.pi / P), r * r])
|
|
181
|
+
b = np.array([a.sum()])
|
|
182
|
+
else:
|
|
183
|
+
r1, r2 = np.exp(-1 / _lu(rng, 1, 200)), np.exp(-1 / _lu(rng, 1, 200))
|
|
184
|
+
a = np.array([1.0, -(r1 + r2), r1 * r2])
|
|
185
|
+
b = np.array([a.sum()])
|
|
186
|
+
return b, a
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _tf(x, rng, dmax=64):
|
|
190
|
+
b, a = _tf_coeffs(rng)
|
|
191
|
+
d = round(np.exp(rng.uniform(0, np.log(dmax + 1)))) - 1
|
|
192
|
+
return _delay(_filt(b, a, x), d)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def f1_transfer(L, hist, rng, cal, lev):
|
|
196
|
+
covs, g = [], np.zeros(L)
|
|
197
|
+
for _ in range(int(rng.choice([1, 1, 2, 3]))):
|
|
198
|
+
x = _input(L, rng, cal)
|
|
199
|
+
covs.append(x)
|
|
200
|
+
if rng.random() < 0.3:
|
|
201
|
+
v = _z(_tf(x, rng, 16))
|
|
202
|
+
s = rng.uniform(0.5, 2.0)
|
|
203
|
+
w = s * np.tanh(v / s) + rng.uniform(-0.3, 0.3) * v * v
|
|
204
|
+
y = _tf(w, rng)
|
|
205
|
+
else:
|
|
206
|
+
y = _tf(x, rng)
|
|
207
|
+
g += _unit(y) * rng.choice([-1, 1]) * rng.uniform(0.3, 1.0)
|
|
208
|
+
if rng.random() < 0.3:
|
|
209
|
+
g += _ar1(L, rng, rng.uniform(0.9, 0.999), np.nanstd(g) * rng.uniform(0.1, 0.6))
|
|
210
|
+
return {"g": g, "covs": covs}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _events(L, hist, rng, cal):
|
|
214
|
+
H = L - hist
|
|
215
|
+
if cal.pw and rng.random() < 0.3:
|
|
216
|
+
day = ((cal.t + cal.t0) // (cal.pd or 1)).astype(int)
|
|
217
|
+
start_day = ((day % 7 == rng.integers(7))
|
|
218
|
+
& (rng.random(day.max() + 1)[day] < rng.uniform(0.3, 1)))
|
|
219
|
+
start = start_day & (np.r_[True, day[1:] != day[:-1]])
|
|
220
|
+
dur = cal.pd or 1
|
|
221
|
+
return start.astype(float), dur
|
|
222
|
+
rate = _lu(rng, 2, 60) / L
|
|
223
|
+
start = rng.random(L) < rate
|
|
224
|
+
if H > 4 and rng.random() < 0.7:
|
|
225
|
+
start[hist + rng.integers(0, H - 1)] = True
|
|
226
|
+
dur = int(max(1, round(_lu(rng, 1, 3 * (cal.pd or 1) * (2 if rng.random() < 0.5 else 1)))))
|
|
227
|
+
return start.astype(float), dur
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def f2_intervention(L, hist, rng, cal, lev):
|
|
231
|
+
start, dur = _events(L, hist, rng, cal)
|
|
232
|
+
ind = np.convolve(start, np.ones(dur), mode="full")[:L].clip(0, 1)
|
|
233
|
+
n = int(start.sum())
|
|
234
|
+
ids = np.cumsum(start).astype(int)
|
|
235
|
+
lift = np.exp(rng.normal(0, rng.uniform(0, 0.7), size=n + 1))
|
|
236
|
+
covs = []
|
|
237
|
+
depth = None
|
|
238
|
+
if rng.random() < 0.35:
|
|
239
|
+
depth = rng.uniform(0.1, 1.0, size=n + 1)
|
|
240
|
+
x = ind * depth[ids]
|
|
241
|
+
lift = lift * depth / depth.mean()
|
|
242
|
+
else:
|
|
243
|
+
x = ind
|
|
244
|
+
covs.append(x)
|
|
245
|
+
resp = rng.choice(["abrupt_temp", "pulse_decay", "step_perm", "gradual_perm"],
|
|
246
|
+
p=[0.45, 0.25, 0.15, 0.15])
|
|
247
|
+
if resp == "abrupt_temp":
|
|
248
|
+
g = ind * lift[ids]
|
|
249
|
+
elif resp == "pulse_decay":
|
|
250
|
+
dlt = rng.uniform(0.3, 0.97)
|
|
251
|
+
g = lfilter([1.0], [1.0, -dlt], start * lift[ids])
|
|
252
|
+
elif resp == "step_perm":
|
|
253
|
+
g = np.cumsum(start * rng.normal(0, 1, L)) if n else np.zeros(L)
|
|
254
|
+
covs[0] = np.cumsum(start)
|
|
255
|
+
else:
|
|
256
|
+
dlt = rng.uniform(0.8, 0.995)
|
|
257
|
+
stepx = np.minimum(np.cumsum(start), 1.0) if rng.random() < 0.5 else np.cumsum(start)
|
|
258
|
+
g = _filt([1 - dlt], [1.0, -dlt], stepx)
|
|
259
|
+
covs[0] = stepx
|
|
260
|
+
sign = 1.0 if rng.random() < 0.8 else -1.0
|
|
261
|
+
g = g * sign
|
|
262
|
+
if resp in ("abrupt_temp", "pulse_decay"):
|
|
263
|
+
if rng.random() < 0.4:
|
|
264
|
+
k = int(max(1, round(dur * rng.uniform(0.5, 3))))
|
|
265
|
+
end = np.r_[0.0, np.maximum(ind[:-1] - ind[1:], 0)]
|
|
266
|
+
dip = np.convolve(end, np.ones(k), mode="full")[:L] * (1 - ind)
|
|
267
|
+
g = g - sign * dip * rng.uniform(0.1, 0.5) * np.mean(lift)
|
|
268
|
+
if rng.random() < 0.3:
|
|
269
|
+
k = int(max(1, round(_lu(rng, 1, 2 * (cal.pd or 3)))))
|
|
270
|
+
lead = np.convolve(start[::-1], np.ones(k), mode="full")[:L][::-1]
|
|
271
|
+
lead = np.r_[lead[1:], 0.0] * (1 - ind)
|
|
272
|
+
g = g + sign * lead * rng.uniform(0.1, 0.6) * np.mean(lift)
|
|
273
|
+
zero = None
|
|
274
|
+
if rng.random() < 0.15 and resp == "abrupt_temp":
|
|
275
|
+
zero = ind > 0
|
|
276
|
+
g = g * 0
|
|
277
|
+
if rng.random() < 0.3:
|
|
278
|
+
s2, d2 = _events(L, hist, rng, cal)
|
|
279
|
+
ind2 = np.convolve(s2, np.ones(d2), mode="full")[:L].clip(0, 1)
|
|
280
|
+
covs.append(ind2)
|
|
281
|
+
g = g + _unit(ind2) * np.nanstd(g if np.nanstd(g) > 0 else ind2) * rng.normal(0, 0.8)
|
|
282
|
+
return {"g": g, "covs": covs, "zero": zero}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def f3_dynregr(L, hist, rng, cal, lev):
|
|
286
|
+
covs, g = [], np.zeros(L)
|
|
287
|
+
for _ in range(int(rng.integers(1, 4))):
|
|
288
|
+
x = _driver(L, rng, cal)
|
|
289
|
+
covs.append(x)
|
|
290
|
+
b0 = rng.normal()
|
|
291
|
+
if rng.random() < 0.5:
|
|
292
|
+
drift = rng.uniform(0.3, 2.0) * max(abs(b0), 0.3)
|
|
293
|
+
beta = b0 + np.cumsum(rng.normal(size=L)) * drift / np.sqrt(L)
|
|
294
|
+
else:
|
|
295
|
+
seg = _holds(L, rng, _lu(rng, 50, 5000))
|
|
296
|
+
levels = b0 + rng.normal(0, 1, size=seg.max() + 1)
|
|
297
|
+
beta = levels[seg % 2] if rng.random() < 0.5 else levels[seg]
|
|
298
|
+
g += beta * x
|
|
299
|
+
g = _unit(g)
|
|
300
|
+
if rng.random() < 0.7:
|
|
301
|
+
r1, r2 = rng.uniform(-0.5, 0.98), rng.uniform(-0.5, 0.9)
|
|
302
|
+
eta = lfilter([1.0, rng.uniform(-0.8, 0.8)], [1.0, -(r1 + r2), r1 * r2], rng.normal(size=L))
|
|
303
|
+
g = g + _unit(eta) * rng.uniform(0.1, 0.7)
|
|
304
|
+
return {"g": g, "covs": covs}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _temperature(L, rng, cal):
|
|
308
|
+
T = _ar1(L, rng, rng.uniform(0.95, 0.999) if cal.pd else rng.uniform(0.6, 0.95), 1.0)
|
|
309
|
+
if cal.pd:
|
|
310
|
+
T = T + rng.uniform(0.3, 1.5) * np.sin(2 * np.pi * cal.phase + rng.uniform(0, 6.3))
|
|
311
|
+
T = T + rng.uniform(0.5, 3.0) * np.sin(2 * np.pi * (cal.t + cal.t0) / cal.py
|
|
312
|
+
+ rng.uniform(0, 6.3))
|
|
313
|
+
return T
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def f4_weather(L, hist, rng, cal, lev):
|
|
317
|
+
kind = rng.choice(["load", "solar", "wind"], p=[0.55, 0.25, 0.2])
|
|
318
|
+
covs = []
|
|
319
|
+
if kind == "load":
|
|
320
|
+
T = _temperature(L, rng, cal)
|
|
321
|
+
Tf = _issued(T, hist, rng, rel_sd=_lu(rng, 0.03, 0.6), lead=rng.random() < 0.25)
|
|
322
|
+
a = rng.uniform(0.02, 0.6) if rng.random() < 0.7 else 1.0
|
|
323
|
+
Ts = _filt([a], [1.0, -(1 - a)], T)
|
|
324
|
+
mid = np.quantile(T, rng.uniform(0.3, 0.7))
|
|
325
|
+
band = rng.uniform(0.0, 1.0) * np.std(T)
|
|
326
|
+
hdd, cdd = np.maximum(0, mid - band - Ts), np.maximum(0, Ts - mid - band)
|
|
327
|
+
sh, sc = rng.uniform(0, 1), rng.uniform(0, 1)
|
|
328
|
+
if cal.pd and rng.random() < 0.7:
|
|
329
|
+
sh = sh * (1 + 0.6 * cal.profile(rng, 2))
|
|
330
|
+
sc = sc * (1 + 0.6 * cal.profile(rng, 2))
|
|
331
|
+
g = sh * hdd + sc * cdd + (rng.uniform(0, 0.2) * Ts ** 2 if rng.random() < 0.3 else 0)
|
|
332
|
+
covs.append(Tf)
|
|
333
|
+
if rng.random() < 0.3:
|
|
334
|
+
covs.append(Tf * rng.uniform(0.5, 1) + _ar1(L, rng, 0.9, 0.5))
|
|
335
|
+
if rng.random() < 0.3:
|
|
336
|
+
covs.append(cal.weekend if cal.weekend is not None else cal.phase)
|
|
337
|
+
return {"g": g, "covs": covs}
|
|
338
|
+
if kind == "solar":
|
|
339
|
+
lat = np.deg2rad(rng.uniform(-10, 60))
|
|
340
|
+
doy = 2 * np.pi * ((cal.t + cal.t0) % cal.py) / cal.py
|
|
341
|
+
decl = np.deg2rad(23.44) * np.sin(doy - 1.39)
|
|
342
|
+
if cal.pd:
|
|
343
|
+
ha = 2 * np.pi * (cal.phase - 0.5)
|
|
344
|
+
elev = np.sin(lat) * np.sin(decl) + np.cos(lat) * np.cos(decl) * np.cos(ha)
|
|
345
|
+
clear = np.maximum(0.0, elev) ** rng.uniform(1.0, 1.5)
|
|
346
|
+
else:
|
|
347
|
+
clear = np.clip(0.6 + 0.4 * np.sin(doy - 1.39) * np.sign(lat + 1e-3), 0.05, None)
|
|
348
|
+
cloud = 1 / (1 + np.exp(-(
|
|
349
|
+
_ar1(L, rng, rng.uniform(0.9, 0.995) if cal.pd else rng.uniform(0.3, 0.8), 1.5)
|
|
350
|
+
+ rng.normal())))
|
|
351
|
+
c, k = rng.uniform(0.5, 0.95), rng.uniform(1.0, 3.5)
|
|
352
|
+
out = clear * (1 - c * cloud ** k)
|
|
353
|
+
if rng.random() < 0.4:
|
|
354
|
+
out = np.minimum(out, np.quantile(out, rng.uniform(0.85, 0.98)))
|
|
355
|
+
cf = np.clip(_issued(cloud, hist, rng, rel_sd=_lu(rng, 0.05, 0.8),
|
|
356
|
+
lead=rng.random() < 0.25), 0, 1)
|
|
357
|
+
r = rng.random()
|
|
358
|
+
if r < 0.4:
|
|
359
|
+
covs.append(cf)
|
|
360
|
+
elif r < 0.8:
|
|
361
|
+
covs.append(clear * (1 - c * cf ** k))
|
|
362
|
+
else:
|
|
363
|
+
covs += [cf, clear]
|
|
364
|
+
return {"own": out / (np.mean(out) + 1e-9), "covs": covs, "noise_floor": True}
|
|
365
|
+
v = np.exp(_ar1(L, rng, rng.uniform(0.95, 0.998) if cal.pd else rng.uniform(0.3, 0.8),
|
|
366
|
+
rng.uniform(0.3, 0.6)))
|
|
367
|
+
v = v * rng.uniform(4, 10)
|
|
368
|
+
vci, vr, vco = rng.uniform(2.5, 4), rng.uniform(10, 14), rng.uniform(20, 30)
|
|
369
|
+
p = np.clip((v ** 3 - vci ** 3) / (vr ** 3 - vci ** 3), 0, 1)
|
|
370
|
+
p[v > vco] = 0.0
|
|
371
|
+
vf = np.maximum(0, _issued(v, hist, rng, rel_sd=_lu(rng, 0.05, 0.6), lead=rng.random() < 0.25))
|
|
372
|
+
covs.append(vf)
|
|
373
|
+
if rng.random() < 0.3:
|
|
374
|
+
covs.append(vf * rng.uniform(1.1, 1.4) + _ar1(L, rng, 0.9, 0.5))
|
|
375
|
+
return {"own": p / (np.mean(p) + 1e-9) + 1e-3, "covs": covs, "noise_floor": True}
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def f5_marketing(L, hist, rng, cal, lev):
|
|
379
|
+
kind = rng.choice(["bass", "adstock", "price"], p=[0.25, 0.4, 0.35])
|
|
380
|
+
covs = []
|
|
381
|
+
seg = _holds(L, rng, _lu(rng, 20, 1000))
|
|
382
|
+
price = np.exp(np.cumsum(rng.normal(0, 0.05, size=seg.max() + 1)))[seg]
|
|
383
|
+
if rng.random() < 0.6:
|
|
384
|
+
start, dur = _events(L, hist, rng, cal)
|
|
385
|
+
cut = np.convolve(start, np.ones(dur), mode="full")[:L].clip(0, 1)
|
|
386
|
+
price = price * (1 - cut * rng.uniform(0.1, 0.4))
|
|
387
|
+
flights = _holds(L, rng, _lu(rng, 10, 500))
|
|
388
|
+
on = rng.random(flights.max() + 1) < rng.uniform(0.2, 0.7)
|
|
389
|
+
spend = on[flights] * np.exp(rng.normal(0, 0.5, size=flights.max() + 1))[flights]
|
|
390
|
+
lam = rng.uniform(0.3, 0.97)
|
|
391
|
+
ad = lfilter([1 - lam], [1.0, -lam], spend)
|
|
392
|
+
kap, h = (rng.uniform(0.7, 3.0),
|
|
393
|
+
np.quantile(ad[ad > 0], rng.uniform(0.2, 0.8)) if (ad > 0).any() else 1.0)
|
|
394
|
+
hill = ad ** kap / (ad ** kap + h ** kap + 1e-12)
|
|
395
|
+
if kind == "bass":
|
|
396
|
+
q = rng.uniform(1.0, 6.0) / (_lu(rng, 0.3, 1.5) * L)
|
|
397
|
+
p = q * _lu(rng, 0.005, 0.3)
|
|
398
|
+
xm = (price / price[:hist].mean()) ** (-rng.uniform(0, 3)) * (1 + rng.uniform(0, 2) * hill)
|
|
399
|
+
effort = np.cumsum(xm)
|
|
400
|
+
e = np.exp(-(p + q) * effort)
|
|
401
|
+
F = (1 - e) / (1 + (q / p) * e)
|
|
402
|
+
f = np.r_[F[0], np.diff(F)]
|
|
403
|
+
covs += [price, spend] if rng.random() < 0.6 else [spend]
|
|
404
|
+
own = f / (np.mean(f) + 1e-12) + 0.02
|
|
405
|
+
return {"own": own, "covs": covs, "mult_level": True}
|
|
406
|
+
if kind == "adstock":
|
|
407
|
+
g = hill * rng.uniform(0.5, 1.5)
|
|
408
|
+
covs.append(spend)
|
|
409
|
+
if rng.random() < 0.4:
|
|
410
|
+
g = g - rng.uniform(0.5, 3) * np.log(price)
|
|
411
|
+
covs.append(price)
|
|
412
|
+
return {"g": g, "covs": covs, "log_effect": True}
|
|
413
|
+
g = -rng.uniform(0.5, 4.0) * np.log(price)
|
|
414
|
+
covs.append(price)
|
|
415
|
+
if rng.random() < 0.3:
|
|
416
|
+
seg2 = _holds(L, rng, _lu(rng, 20, 1000))
|
|
417
|
+
cp = np.exp(np.cumsum(rng.normal(0, 0.05, size=seg2.max() + 1)))[seg2]
|
|
418
|
+
g = g + rng.uniform(0.2, 1.5) * np.log(cp)
|
|
419
|
+
covs.append(cp)
|
|
420
|
+
return {"g": g, "covs": covs, "log_effect": True}
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def f6_capacity(L, hist, rng, cal, lev):
|
|
424
|
+
slots = int(rng.choice([3, 4, 6, 8]))
|
|
425
|
+
prof = np.exp(rng.normal(0, 0.5, size=slots))
|
|
426
|
+
s = prof[np.minimum((cal.phase * slots).astype(int), slots - 1)]
|
|
427
|
+
if cal.weekend is not None and rng.random() < 0.7:
|
|
428
|
+
s = s * (1 - rng.uniform(0.1, 0.6) * cal.weekend)
|
|
429
|
+
if rng.random() < 0.5:
|
|
430
|
+
seg = _holds(L, rng, _lu(rng, 200, 5000))
|
|
431
|
+
s = s * np.exp(rng.normal(0, 0.2, size=seg.max() + 1))[seg]
|
|
432
|
+
covs = []
|
|
433
|
+
disr = np.zeros(L)
|
|
434
|
+
if rng.random() < 0.6:
|
|
435
|
+
start, dur = _events(L, hist, rng, cal)
|
|
436
|
+
disr = np.convolve(start, np.ones(max(dur, 2)), mode="full")[:L].clip(0, 1)
|
|
437
|
+
cut = rng.uniform(0.0, 0.8)
|
|
438
|
+
service = s * (1 - cut * disr)
|
|
439
|
+
lev = np.nan_to_num(lev, nan=np.nanmean(lev))
|
|
440
|
+
rho = rng.uniform(0.2, 1.2)
|
|
441
|
+
demand = lev * (service / service.mean()) ** rho
|
|
442
|
+
cap = rng.uniform(0.6, 1.6) * service / service.mean() * demand.mean()
|
|
443
|
+
if rng.random() < 0.4:
|
|
444
|
+
S = np.cumsum(demand - cap)
|
|
445
|
+
B = S - np.minimum(0.0, np.minimum.accumulate(S))
|
|
446
|
+
served = demand + np.r_[0.0, B[:-1]] - B
|
|
447
|
+
else:
|
|
448
|
+
served = np.minimum(demand, cap)
|
|
449
|
+
covs.append(service if rng.random() < 0.7 else s)
|
|
450
|
+
if disr.any() and rng.random() < 0.7:
|
|
451
|
+
covs.append(disr)
|
|
452
|
+
if rng.random() < 0.3:
|
|
453
|
+
covs.append(cap)
|
|
454
|
+
served = np.maximum(served, 0)
|
|
455
|
+
return {"own": served / (np.mean(served) + 1e-12), "covs": covs}
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def f7_interaction(L, hist, rng, cal, lev):
|
|
459
|
+
kind = rng.choice(["lstr", "hill", "product", "daytype", "phase"])
|
|
460
|
+
x = _driver(L, rng, cal)
|
|
461
|
+
covs = [x]
|
|
462
|
+
if kind == "lstr":
|
|
463
|
+
if rng.random() < 0.5:
|
|
464
|
+
sv = x
|
|
465
|
+
else:
|
|
466
|
+
sv = _driver(L, rng, cal)
|
|
467
|
+
covs.append(sv)
|
|
468
|
+
G = 1 / (1 + np.exp(-_lu(rng, 1, 20) * (sv - rng.uniform(-1, 1))))
|
|
469
|
+
a1, a2 = rng.normal(), rng.normal()
|
|
470
|
+
g = a1 * x + (a2 - a1) * x * G
|
|
471
|
+
elif kind == "hill":
|
|
472
|
+
xp = x - x.min() + 1e-3
|
|
473
|
+
h, k = np.quantile(xp, rng.uniform(0.2, 0.8)), rng.uniform(1, 4)
|
|
474
|
+
g = xp ** k / (xp ** k + h ** k)
|
|
475
|
+
elif kind == "product":
|
|
476
|
+
x2 = _driver(L, rng, cal) if rng.random() < 0.6 else (D.gen_covariate("event", L, rng))
|
|
477
|
+
covs.append(x2)
|
|
478
|
+
g = _z(x) * _z(x2) + rng.normal(0, 0.5) * x + rng.normal(0, 0.5) * _z(x2)
|
|
479
|
+
elif kind == "daytype":
|
|
480
|
+
if cal.weekend is not None:
|
|
481
|
+
dt = cal.weekend
|
|
482
|
+
else:
|
|
483
|
+
dt = (cal.phase < rng.uniform(0.2, 0.5)).astype(float)
|
|
484
|
+
b1, b2 = rng.normal(), rng.normal()
|
|
485
|
+
g = x * (b1 + (b2 - b1) * dt)
|
|
486
|
+
if rng.random() < 0.6:
|
|
487
|
+
covs.append(dt)
|
|
488
|
+
else:
|
|
489
|
+
prof = cal.profile(rng, int(rng.integers(1, 4)))
|
|
490
|
+
g = x * (rng.normal() + rng.uniform(0.5, 1.5) * prof)
|
|
491
|
+
if rng.random() < 0.3:
|
|
492
|
+
covs.append(np.sin(2 * np.pi * cal.phase))
|
|
493
|
+
return {"g": g, "covs": covs}
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def f8_issued(L, hist, rng, cal, lev):
|
|
497
|
+
if rng.random() < 0.5:
|
|
498
|
+
g = np.zeros(L)
|
|
499
|
+
covs = []
|
|
500
|
+
if rng.random() < 0.5:
|
|
501
|
+
x = _driver(L, rng, cal)
|
|
502
|
+
covs.append(x)
|
|
503
|
+
g = x * rng.uniform(0.2, 1)
|
|
504
|
+
return {"g": g, "covs": covs, "fc_target": int(rng.choice([1, 1, 2]))}
|
|
505
|
+
u = _driver(L, rng, cal)
|
|
506
|
+
g = u if rng.random() < 0.6 else np.tanh(u * rng.uniform(0.5, 2)) + rng.uniform(0, 0.4) * u * u
|
|
507
|
+
covs = [_issued(u, hist, rng)]
|
|
508
|
+
if rng.random() < 0.3:
|
|
509
|
+
covs.append(_issued(u, hist, rng))
|
|
510
|
+
return {"g": g, "covs": covs}
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
FAMILIES = {"F0": f0_legacy, "F1": f1_transfer, "F2": f2_intervention, "F3": f3_dynregr,
|
|
514
|
+
"F4": f4_weather, "F5": f5_marketing, "F6": f6_capacity, "F7": f7_interaction,
|
|
515
|
+
"F8": f8_issued}
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
def _level(y0, z0, hist, rng):
|
|
520
|
+
h = y0[:hist]
|
|
521
|
+
fin = np.isfinite(h)
|
|
522
|
+
if fin.any() and np.nanmin(h) > 0 and rng.random() < 0.7:
|
|
523
|
+
lev = y0 / np.nanmean(h)
|
|
524
|
+
if np.nanstd(z0) < 0.5 * np.nanstd((y0 - np.nanmean(h)) / (np.nanstd(h) + 1e-9)):
|
|
525
|
+
lev = 1 + (lev - 1) * 0.3
|
|
526
|
+
elif rng.random() < 0.5:
|
|
527
|
+
lev = np.exp(np.clip(rng.uniform(0.05, 0.6) * z0, -12, 12))
|
|
528
|
+
else:
|
|
529
|
+
lev = z0 - np.nanmin(z0[:hist]) + rng.uniform(0.3, 4.0)
|
|
530
|
+
lev = np.maximum(lev, 0.01)
|
|
531
|
+
m = np.nanmean(lev[:hist]) if np.isfinite(lev[:hist]).any() else 1.0
|
|
532
|
+
return lev / (m if np.isfinite(m) and m > 0 else 1.0)
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def sample(base, rng, hist: int, horizon: int):
|
|
536
|
+
"""Return float32 (target [hist+horizon], covariates [C,hist+horizon]) using `rng`.
|
|
537
|
+
`base.uni` holds (values,freq) pairs; NaN marks target padding and unknown covariates."""
|
|
538
|
+
for _ in range(4):
|
|
539
|
+
try:
|
|
540
|
+
y, c = _sample(base, rng, hist, horizon)
|
|
541
|
+
except (ValueError, FloatingPointError, ZeroDivisionError, IndexError):
|
|
542
|
+
continue
|
|
543
|
+
if not np.isinf(y).any() and not np.isinf(c).any() and np.isfinite(y).any():
|
|
544
|
+
return y, c
|
|
545
|
+
return D.synthetic(base, rng, hist, horizon)
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def _sample(base, rng, hist, horizon):
|
|
549
|
+
names = list(WEIGHTS)
|
|
550
|
+
p = np.array(list(WEIGHTS.values()), float)
|
|
551
|
+
L = hist + horizon
|
|
552
|
+
y0, freq = D.base_window(base, rng, L)
|
|
553
|
+
nan0 = ~np.isfinite(y0)
|
|
554
|
+
mu, sd = np.nanmean(y0[:hist]), np.nanstd(y0[:hist])
|
|
555
|
+
if not np.isfinite(sd) or sd == 0:
|
|
556
|
+
sd = 1.0
|
|
557
|
+
if not np.isfinite(mu):
|
|
558
|
+
mu = 0.0
|
|
559
|
+
z0 = (y0 - mu) / sd
|
|
560
|
+
if rng.random() < DEFAULTS["weak_base"]:
|
|
561
|
+
z0 = z0 * rng.uniform(0, 0.3)
|
|
562
|
+
cal = Cal(freq, L, rng)
|
|
563
|
+
positive = rng.random() < DEFAULTS["positive"]
|
|
564
|
+
lev = _level(y0, z0, hist, rng)
|
|
565
|
+
fam = names[rng.choice(len(names), p=p / p.sum())]
|
|
566
|
+
out = FAMILIES[fam](L, hist, rng, cal, lev)
|
|
567
|
+
covs = [np.asarray(c, dtype="float64") for c in out["covs"]]
|
|
568
|
+
null = rng.random() < DEFAULTS["null"]
|
|
569
|
+
zfill = np.nan_to_num(z0, nan=0.0)
|
|
570
|
+
lfill = np.nan_to_num(lev, nan=1.0)
|
|
571
|
+
if "own" in out and not null:
|
|
572
|
+
own = np.asarray(out["own"], dtype="float64")
|
|
573
|
+
if out.get("mult_level") or rng.random() < 0.5:
|
|
574
|
+
own = own * lfill ** rng.uniform(0.0, 1.0)
|
|
575
|
+
elif out.get("noise_floor"):
|
|
576
|
+
own = own + rng.uniform(0, 0.3) * lfill * rng.random()
|
|
577
|
+
if positive:
|
|
578
|
+
y = own
|
|
579
|
+
else:
|
|
580
|
+
y = _z(own) * rng.uniform(0.5, 1.5) + zfill * rng.uniform(0, 0.5)
|
|
581
|
+
else:
|
|
582
|
+
g = np.zeros(L) if null else _unit(np.nan_to_num(np.asarray(out["g"], dtype="float64")))
|
|
583
|
+
if positive:
|
|
584
|
+
if out.get("log_effect") or rng.random() < 0.65:
|
|
585
|
+
s = _lu(rng, 0.05, 1.2)
|
|
586
|
+
y = lfill * np.exp(np.clip(s * g, -6, 4))
|
|
587
|
+
else:
|
|
588
|
+
y = np.maximum(0.0, lfill + _lu(rng, 0.1, 2.0) * g)
|
|
589
|
+
else:
|
|
590
|
+
y = zfill + _lu(rng, 0.1, 3.0) * g
|
|
591
|
+
if positive and out.get("zero") is not None and not null:
|
|
592
|
+
y = np.where(out["zero"], 0.0, y)
|
|
593
|
+
if positive:
|
|
594
|
+
if rng.random() < DEFAULTS["count"]:
|
|
595
|
+
m = _lu(rng, 0.5, 50.0)
|
|
596
|
+
lam = np.maximum(y, 0) * m / (np.mean(y) + 1e-12)
|
|
597
|
+
if rng.random() < 0.5:
|
|
598
|
+
k = _lu(rng, 1.0, 50.0)
|
|
599
|
+
lam = rng.gamma(k, 1.0 / k, size=L) * lam
|
|
600
|
+
lam = np.nan_to_num(lam, nan=0.0, posinf=1e7, neginf=0.0).clip(0.0, 1e7)
|
|
601
|
+
y = rng.poisson(lam).astype("float64")
|
|
602
|
+
else:
|
|
603
|
+
y = y * np.exp(rng.normal(size=L) * rng.uniform(0, 0.2))
|
|
604
|
+
else:
|
|
605
|
+
y = y + rng.normal(size=L) * rng.uniform(0, 0.3)
|
|
606
|
+
for _ in range(int(out.get("fc_target", 0))):
|
|
607
|
+
f = _issued(y, hist, rng)
|
|
608
|
+
covs.append(np.maximum(f, 0) if positive else f)
|
|
609
|
+
if rng.random() < DEFAULTS["fc_addon"]:
|
|
610
|
+
for _ in range(int(rng.choice([1, 1, 2]))):
|
|
611
|
+
f = _issued(y, hist, rng, lead=rng.random() < 0.25)
|
|
612
|
+
covs.append(np.maximum(f, 0) if positive else f)
|
|
613
|
+
for _ in range(int(rng.integers(0, DEFAULTS["decoys"] + 1))):
|
|
614
|
+
if rng.random() < 0.4:
|
|
615
|
+
x = D.covariate_window(base, rng, L)
|
|
616
|
+
else:
|
|
617
|
+
x = D.gen_covariate(rng.choice(["event", "smooth", "step", "calendar"]), L, rng)
|
|
618
|
+
covs.append(x)
|
|
619
|
+
if not covs:
|
|
620
|
+
covs.append(D.gen_covariate("calendar", L, rng))
|
|
621
|
+
covs = covs[: D.MAX_COV]
|
|
622
|
+
order = rng.permutation(len(covs))
|
|
623
|
+
covs = np.stack([covs[i] for i in order])
|
|
624
|
+
covs[~np.isfinite(covs)] = 0.0
|
|
625
|
+
for i in range(len(covs)):
|
|
626
|
+
if rng.random() < DEFAULTS["past_only"]: # Preserve the draw even at probability zero.
|
|
627
|
+
covs[i, hist:] = np.nan
|
|
628
|
+
if rng.random() < DEFAULTS["gaps"]:
|
|
629
|
+
covs[i, rng.random(L) < rng.uniform(0, 0.1)] = np.nan
|
|
630
|
+
if positive:
|
|
631
|
+
y = y * 10 ** rng.uniform(-1, 4)
|
|
632
|
+
else:
|
|
633
|
+
y = y * 10 ** rng.uniform(-2, 4) + rng.normal() * 10 ** rng.uniform(-1, 4)
|
|
634
|
+
y[nan0] = np.nan
|
|
635
|
+
if rng.random() < DEFAULTS["units"]:
|
|
636
|
+
covs = covs * 10 ** rng.uniform(-2, 3, size=(len(covs), 1))
|
|
637
|
+
return y.astype("float32"), covs.astype("float32")
|