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?
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 forMyNormalDistribution
to haveRandomVariate
work. $\endgroup$