# Distributions.jl - pdf() for Continuous Uniform Distribution

**URL:** <https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686>\
**Category:** New to Julia\
**Tags:** statistics\
**Created:** [August 18, 2019, 6:45pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686 "2019-08-18T18:45:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 18, 2019, 6:45pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/1 "2019-08-18T18:45:56Z")

</div>

Hello,

I’m taking a free online course in Bayesian Statistics (I have no background in this 😜) 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:

```julia
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:

```julia
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
julia> pdf(u, 0.5)
1.0
julia> pdf(u, 0.8)
1.0

```

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [August 18, 2019, 6:54pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/2 "2019-08-18T18:54:39Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 18, 2019, 7:08pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/3 "2019-08-18T19:08:12Z")

</div>

Thanks so much!

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 20, 2019, 2:11pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/4 "2019-08-20T14:11:01Z")

</div>

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

```julia
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
0.49672591941074196

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 20, 2019, 3:02pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/5 "2019-08-20T15:02:59Z")

</div>

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
julia> g = Gamma(2, 3)
Gamma{Float64}(α=2.0, θ=3.0)

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

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 20, 2019, 3:07pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/6 "2019-08-20T15:07:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 20, 2019, 3:36pm UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/7 "2019-08-20T15:36:26Z")

</div>

It’s just a convention, see [Gamma distribution - Wikipedia](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.

---

<div class="post-metadata">

**Author:** ![yoninazarathy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoninazarathy/32/10086_2.png) [@yoninazarathy](https://discourse.julialang.org/u/yoninazarathy)\
**Post date:** [August 25, 2019, 12:26am UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/8 "2019-08-25T00:26:51Z")

</div>

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](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,  
Yoni.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 25, 2019, 12:59am UTC](https://discourse.julialang.org/t/distributions-jl-pdf-for-continuous-uniform-distribution/27686/9 "2019-08-25T00:59:20Z")

</div>

@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!!! 😃
