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/causal.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""Nonlinear causal DAGs, beat layers, AM/FM waves and multiplicative calendar priors."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
|
|
7
|
+
from simons.synth.base import (
|
|
8
|
+
PERIOD_BANK,
|
|
9
|
+
_bank,
|
|
10
|
+
_categorical,
|
|
11
|
+
_choice,
|
|
12
|
+
_logu,
|
|
13
|
+
_periods,
|
|
14
|
+
_randn,
|
|
15
|
+
_spectral,
|
|
16
|
+
_standardise,
|
|
17
|
+
_u,
|
|
18
|
+
kernel_family,
|
|
19
|
+
)
|
|
20
|
+
from simons.synth.nonstat import _ou_scan, _trend
|
|
21
|
+
|
|
22
|
+
MAX_NODES = 8
|
|
23
|
+
MAX_ROOTS = 4
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _slow(gen, n, length, device, lo=0.02, hi=0.5):
|
|
27
|
+
scale = _logu(gen, (n, 1), lo, hi, device) * length
|
|
28
|
+
return _spectral(gen, length, device, lambda w: torch.exp(-((w[None] * scale) ** 2) / 2))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def cauker_family(gen, n, length, device):
|
|
32
|
+
"""Return float32 causal-DAG series [n,length]; replace constant outputs with a root."""
|
|
33
|
+
t = torch.arange(length, device=device, dtype=torch.float32)[None] / length
|
|
34
|
+
roots = _standardise(kernel_family(gen, MAX_ROOTS * n, length, device, max_components=3))
|
|
35
|
+
roots = roots.view(MAX_ROOTS, n, length)
|
|
36
|
+
m = MAX_ROOTS * n
|
|
37
|
+
kind = torch.randint(0, 3, (m, 1), generator=gen, device=device)
|
|
38
|
+
use = _u(gen, (m, 1), device=device) < 0.5
|
|
39
|
+
a, b = _randn(gen, (m, 1), device), _randn(gen, (m, 1), device)
|
|
40
|
+
linear = a * 2 * (t - 0.5) + 0.5 * b
|
|
41
|
+
rate = _u(gen, (m, 1), -2.0, 2.0, device)
|
|
42
|
+
expo = a * torch.exp(rate * t)
|
|
43
|
+
expo = expo - expo.mean(-1, keepdim=True)
|
|
44
|
+
impulses = ((_u(gen, (m, length), device=device) < 0.002)
|
|
45
|
+
* _u(gen, (m, length), -5.0, 5.0, device))
|
|
46
|
+
mean = torch.where(kind == 0, linear, torch.where(kind == 1, expo, impulses))
|
|
47
|
+
roots = roots + (use * mean).view(MAX_ROOTS, n, length)
|
|
48
|
+
count = torch.randint(2, MAX_ROOTS + 1, (n, 1), generator=gen, device=device)
|
|
49
|
+
nodes = []
|
|
50
|
+
for v in range(MAX_NODES):
|
|
51
|
+
root = roots[v] if v < MAX_ROOTS else None
|
|
52
|
+
if v < 2:
|
|
53
|
+
nodes.append(root)
|
|
54
|
+
continue
|
|
55
|
+
stack = torch.stack(nodes, 1) # [n, v, L]
|
|
56
|
+
parents = torch.randint(1, 4, (n, 1), generator=gen, device=device)
|
|
57
|
+
index = (_u(gen, (n, 3), device=device) * v).long().clamp_max(v - 1)
|
|
58
|
+
valid = (torch.arange(3, device=device)[None] < parents).float()
|
|
59
|
+
weight = _randn(gen, (n, 3), device) * valid / parents.float().sqrt()
|
|
60
|
+
picked = stack.gather(1, index[..., None].expand(n, 3, length))
|
|
61
|
+
u = (weight[..., None] * picked).sum(1) + _randn(gen, (n, 1), device)
|
|
62
|
+
act = _categorical(gen, (0.2, 0.25, 0.15, 0.2, 0.15, 0.05), n, device)[:, None]
|
|
63
|
+
slope = _u(gen, (n, 1), 0.01, 0.3, device)
|
|
64
|
+
k = _logu(gen, (n, 1), 0.5, 3.0, device)
|
|
65
|
+
omega = _logu(gen, (n, 1), 0.5, 3.0, device)
|
|
66
|
+
modulus = _u(gen, (n, 1), 1.0, 5.0, device)
|
|
67
|
+
z = torch.where(act == 0, u,
|
|
68
|
+
torch.where(act == 1, u.clamp_min(0),
|
|
69
|
+
torch.where(act == 2, torch.where(u > 0, u, slope * u),
|
|
70
|
+
torch.where(act == 3, torch.sigmoid(k * u),
|
|
71
|
+
torch.where(act == 4, torch.sin(omega * u), torch.remainder(u, modulus))))))
|
|
72
|
+
z = _standardise(z)
|
|
73
|
+
if root is not None:
|
|
74
|
+
z = torch.where(v < count, root, z)
|
|
75
|
+
nodes.append(z)
|
|
76
|
+
stack = torch.stack(nodes, 1)
|
|
77
|
+
pick = count + (_u(gen, (n, 1), device=device) * (MAX_NODES - count)).long()
|
|
78
|
+
pick = pick.clamp_max(MAX_NODES - 1)
|
|
79
|
+
out = stack.gather(1, pick[..., None].expand(n, 1, length))[:, 0]
|
|
80
|
+
out = torch.where(out.std(-1, keepdim=True) < 1e-3, roots[0], out)
|
|
81
|
+
noise = _logu(gen, (n, 1), 0.003, 0.1, device) * (_u(gen, (n, 1), device=device) < 0.5)
|
|
82
|
+
return out + noise * _randn(gen, (n, length), device)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def rhythm_family(gen, n, length, device, layers=5):
|
|
86
|
+
"""Return float32 polyrhythms [n,length]; without drift/noise, pre-beat values are zero."""
|
|
87
|
+
tf = torch.arange(length, device=device, dtype=torch.float32)[None]
|
|
88
|
+
bank = _choice(gen, PERIOD_BANK, (n, 1), device)
|
|
89
|
+
period = torch.where(_u(gen, (n, 1), device=device) < 0.6, bank,
|
|
90
|
+
_logu(gen, (n, 1), 4.0, 400.0, device))
|
|
91
|
+
count = torch.randint(3, layers + 1, (n, 1), generator=gen, device=device)
|
|
92
|
+
m = n * layers
|
|
93
|
+
p = period.repeat(layers, 1) # [layers*n, 1], layer-major
|
|
94
|
+
k = _choice(gen, (1.0, 2.0, 3.0, 4.0, 6.0, 8.0), (m, 1), device)
|
|
95
|
+
spacing = (p / k).clamp_min(2.0)
|
|
96
|
+
offset = _u(gen, (m, 1), device=device) * spacing
|
|
97
|
+
beat = torch.floor((tf - offset) / spacing)
|
|
98
|
+
fired = beat > torch.floor((tf - 1 - offset) / spacing)
|
|
99
|
+
q = _u(gen, (m, 1), 0.3, 1.0, device)
|
|
100
|
+
fired = fired & (_u(gen, (m, length), device=device) < q)
|
|
101
|
+
amp = _logu(gen, (m, 1), 0.2, 5.0, device)
|
|
102
|
+
jitter = torch.exp(0.25 * _randn(gen, (m, length), device)
|
|
103
|
+
* (_u(gen, (m, 1), device=device) < 0.5))
|
|
104
|
+
impulse = fired * amp * jitter
|
|
105
|
+
tau = torch.exp(_u(gen, (m, 1), 0.0, 1.0, device) * torch.log(spacing.clamp_min(1.0)))
|
|
106
|
+
tau = tau.clamp_min(1.0) # e^(-1/tau) >= e^-1 keeps the chunked scan in range.
|
|
107
|
+
decay = torch.exp(-1.0 / tau).expand(m, length).double()
|
|
108
|
+
env = _ou_scan(decay, impulse.double(), torch.zeros(m, device=device, dtype=torch.float64))
|
|
109
|
+
env = env.float().view(layers, n, length)
|
|
110
|
+
active = (torch.arange(layers, device=device)[:, None, None] < count[None])
|
|
111
|
+
y = (env * active).sum(0)
|
|
112
|
+
carrier = 1 + 0.5 * torch.sin(2 * math.pi * tf
|
|
113
|
+
/ (period * _choice(gen, (2.0, 4.0, 7.0, 12.0), (n, 1), device))
|
|
114
|
+
+ _u(gen, (n, 1), 0, 2 * math.pi, device))
|
|
115
|
+
y = torch.where(_u(gen, (n, 1), device=device) < 0.3, y * carrier, y)
|
|
116
|
+
scale = y.std(-1, keepdim=True).clamp_min(1e-3)
|
|
117
|
+
drift = torch.where(_u(gen, (n, 1), device=device) < 0.5, _trend(gen, n, length, device),
|
|
118
|
+
_slow(gen, n, length, device))
|
|
119
|
+
y = (y + (_u(gen, (n, 1), device=device) < 0.4) * _u(gen, (n, 1), 0.1, 1.0, device) * scale
|
|
120
|
+
* drift)
|
|
121
|
+
noisy = _u(gen, (n, 1), device=device) < 0.6
|
|
122
|
+
y = (y + noisy * _logu(gen, (n, 1), 0.003, 0.2, device) * scale
|
|
123
|
+
* _randn(gen, (n, length), device))
|
|
124
|
+
return y
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def wave_family(gen, n, length, device, components=3):
|
|
128
|
+
"""Return float32 AM/FM waveform mixtures [n,length] on `device`, drawn from `gen`."""
|
|
129
|
+
m = n * components
|
|
130
|
+
tf = torch.arange(length, device=device, dtype=torch.float32)[None]
|
|
131
|
+
period = _periods(gen, m, device)[:, None].clamp_min(3.0)
|
|
132
|
+
delta = _u(gen, (m, 1), 0.0, 0.3, device)
|
|
133
|
+
eps = _u(gen, (m, 1), 0.0, 0.5, device)
|
|
134
|
+
freq = torch.exp(delta * _slow(gen, m, length, device)) / period
|
|
135
|
+
cycles = torch.remainder(freq.cumsum(-1) + _u(gen, (m, 1), device=device), 1.0)
|
|
136
|
+
phase = 2 * math.pi * cycles
|
|
137
|
+
shape = torch.randint(0, 4, (m, 1), generator=gen, device=device)
|
|
138
|
+
kappa = _logu(gen, (m, 1), 1.0, 10.0, device)
|
|
139
|
+
sine = torch.sin(phase)
|
|
140
|
+
square = torch.tanh(kappa * sine) / torch.tanh(kappa)
|
|
141
|
+
triangle = 2 / math.pi * torch.asin(sine.clamp(-1, 1))
|
|
142
|
+
saw = 2 * cycles - 1
|
|
143
|
+
wave = torch.where(shape == 0, sine, torch.where(shape == 1, square,
|
|
144
|
+
torch.where(shape == 2, triangle, saw)))
|
|
145
|
+
amplitude = (_logu(gen, (m, 1), 0.2, 2.0, device)
|
|
146
|
+
* torch.exp(eps * _slow(gen, m, length, device)))
|
|
147
|
+
count = torch.randint(1, components + 1, (n, 1), generator=gen, device=device)
|
|
148
|
+
active = (torch.arange(components, device=device)[:, None, None] < count[None])
|
|
149
|
+
y = ((amplitude * wave).view(components, n, length) * active).sum(0)
|
|
150
|
+
trend = _randn(gen, (n, 1), device) * (tf / length - 0.5) * 2
|
|
151
|
+
y = y + (_u(gen, (n, 1), device=device) < 0.5) * trend
|
|
152
|
+
return y + _logu(gen, (n, 1), 0.005, 0.3, device) * _randn(gen, (n, length), device)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
_GRANULARITY = ((24.0, 168.0), (7.0, 365.25), (52.18, 0.0), (12.0, 0.0), (24.0, 0.0), (7.0, 0.0))
|
|
156
|
+
_FLAT = tuple(x for pair in _GRANULARITY for x in pair)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def fpfn_family(gen, n, length, device, harmonics=3):
|
|
160
|
+
"""Return float32 positive trend * seasonal * (1 + noise) series [n,length]."""
|
|
161
|
+
tf = torch.arange(length, device=device, dtype=torch.float32)[None]
|
|
162
|
+
s = tf / length
|
|
163
|
+
g = torch.randint(0, len(_GRANULARITY), (n,), generator=gen, device=device)
|
|
164
|
+
periods = _bank(_FLAT, device).view(-1, 2)[g] # [n, 2]
|
|
165
|
+
m_l = _randn(gen, (n, 1), device).clamp_min(-0.9)
|
|
166
|
+
log_me = _randn(gen, (n, 1), device) * 0.7
|
|
167
|
+
trend = (1 + m_l * s) * torch.exp(log_me * s)
|
|
168
|
+
trend = torch.where(_u(gen, (n, 1), device=device) < 0.2, torch.ones_like(trend), trend)
|
|
169
|
+
season = torch.ones((n, length), device=device)
|
|
170
|
+
start = _u(gen, (n, 1), 0.0, 1000.0, device)
|
|
171
|
+
for j in range(2):
|
|
172
|
+
p = periods[:, j : j + 1]
|
|
173
|
+
used = p > 0
|
|
174
|
+
p = p.clamp_min(1.0)
|
|
175
|
+
level = _u(gen, (n, 1), 0.0, 0.4, device)
|
|
176
|
+
acc = torch.zeros((n, length), device=device)
|
|
177
|
+
for f in range(1, harmonics + 1):
|
|
178
|
+
a = _randn(gen, (n, 1), device) * level / f
|
|
179
|
+
b = _randn(gen, (n, 1), device) * level / f
|
|
180
|
+
arg = 2 * math.pi * f * torch.remainder((tf + start) / p, 1.0)
|
|
181
|
+
acc = acc + a * torch.sin(arg) + b * torch.cos(arg)
|
|
182
|
+
season = season * torch.where(used, (1 + acc).clamp_min(0.05), 1.0)
|
|
183
|
+
shape = _u(gen, (n, 1), 1.0, 5.0, device)
|
|
184
|
+
u = _u(gen, (n, length), 1e-6, 1 - 1e-6, device)
|
|
185
|
+
weibull = (-torch.log1p(-u)) ** (1 / shape)
|
|
186
|
+
mean = torch.exp(torch.lgamma(1 + 1 / shape))
|
|
187
|
+
level = _logu(gen, (n, 1), 0.01, 0.3, device)
|
|
188
|
+
noise = (1 + level * (weibull - mean)).clamp_min(0.01)
|
|
189
|
+
return trend * season * noise
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
MAKERS = {"cauker": cauker_family, "rhythm": rhythm_family, "wave": wave_family,
|
|
193
|
+
"fpfn": fpfn_family}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Synthetic covariates and their effects on corpus or generated targets."""
|