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.
Files changed (49) hide show
  1. simons/__init__.py +10 -0
  2. simons/config.py +82 -0
  3. simons/covariates.py +338 -0
  4. simons/forecast.py +578 -0
  5. simons/model.py +696 -0
  6. simons/periodicity.py +188 -0
  7. simons/predictor.py +56 -0
  8. simons/synth/__init__.py +5 -0
  9. simons/synth/augment.py +143 -0
  10. simons/synth/base.py +448 -0
  11. simons/synth/calendar.py +157 -0
  12. simons/synth/causal.py +193 -0
  13. simons/synth/covariates/__init__.py +1 -0
  14. simons/synth/covariates/classical.py +637 -0
  15. simons/synth/covariates/effects.py +155 -0
  16. simons/synth/covariates/energy.py +528 -0
  17. simons/synth/covariates/families.py +296 -0
  18. simons/synth/covariates/generic.py +342 -0
  19. simons/synth/heavy.py +234 -0
  20. simons/synth/mech.py +636 -0
  21. simons/synth/multi.py +274 -0
  22. simons/synth/nonstat.py +204 -0
  23. simons/synth/observe.py +117 -0
  24. simons/training/__init__.py +1 -0
  25. simons/training/__main__.py +77 -0
  26. simons/training/augment.py +125 -0
  27. simons/training/clusters.py +215 -0
  28. simons/training/config.py +321 -0
  29. simons/training/covariates/__init__.py +1 -0
  30. simons/training/covariates/roles.py +370 -0
  31. simons/training/covariates/stream.py +243 -0
  32. simons/training/covariates/windows.py +239 -0
  33. simons/training/data.py +319 -0
  34. simons/training/distributed.py +124 -0
  35. simons/training/mixture.py +140 -0
  36. simons/training/network.py +115 -0
  37. simons/training/objective.py +193 -0
  38. simons/training/optim.py +164 -0
  39. simons/training/packing.py +94 -0
  40. simons/training/schedule.py +117 -0
  41. simons/training/soup.py +93 -0
  42. simons/training/stitching.py +50 -0
  43. simons/training/trainer.py +571 -0
  44. simons/training/warp.py +144 -0
  45. simons_forecasting-0.1.3.dist-info/METADATA +156 -0
  46. simons_forecasting-0.1.3.dist-info/RECORD +49 -0
  47. simons_forecasting-0.1.3.dist-info/WHEEL +4 -0
  48. simons_forecasting-0.1.3.dist-info/licenses/LICENSE +202 -0
  49. simons_forecasting-0.1.3.dist-info/licenses/NOTICE +57 -0
simons/synth/heavy.py ADDED
@@ -0,0 +1,234 @@
1
+ """Heavy-tailed walks, innovations ETS and intermittent pulses, batched on device."""
2
+
3
+ import torch
4
+
5
+ from simons.synth.base import (
6
+ PERIOD_BANK,
7
+ _categorical,
8
+ _choice,
9
+ _logu,
10
+ _periodic,
11
+ _randn,
12
+ _seasonal_cumsum,
13
+ _smooth,
14
+ _standardise,
15
+ _u,
16
+ )
17
+ from simons.synth.nonstat import _laplace, _ou_scan
18
+
19
+ ETS_BANK = (4, 7, 12, 24, 48, 52, 96, 168, 288, 336, 365, 672)
20
+ ETS_MULT = (7, 4, 12, 52)
21
+ CLIP = 50.0
22
+
23
+
24
+ def _gamma(alpha, gen):
25
+ """Draw Gamma(alpha,1) with fixed rejection rounds, boosting alpha below 1."""
26
+ small = alpha < 1
27
+ a = torch.where(small, alpha + 1, alpha)
28
+ d = a - 1.0 / 3.0
29
+ c = 1.0 / torch.sqrt(9.0 * d)
30
+ out = d.clone()
31
+ done = torch.zeros_like(a, dtype=torch.bool)
32
+ for _ in range(6):
33
+ z = torch.randn(a.shape, generator=gen, device=a.device)
34
+ u = torch.rand(a.shape, generator=gen, device=a.device)
35
+ v = (1 + c * z) ** 3
36
+ ok = (v > 0) & (torch.log(u.clamp_min(1e-12))
37
+ < 0.5 * z**2 + d - d * v + d * torch.log(v.clamp_min(1e-12)))
38
+ out = torch.where(ok & ~done, d * v, out)
39
+ done |= ok
40
+ boost = torch.rand(a.shape, generator=gen, device=a.device).clamp_min(1e-12) ** (
41
+ 1 / alpha.clamp_min(1e-3))
42
+ return torch.where(small, out * boost, out)
43
+
44
+
45
+ def _student(gen, shape, nu, device):
46
+ """Draw unit-variance Student-t values [n,L] with per-row degrees of freedom nu [n,1] > 2."""
47
+ half = (nu / 2).expand(shape).contiguous()
48
+ g = _gamma(half, gen) / half
49
+ z = _randn(gen, shape, device) / g.clamp_min(1e-4).sqrt()
50
+ return z * ((nu - 2) / nu).sqrt()
51
+
52
+
53
+ def _pareto(gen, shape, alpha, device):
54
+ """Draw Pareto(alpha) on [1,inf), flooring uniforms to bound values near 1e4."""
55
+ u = _u(gen, shape, device=device).clamp_min(1e-6)
56
+ return u ** (-1 / alpha)
57
+
58
+
59
+ def _segments(gen, n, length, device, max_breaks):
60
+ """Return segment indices [n,L] from random break times."""
61
+ count = torch.randint(0, max_breaks + 1, (n, 1), generator=gen, device=device)
62
+ positions = torch.randint(1, length, (n, max_breaks), generator=gen, device=device)
63
+ valid = torch.arange(max_breaks, device=device)[None] < count
64
+ positions = torch.where(valid, positions, 0)
65
+ marks = torch.zeros((n, length), device=device).scatter_(1, positions, 1.0)
66
+ marks[:, 0] = 1.0
67
+ return marks.long().cumsum(-1) - 1
68
+
69
+
70
+ def _finish(x):
71
+ x = torch.where(torch.isfinite(x), x, 0.0)
72
+ return _standardise(x).clamp(-CLIP, CLIP)
73
+
74
+
75
+ def levy_family(gen, n, length, device):
76
+ """Return float32 heavy-tailed random walks [n,length], standardized and clipped to +/-CLIP."""
77
+ use = lambda p: _u(gen, (n, 1), device=device) < p # noqa: E731
78
+ nu = _u(gen, (n, 1), 2.5, 8.0, device)
79
+ eps = _student(gen, (n, length), nu, device)
80
+ persist = 1 - _logu(gen, (n, 1), 1e-3, 0.05, device)
81
+ sv_sd = _u(gen, (n, 1), 0.2, 1.0, device)
82
+ sv_drive = sv_sd * (1 - persist**2).sqrt() * _randn(gen, (n, length), device)
83
+ garch = use(0.3)
84
+ beta = _u(gen, (n, 1), 0.9, 0.995, device)
85
+ alpha = _u(gen, (n, 1), 0.05, 0.25, device)
86
+ lagged = torch.cat([torch.zeros_like(eps[:, :1]), eps[:, :-1]], -1)
87
+ g_drive = alpha * (lagged.abs() - lagged.abs().mean(-1, keepdim=True))
88
+ a = torch.where(garch, beta, persist).expand(n, length).double()
89
+ drive = torch.where(garch, g_drive, sv_drive).double()
90
+ h0 = (sv_sd * _randn(gen, (n, 1), device))[:, 0].double() * (~garch[:, 0])
91
+ log_sigma = _ou_scan(a, drive, h0).float().clamp(-4, 4)
92
+ sigma = torch.exp(log_sigma - log_sigma.mean(-1, keepdim=True))
93
+ seg = _segments(gen, n, length, device, 3)
94
+ scale = _logu(gen, (n, 1), 0.01, 0.5, device) * use(0.85)
95
+ rho = _u(gen, (n, 1), -0.5, 1.0, device)
96
+ mu = [_randn(gen, (n, 1), device) * scale]
97
+ for _ in range(3):
98
+ mu.append(rho * mu[-1] + _randn(gen, (n, 1), device) * scale)
99
+ drift = torch.cat(mu, -1).gather(1, seg)
100
+ rate = _logu(gen, (n, 1), 1e-4, 1e-2, device) * use(0.6)
101
+ hits = _u(gen, (n, length), device=device) < rate
102
+ size = _logu(gen, (n, 1), 2.0, 20.0, device)
103
+ tail = _u(gen, (n, 1), 1.5, 3.0, device)
104
+ sign = torch.where(_u(gen, (n, length), device=device) < 0.5, -1.0, 1.0)
105
+ pareto = sign * _pareto(gen, (n, length), tail, device).clamp_max(100.0)
106
+ magnitude = torch.where(use(0.5), _laplace(gen, (n, length), device), pareto)
107
+ jumps = hits * size * magnitude
108
+ increments = drift + sigma * eps + jumps
109
+ revert = use(0.3)
110
+ a_mr = torch.where(revert, _u(gen, (n, 1), 0.98, 1.0, device), 1.0).expand(n, length)
111
+ y = _ou_scan(a_mr.double(), increments.double(),
112
+ torch.zeros(n, device=device, dtype=torch.float64)).float()
113
+ unit = sigma.mean(-1, keepdim=True)
114
+ y = (y + use(0.3) * _logu(gen, (n, 1), 0.1, 2.0, device) * unit
115
+ * _randn(gen, (n, length), device))
116
+ period = _choice(gen, PERIOD_BANK, (n,), device)
117
+ y = (y + use(0.5) * _logu(gen, (n, 1), 0.3, 10.0, device) * unit
118
+ * _periodic(gen, period, length, device))
119
+ logspace = use(0.3)
120
+ span = _u(gen, (n, 1), 0.2, 1.5, device)
121
+ z = _standardise(torch.where(torch.isfinite(y), y, 0.0))
122
+ y = torch.where(logspace, torch.exp((span * z).clamp(-20, 20)), y)
123
+ return _finish(y)
124
+
125
+
126
+ def ets_family(gen, n, length, device):
127
+ """Return float32 innovations ETS paths [n,length], standardized and clipped to +/-CLIP."""
128
+ use = lambda p: _u(gen, (n, 1), device=device) < p # noqa: E731
129
+ sigma = _logu(gen, (n, 1), 0.005, 0.1, device)
130
+ heavy = use(0.3)
131
+ nu = _u(gen, (n, 1), 3.0, 10.0, device)
132
+ e = torch.where(heavy, _student(gen, (n, length), nu, device), _randn(gen, (n, length), device))
133
+ e = sigma * e
134
+ alpha = _u(gen, (n, 1), 0.02, 0.5, device)
135
+ beta = alpha * _u(gen, (n, 1), 0.01, 0.3, device)
136
+ gamma = _u(gen, (n, 1), 0.001, 0.3, device) * (1 - alpha)
137
+ trend = _categorical(gen, (0.35, 0.25, 0.4), n, device)[:, None]
138
+ phi = torch.where(trend == 2, _u(gen, (n, 1), 0.8, 0.98, device), 1.0)
139
+ beta = torch.where(trend == 0, 0.0, beta)
140
+ b0 = _randn(gen, (n, 1), device) * sigma * _logu(gen, (n, 1), 0.01, 1.0, device)
141
+ b0 = torch.where(trend == 0, 0.0, b0)
142
+ b = _ou_scan(phi.expand(n, length).double(), (beta * e).double(), b0[:, 0].double()).float()
143
+ b_prev = torch.cat([b0, b[:, :-1]], -1)
144
+ level = (phi * b_prev + alpha * e).cumsum(-1)
145
+ seasonal = _categorical(gen, (0.3, 0.7), n, device)[:, None] > 0
146
+ m1 = _choice(gen, ETS_BANK, (n,), device)
147
+ m2 = (m1 * _choice(gen, ETS_MULT, (n,), device))
148
+ m2 = torch.where(m2 > 700, m1, m2)
149
+ second = use(0.35) & (m2 != m1)[:, None]
150
+ amp1 = _logu(gen, (n, 1), 0.02, 0.6, device)
151
+ amp2 = _logu(gen, (n, 1), 0.02, 0.4, device)
152
+ share = torch.where(second, _u(gen, (n, 1), 0.3, 0.7, device), 1.0)
153
+ s1 = amp1 * _periodic(gen, m1, length, device) + _seasonal_cumsum(
154
+ share * gamma * e, m1.long(), seasonal[:, 0], ETS_BANK)
155
+ s2 = amp2 * _periodic(gen, m2, length, device) + _seasonal_cumsum(
156
+ (1 - share) * gamma * e, m2.long(), (seasonal & second)[:, 0], ETS_BANK)
157
+ s = seasonal * (s1 + second * s2)
158
+ # Remove the current level innovation before forming the observation.
159
+ y = level - alpha * e + s + e
160
+ mult = use(0.45)
161
+ span = _logu(gen, (n, 1), 0.1, 1.0, device)
162
+ log_y = span * _standardise(torch.where(torch.isfinite(y), y, 0.0))
163
+ y = torch.where(mult, torch.exp(log_y.clamp(-20, 20)), y)
164
+ return _finish(y)
165
+
166
+
167
+ def _occurrence(gen, n, length, device):
168
+ """Draw a two-state Markov on/off chain as alternating geometric runs [n,L]."""
169
+ mean_on = _logu(gen, (n, 1), 1.0, 30.0, device)
170
+ mean_off = _logu(gen, (n, 1), 1.0, 300.0, device)
171
+ k = torch.arange(length, device=device)[None]
172
+ start_on = torch.randint(0, 2, (n, 1), generator=gen, device=device)
173
+ is_on = (k + start_on) % 2 == 0
174
+ mean = torch.where(is_on, mean_on, mean_off)
175
+ p_end = (1 / mean).clamp(1e-4, 1.0)
176
+ u = _u(gen, (n, length), device=device).clamp(1e-7, 1 - 1e-7)
177
+ duration = torch.ceil(torch.log(u) / torch.log1p(-p_end.clamp_max(1 - 1e-6))).clamp(1, length)
178
+ duration = torch.where(p_end >= 1.0, 1.0, duration)
179
+ ends = duration.cumsum(-1).clamp_max(length).long()
180
+ marks = torch.zeros((n, length + 1), device=device).scatter_add_(
181
+ 1, ends, torch.ones_like(duration))[:, :length]
182
+ run = marks.cumsum(-1).long()
183
+ return (run + start_on) % 2 == 0
184
+
185
+
186
+ def pulse_family(gen, n, length, device):
187
+ """Return float32 heavy-tailed pulses [n,length], standardized and clipped to +/-CLIP."""
188
+ use = lambda p: _u(gen, (n, 1), device=device) < p # noqa: E731
189
+ t = torch.arange(length, device=device, dtype=torch.float32)[None]
190
+ on = _occurrence(gen, n, length, device)
191
+ period = _choice(gen, PERIOD_BANK, (n,), device)
192
+ logit = _u(gen, (n, 1), -2.0, 2.0, device) + use(0.5) * _u(gen, (n, 1), 0.5, 3.0, device) * \
193
+ _periodic(gen, period, length, device)
194
+ occur = on & (_u(gen, (n, length), device=device) < torch.sigmoid(logit))
195
+ kind = _categorical(gen, (0.4, 0.3, 0.3), n, device)[:, None]
196
+ shape = _logu(gen, (n, 1), 0.3, 3.0, device)
197
+ gam = _gamma(shape.expand(n, length).contiguous(), gen) / shape
198
+ logn = torch.exp(_u(gen, (n, 1), 0.3, 1.5, device) * _randn(gen, (n, length), device))
199
+ par = _pareto(gen, (n, length), _u(gen, (n, 1), 1.5, 3.0, device), device).clamp_max(1e3)
200
+ amount = torch.where(kind == 0, gam, torch.where(kind == 1, logn, par))
201
+ envelope = torch.exp(use(0.5) * _u(gen, (n, 1), 0.2, 1.0, device)
202
+ * _smooth(gen, n, length, device))
203
+ x = occur * amount * envelope
204
+ sub = _categorical(gen, (0.45, 0.35, 0.2), n, device)[:, None]
205
+ lam = _logu(gen, (n, 1), 0.3, 20.0, device)
206
+ counts = torch.poisson(x * lam, generator=gen)
207
+ raw = torch.where(use(0.35), counts, x)
208
+ a1 = _u(gen, (n, 1), 0.5, 0.995, device)
209
+ a2 = _u(gen, (n, 1), 0.5, 0.995, device)
210
+ zero = torch.zeros(n, device=device, dtype=torch.float64)
211
+ q1 = _ou_scan(a1.expand(n, length).double(), ((1 - a1) * x).double(), zero)
212
+ q2 = _ou_scan(a2.expand(n, length).double(), ((1 - a2) * q1.float()).double(), zero)
213
+ both = use(0.5)
214
+ w = _u(gen, (n, 1), 0.3, 0.9, device)
215
+ q = torch.where(both, (w * q1.float() + (1 - w) * q2.float()), q1.float())
216
+ mean_q = q.mean(-1, keepdim=True)
217
+ base = mean_q * _logu(gen, (n, 1), 0.01, 2.0, device) * (
218
+ 1 + use(0.5) * 0.5 * _periodic(gen, _choice(gen, PERIOD_BANK, (n,), device), length, device)
219
+ ).clamp_min(0)
220
+ reservoir = (q + base + mean_q * _logu(gen, (n, 1), 1e-3, 0.05, device)
221
+ * _randn(gen, (n, length), device))
222
+ total = x.cumsum(-1)
223
+ exclusive = total - x
224
+ reset = _choice(gen, PERIOD_BANK, (n,), device)[:, None]
225
+ start = (torch.floor(t / reset) * reset).long().clamp_max(length - 1)
226
+ within = total - exclusive.gather(1, start)
227
+ capacity = total[:, -1:].clamp_min(1e-6) / _logu(gen, (n, 1), 2.0, 50.0, device)
228
+ inventory = capacity - torch.remainder(total, capacity)
229
+ accumulated = torch.where(use(0.5), within, inventory)
230
+ out = torch.where(sub == 0, raw, torch.where(sub == 1, reservoir, accumulated))
231
+ return _finish(out)
232
+
233
+
234
+ MAKERS = {"levy": levy_family, "ets": ets_family, "pulse": pulse_family}