Hi all,
I want to define a custom distribution that is a modification of the Geometric distribution (the one that has support {1,2,3,...}
) with an additional “atom” at the event 0
. I.e. this is a well-defined distribution on the natural numbers {0, 1, 2, ...}
. The code below correctly constructs this distribution in Julia using a mixture model from Distributions.jl, which is already quite nice.
However, I noticed a type instability when sampling random numbers from that distribution. It seems this currently slows down my code, and I don’t understand what’s going on.
using Distributions
a = 0.2
b = 0.995
d1 = Dirac(0)
d2 = Geometric(1-b) + 1
d = MixtureModel([d1, d2], [a, 1-a])
@code_warntype rand(d)
The @code_warntype
output reads:
MethodInstance for rand(::MixtureModel{Univariate, Discrete, Distribution{Univariate, Discrete}, Categorical{Float64, Vector{Float64}}})
from rand(s::Sampleable, dims::Int64...) @ Distributions ~/.julia/packages/Distributions/UaWBm/src/genericrand.jl:22
Arguments
#self#::Core.Const(rand)
s::MixtureModel{Univariate, Discrete, Distribution{Univariate, Discrete}, Categorical{Float64, Vector{Float64}}}
dims::Tuple{}
Body::Any
1 ─ %1 = Distributions.rand::Core.Const(rand)
│ %2 = Distributions.default_rng()::Core.Const(Random.TaskLocalRNG())
│ %3 = Core.tuple(%2, s)::Tuple{Random.TaskLocalRNG, MixtureModel{Univariate, Discrete, Distribution{Univariate, Discrete}, Categorical{Float64, Vector{Float64}}}}
│ %4 = Core._apply_iterate(Base.iterate, %1, %3, dims)::Any
└── return %4
Any help really appreciated!