SPB Git

Repository Public

Moments.swift 3.5 KB 73 lines swift

1import Foundation23extension Descriptive {4    /// k-th central moment (divisor n).5    public static func centralMoment(_ x: [Double], _ k: Int) -> Double {6        guard !x.isEmpty else { return .nan }7        let m = mean(x)8        return x.reduce(0) { $0 + Foundation.pow($1 - m, Double(k)) } / Double(x.count)9    }1011    /// Skewness.12    /// - `sample: true` (default): the adjusted Fisher–Pearson coefficient G₁ = √(n(n−1))/(n−2) · g₁ — Excel SKEW, SciPy `skew(bias=False)`. Needs n ≥ 3.13    /// - `sample: false`: the population (moment) coefficient g₁ = m₃ / m₂^{3/2} — Excel SKEW.P, SciPy `skew()`.14    public static func skewness(_ x: [Double], sample: Bool = true) -> Double {15        let n = Double(x.count)16        guard x.count >= (sample ? 3 : 1) else { return .nan }17        let m2 = centralMoment(x, 2), m3 = centralMoment(x, 3)18        guard m2 > 0 else { return .nan }19        let g1 = m3 / Foundation.pow(m2, 1.5)20        return sample ? (n * (n - 1)).squareRoot() / (n - 2) * g1 : g121    }2223    /// Excess kurtosis (0 for a normal distribution).24    /// - `sample: true` (default): G₂ = (n−1)/((n−2)(n−3)) · ((n+1)·g₂ + 6) — Excel KURT, SciPy `kurtosis(bias=False)`. Needs n ≥ 4.25    /// - `sample: false`: the population coefficient g₂ = m₄ / m₂² − 3 — SciPy `kurtosis()`.26    public static func kurtosis(_ x: [Double], sample: Bool = true) -> Double {27        let n = Double(x.count)28        guard x.count >= (sample ? 4 : 1) else { return .nan }29        let m2 = centralMoment(x, 2), m4 = centralMoment(x, 4)30        guard m2 > 0 else { return .nan }31        let g2 = m4 / (m2 * m2) - 332        return sample ? (n - 1) / ((n - 2) * (n - 3)) * ((n + 1) * g2 + 6) : g233    }3435    /// Standard error of the mean: s / √n with the sample standard deviation.36    public static func standardError(_ x: [Double]) -> Double {37        x.count > 1 ? standardDeviation(x) / Double(x.count).squareRoot() : .nan38    }3940    /// Coefficient of variation s / mean (sample standard deviation by default). NaN when the mean is 0.41    public static func coefficientOfVariation(_ x: [Double], sample: Bool = true) -> Double {42        let m = mean(x)43        return m == 0 ? .nan : standardDeviation(x, sample: sample) / m44    }4546    /// Geometric mean of strictly positive data (NaN otherwise).47    public static func geometricMean(_ x: [Double]) -> Double {48        guard !x.isEmpty, x.allSatisfy({ $0 > 0 }) else { return .nan }49        return exp(sum(x.map { log($0) }) / Double(x.count))50    }5152    /// Harmonic mean of strictly positive data (NaN otherwise).53    public static func harmonicMean(_ x: [Double]) -> Double {54        guard !x.isEmpty, x.allSatisfy({ $0 > 0 }) else { return .nan }55        return Double(x.count) / sum(x.map { 1 / $0 })56    }5758    /// Mean after removing the fraction `proportion` of observations from EACH tail59    /// (SciPy `trim_mean`: ⌊proportion·n⌋ values cut per side).60    public static func trimmedMean(_ x: [Double], proportion: Double) -> Double {61        guard !x.isEmpty, proportion >= 0, proportion < 0.5 else { return .nan }62        let s = x.sorted()63        let cut = Int((proportion * Double(s.count)).rounded(.down))64        return mean(Array(s[cut..<(s.count - cut)]))65    }6667    /// z-scores (x − mean) / sd with the sample standard deviation by default.68    public static func standardized(_ x: [Double], sample: Bool = true) -> [Double] {69        let m = mean(x), s = standardDeviation(x, sample: sample)70        return x.map { ($0 - m) / s }71    }72}73
Files main
Menu

Theme