Repository Public
1import Foundation2import Testing3import AnalyticsTestSupport4import SheetCore5@testable import Statistics67@Suite struct RegressionPanelTests {8 func data(_ key: String) -> (y: [Double], x: Matrix, entity: [Int], time: [Int], f: JSON) {9 let f = regressionFixture[key]10 return (f["y"].arr, Matrix(columns: [f["p1"].arr, f["p2"].arr]), f["entity"].ints, f["time"].ints, f)11 }1213 func check(_ key: String, twoWay: Bool) throws {14 let (y, x, entity, time, all) = data(key)15 let f = all[twoWay ? "twoway" : "oneway"]16 let label = "\(key) \(twoWay ? "two-way" : "one-way")"17 let r = try PanelRegression.fixedEffects(y: y, x: x, names: ["p1", "p2"], entity: entity, time: time, timeEffects: twoWay)18 // linearmodels PanelOLS19 #expect(allClose(r.coefficients, f["params"].arr, rel: 1e-9), "\(label) params")20 #expect(allClose(r.standardErrors, f["se"].arr, rel: 1e-9), "\(label) se")21 #expect(allClose(r.statistics, f["t"].arr, rel: 1e-9), "\(label) t")22 #expect(allClose(r.covariance.data, f["cov"].mat.flatMap { $0 }, rel: 1e-8, abs: 1e-14), "\(label) cov")23 #expect(r.dfResidual == f["df_resid"].i, "\(label) df")24 #expect(isClose(r.sigma2, f["s2"].d, rel: 1e-10) && isClose(r.ssr, f["ssr"].d, rel: 1e-10), "\(label) s2")25 #expect(isClose(r.rSquared, f["rsquared"].d, rel: 1e-9), "\(label) R² \(r.rSquared) vs \(f["rsquared"].d)")26 #expect(isClose(r.fStatistic, f["f"].d, rel: 1e-8) && isClose(r.fPValue, f["f_p"].d, rel: 1e-6, abs: 1e-13), "\(label) F")27 #expect(isClose(r.poolabilityF, f["f_pooled"].d, rel: 1e-8), "\(label) poolability \(r.poolabilityF) vs \(f["f_pooled"].d)")28 #expect(isClose(r.poolabilityPValue, f["f_pooled_p"].d, rel: 1e-6, abs: 1e-13) && r.dfPoolability == f["f_pooled_df"].i, "\(label) poolability p")29 #expect(allClose(r.residuals, f["resid"].arr, rel: 1e-7, abs: 1e-9), "\(label) residuals")30 #expect(allClose(r.estimatedEffects, f["effects"].arr, rel: 1e-8, abs: 1e-9), "\(label) effects")31 // statsmodels LSDV (explicit dummies): same slopes, t-based p-values and intervals, entity effects32 #expect(allClose(r.coefficients, f["lsdv_params"].arr, rel: 1e-9))33 #expect(allClose(r.standardErrors, f["lsdv_se"].arr, rel: 1e-9))34 #expect(allClose(r.pValues, f["lsdv_p"].arr, rel: 1e-8, abs: 1e-300), "\(label) p")35 #expect(allClose(r.ciLower, f["lsdv_ci"].mat.map { $0[0] }, rel: 1e-9) && allClose(r.ciUpper, f["lsdv_ci"].mat.map { $0[1] }, rel: 1e-9))36 #expect(allClose(r.entityEffects, f["lsdv_entity_effects"].arr, rel: 1e-8, abs: 1e-9), "\(label) entity effects")37 #expect(r.usesT && r.robustFStatistic == nil)38 #expect(r.timeEffects == twoWay && r.timeLabels.count == (twoWay ? r.timeCount : 0))39 if twoWay { #expect(r.timeEffectValues[0] == 0 && r.timeLabels[0] == "2000") }4041 // heteroskedasticity-robust (linearmodels 'robust')42 let rob = try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: time, timeEffects: twoWay, covariance: .robust)43 #expect(allClose(rob.standardErrors, f["se_robust"].arr, rel: 1e-9), "\(label) robust se")44 #expect(isClose(rob.robustFStatistic ?? .nan, f["f_robust"].d, rel: 1e-8), "\(label) robust F")45 #expect(isClose(rob.robustFPValue ?? .nan, f["f_robust_p"].d, rel: 1e-6, abs: 1e-13))46 #expect(!rob.usesT && rob.coefficients == r.coefficients)47 // clustered by entity: linearmodels scaling, the plain sandwich, and the LSDV correction (statsmodels)48 let cl = try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: time, timeEffects: twoWay,49 covariance: .clusteredByEntity(correction: .linearmodels))50 #expect(allClose(cl.standardErrors, f["se_cluster"].arr, rel: 1e-9), "\(label) cluster se")51 #expect(isClose(cl.robustFStatistic ?? .nan, f["f_cluster"].d, rel: 1e-8), "\(label) cluster F")52 let plain = try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: time, timeEffects: twoWay,53 covariance: .clusteredByEntity(correction: .none))54 let nn = Double(y.count)55 let factor = nn / (twoWay ? Double(r.dfResidual) : nn - 2)56 #expect(allClose(plain.standardErrors.map { $0 * factor.squareRoot() }, f["se_cluster"].arr, rel: 1e-9), "\(label) plain sandwich")57 let ls = try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: time, timeEffects: twoWay,58 covariance: .clusteredByEntity(correction: .lsdv))59 #expect(allClose(ls.standardErrors, f["lsdv_cluster_se"].arr, rel: 1e-8), "\(label) LSDV cluster se \(maxRelativeError(ls.standardErrors, f["lsdv_cluster_se"].arr))")60 #expect(allClose(ls.pValues, f["lsdv_cluster_p"].arr, rel: 1e-7, abs: 1e-300))61 let lst = try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: time, timeEffects: twoWay,62 covariance: .clusteredByEntity(correction: .lsdv), useT: true)63 #expect(allClose(lst.pValues, f["lsdv_cluster_t_p"].arr, rel: 1e-7, abs: 1e-300), "\(label) cluster t p")64 #expect(allClose(lst.ciLower, f["lsdv_cluster_t_ci"].mat.map { $0[0] }, rel: 1e-8))65 #expect(lst.dfInference == Double(lst.entityCount - 1))66 }6768 @Test func balancedOneWay() throws { try check("panel_balanced", twoWay: false) }69 @Test func balancedTwoWay() throws { try check("panel_balanced", twoWay: true) }70 @Test func unbalancedOneWay() throws { try check("panel_unbalanced", twoWay: false) }71 @Test func unbalancedTwoWay() throws { try check("panel_unbalanced", twoWay: true) }7273 @Test func labelsShapeAndErrors() throws {74 let (y, x, entity, _, _) = data("panel_balanced")75 // string entity ids, no time index76 let named = try PanelRegression.fixedEffects(y: y, x: x, entity: entity.map { "firm\($0)" })77 let byInt = try PanelRegression.fixedEffects(y: y, x: x, entity: entity)78 #expect(named.coefficients == byInt.coefficients && named.entityLabels.first == "firm0" && named.entityCount == 12 && named.timeCount == 0)79 #expect(named.names == ["x1", "x2"])80 // within-estimator identity: adding an entity-specific constant to y changes only the effects81 let shifted = zip(y, entity).map { $0 + 100 * Double($1) }82 let s = try PanelRegression.fixedEffects(y: shifted, x: x, entity: entity)83 #expect(allClose(s.coefficients, byInt.coefficients, rel: 1e-9) && allClose(s.standardErrors, byInt.standardErrors, rel: 1e-8))84 #expect(isClose(s.entityEffects[3] - byInt.entityEffects[3], 300, rel: 1e-9))85 // a time-invariant regressor is absorbed by the entity effects86 let invariant = Matrix(columns: [x.column(0), entity.map { Double($0 * $0) }])87 #expect(throws: StatError.self) { try PanelRegression.fixedEffects(y: y, x: invariant, entity: entity) }88 #expect(throws: StatError.self) { try PanelRegression.fixedEffects(y: y, x: x, entity: Array(repeating: 1, count: y.count)) }89 #expect(throws: StatError.self) { try PanelRegression.fixedEffects(y: y, x: x, entity: [1, 2]) }90 #expect(throws: StatError.self) { try PanelRegression.fixedEffects(y: y, x: x, entity: entity, time: Optional<[Int]>.none, timeEffects: true) }91 #expect(throws: StatError.self) { try PanelRegression.fixedEffects(y: [1, 2, 3, 4], x: Matrix(columns: [[1, 2, 3, 5]]), entity: [1, 1, 2, 2], time: [1, 2, 1, 2], timeEffects: true) }92 let t = byInt.table()93 #expect(t[0][0] == .text("Fixed-effects (within) regression") && t.allSatisfy { $0.count == 7 })94 #expect(t.contains { $0[0] == .text("Entities") && $0[1] == .number(12) })95 #expect(t.contains { $0[0] == .text("x1") && $0[1] == .number(byInt.coefficients[0]) })96 #expect(t.contains { $0[0] == .text("Entity") && $0[1] == .text("Effect") })97 #expect(try JSONDecoder().decode(PanelResult.self, from: JSONEncoder().encode(byInt)) == byInt)98 #expect(PanelCovariance.clusteredByEntity(correction: .lsdv).label.contains("LSDV"))99 }100}101102@Suite struct RegressionLogisticTests {103 let f = regressionFixture["logit"]104105 @Test func logitMatchesStatsmodels() throws {106 let r = try LogisticRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat), names: ["z1", "z2", "z3"])107 #expect(r.converged && r.iterations < 15)108 #expect(allClose(r.coefficients, f["params"].arr, rel: 1e-9), "params \(maxRelativeError(r.coefficients, f["params"].arr))")109 #expect(allClose(r.standardErrors, f["bse"].arr, rel: 1e-9), "bse \(maxRelativeError(r.standardErrors, f["bse"].arr))")110 #expect(allClose(r.statistics, f["z"].arr, rel: 1e-9))111 #expect(allClose(r.pValues, f["p"].arr, rel: 1e-8, abs: 1e-300))112 #expect(allClose(r.ciLower, f["ci_lo"].arr, rel: 1e-9) && allClose(r.ciUpper, f["ci_hi"].arr, rel: 1e-9))113 #expect(allClose(r.covariance.data, f["cov"].mat.flatMap { $0 }, rel: 1e-8, abs: 1e-13))114 #expect(allClose(r.oddsRatios, f["params"].arr.map { exp($0) }, rel: 1e-9))115 #expect(isClose(r.logLikelihood, f["llf"].d, rel: 1e-12))116 #expect(isClose(r.nullLogLikelihood, f["llnull"].d, rel: 1e-9))117 #expect(isClose(r.pseudoRSquared, f["prsquared"].d, rel: 1e-8))118 #expect(isClose(r.lrStatistic, f["llr"].d, rel: 1e-8) && isClose(r.lrPValue ?? .nan, f["llr_pvalue"].d, rel: 1e-7, abs: 1e-300))119 #expect(isClose(r.aic, f["aic"].d, rel: 1e-12) && isClose(r.bic, f["bic"].d, rel: 1e-12))120 #expect(allClose(r.fittedProbabilities, f["fitted"].arr, rel: 1e-9))121 #expect(r.classificationTable.flatMap { $0 }.map(Double.init) == f["pred_table_05"].mat.flatMap { $0 })122 #expect(r.names == ["const", "z1", "z2", "z3"] && r.dfModel == 3 && r.dfResidual == 246 && r.nobs == 250)123 #expect(allClose(try r.predict(Matrix(f["predict_X"].mat)), f["predict"].arr, rel: 1e-9))124 let t03 = try LogisticRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat), threshold: 0.3)125 #expect(t03.classificationTable.flatMap { $0 }.map(Double.init) == f["pred_table_03"].mat.flatMap { $0 })126 let tab = r.classificationTable127 #expect(isClose(r.accuracy, Double(tab[0][0] + tab[1][1]) / 250, rel: 1e-15))128 #expect(isClose(r.sensitivity, Double(tab[1][1]) / f["y"].arr.reduce(0, +), rel: 1e-15))129 #expect(isClose(r.specificity, Double(tab[0][0]) / (250 - f["y"].arr.reduce(0, +)), rel: 1e-15))130 // score equations hold at the optimum: Xᵀ(y − p) = 0131 let resid = zip(f["y"].arr, r.fittedProbabilities).map { $0 - $1 }132 #expect(abs(resid.reduce(0, +)) < 1e-8)133 #expect(abs(zip(Matrix(f["X"].mat).column(1), resid).reduce(0) { $0 + $1.0 * $1.1 }) < 1e-7)134 }135136 @Test func withoutInterceptAndReport() throws {137 let r = try LogisticRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat), intercept: false)138 #expect(allClose(r.coefficients, f["noconst_params"].arr, rel: 1e-9))139 #expect(allClose(r.standardErrors, f["noconst_bse"].arr, rel: 1e-9))140 #expect(isClose(r.logLikelihood, f["noconst_llf"].d, rel: 1e-12))141 let full = try LogisticRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat), names: ["z1", "z2", "z3"])142 let t = full.table()143 #expect(t.allSatisfy { $0.count == 8 } && t[0][0] == .text("Logistic regression"))144 #expect(t.contains { $0[0] == .text("z1") && $0[1] == .number(full.coefficients[1]) && $0[7] == .number(full.oddsRatios[1]) })145 #expect(t.contains { $0[0] == .text("Actual 1") && $0[2] == .number(Double(full.classificationTable[1][1])) })146 #expect(t.contains { $0[0] == .text("McFadden pseudo R-squared") && $0[1] == .number(full.pseudoRSquared) })147 #expect(try JSONDecoder().decode(LogisticResult.self, from: JSONEncoder().encode(full)) == full)148 }149150 @Test func interceptOnlyHasClosedForm() throws {151 // one balanced dummy: coefficients are log-odds and log odds ratio of the 2×2 table152 // group 0: 3 of 10 successes; group 1: 7 of 10153 let g = (0..<20).map { Double($0 / 10) }154 let yy = (0..<20).map { i -> Double in i < 10 ? (i < 3 ? 1 : 0) : (i < 17 ? 1 : 0) }155 let r = try LogisticRegression.fit(y: yy, columns: [g])156 #expect(isClose(r.coefficients[0], log(3.0 / 7), rel: 1e-9))157 #expect(isClose(r.oddsRatios[1], (7.0 / 3) / (3.0 / 7), rel: 1e-9))158 #expect(isClose(r.standardErrors[1], (1.0 / 3 + 1.0 / 7 + 1.0 / 7 + 1.0 / 3).squareRoot(), rel: 1e-9)) // Woolf159 #expect(isClose(r.nullLogLikelihood, 20 * log(0.5), rel: 1e-13))160 // the null log-likelihood is the closed form n·[p̄ ln p̄ + (1 − p̄) ln(1 − p̄)]161 let yb = f["y"].arr, pbar = yb.reduce(0, +) / 250162 let full = try LogisticRegression.fit(y: yb, x: Matrix(f["X"].mat))163 #expect(isClose(full.nullLogLikelihood, 250 * (pbar * log(pbar) + (1 - pbar) * log(1 - pbar)), rel: 1e-14))164 }165166 @Test func separationAndBadInputAreReported() {167 let xs = (0..<20).map { Double($0) }168 let perfect = xs.map { $0 < 10 ? 0.0 : 1.0 }169 #expect(throws: StatError.self) { try LogisticRegression.fit(y: perfect, columns: [xs]) }170 do { _ = try LogisticRegression.fit(y: perfect, columns: [xs]); Issue.record("separation not detected") } catch let e as StatError {171 if case .notConverged(let m) = e { #expect(m.contains("separation")) } else { Issue.record("wrong error \(e)") }172 } catch { Issue.record("wrong error type") }173 // quasi-complete separation: one overlapping point174 var quasi = perfect; quasi[9] = 1; quasi[10] = 1175 let q = xs.map { $0 == 9 || $0 == 10 ? 9.5 : $0 }176 var q2 = quasi; q2[9] = 0177 #expect(throws: StatError.self) { try LogisticRegression.fit(y: q2, columns: [q]) }178 #expect(throws: StatError.self) { try LogisticRegression.fit(y: xs, columns: [xs]) } // not 0/1179 #expect(throws: StatError.self) { try LogisticRegression.fit(y: Array(repeating: 1, count: 20), columns: [xs]) } // single class180 #expect(throws: StatError.self) { try LogisticRegression.fit(y: perfect, columns: [xs, xs.map { 2 * $0 }]) } // collinear181 #expect(throws: StatError.self) { try LogisticRegression.fit(y: [0, 1], columns: [[1, 2], [3, 5]]) } // n ≤ k182 #expect(throws: StatError.self) { try LogisticRegression.fit(y: [0, 1, 1], columns: [[1, 2]]) }183 }184185 func columns(_ c: [[Double]]) -> Matrix { Matrix(columns: c) }186}187188private extension LogisticRegression {189 static func fit(y: [Double], columns: [[Double]], intercept: Bool = true) throws -> LogisticResult {190 guard columns.allSatisfy({ $0.count == y.count }) else { throw StatError.invalidInput("length mismatch") }191 return try fit(y: y, x: Matrix(columns: columns), intercept: intercept)192 }193}194