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 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
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*.jl.mem
.DS_Store
/Manifest.toml
test/Manifest.toml
/dev/
/docs/build/
/docs/site/
.vscode/
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"

[compat]
AbstractGPs = "0.2, 0.3"
Expand Down
27 changes: 20 additions & 7 deletions src/GPLikelihoods.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
module GPLikelihoods

using Distributions
using AbstractGPs
using Random
using Distributions
using Functors
using StatsFuns: logistic, softmax

import Distributions
using Random
using StatsFuns

export BernoulliLikelihood,
CategoricalLikelihood,
GaussianLikelihood,
HeteroscedasticGaussianLikelihood,
GaussianLikelihood,
HeteroscedasticGaussianLikelihood,
PoissonLikelihood,
ExponentialLikelihood,
GammaLikelihood
export Link,
ChainLink,
ExpLink,
LogLink,
InvLink,
SqrtLink,
SquareLink,
LogitLink,
LogisticLink,
ProbitLink,
NormalCDFLink,
SoftMaxLink

# Links
include("links.jl")

# Likelihoods
theogf marked this conversation as resolved.
Show resolved Hide resolved
include(joinpath("likelihoods", "bernoulli.jl"))
Expand Down
17 changes: 11 additions & 6 deletions src/likelihoods/bernoulli.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""
BernoulliLikelihood
BernoulliLikelihood(l::AbstractLink=LogisticLink())

Bernoulli likelihood is to be used if we assume that the
uncertainity associated with the data follows a Bernoulli distribution.
The link `l` needs to transform the input `f` to the domain [0, 1]

```math
p(y|f) = Bernoulli(y | f)
p(y|f) = Bernoulli(y | l(f))
```
On calling, this would return a Bernoulli distribution with `f` probability of `true`.
On calling, this would return a Bernoulli distribution with `l(f)` probability of `true`.
"""
struct BernoulliLikelihood end
struct BernoulliLikelihood{Tl<:AbstractLink}
theogf marked this conversation as resolved.
Show resolved Hide resolved
invlink::Tl
end

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

(l::BernoulliLikelihood)(fs::AbstractVector{<:Real}) = Product(Bernoulli.(logistic.(fs)))
(l::BernoulliLikelihood)(f::Real) = Bernoulli(l.invlink(f))

(l::BernoulliLikelihood)(fs::AbstractVector{<:Real}) = Product(Bernoulli.(l.invlink.(fs)))
20 changes: 13 additions & 7 deletions src/likelihoods/categorical.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""
CategoricalLikelihood
CategoricalLikelihood(l::AbstractLink=SoftMaxLink())

Categorical likelihood is to be used if we assume that the
uncertainity associated with the data follows a Categorical distribution.
```math
p(y|f_1, f_2, \\dots, f_{n-1}) = Categorical(y | softmax(f_1, f_2, \\dots, f_{n-1}, 0))
p(y|f_1, f_2, \\dots, f_{n-1}) = Categorical(y | l(f_1, f_2, \\dots, f_{n-1}, 0))
```
On calling, this would return a Categorical distribution with `f_i`
probability of `i` category.
Given an `AbstractVector` [f_1, f_2, ..., f_{n-1}], returns a `Categorical` distribution,
with probabilities given by `l(f_1, f_2, ..., f_{n-1}, 0)`.
"""
struct CategoricalLikelihood end
struct CategoricalLikelihood{Tl<:AbstractLink}
invlink::Tl
end

(l::CategoricalLikelihood)(f::AbstractVector{<:Real}) = Categorical(softmax(vcat(f, 0)))
CategoricalLikelihood() = CategoricalLikelihood(SoftMaxLink())

(l::CategoricalLikelihood)(fs::AbstractVector) = Product(Categorical.(softmax.(vcat.(fs, 0))))
(l::CategoricalLikelihood)(f::AbstractVector{<:Real}) = Categorical(l.invlink(vcat(f, 0)))

function (l::CategoricalLikelihood)(fs::AbstractVector)
return Product(Categorical.(l.invlink.(vcat.(fs, 0))))
end
18 changes: 12 additions & 6 deletions src/likelihoods/exponential.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
"""
ExponentialLikelihood()
ExponentialLikelihood(l::AbstractLink=ExpLink())

Exponential likelihood with scale given by `exp(f)`.
Exponential likelihood with scale given by `l(f)`.

```math
p(y|f) = Exponential(y | exp(f))
p(y|f) = Exponential(y | l(f))
```
"""
struct ExponentialLikelihood end
struct ExponentialLikelihood{Tl<:AbstractLink}
invlink::Tl
end

(l::ExponentialLikelihood)(f::Real) = Exponential(exp(f))
ExponentialLikelihood() = ExponentialLikelihood(ExpLink())

(l::ExponentialLikelihood)(fs::AbstractVector{<:Real}) = Product(Exponential.(exp.(fs)))
(l::ExponentialLikelihood)(f::Real) = Exponential(l.invlink(f))

function (l::ExponentialLikelihood)(fs::AbstractVector{<:Real})
return Product(Exponential.(l.invlink.(fs)))
end
17 changes: 10 additions & 7 deletions src/likelihoods/gamma.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""
GammaLikelihood(α)
GammaLikelihood(α::Real=1.0, l::AbstractLink=ExpLink())

Gamma likelihood with fixed shape `α`.

```math
p(y|f) = Gamma(y | α, θ=exp(f))
p(y|f) = Gamma(y | α, l(f))
```
On calling, this would return a gamma distribution with shape `α` and scale `exp(f)`.
On calling, this would return a gamma distribution with shape `α` and scale `l(f)`.
"""
struct GammaLikelihood{T<:Real}
struct GammaLikelihood{T<:Real,Tl<:AbstractLink}
α::T # shape parameter
invlink::Tl
end

GammaLikelihood() = GammaLikelihood(1.)
GammaLikelihood() = GammaLikelihood(1.0)

GammaLikelihood(α::Real) = GammaLikelihood(α, ExpLink())

@functor GammaLikelihood

(l::GammaLikelihood)(f::Real) = Gamma(l.α, exp(f))
(l::GammaLikelihood)(f::Real) = Gamma(l.α, l.invlink(f))

(l::GammaLikelihood)(fs::AbstractVector{<:Real}) = Product(Gamma.(l.α, exp.(fs)))
(l::GammaLikelihood)(fs::AbstractVector{<:Real}) = Product(Gamma.(l.α, l.invlink.(fs)))
31 changes: 21 additions & 10 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,31 +10,42 @@ 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.σ²)))

"""
HeteroscedasticGaussianLikelihood(σ²)
HeteroscedasticGaussianLikelihood(l::AbstractLink=ExpLink())

Heteroscedastic Gaussian likelihood.
This is a Gaussian likelihood whose mean and the log of whose variance are functions of the
latent process.

```math
p(y|[f, g]) = Normal(y | f, exp(g))
p(y|[f, g]) = Normal(y | f, l(g))
```
On calling, this would return a normal distribution with mean `f` and variance `exp(g)`.
On calling, this would return a normal distribution with mean `f` and std. dev. `l(g)`.
Where `l` is link going from R to R^+
"""
struct HeteroscedasticGaussianLikelihood end
struct HeteroscedasticGaussianLikelihood{Tl<:AbstractLink}
invlink::Tl
end

(::HeteroscedasticGaussianLikelihood)(f::AbstractVector{<:Real}) = Normal(f[1], exp(f[2]))
HeteroscedasticGaussianLikelihood() = HeteroscedasticGaussianLikelihood(ExpLink())

(::HeteroscedasticGaussianLikelihood)(fs::AbstractVector) = MvNormal(first.(fs), exp.(last.(fs)))
function (l::HeteroscedasticGaussianLikelihood)(f::AbstractVector{<:Real})
return Normal(f[1], l.invlink(f[2]))
end

function (l::HeteroscedasticGaussianLikelihood)(fs::AbstractVector)
return MvNormal(first.(fs), l.invlink.(last.(fs)))
end
22 changes: 16 additions & 6 deletions src/likelihoods/poisson.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
"""
PoissonLikelihood()
PoissonLikelihood(l::AbstractLink=ExpLink())

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.
Poisson likelihood with rate defined as `l(f)`.

```math
p(y|f) = Poisson(y | θ=l(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}
invlink::L
end

PoissonLikelihood() = PoissonLikelihood(ExpLink())

(l::PoissonLikelihood)(f::Real) = Poisson(exp(f))
(l::PoissonLikelihood)(f::Real) = Poisson(l.invlink(f))

(l::PoissonLikelihood)(fs::AbstractVector{<:Real}) = Product(Poisson.(exp.(fs)))
(l::PoissonLikelihood)(fs::AbstractVector{<:Real}) = Product(Poisson.(l.invlink.(fs)))
Loading