# New Distribution Stack Overflow

**URL:** <https://discourse.julialang.org/t/new-distribution-stack-overflow/77128>\
**Category:** Statistics\
**Tags:** distributions\
**Created:** [February 26, 2022, 3:43pm UTC](https://discourse.julialang.org/t/new-distribution-stack-overflow/77128 "2022-02-26T15:43:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![enweg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enweg/32/24159_2.png) [@enweg](https://discourse.julialang.org/u/enweg)\
**Post date:** [February 26, 2022, 3:43pm UTC](https://discourse.julialang.org/t/new-distribution-stack-overflow/77128/1 "2022-02-26T15:43:24Z")

</div>

I am currently trying to implement a distribution using the Distributions.jl interface. However, when following the guidelines mentioned here for multivariate distributions ([Create New Samplers and Distributions · Distributions.jl](https://juliastats.org/Distributions.jl/stable/extends/)) I always end up with a stack overflow and I absolutely cannot figure out why.

Here is a small example:

```julia
struct Mean_Shift_Normal <: Sampleable{Multivariate, Continuous}
    shift::Real
    size::Int
end

ms = Mean_Shift_Normal(0.5, 2)

Base.length(s::Mean_Shift_Normal) = s.size

function _rand!(rng::AbstractRNG, s::Mean_Shift_Normal, x::AbstractVector{<:Real})
    z = rand(MvNormal(zeros(s.size), ones(s.size)))
    z = z .+ s.shift
    x .= z
    return x
end  

```

calling `rand(ms)` results in a stack overflow.

If anyone has an idea why this is happening or what I am doing wrong, I would appreciate any help.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 26, 2022, 4:26pm UTC](https://discourse.julialang.org/t/new-distribution-stack-overflow/77128/2 "2022-02-26T16:26:59Z")

</div>

I tried a bit without success to understand the stack overflow (which BTW doesn’t seem to touch your \_rand! ever) and then noticed the documentation for the [current version](https://juliastats.org/Distributions.jl/v0.25/extends/#Create-a-Multivariate-Distribution) seems to have different interface requirements.

---

<div class="post-metadata">

**Author:** ![enweg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enweg/32/24159_2.png) [@enweg](https://discourse.julialang.org/u/enweg)\
**Post date:** [March 13, 2022, 6:22pm UTC](https://discourse.julialang.org/t/new-distribution-stack-overflow/77128/3 "2022-03-13T18:22:07Z")

</div>

Sorry for the late reply. I think the mistake is actually a rather silly one. If I replace the function with

```julia
Distributions._rand!(rng::AbstractRNG, s::Mean_Shift_Normal, x::AbstractVector{<:Real}) = begin
       z = rand(MvNormal(zeros(s.size), ones(s.size)))
       z = z .+ s.shift
       x .= z
       x
end

```

then everything works fine. Still do not understand though why it is giving me a stack overflow instead of throwing a not implemented error or similar.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 13, 2022, 6:48pm UTC](https://discourse.julialang.org/t/new-distribution-stack-overflow/77128/4 "2022-03-13T18:48:40Z")

</div>

> [@enweg](#):
>
> Still do not understand though why it is giving me a stack overflow instead of throwing a not implemented error or similar.

Ah, should have thought of that: the same undefined(?) behavior occurs if you try to extend `Base` functions for example. So you should always either `import` the function beforehand or specify the module as you did.

Edit: did a quick search and found no direct explanation in the Julia documentation. Does anyone else have a reference at hand?
