Newer
Older
# Komolgorov - Smirnov
# ( adapted from HypothesisTests.jl )
function distance_ks(x::Vector{<:Real}, d::UnivariateDistribution)
n = length(x)
cdfs = cdf.(d, sort(x))
δp = maximum((1:n) / n - cdfs)
δn = -minimum((0:n-1) / n - cdfs)
δ = max(δn, δp)
return δ
end
function exact_distance_ks(f::Function, d::Distribution, h = 1e-4)
x = [-1:h:1 ... ]
f = f.(x); f = exp.(f); f = f / (2 * mean(f))
F = cumsum(f .* h)
P = cdf.(d, x)
δ = maximum(abs.(F .- P))
return δ
end
function exact_distance_ks(f::Distribution, d::Distribution, h = 1e-4)
x = [-1:h:1 ... ]
F = cdf.(f, x)
P = cdf.(d, x)
return maximum(abs.(F .- P))
end
# Cramér - von Mises
function distance_cm(x::Vector{<:Real}, d::UnivariateDistribution)
n = length(x)
x = sort(x)
cdfs = cdf.(d, x)
ω² = mean( ((1:n) / n .+ .5 - cdfs ).^2 ) + 1/(12*n^2)
end
function exact_distance_cm(f::Function, d::Distribution, h = 1e-4)
x = [-1:h:1 ... ]
f = f.(x); f = exp.(f); f = f / (2 * mean(f))
F = cumsum(f .* h)
P = cdf.(d, x)
p = pdf.(d, x)
ω² = 2 * mean( (F .- P).^2 .* p )
return sqrt(ω²)
function exact_distance_cm(f::Distribution, d::Distribution, h = 1e-4)
x = [-1:h:1 ... ]
F = cdf.(f, x)
P = cdf.(d, x)
p = pdf.(d, x)
ω² = 2 * mean( (F .- P).^2 .* p )
return sqrt(ω²)
end
# Quantify convergence rate y ≈ C⋅x^α
function asymptotic_rate(x::Vector, y::Vector)
log_x = log.(x)
log_y = log.(y)
A = hcat( ones( length(x)), log_x )
(a,b) = (A'A) \ (A' * log_y)