I would think using a list comprehension would be a easy and Julian way of
doing it:
using Distributions
p = [0.1, 0.2, 0.3, 0.4, 0.5]
[rand(Bernoulli(x)) for x in p]
On Monday, January 13, 2014 9:44:02 PM UTC+2, Sam L wrote:
>
> In my case I'm trying to generate an array of independent Bernoulli r.v.s
> where the parameter for each Bernoulli is given in an array p. In R, I'd do
> rbinom(length(p),
> 1, p). It looks like the Distributions package does not support this
> sort of thing. I have a few solutions to my particular problem shown at the
> bottom of this message but I'm curious about the best way to do this in
> general.
>
> Is this something the Distributions package will support in the future or
> is there a better/built in way to do this? It seems to me that it would
> come up frequently and having to write a function to vectorize it every
> time is a pain.
>
> Sam
>
> #Some solutions
> rbern(p) = rand(Bernoulli(p))
> @vectorize_1arg Real rbern
> type RandBernoulli <: Functor{1} end
> NumericExtensions.evaluate(::RandBernoulli, p) = rbern(p) #then map
>
> rbern2(p) = int(rand(length(p)) .< p)
>