Skip to content

Commit

Permalink
Renamed logpdf(gp, y) to _logpdf(gp, y) to respect Distributions.jl A…
Browse files Browse the repository at this point in the history
…PI and add Turing compatibility test (#52)

* Renamed logpdf(gp, y) to _logpdf(gp, y) to resepect Distributions.jl API

* Limit _logpdf to vectors and add specific methods for matrices

* Correct behaviour for `loglikelihood`

* Making product on y a scalar and removing _logpdf!

* Returned docs and restored definition of logpdf for _logpdf

* Missing docs

* Correct typo on docs

Co-authored-by: David Widmann <[email protected]>

* Removing linebreak

Co-authored-by: willtebbutt <[email protected]>

* Adding tests for loglikelihood

* Added a simple test with a Turing model

* Adding Turing to test/Project.toml

* Fixing Turing test

* Modified syntax Turing test and changed test_nowarn to test

* Added a test for the presence of docs

* Bump patch version

Co-authored-by: David Widmann <[email protected]>
Co-authored-by: willtebbutt <[email protected]>
  • Loading branch information
3 people authored Sep 7, 2020
1 parent 617d217 commit ee483a4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name = "AbstractGPs"
uuid = "99985d1d-32ba-4be9-9821-2ec096f28918"
authors = ["willtebbutt <[email protected]>"]
version = "0.2.7"
version = "0.2.8"


[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
6 changes: 5 additions & 1 deletion src/abstract_gp/finite_gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,14 @@ julia> logpdf(f(x), Y) isa AbstractVector{<:Real}
true
```
"""
function Distributions.logpdf(f::FiniteGP, y::AbstractVector{<:Real})
logpdf(f::FiniteGP, y::AbstractVecOrMat{<:Real})

function Distributions._logpdf(f::FiniteGP, y::AbstractVector{<:Real})
return first(logpdf(f, reshape(y, :, 1)))
end

Distributions.loglikelihood(f::FiniteGP, Y::AbstractMatrix{<:Real}) = sum(logpdf(f, Y))

function Distributions.logpdf(f::FiniteGP, Y::AbstractMatrix{<:Real})
m, C_mat = mean_and_cov(f)
C = cholesky(Symmetric(C_mat))
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[compat]
Expand All @@ -16,5 +17,6 @@ Distributions = "0.19, 0.20, 0.21, 0.22, 0.23"
Documenter = "0.24, 0.25"
FiniteDifferences = "0.9.6, 0.10"
Plots = "1"
Turing = "0.14"
Zygote = "0.5"
julia = "1.3"
10 changes: 8 additions & 2 deletions test/abstract_gp/finite_gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ end
# )
# end
# end
@testset "logpdf / elbo / dtc" begin
@testset "logpdf / loglikelihood / elbo / dtc" begin
rng = MersenneTwister(123456)
N = 10
S = 11
Expand All @@ -123,12 +123,13 @@ end
# Check that logpdf returns the correct type and roughly agrees with Distributions.
@test logpdf(y, ŷ) isa Real
@test logpdf(y, ŷ) logpdf(MvNormal(Vector(mean(y)), cov(y)), ŷ)

@test loglikelihood(y, ŷ) == logpdf(y, ŷ)
# Check that multi-sample logpdf returns the correct type and is consistent with
# single-sample logpdf
= rand(rng, y, S)
@test logpdf(y, Ŷ) isa Vector{Float64}
@test logpdf(y, Ŷ) [logpdf(y, Ŷ[:, n]) for n in 1:S]
@test loglikelihood(y, Ŷ) == sum(logpdf(y, Ŷ))

# # Check gradient of logpdf at mean is zero for `f`.
# adjoint_test(ŷ->logpdf(fx, ŷ), 1, ones(size(ŷ)))
Expand Down Expand Up @@ -207,6 +208,11 @@ end
end
end

@testset "Docs" begin
docstring = string(Docs.doc(logpdf, Tuple{FiniteGP, Vector{Float64}}))
@test contains(docstring, "logpdf(f::FiniteGP, y::AbstractVecOrMat{<:Real})")
end

# """
# simple_gp_tests(rng::AbstractRNG, f::GP, xs::AV{<:AV}, σs::AV{<:Real})

Expand Down
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AbstractGPs
using AbstractGPs: AbstractGP, MeanFunction, FiniteGP, ConstMean, GP, ZeroMean,
ConstMean, CustomMean, Xt_A_X, Xt_A_Y, Xt_invA_Y, Xt_invA_X, diag_At_A, diag_At_B,
using AbstractGPs: AbstractGP, MeanFunction, FiniteGP, ConstMean, GP, ZeroMean,
ConstMean, CustomMean, Xt_A_X, Xt_A_Y, Xt_invA_Y, Xt_invA_X, diag_At_A, diag_At_B,
diag_Xt_A_X, diag_Xt_A_Y, diag_Xt_invA_X, diag_Xt_invA_Y, Xtinv_A_Xinv, tr_At_A,
mean_and_cov_diag

Expand All @@ -15,6 +15,7 @@ using Plots
using Random
using Statistics
using Test
using Turing
using Zygote


Expand All @@ -40,6 +41,8 @@ include("test_util.jl")
include(joinpath("latent_gp", "latent_gp.jl"))

include(joinpath("util", "plotting.jl"))

include("turing.jl")

@testset "doctests" begin
DocMeta.setdocmeta!(
Expand Down
25 changes: 25 additions & 0 deletions test/turing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@testset "turing compat" begin
k = SqExponentialKernel()
y = randn(3)
X = randn(3, 1)
x = [rand(1) for _ in 1:3]
@model function GPRegression(y, X)
# Priors.
α ~ LogNormal(0.0, 0.1)
ρ ~ LogNormal(0.0, 1.0)
σ² ~ LogNormal(0.0, 1.0)

# Realized covariance function
kernel = α * transform(SqExponentialKernel(), 1 / ρ)
f = GP(kernel)

# Sampling Distribution.
y ~ f(X, σ²)
end
# Test for matrices
m = GPRegression(y, RowVecs(X))
@test length(sample(m, HMC(0.5, 1), 5)) == 5
# Test for vectors of vector
m = GPRegression(y, x)
@test length(sample(m, HMC(0.5, 1), 5)) == 5
end

2 comments on commit ee483a4

@theogf
Copy link
Member Author

@theogf theogf commented on ee483a4 Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/20985

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.8 -m "<description of version>" ee483a4c2b8bbdba1d2965c94433516b5362fdca
git push origin v0.2.8

Please sign in to comment.