using Distributions, StatsBase, StatsPlots, LaTeXStrings
# Distribution: package with all common distribution
# StatsBase: some common statistics operations
# StatsPlots: Plot + some special stat plots
# LaTeXStrings: Latex writing in figures
n = 1000
X = rand(Cauchy(),n)
# In Julia element wise operations like take the inverse of each element of an array is denoted with a dot .
# The purpose is to clarify what operation on what object is really done.
Y = 1 ./rand(Cauchy(),n)
histogram(X,alpha=0.5,label=L"X",xlims=(0,100),nbins=1000)
histogram!(Y,alpha=0.5,label= L"Y",xlims=(0,100),nbins=1000)
n = 10_000
d = Normal()
X = rand(d,n)
Y = -X
histogram([X,Y],alpha=0.5,label=[L"X" L"Y"])
BienayméChebychev(d,a) = var(d)/a^2
Cantelli(d,a) = var(d)/(a^2+var(d))
d = Normal(0,1)
xr = 0:0.01:5
plot(x-> 1-cdf(d,x),xr, label=L"P(X\geq a)")
plot!(x-> BienayméChebychev(d,x), xr, label="Bienaymé Chebychev")
plot!(x-> Cantelli(d,x), xr, label="Cantelli")
title!("$d")
ylims!(0,1)
xlabel!(L"a")
k = 2 # k≥1
xs = [-1.,0,1] # Values of X
ps = [1/(2*k^2),1-1/k^2, 1/(2*k^2)] # Associated probability
d = DiscreteNonParametric(xs, ps)
println(L"$\mathbb{E}(X) = $", mean(d)) # 0
println(L"\operatorname{Var}(X) = ", var(d)) # 1/k^2
xr = 0:0.01:5
plot(x-> 2(1-cdf(d,x)),xr, label=L"P(|X|\geq a)")
plot!(x-> BienayméChebychev(d,x), xr, label="Bienaymé Chebychev")
# plot!(x-> Cantelli(d,x), xr, label="Cantelli")
ylims!(0,1)
xlabel!(L"a")
v = 0.2;
xs = [1.,-v] # Values of X
ps = [v/(1+v), 1/(1+v)] # Associated probability
d = DiscreteNonParametric(xs, ps)
println(L"\mathbb{E}(X) = ", mean(d))
println(L"\operatorname{Var}(X) = ", var(d))
xr = 0:0.01:5
plot(x-> 1-cdf(d,x),xr, label=L"P(X\geq a)")
# plot!(x-> BienayméChebychev(d,x), xr, label="Bienaymé Chebychev")
plot!(x-> Cantelli(d,x), xr, label="Cantelli",c=3)
ylims!(0,1)
xlabel!("a")
f7(x,y) = 2/Ï€*exp(-x*(1+y^2))
xr = range(0, stop = 2, length = 100)
yr = range(0, stop = 3, length = 100)
contourf(xr, yr, (xr, yr) -> f7(xr,yr))
To simulate $f_{X,Y}(x,y)=(1+xy)\mathbb{1}_{(x,y)\in [-1,1]^2}/4$ one can use the conditional density $f_{X|Y}(x,y) = (1+xy)\mathbb{1}_{x\in [-1,1]}/2$ and marginal $f_{Y}(y) = \mathbb{1}_{y\in [-1,1]}/2$ (= uniform density) so that $$f_{X,Y}(x,y)=f_{X|Y}(x,y)\times f_{Y}(y)$$ In this latter form to simulate a pair $(X,Y)$ one just need to draw $Y\sim U([-1,1])$ and then draw from the conditional density $f_{X|Y}(x,y)$.
We have $$ F_{X|Y}(t) = \mathbb{P}(X<t|Y=y) = \int_{-1}^x \frac{1}{2} (1+x y) \, dx = \frac{1}{4} (1+x) ((x-1) y+2)$$
To use the Inverse Transform method we need to compute $F^{-1}_{X|Y}(U)$ which here can be found as $$F^{-1}_{X|Y}(U) = \frac{\sqrt{4 U y+y^2-2 y+1}-1}{y}$$
f(x,y) = (1+x*y)/4
g(x,y) = 1/4
xr = yr = range(-1, stop = 1, length = 100)
contour(xr, yr, (xr, yr) -> f(xr,yr), fill = true)
xr = yr = range(-1, stop = 1, length = 100)
contour(xr, yr, (xr, yr) -> g(xr,yr), fill = true)
# F^{-1}_{X|Y=y}(U)
sampler_x(u,y) = (-1 + sqrt(1 - 2*y + 4*u*y + y^2))/y
n = 100_000
x_ind = rand(Uniform(-1,1),n)
y_ind = rand(Uniform(-1,1),n)
y = rand(Uniform(-1,1),n)
x = sampler_x.(rand(n),y)
h = fit(Histogram, (x,y), nbins=10)
contour(midpoints(h.edges[1]),midpoints(h.edges[2]),h.weights,xlabel=L"x", ylabel=L"y", fill = true)
# , title = L"f(x,y)=(1+xy)\mathbb{1}_{(x,y)\in [-1,1]^2}/4"
p1 = marginalhist(x,y,xlabel=L"x", ylabel=L"y",bins=20)
#, title = L"f(x,y)=\mathbb{1}_{(x,y)\in [-1,1]^2}/4"
p2 = marginalhist(x_ind,y_ind,xlabel=L"x", ylabel=L"y",bins=100)
plot(p1, p2, size=(1000,500))