Optimization of custom probability distibutions?

2

Consider the two tests:

Timing[Do[RandomVariate[NormalDistribution[0.5, 0.3]], {200}]]  

{0.001188, Null}

MyNormalDistribution[mu_,sig_] = ProbabilityDistribution[1/(Sqrt[2 \[Pi]]sig)
             Exp[-(x - mu)^2/(2 sig^2))], {x, -\[Infinity], \[Infinity]}];
Timing[Do[RandomVariate[MyNormalDistribution[0.5, 0.3]], {200}]]

{3.375444, Null}

So it seems that the built in normal distribution is much faster (~3000 times faster!) then the custom one. My question is how is this optimization done and whether it can be implemented for custom distributions as well?

JeffDror

Posted 2015-04-24T14:47:07.590

Reputation: 1 740

1

Related questions: (1124), (20067), (56180), (75303)

– dr.blochwave – 2015-04-24T14:56:01.000

In particular question #1124 – dr.blochwave – 2015-04-24T14:58:12.720

The LHS of your definition should read MyNormalDistribution[mu_, sig_] Without the patterns, MyNormalDistribution[0.5, 0.3] is undefined. Even after this correction, RandomVariate[MyNormalDistribution[0.5, 0.3]] does not return a value. RandomVariate only works with the built-in distributions. You would need to define an upvalue for MyNormalDistribution to have RandomVariate work. – Bob Hanlon – 2015-04-24T16:16:06.827

@BobHanlon, You are right about the missing underscores, thanks. This was a typo. However, RandomVariate does work with custom distributions (at least in v10). Feel free to try it yourself.... – JeffDror – 2015-04-24T16:45:01.633

@JeffDror - You are correct. I though I had tried RandomVariate with the custom distribution but in a new session it worked fine. – Bob Hanlon – 2015-04-24T19:54:49.937

1Do you really require them one at a time? Is the distribution changing between calls (only real reason I can think of that would need singlets)? Otherwise, produce them in bulk, will be orders of magnitude faster. – ciao – 2016-04-13T05:29:51.857

No answers