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
simons/synth/mech.py
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
"""Mechanistic priors: polynomial flows, epidemics, rainfall-runoff, queues and thermal systems."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
|
|
7
|
+
from simons.synth.base import _logu, _randn, _standardise, _u
|
|
8
|
+
|
|
9
|
+
BANK = {"chaos": 16384, "epi": 8192, "hydro": 8192, "thermal": 8192}
|
|
10
|
+
BANK_LENGTH = 9216
|
|
11
|
+
REFRESH = 400
|
|
12
|
+
PART = 1
|
|
13
|
+
SPARE = 0.35
|
|
14
|
+
_STATE = {}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def configure(bank=None, length=None, refresh=None, part=None):
|
|
18
|
+
"""Update trajectory-bank settings and clear shared caches; None retains a setting.
|
|
19
|
+
`length`: steps per path; `refresh`: calls between refreshes (0: never); fraction: 1/part."""
|
|
20
|
+
if bank is not None:
|
|
21
|
+
scale = int(bank) / 16384
|
|
22
|
+
for k in BANK:
|
|
23
|
+
BANK[k] = max(64, round({"chaos": 16384}.get(k, 8192) * scale))
|
|
24
|
+
global BANK_LENGTH, REFRESH, PART
|
|
25
|
+
if part is not None:
|
|
26
|
+
PART = max(1, int(part))
|
|
27
|
+
if length is not None:
|
|
28
|
+
BANK_LENGTH = int(length)
|
|
29
|
+
if refresh is not None:
|
|
30
|
+
REFRESH = int(refresh)
|
|
31
|
+
_STATE.clear()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _ar1(gen, n, length, device, phi, sd):
|
|
35
|
+
"""Draw AR(1) paths [n,L] from zero with per-row phi and asymptotic sd [n,1]."""
|
|
36
|
+
from simons.synth.nonstat import _ou_scan
|
|
37
|
+
|
|
38
|
+
z = _randn(gen, (n, length), device).double()
|
|
39
|
+
phi = phi.double()
|
|
40
|
+
a = phi.expand(n, length).contiguous()
|
|
41
|
+
drive = z * torch.sqrt(1 - phi**2)
|
|
42
|
+
y = _ou_scan(a, drive, z[:, 0] * 0)
|
|
43
|
+
return (y * sd.double()).float()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# Founder tuples: (name, period, amplitude, init center, init scale, equations).
|
|
47
|
+
X, Y, Z, U, V = 0, 1, 2, 3, 4
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _sprott():
|
|
51
|
+
return [
|
|
52
|
+
("sprott_a", 6.0, 1.5, (0, 5, 0), 0.5,
|
|
53
|
+
[[(1, (Y,))],
|
|
54
|
+
[(-1, (X,)), (1, (Y, Z))],
|
|
55
|
+
[(1, ()), (-1, (Y, Y))]]),
|
|
56
|
+
("sprott_b", 6.0, 1.5, (0.4, 0.3, 0.1), 0.5,
|
|
57
|
+
[[(1, (Y, Z))],
|
|
58
|
+
[(1, (X,)), (-1, (Y,))],
|
|
59
|
+
[(1, ()), (-1, (X, Y))]]),
|
|
60
|
+
("sprott_c", 6.0, 1.5, (0.4, 0.3, 0.1), 0.5,
|
|
61
|
+
[[(1, (Y, Z))],
|
|
62
|
+
[(1, (X,)), (-1, (Y,))],
|
|
63
|
+
[(1, ()), (-1, (X, X))]]),
|
|
64
|
+
("sprott_e", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
65
|
+
[[(1, (Y, Z))],
|
|
66
|
+
[(1, (X, X)), (-1, (Y,))],
|
|
67
|
+
[(1, ()), (-4, (X,))]]),
|
|
68
|
+
("sprott_g", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
69
|
+
[[(0.4, (X,)), (1, (Z,))],
|
|
70
|
+
[(1, (X, Z)), (-1, (Y,))],
|
|
71
|
+
[(-1, (X,)), (1, (Y,))]]),
|
|
72
|
+
("sprott_h", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
73
|
+
[[(-1, (Y,)), (1, (Z, Z))],
|
|
74
|
+
[(1, (X,)), (0.5, (Y,))],
|
|
75
|
+
[(1, (X,)), (-1, (Z,))]]),
|
|
76
|
+
("sprott_j", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
77
|
+
[[(2, (Z,))],
|
|
78
|
+
[(-2, (Y,)), (1, (Z,))],
|
|
79
|
+
[(-1, (X,)), (1, (Y,)), (1, (Y, Y))]]),
|
|
80
|
+
("sprott_k", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
81
|
+
[[(1, (X, Y)), (-1, (Z,))],
|
|
82
|
+
[(1, (X,)), (-1, (Y,))],
|
|
83
|
+
[(1, (X,)), (0.3, (Z,))]]),
|
|
84
|
+
("sprott_n", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
85
|
+
[[(-2, (Y,))],
|
|
86
|
+
[(1, (X,)), (1, (Z, Z))],
|
|
87
|
+
[(1, ()), (1, (Y,)), (-2, (Z,))]]),
|
|
88
|
+
("sprott_s", 6.0, 1.5, (0.5, 0.5, 0.5), 0.3,
|
|
89
|
+
[[(-1, (X,)), (-4, (Y,))],
|
|
90
|
+
[(1, (X,)), (1, (Z, Z))],
|
|
91
|
+
[(1, ()), (1, (X,))]]),
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
FOUNDERS = [
|
|
96
|
+
("lorenz", 0.75, 8.0, (0, 1, 25), 5.0,
|
|
97
|
+
[[(-10, (X,)), (10, (Y,))],
|
|
98
|
+
[(28, (X,)), (-1, (X, Z)), (-1, (Y,))],
|
|
99
|
+
[(1, (X, Y)), (-8 / 3, (Z,))]]),
|
|
100
|
+
("roessler", 6.1, 5.0, (0, -5, 0.5), 2.0,
|
|
101
|
+
[[(-1, (Y,)), (-1, (Z,))],
|
|
102
|
+
[(1, (X,)), (0.2, (Y,))],
|
|
103
|
+
[(0.2, ()), (1, (X, Z)), (-5.7, (Z,))]]),
|
|
104
|
+
("chen", 0.45, 8.0, (-3, 2, 20), 4.0,
|
|
105
|
+
[[(-35, (X,)), (35, (Y,))],
|
|
106
|
+
[(-7, (X,)), (-1, (X, Z)), (28, (Y,))],
|
|
107
|
+
[(1, (X, Y)), (-3, (Z,))]]),
|
|
108
|
+
("lu", 0.6, 8.0, (1, 1, 15), 4.0,
|
|
109
|
+
[[(-36, (X,)), (36, (Y,))],
|
|
110
|
+
[(-1, (X, Z)), (20, (Y,))],
|
|
111
|
+
[(1, (X, Y)), (-3, (Z,))]]),
|
|
112
|
+
*_sprott(),
|
|
113
|
+
("halvorsen", 2.5, 4.0, (-1, 0, 0), 1.0,
|
|
114
|
+
[[(-1.89, (X,)), (-4, (Y,)), (-4, (Z,)), (-1, (Y, Y))],
|
|
115
|
+
[(-1.89, (Y,)), (-4, (Z,)), (-4, (X,)), (-1, (Z, Z))],
|
|
116
|
+
[(-1.89, (Z,)), (-4, (X,)), (-4, (Y,)), (-1, (X, X))]]),
|
|
117
|
+
("shimizu", 8.0, 1.0, (0.1, 0, 0), 0.3,
|
|
118
|
+
[[(1, (Y,))],
|
|
119
|
+
[(1, (X,)), (-0.75, (Y,)), (-1, (X, Z))],
|
|
120
|
+
[(-0.45, (Z,)), (1, (X, X))]]),
|
|
121
|
+
("arneodo", 5.0, 2.0, (0.2, 0.2, 0.2), 0.3,
|
|
122
|
+
[[(1, (Y,))],
|
|
123
|
+
[(1, (Z,))],
|
|
124
|
+
[(5.5, (X,)), (-3.5, (Y,)), (-1, (Z,)), (-1, (X, X, X))]]),
|
|
125
|
+
("genesio", 6.0, 0.5, (0.1, 0.1, 0.1), 0.1,
|
|
126
|
+
[[(1, (Y,))],
|
|
127
|
+
[(1, (Z,))],
|
|
128
|
+
[(-1, (X,)), (-1.1, (Y,)), (-0.44, (Z,)), (1, (X, X))]]),
|
|
129
|
+
("burke_shaw", 1.0, 1.0, (0.6, 0, 0), 0.3,
|
|
130
|
+
[[(-10, (X,)), (-10, (Y,))],
|
|
131
|
+
[(-1, (Y,)), (-10, (X, Z))],
|
|
132
|
+
[(10, (X, Y)), (4.272, ())]]),
|
|
133
|
+
("hindmarsh_rose", 10.0, 1.0, (-1, -5, 3), 0.3,
|
|
134
|
+
[[(1, (Y,)), (-1, (X, X, X)), (3, (X, X)), (-1, (Z,)), (3.25, ())],
|
|
135
|
+
[(1, ()), (-5, (X, X)), (-1, (Y,))],
|
|
136
|
+
[(0.02, (X,)), (0.032, ()), (-0.005, (Z,))]]),
|
|
137
|
+
("lotka_volterra", 40.0, 0.3, (0.3, 0.3, 0.3), 0.1,
|
|
138
|
+
[[(0.001, ()), (1, (X,)), (-1, (X, X)), (-0.8, (X, Y)), (-1.3, (X, Z))],
|
|
139
|
+
[(0.001, ()), (1, (Y,)), (-1.3, (X, Y)), (-1, (Y, Y)), (-0.8, (Y, Z))],
|
|
140
|
+
[(0.001, ()), (1, (Z,)), (-0.8, (X, Z)), (-1.3, (Y, Z)), (-1, (Z, Z))]]),
|
|
141
|
+
("moore_spiegel", 3.0, 1.0, (0.1, 0, 0), 0.3,
|
|
142
|
+
[[(1, (Y,))],
|
|
143
|
+
[(1, (Z,))],
|
|
144
|
+
[(-1, (Z,)), (14, (Y,)), (-20, (X, X, Y)), (-6, (X,))]]),
|
|
145
|
+
("rucklidge", 4.0, 2.0, (1, 0, 4.5), 1.0,
|
|
146
|
+
[[(-2, (X,)), (6.7, (Y,)), (-1, (Y, Z))],
|
|
147
|
+
[(1, (X,))],
|
|
148
|
+
[(-1, (Z,)), (1, (Y, Y))]]),
|
|
149
|
+
("aizawa", 1.8, 0.7, (0.1, 0, 0), 0.1,
|
|
150
|
+
[[(1, (X, Z)), (-0.7, (X,)), (-3.5, (Y,))],
|
|
151
|
+
[(3.5, (X,)), (1, (Y, Z)), (-0.7, (Y,))],
|
|
152
|
+
[(0.6, ()), (0.95, (Z,)), (-1 / 3, (Z, Z, Z)), (-1, (X, X)), (-1, (Y, Y)), (-0.25, (X, X, Z)),
|
|
153
|
+
(-0.25, (Y, Y, Z))]]),
|
|
154
|
+
("dadras", 2.0, 3.0, (1, 1, 0), 1.0,
|
|
155
|
+
[[(1, (Y,)), (-3, (X,)), (2.7, (Y, Z))],
|
|
156
|
+
[(1.7, (Y,)), (-1, (X, Z)), (1, (Z,))],
|
|
157
|
+
[(2, (X, Y)), (-9, (Z,))]]),
|
|
158
|
+
]
|
|
159
|
+
# Forced tuples: (name, forcing frequency, amplitude, init, scale, [eq_x, eq_y]).
|
|
160
|
+
FORCED = [
|
|
161
|
+
("duffing", 1.2, 1.0, (1, 0, 0), 0.5,
|
|
162
|
+
[[(1, (Y,))],
|
|
163
|
+
[(-0.3, (Y,)), (1, (X,)), (-1, (X, X, X)), (0.5, (U,))]]),
|
|
164
|
+
("vanderpol", 1.1, 2.0, (1, 0, 0), 0.5,
|
|
165
|
+
[[(1, (Y,))],
|
|
166
|
+
[(2, (Y,)), (-2, (X, X, Y)), (-1, (X,)), (1.0, (U,))]]),
|
|
167
|
+
("fitzhugh", 0.2, 1.5, (0, 0, 0), 0.5,
|
|
168
|
+
[[(1, (X,)), (-1 / 3, (X, X, X)), (-1, (Y,)), (0.5, ()), (0.3, (U,))],
|
|
169
|
+
[(0.056, ()), (0.08, (X,)), (-0.064, (Y,))]]),
|
|
170
|
+
("brusselator", 0.81, 1.0, (1, 1, 0), 0.3,
|
|
171
|
+
[[(0.4, ()), (-2.2, (X,)), (1, (X, X, Y)), (0.05, (U,))],
|
|
172
|
+
[(1.2, (X,)), (-1, (X, X, Y))]]),
|
|
173
|
+
]
|
|
174
|
+
DIM, FEATURES = 6, 1 + 6 + 36 + 216
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _feature(mono):
|
|
178
|
+
m = sorted(mono)
|
|
179
|
+
if not m:
|
|
180
|
+
return 0
|
|
181
|
+
if len(m) == 1:
|
|
182
|
+
return 1 + m[0]
|
|
183
|
+
if len(m) == 2:
|
|
184
|
+
return 7 + 6 * m[0] + m[1]
|
|
185
|
+
return 43 + 36 * m[0] + 6 * m[1] + m[2]
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _template(eqs, shift):
|
|
189
|
+
c = torch.zeros(FEATURES, DIM)
|
|
190
|
+
for row, terms in enumerate(eqs):
|
|
191
|
+
for coef, mono in terms:
|
|
192
|
+
c[_feature(tuple(i + shift for i in mono)), row + shift] += coef
|
|
193
|
+
return c
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
_TEMPLATES = {}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _templates(device):
|
|
200
|
+
key = str(device)
|
|
201
|
+
if key not in _TEMPLATES:
|
|
202
|
+
a = [_template(f[5], 0) for f in FOUNDERS] + [_template(f[5], 0) for f in FORCED]
|
|
203
|
+
b = [_template(f[5], 3) for f in FOUNDERS]
|
|
204
|
+
for i, f in enumerate(FORCED):
|
|
205
|
+
a[len(FOUNDERS) + i][_feature((V,)), U] = f[1]
|
|
206
|
+
a[len(FOUNDERS) + i][_feature((U,)), V] = -f[1]
|
|
207
|
+
meta = torch.tensor(
|
|
208
|
+
[[2 * math.pi / f[1] if j >= len(FOUNDERS) else f[1], f[2], *f[3], f[4]]
|
|
209
|
+
for j, f in enumerate(FOUNDERS + FORCED)], dtype=torch.float32)
|
|
210
|
+
_TEMPLATES[key] = (torch.stack(a).to(device), torch.stack(b).to(device), meta.to(device))
|
|
211
|
+
return _TEMPLATES[key]
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
TERMS = 32
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _rhs(x, feat, eq, val, ones):
|
|
218
|
+
"""Evaluate sparse polynomial terms: sum val_k * monomial(feat_k) into equation eq_k."""
|
|
219
|
+
n = x.shape[0]
|
|
220
|
+
x2 = (x[:, :, None] * x[:, None, :]).reshape(n, 36)
|
|
221
|
+
x3 = (x2[:, :, None] * x[:, None, :]).reshape(n, 216)
|
|
222
|
+
f = torch.cat([ones, x, x2, x3], 1)
|
|
223
|
+
return torch.zeros_like(x).scatter_add_(1, eq, f.gather(1, feat) * val)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _chaos_bank(gen, n, length, device, channels=3, burn=768, sub=3):
|
|
227
|
+
ta, tb, meta = _templates(device)
|
|
228
|
+
nf, nforced = len(FOUNDERS), len(FORCED)
|
|
229
|
+
founder = torch.randint(0, nf + nforced, (n,), generator=gen, device=device)
|
|
230
|
+
forced = founder >= nf
|
|
231
|
+
response = torch.randint(0, nf, (n,), generator=gen, device=device)
|
|
232
|
+
skew = (_u(gen, (n,), device=device) < 0.4) & ~forced
|
|
233
|
+
period, amp = meta[founder, 0], meta[founder, 1]
|
|
234
|
+
coef = ta[founder]
|
|
235
|
+
rate = meta[response, 0] / period * _logu(gen, (n,), 0.5, 2.0, device)
|
|
236
|
+
coef = coef + torch.where(skew[:, None, None], tb[response] * rate[:, None, None], 0.0)
|
|
237
|
+
kappa = _u(gen, (n,), 0.05, 0.6, device) * meta[response, 1] / meta[response, 0] * rate / amp
|
|
238
|
+
coef[:, 1 + X, 3] += torch.where(skew, kappa, 0.0)
|
|
239
|
+
sigma = _u(gen, (n, 1, 1), 0.0, 0.2, device)
|
|
240
|
+
mutate = torch.exp(sigma * _randn(gen, coef.shape, device))
|
|
241
|
+
keep = torch.zeros(DIM, device=device, dtype=torch.bool)
|
|
242
|
+
keep[3:5] = True
|
|
243
|
+
keep = forced[:, None, None] & keep[None, None, :]
|
|
244
|
+
coef = torch.where(keep, coef, coef * mutate)
|
|
245
|
+
x = torch.zeros(n, DIM, device=device)
|
|
246
|
+
x[:, :3] = meta[founder, 2:5] + meta[founder, 5:6] * _randn(gen, (n, 3), device)
|
|
247
|
+
x[:, 3:] = meta[response, 2:5] + meta[response, 5:6] * _randn(gen, (n, 3), device)
|
|
248
|
+
phase = _u(gen, (n,), 0.0, 2 * math.pi, device)
|
|
249
|
+
x[:, 3] = torch.where(forced, torch.cos(phase), x[:, 3])
|
|
250
|
+
x[:, 4] = torch.where(forced, -torch.sin(phase), x[:, 4])
|
|
251
|
+
x[:, 5] = torch.where(forced, 0.0, x[:, 5])
|
|
252
|
+
block = torch.where(skew & (_u(gen, (n,), device=device) < 0.6), 3, 0)
|
|
253
|
+
active = torch.where(forced, 2, 3)
|
|
254
|
+
w = torch.zeros(n, DIM, channels, device=device)
|
|
255
|
+
rows = torch.arange(n, device=device)
|
|
256
|
+
for c in range(channels):
|
|
257
|
+
if c == 1:
|
|
258
|
+
g = (_randn(gen, (n, 3), device)
|
|
259
|
+
* (torch.arange(3, device=device)[None] < active[:, None]))
|
|
260
|
+
for k in range(3):
|
|
261
|
+
w[rows, block + k, c] = g[:, k]
|
|
262
|
+
else:
|
|
263
|
+
k = (_u(gen, (n,), device=device) * active).long().clamp(max=2)
|
|
264
|
+
other = torch.where(skew & (_u(gen, (n,), device=device) < 0.5), 3 - block, block)
|
|
265
|
+
w[rows, (other if c == 2 else block) + k, c] = 1.0
|
|
266
|
+
flat = coef.reshape(n, FEATURES * DIM)
|
|
267
|
+
top = flat.abs().topk(TERMS, dim=1).indices
|
|
268
|
+
val, feat, eq = flat.gather(1, top), top // DIM, top % DIM
|
|
269
|
+
del coef, flat, mutate
|
|
270
|
+
ppp = _logu(gen, (n,), 14.0, 200.0, device)
|
|
271
|
+
dt = (period / ppp / sub)[:, None]
|
|
272
|
+
dt = torch.minimum(dt, (period / 42)[:, None])
|
|
273
|
+
half, sixth = dt / 2, dt / 6
|
|
274
|
+
ones = torch.ones(n, 1, device=device)
|
|
275
|
+
out = torch.empty(n, length, channels, device=device, dtype=torch.float16)
|
|
276
|
+
for t in range(burn + length):
|
|
277
|
+
for _ in range(sub):
|
|
278
|
+
k1 = _rhs(x, feat, eq, val, ones)
|
|
279
|
+
k2 = _rhs(torch.addcmul(x, half, k1), feat, eq, val, ones)
|
|
280
|
+
k3 = _rhs(torch.addcmul(x, half, k2), feat, eq, val, ones)
|
|
281
|
+
k4 = _rhs(torch.addcmul(x, dt, k3), feat, eq, val, ones)
|
|
282
|
+
x = torch.addcmul(x, sixth, k1 + 2 * (k2 + k3) + k4)
|
|
283
|
+
x = x.clamp(-1e5, 1e5)
|
|
284
|
+
if t >= burn:
|
|
285
|
+
out[:, t - burn] = torch.bmm(x[:, None], w)[:, 0].clamp(-6e4, 6e4).half()
|
|
286
|
+
return out
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _flow(x, p, z):
|
|
290
|
+
"""Approximate Binomial(x,p) with normal draws clamped to [0,x]."""
|
|
291
|
+
m = x * p
|
|
292
|
+
return torch.minimum(
|
|
293
|
+
torch.addcmul(m, torch.sqrt((m * (1 - p)).clamp_min(0)), z).clamp_min(0), x)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _epi_bank(gen, n, length, device, burn=512):
|
|
297
|
+
total = burn + length
|
|
298
|
+
t = torch.arange(total, device=device, dtype=torch.float32)[None]
|
|
299
|
+
weekly = _u(gen, (n, 1), device=device) < 0.35
|
|
300
|
+
dt = torch.where(weekly, 7.0, 1.0)
|
|
301
|
+
year = 365.25 / dt
|
|
302
|
+
pop = _logu(gen, (n, 1), 1e4, 1e7, device)
|
|
303
|
+
gamma = 1 / _u(gen, (n, 1), 2.0, 10.0, device)
|
|
304
|
+
sir = _u(gen, (n, 1), device=device) < 0.3
|
|
305
|
+
sigma = torch.where(sir, 50.0, 1 / _u(gen, (n, 1), 1.0, 6.0, device))
|
|
306
|
+
r0 = _u(gen, (n, 1), 1.1, 4.0, device)
|
|
307
|
+
waning = torch.where(_u(gen, (n, 1), device=device) < 0.7,
|
|
308
|
+
1 / _logu(gen, (n, 1), 60.0, 2000.0, device), 0.0)
|
|
309
|
+
births = torch.where((_u(gen, (n, 1), device=device) < 0.4) | (waning == 0),
|
|
310
|
+
1 / _logu(gen, (n, 1), 5 * 365.0, 70 * 365.0, device), 0.0)
|
|
311
|
+
season = torch.ones(n, total, device=device)
|
|
312
|
+
for k, (hi, p) in enumerate(((0.45, 1.0), (0.15, 0.5), (0.1, 0.3))):
|
|
313
|
+
a = _u(gen, (n, 1), 0.0, hi, device) * (_u(gen, (n, 1), device=device) < p)
|
|
314
|
+
ph = _u(gen, (n, 1), 0.0, 2 * math.pi, device)
|
|
315
|
+
season = season + a * torch.cos(2 * math.pi * (k + 1) * t / year + ph)
|
|
316
|
+
logm = torch.zeros(n, total, device=device)
|
|
317
|
+
count = torch.randint(0, 5, (n, 1), generator=gen, device=device)
|
|
318
|
+
for e in range(4):
|
|
319
|
+
start = _u(gen, (n, 1), 0.0, float(total), device)
|
|
320
|
+
dur = _logu(gen, (n, 1), 10.0, 600.0, device) / dt
|
|
321
|
+
m = torch.log(_logu(gen, (n, 1), 0.35, 1.6, device))
|
|
322
|
+
logm = logm + m * ((t >= start) & (t < start + dur) & (count > e))
|
|
323
|
+
od = _u(gen, (n, 1), 0.0, 0.4, device)
|
|
324
|
+
noise = torch.exp(od * _randn(gen, (n, total), device) - od**2 / 2)
|
|
325
|
+
beta = (r0 * gamma) * season.clamp_min(0.05) * torch.exp(logm) * noise * dt / pop
|
|
326
|
+
imports = torch.poisson((_logu(gen, (n, 1), 0.01, 3.0, device) * dt)
|
|
327
|
+
.expand(n, total).contiguous(), generator=gen)
|
|
328
|
+
z = _randn(gen, (n, total, 4), device)
|
|
329
|
+
p_const = 1 - torch.exp(-torch.cat([sigma, gamma, waning], 1) * dt)
|
|
330
|
+
p = torch.cat([torch.zeros(n, 1, device=device), p_const], 1)
|
|
331
|
+
p_birth = 1 - torch.exp(-births * dt)
|
|
332
|
+
s0 = _u(gen, (n, 1), 0.2, 1.0, device)
|
|
333
|
+
i0 = _logu(gen, (n, 1), 1e-5, 1e-2, device)
|
|
334
|
+
x = torch.cat([s0 * (1 - i0), i0 / 2, i0 / 2, (1 - s0) * (1 - i0)], 1) * pop
|
|
335
|
+
inc = torch.empty(n, length, device=device)
|
|
336
|
+
prev = torch.empty(n, length, device=device)
|
|
337
|
+
for step in range(total):
|
|
338
|
+
p[:, 0] = 1 - torch.exp(-beta[:, step] * x[:, 2])
|
|
339
|
+
f = _flow(x, p, z[:, step])
|
|
340
|
+
x = x - f + f.roll(1, 1)
|
|
341
|
+
x = x * (1 - p_birth)
|
|
342
|
+
x[:, 0] += (p_birth * pop)[:, 0]
|
|
343
|
+
x[:, 1] += imports[:, step]
|
|
344
|
+
if step >= burn:
|
|
345
|
+
inc[:, step - burn] = f[:, 1]
|
|
346
|
+
prev[:, step - burn] = x[:, 2]
|
|
347
|
+
from simons.synth.nonstat import _ou_scan
|
|
348
|
+
|
|
349
|
+
tt = t[:, :length]
|
|
350
|
+
r_a, r_b = _logu(gen, (n, 1), 0.02, 0.8, device), _logu(gen, (n, 1), 0.02, 0.8, device)
|
|
351
|
+
centre, width = (_u(gen, (n, 1), 0.0, float(length), device),
|
|
352
|
+
_logu(gen, (n, 1), 5.0, 500.0, device))
|
|
353
|
+
report = r_a + (r_b - r_a) * torch.sigmoid((tt - centre) / width)
|
|
354
|
+
q = _u(gen, (n, 1), math.exp(-0.25), 0.97, device)
|
|
355
|
+
inc = torch.where(torch.isfinite(inc), inc, 0).clamp(0, 1e9)
|
|
356
|
+
delayed = _ou_scan(q.double().expand(n, length).contiguous(),
|
|
357
|
+
((1 - q).double() * inc.double()), inc[:, 0].double() * 0).float()
|
|
358
|
+
delayed = torch.where(_u(gen, (n, 1), device=device) < 0.6, delayed, inc)
|
|
359
|
+
wd = torch.exp(_u(gen, (n, 1), 0.0, 0.4, device) * _randn(gen, (n, 7), device))
|
|
360
|
+
wd = torch.where(weekly, 1.0, wd)
|
|
361
|
+
weekday = wd.gather(1, (tt.long() % 7).expand(n, length))
|
|
362
|
+
obs_sd = _u(gen, (n, 1), 0.0, 0.5, device)
|
|
363
|
+
mean = report * delayed * weekday * torch.exp(obs_sd * _randn(gen, (n, length), device)
|
|
364
|
+
- obs_sd**2 / 2)
|
|
365
|
+
cases = torch.poisson(torch.nan_to_num(mean, 0.0, 0.0, 0.0).clamp(0, 1e8), generator=gen)
|
|
366
|
+
occupancy = prev * _logu(gen, (n, 1), 0.001, 0.3, device) * torch.exp(
|
|
367
|
+
0.05 * _randn(gen, (n, length), device))
|
|
368
|
+
return torch.stack([cases, occupancy], -1)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def _hydro_bank(gen, n, length, device, burn=512):
|
|
372
|
+
from simons.synth.heavy import _gamma
|
|
373
|
+
|
|
374
|
+
total = burn + length
|
|
375
|
+
t = torch.arange(total, device=device, dtype=torch.float32)[None]
|
|
376
|
+
hourly = _u(gen, (n, 1), device=device) < 0.4
|
|
377
|
+
per_day = torch.where(hourly, 24.0, 1.0)
|
|
378
|
+
year = 365.25 * per_day
|
|
379
|
+
ph = _u(gen, (n, 1), 0.0, 2 * math.pi, device)
|
|
380
|
+
seas = torch.cos(2 * math.pi * t / year + ph)
|
|
381
|
+
p01 = torch.where(hourly, _logu(gen, (n, 1), 0.003, 0.04, device),
|
|
382
|
+
_logu(gen, (n, 1), 0.03, 0.4, device))
|
|
383
|
+
p11 = torch.where(hourly, _u(gen, (n, 1), 0.6, 0.95, device), _u(gen, (n, 1), 0.2, 0.8, device))
|
|
384
|
+
p01_t = (p01 * (1 + _u(gen, (n, 1), 0.0, 0.8, device) * seas)).clamp(0, 1)
|
|
385
|
+
shape = _u(gen, (n, 1), 0.5, 1.3, device)
|
|
386
|
+
amount = _gamma(shape.expand(n, total).contiguous(), gen) / shape
|
|
387
|
+
amount = amount * torch.where(hourly, _u(gen, (n, 1), 0.5, 3.0, device),
|
|
388
|
+
_u(gen, (n, 1), 3.0, 15.0, device))
|
|
389
|
+
amount = amount * (1 + _u(gen, (n, 1), 0.0, 0.5, device) * seas)
|
|
390
|
+
occ = _u(gen, (n, total), device=device)
|
|
391
|
+
snowy = _u(gen, (n, 1), device=device) < 0.35
|
|
392
|
+
temp = (_u(gen, (n, 1), -5.0, 12.0, device) + _u(gen, (n, 1), 5.0, 15.0, device) * seas
|
|
393
|
+
+ torch.where(hourly, _u(gen, (n, 1), 0.0, 6.0, device), 0.0)
|
|
394
|
+
* torch.cos(2 * math.pi * t / 24 + _u(gen, (n, 1), 0.0, 6.3, device))
|
|
395
|
+
+ _ar1(gen, n, total, device, torch.where(hourly, 0.99, 0.8),
|
|
396
|
+
torch.full((n, 1), 3.0, device=device)))
|
|
397
|
+
snow_frac = torch.where(snowy, torch.sigmoid(-temp), 0.0)
|
|
398
|
+
melt_rate = _u(gen, (n, 1), 1.0, 5.0, device) / per_day * torch.relu(temp) * snowy
|
|
399
|
+
smax = _logu(gen, (n, 1), 20.0, 300.0, device)
|
|
400
|
+
k = _logu(gen, (n, 1), 2e-3, 0.15, device) / per_day
|
|
401
|
+
a = _u(gen, (n, 1), 1.0, 3.0, device)
|
|
402
|
+
pet = _u(gen, (n, 1), 0.5, 5.0, device) / per_day * (1 + 0.8 * -seas).clamp_min(0)
|
|
403
|
+
pet = pet * torch.where(hourly, (math.pi * torch.sin(2 * math.pi * t / 24)).clamp_min(0), 1.0)
|
|
404
|
+
alpha = torch.where(hourly, _logu(gen, (n, 1), 0.01, 0.3, device),
|
|
405
|
+
_logu(gen, (n, 1), 0.1, 0.9, device))
|
|
406
|
+
fg = _u(gen, (n, 1), 0.1, 0.7, device)
|
|
407
|
+
g = _logu(gen, (n, 1), 2e-4, 2e-2, device) / per_day * torch.where(hourly, 4.0, 1.0)
|
|
408
|
+
soil = smax[:, 0] * _u(gen, (n,), 0.2, 0.8, device)
|
|
409
|
+
snow, route = torch.zeros(n, device=device), torch.zeros(n, device=device)
|
|
410
|
+
ground = torch.zeros(n, device=device)
|
|
411
|
+
wet = torch.zeros(n, device=device, dtype=torch.bool)
|
|
412
|
+
smax, k, a, alpha, fg, g = (v[:, 0] for v in (smax, k, a, alpha, fg, g))
|
|
413
|
+
flow = torch.empty(n, length, device=device)
|
|
414
|
+
moist = torch.empty(n, length, device=device)
|
|
415
|
+
for step in range(total):
|
|
416
|
+
wet = torch.where(wet, occ[:, step] < p11[:, 0], occ[:, step] < p01_t[:, step])
|
|
417
|
+
rain = amount[:, step] * wet
|
|
418
|
+
sf = snow_frac[:, step]
|
|
419
|
+
melt = torch.minimum(snow, melt_rate[:, step])
|
|
420
|
+
snow = snow + rain * sf - melt
|
|
421
|
+
soil = soil + rain * (1 - sf) + melt
|
|
422
|
+
soil = (soil - pet[:, step] * (soil / smax).clamp(max=1)).clamp_min(0)
|
|
423
|
+
over = torch.relu(soil - smax)
|
|
424
|
+
soil = soil - over
|
|
425
|
+
q = torch.minimum(k * smax * (soil / smax) ** a, soil)
|
|
426
|
+
soil = soil - q
|
|
427
|
+
ground = ground + fg * q - g * ground
|
|
428
|
+
route = route + alpha * ((1 - fg) * q + over - route)
|
|
429
|
+
if step >= burn:
|
|
430
|
+
flow[:, step - burn] = route + g * ground
|
|
431
|
+
moist[:, step - burn] = soil + snow
|
|
432
|
+
return torch.stack([flow + 1e-3 * smax[:, None], moist], -1)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def _thermal_bank(gen, n, length, device, burn=512):
|
|
436
|
+
total = burn + length
|
|
437
|
+
t = torch.arange(total, device=device, dtype=torch.float32)[None]
|
|
438
|
+
fine = _u(gen, (n, 1), device=device) < 0.35
|
|
439
|
+
day = torch.where(fine, 96.0, 24.0)
|
|
440
|
+
year = 365.25 * day
|
|
441
|
+
ambient = (_u(gen, (n, 1), -5.0, 25.0, device)
|
|
442
|
+
+ _u(gen, (n, 1), 0.0, 12.0, device)
|
|
443
|
+
* torch.cos(2 * math.pi * t / year + _u(gen, (n, 1), 0.0, 6.3, device))
|
|
444
|
+
+ _u(gen, (n, 1), 0.0, 6.0, device)
|
|
445
|
+
* torch.cos(2 * math.pi * t / day + _u(gen, (n, 1), 0.0, 6.3, device))
|
|
446
|
+
+ _ar1(gen, n, total, device, torch.where(fine, 0.995, 0.98),
|
|
447
|
+
_u(gen, (n, 1), 0.5, 3.0, device)))
|
|
448
|
+
hour = 2 * math.pi * t / day
|
|
449
|
+
profile = (1 + _u(gen, (n, 1), 0.0, 0.6, device)
|
|
450
|
+
* torch.cos(hour + _u(gen, (n, 1), 0.0, 6.3, device))
|
|
451
|
+
+ _u(gen, (n, 1), 0.0, 0.3, device)
|
|
452
|
+
* torch.cos(2 * hour + _u(gen, (n, 1), 0.0, 6.3, device)))
|
|
453
|
+
weekend = ((t / day).floor() % 7) >= 5
|
|
454
|
+
profile = profile * torch.where(weekend, 1 - _u(gen, (n, 1), 0.0, 0.5, device), 1.0)
|
|
455
|
+
load = (profile * torch.exp(_ar1(gen, n, total, device, torch.where(fine, 0.99, 0.95),
|
|
456
|
+
_u(gen, (n, 1), 0.05, 0.4, device)))).clamp_min(0)
|
|
457
|
+
quad = _u(gen, (n, 1), device=device) < 0.5
|
|
458
|
+
heat = torch.where(quad, load**2, load) * _u(gen, (n, 1), 0.02, 0.5, device)
|
|
459
|
+
r12 = _logu(gen, (n, 1), 0.01, 0.3, device)
|
|
460
|
+
r1a = _logu(gen, (n, 1), 0.003, 0.08, device)
|
|
461
|
+
r2a = _logu(gen, (n, 1), 0.001, 0.05, device)
|
|
462
|
+
cap = _logu(gen, (n, 1), 0.1, 1.0, device)
|
|
463
|
+
cooled = _u(gen, (n, 1), device=device) < 0.8
|
|
464
|
+
prop = _u(gen, (n, 1), device=device) < 0.3
|
|
465
|
+
setpoint = ambient.mean(1, keepdim=True) + _u(gen, (n, 1), 2.0, 15.0, device)
|
|
466
|
+
band = _u(gen, (n, 1), 0.3, 3.0, device)
|
|
467
|
+
power = (heat.mean(1, keepdim=True) + r1a * 10) * _u(gen, (n, 1), 1.2, 3.0, device) * cooled
|
|
468
|
+
t1 = ambient[:, 0] + 5
|
|
469
|
+
t2 = ambient[:, 0] + 5
|
|
470
|
+
on = torch.zeros(n, device=device, dtype=torch.bool)
|
|
471
|
+
r12, r1a, r2a, cap, setpoint, band, power, prop = (
|
|
472
|
+
v[:, 0] for v in (r12, r1a, r2a, cap, setpoint, band, power, prop))
|
|
473
|
+
temp = torch.empty(n, length, device=device)
|
|
474
|
+
energy = torch.empty(n, length, device=device)
|
|
475
|
+
for step in range(total):
|
|
476
|
+
on = (t1 > setpoint + band) | (on & (t1 > setpoint - band))
|
|
477
|
+
cool = torch.where(prop, power * ((t1 - setpoint + band) / (2 * band)).clamp(0, 1),
|
|
478
|
+
power * on)
|
|
479
|
+
amb = ambient[:, step]
|
|
480
|
+
t1, t2 = (t1 + r12 * (t2 - t1) + r1a * (amb - t1) + heat[:, step] - cool,
|
|
481
|
+
t2 + r12 * cap * (t1 - t2) + r2a * (amb - t2))
|
|
482
|
+
if step >= burn:
|
|
483
|
+
temp[:, step - burn] = t1
|
|
484
|
+
energy[:, step - burn] = cool + heat[:, step]
|
|
485
|
+
return torch.stack([temp, energy], -1)
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
def queue_family(gen, n, length, device):
|
|
489
|
+
"""Return float32 fluid-queue observables [n,length] on `device`, drawn from `gen`."""
|
|
490
|
+
t = torch.arange(length, device=device, dtype=torch.float32)[None]
|
|
491
|
+
day = torch.tensor([288.0, 96.0, 24.0], device=device)[
|
|
492
|
+
torch.randint(0, 3, (n, 1), generator=gen, device=device)]
|
|
493
|
+
hour = 2 * math.pi * (t + _u(gen, (n, 1), 0.0, 1e4, device)) / day
|
|
494
|
+
logp = (_u(gen, (n, 1), 0.0, 1.0, device) * torch.cos(hour)
|
|
495
|
+
+ _u(gen, (n, 1), 0.0, 0.4, device)
|
|
496
|
+
* torch.cos(2 * hour + _u(gen, (n, 1), 0.0, 6.3, device))
|
|
497
|
+
- _u(gen, (n, 1), 0.0, 0.6, device) * ((((t / day).floor()) % 7) >= 5)
|
|
498
|
+
+ _ar1(gen, n, length, device, _u(gen, (n, 1), 0.9, 0.999, device),
|
|
499
|
+
_u(gen, (n, 1), 0.02, 0.3, device)))
|
|
500
|
+
hazard = _logu(gen, (n, 1), 1e-3, 3e-2, device)
|
|
501
|
+
regime = ((_u(gen, (n, length), device=device) < hazard).long().cumsum(-1) % 2).bool()
|
|
502
|
+
regime = regime & (_u(gen, (n, 1), device=device) < 0.6)
|
|
503
|
+
logp = logp + torch.log(_logu(gen, (n, 1), 1.2, 3.0, device)) * regime
|
|
504
|
+
profile = torch.exp(logp - logp.mean(1, keepdim=True))
|
|
505
|
+
cap = _logu(gen, (n, 1), 5.0, 500.0, device)
|
|
506
|
+
dip = ((_u(gen, (n, length), device=device) < 5e-4).long().cumsum(-1) % 2).bool()
|
|
507
|
+
dip = dip & (_u(gen, (n, 1), device=device) < 0.3)
|
|
508
|
+
cap_t = cap * torch.where(dip, _u(gen, (n, 1), 0.3, 0.8, device), 1.0)
|
|
509
|
+
rho = _u(gen, (n, 1), 0.5, 1.1, device)
|
|
510
|
+
arrivals = torch.poisson((cap * rho * profile).clamp(0, 1e6), generator=gen)
|
|
511
|
+
s = (arrivals - cap_t).double().cumsum(-1)
|
|
512
|
+
backlog = (s - s.cummin(-1).values.clamp(max=0)).float()
|
|
513
|
+
before = torch.cat([torch.zeros(n, 1, device=device), backlog[:, :-1]], 1)
|
|
514
|
+
served = torch.minimum(before + arrivals, cap_t)
|
|
515
|
+
util = served / cap_t
|
|
516
|
+
latency = (backlog / cap_t + 1) * torch.exp(0.1 * _randn(gen, (n, length), device))
|
|
517
|
+
occ = ((arrivals + backlog) / (cap * _u(gen, (n, 1), 1.5, 3.0, device))).clamp(0.01, 0.98)
|
|
518
|
+
crit = _u(gen, (n, 1), 0.2, 0.5, device)
|
|
519
|
+
speed = (torch.where(occ < crit, 1.0, crit / (1 - crit) * (1 - occ) / occ)
|
|
520
|
+
* _u(gen, (n, 1), 40, 120, device))
|
|
521
|
+
kind = torch.randint(0, 5, (n, 1), generator=gen, device=device)
|
|
522
|
+
out = torch.where(kind == 0, util, torch.where(kind == 1, backlog, torch.where(
|
|
523
|
+
kind == 2, latency, torch.where(kind == 3, served, speed))))
|
|
524
|
+
return out
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
SIMULATORS = {"chaos": _chaos_bank, "epi": _epi_bank, "hydro": _hydro_bank,
|
|
528
|
+
"thermal": _thermal_bank}
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def _valid(x):
|
|
532
|
+
x0 = x[..., 0].float()
|
|
533
|
+
ok = torch.isfinite(x0).all(1) & (x0.abs().amax(1) < 6e4)
|
|
534
|
+
x0 = torch.where(torch.isfinite(x0), x0, 0)
|
|
535
|
+
tail = x0[:, x0.shape[1] // 2 :].std(1)
|
|
536
|
+
return ok & (tail > 0.05 * x0.std(1)) & (tail > 1e-6 * (x0.abs().mean(1) + 1e-6))
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
def _simulate(name, gen, n, device):
|
|
540
|
+
spare = n + max(1, int(n * SPARE))
|
|
541
|
+
with torch.no_grad(), torch.autocast(device_type=torch.device(device).type, enabled=False):
|
|
542
|
+
previous = torch.backends.cuda.matmul.allow_tf32
|
|
543
|
+
torch.backends.cuda.matmul.allow_tf32 = False
|
|
544
|
+
try:
|
|
545
|
+
x = SIMULATORS[name](gen, spare, BANK_LENGTH, device)
|
|
546
|
+
finally:
|
|
547
|
+
torch.backends.cuda.matmul.allow_tf32 = previous
|
|
548
|
+
ok = _valid(x)
|
|
549
|
+
order = torch.argsort((~ok).to(torch.int8), stable=True)[:n]
|
|
550
|
+
x = x[order].float()
|
|
551
|
+
x = torch.where(torch.isfinite(x), x, 0).clamp(-6e4, 6e4)
|
|
552
|
+
rms = x.pow(2).mean(1, keepdim=True).sqrt().clamp_min(1e-12)
|
|
553
|
+
x = (x / rms).clamp(-6e4, 6e4).half()
|
|
554
|
+
return x
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
def _bank(name, gen, device):
|
|
558
|
+
key = (name, torch.device(device).type) # cuda and cuda:0 must share one bank.
|
|
559
|
+
state = _STATE.get(key)
|
|
560
|
+
if state is None:
|
|
561
|
+
state = _STATE[key] = {"bank": _simulate(name, gen, BANK[name], device), "calls": 0,
|
|
562
|
+
"next": 0}
|
|
563
|
+
state["calls"] += 1
|
|
564
|
+
if REFRESH > 0 and state["calls"] % REFRESH == 0:
|
|
565
|
+
bank = state["bank"]
|
|
566
|
+
part = max(1, len(bank) // PART)
|
|
567
|
+
lo = state["next"] * part
|
|
568
|
+
bank[lo : lo + part] = _simulate(name, gen, part, device)
|
|
569
|
+
state["next"] = (state["next"] + 1) % PART
|
|
570
|
+
return state["bank"]
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
def _crop(gen, bank, n, length, device):
|
|
574
|
+
rows, size, channels = bank.shape
|
|
575
|
+
if length > 2 * size:
|
|
576
|
+
raise ValueError(f"{length}-step series need mech_length >= {(length + 1) // 2} (the bank "
|
|
577
|
+
f"has {size} steps)")
|
|
578
|
+
span = min(length, size)
|
|
579
|
+
idx = torch.randint(0, rows, (n, 1), generator=gen, device=device)
|
|
580
|
+
ch = torch.randint(0, channels, (n, 1), generator=gen, device=device)
|
|
581
|
+
max_stride = max(1, min(4, size // span))
|
|
582
|
+
stride = torch.randint(1, max_stride + 1, (n, 1), generator=gen, device=device)
|
|
583
|
+
pool = _u(gen, (n, 1), device=device) < 0.5
|
|
584
|
+
room = (size - stride * span).clamp_min(0)
|
|
585
|
+
offset = (_u(gen, (n, 1), device=device) * (room + 1)).long().clamp(max=size - 1)
|
|
586
|
+
pos = offset + stride * torch.arange(span, device=device)[None]
|
|
587
|
+
x = torch.zeros(n, span, device=device)
|
|
588
|
+
for j in range(max_stride):
|
|
589
|
+
use = torch.where(pool, (j < stride).float() / stride, float(j == 0))
|
|
590
|
+
p = (pos + j).clamp(max=size - 1)
|
|
591
|
+
x = x + use * bank[idx, p, ch].float()
|
|
592
|
+
if span < length:
|
|
593
|
+
x = torch.cat([x.flip(-1), x], -1)[:, -length:]
|
|
594
|
+
return x
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def _observe(gen, x, nonlinear):
|
|
598
|
+
n, length = x.shape
|
|
599
|
+
device = x.device
|
|
600
|
+
z = _standardise(x)
|
|
601
|
+
if nonlinear > 0:
|
|
602
|
+
kind = torch.randint(0, 3, (n, 1), generator=gen, device=device)
|
|
603
|
+
a = _u(gen, (n, 1), 0.3, 1.0, device)
|
|
604
|
+
y = torch.where(kind == 0, torch.exp(a * z),
|
|
605
|
+
torch.where(kind == 1, torch.tanh(a * z),
|
|
606
|
+
torch.nn.functional.softplus(2 * a * z)))
|
|
607
|
+
z = torch.where(_u(gen, (n, 1), device=device) < nonlinear, _standardise(y), z)
|
|
608
|
+
level = _u(gen, (n, 1), 0.0, 0.1, device) * (_u(gen, (n, 1), device=device) < 0.7)
|
|
609
|
+
eps = _randn(gen, (n, length), device)
|
|
610
|
+
mult = _u(gen, (n, 1), device=device) < 0.3
|
|
611
|
+
base = x / x.std(1, keepdim=True).clamp_min(1e-6)
|
|
612
|
+
out = torch.where(mult, base * (1 + level * eps), z + level * eps)
|
|
613
|
+
t = torch.linspace(0, 1, length, device=device)[None]
|
|
614
|
+
trend = (_u(gen, (n, 1), -1.0, 1.0, device) * t
|
|
615
|
+
+ _u(gen, (n, 1), 0.0, 0.5, device)
|
|
616
|
+
* torch.sin(2 * math.pi * t * _u(gen, (n, 1), 0.2, 1.5, device)))
|
|
617
|
+
return out + trend * (_u(gen, (n, 1), device=device) < 0.2)
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
def _maker(name):
|
|
621
|
+
|
|
622
|
+
def make(gen, n, length, device):
|
|
623
|
+
"""Draw float32 [n,length] family series; cached banks support length <= 2 * BANK_LENGTH."""
|
|
624
|
+
if name == "queue":
|
|
625
|
+
x = queue_family(gen, n, length, device)
|
|
626
|
+
x = _observe(gen, torch.where(torch.isfinite(x), x, 0), 0.0)
|
|
627
|
+
else:
|
|
628
|
+
x = _observe(gen, _crop(gen, _bank(name, gen, device), n, length, device),
|
|
629
|
+
0.25 if name == "chaos" else 0.0)
|
|
630
|
+
return torch.where(torch.isfinite(x), x, 0)
|
|
631
|
+
|
|
632
|
+
return make
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
NAMES = ("chaos", "epi", "hydro", "queue", "thermal")
|
|
636
|
+
MAKERS = {k: _maker(k) for k in NAMES}
|