Module Algostream_stochastic.Quantile

Exact sample quantiles and bootstrap confidence intervals.

Do not use Math_utils.Statistics.create_percentile_tracker for any confidence interval. It is reservoir-sampled (so approximate) and seeded with Random.State.make_self_init () (so not reproducible across runs). Both properties are disqualifying for a Monte Carlo result that is supposed to be replayable. Everything here sorts the full sample.

Working in empirical quantiles rather than parametric CDFs is also what lets this module escape the "claim no more than two significant figures" caveat that applies to Advanced_models.Distribution — a percentile interval never inverts a CDF. bca is the one exception; it uses Distribution.Normal and inherits the caveat.

val of_sorted : sorted:float array -> p:float -> float

Type-7 quantile (linear interpolation between order statistics) — the default in R and NumPy. sorted must already be ascending; p is clamped to [0, 1]. Raises Invalid_argument on an empty array.

val quantile : float array -> p:float -> float

of_sorted on a sorted copy of the input. O(n log n) per call — sort once and reuse of_sorted when taking several quantiles from the same sample.

val median : float array -> float
val percentile_interval : float array -> level:float -> float * float

Percentile-method interval. level = 0.95 returns the 2.5% and 97.5% points.

val basic_interval : float array -> point_estimate:float -> level:float -> float * float

Basic (reverse-percentile) interval: (2θ̂ - q_{1-α/2}, 2θ̂ - q_{α/2}). Corrects for bias in the opposite direction to the percentile method; the two disagreeing is a signal the bootstrap distribution is skewed and bca is warranted.

val bca : float array -> point_estimate:float -> jackknife:float array -> level:float -> float * float

Bias-corrected and accelerated interval. jackknife holds the leave-one-out estimates of the statistic, from which the acceleration constant is computed. Preferred when the statistic's distribution is skewed — which Sharpe ratios and maximum drawdowns always are. Falls back to percentile_interval when the sample is degenerate.

val mc_standard_error : float array -> p:float -> float

Monte Carlo standard error of the p-quantile estimate, by the standard order-statistic formula sqrt(p(1-p)/n) / f(q_p) with the density estimated from a finite difference of the empirical quantile function.

Reporting this alongside an interval is the difference between an honest confidence interval and a decorative one: at n = 10_000 and p = 0.99 only ~100 observations sit in the tail, so the 99% level is materially less certain than the 95% level, and the caller deserves to know by how much.

type summary = {
  1. n : int;
  2. mean : float;
  3. stddev : float;
  4. min : float;
  5. max : float;
  6. p01 : float;
  7. p05 : float;
  8. p25 : float;
  9. p50 : float;
  10. p75 : float;
  11. p95 : float;
  12. p99 : float;
  13. ci95_lo : float;
  14. ci95_hi : float;
  15. ci99_lo : float;
  16. ci99_hi : float;
  17. prob_negative : float;
  18. skewness : float;
  19. excess_kurtosis : float;
  20. mc_se_p05 : float;
  21. mc_se_p95 : float;
}

One-pass summary of a Monte Carlo metric distribution.

val summarize : float array -> summary
val summary_to_string : summary -> string