I am trying to replicate this table (from the MITx course in “Fundamental of statistics”):
I can get the cdf, but not the quantiles. For example, for n=7:
julia> using Distributions
julia> KSd = KSDist(7)
KSDist(n=7)
julia> cdf(KSd,0.48342)
0.9499971874088311
julia> quantile(KSd,0.95)
ERROR: MethodError: no method matching iterate(::KSDist)
Closest candidates are:
iterate(::Core.SimpleVector) at essentials.jl:604
iterate(::Core.SimpleVector, ::Any) at essentials.jl:604
iterate(::ExponentialBackOff) at error.jl:214
...
Stacktrace:
[1] copyto!(::Array{Float64,1}, ::KSDist) at ./abstractarray.jl:722
[2] _collect(::UnitRange{Int64}, ::KSDist, ::Base.HasEltype, ::Base.HasLength) at ./array.jl:550
[3] collect(::KSDist) at ./array.jl:544
[4] #quantile#50(::Bool, ::typeof(quantile), ::KSDist, ::Float64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Statistics/src/Statistics.jl:941
[5] quantile(::KSDist, ::Float64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Statistics/src/Statistics.jl:941
[6] top-level scope at none:0
Are quantiles not defined for the KSDis distribution ? Would be there any workaround ?
efectively.
julia> methodswith(KSDist)
[1] ccdf(d::KSDist, x::Float64) in Distributions at C:\Users\andre\.julia\packages\Distributions\wRw5p\src\univariate\continuous\ksdist.jl:53
[2] cdf(d::KSDist, x::Float64) in Distributions at C:\Users\andre\.julia\packages\Distributions\wRw5p\src\univariate\continuous\ksdist.jl:24
[3] maximum(d::KSDist) in Distributions at C:\Users\andre\.julia\packages\Distributions\wRw5p\src\univariates.jl:148
[4] minimum(d::KSDist) in Distributions at C:\Users\andre\.julia\packages\Distributions\wRw5p\src\univariates.jl:147
for a workaround, maybe solving for a point by newton or other method?
1 Like
for example:
using Roots
function myquantile(dist::KSDist,px::T) where T<:Real
return Roots.find_zero(x->cdf(dist,x) - px,(0.0,1.0))
end
julia> myquantile(KSd,0.95)
0.4834236023636414
Update: there exists the function Distributions.quantile_bisect
that calculates the quantile via bisection, and is integrated on Distributions.jl, so this works without additional packages:
Distributions.quantile_bisect(KSd,0.95)
0.48342360236396026
for measuring accuracy:
julia> cdf(KSd,Distributions.quantile_bisect(KSd,0.95))
0.9500000000002492
julia> cdf(KSd,myquantile(KSd,0.95))
0.95
3 Likes