# How get the values of pdf function Beta

**URL:** <https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962>\
**Category:** Statistics\
**Tags:** distributions\
**Created:** [February 21, 2023, 12:34pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962 "2023-02-21T12:34:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 12:34pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/1 "2023-02-21T12:34:14Z")

</div>

```julia
using Distributions
    using StatsPlots
    plot(Beta(1, 1), ylim=(0, 5), size=(400, 300), label=false, xlabel="x", ylabel="P(x)", title="Beta distribution shapes")

```

How does plot get the values of the Beta pdfunction to plot the graph.  
In other words, if I wanted to calculate the value of f(x; a,b) for x=0.73 how could I do it, apart from going to the definition I find on Wikipedia.

And for the calculation of Beta(a,b), how do you do it?

Perhaps in this way ![{isplaystyle athrm {B} (lpha ,eta )={rac {amma (lpha )amma (eta )}{amma (lpha +eta )}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b32815268d9d70b0b9fbb8fd5a25be7bc640aa50) ?

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [February 21, 2023, 1:09pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/2 "2023-02-21T13:09:10Z")

</div>

If you want the PDF, just ask for it : `pdf(Beta(2,3),0.73)` should do the trick.

More generally, refer to the documentation of Distributions.jl : type `?pdf` in your repl

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 21, 2023, 1:31pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/3 "2023-02-21T13:31:44Z")

</div>

`StatsPlots` is basically doing something like

```julia
julia> using Plots, Distributions

julia> plot(x -> pdf(Beta(1, 1), x), minimum(support(Beta(1,1))):0.01:maximum(support(Beta(1,1))))

```

when you plot a distribution (while probably being a bit smarter than this about automatic x and y lims looking at the plot that the above produces…)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 1:39pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/4 "2023-02-21T13:39:03Z")

</div>

Tanks!  
And for the evaluation of Beta(5,3)?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 1:46pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/5 "2023-02-21T13:46:03Z")

</div>

Tanks!!

I tried like this and the graph is identical

```julia
plot(x -> pdf(Beta(10, 3), x), minimum(support(Beta(1,1))):0.01:maximum(support(Beta(1,1))), ylim=(0, 5), size=(400, 300), label=false, xlabel="x", ylabel="P(x)", title="Beta distribution shapes")

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 21, 2023, 1:47pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/6 "2023-02-21T13:47:04Z")

</div>

Actually `support` doesn’t even need the specific beta distribution:

```julia
julia> support(Beta)
RealInterval{Float64}(0.0, 1.0)

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [February 21, 2023, 1:56pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/7 "2023-02-21T13:56:17Z")

</div>

What do you mean by the evaluation of `Beta(5,3)` ? Do you want the beta function ? in which case,

```julia
using SpecialFunctions
?beta

```

should guide you.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 2:03pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/8 "2023-02-21T14:03:06Z")

</div>

I’m sorry I wrote wrong. B(a,b) not Beta(a,b)  
That’s what I meant …

```julia
a,b= 5, 3
B(a,b)=integral[0,1] u^(a-1)*(1-u)^(b-1) du

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 21, 2023, 2:23pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/9 "2023-02-21T14:23:39Z")

</div>

Various beta functions are available in `SpecialFunctions`

[https://specialfunctions.juliamath.org/stable/functions\_list/#SpecialFunctions.beta](https://specialfunctions.juliamath.org/stable/functions_list/#SpecialFunctions.beta)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 2:46pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/10 "2023-02-21T14:46:53Z")

</div>

Ok.  
looks like what I assume in the previous message.  
But it doesn’t look as good as in the preview… I don’t know why

 ![Immagine 2023-02-21 154724](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0a5774c9bd67eceac82fae36800020eae22d34a.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 21, 2023, 2:53pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/11 "2023-02-21T14:53:00Z")

</div>

I’m not sure I’m following - what is your issue? The title suggests you are interested in a pdf which suggests you are talking about a distribution, hence the initial answers you got relating to the Beta probability distribution. The beta function shares a name with and is used in the pdf of the Beta distribution but is not the same thing.

```julia
julia> using Distributions, SpecialFunctions

julia> pdf_myBeta(α, β, x) = 1/beta(α, β) * x^(α-1) * (1-x)^(β-1)
pdf_myBeta (generic function with 1 method)

julia> pdf_myBeta(5, 3, 0.5)
1.640624999999998

julia> pdf(Beta(5, 3), 0.5)
1.6406249999999987

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 21, 2023, 3:05pm UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/12 "2023-02-21T15:05:27Z")

</div>

Nothing more than that.  
I wanted to understand how plot could get the values to plot a particular beta pdf.  
And since the constant B(a,b) is used in the definition of pdfbeta, I wanted to understand where and how it is calculated.  
Doing @edit Beta(5,3) takes me inside the beta.jl module where I couldn’t find how the calculations of the pdf and the constant B(a,b) are done.  
Now following your(s) instructions I found that it is calculated (inside SpecialFunctions.jl module, using C libraries

```julia
beta(x, y)
"""
Euler integral of the first kind ``\\operatorname{B}(x,y) = \\Gamma(x)\\Gamma(y)/\\Gamma(x+y)``.
"""
function beta(a::Number, b::Number)
    lab, sign = logabsbeta(a, b)
    return sign*exp(lab)
end

if Base.MPFR.version() >= v"4.0.0"
    function beta(y::BigFloat, x::BigFloat)
        z = BigFloat()
        ccall((:mpfr_beta, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Ref{BigFloat}, Int32), z, y, x, ROUNDING_MODE[])
        return z
    end
end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 22, 2023, 7:53am UTC](https://discourse.julialang.org/t/how-get-the-values-of-pdf-function-beta/94962/13 "2023-02-22T07:53:51Z")

</div>

a bit for fun I tried to get the results also in the following ways

```julia

using QuadGK
a,b=5,3
integral, err = quadgk(t -> t^(a-1)*(1-t)^(b-1), 0, 1, rtol=1e-20)
# (0.009523809523809523, 0.0)
using SpecialFunctions
beta(5,3) # 0.009523809523809535

using Integrals
f(t,p) = p*t^4*(1-t)^2
p=1
prob = IntegralProblem(f, 0, 1, p)
sol = solve(prob) # u: 0.009523809523809523

setprecision(60, base=10) # use 60-digit arithmetic

integral, err = quadgk(t -> t^(a-1)*(1-t)^(b-1), big"0.0", big"1.0", rtol=1e-50)
# (0.0095238095238095238095238095238095238095238095238095238095238286, 9.7234613716580339174126000840314441259222690136268236454704947e-63)

beta(5,3) ≈ integral #true
beta(5,3) ≈ factorial(4)*factorial(2)/factorial(7) #true
beta(5,3) ≈ gamma(5)*gamma(3)/gamma(8) #true

# "≈" can be typed by \approx<tab>

```
