34

10

Recently, Oleksandr kindly showed a list of Mathematica commands that can be compiled. RandomVariate was part of that list. However, whether this can be compiled depends upon the distribution that is being sampled.

```
Needs["CompiledFunctionTools`"]
cf1 = Compile[{{m, _Real}, {s, _Real}},
Module[{v1, v2, v3, v4, v5, v6},
v1 = RandomVariate[NormalDistribution[m, s]];
v2 = RandomVariate[UniformDistribution[{m, s}]];
v3 = RandomVariate[GammaDistribution[m, s]];
v4 = RandomVariate[PoissonDistribution[m]];
v5 = RandomVariate[ChiSquareDistribution[m]];
v6 = RandomVariate[ExponentialDistribution[m]];
{v1, v2, v3, v4, v5, v6}
]
]
```

Using CompilePrint shows that RandomVariate can be compiled for the Normal Distribution or the Uniform Distribution and not with some others.

```
CompilePrint[cf1]
2 arguments
4 Integer registers
8 Real registers
1 Tensor register
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
R0 = A1
R1 = A2
Result = T(R1)0
1 R2 = RandomNormal[ R0, R1]]
2 R3 = RandomReal[ R0, R1]]
3 I0 = MainEvaluate[ Function[{m, s},
RandomVariate[GammaDistribution[m, s]]][ R0, R1]]
4 I1 = MainEvaluate[ Function[{m, s},
RandomVariate[PoissonDistribution[m]]][ R0, R1]]
5 I2 = MainEvaluate[ Function[{m, s},
RandomVariate[ChiSquareDistribution[m]]][ R0, R1]]
6 I3 = MainEvaluate[ Function[{m, s},
RandomVariate[ExponentialDistribution[m]]][ R0, R1]]
7 R4 = I0
8 R5 = I1
9 R6 = I2
10 R7 = I3
11 T(R1)0 ={ R2, R3, R4, R5, R6, R7 }
12 Return
```

Does anyone have a list of all the distributions that can be compiled (including, PDF, CDF and RandomVariate functionality)?

1Since the results of

`RandomVariate`

are ALWAYS going to be Reals (for a continuous distribution), and Integers (for most inbuilt discrete distributions), I can't see why there would be any advantage to compiling them, that could not have automatically been built into the function. Does anyone have some timing tests where manual compilation yields substantive benefits to something like:`RandomVariate[ dist, {10^6}]`

? – wolfies – 2014-08-05T15:37:22.813Well, you can check each of them, or semi-automate checking. When one is not compiled, and you try to use it, you'll easily find out anyway: see my answer to that post asking about compilable functions. – Szabolcs – 2012-02-01T19:33:31.570

@szabolcs There are too many combinations to try out, unfortunately. – asim – 2012-02-02T01:43:00.713

3Beta distribution can be compiled too.. – asim – 2012-02-10T14:09:25.800