5

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

Jim Maas
  • 1,069
  • 9
  • 22

1 Answers1

7

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
DNF
  • 6,601
  • 1
  • 17
  • 28
  • 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? – Jim Maas Jan 27 '21 at 18:00
  • 2
    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. – DNF Jan 27 '21 at 18:16
  • 1
    BTW, `rand` lives in Base, it's only the Binomial distribution itself you need to import. – DNF Jan 27 '21 at 18:23
  • 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! – Jim Maas Jan 27 '21 at 18:35
  • 1
    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. – DNF Jan 27 '21 at 18:40
  • 2
    I find this approach very elegant, rather than having dozens of different names for random sampling from different distributions.. – Antonello Jan 27 '21 at 21:17
  • 1
    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. – DNF Jan 27 '21 at 21:37