-
Notifications
You must be signed in to change notification settings - Fork 12
/
smooth_quantile.jl
32 lines (28 loc) · 1.3 KB
/
smooth_quantile.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using InferOpt: soft_sort_kl
using StatsBase
@doc raw"""
qplus_smooth(v::AbstractArray, coverage::AbstractFloat=0.9)
Implements the ``\hat{q}_{n,\alpha}^{+}`` finite-sample corrected quantile function as defined in Barber et al. (2020): https://arxiv.org/pdf/1905.02928.pdf. To allow for differentiability, we use the soft sort function from InferOpt.jl.
"""
function qplus_smooth(v::AbstractArray, coverage::AbstractFloat=0.9; ε::Real=1e-6, kwrgs...)
n = length(v)
p̂ = ceil(((n + 1) * coverage)) / n
p̂ = clamp(p̂, 0.0, 1.0)
v = soft_sort_kl(v; ε=ε) # soft sort (differentiable)
q̂ = quantile(v, p̂; sorted=true, kwrgs...)
return q̂
end
@doc raw"""
qminus_smooth(v::AbstractArray, coverage::AbstractFloat=0.9)
Implements the ``\hat{q}_{n,\alpha}^{-}`` finite-sample corrected quantile function as defined in Barber et al. (2020): https://arxiv.org/pdf/1905.02928.pdf. To allow for differentiability, we use the soft sort function from InferOpt.jl.
"""
function qminus_smooth(
v::AbstractArray, coverage::AbstractFloat=0.9; ε::Real=1e-6, kwrgs...
)
n = length(v)
p̂ = floor(((n + 1) * coverage)) / n
p̂ = clamp(p̂, 0.0, 1.0)
v = soft_sort_kl(v; ε=ε) # soft sort (differentiable)
q̂ = quantile(v, p̂; sorted=true, kwrgs...)
return q̂
end