Skip to content

Commit

Permalink
Fix exponential (#164)
Browse files Browse the repository at this point in the history
* Make _logpdf use Real (for autodiff friendliness)

* exponential updates

* bump version
  • Loading branch information
cscherrer authored Oct 4, 2021
1 parent 8ddd2a5 commit 0890e24
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MeasureTheory"
uuid = "eadaa1a4-d27c-401d-8699-e962e1bbc33b"
authors = ["Chad Scherrer <[email protected]> and contributors"]
version = "0.11.4"
version = "0.12.0"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
3 changes: 2 additions & 1 deletion src/MeasureTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function logpdf(d::AbstractMeasure, x)
_logpdf(d, x, logdensity(d,x))
end

function _logpdf(d::AbstractMeasure, x, acc::Float64)
function _logpdf(d::AbstractMeasure, x, acc::Real)
β = basemeasure(d)
d === β && return acc

Expand Down Expand Up @@ -104,6 +104,7 @@ include("combinators/weighted.jl")
include("combinators/product.jl")
include("combinators/transforms.jl")
include("combinators/chain.jl")
# include("combinators/basemeasure.jl")

include("distributions.jl")

Expand Down
71 changes: 66 additions & 5 deletions src/parameterized/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

export Exponential

@parameterized Exponential(λ) Lebesgue(ℝ₊)
@parameterized Exponential(β) Lebesgue(ℝ₊)


@kwstruct Exponential(λ)
@kwstruct Exponential()

function logdensity(d::Exponential{()} , x)
Expand All @@ -14,21 +14,82 @@ end

Base.rand(rng::AbstractRNG, T::Type, μ::Exponential{()}) = randexp(rng,T)

TV.as(::Exponential) = asℝ₊


##########################
# Scale β

@kwstruct Exponential(β)

function Base.rand(rng::AbstractRNG, T::Type, d::Exponential{(:β,)})
randexp(rng, T) * d.β
end

function logdensity(d::Exponential{(:β,)}, x)
z = x / d.β
return logdensity(Exponential(), z) - log(d.β)
end

distproxy(d::Exponential{(:β,)}) = Dists.Exponential(d.β)

asparams(::Type{<:Exponential}, ::Val{:β}) = asℝ₊

##########################
# Log-Scale logβ


@kwstruct Exponential(logβ)

function Base.rand(rng::AbstractRNG, T::Type, d::Exponential{(:logβ,)})
randexp(rng, T) * exp(d.logβ)
end

function logdensity(d::Exponential{(:logβ,)}, x)
z = x * exp(-d.logβ)
return logdensity(Exponential(), z) - d.logβ
end

distproxy(d::Exponential{(:logβ,)}) = Dists.Exponential(exp(d.logβ))

asparams(::Type{<:Exponential}, ::Val{:logβ}) = asℝ




##########################
# Rate λ

@kwstruct Exponential(λ)

function Base.rand(rng::AbstractRNG, T::Type, d::Exponential{(:λ,)})
randexp(rng, T) / d.λ
end

TV.as(::Exponential) = asℝ₊

function logdensity(d::Exponential{(:λ,)}, x)
z = x * d.λ
return logdensity(Exponential(), z) + log(d.λ)
end

distproxy(d::Exponential{(:λ,)}) = Dists.Exponential(d.λ)
distproxy(d::Exponential{(:λ,)}) = Dists.Exponential(1/d.λ)

asparams(::Type{<:Exponential}, ::Val{:λ}) = asℝ₊

##########################
# Log-Rate logλ


@kwstruct Exponential(logλ)

function Base.rand(rng::AbstractRNG, T::Type, d::Exponential{(:logλ,)})
randexp(rng, T) * exp(-d.logλ)
end

function logdensity(d::Exponential{(:logλ,)}, x)
z = x * exp(d.logλ)
return logdensity(Exponential(), z) + d.logλ
end

distproxy(d::Exponential{(:logλ,)}) = Dists.Exponential(exp(-d.logλ))

asparams(::Type{<:Exponential}, ::Val{:logλ}) = asℝ
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ end
@test_broken logdensity(Binomial(n,p), CountingMeasure(ℤ[0:n]), x) binomlogpdf(n,p,x)
end

@testset "Exponential" begin
r = rand(MersenneTwister(123), Exponential(2))
@test r rand(MersenneTwister(123), Exponential=2))
@test r rand(MersenneTwister(123), Exponential=0.5))
@test r rand(MersenneTwister(123), Exponential(logβ=log(2)))
@test r rand(MersenneTwister(123), Exponential(logλ=log(0.5)))

= logdensity(Exponential(2), r)
@test logdensity(Exponential=2), r)
@test logdensity(Exponential=0.5), r)
@test logdensity(Exponential(logβ=log(2)), r)
@test logdensity(Exponential(logλ=log(0.5)), r)
end

@testset "NegativeBinomial" begin
D = NegativeBinomial{(:r, :p)}
par = transform(asparams(D), randn(2))
Expand Down

2 comments on commit 0890e24

@cscherrer
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/46085

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.12.0 -m "<description of version>" 0890e24db5bf72d5aab67e1b5ce3f4eef30b2163
git push origin v0.12.0

Please sign in to comment.