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,296 @@
|
|
|
1
|
+
"""Mix generic, classical, energy, retail and lagged covariate-effect families."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import warnings
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
from simons.synth.covariates.effects import smooth
|
|
9
|
+
|
|
10
|
+
FAMILIES = ("generic", "classical", "energy", "retail", "lagged")
|
|
11
|
+
RETAIL_HORIZONS = (1, 1, 2, 2, 3, 4)
|
|
12
|
+
ENERGY_CFG = {"kinds": {"load": 0.35, "pv": 0.2, "wind": 0.1, "price": 0.15, "tariff": 0.1}}
|
|
13
|
+
PERIOD_FREQ = {24: "H", 168: "H", 48: "30T", 96: "15T", 144: "10T", 288: "5T", 7: "D",
|
|
14
|
+
30: "D", 365: "D", 52: "W", 12: "M"}
|
|
15
|
+
FREQ_FALLBACK = (("H", 0.35), ("D", 0.25), ("15T", 0.12), ("30T", 0.08), ("W", 0.12),
|
|
16
|
+
("M", 0.08))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def parse_mix(mix):
|
|
20
|
+
"""(names, probabilities) of an effect_mix dict {family: weight}."""
|
|
21
|
+
unknown = set(mix) - set(FAMILIES)
|
|
22
|
+
if unknown:
|
|
23
|
+
raise ValueError(f"effect_mix: unknown families {sorted(unknown)}")
|
|
24
|
+
names = tuple(k for k in FAMILIES if float(mix.get(k, 0)) > 0)
|
|
25
|
+
if not names:
|
|
26
|
+
raise ValueError("effect_mix: no family has a positive weight")
|
|
27
|
+
p = np.array([float(mix[k]) for k in names])
|
|
28
|
+
return names, p / p.sum()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def horizon(name, rng, horizons):
|
|
32
|
+
"""The family's horizon in patches."""
|
|
33
|
+
if name == "retail":
|
|
34
|
+
return int(rng.choice(RETAIL_HORIZONS))
|
|
35
|
+
return int(rng.choice(horizons))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def freq_label(b, rng, hist):
|
|
39
|
+
"""Infer a frequency from the base period within 10%, or draw from FREQ_FALLBACK."""
|
|
40
|
+
from simons.synth.covariates.generic import _period
|
|
41
|
+
|
|
42
|
+
p = _period(b, rng, hist)
|
|
43
|
+
best = min(PERIOD_FREQ, key=lambda q: abs(q - p))
|
|
44
|
+
if abs(best - p) <= 0.1 * best:
|
|
45
|
+
return PERIOD_FREQ[best]
|
|
46
|
+
names, w = zip(*FREQ_FALLBACK)
|
|
47
|
+
return names[int(rng.choice(len(names), p=np.array(w) / sum(w)))]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class SynthUni:
|
|
51
|
+
"""Expose synthetic windows as (values,freq) pairs in `.uni`."""
|
|
52
|
+
|
|
53
|
+
def __init__(self, windows):
|
|
54
|
+
self.uni = windows
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _classical(b, rng, hist, H, more):
|
|
58
|
+
from simons.synth.covariates import classical
|
|
59
|
+
|
|
60
|
+
L = hist + H
|
|
61
|
+
freq = freq_label(b, rng, hist)
|
|
62
|
+
pool = SynthUni([(b.astype("float32"), freq), (more(L).astype("float32"), freq)])
|
|
63
|
+
return classical.sample(pool, rng, hist, H)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _energy(b, rng, hist, H):
|
|
67
|
+
from simons.synth.covariates import energy
|
|
68
|
+
|
|
69
|
+
y, c = energy.sample(None, rng, hist, H, ENERGY_CFG)
|
|
70
|
+
nan0 = ~np.isfinite(b)
|
|
71
|
+
y = y.copy()
|
|
72
|
+
y[nan0] = np.nan
|
|
73
|
+
return y, c
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _runs(L, duty, mean_run, rng):
|
|
77
|
+
on = np.zeros(L, bool)
|
|
78
|
+
rid = np.full(L, -1)
|
|
79
|
+
mean_gap = max(1.0, mean_run * (1 - duty) / max(duty, 1e-3))
|
|
80
|
+
t, r = int(rng.integers(0, int(mean_gap) + 1)), 0
|
|
81
|
+
while t < L:
|
|
82
|
+
n = int(rng.geometric(1.0 / mean_run))
|
|
83
|
+
on[t:t + n] = True
|
|
84
|
+
rid[t:t + n] = r
|
|
85
|
+
r += 1
|
|
86
|
+
t += n + int(rng.geometric(1.0 / mean_gap))
|
|
87
|
+
return on, rid, r
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _retail_decoy(kind, n, rng):
|
|
91
|
+
t = np.arange(n)
|
|
92
|
+
if kind == "flag":
|
|
93
|
+
return (rng.random(n) < rng.uniform(0.02, 0.4)).astype(float)
|
|
94
|
+
if kind == "fraction":
|
|
95
|
+
x = rng.uniform(0.02, 0.15) + np.cumsum(rng.normal(0, 0.003, n))
|
|
96
|
+
hold = int(rng.integers(1, 8))
|
|
97
|
+
return np.clip(x[(t // hold) * hold], 0, 1)
|
|
98
|
+
if kind == "int":
|
|
99
|
+
hi = int(rng.integers(1, 6))
|
|
100
|
+
changes = rng.random(n) < rng.uniform(0.01, 0.2)
|
|
101
|
+
lv = rng.integers(0, hi + 1, changes.sum() + 1)
|
|
102
|
+
return lv[np.cumsum(changes)].astype(float)
|
|
103
|
+
p0 = np.exp(rng.uniform(np.log(0.5), np.log(20)))
|
|
104
|
+
on, _, _ = _runs(n, rng.uniform(0.05, 0.5), rng.uniform(1.3, 3), rng)
|
|
105
|
+
return np.round(p0 * (1 - on * rng.uniform(0.05, 0.4)), 2)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _retail(b, rng, hist, H):
|
|
109
|
+
"""Apply multiplicative SCAN*PRO-style lifts, capped at 4, to a smoothed observed baseline."""
|
|
110
|
+
L = hist + H
|
|
111
|
+
fin = np.isfinite(b)
|
|
112
|
+
first = int(np.argmax(fin)) if fin.any() else L
|
|
113
|
+
n = L - first
|
|
114
|
+
a = np.where(fin, b, 0.0)[first:].astype("float64")
|
|
115
|
+
a = smooth(a, int(rng.integers(4, 13)))
|
|
116
|
+
z = (a - a.mean()) / (a.std() + 1e-9)
|
|
117
|
+
t = np.arange(n)
|
|
118
|
+
daily = rng.random() < 0.5
|
|
119
|
+
loglam = np.clip(rng.uniform(0.05, 0.5) * z, -3, 3)
|
|
120
|
+
if daily:
|
|
121
|
+
dow = rng.normal(0, rng.uniform(0.05, 0.35), 7)
|
|
122
|
+
loglam = loglam + dow[(t + int(rng.integers(7))) % 7]
|
|
123
|
+
else:
|
|
124
|
+
loglam = loglam + rng.uniform(0, 0.4) * np.sin(2 * np.pi * t / 52 + rng.uniform(0, 6.3))
|
|
125
|
+
loglam = loglam + rng.uniform(np.log(0.3), np.log(3000))
|
|
126
|
+
eff = 0.0 if rng.random() < 0.25 else 1.0
|
|
127
|
+
duty, mean_run = rng.uniform(0.05, 0.6), rng.uniform(1.3, 3.0)
|
|
128
|
+
on, rid, nruns = _runs(n, duty, mean_run, rng)
|
|
129
|
+
reg = np.exp(rng.uniform(np.log(0.1), np.log(20)))
|
|
130
|
+
steps = rng.random(n) < 0.004
|
|
131
|
+
regular = reg * np.cumprod(np.where(steps, 1 + rng.choice([-1, 1], n)
|
|
132
|
+
* rng.uniform(0.03, 0.15, n), 1))
|
|
133
|
+
cut_run = rng.random(max(nruns, 1)) < rng.uniform(0.6, 1.0)
|
|
134
|
+
delta_run = rng.uniform(0.05, 0.5, max(nruns, 1)) * cut_run
|
|
135
|
+
delta = np.where(on, delta_run[np.maximum(rid, 0)], 0.0)
|
|
136
|
+
price = regular * (1 - delta)
|
|
137
|
+
if rng.random() < 0.5:
|
|
138
|
+
price = np.round(price, 2)
|
|
139
|
+
if rng.random() < 0.3:
|
|
140
|
+
price = price * np.exp(rng.normal(0, 0.01, n))
|
|
141
|
+
beta = 0.0 if rng.random() < 0.15 else -np.clip(np.exp(rng.normal(np.log(2.5), 0.6)), 0.2, 6)
|
|
142
|
+
run_lift = beta * np.log(np.maximum(1 - delta, 1e-3))
|
|
143
|
+
flags = []
|
|
144
|
+
for _ in range(int(rng.integers(1, 4))):
|
|
145
|
+
pick = rng.random(max(nruns, 1)) < rng.uniform(0.6, 0.9)
|
|
146
|
+
f = (on & pick[np.maximum(rid, 0)]) | (rng.random(n) < rng.uniform(0, 0.03))
|
|
147
|
+
run_lift = run_lift + rng.normal(0.3, 0.3) * f
|
|
148
|
+
flags.append(f.astype(float))
|
|
149
|
+
sigma_ev = rng.uniform(0, 0.6)
|
|
150
|
+
het = np.exp(rng.normal(0, sigma_ev, max(nruns, 1)))[np.maximum(rid, 0)]
|
|
151
|
+
het = np.where(rid >= 0, het, np.exp(rng.normal(0, sigma_ev, n)))
|
|
152
|
+
lift = run_lift * het
|
|
153
|
+
if rng.random() < 0.4:
|
|
154
|
+
d, m = rng.uniform(0.05, 0.3), int(rng.integers(1, 4))
|
|
155
|
+
ends = np.flatnonzero(on[:-1] & ~on[1:])
|
|
156
|
+
for j in range(1, m + 1):
|
|
157
|
+
idx = ends + j
|
|
158
|
+
idx = idx[idx < n]
|
|
159
|
+
idx = idx[~on[idx]]
|
|
160
|
+
src = idx - j
|
|
161
|
+
lift[idx] -= d * np.maximum(lift[src], 0) * (0.6 ** (j - 1))
|
|
162
|
+
covs = [price, *flags]
|
|
163
|
+
if rng.random() < 0.25:
|
|
164
|
+
hon, _, _ = _runs(n, rng.uniform(0.05, 0.4), rng.uniform(1.3, 3), rng)
|
|
165
|
+
lift = lift + rng.normal(0, 0.2) * hon
|
|
166
|
+
covs.append(hon.astype(float))
|
|
167
|
+
hol = np.zeros(n)
|
|
168
|
+
if rng.random() < 0.6:
|
|
169
|
+
per = 365 if daily else 52
|
|
170
|
+
days = rng.choice(per, int(rng.integers(1, 6 if daily else 4)), replace=False)
|
|
171
|
+
hol = np.isin((t + int(rng.integers(per))) % per, days).astype(float)
|
|
172
|
+
lift = lift + rng.normal(0, 0.5) * hol
|
|
173
|
+
covs.append(hol)
|
|
174
|
+
closed = np.zeros(n, bool)
|
|
175
|
+
if daily and rng.random() < 0.5:
|
|
176
|
+
closed = rng.random(n) < rng.uniform(0, 0.03)
|
|
177
|
+
if rng.random() < 0.5:
|
|
178
|
+
closed |= ((t + int(rng.integers(7))) % 7) == 0
|
|
179
|
+
if hol.any() and rng.random() < 0.5:
|
|
180
|
+
closed |= hol > 0
|
|
181
|
+
covs.append((~closed).astype(float) if rng.random() < 0.6 else closed.astype(float))
|
|
182
|
+
lift = np.clip(lift, -4.0, 4.0) * eff
|
|
183
|
+
mu = np.exp(loglam + lift)
|
|
184
|
+
mu = np.where(closed & (eff > 0), 0.0, mu)
|
|
185
|
+
if rng.random() < 0.7:
|
|
186
|
+
r = np.exp(rng.uniform(np.log(0.5), np.log(200)))
|
|
187
|
+
y = rng.poisson(np.minimum(rng.gamma(r, np.maximum(mu, 1e-12) / r), 1e8)).astype(float)
|
|
188
|
+
else:
|
|
189
|
+
y = mu * np.exp(rng.normal(0, rng.uniform(0.05, 0.5), n))
|
|
190
|
+
if rng.random() < 0.1:
|
|
191
|
+
yc = np.maximum(np.round(np.maximum(y, 0)), 0).astype(np.int64)
|
|
192
|
+
covs.append(rng.binomial(yc, rng.uniform(0.15, 0.7)).astype(float))
|
|
193
|
+
for _ in range(int(rng.integers(0, 4))):
|
|
194
|
+
covs.append(_retail_decoy(rng.choice(["flag", "fraction", "int", "price"]), n, rng))
|
|
195
|
+
covs = np.stack([covs[i] for i in rng.permutation(len(covs))])
|
|
196
|
+
if first:
|
|
197
|
+
y = np.concatenate([np.full(first, np.nan), y])
|
|
198
|
+
covs = np.concatenate([np.full((len(covs), first), np.nan), covs], 1)
|
|
199
|
+
return y.astype("float32"), covs.astype("float32")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _shift(x, lag):
|
|
203
|
+
"""Return x(t - lag) with held edges; positive lag delays, negative lag leads."""
|
|
204
|
+
if lag == 0:
|
|
205
|
+
return x
|
|
206
|
+
if lag > 0:
|
|
207
|
+
lag = min(lag, len(x) - 1)
|
|
208
|
+
return np.concatenate([np.full(lag, x[0]), x[:-lag]])
|
|
209
|
+
lag = min(-lag, len(x) - 1)
|
|
210
|
+
return np.concatenate([x[lag:], np.full(lag, x[-1])])
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _lagged_cov(rng, L, more):
|
|
214
|
+
kind = rng.choice(["base", "step", "bell", "event"], p=[0.35, 0.2, 0.2, 0.25])
|
|
215
|
+
t = np.arange(L, dtype="float64")
|
|
216
|
+
if kind == "base":
|
|
217
|
+
x = more(L)
|
|
218
|
+
fill = np.nanmean(x) if np.isfinite(x).any() else 0.0
|
|
219
|
+
return np.where(np.isfinite(x), x, fill)
|
|
220
|
+
if kind == "step":
|
|
221
|
+
changes = rng.random(L) < 10 ** rng.uniform(-3.3, -1.5)
|
|
222
|
+
return rng.normal(size=int(changes.sum()) + 1)[np.cumsum(changes)]
|
|
223
|
+
if kind == "bell":
|
|
224
|
+
n = max(1, rng.poisson(L * 10 ** rng.uniform(-3.3, -2)))
|
|
225
|
+
x = np.zeros(L)
|
|
226
|
+
for c, w, a in zip(rng.integers(0, L, n), np.exp(rng.uniform(np.log(2), np.log(64), n)),
|
|
227
|
+
rng.uniform(0.3, 1.5, n)):
|
|
228
|
+
lo, hi = max(0, int(c - 4 * w)), min(L, int(c + 4 * w) + 1)
|
|
229
|
+
x[lo:hi] += a * np.exp(-0.5 * ((t[lo:hi] - c) / w) ** 2)
|
|
230
|
+
return x
|
|
231
|
+
starts = rng.random(L) < 10 ** rng.uniform(-3, -1.3)
|
|
232
|
+
width = int(rng.integers(1, 12))
|
|
233
|
+
return np.convolve(starts.astype(float), np.ones(width), mode="full")[:L].clip(0, 1)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _lagged(b, rng, hist, H, kmax, more):
|
|
237
|
+
L = hist + H
|
|
238
|
+
k = int(min(kmax, 8, rng.geometric(0.25)))
|
|
239
|
+
nan0 = ~np.isfinite(b)
|
|
240
|
+
h = b[:hist][np.isfinite(b[:hist])]
|
|
241
|
+
mu, sd = (float(h.mean()), float(h.std())) if len(h) else (0.0, 1.0)
|
|
242
|
+
z = np.where(nan0, 0.0, (b - mu) / (sd if sd > 1e-9 else 1.0))
|
|
243
|
+
z = z * rng.uniform(0.0, 1.0)
|
|
244
|
+
impact = np.zeros(L)
|
|
245
|
+
covs = []
|
|
246
|
+
for _ in range(k):
|
|
247
|
+
x = _lagged_cov(rng, L, more)
|
|
248
|
+
covs.append(x)
|
|
249
|
+
if rng.random() < 0.25:
|
|
250
|
+
continue
|
|
251
|
+
xs = (x - x.mean()) / (x.std() + 1e-9)
|
|
252
|
+
lag = 0 if rng.random() < 0.3 else int(min(512, math.exp(rng.uniform(0, math.log(512)))))
|
|
253
|
+
g = _shift(xs, lag)
|
|
254
|
+
if rng.random() < 0.15:
|
|
255
|
+
g = np.where(g > np.quantile(g, rng.uniform(0.5, 0.95)), g, 0.0)
|
|
256
|
+
elif rng.random() < 0.2:
|
|
257
|
+
g = np.diff(g, prepend=g[0])
|
|
258
|
+
g = g / (g.std() + 1e-9)
|
|
259
|
+
elif rng.random() < 0.3:
|
|
260
|
+
g = np.tanh(g * rng.uniform(0.5, 2.0))
|
|
261
|
+
if rng.random() < 0.1:
|
|
262
|
+
g = g + rng.uniform(0.2, 0.8) * _shift(g, -int(rng.integers(1, 33)))
|
|
263
|
+
impact += rng.normal() * g
|
|
264
|
+
y = z + impact * rng.uniform(0.3, 2.0) + rng.normal(size=L) * rng.uniform(0.0, 0.3)
|
|
265
|
+
if rng.random() < 0.3:
|
|
266
|
+
y = np.exp(np.clip(0.3 * y, -6, 6))
|
|
267
|
+
y = y * 10 ** rng.uniform(-1, 3) + (rng.normal() * 10 ** rng.uniform(-1, 2)
|
|
268
|
+
if rng.random() < 0.5 else 0.0)
|
|
269
|
+
y[nan0] = np.nan
|
|
270
|
+
covs = np.stack(covs) * 10 ** rng.uniform(-1, 2, size=(len(covs), 1))
|
|
271
|
+
return y.astype("float32"), covs.astype("float32")
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def make_task(name, b, rng, hist, H, kmax, more):
|
|
275
|
+
"""(y [hist+H], covs [k <= kmax, hist+H]) of family `name` on synthetic base b."""
|
|
276
|
+
from simons.synth.covariates import generic
|
|
277
|
+
|
|
278
|
+
if name == "generic":
|
|
279
|
+
return generic.make_task(b, rng, hist, H, kmax)
|
|
280
|
+
with warnings.catch_warnings(), np.errstate(all="ignore"):
|
|
281
|
+
warnings.simplefilter("ignore", RuntimeWarning)
|
|
282
|
+
if name == "classical":
|
|
283
|
+
y, c = _classical(b, rng, hist, H, more)
|
|
284
|
+
elif name == "energy":
|
|
285
|
+
y, c = _energy(b, rng, hist, H)
|
|
286
|
+
elif name == "retail":
|
|
287
|
+
y, c = _retail(b, rng, hist, H)
|
|
288
|
+
elif name == "lagged":
|
|
289
|
+
y, c = _lagged(b, rng, hist, H, kmax, more)
|
|
290
|
+
else:
|
|
291
|
+
raise ValueError(name)
|
|
292
|
+
y = np.asarray(y, np.float32)
|
|
293
|
+
c = np.asarray(c, np.float32)[:kmax]
|
|
294
|
+
y[~np.isfinite(y)] = np.nan
|
|
295
|
+
c[~np.isfinite(c)] = np.nan
|
|
296
|
+
return y, c
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"""Generate covariates and their effects on a base window."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from scipy.signal import lfilter
|
|
5
|
+
|
|
6
|
+
from simons.synth.covariates.effects import log_uniform
|
|
7
|
+
|
|
8
|
+
KINDS = ("forecast", "weather", "promo", "closure", "price", "schedule", "setpoint", "decoy")
|
|
9
|
+
KIND_P = (0.15, 0.17, 0.18, 0.10, 0.08, 0.10, 0.10, 0.12)
|
|
10
|
+
NONE_RELEVANT = 0.2
|
|
11
|
+
INTERACT = 0.2
|
|
12
|
+
MAX_COV = 8
|
|
13
|
+
PERIODS = (7, 12, 24, 48, 96, 144, 168, 288, 52, 30)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _std(x):
|
|
17
|
+
s = float(np.std(x))
|
|
18
|
+
return s if s > 1e-9 else 1.0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _ou(rng, L, tau):
|
|
22
|
+
phi = np.exp(-1.0 / max(tau, 1.0))
|
|
23
|
+
x = lfilter([1.0], [1.0, -phi], rng.normal(size=L + 200))[200:]
|
|
24
|
+
return (x - x.mean()) / _std(x)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _smooth_noise(rng, L, lo, hi):
|
|
28
|
+
return _ou(rng, L, log_uniform(rng, lo, hi))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _period(base, rng, hist):
|
|
32
|
+
"""Use a valid history FFT peak with probability 0.7; otherwise draw from PERIODS."""
|
|
33
|
+
h = np.nan_to_num(base[:hist])
|
|
34
|
+
if hist >= 64 and rng.random() < 0.7:
|
|
35
|
+
spec = np.abs(np.fft.rfft(h - h.mean())) ** 2
|
|
36
|
+
spec[:2] = 0
|
|
37
|
+
k = int(np.argmax(spec))
|
|
38
|
+
if k > 0:
|
|
39
|
+
p = round(hist / k)
|
|
40
|
+
if 3 <= p <= hist // 3:
|
|
41
|
+
return p
|
|
42
|
+
return int(rng.choice(PERIODS))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _runs(rng, L, duty, mean_len):
|
|
46
|
+
"""Draw binary runs with on-share `duty` and geometric run lengths of mean `mean_len`."""
|
|
47
|
+
mean_len = max(1.0, mean_len)
|
|
48
|
+
n = max(1, rng.poisson(duty * L / mean_len))
|
|
49
|
+
x = np.zeros(L)
|
|
50
|
+
starts = rng.integers(0, L, size=n)
|
|
51
|
+
lengths = rng.geometric(1.0 / mean_len, size=n)
|
|
52
|
+
for s, m in zip(starts, lengths):
|
|
53
|
+
x[s:s + m] = 1.0
|
|
54
|
+
return x
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _edges(x):
|
|
58
|
+
d = np.diff(np.concatenate([[0.0], x]))
|
|
59
|
+
return (d > 0).astype(float), (d < 0).astype(float)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _decay(impulse, k):
|
|
63
|
+
return lfilter([1.0], [1.0, -k], impulse)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _phase_gate(rng, L, P):
|
|
67
|
+
t = np.arange(L)
|
|
68
|
+
q = rng.uniform(0.5, 3.0)
|
|
69
|
+
return (0.5 * (1 + np.cos(2 * np.pi * (t - rng.uniform(0, P)) / P))) ** q
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _forecast_driver(rng, L, P):
|
|
74
|
+
d = _smooth_noise(rng, L, 3, 150)
|
|
75
|
+
if rng.random() < 0.5:
|
|
76
|
+
d = d + rng.uniform(0.3, 1.5) * np.sin(2 * np.pi * np.arange(L) / P + rng.uniform(0, 6.3))
|
|
77
|
+
d = (d - d.mean()) / _std(d)
|
|
78
|
+
e = rng.choice([-1, 1]) * rng.uniform(0.15, 0.8) * d
|
|
79
|
+
return d, e
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _issue(rng, truth, hist):
|
|
83
|
+
"""Issue a forecast with small history error and horizon error growing with lead."""
|
|
84
|
+
L = len(truth)
|
|
85
|
+
sd = _std(truth)
|
|
86
|
+
lead = np.clip((np.arange(L) - hist + 1) / max(1, L - hist), 0, None)
|
|
87
|
+
err = rng.uniform(0.02, 0.4) * _smooth_noise(rng, L, 1, 20)
|
|
88
|
+
err = err + rng.uniform(0.0, 1.0) * lead * _smooth_noise(rng, L, 5, 100)
|
|
89
|
+
x = truth + err * sd
|
|
90
|
+
if rng.random() < 0.3:
|
|
91
|
+
x = x * rng.uniform(0.8, 1.2) + rng.normal() * 0.3 * sd
|
|
92
|
+
return x
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _weather(rng, L, P):
|
|
96
|
+
t = np.arange(L)
|
|
97
|
+
d = _ou(rng, L, log_uniform(rng, 20, 500))
|
|
98
|
+
if rng.random() < 0.6:
|
|
99
|
+
d = d + rng.uniform(0, 1.2) * np.sin(2 * np.pi * t / P + rng.uniform(0, 6.3))
|
|
100
|
+
d = (d - d.mean()) / _std(d)
|
|
101
|
+
theta = float(np.quantile(d, rng.uniform(0.2, 0.8)))
|
|
102
|
+
shape = rng.choice(["hinge_up", "hinge_down", "v", "tanh", "linear"],
|
|
103
|
+
p=[0.25, 0.2, 0.25, 0.15, 0.15])
|
|
104
|
+
if shape == "hinge_up":
|
|
105
|
+
r = np.maximum(d - theta, 0)
|
|
106
|
+
elif shape == "hinge_down":
|
|
107
|
+
r = np.maximum(theta - d, 0)
|
|
108
|
+
elif shape == "v":
|
|
109
|
+
r = rng.uniform(0.3, 1.0) * np.maximum(d - theta, 0) + rng.uniform(0.3, 1.0) * np.maximum(
|
|
110
|
+
theta - d, 0)
|
|
111
|
+
elif shape == "tanh":
|
|
112
|
+
r = np.tanh(rng.uniform(0.5, 3) * (d - theta))
|
|
113
|
+
else:
|
|
114
|
+
r = d
|
|
115
|
+
if rng.random() < 0.4:
|
|
116
|
+
k = rng.uniform(0.5, 0.98)
|
|
117
|
+
r = _decay(r, k) * (1 - k)
|
|
118
|
+
if rng.random() < 0.3:
|
|
119
|
+
r = r * _phase_gate(rng, L, P)
|
|
120
|
+
e = rng.choice([-1, 1]) * rng.uniform(0.1, 0.8) * (r - r.mean()) / _std(r)
|
|
121
|
+
x = d * rng.uniform(2, 15) + rng.uniform(-10, 30)
|
|
122
|
+
return x, e
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _promo(rng, L):
|
|
126
|
+
x = _runs(rng, L, rng.uniform(0.01, 0.15), log_uniform(rng, 1, 30))
|
|
127
|
+
lift = np.log1p(log_uniform(rng, 0.1, 1.5))
|
|
128
|
+
if rng.random() < 0.3:
|
|
129
|
+
k = rng.uniform(0.8, 0.99)
|
|
130
|
+
on = x * _decay(_edges(x)[0], k)
|
|
131
|
+
e = lift * np.where(x > 0, np.clip(on, 0.3, 1.0), 0.0)
|
|
132
|
+
else:
|
|
133
|
+
e = lift * x
|
|
134
|
+
starts, ends = _edges(x)
|
|
135
|
+
if rng.random() < 0.5:
|
|
136
|
+
k = rng.uniform(0.5, 0.95)
|
|
137
|
+
e = e + lift * rng.uniform(0.2, 0.7) * _decay(ends, k) * (1 - x)
|
|
138
|
+
if rng.random() < 0.4:
|
|
139
|
+
k = rng.uniform(0.6, 0.97)
|
|
140
|
+
e = e - lift * rng.uniform(0.1, 0.5) * _decay(ends, k) * (1 - x)
|
|
141
|
+
if rng.random() < 0.3:
|
|
142
|
+
r = int(rng.integers(2, 32))
|
|
143
|
+
ramp = np.zeros(len(x))
|
|
144
|
+
for s in np.flatnonzero(starts):
|
|
145
|
+
lo = max(0, s - r)
|
|
146
|
+
ramp[lo:s] = np.maximum(ramp[lo:s], np.linspace(0, 1, s - lo + 1)[1:])
|
|
147
|
+
e = e + lift * rng.uniform(0.1, 0.5) * ramp * (1 - x)
|
|
148
|
+
if rng.random() < 0.2:
|
|
149
|
+
depth = rng.uniform(0.2, 1.0, size=L)
|
|
150
|
+
depth = np.round(depth * 4) / 4
|
|
151
|
+
run_id = np.cumsum(starts)
|
|
152
|
+
level = depth[np.minimum(run_id.astype(int), L - 1)]
|
|
153
|
+
x = x * level
|
|
154
|
+
e = e * np.where(x > 0, level, 1.0)
|
|
155
|
+
return x, e
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _closure(rng, L, P):
|
|
159
|
+
return _runs(rng, L, rng.uniform(0.005, 0.1), log_uniform(rng, 1, max(2, P)))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _price(rng, L):
|
|
163
|
+
changes = rng.random(L) < rng.uniform(0.002, 0.02)
|
|
164
|
+
walk = np.cumsum(np.where(changes, rng.normal(0, 0.1, size=L), 0))
|
|
165
|
+
disc = _runs(rng, L, rng.uniform(0.03, 0.2), rng.uniform(3, 20))
|
|
166
|
+
starts, _ = _edges(disc)
|
|
167
|
+
depth = rng.uniform(0.1, 0.4, size=L)[np.minimum(np.cumsum(starts).astype(int), L - 1)]
|
|
168
|
+
logp = walk - disc * depth
|
|
169
|
+
eps = rng.uniform(0.5, 3.0)
|
|
170
|
+
e = -eps * (logp - logp.mean())
|
|
171
|
+
e = np.clip(e, -2.5, 2.5)
|
|
172
|
+
x = np.exp(logp) * log_uniform(rng, 1, 100)
|
|
173
|
+
return x, e
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _schedule(rng, L, P):
|
|
177
|
+
P = max(2, int(P))
|
|
178
|
+
segs = int(rng.integers(2, 7))
|
|
179
|
+
cuts = np.sort(rng.choice(np.arange(1, P), size=min(segs - 1, P - 1), replace=False))
|
|
180
|
+
vals = rng.random(len(cuts) + 1)
|
|
181
|
+
vals[rng.random(len(vals)) < 0.3] = 0.0
|
|
182
|
+
phase = (np.arange(L) + int(rng.integers(P))) % P
|
|
183
|
+
pattern = vals[np.searchsorted(cuts, phase, side="right")]
|
|
184
|
+
K = int(rng.integers(1, 13))
|
|
185
|
+
level = np.full(L, float(K))
|
|
186
|
+
if rng.random() < 0.3:
|
|
187
|
+
at = int(rng.integers(L // 4, L))
|
|
188
|
+
level[at:] = max(1, K + int(rng.integers(-K // 2 - 1, K // 2 + 2)))
|
|
189
|
+
c = np.round(level * pattern)
|
|
190
|
+
if rng.random() < 0.3:
|
|
191
|
+
c = c * (1 - _runs(rng, L, rng.uniform(0.01, 0.05), rng.uniform(1, 10)))
|
|
192
|
+
gamma = rng.uniform(0.5, 1.2)
|
|
193
|
+
delta = 0.1 * max(1.0, float(c.mean()))
|
|
194
|
+
e = gamma * (np.log(c + delta) - np.log(c.mean() + delta))
|
|
195
|
+
return c, e
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _setpoint(rng, L):
|
|
199
|
+
changes = rng.random(L) < log_uniform(rng, 0.002, 0.03)
|
|
200
|
+
levels = rng.normal(size=int(changes.sum()) + 1)
|
|
201
|
+
x = levels[np.cumsum(changes)]
|
|
202
|
+
if rng.random() < 0.3:
|
|
203
|
+
w = int(rng.integers(2, 64))
|
|
204
|
+
x = np.convolve(np.pad(x, (w - 1, 0), mode="edge"), np.ones(w) / w, mode="valid")
|
|
205
|
+
k1 = rng.uniform(0.5, 0.995)
|
|
206
|
+
r = _decay(x - x[0], k1) * (1 - k1) + x[0]
|
|
207
|
+
if rng.random() < 0.4:
|
|
208
|
+
k2 = rng.uniform(0.5, 0.98)
|
|
209
|
+
r = _decay(r - r[0], k2) * (1 - k2) + r[0]
|
|
210
|
+
e = rng.choice([-1, 1]) * rng.uniform(0.3, 1.0) * (r - r.mean()) / _std(r)
|
|
211
|
+
return x * log_uniform(rng, 0.1, 100) + rng.normal() * 20, e
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _decoy(rng, L, P):
|
|
215
|
+
kind = rng.choice(["smooth", "runs", "price", "schedule", "periodic", "weather"])
|
|
216
|
+
if kind == "smooth":
|
|
217
|
+
return _smooth_noise(rng, L, 3, 300) * log_uniform(rng, 0.1, 100) + rng.normal() * 10
|
|
218
|
+
if kind == "runs":
|
|
219
|
+
return _runs(rng, L, rng.uniform(0.01, 0.2), log_uniform(rng, 1, 30))
|
|
220
|
+
if kind == "price":
|
|
221
|
+
return _price(rng, L)[0]
|
|
222
|
+
if kind == "schedule":
|
|
223
|
+
return _schedule(rng, L, P)[0]
|
|
224
|
+
if kind == "weather":
|
|
225
|
+
return _weather(rng, L, P)[0]
|
|
226
|
+
q = int(rng.choice(PERIODS))
|
|
227
|
+
return np.sin(2 * np.pi * np.arange(L) / q + rng.uniform(0, 6.3))
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def make_task(base, rng, hist: int, horizon: int, k: int):
|
|
231
|
+
"""(target [L] float32, covariates [k, L] float32) from one base window [L]."""
|
|
232
|
+
L = hist + horizon
|
|
233
|
+
base = np.asarray(base[:L], dtype="float64")
|
|
234
|
+
pad = ~np.isfinite(base)
|
|
235
|
+
b = np.where(pad, 0.0, base)
|
|
236
|
+
obs = b[:hist][~pad[:hist]] if (~pad[:hist]).any() else b
|
|
237
|
+
b = (b - obs.mean()) / _std(obs)
|
|
238
|
+
b = np.clip(b, -8, 8)
|
|
239
|
+
P = _period(np.where(pad, np.nan, b), rng, hist)
|
|
240
|
+
none_relevant = rng.random() < NONE_RELEVANT
|
|
241
|
+
kinds = rng.choice(KINDS, size=k, p=KIND_P)
|
|
242
|
+
covs, effects, switches, target_fc = [], [], [], []
|
|
243
|
+
for kind in kinds:
|
|
244
|
+
relevant = not none_relevant and kind != "decoy"
|
|
245
|
+
if not relevant:
|
|
246
|
+
covs.append(_decoy(rng, L, P))
|
|
247
|
+
effects.append(None)
|
|
248
|
+
continue
|
|
249
|
+
if kind == "forecast":
|
|
250
|
+
if rng.random() < 0.4:
|
|
251
|
+
target_fc.append(len(covs))
|
|
252
|
+
covs.append(None)
|
|
253
|
+
effects.append(None)
|
|
254
|
+
continue
|
|
255
|
+
d, e = _forecast_driver(rng, L, P)
|
|
256
|
+
covs.append(_issue(rng, d, hist) * log_uniform(rng, 0.1, 100))
|
|
257
|
+
effects.append(e)
|
|
258
|
+
elif kind == "weather":
|
|
259
|
+
x, e = _weather(rng, L, P)
|
|
260
|
+
covs.append(x)
|
|
261
|
+
effects.append(e)
|
|
262
|
+
elif kind == "promo":
|
|
263
|
+
x, e = _promo(rng, L)
|
|
264
|
+
covs.append(x)
|
|
265
|
+
effects.append(e)
|
|
266
|
+
elif kind == "closure":
|
|
267
|
+
x = _closure(rng, L, P)
|
|
268
|
+
covs.append(x)
|
|
269
|
+
effects.append(None)
|
|
270
|
+
switches.append(x)
|
|
271
|
+
elif kind == "setpoint":
|
|
272
|
+
x, e = _setpoint(rng, L)
|
|
273
|
+
covs.append(x)
|
|
274
|
+
effects.append(e)
|
|
275
|
+
elif kind == "price":
|
|
276
|
+
x, e = _price(rng, L)
|
|
277
|
+
covs.append(x)
|
|
278
|
+
effects.append(e)
|
|
279
|
+
else:
|
|
280
|
+
x, e = _schedule(rng, L, P)
|
|
281
|
+
covs.append(x)
|
|
282
|
+
effects.append(e)
|
|
283
|
+
live = [i for i, e in enumerate(effects) if e is not None]
|
|
284
|
+
for i in live:
|
|
285
|
+
others = [j for j in range(len(covs)) if j != i and covs[j] is not None]
|
|
286
|
+
if others and rng.random() < INTERACT:
|
|
287
|
+
z = covs[int(rng.choice(others))]
|
|
288
|
+
z = np.clip((z - z.mean()) / _std(z), -2, 2)
|
|
289
|
+
effects[i] = effects[i] * np.clip(1 + rng.uniform(0.2, 0.6) * z, 0, None)
|
|
290
|
+
total = np.zeros(L)
|
|
291
|
+
for e in effects:
|
|
292
|
+
if e is not None:
|
|
293
|
+
total = total + e
|
|
294
|
+
log_mode = rng.random() < 0.6
|
|
295
|
+
if log_mode:
|
|
296
|
+
if rng.random() < 0.5:
|
|
297
|
+
level = np.exp(rng.uniform(0.1, 0.6) * b)
|
|
298
|
+
else:
|
|
299
|
+
level = b - b.min() + rng.uniform(0.02, 0.5) * (b.max() - b.min() + 1e-6)
|
|
300
|
+
y = level * np.exp(np.clip(total, -6, 6))
|
|
301
|
+
else:
|
|
302
|
+
if rng.random() < 0.25:
|
|
303
|
+
b = b * rng.uniform(0, 0.3)
|
|
304
|
+
y = b + rng.uniform(0.5, 3.0) * total
|
|
305
|
+
for x in switches:
|
|
306
|
+
if rng.random() < 0.5:
|
|
307
|
+
floor = rng.uniform(0, 0.1) * float(np.mean(np.abs(y)))
|
|
308
|
+
alt = np.full(L, floor) if log_mode else np.full(L, float(y.min()))
|
|
309
|
+
else:
|
|
310
|
+
w = max(2, int(P))
|
|
311
|
+
kern = np.ones(w) / w
|
|
312
|
+
smooth = np.convolve(np.pad(y, (w - 1, 0), mode="edge"), kern, mode="valid")
|
|
313
|
+
lam = rng.uniform(0.3, 1.3)
|
|
314
|
+
alt = smooth * lam if log_mode else smooth + (lam - 1) * _std(y)
|
|
315
|
+
y = np.where(x > 0, alt, y)
|
|
316
|
+
sd = _std(y)
|
|
317
|
+
if log_mode:
|
|
318
|
+
y = y * np.exp(rng.normal(size=L) * rng.uniform(0.01, 0.2))
|
|
319
|
+
if rng.random() < 0.2:
|
|
320
|
+
scale = log_uniform(rng, 0.5, 50) / max(1e-9, float(np.mean(y)))
|
|
321
|
+
y = rng.poisson(np.clip(y * scale, 0, 1e6)).astype("float64")
|
|
322
|
+
else:
|
|
323
|
+
y = y + rng.normal(size=L) * rng.uniform(0.01, 0.3) * sd
|
|
324
|
+
for i in target_fc:
|
|
325
|
+
w = int(rng.integers(1, 9))
|
|
326
|
+
smooth = np.convolve(np.pad(y, (w - 1, 0), mode="edge"), np.ones(w) / w, mode="valid")
|
|
327
|
+
covs[i] = _issue(rng, smooth, hist)
|
|
328
|
+
units = log_uniform(rng, 1e-2, 1e4)
|
|
329
|
+
y = y * units
|
|
330
|
+
if not log_mode:
|
|
331
|
+
y = y + rng.normal() * units * log_uniform(rng, 0.1, 30)
|
|
332
|
+
for i in target_fc:
|
|
333
|
+
covs[i] = covs[i] * units
|
|
334
|
+
c = np.stack(covs)
|
|
335
|
+
order = rng.permutation(len(c))
|
|
336
|
+
c = c[order]
|
|
337
|
+
for i in range(len(c)):
|
|
338
|
+
if rng.random() < 0.05:
|
|
339
|
+
c[i, rng.random(L) < rng.uniform(0, 0.1)] = np.nan
|
|
340
|
+
y[pad] = np.nan
|
|
341
|
+
return y.astype("float32"), c.astype("float32")
|
|
342
|
+
|