|
1 |
| -using Plots; pyplot(); |
| 1 | +using Plots; |
| 2 | +pyplot(); |
2 | 3 | using Distributions
|
3 | 4 | using LinearAlgebra
|
4 | 5 | using KernelFunctions
|
5 | 6 | # Translational invariants kernels
|
6 | 7 |
|
7 |
| -default(lw=3.0,titlefontsize=28,tickfontsize=18) |
| 8 | +default(; lw=3.0, titlefontsize=28, tickfontsize=18) |
8 | 9 |
|
9 |
| -x₀ = 0.0; l = 0.1 |
| 10 | +x₀ = 0.0; |
| 11 | +l = 0.1; |
10 | 12 | n_grid = 101
|
11 |
| -fill(x₀,n_grid,1) |
12 |
| -xrange = reshape(collect(range(-3,3,length=n_grid)),:,1) |
| 13 | +fill(x₀, n_grid, 1) |
| 14 | +xrange = reshape(collect(range(-3, 3; length=n_grid)), :, 1) |
13 | 15 |
|
14 |
| -k = transform(SqExponentialKernel(),1.0) |
15 |
| -K1 = kernelmatrix(k,xrange,obsdim=1) |
16 |
| -p = heatmap(K1,yflip=true,colorbar=false,framestyle=:none,background_color=RGBA(0.0,0.0,0.0,0.0)) |
17 |
| -savefig(joinpath(@__DIR__,"src","assets","heatmap_sqexp.png")) |
| 16 | +k = transform(SqExponentialKernel(), 1.0) |
| 17 | +K1 = kernelmatrix(k, xrange; obsdim=1) |
| 18 | +p = heatmap( |
| 19 | + K1; |
| 20 | + yflip=true, |
| 21 | + colorbar=false, |
| 22 | + framestyle=:none, |
| 23 | + background_color=RGBA(0.0, 0.0, 0.0, 0.0), |
| 24 | +) |
| 25 | +savefig(joinpath(@__DIR__, "src", "assets", "heatmap_sqexp.png")) |
18 | 26 |
|
| 27 | +k = @kernel Matern32Kernel FunctionTransform(x -> (sin.(x)) .^ 2) |
| 28 | +K2 = kernelmatrix(k, xrange; obsdim=1) |
| 29 | +p = heatmap( |
| 30 | + K2; |
| 31 | + yflip=true, |
| 32 | + colorbar=false, |
| 33 | + framestyle=:none, |
| 34 | + background_color=RGBA(0.0, 0.0, 0.0, 0.0), |
| 35 | +) |
| 36 | +savefig(joinpath(@__DIR__, "src", "assets", "heatmap_matern.png")) |
19 | 37 |
|
20 |
| -k = @kernel Matern32Kernel FunctionTransform(x->(sin.(x)).^2) |
21 |
| -K2 = kernelmatrix(k,xrange,obsdim=1) |
22 |
| -p = heatmap(K2,yflip=true,colorbar=false,framestyle=:none,background_color=RGBA(0.0,0.0,0.0,0.0)) |
23 |
| -savefig(joinpath(@__DIR__,"src","assets","heatmap_matern.png")) |
| 38 | +k = transform(PolynomialKernel(; c=0.0, d=2.0), LinearTransform(randn(3, 1))) |
| 39 | +K3 = kernelmatrix(k, xrange; obsdim=1) |
| 40 | +p = heatmap( |
| 41 | + K3; |
| 42 | + yflip=true, |
| 43 | + colorbar=false, |
| 44 | + framestyle=:none, |
| 45 | + background_color=RGBA(0.0, 0.0, 0.0, 0.0), |
| 46 | +) |
| 47 | +savefig(joinpath(@__DIR__, "src", "assets", "heatmap_poly.png")) |
24 | 48 |
|
| 49 | +k = |
| 50 | + 0.5 * SqExponentialKernel() * transform(LinearKernel(), 0.5) + |
| 51 | + 0.4 * (@kernel Matern32Kernel() FunctionTransform(x -> sin.(x))) |
| 52 | +K4 = kernelmatrix(k, xrange; obsdim=1) |
| 53 | +p = heatmap( |
| 54 | + K4; |
| 55 | + yflip=true, |
| 56 | + colorbar=false, |
| 57 | + framestyle=:none, |
| 58 | + background_color=RGBA(0.0, 0.0, 0.0, 0.0), |
| 59 | +) |
| 60 | +savefig(joinpath(@__DIR__, "src", "assets", "heatmap_prodsum.png")) |
25 | 61 |
|
26 |
| -k = transform(PolynomialKernel(c=0.0,d=2.0), LinearTransform(randn(3,1))) |
27 |
| -K3 = kernelmatrix(k,xrange,obsdim=1) |
28 |
| -p = heatmap(K3,yflip=true,colorbar=false,framestyle=:none,background_color=RGBA(0.0,0.0,0.0,0.0)) |
29 |
| -savefig(joinpath(@__DIR__,"src","assets","heatmap_poly.png")) |
30 |
| - |
31 |
| -k = 0.5*SqExponentialKernel()*transform(LinearKernel(),0.5) + 0.4*(@kernel Matern32Kernel() FunctionTransform(x->sin.(x))) |
32 |
| -K4 = kernelmatrix(k,xrange,obsdim=1) |
33 |
| -p = heatmap(K4,yflip=true,colorbar=false,framestyle=:none,background_color=RGBA(0.0,0.0,0.0,0.0)) |
34 |
| -savefig(joinpath(@__DIR__,"src","assets","heatmap_prodsum.png")) |
35 |
| - |
36 |
| -plot(heatmap.([K1,K2,K3,K4],yflip=true,colorbar=false)...,layout=(2,2)) |
37 |
| -savefig(joinpath(@__DIR__,"src","assets","heatmap_combination.png")) |
| 62 | +plot(heatmap.([K1, K2, K3, K4], yflip=true, colorbar=false)...; layout=(2, 2)) |
| 63 | +savefig(joinpath(@__DIR__, "src", "assets", "heatmap_combination.png")) |
38 | 64 |
|
39 | 65 | ##
|
40 | 66 |
|
41 |
| -for k in [SqExponentialKernel,ExponentialKernel] |
42 |
| - K = kernelmatrix(k(),xrange,obsdim=1) |
43 |
| - v = rand(MvNormal(K+1e-7I)) |
44 |
| - plot(xrange,v,lab="",title="f(x)",framestyle=:none) |> display |
45 |
| - savefig(joinpath(@__DIR__,"src","assets","GP_sample_$(k).png")) |
46 |
| - plot(xrange,kernel.(k(),x₀,xrange),lab="",ylims=(0,1.1),title="k(0,x)") |> display |
47 |
| - savefig(joinpath(@__DIR__,"src","assets","kappa_function_$(k).png")) |
| 67 | +for k in [SqExponentialKernel, ExponentialKernel] |
| 68 | + K = kernelmatrix(k(), xrange; obsdim=1) |
| 69 | + v = rand(MvNormal(K + 1e-7I)) |
| 70 | + display(plot(xrange, v; lab="", title="f(x)", framestyle=:none)) |
| 71 | + savefig(joinpath(@__DIR__, "src", "assets", "GP_sample_$(k).png")) |
| 72 | + display(plot(xrange, kernel.(k(), x₀, xrange); lab="", ylims=(0, 1.1), title="k(0,x)")) |
| 73 | + savefig(joinpath(@__DIR__, "src", "assets", "kappa_function_$(k).png")) |
48 | 74 | end
|
49 | 75 |
|
50 |
| -for k in [GammaExponentialKernel(1.0,1.5)] |
51 |
| - sparse =1 |
52 |
| - while !isposdef(kernelmatrix(k,xrange*sparse,obsdim=1) + 1e-5I); sparse += 1; end |
53 |
| - v = rand(MvNormal(kernelmatrix(k,xrange*sparse,obsdim=1)+1e-7I)) |
54 |
| - plot(xrange,v,lab="",title="f(x)",framestyle=:none) |> display |
55 |
| - savefig(joinpath(@__DIR__,"src","assets","GP_sample_GammaExponentialKernel.png")) |
56 |
| - plot(xrange,kernel.(k,x₀,xrange),lab="",ylims=(0,1.1),title="k(0,x)") |> display |
57 |
| - savefig(joinpath(@__DIR__,"src","assets","kappa_function_GammaExponentialKernel.png")) |
| 76 | +for k in [GammaExponentialKernel(1.0, 1.5)] |
| 77 | + sparse = 1 |
| 78 | + while !isposdef(kernelmatrix(k, xrange * sparse; obsdim=1) + 1e-5I) |
| 79 | + sparse += 1 |
| 80 | + end |
| 81 | + v = rand(MvNormal(kernelmatrix(k, xrange * sparse; obsdim=1) + 1e-7I)) |
| 82 | + display(plot(xrange, v; lab="", title="f(x)", framestyle=:none)) |
| 83 | + savefig(joinpath(@__DIR__, "src", "assets", "GP_sample_GammaExponentialKernel.png")) |
| 84 | + display(plot(xrange, kernel.(k, x₀, xrange); lab="", ylims=(0, 1.1), title="k(0,x)")) |
| 85 | + savefig( |
| 86 | + joinpath(@__DIR__, "src", "assets", "kappa_function_GammaExponentialKernel.png") |
| 87 | + ) |
58 | 88 | end
|
59 | 89 |
|
60 |
| -for k in [MaternKernel,Matern32Kernel,Matern52Kernel] |
61 |
| - K = kernelmatrix(k(),xrange,obsdim=1) |
62 |
| - v = rand(MvNormal(K+1e-7I)) |
63 |
| - plot(xrange,v,lab="",title="f(x)",framestyle=:none) |> display |
64 |
| - savefig(joinpath(@__DIR__,"src","assets","GP_sample_$(k).png")) |
65 |
| - plot(xrange,kernel.(k(),x₀,xrange),lab="",ylims=(0,1.1),title="k(0,x)") |> display |
66 |
| - savefig(joinpath(@__DIR__,"src","assets","kappa_function_$(k).png")) |
| 90 | +for k in [MaternKernel, Matern32Kernel, Matern52Kernel] |
| 91 | + K = kernelmatrix(k(), xrange; obsdim=1) |
| 92 | + v = rand(MvNormal(K + 1e-7I)) |
| 93 | + display(plot(xrange, v; lab="", title="f(x)", framestyle=:none)) |
| 94 | + savefig(joinpath(@__DIR__, "src", "assets", "GP_sample_$(k).png")) |
| 95 | + display(plot(xrange, kernel.(k(), x₀, xrange); lab="", ylims=(0, 1.1), title="k(0,x)")) |
| 96 | + savefig(joinpath(@__DIR__, "src", "assets", "kappa_function_$(k).png")) |
67 | 97 | end
|
68 | 98 |
|
69 |
| - |
70 | 99 | for k in [RationalQuadraticKernel]
|
71 |
| - K = kernelmatrix(k(),xrange,obsdim=1) |
72 |
| - v = rand(MvNormal(K+1e-7I)) |
73 |
| - plot(xrange,v,lab="",title="f(x)",framestyle=:none) |> display |
74 |
| - savefig(joinpath(@__DIR__,"src","assets","GP_sample_$(k).png")) |
75 |
| - plot(xrange,kernel.(k(),x₀,xrange),lab="",ylims=(0,1.1),title="k(0,x)") |> display |
76 |
| - savefig(joinpath(@__DIR__,"src","assets","kappa_function_$(k).png")) |
| 100 | + K = kernelmatrix(k(), xrange; obsdim=1) |
| 101 | + v = rand(MvNormal(K + 1e-7I)) |
| 102 | + display(plot(xrange, v; lab="", title="f(x)", framestyle=:none)) |
| 103 | + savefig(joinpath(@__DIR__, "src", "assets", "GP_sample_$(k).png")) |
| 104 | + display(plot(xrange, kernel.(k(), x₀, xrange); lab="", ylims=(0, 1.1), title="k(0,x)")) |
| 105 | + savefig(joinpath(@__DIR__, "src", "assets", "kappa_function_$(k).png")) |
77 | 106 | end
|
0 commit comments