-
Notifications
You must be signed in to change notification settings - Fork 306
/
kernel-synth.py
200 lines (168 loc) · 5.64 KB
/
kernel-synth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import argparse
import functools
from pathlib import Path
from typing import Optional
import numpy as np
from gluonts.dataset.arrow import ArrowWriter
from joblib import Parallel, delayed
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import (
RBF,
ConstantKernel,
DotProduct,
ExpSineSquared,
Kernel,
RationalQuadratic,
WhiteKernel,
)
from tqdm.auto import tqdm
LENGTH = 1024
KERNEL_BANK = [
ExpSineSquared(periodicity=24 / LENGTH), # H
ExpSineSquared(periodicity=48 / LENGTH), # 0.5H
ExpSineSquared(periodicity=96 / LENGTH), # 0.25H
ExpSineSquared(periodicity=24 * 7 / LENGTH), # H
ExpSineSquared(periodicity=48 * 7 / LENGTH), # 0.5H
ExpSineSquared(periodicity=96 * 7 / LENGTH), # 0.25H
ExpSineSquared(periodicity=7 / LENGTH), # D
ExpSineSquared(periodicity=14 / LENGTH), # 0.5D
ExpSineSquared(periodicity=30 / LENGTH), # D
ExpSineSquared(periodicity=60 / LENGTH), # 0.5D
ExpSineSquared(periodicity=365 / LENGTH), # D
ExpSineSquared(periodicity=365 * 2 / LENGTH), # 0.5D
ExpSineSquared(periodicity=4 / LENGTH), # W
ExpSineSquared(periodicity=26 / LENGTH), # W
ExpSineSquared(periodicity=52 / LENGTH), # W
ExpSineSquared(periodicity=4 / LENGTH), # M
ExpSineSquared(periodicity=6 / LENGTH), # M
ExpSineSquared(periodicity=12 / LENGTH), # M
ExpSineSquared(periodicity=4 / LENGTH), # Q
ExpSineSquared(periodicity=4 * 10 / LENGTH), # Q
ExpSineSquared(periodicity=10 / LENGTH), # Y
DotProduct(sigma_0=0.0),
DotProduct(sigma_0=1.0),
DotProduct(sigma_0=10.0),
RBF(length_scale=0.1),
RBF(length_scale=1.0),
RBF(length_scale=10.0),
RationalQuadratic(alpha=0.1),
RationalQuadratic(alpha=1.0),
RationalQuadratic(alpha=10.0),
WhiteKernel(noise_level=0.1),
WhiteKernel(noise_level=1.0),
ConstantKernel(),
]
def random_binary_map(a: Kernel, b: Kernel):
"""
Applies a random binary operator (+ or *) with equal probability
on kernels ``a`` and ``b``.
Parameters
----------
a
A GP kernel.
b
A GP kernel.
Returns
-------
The composite kernel `a + b` or `a * b`.
"""
binary_maps = [lambda x, y: x + y, lambda x, y: x * y]
return np.random.choice(binary_maps)(a, b)
def sample_from_gp_prior(
kernel: Kernel, X: np.ndarray, random_seed: Optional[int] = None
):
"""
Draw a sample from a GP prior.
Parameters
----------
kernel
The GP covaraince kernel.
X
The input "time" points.
random_seed, optional
The random seed for sampling, by default None.
Returns
-------
A time series sampled from the GP prior.
"""
if X.ndim == 1:
X = X[:, None]
assert X.ndim == 2
gpr = GaussianProcessRegressor(kernel=kernel)
ts = gpr.sample_y(X, n_samples=1, random_state=random_seed)
return ts
def sample_from_gp_prior_efficient(
kernel: Kernel,
X: np.ndarray,
random_seed: Optional[int] = None,
method: str = "eigh",
):
"""
Draw a sample from a GP prior. An efficient version that allows specification
of the sampling method. The default sampling method used in GaussianProcessRegressor
is based on SVD which is significantly slower that alternatives such as `eigh` and
`cholesky`.
Parameters
----------
kernel
The GP covaraince kernel.
X
The input "time" points.
random_seed, optional
The random seed for sampling, by default None.
method, optional
The sampling method for multivariate_normal, by default `eigh`.
Returns
-------
A time series sampled from the GP prior.
"""
if X.ndim == 1:
X = X[:, None]
assert X.ndim == 2
cov = kernel(X)
ts = np.random.default_rng(seed=random_seed).multivariate_normal(
mean=np.zeros(X.shape[0]), cov=cov, method=method
)
return ts
def generate_time_series(max_kernels: int = 5):
"""Generate a synthetic time series from KernelSynth.
Parameters
----------
max_kernels, optional
The maximum number of base kernels to use for each time series, by default 5
Returns
-------
A time series generated by KernelSynth.
"""
while True:
X = np.linspace(0, 1, LENGTH)
# Randomly select upto max_kernels kernels from the KERNEL_BANK
selected_kernels = np.random.choice(
KERNEL_BANK, np.random.randint(1, max_kernels + 1), replace=True
)
# Combine the sampled kernels using random binary operators
kernel = functools.reduce(random_binary_map, selected_kernels)
# Sample a time series from the GP prior
try:
ts = sample_from_gp_prior(kernel=kernel, X=X)
except np.linalg.LinAlgError as err:
print("Error caught:", err)
continue
# The timestamp is arbitrary
return {"start": np.datetime64("2000-01-01 00:00", "s"), "target": ts.squeeze()}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-N", "--num-series", type=int, default=1000_000)
parser.add_argument("-J", "--max-kernels", type=int, default=5)
args = parser.parse_args()
path = Path(__file__).parent / "kernelsynth-data.arrow"
generated_dataset = Parallel(n_jobs=-1)(
delayed(generate_time_series)(max_kernels=args.max_kernels)
for _ in tqdm(range(args.num_series))
)
ArrowWriter(compression="lz4").write_to_file(
generated_dataset,
path=path,
)