-
Notifications
You must be signed in to change notification settings - Fork 7
create ApproximateGPs.TestUtils #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6c4c257
create ApproximateGPs.TestUtils
st-- de56ed2
missing import
st-- 1ed134a
sort
st-- 78d48f2
explicit Flux import
st-- 93bd28d
update Project.toml
st-- 4c18376
Merge branch 'master' of github.com:JuliaGaussianProcesses/Approximat…
st-- 41429b6
patch bump
st-- 84a69d1
fix qualifying
st-- 3a4b7fb
add docstring
st-- 6b4f4b1
update docstring
st-- 0e8b75e
docstr
st-- ff736ee
test_approx_lml stub
st-- 37a541e
run test_approximation_predictions in SVA
st-- 69ef2be
Revert "run test_approximation_predictions in SVA"
st-- 4748639
add Test dependency
st-- 3c8c0fa
revert reexporting AbstractGPs
st-- 553550f
Update test/SparseVariationalApproximationModule.jl
st-- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,6 @@ include("LaplaceApproximationModule.jl") | |
|
|
||
| include("deprecations.jl") | ||
|
|
||
| include("TestUtils.jl") | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| module TestUtils | ||
|
|
||
| using LinearAlgebra | ||
| using Random | ||
| using Test | ||
|
|
||
| using Distributions | ||
| using LogExpFunctions: logistic, softplus | ||
|
|
||
| using AbstractGPs | ||
| using ApproximateGPs | ||
|
|
||
| function generate_data() | ||
| X = range(0, 23.5; length=48) | ||
| # The random number generator changed in 1.6->1.7. The following vector was generated in Julia 1.6. | ||
| # The generating code below is only kept for illustrative purposes. | ||
| #! format: off | ||
| 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] | ||
| #! format: on | ||
| # Random.seed!(1) | ||
| # fs = @. 3 * sin(10 + 0.6X) + sin(0.1X) - 1 | ||
| # # invlink = normcdf | ||
| # invlink = logistic | ||
| # ps = invlink.(fs) | ||
| # Y = @. rand(Bernoulli(ps)) | ||
| return X, Y | ||
| end | ||
|
|
||
| dist_y_given_f(f) = Bernoulli(logistic(f)) | ||
|
|
||
| function build_latent_gp(theta) | ||
| variance = softplus(theta[1]) | ||
| lengthscale = softplus(theta[2]) | ||
| kernel = variance * with_lengthscale(SqExponentialKernel(), lengthscale) | ||
| return LatentGP(GP(kernel), dist_y_given_f, 1e-8) | ||
| end | ||
|
|
||
| """ | ||
| test_approx_lml(approx) | ||
|
|
||
| Test whether in the conjugate case `approx_lml(approx, LatentGP(f, | ||
| GaussianLikelihood(), jitter)(x), y)` gives approximately the same answer as | ||
| the log marginal likelihood in exact GP regression. | ||
|
|
||
| !!! todo | ||
| Not yet implemented. | ||
|
|
||
| Will not necessarily work for approximations that rely on optimization such | ||
| as `SparseVariationalApproximation`. | ||
|
|
||
| !!! todo | ||
| Also test gradients (for hyperparameter optimization). | ||
| """ | ||
| function test_approx_lml end | ||
|
|
||
| """ | ||
| test_approximation_predictions(approx) | ||
|
|
||
| Test whether the prediction interface for `approx` works and whether in the | ||
| conjugate case `posterior(approx, LatentGP(f, GaussianLikelihood(), jitter)(x), y)` | ||
| gives approximately the same answer as the exact GP regression posterior. | ||
|
|
||
| !!! note | ||
| Should be satisfied by all approximate inference methods, but note that | ||
| this does not currently apply for some approximations which rely on | ||
| optimization such as `SparseVariationalApproximation`. | ||
|
|
||
| !!! warning | ||
| Do not rely on this as the only test of a new approximation! | ||
|
|
||
| See `test_approx_lml`. | ||
| """ | ||
| function test_approximation_predictions(approx) | ||
| rng = MersenneTwister(123456) | ||
| N_cond = 5 | ||
| N_a = 6 | ||
| N_b = 7 | ||
|
|
||
| # Specify prior. | ||
| f = GP(Matern32Kernel()) | ||
| # Sample from prior. | ||
| x = collect(range(-1.0, 1.0; length=N_cond)) | ||
| # TODO: Change to x = ColVecs(rand(2, N_cond)) once #109 is fixed | ||
| noise_scale = 0.1 | ||
| fx = f(x, noise_scale^2) | ||
| y = rand(rng, fx) | ||
|
|
||
| jitter = 0.0 # not needed in Gaussian case | ||
| lf = LatentGP(f, f -> Normal(f, noise_scale), jitter) | ||
| f_approx_post = posterior(approx, lf(x), y) | ||
|
|
||
| @testset "AbstractGPs API" begin | ||
| a = collect(range(-1.2, 1.2; length=N_a)) | ||
| b = randn(rng, N_b) | ||
| AbstractGPs.TestUtils.test_internal_abstractgps_interface(rng, f_approx_post, a, b) | ||
| end | ||
|
|
||
| @testset "exact GPR equivalence for Gaussian likelihood" begin | ||
| f_exact_post = posterior(f(x, noise_scale^2), y) | ||
| xt = vcat(x, randn(rng, 3)) # test at training and new points | ||
|
|
||
| m_approx, c_approx = mean_and_cov(f_approx_post(xt)) | ||
| m_exact, c_exact = mean_and_cov(f_exact_post(xt)) | ||
|
|
||
| @test m_approx ≈ m_exact | ||
| @test c_approx ≈ c_exact | ||
| end | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.