Computing the likelihood of data for Binomial Distribution

Viewed 172

I am following the book (Statistical Rethinking) which has code in R and want to reproduce the same in code in Julia. In the book, they compute the likelihood of six successes out of 9 trials where a success, has a probability of 0.5. They achieve this using the following R code.

 #R Code
 dbinom(6, size = 9, prob=0.5)
 #Out > 0.1640625

I am wondering how to do the same in Julia,

 #Julia
 using Distributions
 
 b = Binomial(9,0.5)
 # Its possible to look at random value,
 rand(b)
 #Out > 5

But how do I look at a specific value such as six successes?

1 Answers

I'm sure you know this but just to be sure the r dbinom function is the probability density (mass) function for the Binomial distribution.

Julia's Distributions package makes use of multiple dispatch to just have one generic pdf function that can be called with any type of Distribution as the first argument, rather than defining a bunch of methods like dbinom, dnorm (for the Normal distribution). So you can do:

julia> using Distributions

julia> b = Binomial(9, 0.5)
Binomial{Float64}(n=9, p=0.5)

julia> pdf(b, 6)
0.1640625000000001

There is also cdf which works in the same way to calculate (maybe unsurprisingly) for the cumulative density function.

Related