Skip to content

Commit

Permalink
add method for Distributions.rand(::ResettableRNG, ::Binomial) (#249)
Browse files Browse the repository at this point in the history
* add method for Distributions.rand(::ResettableRNG, ::Binomial

* Fix ResettableRNG dispatch

* Test with MersenneTwister (Xoshiro not available in Julia 1.6)

* bump version

---------

Co-authored-by: Chad Scherrer <[email protected]>
  • Loading branch information
slwu89 and cscherrer authored Feb 26, 2023
1 parent 9960e9b commit a3b8f6d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 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.18.1"
version = "0.18.2"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
4 changes: 2 additions & 2 deletions src/parameterized/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ end
x (0, 1)
end

function Base.rand(rng::AbstractRNG, ::Type, d::Binomial{(:n, :p)})
rand(rng, Dists.Binomial(d.n, d.p))
function Base.rand(rng::AbstractRNG, ::Type{T}, d::Binomial{(:n, :p)}) where {T}
rand(rng, T, Dists.Binomial(d.n, d.p))
end

Binomial(n) = Binomial(n, 0.5)
Expand Down
8 changes: 8 additions & 0 deletions src/resettable-rng.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ for T in vcat(subtypes(Signed), subtypes(Unsigned), subtypes(AbstractFloat))
end
end

function Base.rand(r::ResettableRNG, d::AbstractMeasure)
rand(r.rng, d)
end

function Base.rand(r::ResettableRNG, ::Type{T}, d::AbstractMeasure) where {T}
rand(r.rng, T, d)
end

Base.iterate(r::ResettableRNG) = iterate(r, nothing)

function Base.iterate(r::ResettableRNG, ::Nothing)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ end
@test logdensity_def(Binomial(; n, logitp), y)
@test logdensity_def(Binomial(; n, probitp), y)

rng = ResettableRNG(Random.MersenneTwister())
@test rand(rng, Binomial(n=0, p=1.0)) == 0
@test rand(rng, Binomial(n=10, p=1.0)) == 10

@test_broken logdensity_def(Binomial(n, p), CountingMeasure(ℤ[0:n]), x)
binomlogpdf(n, p, x)
end
Expand Down

2 comments on commit a3b8f6d

@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/78568

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.18.2 -m "<description of version>" a3b8f6d5983567f861740a5f6b970fd670390dea
git push origin v0.18.2

Please sign in to comment.