# Why isn't this random number generation implemented?

**URL:** <https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419>\
**Category:** Questions\
**Tags:** statistics-and-probability\
**Created:** [May 2, 2018, 11:04pm UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419 "2018-05-02T23:04:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chwanguiuc98](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@chwanguiuc98](https://community.wolfram.com/u/chwanguiuc98)\
**Post date:** [May 2, 2018, 11:04pm UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/1 "2018-05-02T23:04:15Z")

</div>

Dear community,

I tried to generate bivariate random numbers from the following code,

```
RandomVariate[
 ProbabilityDistribution[
  Piecewise[{{1/\[Pi], 0 <= Sqrt[x^2 + y^2] <= 1}}], {x, -1, 
   1}, {y, -1, 1}]]

```

but was given a response as follows,

```
RandomVariate::noimp: Sampling from ProbabilityDistribution[\[Piecewise]	1/\[Pi]	0<=Sqrt[Power[<<2>>]+Power[<<2>>]]<=1
0	True

,{\[FormalX]1,-1,1},{\[FormalX]2,-1,1}] is not implemented.

```

The ProbabilityDistribution was taken from an example from the Documentation Center and it worked well with Mean and StandardDeviation. Does anyone have ideas why it won’t work for generating random numbers?

Many thanks,

Chi-Hsiang

---

<div class="post-metadata">

**Author:** ![sjoerds](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sjoerds/32/19589_2.png) [@sjoerds](https://community.wolfram.com/u/sjoerds)\
**Post date:** [May 3, 2018, 8:48am UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/2 "2018-05-03T08:48:01Z")

</div>

I’m guessing that this is because sampling from arbitrary multi-dimensional distributions is generally not a simple problem which they haven’t got around to implement yet. There are some trivial exceptions like `ProbabilityDistribution[1, {x, 0, 1}, {y, 0, 1}]`, but in general it seems like you can only use build-in multi-dimensional distributions with `RandomVariate`.

In your case, you could try to sample the unit disk by transforming the coordinates to `r` and `theta`. This will factorize the distribution so you can draw from two 1D distributions, which should be easy to do.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [May 3, 2018, 8:57am UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/3 "2018-05-03T08:57:54Z")

</div>

In the special case described in the original post, this one should generate the points:

```
RandomPoint[Disk[], 10]

```

Cheers,

Marco

---

<div class="post-metadata">

**Author:** ![chwanguiuc98](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@chwanguiuc98](https://community.wolfram.com/u/chwanguiuc98)\
**Post date:** [May 4, 2018, 3:22am UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/4 "2018-05-04T03:22:38Z")

</div>

Many thanks, Sjoerd and Marco. So it seems, as Sjoerd suggested, Wolfram hasn’t implemented multivariate random number generation for user-defined distributions. It’s certainly possible to use conditional probability for simulation, but let’s hope Wolfram provide handy, automatic functions like RandomVariate for this purpose very soon.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [May 5, 2018, 9:28am UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/5 "2018-05-05T09:28:48Z")

</div>

> Wolfram hasn’t implemented multivariate random number generation for user-defined distributions.

This is not true. It depends on the symbolic form of the distribution. Try a simple polynomial one, and it will likely work.

```
d = ProbabilityDistribution[x^2 + y^4, {x, -1, 1}, {y, -1, 1}, 
  Method -> "Normalize"]

RandomVariate[d]

```

This, along with features like `RandomPoint`, opens up a lot of flexibility for designing an efficient rejection sampling scheme for more complicated distributions too.

---

<div class="post-metadata">

**Author:** ![sjoerds](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sjoerds/32/19589_2.png) [@sjoerds](https://community.wolfram.com/u/sjoerds)\
**Post date:** [May 5, 2018, 2:45pm UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/6 "2018-05-05T14:45:55Z")

</div>

Ok, I didn’t expect that to work. I tried some other distributions myself and didn’t find any other ones that worked even when while attempting some really elementary ones (like a 2D normal distribution). It’s good to know polynomials work, but the fact remains that you basically have to be creative to get it to work and there are no guarantees. It’s not like there’s a robust MCMC sampler that just works for arbitrary PDFs. Something like that is quite high on my wish list, frankly speaking, and I’m a bit surprised that WL seems to be behind the curve with that, given how many applications there are for functionality like that.

---

<div class="post-metadata">

**Author:** ![chwanguiuc98](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@chwanguiuc98](https://community.wolfram.com/u/chwanguiuc98)\
**Post date:** [May 6, 2018, 6:02am UTC](https://community.wolfram.com/t/why-isnt-this-random-number-generation-implemented/12419/7 "2018-05-06T06:02:32Z")

</div>

Yes, I found a lack of robust MCMC in Mathematica is surprising, and disappointing, too! In the last couple decades full Bayesian analysis has become fairly popular and MCMC is at present one of the most powerful methods to obtain any posterior distributions. In fact my need was to obtain a simulated posterior distribution that involves logistic functions and beta distributions, both of which are basic functions/distributions for categorical data analysis.

Hope Wolfram ups its efforts in improving robust functional capabilities for statistical data analysis, particularly Bayesian data analysis!
