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 inverse and simplify type structure #48

Merged
merged 4 commits into from
Oct 8, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GPLikelihoods"
uuid = "6031954c-0455-49d7-b3b9-3e1c99afaf40"
authors = ["JuliaGaussianProcesses Team"]
version = "0.2.2"
version = "0.2.3"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
3 changes: 3 additions & 0 deletions src/GPLikelihoods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export Link,
NormalCDFLink,
SoftMaxLink

# Inverses
include("inverse.jl")

# Links
include("links.jl")

Expand Down
27 changes: 27 additions & 0 deletions src/inverse.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
inverse(f)

Return the inverse of function `f`.

This is an internal function to avoid type piracies such as `inv(::typeof(exp))`. At some
point `inv` might support standard Julia functions which would allow us to remove `inverse`.
"""
inverse(f)

inverse(::typeof(exp)) = log
theogf marked this conversation as resolved.
Show resolved Hide resolved
inverse(::typeof(log)) = exp

inverse(::typeof(log1pexp)) = logexpm1
inverse(::typeof(logexpm1)) = log1pexp

inverse(::typeof(inv)) = inv

square(x) = x^2
inverse(::typeof(sqrt)) = square
inverse(::typeof(square)) = sqrt

inverse(::typeof(logit)) = logistic
inverse(::typeof(logistic)) = logit

inverse(::typeof(normcdf)) = norminvcdf
inverse(::typeof(norminvcdf)) = normcdf
77 changes: 29 additions & 48 deletions src/links.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,106 +25,89 @@ end

(l::Link)(x) = l.f(x)

Base.inv(l::Link) = Link(inverse(l.f))

# alias
const LogLink = Link{typeof(log)}
const ExpLink = Link{typeof(exp)}

const InvLink = Link{typeof(inv)}

const SqrtLink = Link{typeof(sqrt)}
const SquareLink = Link{typeof(square)}

const LogitLink = Link{typeof(logit)}
const LogisticLink = Link{typeof(logistic)}

const ProbitLink = Link{typeof(norminvcdf)}
const NormalCDFLink = Link{typeof(normcdf)}

const SoftMaxLink = Link{typeof(softmax)}

"""
LogLink()

`log` link, f:ℝ⁺->ℝ . Its inverse is the [`ExpLink`](@ref).
"""
struct LogLink <: AbstractLink end

(::LogLink)(x) = log(x)

Base.inv(::LogLink) = ExpLink()
LogLink() = Link(log)

"""
ExpLink()

`exp` link, f:ℝ->ℝ⁺. Its inverse is the [`LogLink`](@ref).
"""
struct ExpLink <: AbstractLink end

(::ExpLink)(x) = exp(x)

Base.inv(::ExpLink) = LogLink()
ExpLink() = Link(exp)

"""
InvLink()

`inv` link, f:ℝ/{0}->ℝ/{0}. It is its own inverse.
"""
struct InvLink <: AbstractLink end

(::InvLink)(x) = inv(x)

Base.inv(::InvLink) = InvLink()
InvLink() = Link(inv)

"""
SqrtLink()

`sqrt` link, f:ℝ⁺∪{0}->ℝ⁺∪{0}. Its inverse is the [`SquareLink`](@ref).
"""
struct SqrtLink <: AbstractLink end

(::SqrtLink)(x) = sqrt(x)

Base.inv(::SqrtLink) = SquareLink()
SqrtLink() = Link(sqrt)

"""
SquareLink()

`^2` link, f:ℝ->ℝ⁺∪{0}. Its inverse is the [`SqrtLink`](@ref).
"""
struct SquareLink <: AbstractLink end

(::SquareLink)(x) = x^2

Base.inv(::SquareLink) = SqrtLink()
SquareLink() = Link(square)

"""
LogitLink()

`log(x/(1-x))` link, f:[0,1]->ℝ. Its inverse is the [`LogisticLink`](@ref).
"""
struct LogitLink <: AbstractLink end

(::LogitLink)(x) = logit(x)

Base.inv(::LogitLink) = LogisticLink()
LogitLink() = Link(logit)

"""
LogisticLink()

`exp(x)/(1+exp(-x))` link. f:ℝ->[0,1]. Its inverse is the [`LogitLink`](@ref).
"""
struct LogisticLink <: AbstractLink end

(::LogisticLink)(x) = logistic(x)

Base.inv(::LogisticLink) = LogitLink()
LogisticLink() = Link(logistic)

"""
ProbitLink()

`ϕ⁻¹(y)` link, where `ϕ⁻¹` is the `invcdf` of a `Normal` distribution, f:[0,1]->ℝ.
Its inverse is the [`NormalCDFLink`](@ref).
"""
struct ProbitLink <: AbstractLink end

(::ProbitLink)(x) = norminvcdf(x)

Base.inv(::ProbitLink) = NormalCDFLink()
ProbitLink() = Link(norminvcdf)

"""
NormalCDFLink()

`ϕ(y)` link, where `ϕ` is the `cdf` of a `Normal` distribution, f:ℝ->[0,1].
Its inverse is the [`ProbitLink`](@ref).
"""
struct NormalCDFLink <: AbstractLink end

(::NormalCDFLink)(x) = normcdf(x)

Base.inv(::NormalCDFLink) = ProbitLink()
NormalCDFLink() = Link(normcdf)

(::ChainLink{<:Tuple{LogLink,NormalCDFLink}})(x) = normlogcdf(x) # Specialisation for log + normal cdf

Expand All @@ -135,6 +118,4 @@ Base.inv(::NormalCDFLink) = ProbitLink()
f:ℝⁿ->Sⁿ⁻¹, where Sⁿ⁻¹ is an [(n-1)-simplex](https://en.wikipedia.org/wiki/Simplex)
It has no defined inverse
"""
struct SoftMaxLink <: AbstractLink end

(::SoftMaxLink)(x::AbstractVector{<:Real}) = softmax(x)
SoftMaxLink() = Link(softmax)
32 changes: 32 additions & 0 deletions test/inverse.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@testset "inverse.jl" begin
@testset "square" begin
x = randn()
@test GPLikelihoods.square(x) == x^2
end

@testset "inverse" begin
x = rand()
for f in (
exp,
log,
inv,
log1pexp,
logexpm1,
sqrt,
GPLikelihoods.square,
logit,
logistic,
normcdf,
norminvcdf,
)
g = GPLikelihoods.inverse(f)

# check that definitions are correct
@test g(f(x)) ≈ x
@test f(g(x)) ≈ x

# check that definitions are complete
@test GPLikelihoods.inverse(g) === f
end
end
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 StatsFuns

@testset "GPLikelihoods.jl" begin
include("test_utils.jl")
include("inverse.jl")
include("links.jl")
@testset "likelihoods" begin
include("likelihoods/bernoulli.jl")
Expand Down