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

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). 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=(d1x+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.

If you’d like to build your own F distribution pdf, it can be helpful to use the Distributions.jl F distribution as a guide.

using Distributions
using StatsPlots

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

# 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.

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

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.

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

fdist2

2 Likes

Gadfly examples here.

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