Skip to content

Commit

Permalink
Improve numerical accuracy of confint(::KaplanMeier)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Apr 2, 2024
1 parent cb9bb8c commit f1bf90e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/kaplanmeier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ function as a vector of tuples.
function StatsAPI.confint(km::KaplanMeier; level::Real=0.05)
q = quantile(Normal(), 1 - level/2)
return map(km.survival, km.stderr) do srv, se
l = log(-log(srv))
# The direct implementation here would be
# ℓ = log(-log(srv))
# a = q * se / log(srv)
# lower = exp(-exp(ℓ - a))
# upper = exp(-exp(ℓ + a))
# However, this has some issues with numerical accuracy. An approximation to
# this quantity with improved accuracy is implemented here. The approximation
# was obtained via Herbie (https://herbie.uwplse.org/).
a = q * se / log(srv)
exp(-exp(l - a)), exp(-exp(l + a))
return (srv^exp(-a), srv^exp(a))
end
end

Expand Down
5 changes: 3 additions & 2 deletions src/nelsonaalen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ function as a vector of tuples.
"""
function StatsAPI.confint(na::NelsonAalen; level::Real=0.05)
q = quantile(Normal(), 1 - level/2)
return map(na.chaz, na.stderr) do srv, se
srv - q * se, srv + q * se
return map(na.chaz, na.stderr) do ch, se
a = q * se
return (ch - a, ch + a)
end
end

Expand Down

0 comments on commit f1bf90e

Please sign in to comment.