use histquantile; use rankband; fn nightingale_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn nightingale_counts(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn nightingale_compute(counts: vector, edges: vector, probability: f64, threshold: f64) -> vector { if len(edges) != len(counts) + 1 { return []; } let rank = histquantile_bucket(counts, probability); if len(rank) == 0 { return []; } let band = rankband_interval(edges, at(rank, 0)); if len(band) == 0 { return []; } let mut decision = 0; if at(band, 1) < threshold { decision = -1; } if at(band, 0) >= threshold { decision = 1; } [at(band, 0), at(band, 1), at(rank, 1), decision] } // Bound a percentile from histogram counts without inventing a within-bin value. // Returns [lower bound, upper bound, rank, classification]. Classification is -1 when the whole interval is below threshold, 1 when entirely at or above, and 0 when the bins cannot decide. The upper-bound comparison is deliberately conservative. pub fn nightingale_quantile(counts: vector, edges: vector, probability: f64, threshold: f64) -> vector { if !nightingale_counts(counts) { return []; } if !nightingale_finite(edges) { return []; } if !(probability >= 0.0 && probability <= 1.0) { return []; } if !(threshold - threshold == 0.0) { return []; } let result = nightingale_compute(counts, edges, probability, threshold); if !nightingale_finite(result) { return []; } result }