Repository Public
1import Foundation23/// Sample quantile definitions (Hyndman & Fan 1996 numbering; names follow NumPy's `method=`).4public enum QuantileMethod: String, Sendable, Codable, CaseIterable {5 /// Type 1: inverse of the empirical CDF (no interpolation).6 case invertedCDF7 /// Type 2: like type 1 but averaging at discontinuities.8 case averagedInvertedCDF9 /// Type 4: linear interpolation of the empirical CDF.10 case interpolatedInvertedCDF11 /// Type 5: Hazen, plotting position (k − ½)/n.12 case hazen13 /// Type 6: Weibull, k/(n + 1) — Excel PERCENTILE.EXC / QUARTILE.EXC, Minitab, SPSS.14 case weibull15 /// Type 7: linear, (k − 1)/(n − 1) — Excel PERCENTILE.INC / QUARTILE.INC, NumPy and R default. **Default.**16 case linear17 /// Type 8: median-unbiased, (k − ⅓)/(n + ⅓).18 case medianUnbiased19 /// Type 9: approximately unbiased for normal data, (k − ⅜)/(n + ¼).20 case normalUnbiased21}2223extension Descriptive {24 /// Quantile of already sorted data at probability p ∈ [0, 1]. See `QuantileMethod`; default type 7.25 public static func quantile(sorted s: [Double], _ p: Double, method: QuantileMethod = .linear) -> Double {26 let n = s.count27 guard n > 0, p >= 0, p <= 1 else { return .nan }28 if n == 1 { return s[0] }29 let nd = Double(n)30 func at(_ k: Int) -> Double { s[Swift.min(n - 1, Swift.max(0, k))] } // zero-based, clamped31 switch method {32 case .invertedCDF:33 let h = nd * p34 return at(Int(h.rounded(.up)) - 1)35 case .averagedInvertedCDF:36 let h = nd * p37 let k = Int(h.rounded(.down))38 return h == Double(k) ? 0.5 * (at(k - 1) + at(k)) : at(k)39 default:40 // continuous family: virtual index h = n p + m, m = α + p(1 − α − β)41 let (alpha, beta): (Double, Double)42 switch method {43 case .interpolatedInvertedCDF: (alpha, beta) = (0, 1)44 case .hazen: (alpha, beta) = (0.5, 0.5)45 case .weibull: (alpha, beta) = (0, 0)46 case .linear: (alpha, beta) = (1, 1)47 case .medianUnbiased: (alpha, beta) = (1.0 / 3, 1.0 / 3)48 default: (alpha, beta) = (3.0 / 8, 3.0 / 8)49 }50 let h = nd * p + alpha + p * (1 - alpha - beta) // one-based position51 let lo = h.rounded(.down)52 let g = h - lo53 let k = Int(lo)54 if k < 1 { return s[0] }55 if k >= n { return s[n - 1] }56 return s[k - 1] + g * (s[k] - s[k - 1])57 }58 }5960 /// Quantile at probability p ∈ [0, 1] (sorts a copy). Default method: type 7 "linear".61 public static func quantile(_ x: [Double], _ p: Double, method: QuantileMethod = .linear) -> Double {62 quantile(sorted: x.sorted(), p, method: method)63 }6465 /// Several quantiles with a single sort.66 public static func quantiles(_ x: [Double], _ ps: [Double], method: QuantileMethod = .linear) -> [Double] {67 let s = x.sorted()68 return ps.map { quantile(sorted: s, $0, method: method) }69 }7071 /// Median (mean of the two central values for even n).72 public static func median(_ x: [Double]) -> Double {73 let s = x.sorted()74 let n = s.count75 guard n > 0 else { return .nan }76 return n % 2 == 1 ? s[n / 2] : 0.5 * (s[n / 2 - 1] + s[n / 2])77 }7879 public static func minimum(_ x: [Double]) -> Double { x.min() ?? .nan }80 public static func maximum(_ x: [Double]) -> Double { x.max() ?? .nan }81 /// max − min.82 public static func range(_ x: [Double]) -> Double { x.isEmpty ? .nan : x.max()! - x.min()! }8384 /// Interquartile range Q3 − Q1 with the chosen quantile method (default type 7).85 public static func interquartileRange(_ x: [Double], method: QuantileMethod = .linear) -> Double {86 let q = quantiles(x, [0.25, 0.75], method: method)87 return q[1] - q[0]88 }8990 /// All most-frequent values, ascending. Empty when every value occurs once (Excel's MODE returns #N/A then).91 public static func modes(_ x: [Double]) -> [Double] {92 var counts: [Double: Int] = [:]93 for v in x { counts[v, default: 0] += 1 }94 guard let top = counts.values.max(), top > 1 else { return [] }95 return counts.filter { $0.value == top }.keys.sorted()96 }9798 /// The smallest most-frequent value (SciPy `mode`; Excel MODE.SNGL returns the first one met instead), or nil when no value repeats.99 public static func mode(_ x: [Double]) -> Double? { modes(x).first }100}101