SPB Git

Repository Public

Correlation.swift 5.7 KB 129 lines swift

1import Foundation2import SheetCore34public enum CorrelationMethod: String, Sendable, Codable, CaseIterable {5    /// Product-moment correlation.6    case pearson7    /// Pearson correlation of the average ranks.8    case spearman9    /// Kendall's τ-b (adjusted for ties in either variable).10    case kendall11}1213/// How rows with missing values (NaN) are treated when several columns are analysed together.14public enum MissingPolicy: String, Sendable, Codable, CaseIterable {15    /// Drop every row that has a missing value in any column (all statistics share the same rows).16    case listwise17    /// For each pair of columns use the rows where both are present (per-column statistics use all present values).18    case pairwise19}2021/// A square matrix of pairwise statistics with the number of observations behind each entry.22public struct AssociationMatrix: Sendable, Hashable, Codable, TableReport {23    public var names: [String]24    public var values: Matrix25    /// Observations used for each pair.26    public var counts: [[Int]]27    public var kind: String2829    public func table() -> [[Value]] {30        Report.labelled(corner: kind, rows: names, cols: names) { values[$0, $1] }31    }32}3334extension Descriptive {35    /// Spearman rank correlation (Pearson correlation of average ranks, so ties are handled).36    public static func spearman(_ x: [Double], _ y: [Double]) -> Double {37        guard x.count == y.count, x.count > 1 else { return .nan }38        return pearson(ranks(x), ranks(y))39    }4041    /// Kendall's τ-b = (C − D) / √((n₀ − n₁)(n₀ − n₂)), with n₀ = n(n−1)/2 and n₁, n₂ the numbers of42    /// pairs tied in x and in y. O(n²) pair enumeration.43    public static func kendallTauB(_ x: [Double], _ y: [Double]) -> Double {44        kendallCounts(x, y).tau45    }4647    /// Concordant/discordant/tie counts behind Kendall's τ-b.48    static func kendallCounts(_ x: [Double], _ y: [Double]) -> (tau: Double, concordant: Int, discordant: Int, tiesX: Int, tiesY: Int) {49        let n = x.count50        guard n == y.count, n > 1 else { return (.nan, 0, 0, 0, 0) }51        var c = 0, d = 0, tx = 0, ty = 052        for i in 0..<(n - 1) {53            for j in (i + 1)..<n {54                let dx = x[i] - x[j], dy = y[i] - y[j]55                if dx == 0 && dy == 0 { tx += 1; ty += 1 }56                else if dx == 0 { tx += 1 }57                else if dy == 0 { ty += 1 }58                else if (dx > 0) == (dy > 0) { c += 1 } else { d += 1 }59            }60        }61        let n0 = Double(n * (n - 1) / 2)62        let denom = ((n0 - Double(tx)) * (n0 - Double(ty))).squareRoot()63        return (denom > 0 ? Double(c - d) / denom : .nan, c, d, tx, ty)64    }6566    /// Correlation between two series by the chosen method.67    public static func correlation(_ x: [Double], _ y: [Double], method: CorrelationMethod = .pearson) -> Double {68        switch method {69        case .pearson: pearson(x, y)70        case .spearman: spearman(x, y)71        case .kendall: kendallTauB(x, y)72        }73    }7475    /// Rows of `columns` that have no NaN in any column.76    public static func completeRows(_ columns: [[Double]]) -> [Int] {77        guard let n = columns.first?.count else { return [] }78        return (0..<n).filter { r in columns.allSatisfy { !$0[r].isNaN } }79    }8081    private static func pairs(_ columns: [[Double]], missing: MissingPolicy, _ f: ([Double], [Double]) -> Double) throws -> (Matrix, [[Int]]) {82        let k = columns.count83        guard k > 0, let n = columns.first?.count, columns.allSatisfy({ $0.count == n }) else {84            throw StatError.invalidInput("columns must be non-empty and of equal length")85        }86        var cols = columns87        if missing == .listwise {88            let keep = completeRows(columns)89            cols = columns.map { c in keep.map { c[$0] } }90        }91        var m = Matrix(rows: k, cols: k)92        var counts = Array(repeating: Array(repeating: 0, count: k), count: k)93        for i in 0..<k {94            for j in i..<k {95                var a = cols[i], b = cols[j]96                if missing == .pairwise {97                    let keep = a.indices.filter { !a[$0].isNaN && !b[$0].isNaN }98                    a = keep.map { cols[i][$0] }; b = keep.map { cols[j][$0] }99                }100                let v = f(a, b)101                m[i, j] = v; m[j, i] = v102                counts[i][j] = a.count; counts[j][i] = a.count103            }104        }105        return (m, counts)106    }107108    /// Covariance matrix of the columns (NaN = missing). Sample covariance (n − 1) by default.109    public static func covarianceMatrix(_ columns: [[Double]], names: [String]? = nil, sample: Bool = true,110                                        missing: MissingPolicy = .listwise) throws -> AssociationMatrix {111        let (m, counts) = try pairs(columns, missing: missing) { covariance($0, $1, sample: sample) }112        return AssociationMatrix(names: names ?? columns.indices.map { "X\($0 + 1)" }, values: m, counts: counts,113                                 kind: sample ? "Covariance (sample)" : "Covariance (population)")114    }115116    /// Correlation matrix of the columns (NaN = missing) by Pearson, Spearman or Kendall τ-b.117    public static func correlationMatrix(_ columns: [[Double]], names: [String]? = nil, method: CorrelationMethod = .pearson,118                                         missing: MissingPolicy = .listwise) throws -> AssociationMatrix {119        let (m, counts) = try pairs(columns, missing: missing) { correlation($0, $1, method: method) }120        let label: String121        switch method {122        case .pearson: label = "Pearson r"123        case .spearman: label = "Spearman ρ"124        case .kendall: label = "Kendall τ-b"125        }126        return AssociationMatrix(names: names ?? columns.indices.map { "X\($0 + 1)" }, values: m, counts: counts, kind: label)127    }128}129
Files main
Menu

Theme