SPB Git

Repository Public

LeastSquaresTests.swift 9.6 KB 193 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetAnalytics/Tests/StatisticsTests/LeastSquaresTests.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 AnalyticsTestSupport90@testable import Statistics9192private let lsFixture = Fixture.load("multivariate", .module)["leastsq"]9394@Suite struct LeastSquaresTests {95    func checkCovariance(_ r: LeastSquaresResult, _ ref: JSON, rel: Double, _ label: String) {96        let want = ref["pcov"].mat97        guard let cov = r.covariance else { Issue.record("\(label): no covariance"); return }98        #expect(allClose(cov.data, want.flatMap { $0 }, rel: rel), "\(label) covariance \(maxRelativeError(cov.data, want.flatMap { $0 }))")99        #expect(allClose(r.standardErrors!, want.indices.map { want[$0][$0].squareRoot() }, rel: rel), "\(label) se")100    }101102    @Test func nistMisra1a() throws {103        let f = lsFixture["misra1a"]104        let model: (Double, [Double]) -> Double = { x, b in b[0] * (1 - exp(-b[1] * x)) }105        for startKey in ["start1", "start2"] {106            let r = try LeastSquares.curveFit(model: model, x: f["x"].arr, y: f["y"].arr, start: f[startKey].arr)107            #expect(r.converged, "\(startKey): \(r.termination)")108            #expect(allClose(r.parameters, f["certified"]["b"].arr, rel: 1e-8), "\(startKey) vs NIST: \(r.parameters)")109            #expect(isClose(r.sumOfSquares, f["certified"]["rss"].d, rel: 1e-9))110            #expect(allClose(r.parameters, f["popt"].arr, rel: 1e-8))111            checkCovariance(r, f, rel: 1e-6, startKey)112            #expect(r.degreesOfFreedom == 12)113            #expect(isClose(r.residualVariance, r.sumOfSquares / 12, rel: 1e-14))114        }115    }116117    @Test func nistDanWood() throws {118        let f = lsFixture["danwood"]119        let model: (Double, [Double]) -> Double = { x, b in b[0] * pow(x, b[1]) }120        for startKey in ["start1", "start2"] {121            let r = try LeastSquares.curveFit(model: model, x: f["x"].arr, y: f["y"].arr, start: f[startKey].arr)122            #expect(r.converged)123            #expect(allClose(r.parameters, f["certified"]["b"].arr, rel: 1e-8), "\(startKey) vs NIST: \(r.parameters)")124            #expect(isClose(r.sumOfSquares, f["certified"]["rss"].d, rel: 1e-9))125            checkCovariance(r, f, rel: 1e-6, startKey)126        }127    }128129    @Test func exponentialDecayAndJacobianVariants() throws {130        let f = lsFixture["decay"]131        let x = f["x"].arr, y = f["y"].arr132        let resid: ([Double]) -> [Double] = { p in zip(x, y).map { p[0] * exp(-p[1] * $0) + p[2] - $1 } }133        let analytic: ([Double]) -> Matrix = { p in134            Matrix(x.map { [exp(-p[1] * $0), -p[0] * $0 * exp(-p[1] * $0), 1] })135        }136        var forward = LeastSquares.Options()137        forward.jacobian = .forward138        let a = try LeastSquares.levenbergMarquardt(residuals: resid, start: f["start"].arr, jacobian: analytic)139        let c = try LeastSquares.levenbergMarquardt(residuals: resid, start: f["start"].arr)140        let fw = try LeastSquares.levenbergMarquardt(residuals: resid, start: f["start"].arr, options: forward)141        for (r, label, tol) in [(a, "analytic", 1e-8), (c, "central", 1e-8), (fw, "forward", 1e-6)] {142            #expect(r.converged, "\(label)")143            #expect(allClose(r.parameters, f["popt"].arr, rel: tol), "\(label) \(r.parameters)")144            #expect(isClose(r.sumOfSquares, f["rss"].d, rel: 1e-9), "\(label) rss")145        }146        checkCovariance(a, f, rel: 1e-7, "analytic")147        checkCovariance(c, f, rel: 1e-6, "central")148        checkCovariance(fw, f, rel: 1e-4, "forward")149        #expect(a.functionEvaluations < c.functionEvaluations)150        #expect(a.gradientNorm < 1e-6)151        #expect(a.table().count == 10)152    }153154    @Test func weightedGaussianPeak() throws {155        let f = lsFixture["gauss"]156        let sigma = f["sigma"].arr157        let r = try LeastSquares.curveFit(model: { x, p in p[0] * exp(-0.5 * pow((x - p[1]) / p[2], 2)) },158                                          x: f["x"].arr, y: f["y"].arr, start: f["start"].arr, weights: sigma.map { 1 / $0 })159        #expect(r.converged)160        #expect(allClose(r.parameters, f["popt"].arr, rel: 1e-8))161        checkCovariance(r, f, rel: 1e-6, "gauss")162    }163164    @Test func rosenbrockAndLinearProblems() throws {165        let r = try LeastSquares.levenbergMarquardt(residuals: { p in [10 * (p[1] - p[0] * p[0]), 1 - p[0]] }, start: [-1.2, 1])166        #expect(r.converged)167        #expect(allClose(r.parameters, [1, 1], rel: 1e-9))                    // analytic minimum168        #expect(allClose(r.parameters, lsFixture["rosenbrock"]["x"].arr, rel: 1e-8))169        #expect(r.sumOfSquares < 1e-18)170        #expect(r.covariance == nil && r.degreesOfFreedom == 0)               // n = p: no residual variance171        // a linear model is solved exactly: y = 2 + 3x with one outlier-free fit172        let xs = [0.0, 1, 2, 3, 4], ys = [2.1, 4.9, 8.2, 10.9, 14.1]173        let lin = try LeastSquares.curveFit(model: { x, p in p[0] + p[1] * x }, x: xs, y: ys, start: [0, 0])174        let slope = Descriptive.covariance(xs, ys) / Descriptive.variance(xs)175        #expect(allClose(lin.parameters, [Descriptive.mean(ys) - slope * 2, slope], rel: 1e-10))176        // OLS standard error of the slope: s / √Sxx177        #expect(isClose(lin.standardErrors![1], (lin.residualVariance / Descriptive.sumSquaredDeviations(xs)).squareRoot(), rel: 1e-8))178        // already at the optimum179        let done = try LeastSquares.levenbergMarquardt(residuals: { p in [p[0] - 1, p[1] + 2] }, start: [1, -2])180        #expect(done.converged && done.iterations == 0 && done.termination == "gradient")181    }182183    @Test func invalidInputs() {184        #expect(throws: StatError.self) { try LeastSquares.levenbergMarquardt(residuals: { _ in [1] }, start: []) }185        #expect(throws: StatError.self) { try LeastSquares.levenbergMarquardt(residuals: { _ in [.nan] }, start: [1]) }186        #expect(throws: StatError.self) { try LeastSquares.curveFit(model: { x, _ in x }, x: [1, 2], y: [1], start: [1]) }187        #expect(throws: StatError.self) { try LeastSquares.curveFit(model: { x, _ in x }, x: [1, 2], y: [1, 2], start: [1], weights: [1]) }188        #expect(throws: StatError.self) {189            try LeastSquares.levenbergMarquardt(residuals: { p in [p[0], p[0] * 2] }, start: [1], jacobian: { _ in Matrix(rows: 1, cols: 1) })190        }191    }192}193
Files main
Menu

Theme