# Problem with RandomVariate in Mathematica 10 (and 9) Is it a bug?

**URL:** <https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465>\
**Category:** Questions\
**Tags:** mathematica, statistics-and-probability\
**Created:** [September 29, 2014, 12:17pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465 "2014-09-29T12:17:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aramos](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@aramos](https://community.wolfram.com/u/aramos)\
**Post date:** [September 29, 2014, 12:17pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/1 "2014-09-29T12:17:37Z")

</div>

Hello,

I have a problema with RandomVariate in Mathematica 10 and 9, in the sense that  
for a specific distribution constructed by myself, the RandomVariate sample does not follow  
the distribution the sample comes from.

I would like to discard that it be a specific problem of my machine or my installation.

Thus I would be grateful if anyone of you could run the attached notebook with Mathematica 10  
and send it back to me. I have scanned the notebook and it is free from viruses according  
to my antivirus.

Thanks in advance,  
Arturo Ramos

---

<div class="post-metadata">

**Author:** ![jfriedman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jfriedman/32/35953_2.png) [@jfriedman](https://community.wolfram.com/u/jfriedman)\
**Post date:** [September 29, 2014, 9:13pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/2 "2014-09-29T21:13:31Z")

</div>

Here you go! Same result as you, it appears.

```
In[1]:= $Version

Out[1]= "10.0 for Mac OS X x86 (64-bit) (June 29, 2014)"

```

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [September 30, 2014, 4:26am UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/3 "2014-09-30T04:26:25Z")

</div>

You can always make a function that is hard to numerically integrate. In such a case, it’s sometimes hard to know whether you should consider it to be a bug or not. Similarly, it is always possible to make a PDF that doesn’t get sampled well enough at the tails with RandomVariate for numerical reasons. And it’s hard to know whether it should be considered a bug or not. There isn’t a always a clear yes or no answer.

It looks like it’s not RandomVariate isn’t sampling well at some very extreme tails. If you ask it to make the ProbabilityDistribution of f[Exp[x]] Exp[x] instead of f to make the tails more reasonable, everything seems to work fine.

There’s a couple of things you can try here to help this to work. The first I would try is to rewrite as a Piecewise function. The function f seems numerically complicated and this should help simplify things down a bit. Can you reproduce the problem with simpler values?

The function f doesn’t integrate easily with NIntegrate. This might be better fixed by using a Piecewise function If something is difficult to numerically integrate (say with NIntegrate) it stands to reason that you’d have a hard time making a very good random number generator from it.

---

<div class="post-metadata">

**Author:** ![aramos](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@aramos](https://community.wolfram.com/u/aramos)\
**Post date:** [September 30, 2014, 10:13am UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/4 "2014-09-30T10:13:59Z")

</div>

Thanks a lot Jesse, you have been of great help!.

Best,  
Arturo

---

<div class="post-metadata">

**Author:** ![aramos](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@aramos](https://community.wolfram.com/u/aramos)\
**Post date:** [September 30, 2014, 10:17am UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/5 "2014-09-30T10:17:55Z")

</div>

Thanks a lot Sean,

You have given me a pair of good ideas and  
now I could write my code more efficiently adapted to the mathematics in play.

In fact, with the Exp transformation anything works well and the Piecewise command  
is something I will try.

Thanks again!

Best,  
Arturo

---

<div class="post-metadata">

**Author:** ![aramos](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@aramos](https://community.wolfram.com/u/aramos)\
**Post date:** [September 30, 2014, 5:16pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/6 "2014-09-30T17:16:04Z")

</div>

Dear Sean,

Now I have tried the sampling of f[Exp[y]]Exp[y] and works very well.

I have also tried using Piecewise[] instead of the HeavisideTheta[]  
and the result is that the computations are quite slower with Piecewise[]  
and it does not solve the initial problem. So I keep the first idea for the future!

Thanks again and all the best.

---

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [September 30, 2014, 5:17pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/7 "2014-09-30T17:17:08Z")

</div>

A developer gave me this suggestion,

= = =  
He is attempting to use ProbabilityDistribution to model a mix of  
non-overlapping distributions.

His distribution is equivalent to

```
spd = SplicedDistribution[{0.03673118299607324`, 0.8606546277105262`,
   0.10261418929340049`}, {0, 151, 12846,
   Infinity}, {PowerDistribution[1/151, 1.8923],
   BetaPrimeDistribution[1, 2701/10000, 5000/3333,
    495.9127480196388`], ParetoDistribution[12846, 3253/2500]}];

```

Now the sampling is faster and is more precise:

```
In[317]:= AbsoluteTiming[mpf = RandomVariate[spd, 10^6];]

Out[317]= {1.190119, Null} 

```

= = =

---

<div class="post-metadata">

**Author:** ![aramos](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@aramos](https://community.wolfram.com/u/aramos)\
**Post date:** [September 30, 2014, 5:24pm UTC](https://community.wolfram.com/t/problem-with-randomvariate-in-mathematica-10-and-9-is-it-a-bug/3465/8 "2014-09-30T17:24:39Z")

</div>

Thanks Bruce,

It is another good idea to take into account.
