|
1 | 1 | @testset "laplace" begin |
2 | | - function generate_data() |
3 | | - X = range(0, 23.5; length=48) |
4 | | - # The random number generator changed in 1.6->1.7. The following vector was generated in Julia 1.6. |
5 | | - # The generating code below is only kept for illustrative purposes. |
6 | | - #! format: off |
7 | | - Y = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0] |
8 | | - #! format: on |
9 | | - # Random.seed!(1) |
10 | | - # fs = @. 3 * sin(10 + 0.6X) + sin(0.1X) - 1 |
11 | | - # # invlink = normcdf |
12 | | - # invlink = logistic |
13 | | - # ps = invlink.(fs) |
14 | | - # Y = [rand(Bernoulli(p)) for p in ps] |
15 | | - return X, Y |
16 | | - end |
17 | | - |
18 | | - dist_y_given_f(f) = Bernoulli(logistic(f)) |
19 | | - |
20 | | - function build_latent_gp(theta) |
21 | | - variance = softplus(theta[1]) |
22 | | - lengthscale = softplus(theta[2]) |
23 | | - kernel = variance * with_lengthscale(SqExponentialKernel(), lengthscale) |
24 | | - return LatentGP(GP(kernel), dist_y_given_f, 1e-8) |
25 | | - end |
| 2 | + generate_data = ApproximateGPs.TestUtils.generate_data |
| 3 | + dist_y_given_f = ApproximateGPs.TestUtils.dist_y_given_f |
| 4 | + build_latent_gp = ApproximateGPs.TestUtils.build_latent_gp |
26 | 5 |
|
27 | 6 | function optimize_elbo( |
28 | 7 | build_latent_gp, |
|
49 | 28 | end |
50 | 29 |
|
51 | 30 | @testset "predictions" begin |
52 | | - rng = MersenneTwister(123456) |
53 | | - N_cond = 5 |
54 | | - N_a = 6 |
55 | | - N_b = 7 |
56 | | - |
57 | | - # Specify prior. |
58 | | - f = GP(Matern32Kernel()) |
59 | | - # Sample from prior. |
60 | | - x = collect(range(-1.0, 1.0; length=N_cond)) |
61 | | - noise_scale = 0.1 |
62 | | - fx = f(x, noise_scale^2) |
63 | | - y = rand(rng, fx) |
64 | | - |
65 | | - jitter = 0.0 # not needed in Gaussian case |
66 | | - lf = LatentGP(f, f -> Normal(f, noise_scale), jitter) |
67 | | - # in Gaussian case, Laplace converges to f_opt in one step; we need the |
68 | | - # second step to compute the cache at f_opt rather than f_init! |
69 | | - f_approx_post = posterior(LaplaceApproximation(; maxiter=2), lf(x), y) |
70 | | - |
71 | | - @testset "AbstractGPs API" begin |
72 | | - a = collect(range(-1.2, 1.2; length=N_a)) |
73 | | - b = randn(rng, N_b) |
74 | | - AbstractGPs.TestUtils.test_internal_abstractgps_interface( |
75 | | - rng, f_approx_post, a, b |
76 | | - ) |
77 | | - end |
78 | | - |
79 | | - @testset "equivalence to exact GPR for Gaussian likelihood" begin |
80 | | - f_exact_post = posterior(f(x, noise_scale^2), y) |
81 | | - xt = vcat(x, randn(rng, 3)) # test at training and new points |
82 | | - |
83 | | - m_approx, c_approx = mean_and_cov(f_approx_post(xt)) |
84 | | - m_exact, c_exact = mean_and_cov(f_exact_post(xt)) |
85 | | - |
86 | | - @test m_approx ≈ m_exact |
87 | | - @test c_approx ≈ c_exact |
88 | | - end |
| 31 | + approx = LaplaceApproximation(; maxiter=2) |
| 32 | + ApproximateGPs.TestUtils.test_approximation_predictions(approx) |
89 | 33 | end |
90 | 34 |
|
91 | 35 | @testset "gradients" begin |
|
0 commit comments