Skip to content
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

Add Bernoulli likelihood #15

Merged
merged 5 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/GPLikelihoods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ using StatsFuns: logistic, softmax

import Distributions

export CategoricalLikelihood,
export BernoulliLikelihood,
CategoricalLikelihood,
GaussianLikelihood,
HeteroscedasticGaussianLikelihood,
PoissonLikelihood

# Likelihoods
include("likelihoods/bernoulli.jl")
include("likelihoods/categorical.jl")
include("likelihoods/gaussian.jl")
include("likelihoods/poisson.jl")
Expand Down
16 changes: 16 additions & 0 deletions src/likelihoods/bernoulli.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
BernoulliLikelihood

Bernoulli likelihood is to be used if we assume that the
uncertainity associated with the data follows a Bernoulli distribution.

```math
p(y|f) = Bernoulli(y | f)
```
On calling, this would return a Bernoulli distribution with `f` probability of `true`.
"""
struct BernoulliLikelihood end

(l::BernoulliLikelihood)(f::Real) = Bernoulli(logistic(f))

(l::BernoulliLikelihood)(fs::AbstractVector{<:Real}) = Product(Bernoulli.(logistic.(fs)))
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
AbstractGPs = "0.2"
Distributions = "0.19, 0.20, 0.21, 0.22, 0.23"
Functors = "0.1"
StatsFuns = "0.9"
julia = "1.3"
13 changes: 13 additions & 0 deletions test/likelihoods/bernoulli.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testset "BernoulliLikelihood" begin
rng = MersenneTwister(123)
gp = GP(SqExponentialKernel())
x = rand(rng, 10)
y = rand(rng, 10)
lik = BernoulliLikelihood()
lgp = LatentGP(gp, lik, 1e-5)
lfgp = lgp(x)

@test typeof(lik(rand(rng, lfgp.fx))) <: Distribution
@test length(rand(rng, lik(rand(rng, lfgp.fx)))) == 10
@test Functors.functor(lik)[1] == ()
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Distributions
@testset "GPLikelihoods.jl" begin

@testset "likelihoods" begin
include("likelihoods/bernoulli.jl")
include("likelihoods/categorical.jl")
include("likelihoods/gaussian.jl")
include("likelihoods/poisson.jl")
Expand Down