From 306a7c12eb5a2d357db92de794a8175c0db3b05b Mon Sep 17 00:00:00 2001 From: Ross Viljoen Date: Wed, 21 Jul 2021 12:47:38 +0100 Subject: [PATCH 1/2] Added Gamma and Exponential likelihoods --- src/GPLikelihoods.jl | 6 +++++- src/likelihoods/exponential.jl | 14 ++++++++++++++ src/likelihoods/gamma.jl | 21 +++++++++++++++++++++ test/likelihoods/exponential.jl | 4 ++++ test/likelihoods/gamma.jl | 4 ++++ test/runtests.jl | 2 ++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/likelihoods/exponential.jl create mode 100644 src/likelihoods/gamma.jl create mode 100644 test/likelihoods/exponential.jl create mode 100644 test/likelihoods/gamma.jl diff --git a/src/GPLikelihoods.jl b/src/GPLikelihoods.jl index 8d0ec54..0fe5087 100644 --- a/src/GPLikelihoods.jl +++ b/src/GPLikelihoods.jl @@ -12,12 +12,16 @@ export BernoulliLikelihood, CategoricalLikelihood, GaussianLikelihood, HeteroscedasticGaussianLikelihood, - PoissonLikelihood + PoissonLikelihood, + ExponentialLikelihood, + GammaLikelihood # Likelihoods include("likelihoods/bernoulli.jl") include("likelihoods/categorical.jl") include("likelihoods/gaussian.jl") include("likelihoods/poisson.jl") +include("likelihoods/gamma.jl") +include("likelihoods/exponential.jl") end # module diff --git a/src/likelihoods/exponential.jl b/src/likelihoods/exponential.jl new file mode 100644 index 0000000..ded96d7 --- /dev/null +++ b/src/likelihoods/exponential.jl @@ -0,0 +1,14 @@ +""" + ExponentialLikelihood() + +Exponential likelihood with scale given by `exp(f)`. + +```math + p(y|f) = Exponential(y | exp(f)) +``` +""" +struct ExponentialLikelihood end + +(l::ExponentialLikelihood)(f::Real) = Exponential(exp(f)) + +(l::ExponentialLikelihood)(fs::AbstractVector{<:Real}) = Product(Exponential.(exp.(fs))) diff --git a/src/likelihoods/gamma.jl b/src/likelihoods/gamma.jl new file mode 100644 index 0000000..cde7740 --- /dev/null +++ b/src/likelihoods/gamma.jl @@ -0,0 +1,21 @@ +""" + GammaLikelihood(α) + +Gamma likelihood with fixed shape `α`. + +```math + p(y|f) = Gamma(y | α, θ=exp(f)) +``` +On calling, this would return a gamma distribution with shape `α` and scale `exp(f)`. +""" +struct GammaLikelihood{T<:Real} + α::T # shape parameter +end + +GammaLikelihood() = GammaLikelihood(1.) + +@functor GammaLikelihood + +(l::GammaLikelihood)(f::Real) = Gamma(l.α, exp(f)) + +(l::GammaLikelihood)(fs::AbstractVector{<:Real}) = Product(Gamma.(l.α, exp.(fs))) diff --git a/test/likelihoods/exponential.jl b/test/likelihoods/exponential.jl new file mode 100644 index 0000000..c988337 --- /dev/null +++ b/test/likelihoods/exponential.jl @@ -0,0 +1,4 @@ +@testset "ExponentialLikelihood" begin + lik = ExponentialLikelihood() + test_interface(lik, SqExponentialKernel(), rand(10)) +end diff --git a/test/likelihoods/gamma.jl b/test/likelihoods/gamma.jl new file mode 100644 index 0000000..72f474b --- /dev/null +++ b/test/likelihoods/gamma.jl @@ -0,0 +1,4 @@ +@testset "GammaLikelihood" begin + lik = GammaLikelihood(1.) + test_interface(lik, SqExponentialKernel(), rand(10); functor_args=(:α,)) +end diff --git a/test/runtests.jl b/test/runtests.jl index db7ba16..2edc2e0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,6 +14,8 @@ using Distributions include("likelihoods/categorical.jl") include("likelihoods/gaussian.jl") include("likelihoods/poisson.jl") + include("likelihoods/gamma.jl") + include("likelihoods/exponential.jl") end end From ff89e7f881c35c741349986531ddd1748543b6da Mon Sep 17 00:00:00 2001 From: Ross Viljoen Date: Wed, 21 Jul 2021 14:10:13 +0100 Subject: [PATCH 2/2] Use `joinpath` --- src/GPLikelihoods.jl | 12 ++++++------ test/runtests.jl | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/GPLikelihoods.jl b/src/GPLikelihoods.jl index 0fe5087..fa5cd83 100644 --- a/src/GPLikelihoods.jl +++ b/src/GPLikelihoods.jl @@ -17,11 +17,11 @@ export BernoulliLikelihood, GammaLikelihood # Likelihoods -include("likelihoods/bernoulli.jl") -include("likelihoods/categorical.jl") -include("likelihoods/gaussian.jl") -include("likelihoods/poisson.jl") -include("likelihoods/gamma.jl") -include("likelihoods/exponential.jl") +include(joinpath("likelihoods", "bernoulli.jl")) +include(joinpath("likelihoods", "categorical.jl")) +include(joinpath("likelihoods", "gaussian.jl")) +include(joinpath("likelihoods", "poisson.jl")) +include(joinpath("likelihoods", "gamma.jl")) +include(joinpath("likelihoods", "exponential.jl")) end # module diff --git a/test/runtests.jl b/test/runtests.jl index 2edc2e0..33171d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,12 +10,12 @@ using Distributions include("test_utils.jl") @testset "likelihoods" begin - include("likelihoods/bernoulli.jl") - include("likelihoods/categorical.jl") - include("likelihoods/gaussian.jl") - include("likelihoods/poisson.jl") - include("likelihoods/gamma.jl") - include("likelihoods/exponential.jl") + include(joinpath("likelihoods", "bernoulli.jl")) + include(joinpath("likelihoods", "categorical.jl")) + include(joinpath("likelihoods", "gaussian.jl")) + include(joinpath("likelihoods", "poisson.jl")) + include(joinpath("likelihoods", "gamma.jl")) + include(joinpath("likelihoods", "exponential.jl")) end end