Julia, function to replicate "rbinom()" in R
Asked Answered
F

1

6

I have dug around and googled but not found an example. I'm sure Julia has a powerful function (in base?) to generate random binomial (bernoulli?) "successes" with a given probability. I can't find it or figure out how to do the equivalent to in Julia:

> rbinom(20,1,0.3)
 [1] 1 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 0

Thx. J

Fowle answered 27/1, 2021 at 16:10 Comment(2)
One way is just to write rand(20) .< 0.3.Wilmoth
It gets a bit more tricky for other values of the input arguments, such as if the second arg is >1Leftward
L
9

You can use Distributions and the rand function for this. Any distribution can be passed to rand. To replicate what you want:

julia> using Distributions

julia> p = Binomial(1, 0.3)   # first arg is number of trials, second is probability of success
Binomial{Float64}(n=1, p=0.3)

julia> rand(p, 20)
20-element Array{Int64,1}:
 0
 1
 1
 0
 1
 0
 0
 1
 0
 1
 1
 1
 0
 0
 1
 0
 1
 0
 0
 1
Leftward answered 27/1, 2021 at 17:30 Comment(7)
That works great thanks, but surprised that the Distributions package is required, and there are no functions in the base? Is Julia very very basic in its base configuration?Fowle
Julia is a more general purpose language than R, which is basically a statistics language, as far as I can tell. So functionality is kept in libraries to a large degree. It does not hurt performance at all. This is a deliberate design choice, many things have been moved out of Base in the last few years. I still wouldn't call it very basic, the Base library has a lot of built-in functionality, too much, for some people.Leftward
BTW, rand lives in Base, it's only the Binomial distribution itself you need to import.Leftward
Thx DNF, very enlightening. I thought adding packages would slow things down, but guess the fact that Julia is compiled means that it does not affect performance. Learning!Fowle
Adding packages can increase the compile time. But that also means you save compile time when you don't need them, which you couldn't if they were included in Base. The runtime is unaffected.Leftward
I find this approach very elegant, rather than having dozens of different names for random sampling from different distributions..Killen
Yep, for any distribution, p, you just use rand(p), pdf(p), cdf(p), quantile(p), etc, instead of needing to dig up the name of each function. It's all very composable.Leftward

© 2022 - 2024 — McMap. All rights reserved.