# Required methods to sample a multivariate distribution

**URL:** <https://discourse.julialang.org/t/required-methods-to-sample-a-multivariate-distribution/105443>\
**Category:** Statistics\
**Tags:** distributions\
**Created:** [October 26, 2023, 1:29pm UTC](https://discourse.julialang.org/t/required-methods-to-sample-a-multivariate-distribution/105443 "2023-10-26T13:29:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [October 26, 2023, 1:29pm UTC](https://discourse.julialang.org/t/required-methods-to-sample-a-multivariate-distribution/105443/1 "2023-10-26T13:29:40Z")

</div>

Hey,

I have a question, maybe you can answer me.

Following [this `Distributions.jl` documentation](https://juliastats.org/Distributions.jl/stable/extends/#Create-a-Multivariate-Distribution), when I define a multivariate distribution, I implement the two functions :

```julia
function Distributions._rand!(rng::Distributions.AbstractRNG, X::MyDistribution, x::AbstractVector{T}) where {T}
     x .= #something
     return x
end
function Base.rand(rng::Distributions.AbstractRNG,X::MyDistribution)
     x = zeros(length(X)) #allocate
     x .= #something, in fact exactly the same something as the previous function. 
     return x
end

```

I remember that a while ago both were needed, and now it simply became an habit. But:

1. First, I cannot recall why both interfaces would be needed, would you by chance point me to the right place ?
2. Then, I define a lot of distributions, and I wander if I cannot just use **one** method for the second function as:

```julia
function Base.rand(rng::Distributions.AbstractRNG,X<:MyAbstractDistributionFamilly)
     x = zeros(length(X)) #allocate
     Distributions._rand!(rng,X,x)
end

```

and then only define the `Distributions._rand!(..)` version for each subtype.

Edit: This might be related: [`Base.rand` but `Random.rand!`?](https://discourse.julialang.org/t/base-rand-but-random-rand/14174)

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [October 26, 2023, 2:37pm UTC](https://discourse.julialang.org/t/required-methods-to-sample-a-multivariate-distribution/105443/2 "2023-10-26T14:37:29Z")

</div>

According to a thread on Slack with [@Sukera](https://julialang.slack.com/team/UH24GRBLL) and [@Andy Dienes](https://julialang.slack.com/team/U0387EZS4D8), the situation is as follows:

- `Distributions.rand === Random.rand === Base.rand`
- `Random.rand`’s API and `Distributions._rand!`'s API are conflicting with each other and incompatible.

Thus, keeping only the `DIstributions._rand!` looks like it is enough, and everything else can be ditched. At least for the moment.
