Question

I am looking for the equivalent of the below Matlab function in Mathematica:

"R = poissrnd(lambda) generates random numbers from the Poisson distribution with mean parameter lambda. lambda can be a vector, a matrix, or a multidimensional array. The size of R is the size of lambda."

An example of the function output below.

b = 95.7165   95.7165   95.7165   95.7165   95.7165   98.9772   98.9772   98.9772   98.9772    0.3876

poissrnd(b)

ans =100   115     81    90   109   106   104    87   104     2

How could I do something similar in Mathematica 8 ?

Was it helpful?

Solution

The Poisson distribution is defined only for integers. So you'll need to use RandomInteger with PoissonDistribution like so:

poissrnd[lambda_]:=RandomInteger[PoissonDistribution[lambda]]

Usage:

b = {95.7165, 95.7165, 95.7165, 95.7165, 95.7165, 98.9772, 98.9772, 
  98.9772, 98.9772, 0.3876};

poissrnd /@ b

Out[1] = {104, 97, 67, 84, 96, 123, 93, 96, 100, 0}

OTHER TIPS

By reading the extensive online Mathematica documentation, especially the bit about PoissonDistribution and its plotting example, which points you towards PDF. This will allow you to calculate the distribution values.

Note that in my personal experience, for simple distributions, it's faster to just plug in the distribution's formula and use that instead of the fancy PDF approach. A Poisson distribution isn't too complicated.

Alternatively, you could use

In[2]:= lambda = {1.0, 2.05, 11.04}

Out[2]= {1., 2.05, 11.04}

In[3]:= Map[RandomVariate[PoissonDistribution[#]] &, lambda]

Out[3]= {0, 3, 11}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top