Skip to content

Commit

Permalink
add beta binomial distribution (#234)
Browse files Browse the repository at this point in the history
* add beta binomial distribution

* run JuliaFormatter

* Apply suggestions from code review

Co-authored-by: Chad Scherrer <[email protected]>

* remove proxy, bump version

Co-authored-by: Chad Scherrer <[email protected]>
  • Loading branch information
nignatiadis and cscherrer authored Aug 30, 2022
1 parent 6a42ef6 commit 5311dff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 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.17.2"
version = "0.17.3"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
1 change: 1 addition & 0 deletions src/MeasureTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ include("parameterized/binomial.jl")
include("parameterized/multinomial.jl")
include("parameterized/lkj-cholesky.jl")
include("parameterized/negativebinomial.jl")
include("parameterized/betabinomial.jl")
include("parameterized/gamma.jl")
include("parameterized/snedecorf.jl")
include("parameterized/inverse-gaussian.jl")
Expand Down
37 changes: 37 additions & 0 deletions src/parameterized/betabinomial.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Beta-Binomial distribution

export BetaBinomial
import Base
using SpecialFunctions

@parameterized BetaBinomial(n, α, β)

basemeasure(d::BetaBinomial) = CountingMeasure()

testvalue(::BetaBinomial) = 0

@kwstruct BetaBinomial(n, α, β)

function Base.rand(
rng::AbstractRNG,
::Type{T},
d::BetaBinomial{(:n, :α, :β)},
) where {T}
k = rand(rng, T, Beta(d.α, d.β))
return rand(rng, T, Binomial(d.n, k))
end

@inline function insupport(d::BetaBinomial, x)
isinteger(x) && 0 x d.n
end

@inline function logdensity_def(d::BetaBinomial{(:n, :α, :β)}, y)
(n, α, β) = (d.n, d.α, d.β)
logbinom = -log1p(n) - logbeta(y + 1, n - y + 1)
lognum = logbeta(y + α, n - y + β)
logdenom = logbeta(α, β)
return logbinom + lognum - logdenom
end

asparams(::Type{<:BetaBinomial}, ::StaticSymbol{:α}) = asℝ₊
asparams(::Type{<:BetaBinomial}, ::StaticSymbol{:β}) = asℝ₊
15 changes: 10 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ test_measures = Any[
Bernoulli(0.2)
Beta(2, 3)
Binomial(10, 0.3)
BetaBinomial(10, 2, 3)
Cauchy()
Dirichlet(ones(3))
Exponential()
Expand Down Expand Up @@ -277,7 +278,7 @@ end

@testset "Product of Diracs" begin
x = randn(3)
t = as(productmeasure(Dirac.(x)))
t = as(productmeasure(Dirac.(x)))
@test transform(t, []) == x
end

Expand All @@ -297,7 +298,7 @@ end

# chain = Chain(kernel, μ)

# dyniterate(iter::TimeLift, ::Nothing) = dyniterate(iter, 0=>nothing)
# dyniterate(iter::TimeLift, ::Nothing) = dyniterate(iter, 0=>nothing)
# tr1 = trace(TimeLift(chain), nothing, u -> u[1] > 15)
# tr2 = trace(TimeLift(rand(Random.GLOBAL_RNG, chain)), nothing, u -> u[1] > 15)
# collect(Iterators.take(chain, 10))
Expand Down Expand Up @@ -348,8 +349,8 @@ end
# NOTE: The `test_broken` below are mostly because of the change to `Affine`.
# For example, `Normal{(:μ,:σ)}` is now `Affine{(:μ,:σ), Normal{()}}`.
# The problem is not really with these measures, but with the tests
# themselves.
#
# themselves.
#
# We should instead probably be doing e.g.
# `D = typeof(Normal(μ=0.3, σ=4.1))`

Expand All @@ -371,6 +372,10 @@ end
@test repro(Beta, (, ))
end

@testset "BetaBinomial" begin
@test repro(BetaBinomial, (:n, , ), (n = 10,))
end

@testset "Cauchy" begin
@test_broken repro(Cauchy, (, ))
end
Expand Down Expand Up @@ -652,7 +657,7 @@ end
end

x = rand(d)

@test logdensityof(d, x) isa Real
end

Expand Down

2 comments on commit 5311dff

@cscherrer
Copy link
Collaborator

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/67376

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.17.3 -m "<description of version>" 5311dff7bc00b5aad0f2fb0dd655fb8545ac50cf
git push origin v0.17.3

Please sign in to comment.