Distributions.jl - pdf() for Continuous Uniform Distribution


I’m taking a free online course in Bayesian Statistics (I have no background in this :stuck_out_tongue_winking_eye:) and I’m trying to work through the course with Distributions.jl (and eventually Turing.jl) so that I can learn all of these things together. I’ve run into an issue though in computing probabilities for a continuous uniform distribution. Given a continuous uniform distribution Uniform(0, 1), how would I compute P(0.2 ≤ X ≤ 0.8) in Julia? I realize that this is a trivial question (according to the course the answer is just 0.8 - 0.2 = 0.6) but I’d like to know how to compute it with the Distributions package.

For a binomial distribution, I can just use the pdf() function like this:

using Distributions

b = Binomial(3, 0.2)

# To compute P(0 ≤ X ≤ 2) I can just do this: 

sum(pdf.(b, collect(0:2)))

But for the uniform distribution, I get 7.0 (instead of 0.6) when I do this:

u = Uniform(0, 1)

sum(pdf.(u, collect(0.2:0.1:0.8)))

And I get 1.0 no matter what number I plug into pdf(u, x). For example:

julia> pdf(u, 0.5)
julia> pdf(u, 0.8)

When you want to calculate the probability of some event (generally, some subset of the possible outcomes), you want to use the cumulative distribution function (CDF, cdf in Distributions.jl). For a probability distribution function f, the CDF is

F(b) = \int_0^b f(x) dx

This is \Pr(X < b), the probability that the outcome is less than x. So to get \Pr(a < x < b), you want

\int_a^b f(x) dx = \int_0^b f(x) dx - \int_0^a f(x) dx = F(b) - F(a).

It just so happens that summation works for discrete distributions, but the CDF is also defined for them. So, short story, use something like

cdf(Uniform(0, 1), 0.6) - cdf(Uniform(0, 1), 0.2)

Thanks so much!

I’m stuck on another one :confounded: Any idea how to compute the following with Distributions.jl?

Let Y ∼ Gamma(2, 1/3). Find P(0.5 < Y < 1.5)
# According to the course material, the answer is 0.078

gamma = Gamma(2, 1/3)
cdf(gamma, 1.5) - cdf(gamma, 0.5)
# yields

Many distribution families have alternative parametrizations, you should read ?Gamma to find out which one is being used. You are probably looking for \theta = 1/\beta (“scale” vs “rate”), ie

julia> g = Gamma(2, 3)
Gamma{Float64}(α=2.0, θ=3.0)

julia> cdf(g, 1.5) - cdf(g, 0.5)

Nice, thank you. I’ll have to research this more to understand it, but thanks for pointing me in the right direction!

It’s just a convention, see https://en.wikipedia.org/wiki/Gamma_distribution .

If I had a cent for every time I was burned by some version of this with various distributions (gamma, Poisson), I would have…, well, probably less than €0.50.

1 Like

Let me please recommend that you spend a bit of time on Chapter 3 of “Statistics with Julia” (draft): https://people.smp.uq.edu.au/YoniNazarathy/julia-stats/StatisticsWithJulia.pdf .

You don’t need chapters 1 and 2 to understand chapter 3. It develops the basics of distributions pretty much from scratch. Just follow each of the code examples and see that you understand how and why they work.

Happy for feedback,


@yoninazarathy This looks fantastic! I really appreciate the info. I’ve actually been looking for a new stats/data science for Julia kind of book but haven’t found anything yet that I thought looked particularly promising. I’ve had the McNicholas/Tait book Data Science with Julia for about 6 months but I’ve been a bit disappointed with it, to be honest. I’m currently taking an online course on Bayesian stats but all the examples are in R and it’s been a bit difficult to figure out how to solve some of the problems with Julia.

I’m a professional data analyst and I’ve been trying to push the boundaries in my organization by acquiring new skills and really taking our (fledgling) data analytics work to the next level, but as I do that I’m obviously finding the need to either go back and re-learn things that I saw at university a long time ago or that I may have never seen. Your book looks like it will be a good resource for me and I’ll be happy to provide feedback as I work through it.

Thanks again!!! :smiley: