hard worker
hard worker

Reputation: 191

random generator question,how the rpois(a,b) performed when b is a vector?

I just wondering how the rpois(a,b) performed when b is a vector?

for example

m <- rgamma(1000, 10, 1)
j <- rpois(1000, m)

Can anyone tell me how the J performs? Is it run a loop on m and generate 1 random variable every time?

Thank you

Upvotes: 1

Views: 138

Answers (1)

akrun
akrun

Reputation: 887531

The function is vectorized for the lamdba as according to ?rpois

lamdba - vector of (non-negative) means.

It can be tested with the same seed

set.seed(24)
j <- rpois(1000, m)

Now, we do this in a loop

j1 <- integer(length(m))
set.seed(24)
for(i in seq_along(m)) j1[i] <- rpois(1, m[i])

check for equality

identical(j, j1)
#[1] TRUE

For the second case of a different length for lambda, the 'm' values are recycled once it reaches every 100 values i.e. the 'm' is repeated for each block of 100 or repeated 10 times

m <- rgamma(100,10,1)
set.seed(24)
j <- rpois(1000, m)

To test this, we replicate the 'm' values to make the length same

m1 <- rep(m, length.out = 1000)
j1 <- integer(1000)
set.seed(24)
for(i in seq_along(m1)) j1[i] <- rpois(1, m1[i])

check for equality

identical(j, j1)
#[1] TRUE

Upvotes: 1

Related Questions