JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`rand(::Sampleable{Multivariate}, 0)` errors

btmit opened this issue · comments

Often we need to draw a number of samples from a distribution where that number might sometimes be 0: rand(d, 0). For custom multivariate distributions this throws an error. It seems to work for the built-in multivariate (e.g. MvNormal) and for custom univariate distributions.

using Distributions, Random

struct Foo <: Sampleable{Multivariate, Continuous} end

Base.length(::Foo) = 3
Base.eltype(::Foo) = Float64
Distributions._rand!(rng::AbstractRNG, ::Foo, x::AbstractVector{<:Real}) = Distributions.rand!(rng, x)

rand(Foo(), 0)

ERROR: BoundsError: attempt to access 3×0 Matrix{Float64} at index [1:3, 1]
Stacktrace:
[1] throw_boundserror(A::Matrix{Float64}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Int64})
@ Base ./abstractarray.jl:734
[2] checkbounds
@ ./abstractarray.jl:699 [inlined]
[3] view
@ ./subarray.jl:179 [inlined]
[4] EachVariate
@ ~/.julia/packages/Distributions/km0AH/src/eachvariate.jl:10 [inlined]
[5] eachvariate
@ ~/.julia/packages/Distributions/km0AH/src/eachvariate.jl:31 [inlined]
[6] _rand!(rng::TaskLocalRNG, s::Foo, x::Matrix{Float64})
@ Distributions ~/.julia/packages/Distributions/km0AH/src/genericrand.jl:116
[7] rand!
@ Distributions ~/.julia/packages/Distributions/km0AH/src/genericrand.jl:108 [inlined]
[8] rand
@ Distributions ~/.julia/packages/Distributions/km0AH/src/multivariates.jl:23 [inlined]
[9] rand(s::Foo, dims::Int64)
@ Distributions ~/.julia/packages/Distributions/km0AH/src/genericrand.jl:22
[10] top-level scope
@ REPL[38]:1