# I was greatly inspired by StatsWithJuliaBook
# https://statisticswithjulia.org/index.html
using StatsPlots, LaTeXStrings, Measures, LinearAlgebra, Distributions, Random, StatsBase
The vertical lines are the means
lambda, N = 1/3, 10^5
bulbs = [0.4,1,2]
xGrid = 0:0.1:10
C = [:blue :red :green]
dists = [Gamma(n,2) for n in bulbs]
L = [ "α = "*string.(shape.(i))*", β = "*
string.(round.(scale.(i),digits=2)) for i in dists ]
p=plot(xGrid, [pdf.(i,xGrid) for i in dists], c=C, label=reshape(L, 1,:))
[vline!(p,[mean(dists[i])], c=C[i], label=:none, ls=:dash) for i in 1:3]
p
N = 10000
support = -4:0.1:4
a = 1.5
X = rand(Normal(0,1), N)
f(x,a) = abs(x)>a ? x : -x
Y = f.(X,a)
plot(x->pdf(Normal(0,1),x),support, label="pdf Normal(0,1)")
histogram!(X, label = "X", alpha = 0.5, norm = :pdf)
histogram!(Y, label = "Y", alpha = 0.5, norm = :pdf)
# support = -4:0.1:4
p1 = scatter(X,Y,xlabel = "X", ylabel= "Y", label = :none)
p2 = histogram2d(X,Y,xlabel = "X", ylabel= "Y")
plot(p1, p2, size = (1000,600))
# support = -4:0.1:4
histogram(X+Y,label = "X+Y", norm = :pdf)
gcdf = ecdf(X+Y)
plot(x -> gcdf(x), -6, 6, label = L"ECDF_{X+Y}(t)", xlabel = "t", legend = :topleft)
cont = MixtureModel(Normal,[(0,1),(0,10)],[0.9,0.1])
plot(x-> pdf(cont,x),-10,10, label = "Contaminated Normal",yscale=:log10)
plot!(x-> pdf(Normal(),x),-10,10, label = "Standard Normal")
plot!(x-> pdf(Normal(0,10),x),-10,10, label = "Contaminated part",ylims=(1e-4,1))
meanVect = [27., 26.]
covMat = [16. 0.;
0. 12.]
biNorm = MvNormal(meanVect,covMat)
N = 10^3
points = rand(MvNormal(meanVect,covMat),N)
support = 15:0.5:40
z = [ pdf(biNorm,[x,y]) for y in support, x in support ];
p1 = scatter(points[1,:], points[2,:], ms=2, c=:black, legend=:none)
p1 = contour!(support, support, z,
levels=[0.001, 0.005, 0.02], c=[:blue, :red, :green],
xlims=(15,40), ylims=(15,40), ratio=:equal, legend=:none,
xlabel="x", ylabel="y")
p2 = contour(support, support, z,
legend=:none, xlabel="x", ylabel="y")
plot(p1, p2, size=(1000, 500))
plot(Chi(2), label="χ^2")
plot([Normal(), TDist(2), TDist(10)],-5,5, ls = [:dash :solid :dot], label=["Normal" "t-distribution DOF = 2" "t-distribution DOF = 10"])
N = 10000
λ = 1
invF(u) = -log(1-u)/λ
U = rand(N) # Uniform sample
X = invF.(U)
histogram(X, alpha = 0.5, label = "X", norm = :pdf)
plot!(x-> pdf(Exponential(λ), x), 0:0.1:10, label = "pdf λ exp(λx)")