Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Sheet9// File : Packages/SheetAnalytics/Tests/StatisticsTests/RegressionDiagnosticsTests.swift10// Module : StatisticsTests11// Repository : https://git.spboucher.ai/orix-sheet12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import AnalyticsTestSupport90import SheetCore91@testable import Statistics9293@Suite struct RegressionDiagnosticsTests {94 let f = regressionFixture["ols"]95 let d = regressionFixture["ols"]["diag"]96 var model: LinearModelResult { try! LinearRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat)) }9798 /// statsmodels returns (lm, lm_p, f, f_p).99 func expectLM(_ t: RegressionDiagnostic, _ ref: JSON, _ label: String, rel: Double = 1e-8) {100 #expect(isClose(t.statistic, ref[0].d, rel: rel), "\(label) LM \(t.statistic) vs \(ref[0].d)")101 #expect(isClose(t.pValue ?? .nan, ref[1].d, rel: rel, abs: 1e-300), "\(label) LM p \(t.pValue ?? .nan) vs \(ref[1].d)")102 #expect(isClose(t.alternativeStatistic ?? .nan, ref[2].d, rel: rel), "\(label) F \(t.alternativeStatistic ?? .nan) vs \(ref[2].d)")103 #expect(isClose(t.alternativePValue ?? .nan, ref[3].d, rel: rel, abs: 1e-300), "\(label) F p")104 #expect(t.tail == "upper-tail (one-sided)" && !t.assumptions.isEmpty && !t.nullHypothesis.isEmpty)105 }106107 @Test func breuschPagan() throws {108 let bp = try RegressionDiagnostics.breuschPagan(model)109 expectLM(bp, d["bp"], "BP studentized")110 #expect(bp.distribution == "chi2(3)" && bp.df == [3] && bp.alternativeDistribution == "F(3, 56)")111 #expect(bp.name.contains("Koenker"))112 let orig = try RegressionDiagnostics.breuschPagan(model, studentized: false)113 expectLM(orig, d["bp_original"], "BP original")114 #expect(orig.statistic != bp.statistic)115 let noConst = try LinearRegression.fit(y: f["y"].arr, x: Matrix(f["X"].mat), intercept: false)116 #expect(throws: StatError.self) { try RegressionDiagnostics.breuschPagan(noConst) }117 }118119 @Test func white() throws {120 let w = try RegressionDiagnostics.white(model)121 expectLM(w, d["white"], "White")122 #expect(w.df == [9]) // 3 regressors + 3 squares + 3 cross products123 // one regressor: constant, x, x² → 2 df124 let simple = try LinearRegression.fit(y: f["y"].arr, columns: [Matrix(f["X"].mat).column(0)])125 let ws = try RegressionDiagnostics.white(simple)126 expectLM(ws, regressionFixture["ols_simple"]["white"], "White simple")127 #expect(ws.df == [2])128 // a 0/1 dummy squared is itself: the redundant column is dropped instead of breaking the regression129 let dummy = (0..<60).map { Double($0 % 2) }130 let withDummy = try LinearRegression.fit(y: f["y"].arr, columns: [Matrix(f["X"].mat).column(0), dummy])131 #expect(try RegressionDiagnostics.white(withDummy).df == [4]) // x, d, x², x·d132 }133134 @Test func durbinWatson() throws {135 #expect(isClose(RegressionDiagnostics.durbinWatson(model.residuals), d["dw"].d, rel: 1e-9))136 let rec = RegressionDiagnostics.durbinWatson(model)137 #expect(isClose(rec.statistic, d["dw"].d, rel: 1e-9) && rec.pValue == nil && rec.distribution.isEmpty)138 #expect(RegressionDiagnostics.durbinWatson([1, -1, 1, -1]) == 3) // (3·4)/4139 #expect(RegressionDiagnostics.durbinWatson([2, 2, 2]) == 0)140 #expect(RegressionDiagnostics.durbinWatson([1]).isNaN)141 }142143 @Test func breuschGodfrey() throws {144 let b1 = try RegressionDiagnostics.breuschGodfrey(model, lags: 1)145 expectLM(b1, d["bg1"], "BG(1)")146 #expect(b1.distribution == "chi2(1)" && b1.alternativeDistribution == "F(1, 55)")147 let b3 = try RegressionDiagnostics.breuschGodfrey(model, lags: 3)148 expectLM(b3, d["bg3"], "BG(3)")149 #expect(b3.alternativeDistribution == "F(3, 53)")150 #expect((b1.pValue ?? 1) < 0.05) // the fixture errors are AR(1) with ρ = 0.5151 #expect(throws: StatError.self) { try RegressionDiagnostics.breuschGodfrey(model, lags: 0) }152 #expect(throws: StatError.self) { try RegressionDiagnostics.breuschGodfrey(model, lags: 56) }153 }154155 @Test func ljungBoxAndBoxPierce() throws {156 let e = model.residuals157 for (i, lag) in d["lb_lags"].ints.enumerated() {158 let lb = try RegressionDiagnostics.ljungBox(e, lags: lag)159 #expect(isClose(lb.statistic, d["lb_stat"][i].d, rel: 1e-8), "LB(\(lag))")160 #expect(isClose(lb.pValue ?? .nan, d["lb_p"][i].d, rel: 1e-8), "LB(\(lag)) p")161 #expect(lb.df == [Double(lag)])162 let bp = try RegressionDiagnostics.ljungBox(e, lags: lag, boxPierce: true)163 #expect(isClose(bp.statistic, d["bp_stat"][i].d, rel: 1e-8), "BoxPierce(\(lag))")164 #expect(isClose(bp.pValue ?? .nan, d["bp_p"][i].d, rel: 1e-8), "BoxPierce(\(lag)) p")165 #expect(bp.statistic < lb.statistic)166 }167 let adj = try RegressionDiagnostics.ljungBox(e, lags: 5, modelDF: 2)168 #expect(isClose(adj.statistic, d["lb5_df2"][0].d, rel: 1e-8) && isClose(adj.pValue ?? .nan, d["lb5_df2"][1].d, rel: 1e-8))169 #expect(adj.distribution == "chi2(3)")170 #expect(throws: StatError.self) { try RegressionDiagnostics.ljungBox(e, lags: 2, modelDF: 2) }171 #expect(throws: StatError.self) { try RegressionDiagnostics.ljungBox(e, lags: 60) }172 #expect(throws: StatError.self) { try RegressionDiagnostics.ljungBox(e, lags: 0) }173 }174175 @Test func jarqueBera() throws {176 let jb = try RegressionDiagnostics.jarqueBera(model)177 #expect(isClose(jb.statistic, d["jb"][0].d, rel: 1e-8))178 #expect(isClose(jb.pValue ?? .nan, d["jb"][1].d, rel: 1e-8))179 let m = RegressionDiagnostics.residualMoments(model.residuals)180 #expect(isClose(m.skewness, d["jb"][2].d, rel: 1e-8) && isClose(m.kurtosis, d["jb"][3].d, rel: 1e-8))181 #expect(jb.distribution == "chi2(2)")182 // symmetric two-point data: S = 0, K = 1 → JB = n/6·(4/4) = n/6183 #expect(isClose(try RegressionDiagnostics.jarqueBera([1, -1, 1, -1, 1, -1]).statistic, 1, rel: 1e-13))184 #expect(throws: StatError.self) { try RegressionDiagnostics.jarqueBera([1, 2]) }185 #expect(throws: StatError.self) { try RegressionDiagnostics.jarqueBera([3, 3, 3, 3]) }186 }187188 @Test func ramseyReset() throws {189 for (power, key) in [(2, "reset2"), (3, "reset3"), (4, "reset4")] {190 let r = try RegressionDiagnostics.reset(model, power: power)191 #expect(isClose(r.statistic, d[key][0].d, rel: 1e-6), "RESET(\(power)) \(r.statistic) vs \(d[key][0].d)")192 #expect(isClose(r.pValue ?? .nan, d[key][1].d, rel: 1e-6, abs: 1e-300), "RESET(\(power)) p")193 #expect(r.df == [Double(power - 1), Double(56 - (power - 1))])194 }195 let r3 = try RegressionDiagnostics.reset(model, power: 3)196 #expect(isClose(r3.alternativeStatistic ?? .nan, d["reset3_chi2"][0].d, rel: 1e-6))197 #expect(isClose(r3.alternativePValue ?? .nan, d["reset3_chi2"][1].d, rel: 1e-6, abs: 1e-300))198 #expect(throws: StatError.self) { try RegressionDiagnostics.reset(model, power: 1) }199 // a model that is quadratic by construction but fitted linearly is flagged; adding the square clears it200 let xs = (0..<40).map { Double($0) / 4 }201 let yq = xs.enumerated().map { 1 + 0.5 * $1 + 0.3 * $1 * $1 + sin(Double($0) * 1.7) * 0.2 }202 let linear = try LinearRegression.fit(y: yq, columns: [xs])203 let quadratic = try LinearRegression.fit(y: yq, columns: [xs, xs.map { $0 * $0 }])204 let pLinear = try RegressionDiagnostics.reset(linear, power: 2).pValue ?? 1205 let pQuadratic = try RegressionDiagnostics.reset(quadratic, power: 2).pValue ?? 0206 #expect(pLinear < 1e-6 && pQuadratic > 0.05)207 _ = try JSONEncoder().encode(RegressionDiagnostics.durbinWatson(model))208 let t = r3.table()209 #expect(t[0][0] == .text(r3.name) && t.contains { $0[0] == .text("p-value") && $0[1] == .number(r3.pValue ?? .nan) })210 #expect(t.contains { $0[0] == .text("Tail") && $0[1] == .text("upper-tail (one-sided)") })211 #expect(try JSONDecoder().decode(RegressionDiagnostic.self, from: JSONEncoder().encode(r3)) == r3)212 }213}214