bootstrapCI
Computes bootstrap confidence intervals for a statistic using three methods: percentile, basic (pivotal), and BCa (bias-corrected and accelerated).
The bootstrap works by repeatedly resampling the data with replacement, computing the statistic on each resample, and using the distribution of those values to estimate the uncertainty of the statistic. The BCa method additionally uses a jackknife (leave-one-out) procedure to correct for bias and skewness.
Example:
val data = doubleArrayOf(23.0, 25.0, 27.0, 30.0, 32.0, 28.0)
val ci = bootstrapCI(data, nResamples = 10_000, random = Random(42)) { it.average() }
ci.percentile // percentile CI
ci.bca // BCa CI (most accurate for skewed data)
ci.basic // basic (pivotal) CIReturn
a BootstrapCIResult containing all three confidence intervals, the observed statistic, and metadata.
Parameters
the observed data to bootstrap from.
the number of bootstrap resamples to generate. Defaults to 10_000. More resamples produce more stable intervals at the cost of computation time.
the confidence level for the intervals, in (0, 1) exclusive. Defaults to 0.95 (95%).
the random number generator used for resampling. Defaults to Random. Pass a seeded instance (e.g. Random(42)) for reproducible results.
a function that computes the statistic of interest from a DoubleArray.
Computes bootstrap confidence intervals for a statistic on a list of arbitrary elements, using three methods: percentile, basic (pivotal), and BCa (bias-corrected and accelerated).
This overload accepts any List<T>, making it suitable for bootstrapping statistics on structured data (e.g. weighted measurements, records). The statistic function receives a List<T> resample and must return a scalar Double.
Example:
data class Obs(val value: Double, val weight: Double)
val data = listOf(Obs(1.0, 0.5), Obs(2.0, 1.0), Obs(3.0, 1.5))
val ci = bootstrapCI(data, nResamples = 10_000, random = Random(42)) { sample ->
sample.sumOf { it.value * it.weight } / sample.sumOf { it.weight }
}
ci.bca // BCa CI for the weighted meanReturn
a BootstrapCIResult containing all three confidence intervals, the observed statistic, and metadata.
Parameters
the observed data to bootstrap from.
the number of bootstrap resamples to generate. Defaults to 10_000. More resamples produce more stable intervals at the cost of computation time.
the confidence level for the intervals, in (0, 1) exclusive. Defaults to 0.95 (95%).
the random number generator used for resampling. Defaults to Random. Pass a seeded instance (e.g. Random(42)) for reproducible results.
a function that computes the statistic of interest from a List<T>.
Type Parameters
the element type of the data list.