# Compute integral of a beta distribution

Hi everyone,

I used the `Distributions` package in order to generate a beta distribution, which works quite nicely:

``````using Distributions
d = Beta(10,15)
``````

However - is it possible to integrate over an interval of the distribution? I’m looking for a Julia equivalent to R’s `integrate` function which allows something like:

``````integrate(function(p) dbeta(p, 10, 15), 0, 0.5)
``````

Any hints on where to look are appreciated!

Regards,

Thomas

I don’t know any R so I’m not sure what algorithm the `integrate` function uses, but have you tried QuadGK.jl?

``````using QuadGK
``````

gives

``````julia> integral, err = quadgk(x->pdf(d,x),0,0.5)
(0.8462718725204469, 2.8282575934013288e-9)
``````

Here, `x->pdf(d,x)` is an anonymous function that represents the probability density function (pdf in Distributions.jl).

This is what I get in Mathematica:

3 Likes

Thanks crstnbr,

that works like a charm!

Regards,

Thomas

1 Like

This seems like a job for the `cdf` function:

``````julia> p = Beta(10, 15)
Beta{Float64}(α=10.0, β=15.0)

julia> cdf(p, 0.5) - cdf(p, 0)
0.8462718725204467
``````
6 Likes

It’s way faster too:

``````julia> using BenchmarkTools

julia> @btime integral, err = quadgk(x->pdf(\$p,x),0,0.5);
8.000 μs (34 allocations: 912 bytes)

julia> @btime cdf(\$p, 0.5) - cdf(\$p, 0);
321.267 ns (0 allocations: 0 bytes)
``````
1 Like

Thanks, DNF,

the difference in speed in this case will definitely something I’ll have to consider.

Regards,

Thomas

To clarify, it’s not just faster, it should also be more accurate and more ‘correct’.

The cumulative distribution function is defined to be the integral of the probability density function. The `QuadGK` code is using numerical quadrature to approximate the CDF. It’s of course better to directly use the known analytical expression for the CDF, which is what the `cdf` function does.

As a bonus, you don’t need to import an extra package.

BTW, I’m not familiar with R, but I’m pretty sure this is how you should do it in R too.

7 Likes

Indeed, in R you would use `pbeta(0.5, 10, 15)`, not a numerical integration.

3 Likes