# How do you guys would plot and histogram this Distribution F code?

**URL:** <https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675>\
**Category:** Statistics\
**Created:** [September 12, 2019, 2:44am UTC](https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675 "2019-09-12T02:44:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Halor](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Halor](https://discourse.julialang.org/u/Halor)\
**Post date:** [September 12, 2019, 2:44am UTC](https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675/1 "2019-09-12T02:44:43Z")

</div>

Hi.  
I am a new user in programming and in statistics so I want to learn all the things that I can. I try to code a PDF of F Distribution which formula have taken from Wikipedia ([Distribución F - Wikipedia, la enciclopedia libre](https://es.wikipedia.org/wiki/Distribuci%C3%B3n_F)). The code is the next one:

d1=10.0  
d2=2.0  
x=1  
blocks=10  
#función beta  
β=factorial(Int(d1/2)-1)_factorial(Int(d2/2)-1)/factorial(Int(d1/2))+(Int(d1/2)-1)  
println(β)  
num=(d1_x)^d1\*(d2^d2)  
den=(d1_x+d2)^d1+d2  
pdf=sqrt(num/den)/β_x #PDF distribucion F

I do not know if I am right or even if my code is right, so feel free to give any advice since I am learning by my way and I want to improve my programming skills.

Also, my objective is to make an histogram and plot this. I know that there is Distributions Package in Julia, but I wanted to practice by using the formula given by wikipedia. How do you would histogram and plot this program?

Any answer would be appreciated.

---

<div class="post-metadata">

**Author:** ![brwst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brwst/32/6645_2.png) [@brwst](https://discourse.julialang.org/u/brwst)\
**Post date:** [September 12, 2019, 5:44am UTC](https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675/2 "2019-09-12T05:44:12Z")

</div>

If you’d like to build your own F distribution pdf, it can be helpful to use the [Distributions.jl F distribution](https://juliastats.github.io/Distributions.jl/stable/univariate/#Distributions.FDist) as a guide.

```julia
using Distributions
using StatsPlots

```

To construct an F distribution pdf, you’ll first have to define a beta function.

```julia
# Only for integers x > 0, y > 0.
β(x::Real, y::Real) = factorial(x - 1) * factorial(y - 1) / factorial(x + y - 1)

Fpdf(d1::Real, d2::Real, x::Number) = sqrt((d1 * x)^d1 * d2^d2 / (d1 * x + d2)^(d1 + d2)) / (x * β(d1 / 2, d2 / 2))

```

Specify your two parameters, create the Distributions.jl F distribution and plot both pdfs together to see if they line up.

```julia
d1 = 10.0
d2 = 2.0
d = FDist(d1, d2) # from Distributions.jl

x = collect(0:0.1:50)
plot(x, pdf.(d, x), label="FDist")
plot!(x, Fpdf.(d1, d2, x), label="Fpdf")

```

![fdist1](https://global.discourse-cdn.com/julialang/original/3X/a/8/a8e5ce4a8d76596d6f07c1ad74c319399595c4aa.png)

You can also create a histogram using StatsPlots, but you need data to bin. As an example, draw random numbers from the distribution `d` and plot the histogram alongside the pdfs.

```julia
y = rand(d, 100)
histogram!(y, normalize=:pdf, label="rand")

```

![fdist2](https://global.discourse-cdn.com/julialang/original/3X/3/6/36ba34dabf545e2a15f963288e06e274db1ce0ff.png)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [September 12, 2019, 6:34am UTC](https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675/3 "2019-09-12T06:34:49Z")

</div>

Gadfly examples [here](https://github.com/GiovineItalia/Gadfly.jl/pull/1249#issue-250393478).

---

<div class="post-metadata">

**Author:** ![Halor](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Halor](https://discourse.julialang.org/u/Halor)\
**Post date:** [September 12, 2019, 7:29pm UTC](https://discourse.julialang.org/t/how-do-you-guys-would-plot-and-histogram-this-distribution-f-code/28675/4 "2019-09-12T19:29:28Z")

</div>

Thank you very much! It is exactly what I was looking for
