In [23]:
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

Ex 3

In [8]:
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)
Out[8]:

Ex 4

In [6]:
n = 10_000
d = Normal()
X = rand(d,n)
Y = -X
histogram([X,Y],alpha=0.5,label=[L"X" L"Y"])
Out[6]:

Ex 6

In [9]:
BienayméChebychev(d,a) = var(d)/a^2
Cantelli(d,a) = var(d)/(a^2+var(d))
Out[9]:
Cantelli (generic function with 1 method)
In [10]:
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")
Out[10]:

Are theses bounds sharp? i.e. is the bound reached by some distributions?

Exemple: $\mathbb{P}(X=-1) = \mathbb{P}(X=-1) = 1/(2k^2)$ and $ \mathbb{P}(X=0) = 1-1/k^2$ for $k\geq 1$

In [49]:
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")
$\mathbb{E}(X) = $0.0
$\operatorname{Var}(X) = $0.25
Out[49]:

Exemple: $\mathbb{P}(X=1) = \dfrac{v}{1+v}$ and $\mathbb{P}(X=-v) = \dfrac{1}{1+v}$ for $v\geq 0$

In [13]:
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")
$\mathbb{E}(X) = $0.0
$\operatorname{Var}(X) = $0.2
Out[13]: