-
Notifications
You must be signed in to change notification settings - Fork 32
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 method for Distributions.rand(::ResettableRNG, ::Binomial) #249
Conversation
Hi @slwu89 , looks like I have some catching up to do:
I like the simplicity of your approach, but it specializes for function Dists.rand(rng::ResettableRNG, d::Dists.Binomial)
rand(rng.rng, d)
end we'd usually instead write function Dists.rand(rng::ResettableRNG, ::Type{T}, d::Dists.Binomial) where {T}
rand(rng.rng, T, d)
end The idea is that the |
Thanks @cscherrer! Ok, that all makes sense. I had a feeling that the specialization for that specific distribution wasn't necessary but I'm not fully familiar with how the internals are designed. And thanks for the welcome, I'm looking forward to getting more familiar with this package! For what its worth the stack trace of my failure MWE (pasted at end of this comment) is below, it looks to me like the "crucial" part is around frame 4, where the call hits this line in Distributions.jl https://github.com/JuliaStats/Distributions.jl/blob/master/src/samplers/binomial.jl#L52 ERROR: MethodError: no method matching rng_native_52(::ResettableRNG{Xoshiro, Int64})
Closest candidates are:
rng_native_52(::Xoshiro) at ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/Xoshiro.jl:75
rng_native_52(::TaskLocalRNG) at ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/Xoshiro.jl:114
rng_native_52(::RandomDevice) at ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/RNGs.jl:36
...
Stacktrace:
[1] rand(r::ResettableRNG{Xoshiro, Int64}, #unused#::Random.SamplerTrivial{Random.UInt52Raw{UInt64}, UInt64})
@ Random ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/generation.jl:114
[2] rand(rng::ResettableRNG{Xoshiro, Int64}, X::Random.UInt52Raw{UInt64})
@ Random ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/Random.jl:254
[3] randexp(rng::ResettableRNG{Xoshiro, Int64})
@ Random ~/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.8/Random/src/normal.jl:118
[4] rand(rng::ResettableRNG{Xoshiro, Int64}, s::Distributions.BinomialGeomSampler)
@ Distributions ~/.julia/packages/Distributions/bQ6Gj/src/samplers/binomial.jl:52
[5] rand(rng::ResettableRNG{Xoshiro, Int64}, d::Distributions.Binomial{Float64})
@ Distributions ~/.julia/packages/Distributions/bQ6Gj/src/univariate/discrete/binomial.jl:139
[6] rand
@ ~/.julia/packages/MeasureTheory/y4m8h/src/parameterized/binomial.jl:49 [inlined]
[7] rand
@ ~/.julia/packages/MeasureTheory/y4m8h/src/combinators/product.jl:50 [inlined]
[8] rand(rng::ResettableRNG{Xoshiro, Int64}, d::ProductMeasure{Vector{Binomial{(:n, :p), Tuple{Int64, Float64}}}})
@ MeasureBase ~/.julia/packages/MeasureBase/1ryrV/src/rand.jl:7
[9] top-level scope MWE: using Random
using MeasureTheory
rng = ResettableRNG(Random.Xoshiro(), 6542022242862247233)
mu = ProductMeasure([Binomial(n = 990, p = 0.00995017), Binomial(n = 10, p = 0.0246901)])
rand(rng, mu) |
Codecov ReportBase: 46.11% // Head: 45.98% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #249 +/- ##
==========================================
- Coverage 46.11% 45.98% -0.14%
==========================================
Files 43 43
Lines 1340 1344 +4
==========================================
Hits 618 618
- Misses 722 726 +4
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Hi @slwu89 , I think I got the dispatch problems fixed. Here's your MWE now (using Julia 1.9, because that's just what I have loaded) julia> using Random
julia> using MeasureTheory
julia> rng = ResettableRNG(Random.Xoshiro(), 6542022242862247233)
ResettableRNG(::Xoshiro, 6542022242862247233)
julia> mu = ProductMeasure([Binomial(n = 990, p = 0.00995017), Binomial(n = 10, p = 0.0246901)])
ProductMeasure([Binomial(n = 990, p = 0.00995017), Binomial(n = 10, p = 0.0246901)])
julia> rand(rng, mu)
2-element Vector{Int64}:
12
1 |
I think this is ready to go once tests pass. Thanks for catching this and starting on the fix! I changed the solution, but it was very helpful to have the test you added to make sure things are working properly. |
Thanks @cscherrer! Great to see the "official" solution too, helps me understand better the design of the package. |
This should address #248, which actually didn't have anything to do with product measure, but rather a method for the Binomial distribution. Also added a test to check for this behavior; it would fail previously.