-
Notifications
You must be signed in to change notification settings - Fork 12
/
measures.jl
51 lines (43 loc) · 1.23 KB
/
measures.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
emp_coverage(ŷ, y)
Computes the empirical coverage for conformal predictions `ŷ`.
"""
function emp_coverage(ŷ, y)
R = length(ŷ)
C̄ = 1 / R * sum(is_covered(ŷ, y))
return C̄
end
"""
size_stratified_coverage(ŷ, y)
Computes the size-stratified coverage for conformal predictions `ŷ`.
"""
function size_stratified_coverage(ŷ, y)
# Setup:
stratum_indicator = size_indicator(ŷ) |> x -> x.refs
unique_stratums = sort(unique(stratum_indicator))
unique_stratums = unique_stratums[unique_stratums .!= 0]
_covs = []
if length(unique_stratums) == 1 && is_regression(ŷ)
C̄ = -Inf
else
# Compute empirical coverage for each stratum:
for stratum in unique_stratums
_in_this_stratum = stratum_indicator .== stratum
_mask = findall(_in_this_stratum)
ŷ_g, y_g = (ŷ[_mask], y[_mask])
push!(_covs, emp_coverage(ŷ_g, y_g))
end
# Find minimum:
C̄ = minimum(_covs)
end
return C̄
end
"""
ineff(ŷ)
Computes the inefficiency (average set size) for conformal predictions `ŷ`.
"""
function ineff(ŷ, y=missing)
R = length(ŷ)
ineff = sum(set_size.(ŷ)) / R
return ineff
end