-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
511 lines (401 loc) · 16.7 KB
/
examples.py
File metadata and controls
511 lines (401 loc) · 16.7 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""
Comprehensive Examples for Compositional Kernel Search
This script demonstrates the kernel search algorithm on multiple datasets
including the classic examples from the paper (Mauna Loa CO2, Airline passengers).
"""
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from compositional_kernel_search import (
CompositionalKernelSearch, GaussianProcess,
SquaredExponential, Periodic, Linear, RationalQuadratic,
SumKernel, ProductKernel, decompose_posterior
)
def example_1_simple_periodic():
"""Example 1: Simple periodic data with trend."""
print("\n" + "="*70)
print("EXAMPLE 1: Simple Periodic Data with Linear Trend")
print("="*70)
# Generate data: linear trend + periodic + noise
np.random.seed(42)
X = np.linspace(0, 4*np.pi, 80)
y = 0.5 * X + 2 * np.sin(X) + 0.3 * np.random.randn(len(X))
# Normalize
X_norm = (X - X.mean()) / X.std()
y_norm = (y - y.mean()) / y.std()
# Search
searcher = CompositionalKernelSearch(max_depth=3, n_restarts=2, verbose=True)
result = searcher.search(X_norm, y_norm)
# Fit GP
gp = GaussianProcess(result.kernel, noise_variance=result.noise_var)
gp.fit(X_norm, y_norm)
# Predict
X_test = np.linspace(X_norm.min() - 0.5, X_norm.max() + 1, 200)
mean, var = gp.predict(X_test)
std = np.sqrt(var)
# Denormalize
X_test_orig = X_test * X.std() + X.mean()
mean_orig = mean * y.std() + y.mean()
std_orig = std * y.std()
# Plot
fig, ax = plt.subplots(figsize=(12, 5))
ax.scatter(X, y, c='black', s=20, alpha=0.7, label='Data')
ax.plot(X_test_orig, mean_orig, 'b-', lw=2, label='Mean')
ax.fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig,
alpha=0.3, color='blue', label='95% CI')
ax.axvline(x=X.max(), color='red', linestyle='--', alpha=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title(f'Example 1: Simple Periodic + Trend\nDiscovered Kernel: {result.kernel}')
ax.legend()
ax.grid(True, alpha=0.3)
return fig
def example_2_airline_passengers():
"""Example 2: Airline passengers with trend and growing seasonality."""
print("\n" + "="*70)
print("EXAMPLE 2: Airline Passengers (Trend + Seasonality)")
print("="*70)
# Generate airline-like data
np.random.seed(123)
months = np.arange(144) # 12 years
# Components
trend = 100 + 2.5 * months
seasonality = 25 * np.sin(2 * np.pi * months / 12)
# Growing amplitude
growing_seasonality = seasonality * (1 + 0.008 * months)
noise = 8 * np.random.randn(len(months))
y = trend + growing_seasonality + noise
# Normalize
X_norm = (months - months.mean()) / months.std()
y_norm = (y - y.mean()) / y.std()
# Search
searcher = CompositionalKernelSearch(max_depth=4, n_restarts=3, verbose=True)
result = searcher.search(X_norm, y_norm)
# Fit GP
gp = GaussianProcess(result.kernel, noise_variance=result.noise_var)
gp.fit(X_norm, y_norm)
# Predict (including extrapolation)
X_test = np.linspace(X_norm.min() - 0.2, X_norm.max() + 0.5, 250)
mean, var = gp.predict(X_test)
std = np.sqrt(var)
# Denormalize
X_test_orig = X_test * months.std() + months.mean()
mean_orig = mean * y.std() + y.mean()
std_orig = std * y.std()
# Plot
fig, axes = plt.subplots(2, 1, figsize=(14, 10))
# Main plot
axes[0].scatter(months, y, c='black', s=15, alpha=0.7, label='Data')
axes[0].plot(X_test_orig, mean_orig, 'b-', lw=2, label='Prediction')
axes[0].fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig,
alpha=0.3, color='blue', label='95% CI')
axes[0].axvline(x=months.max(), color='red', linestyle='--', alpha=0.5, label='Training cutoff')
axes[0].set_xlabel('Month')
axes[0].set_ylabel('Passengers')
axes[0].set_title(f'Airline Passengers\nDiscovered: {result.kernel}')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Residuals
pred, _ = gp.predict(X_norm)
residuals = y_norm - pred
axes[1].scatter(months, residuals * y.std(), c='black', s=15, alpha=0.7)
axes[1].axhline(y=0, color='red', linestyle='--')
axes[1].set_xlabel('Month')
axes[1].set_ylabel('Residual')
axes[1].set_title('Residuals')
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
return fig
def example_3_mauna_loa_co2():
"""Example 3: Mauna Loa CO2 style data."""
print("\n" + "="*70)
print("EXAMPLE 3: Mauna Loa CO2 (Long-term + Seasonal + Medium-term)")
print("="*70)
# Generate Mauna Loa-like data
np.random.seed(456)
t = np.arange(400) # Monthly data
# Long-term trend (approximately quadratic)
trend = 310 + 0.1 * t + 0.0003 * t**2
# Annual cycle
seasonal = 3 * np.sin(2 * np.pi * t / 12)
# Medium-term variations
medium = 1.5 * np.sin(2 * np.pi * t / 50)
noise = 0.3 * np.random.randn(len(t))
y = trend + seasonal + medium + noise
# Use only part for training (to show extrapolation)
train_idx = t < 350
X_train, y_train = t[train_idx], y[train_idx]
# Normalize
X_norm = (X_train - X_train.mean()) / X_train.std()
y_norm = (y_train - y_train.mean()) / y_train.std()
# Search
searcher = CompositionalKernelSearch(max_depth=4, n_restarts=3, verbose=True)
result = searcher.search(X_norm, y_norm)
# Fit GP
gp = GaussianProcess(result.kernel, noise_variance=result.noise_var)
gp.fit(X_norm, y_norm)
# Predict over full range
X_test = np.linspace((t.min() - X_train.mean()) / X_train.std(),
(t.max() - X_train.mean()) / X_train.std(), 300)
mean, var = gp.predict(X_test)
std = np.sqrt(var)
# Denormalize
X_test_orig = X_test * X_train.std() + X_train.mean()
mean_orig = mean * y_train.std() + y_train.mean()
std_orig = std * y_train.std()
# Plot
fig, ax = plt.subplots(figsize=(14, 6))
ax.scatter(X_train, y_train, c='black', s=10, alpha=0.5, label='Training data')
ax.scatter(t[~train_idx], y[~train_idx], c='red', s=10, alpha=0.5, label='Test data')
ax.plot(X_test_orig, mean_orig, 'b-', lw=2, label='Prediction')
ax.fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig,
alpha=0.3, color='blue', label='95% CI')
ax.axvline(x=X_train.max(), color='red', linestyle='--', alpha=0.5)
ax.set_xlabel('Month')
ax.set_ylabel('CO2 (ppm)')
ax.set_title(f'Mauna Loa CO2 Style Data\nDiscovered: {result.kernel}')
ax.legend()
ax.grid(True, alpha=0.3)
return fig
def example_4_locally_periodic():
"""Example 4: Locally periodic (SE × Per)."""
print("\n" + "="*70)
print("EXAMPLE 4: Locally Periodic Data (Decaying Oscillations)")
print("="*70)
# Generate locally periodic data
np.random.seed(789)
X = np.linspace(0, 10, 150)
# Locally periodic: periodic modulated by SE envelope
y = np.sin(2 * np.pi * X) * np.exp(-0.15 * (X - 5)**2)
y += 0.1 * np.random.randn(len(X))
# Normalize
X_norm = (X - X.mean()) / X.std()
y_norm = (y - y.mean()) / y.std()
# Search
searcher = CompositionalKernelSearch(max_depth=3, n_restarts=3, verbose=True)
result = searcher.search(X_norm, y_norm)
# Fit GP
gp = GaussianProcess(result.kernel, noise_variance=result.noise_var)
gp.fit(X_norm, y_norm)
# Predict
X_test = np.linspace(X_norm.min() - 0.3, X_norm.max() + 0.5, 200)
mean, var = gp.predict(X_test)
std = np.sqrt(var)
# Denormalize
X_test_orig = X_test * X.std() + X.mean()
mean_orig = mean * y.std() + y.mean()
std_orig = std * y.std()
# Plot
fig, ax = plt.subplots(figsize=(12, 5))
ax.scatter(X, y, c='black', s=20, alpha=0.7, label='Data')
ax.plot(X_test_orig, mean_orig, 'b-', lw=2, label='Prediction')
ax.fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig,
alpha=0.3, color='blue', label='95% CI')
ax.axvline(x=X.max(), color='red', linestyle='--', alpha=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title(f'Locally Periodic Data\nDiscovered: {result.kernel}')
ax.legend()
ax.grid(True, alpha=0.3)
return fig
def example_5_multiscale():
"""Example 5: Multi-scale data (good for RQ kernel)."""
print("\n" + "="*70)
print("EXAMPLE 5: Multi-scale Variation")
print("="*70)
# Generate multi-scale data
np.random.seed(321)
X = np.linspace(0, 10, 200)
# Multiple scales
y = np.sin(0.5 * X) + 0.5 * np.sin(2 * X) + 0.2 * np.sin(5 * X)
y += 0.1 * np.random.randn(len(X))
# Normalize
X_norm = (X - X.mean()) / X.std()
y_norm = (y - y.mean()) / y.std()
# Search
searcher = CompositionalKernelSearch(max_depth=4, n_restarts=3, verbose=True, use_rq=True)
result = searcher.search(X_norm, y_norm)
# Fit GP
gp = GaussianProcess(result.kernel, noise_variance=result.noise_var)
gp.fit(X_norm, y_norm)
# Predict
X_test = np.linspace(X_norm.min() - 0.2, X_norm.max() + 0.3, 250)
mean, var = gp.predict(X_test)
std = np.sqrt(var)
# Denormalize
X_test_orig = X_test * X.std() + X.mean()
mean_orig = mean * y.std() + y.mean()
std_orig = std * y.std()
# Plot
fig, ax = plt.subplots(figsize=(12, 5))
ax.scatter(X, y, c='black', s=10, alpha=0.5, label='Data')
ax.plot(X_test_orig, mean_orig, 'b-', lw=2, label='Prediction')
ax.fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig,
alpha=0.3, color='blue', label='95% CI')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title(f'Multi-scale Data\nDiscovered: {result.kernel}')
ax.legend()
ax.grid(True, alpha=0.3)
return fig
def example_6_decomposition():
"""Example 6: Show posterior decomposition."""
print("\n" + "="*70)
print("EXAMPLE 6: Posterior Decomposition into Components")
print("="*70)
# Generate data with clear components
np.random.seed(999)
X = np.linspace(0, 10, 100)
# Clear linear + periodic components
linear_comp = 0.3 * X
periodic_comp = np.sin(2 * np.pi * X / 2)
noise = 0.15 * np.random.randn(len(X))
y = linear_comp + periodic_comp + noise
# Normalize
X_norm = (X - X.mean()) / X.std()
y_norm = (y - y.mean()) / y.std()
# Create known kernel structure: Lin + Per
kernel = SumKernel(Linear(0), Periodic(0))
# Fit GP
gp = GaussianProcess(kernel, noise_variance=0.1)
gp.fit(X_norm, y_norm)
gp.optimize(n_restarts=5)
print(f"Fitted kernel: {gp.kernel}")
# Get decomposition
X_test = np.linspace(X_norm.min(), X_norm.max(), 150)
components = decompose_posterior(gp, X_test)
# Denormalize
X_test_orig = X_test * X.std() + X.mean()
# Full prediction
mean_full, var_full = gp.predict(X_test)
# Plot decomposition
fig, axes = plt.subplots(len(components) + 2, 1, figsize=(12, 3*(len(components) + 2)))
# Full model
axes[0].scatter(X, y, c='black', s=20, alpha=0.7)
mean_orig = mean_full * y.std() + y.mean()
std_orig = np.sqrt(var_full) * y.std()
axes[0].plot(X_test_orig, mean_orig, 'b-', lw=2)
axes[0].fill_between(X_test_orig, mean_orig - 2*std_orig, mean_orig + 2*std_orig, alpha=0.3)
axes[0].set_title(f'Full Model: {gp.kernel}')
axes[0].set_ylabel('Y')
axes[0].grid(True, alpha=0.3)
# Components
for i, (name, (comp_mean, comp_std)) in enumerate(components.items()):
comp_mean_orig = comp_mean * y.std()
comp_std_orig = comp_std * y.std()
axes[i+1].plot(X_test_orig, comp_mean_orig, 'b-', lw=2)
axes[i+1].fill_between(X_test_orig,
comp_mean_orig - 2*comp_std_orig,
comp_mean_orig + 2*comp_std_orig, alpha=0.3)
axes[i+1].set_title(f'Component: {name}')
axes[i+1].set_ylabel('Y')
axes[i+1].grid(True, alpha=0.3)
# Residuals
pred, _ = gp.predict(X_norm)
residuals = (y_norm - pred) * y.std()
axes[-1].scatter(X, residuals, c='black', s=20, alpha=0.7)
axes[-1].axhline(y=0, color='red', linestyle='--')
axes[-1].set_title('Residuals')
axes[-1].set_xlabel('X')
axes[-1].set_ylabel('Residual')
axes[-1].grid(True, alpha=0.3)
plt.tight_layout()
return fig
def create_summary_figure():
"""Create a summary figure showing different kernel behaviors."""
print("\n" + "="*70)
print("Creating Summary of Base Kernels")
print("="*70)
fig, axes = plt.subplots(2, 4, figsize=(16, 8))
X = np.linspace(-3, 3, 100)
# Base kernels
kernels_info = [
(SquaredExponential(lengthscale=1.0), 'SE (Squared Exponential)', 'Local smoothness'),
(Periodic(period=2.0, lengthscale=0.5), 'Periodic', 'Repeating patterns'),
(Linear(), 'Linear', 'Linear functions'),
(RationalQuadratic(alpha=1.0), 'Rational Quadratic', 'Multi-scale variation'),
]
# Composite kernels
composite_info = [
(SumKernel(Linear(), SquaredExponential()), 'Lin + SE', 'Linear trend + local deviation'),
(ProductKernel(Periodic(), SquaredExponential()), 'Per × SE', 'Locally periodic'),
(ProductKernel(Linear(), Periodic()), 'Lin × Per', 'Growing amplitude'),
(SumKernel(Periodic(), Linear()), 'Per + Lin', 'Periodic with trend'),
]
np.random.seed(42)
# Plot base kernels
for i, (kernel, name, desc) in enumerate(kernels_info):
gp = GaussianProcess(kernel, noise_variance=0.01)
# Sample from prior
K = kernel(X.reshape(-1, 1), X.reshape(-1, 1)) + 1e-6 * np.eye(len(X))
L = np.linalg.cholesky(K)
for _ in range(3):
sample = L @ np.random.randn(len(X))
axes[0, i].plot(X, sample, alpha=0.7)
axes[0, i].set_title(f'{name}\n{desc}')
axes[0, i].set_xlabel('x')
axes[0, i].grid(True, alpha=0.3)
# Plot composite kernels
for i, (kernel, name, desc) in enumerate(composite_info):
K = kernel(X.reshape(-1, 1), X.reshape(-1, 1)) + 1e-6 * np.eye(len(X))
try:
L = np.linalg.cholesky(K)
for _ in range(3):
sample = L @ np.random.randn(len(X))
axes[1, i].plot(X, sample, alpha=0.7)
except:
axes[1, i].text(0.5, 0.5, 'Sampling failed', ha='center', va='center')
axes[1, i].set_title(f'{name}\n{desc}')
axes[1, i].set_xlabel('x')
axes[1, i].grid(True, alpha=0.3)
axes[0, 0].set_ylabel('Base Kernels\nf(x)')
axes[1, 0].set_ylabel('Composite Kernels\nf(x)')
plt.suptitle('Kernel Functions: Base and Composite', fontsize=14, fontweight='bold')
plt.tight_layout()
return fig
def main():
"""Run all examples and save figures."""
print("\n" + "#"*70)
print(" COMPOSITIONAL KERNEL SEARCH - COMPREHENSIVE EXAMPLES")
print(" Implementation of Duvenaud et al., ICML 2013")
print("#"*70)
# Summary figure
fig_summary = create_summary_figure()
fig_summary.savefig('/mnt/user-data/outputs/kernel_summary.png', dpi=150, bbox_inches='tight')
print("\nSaved: kernel_summary.png")
# Example 1
fig1 = example_1_simple_periodic()
fig1.savefig('/mnt/user-data/outputs/example1_periodic_trend.png', dpi=150, bbox_inches='tight')
print("Saved: example1_periodic_trend.png")
plt.close(fig1)
# Example 2
fig2 = example_2_airline_passengers()
fig2.savefig('/mnt/user-data/outputs/example2_airline.png', dpi=150, bbox_inches='tight')
print("Saved: example2_airline.png")
plt.close(fig2)
# Example 3
fig3 = example_3_mauna_loa_co2()
fig3.savefig('/mnt/user-data/outputs/example3_mauna_loa.png', dpi=150, bbox_inches='tight')
print("Saved: example3_mauna_loa.png")
plt.close(fig3)
# Example 4
fig4 = example_4_locally_periodic()
fig4.savefig('/mnt/user-data/outputs/example4_locally_periodic.png', dpi=150, bbox_inches='tight')
print("Saved: example4_locally_periodic.png")
plt.close(fig4)
# Example 5
fig5 = example_5_multiscale()
fig5.savefig('/mnt/user-data/outputs/example5_multiscale.png', dpi=150, bbox_inches='tight')
print("Saved: example5_multiscale.png")
plt.close(fig5)
# Example 6
fig6 = example_6_decomposition()
fig6.savefig('/mnt/user-data/outputs/example6_decomposition.png', dpi=150, bbox_inches='tight')
print("Saved: example6_decomposition.png")
plt.close(fig6)
print("\n" + "="*70)
print("All examples completed successfully!")
print("="*70)
if __name__ == "__main__":
main()