Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia, function to replicate "rbinom()" in R

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

like image 544
Jim Maas Avatar asked Jan 27 '21 16:01

Jim Maas


People also ask

How to work with the binomial distribution in R?

This tutorial explains how to work with the binomial distribution in R using the functions dbinom, pbinom, qbinom, and rbinom. The function dbinom returns the value of the probability density function (pdf) of the binomial distribution given a certain random variable x, number of trials (size) and probability of success on each trial (prob).

What is rbinom in R?

R rbinom – Simulate Binomial or Bernoulli trials This article about R’s rbinom function is part of a series about generating random numbers using R. The rbinom function can be used to simulate the outcome of Bernoulli trials. This is a fancy statistical word for flipping coins.

How do I use the dbinom R function?

First, we have to create a vector of quantiles as input for the dbinom R function: Then, we can apply the dbinom function to this vector as shown below. Note that I have specified the size to be equal to 100 (i.e. the number of trials) and the probability for each binomial draw to be equal to 0.5 (i.e. 50%).

What is the expected syntax for the rbinom function?

R’s rbinom function simulates a series of Bernoulli trials and return the results. The function takes three arguments: The expected syntax is: rbinom (# observations, # trails/observation, probability of success ) For this example, lets assume we’re in charge of quality for a factory. We make 150 widgets per day.


1 Answers

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
like image 117
DNF Avatar answered Oct 17 '22 17:10

DNF