|
| 1 | +module TestUtils |
| 2 | + |
| 3 | +using LinearAlgebra |
| 4 | +using Random |
| 5 | +using Test |
| 6 | + |
| 7 | +using Distributions |
| 8 | +using LogExpFunctions: logistic, softplus |
| 9 | + |
| 10 | +using AbstractGPs |
| 11 | +using ApproximateGPs |
| 12 | + |
| 13 | +function generate_data() |
| 14 | + X = range(0, 23.5; length=48) |
| 15 | + # The random number generator changed in 1.6->1.7. The following vector was generated in Julia 1.6. |
| 16 | + # The generating code below is only kept for illustrative purposes. |
| 17 | + #! format: off |
| 18 | + 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] |
| 19 | + #! format: on |
| 20 | + # Random.seed!(1) |
| 21 | + # fs = @. 3 * sin(10 + 0.6X) + sin(0.1X) - 1 |
| 22 | + # # invlink = normcdf |
| 23 | + # invlink = logistic |
| 24 | + # ps = invlink.(fs) |
| 25 | + # Y = @. rand(Bernoulli(ps)) |
| 26 | + return X, Y |
| 27 | +end |
| 28 | + |
| 29 | +dist_y_given_f(f) = Bernoulli(logistic(f)) |
| 30 | + |
| 31 | +function build_latent_gp(theta) |
| 32 | + variance = softplus(theta[1]) |
| 33 | + lengthscale = softplus(theta[2]) |
| 34 | + kernel = variance * with_lengthscale(SqExponentialKernel(), lengthscale) |
| 35 | + return LatentGP(GP(kernel), dist_y_given_f, 1e-8) |
| 36 | +end |
| 37 | + |
| 38 | +""" |
| 39 | + test_approx_lml(approx) |
| 40 | +
|
| 41 | +Test whether in the conjugate case `approx_lml(approx, LatentGP(f, |
| 42 | +GaussianLikelihood(), jitter)(x), y)` gives approximately the same answer as |
| 43 | +the log marginal likelihood in exact GP regression. |
| 44 | +
|
| 45 | +!!! todo |
| 46 | + Not yet implemented. |
| 47 | +
|
| 48 | + Will not necessarily work for approximations that rely on optimization such |
| 49 | + as `SparseVariationalApproximation`. |
| 50 | +
|
| 51 | +!!! todo |
| 52 | + Also test gradients (for hyperparameter optimization). |
| 53 | +""" |
| 54 | +function test_approx_lml end |
| 55 | + |
| 56 | +""" |
| 57 | + test_approximation_predictions(approx) |
| 58 | +
|
| 59 | +Test whether the prediction interface for `approx` works and whether in the |
| 60 | +conjugate case `posterior(approx, LatentGP(f, GaussianLikelihood(), jitter)(x), y)` |
| 61 | +gives approximately the same answer as the exact GP regression posterior. |
| 62 | +
|
| 63 | +!!! note |
| 64 | + Should be satisfied by all approximate inference methods, but note that |
| 65 | + this does not currently apply for some approximations which rely on |
| 66 | + optimization such as `SparseVariationalApproximation`. |
| 67 | +
|
| 68 | +!!! warning |
| 69 | + Do not rely on this as the only test of a new approximation! |
| 70 | +
|
| 71 | +See `test_approx_lml`. |
| 72 | +""" |
| 73 | +function test_approximation_predictions(approx) |
| 74 | + rng = MersenneTwister(123456) |
| 75 | + N_cond = 5 |
| 76 | + N_a = 6 |
| 77 | + N_b = 7 |
| 78 | + |
| 79 | + # Specify prior. |
| 80 | + f = GP(Matern32Kernel()) |
| 81 | + # Sample from prior. |
| 82 | + x = collect(range(-1.0, 1.0; length=N_cond)) |
| 83 | + # TODO: Change to x = ColVecs(rand(2, N_cond)) once #109 is fixed |
| 84 | + noise_scale = 0.1 |
| 85 | + fx = f(x, noise_scale^2) |
| 86 | + y = rand(rng, fx) |
| 87 | + |
| 88 | + jitter = 0.0 # not needed in Gaussian case |
| 89 | + lf = LatentGP(f, f -> Normal(f, noise_scale), jitter) |
| 90 | + f_approx_post = posterior(approx, lf(x), y) |
| 91 | + |
| 92 | + @testset "AbstractGPs API" begin |
| 93 | + a = collect(range(-1.2, 1.2; length=N_a)) |
| 94 | + b = randn(rng, N_b) |
| 95 | + AbstractGPs.TestUtils.test_internal_abstractgps_interface(rng, f_approx_post, a, b) |
| 96 | + end |
| 97 | + |
| 98 | + @testset "exact GPR equivalence for Gaussian likelihood" begin |
| 99 | + f_exact_post = posterior(f(x, noise_scale^2), y) |
| 100 | + xt = vcat(x, randn(rng, 3)) # test at training and new points |
| 101 | + |
| 102 | + m_approx, c_approx = mean_and_cov(f_approx_post(xt)) |
| 103 | + m_exact, c_exact = mean_and_cov(f_exact_post(xt)) |
| 104 | + |
| 105 | + @test m_approx ≈ m_exact |
| 106 | + @test c_approx ≈ c_exact |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +end |
0 commit comments