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

Adding a Link object #14

Merged
merged 22 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.jl.mem
.DS_Store
/Manifest.toml
test/Manifest.toml
/dev/
/docs/build/
/docs/site/
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ 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"

[compat]
AbstractGPs = "0.2"
Distributions = "0.19, 0.20, 0.21, 0.22, 0.23"
Functors = "0.1"
StatsFuns = "0.9"
julia = "1.3"
9 changes: 6 additions & 3 deletions src/GPLikelihoods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ using Distributions
using AbstractGPs
using Random
using Functors
using StatsFuns: logistic
theogf marked this conversation as resolved.
Show resolved Hide resolved

import Distributions

export GaussianLikelihood, PoissonLikelihood
export Link, LogisticLink
theogf marked this conversation as resolved.
Show resolved Hide resolved

# Links
include(joinpath("likelihoods", "link.jl"))
# Likelihoods
theogf marked this conversation as resolved.
Show resolved Hide resolved
include("likelihoods/gaussian.jl")
include("likelihoods/poisson.jl")

include(joinpath("likelihoods", "gaussian.jl"))
include(joinpath("likelihoods", "poisson.jl"))
end # module
10 changes: 6 additions & 4 deletions src/likelihoods/gaussian.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
GaussianLikelihood(σ²)

Gaussian likelihood with `σ²` variance. This is to be used if we assume that the
Gaussian likelihood with `σ²` variance. This is to be used if we assume that the
uncertainity associated with the data follows a Gaussian distribution.

```math
Expand All @@ -10,13 +10,15 @@ uncertainity associated with the data follows a Gaussian distribution.
On calling, this would return a normal distribution with mean `f` and variance σ².
"""
struct GaussianLikelihood{T<:Real}
σ²::T
σ²::Vector{T}
sharanry marked this conversation as resolved.
Show resolved Hide resolved
end

GaussianLikelihood() = GaussianLikelihood(1e-6)

GaussianLikelihood(σ²::Real) = GaussianLikelihood([σ²])

@functor GaussianLikelihood

(l::GaussianLikelihood)(f::Real) = Normal(f, sqrt(l.σ²))
(l::GaussianLikelihood)(f::Real) = Normal(f, sqrt(first(l.σ²)))

(l::GaussianLikelihood)(fs::AbstractVector{<:Real}) = MvNormal(fs, sqrt(l.σ²))
(l::GaussianLikelihood)(fs::AbstractVector{<:Real}) = MvNormal(fs, sqrt(first(l.σ²)))
21 changes: 21 additions & 0 deletions src/likelihoods/link.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
abstract type AbstractLink end

(f::AbstractLink)(x) = apply(f, x)

struct Link{F} <: AbstractLink
f::F
end

sharanry marked this conversation as resolved.
Show resolved Hide resolved
apply(l::Link, x) = l.f(x)

struct LogisticLink{T<:Real} <: AbstractLink
λ::Vector{T}
end

LogisticLink() = LogisticLink(1.0)

LogisticLink(λ::Real) = LogisticLink([λ])

@functor LogisticLink

apply(l::LogisticLink, x) = first(l.λ) * logistic(x)
14 changes: 11 additions & 3 deletions src/likelihoods/poisson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
Poisson likelihood with rate as exponential of samples from GP `f`. This is to be used if
we assume that the uncertainity associated with the data follows a Poisson distribution.
"""
struct PoissonLikelihood end
struct PoissonLikelihood{L<:AbstractLink}
link::L
end

(l::PoissonLikelihood)(f::Real) = Poisson(exp(f))
PoissonLikelihood() = PoissonLikelihood(Link(exp))

(l::PoissonLikelihood)(fs::AbstractVector{<:Real}) = Product(Poisson.(exp.(fs)))
PoissonLikelihood(f::Function) = PoissonLikelihood(Link(f))

@functor PoissonLikelihood

(l::PoissonLikelihood)(f::Real) = Poisson(l.link(f))

(l::PoissonLikelihood)(fs::AbstractVector{<:Real}) = Product(Poisson.(l.link.(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"
4 changes: 4 additions & 0 deletions test/likelihoods/gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
@test typeof(lik(rand(rng, lfgp.fx))) <: Distribution
@test length(rand(rng, lik(rand(rng, lfgp.fx)))) == 10
@test keys(Functors.functor(lik)[1]) == (:σ²,)

# Test default constructore
l = GaussianLikelihood()
@test l.σ² == [1e-6]
end
13 changes: 13 additions & 0 deletions test/likelihoods/link.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testset "link" begin
using GPLikelihoods: AbstractLink, Link, LogisticLink
f = sin
l = Link(f)
x = rand()
@test l(x) == f(x)

λ = 2.0
l = LogisticLink(λ)
@test l(x) == λ * logistic(x)
l = LogisticLink()
@test l(x) == logistic(x)
end
8 changes: 6 additions & 2 deletions test/likelihoods/poisson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
lik = PoissonLikelihood()
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] == ()
@test lik.link == Link(exp)
@test Functors.functor(lik)[1] == (link = Link{typeof(exp)}(exp),)

lik = PoissonLikelihood(LogisticLink())
@test lik.link isa LogisticLink
end
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ using Test
using Random
using Functors
using Distributions
using StatsFuns

@testset "GPLikelihoods.jl" begin

@testset "likelihoods" begin
include("likelihoods/gaussian.jl")
include("likelihoods/poisson.jl")
include(joinpath("likelihoods", "gaussian.jl"))
include(joinpath("likelihoods", "poisson.jl"))
include(joinpath("likelihoods", "link.jl"))
end

end