/
test_utils.jl
52 lines (45 loc) · 1.5 KB
/
test_utils.jl
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
function test_interface(
rng::AbstractRNG, lik, k::Kernel, x::AbstractVector; functor_args=(),
)
gp = GP(k)
lgp = LatentGP(gp, lik, 1e-5)
lfgp = lgp(x)
# Check if likelihood produces a distribution
@test lik(rand(rng, lfgp.fx)) isa Distribution
N = length(x)
y = rand(rng, lfgp.fx)
if x isa MOInput
# TODO: replace with mo_inverse_transform
N = length(x.x)
y = [y[[i + j*N for j in 0:(x.out_dim - 1)]] for i in 1:N]
end
# Check if the likelihood samples are of correct length
@test length(rand(rng, lik(y))) == N
# Check if functor works properly
if functor_args == ()
@test Functors.functor(lik)[1] == functor_args
else
@test keys(Functors.functor(lik)[1]) == functor_args
end
end
"""
test_interface(lik, k::Kernel, x::AbstractVector; functor_args=())
This function provides unified method to check the interface of the various likelihoods
defined. It checks if the likelihood produces a distribution, length of likelihood
samples is correct and if the functor works as intended.
...
# Arguments
- `lik`: the likelihood to test the interface of
- `k::Kernel`: the kernel to use for the GP
- `x::AbstractVector`: intputs to compute the likelihood on
- `functor_args=()`: a collection of symbols of arguments to match functor parameters with.
...
"""
function test_interface(
lik,
k::KernelFunctions.Kernel,
x::AbstractVector;
kwargs...
)
test_interface(Random.GLOBAL_RNG, lik, k, x; kwargs...)
end