Repository Public
1import Foundation23/// Descriptive statistics on plain arrays. Functions return NaN when the statistic is undefined4/// (empty input, n < 2 for sample variance…) rather than throwing.5public enum Descriptive {6 /// Arithmetic mean (pairwise-compensated sum).7 public static func mean(_ x: [Double]) -> Double {8 guard !x.isEmpty else { return .nan }9 return sum(x) / Double(x.count)10 }1112 /// Neumaier-compensated sum.13 public static func sum(_ x: [Double]) -> Double {14 var s = 0.0, c = 0.015 for v in x {16 let t = s + v17 c += abs(s) >= abs(v) ? (s - t) + v : (v - t) + s18 s = t19 }20 return s + c21 }2223 /// Sum of squared deviations from the mean (with the second-pass correction for rounding).24 public static func sumSquaredDeviations(_ x: [Double]) -> Double {25 guard !x.isEmpty else { return .nan }26 let m = mean(x)27 var ss = 0.0, comp = 0.028 for v in x { let d = v - m; ss += d * d; comp += d }29 return ss - comp * comp / Double(x.count)30 }3132 /// Variance. `sample` = true divides by n − 1 (Excel VAR.S), false by n (VAR.P).33 public static func variance(_ x: [Double], sample: Bool = true) -> Double {34 let n = x.count35 guard n > (sample ? 1 : 0) else { return .nan }36 return sumSquaredDeviations(x) / Double(sample ? n - 1 : n)37 }3839 /// Standard deviation (Excel STDEV.S / STDEV.P).40 public static func standardDeviation(_ x: [Double], sample: Bool = true) -> Double { variance(x, sample: sample).squareRoot() }4142 /// Covariance of two equally long series (sample: n − 1; population: n).43 public static func covariance(_ x: [Double], _ y: [Double], sample: Bool = true) -> Double {44 let n = x.count45 guard n == y.count, n > (sample ? 1 : 0) else { return .nan }46 let mx = mean(x), my = mean(y)47 var s = 0.048 for i in 0..<n { s += (x[i] - mx) * (y[i] - my) }49 return s / Double(sample ? n - 1 : n)50 }5152 /// Pearson product-moment correlation.53 public static func pearson(_ x: [Double], _ y: [Double]) -> Double {54 let n = x.count55 guard n == y.count, n > 1 else { return .nan }56 let mx = mean(x), my = mean(y)57 var sxy = 0.0, sxx = 0.0, syy = 0.058 for i in 0..<n {59 let dx = x[i] - mx, dy = y[i] - my60 sxy += dx * dy; sxx += dx * dx; syy += dy * dy61 }62 guard sxx > 0, syy > 0 else { return .nan }63 return Swift.max(-1, Swift.min(1, sxy / (sxx * syy).squareRoot()))64 }6566 /// Ranks 1…n with ties receiving the average of the ranks they span (Excel RANK.AVG, SciPy "average").67 public static func ranks(_ x: [Double]) -> [Double] {68 let n = x.count69 let order = x.indices.sorted { x[$0] < x[$1] }70 var out = [Double](repeating: 0, count: n)71 var i = 072 while i < n {73 var j = i74 while j + 1 < n && x[order[j + 1]] == x[order[i]] { j += 1 }75 let avg = Double(i + j) / 2 + 176 for k in i...j { out[order[k]] = avg }77 i = j + 178 }79 return out80 }8182 /// Sizes of the groups of tied values (only groups of 2 or more), for tie corrections.83 public static func tieGroupSizes(_ x: [Double]) -> [Int] {84 let s = x.sorted()85 var out = [Int]()86 var i = 087 while i < s.count {88 var j = i89 while j + 1 < s.count && s[j + 1] == s[i] { j += 1 }90 if j > i { out.append(j - i + 1) }91 i = j + 192 }93 return out94 }95}96