Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Sheet9// File : Packages/SheetCharts/Tests/SheetChartsTests/NumericTests.swift10// Module : SheetChartsTests11// 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 CoreGraphics88import Foundation89import SheetCore90import Testing91@testable import SheetCharts9293@Suite("Numeric algorithms vs numpy / scipy / matplotlib")94struct NumericTests {95 let ref = Reference.shared9697 @Test func niceLinearTicksMatchMaxNLocator() {98 for c in ref.ticks {99 let t = Ticks.linear(min: c.min, max: c.max, maxIntervals: c.intervals, extend: true)100 #expect(t.count == c.ticks.count, "ticks \(c.min)…\(c.max) /\(c.intervals): \(t) vs \(c.ticks)")101 let step = c.ticks.count > 1 ? c.ticks[1] - c.ticks[0] : 1102 for (a, b) in zip(t, c.ticks) { #expect(abs(a - b) <= 1e-9 * abs(step), "\(a) vs \(b)") }103 // ticks enclose the data and are exact multiples104 #expect(t.first! <= c.min + 1e-9 * step && t.last! >= c.max - 1e-9 * step)105 }106 // decimal ticks are the nearest doubles, not accumulated sums107 #expect(Ticks.linear(min: 0, max: 0.3, maxIntervals: 3) == [0, 0.1, 0.2, 0.3])108 #expect(Ticks.linear(min: 0, max: 1, maxIntervals: 5, extend: false) == [0, 0.2, 0.4, 0.6, 0.8, 1])109 #expect(Ticks.linear(min: 4, max: 4) == [4])110 #expect(Ticks.linear(min: .nan, max: 1).isEmpty)111 }112113 @Test func logTicksArePowersInsideTheRange() {114 for c in ref.logTicks {115 let t = Ticks.log(min: c.min, max: c.max, base: c.base, maxTicks: 100)116 #expect(t.major.count == c.powers.count)117 for (a, b) in zip(t.major, c.powers) { #expect(close(a, b, rtol: 1e-12)) }118 for m in t.minor { #expect(m >= c.min * (1 - 1e-9) && m <= c.max * (1 + 1e-9)) }119 }120 // thinning keeps a regular stride of decades121 let thin = Ticks.log(min: 1e-12, max: 1e12, maxTicks: 7)122 #expect(thin.major.count <= 7 && thin.major.count >= 2)123 let exps = thin.major.map { Int(log10($0).rounded()) }124 #expect(Set(zip(exps, exps.dropFirst()).map { $1 - $0 }).count == 1)125 // less than a decade: 1-2-5 multiples126 #expect(Ticks.log(min: 1.5, max: 7).major == [2, 5])127 #expect(Ticks.log(min: -1, max: 10).major.isEmpty)128 }129130 @Test func calendarTicksMatchPythonDates() {131 for c in ref.dateTicks {132 let unit = DateTickUnit(rawValue: c.unit)!133 let t = Ticks.dateTicks(c.min, c.max, unit: unit, step: c.step, date1904: false)134 #expect(t == c.ticks, "\(c.unit)/\(c.step): \(t.count) vs \(c.ticks.count)")135 for (v, iso) in zip(t, c.labels) { #expect(AxisFormatter.date(v, code: "yyyy-mm-dd") == iso) }136 }137 for c in ref.dateSerials {138 let p = c.iso.split(separator: "-").map { Int($0)! }139 #expect(DateSerial.serial(year: p[0], month: p[1], day: p[2]) == c.serial)140 }141 // automatic unit selection142 let y = Ticks.date(min: DateSerial.serial(year: 2001, month: 5, day: 1), max: DateSerial.serial(year: 2024, month: 5, day: 1), maxTicks: 8)143 #expect(y.unit == .year && y.step == 5 && y.values.count <= 8)144 let m = Ticks.date(min: DateSerial.serial(year: 2024, month: 1, day: 1), max: DateSerial.serial(year: 2024, month: 7, day: 1), maxTicks: 8)145 #expect(m.unit == .month && m.step == 1 && m.values.count == 7)146 #expect(AxisFormatter.dateLabels(m) == ["Jan 2024", "Feb", "Mar", "Apr", "May", "Jun", "Jul"])147 let h = Ticks.date(min: 45000.25, max: 45000.75, maxTicks: 8)148 #expect(h.unit == .hour && h.step == 2)149 #expect(AxisFormatter.dateLabels(h).first == "15 Mar 06:00")150 let s = Ticks.date(min: 45000, max: 45000 + 20.0 / 86400, maxTicks: 6)151 #expect(s.unit == .second && s.step == 5 && s.values.count == 5)152 // 1904 system153 let d1904 = Ticks.dateTicks(0, 400, unit: .year, step: 1, date1904: true)154 #expect(d1904 == [0, 366])155 }156157 @Test func histogramBinsMatchNumpy() {158 for c in ref.bins {159 let x = ref.datasets[c.data]!160 let rule = BinRule(rawValue: c.rule)!161 let edges = Binning.edges(x, rule: rule, count: 13)162 #expect(edges.count == c.edges.count, "\(c.data)/\(c.rule): \(edges.count - 1) bins vs \(c.edges.count - 1)")163 guard edges.count == c.edges.count else { continue }164 let span = c.edges.last! - c.edges.first!165 for (a, b) in zip(edges, c.edges) { #expect(abs(a - b) <= 1e-10 * span, "\(c.data)/\(c.rule)") }166 let bins = Binning.count(x, edges: edges)167 #expect(bins.counts == c.counts, "\(c.data)/\(c.rule)")168 #expect(bins.total == x.count)169 if !c.density.isEmpty {170 for (a, b) in zip(bins.heights(.density), c.density) { #expect(close(a, b, rtol: 1e-9, atol: 1e-300)) }171 }172 #expect(abs(bins.heights(.probability).reduce(0, +) - 1) < 1e-12)173 }174 // fixed width: edges on multiples of the width, last bin closed175 let fw = Binning.histogram([0.2, 1.0, 1.9, 2.0, 4.0], rule: .fixedWidth, width: 1)176 #expect(fw.edges == [0, 1, 2, 3, 4])177 #expect(fw.counts == [1, 2, 1, 1])178 #expect(Binning.edges([], rule: .auto) == [0, 1])179 #expect(Binning.edges((0..<100_000).map(Double.init), rule: .fixedWidth, width: 1e-3).count <= Binning.maxBins + 2)180 }181182 @Test func quantilesMatchNumpyMethods() {183 for c in ref.quantiles {184 let x = ref.datasets[c.data]!185 let method = QuartileMethod(rawValue: c.method)!186 let scale = (x.map(abs).max() ?? 1)187 for (p, q) in zip(c.p, c.q) {188 let v = ChartStats.quantile(x, p, method: method)189 #expect(abs(v - q) <= 1e-12 * Swift.max(scale, 1e-300) + 1e-12 * abs(q), "\(c.data) \(c.method) p=\(p): \(v) vs \(q)")190 }191 }192 #expect(ChartStats.quantile([], 0.5).isNaN)193 }194195 @Test func boxStatisticsMatchMatplotlib() {196 for c in ref.box {197 let x = ref.datasets[c.data]!198 let s = BoxStats(x, method: .inclusive, whiskers: .tukey)!199 #expect(close(s.q1, c.q1, rtol: 1e-12) && close(s.median, c.median, rtol: 1e-12) && close(s.q3, c.q3, rtol: 1e-12))200 #expect(close(s.whiskerLow, c.whiskerLow, rtol: 1e-12) && close(s.whiskerHigh, c.whiskerHigh, rtol: 1e-12), "\(c.data)")201 #expect(s.outliers.count == c.outliers.count)202 for (a, b) in zip(s.outliers, c.outliers) { #expect(close(a, b, rtol: 1e-12)) }203 #expect(close(s.mean, c.mean, rtol: 1e-12))204 let mm = BoxStats(x, whiskers: .minMax)!205 #expect(mm.outliers.isEmpty && mm.whiskerLow == x.min()! && mm.whiskerHigh == x.max()!)206 }207 #expect(BoxStats([]) == nil)208 let one = BoxStats([7])!209 #expect(one.q1 == 7 && one.q3 == 7 && one.whiskerLow == 7 && one.outliers.isEmpty)210 }211212 @Test func gaussianKDEMatchesScipy() {213 for c in ref.kde {214 let x = ref.datasets[c.data]!215 let rule = BandwidthRule(rawValue: c.rule)!216 let h = KDE.bandwidth(x, rule: rule, fixed: c.bandwidth)!217 #expect(close(h, c.bandwidth, rtol: 1e-12), "\(c.data)/\(c.rule) bandwidth \(h) vs \(c.bandwidth)")218 let d = KDE.density(x, at: c.grid, bandwidth: h)219 let peak = c.density.max()!220 for (a, b) in zip(d, c.density) { #expect(abs(a - b) <= 1e-9 * peak, "\(c.data)/\(c.rule): \(a) vs \(b)") }221 }222 #expect(KDE.bandwidth([1], rule: .scott) == nil)223 #expect(KDE.bandwidth([2, 2, 2], rule: .silverman) == nil)224 // a density integrates to one225 let x = ref.datasets["normal500"]!226 let curve = KDE.curve(x, bandwidth: KDE.bandwidth(x, rule: .silverman)!, points: 2001, cut: 8)227 var area = 0.0228 for i in 1..<curve.x.count { area += (curve.x[i] - curve.x[i - 1]) * (curve.y[i] + curve.y[i - 1]) / 2 }229 #expect(abs(area - 1) < 1e-6)230 }231232 @Test func trendlinesMatchNumpyPolyfit() {233 let wellConditioned: Set<String> = ["linear", "curved", "growth", "power", "logish"]234 for c in ref.trend {235 let ySpan = c.y.max()! - c.y.min()!236 for f in c.fits {237 let kind = TrendlineKind(rawValue: f.kind)!238 guard let fit = Regression.fit(x: c.x, y: c.y, kind: kind, order: f.order) else {239 Issue.record("no fit for \(c.name) \(f.kind) \(f.order)"); continue240 }241 // predictions are compared everywhere; raw coefficients only where numpy itself is well conditioned242 // (numpy.polyfit warns that its own degree-5 fit is poorly conditioned on the un-centred "years" and243 // "bigx" abscissae; the engine centres and scales x, so those two references are not comparable)244 if f.order == 5 && !wellConditioned.contains(c.name) { continue }245 let xs = [c.x[0], c.x[c.x.count / 2], c.x[c.x.count - 1]]246 #expect(abs(fit.r2 - f.r2) < 1e-8, "\(c.name) \(f.kind)/\(f.order) R² \(fit.r2) vs \(f.r2)")247 for (x, expected) in zip(xs, f.probe) { #expect(abs(fit.value(at: x) - expected) <= 1e-7 * ySpan, "\(c.name) \(f.kind)/\(f.order)") }248 if wellConditioned.contains(c.name) && (f.order <= 3) {249 #expect(fit.coefficients.count == f.coefficients.count)250 for (a, b) in zip(fit.coefficients, f.coefficients) { #expect(close(a, b, rtol: 1e-6, atol: 1e-9), "\(c.name) \(f.kind)/\(f.order): \(a) vs \(b)") }251 }252 }253 let ma = Regression.movingAverage(c.y.map { $0 }, period: 5)254 #expect(ma.prefix(4).allSatisfy { $0 == nil })255 for (a, b) in zip(ma.dropFirst(4), c.movingAverage5) { #expect(close(a!, b, rtol: 1e-10, atol: 1e-10)) }256 }257 // exact data, equations, degenerate input258 let line = Regression.fit(x: [1, 2, 3, 4], y: [5, 7, 9, 11], kind: .linear)!259 #expect(abs(line.coefficients[0] - 3) < 1e-12 && abs(line.coefficients[1] - 2) < 1e-12 && abs(line.r2 - 1) < 1e-12)260 #expect(line.equation == "y = 2x + 3")261 let quad = Regression.fit(x: [-2, -1, 0, 1, 2, 3], y: [-2, -1, 0, 1, 2, 3].map { 0.5 * $0 * $0 - 3 * $0 - 1.25 }, kind: .polynomial, order: 2)!262 #expect(quad.equation == "y = 0.5x² − 3x − 1.25")263 #expect(quad.r2Text == "R² = 1.0000")264 #expect(Regression.fit(x: [1, 1, 1], y: [1, 2, 3], kind: .linear) == nil)265 #expect(Regression.fit(x: [1], y: [1], kind: .linear) == nil)266 #expect(Regression.fit(x: [1, 2, 3], y: [-1, -2, -3], kind: .exponential) == nil)267 #expect(Regression.movingAverage([1, nil, 3, 4, 5], period: 2) == [nil, nil, nil, 3.5, 4.5])268 }269270 @Test func stackingMatchesNumpyCumulativeSums() {271 let s = ref.stack272 let input: [[Double?]] = s.values.map { $0.map { Optional($0) } }273 let stacked = Stacking.stack(input, mode: .stacked), pct = Stacking.stack(input, mode: .percent)274 for r in s.values.indices {275 for c in s.values[r].indices {276 #expect(close(stacked[r][c]!.top, s.top[r][c], rtol: 1e-12, atol: 1e-12) && close(stacked[r][c]!.base, s.base[r][c], rtol: 1e-12, atol: 1e-12))277 #expect(close(pct[r][c]!.top, s.percentTop[r][c], rtol: 1e-10, atol: 1e-10) && close(pct[r][c]!.base, s.percentBase[r][c], rtol: 1e-10, atol: 1e-10))278 }279 }280 // positive and negative stacks of a 100 % column span exactly 100 together281 for c in s.values[0].indices {282 let tops = pct.map { $0[c]! }283 let span = tops.map(\.top).max()! - tops.map { Swift.min($0.base, $0.top) }.min()!284 #expect(abs(span - 100) < 1e-9)285 }286 // missing values neither draw nor move the stack287 let gaps = Stacking.stack([[1, nil, 3], [2, 5, nil], [nil, 1, -4]], mode: .stacked)288 #expect(gaps[0][1] == nil && gaps[1][2] == nil && gaps[2][0] == nil)289 #expect(gaps[1][0] == StackSegment(base: 1, top: 3) && gaps[2][1] == StackSegment(base: 5, top: 6) && gaps[2][2] == StackSegment(base: 0, top: -4))290 #expect(Stacking.stack([[0, 0]], mode: .percent)[0][0] == StackSegment(base: 0, top: 0))291 }292293 @Test func marchingSquaresMatchesContourpyAndGeometry() {294 let c = ref.contour295 let z: [[Double?]] = c.z.map { $0.map { Optional($0) } }296 func length(_ lines: [[CGPoint]]) -> Double {297 lines.reduce(0) { acc, l in acc + zip(l, l.dropFirst()).reduce(0) { $0 + Double(hypot($1.1.x - $1.0.x, $1.1.y - $1.0.y)) } }298 }299 for (i, level) in c.levels.enumerated() {300 let lines = MarchingSquares.isolines(z, level: level)301 #expect(abs(length(lines) - c.lengths[i]) <= 0.01 * c.lengths[i], "level \(level): \(length(lines)) vs \(c.lengths[i])")302 #expect(lines.count == c.segments[i], "level \(level): \(lines.count) lines vs \(c.segments[i])")303 }304 // circle: f = x² + y² on a grid, level r²305 let n = 81306 let g = (0..<n).map { -2 + 4 * Double($0) / Double(n - 1) }307 let circle: [[Double?]] = g.map { y in g.map { x in x * x + y * y } }308 let loops = MarchingSquares.isolines(circle, level: 1.44)309 #expect(loops.count == 1)310 let loop = loops[0]311 #expect(loop.first == loop.last)312 for p in loop {313 let x = -2 + 4 * Double(p.x) / Double(n - 1), y = -2 + 4 * Double(p.y) / Double(n - 1)314 #expect(abs((x * x + y * y).squareRoot() - 1.2) < 2e-3)315 }316 #expect(abs(length(loops) * 4 / Double(n - 1) - 2 * .pi * 1.2) < 0.01)317 // filled bands tile the grid exactly: areas sum to the grid area and match the analytic disc318 let bands = MarchingSquares.bands(circle, edges: [0, 1.44, 8])319 func area(_ poly: [CGPoint]) -> Double {320 var a = 0.0321 for i in poly.indices { let p = poly[i], q = poly[(i + 1) % poly.count]; a += Double(p.x * q.y - q.x * p.y) }322 return abs(a) / 2323 }324 let areas = bands.map { $0.reduce(0) { $0 + area($1) } }325 #expect(abs(areas.reduce(0, +) - Double((n - 1) * (n - 1))) < 1e-6)326 let cell = 4.0 / Double(n - 1)327 #expect(abs(areas[0] * cell * cell - .pi * 1.44) < 0.01)328 // missing nodes remove their cells; saddle and empty cases do not crash329 var holed = circle; holed[40][40] = nil330 #expect(MarchingSquares.bands(holed, edges: [0, 8]).first!.reduce(0) { $0 + area($1) } == Double((n - 1) * (n - 1) - 4))331 #expect(MarchingSquares.isolines([[0, 1], [1, 0]], level: 0.5).count == 2)332 #expect(MarchingSquares.isolines([[1]], level: 0).isEmpty)333 }334335 @Test func treemapAreasAreProportionalToValues() {336 var rng = SampleRandom(seed: 99)337 for trial in 0..<20 {338 let n = 1 + trial * 3339 let values: [Double?] = (0..<n).map { i in i % 7 == 6 ? nil : (i % 11 == 10 ? -3 : 1 + 99 * rng.uniform() * rng.uniform()) }340 let rect = CGRect(x: 10, y: 20, width: 300 + 20 * CGFloat(trial), height: 200)341 let rects = Treemap.squarify(values, in: rect)342 let total = values.compactMap { $0 }.filter { $0 > 0 }.reduce(0, +)343 var sum = 0.0344 for (v, r) in zip(values, rects) {345 guard let v, v > 0 else { #expect(r == nil); continue }346 let r = r!347 #expect(abs(Double(r.width * r.height) / Double(rect.width * rect.height) - v / total) < 1e-9)348 #expect(rect.insetBy(dx: -1e-6, dy: -1e-6).contains(r))349 sum += Double(r.width * r.height)350 }351 #expect(abs(sum - Double(rect.width * rect.height)) < 1e-6 * Double(rect.width * rect.height))352 let present = rects.compactMap { $0 }353 for i in present.indices { for j in present.indices where j > i {354 let x = present[i].intersection(present[j])355 #expect(x.isNull || x.width * x.height < 1e-6)356 } }357 }358 // squarified: equal values in a square give squares359 let four = Treemap.squarify([1, 1, 1, 1], in: CGRect(x: 0, y: 0, width: 100, height: 100)).compactMap { $0 }360 #expect(four.allSatisfy { abs($0.width - 50) < 1e-9 && abs($0.height - 50) < 1e-9 })361 #expect(Treemap.squarify([nil, 0, -1], in: CGRect(x: 0, y: 0, width: 10, height: 10)).allSatisfy { $0 == nil })362 }363364 @Test func colorMapsMatchMatplotlib() {365 for (name, samples) in Reference.colormaps {366 let map = ColorMap(ColorMapName(rawValue: name)!)367 // matplotlib samples a 256-entry table without interpolation; the engine interpolates 65 anchors,368 // so a few 8-bit levels of difference are expected where a map changes quickly369 for (i, rgb) in samples.enumerated() {370 let c = map.color(Double(i) / Double(samples.count - 1))371 #expect(abs(Int(c.r) - rgb[0]) <= 4 && abs(Int(c.g) - rgb[1]) <= 4 && abs(Int(c.b) - rgb[2]) <= 4, "\(name) @\(i)")372 }373 }374 #expect(ColorMap(.viridis, reversed: true).color(0) == ColorMap(.viridis).color(1))375 #expect(ColorMap(.viridis).color(.nan).a == 0)376 #expect(ColorMap(.rdBu).color(value: 0, min: -1, max: 1).luminance > 0.8)377 #expect(Set(ColorMapName.allCases).isSubset(of: Set(ColorMapData.anchors.keys)))378 }379380 @Test func decimationPreservesTheEnvelope() {381 var rng = SampleRandom(seed: 5)382 var y = 0.0383 let pts: [CGPoint] = (0..<100_000).map { i in y += rng.normal(); return CGPoint(x: 40 + 560 * CGFloat(i) / 99_999, y: 200 + CGFloat(y) * 0.2 + 30 * CGFloat(rng.normal())) }384 let keep = Decimation.minMaxIndices(pts, bucket: 0.5)385 #expect(keep.count <= 4 * 1121 && keep.count >= 1000)386 #expect(keep == keep.sorted() && Set(keep).count == keep.count && keep.first == 0 && keep.last == pts.count - 1)387 func envelope(_ idx: [Int]) -> [Int: (CGFloat, CGFloat)] {388 var e: [Int: (CGFloat, CGFloat)] = [:]389 for i in idx { let b = Int((pts[i].x / 0.5).rounded(.down)); let v = e[b] ?? (pts[i].y, pts[i].y); e[b] = (Swift.min(v.0, pts[i].y), Swift.max(v.1, pts[i].y)) }390 return e391 }392 let full = envelope(Array(pts.indices)), reduced = envelope(keep)393 #expect(full.count == reduced.count)394 for (b, v) in full { #expect(reduced[b]! == v) }395 #expect(Decimation.minMaxIndices(Array(pts.prefix(100))).count == 100)396 let occ = Decimation.occupancyIndices((0..<50_000).map { CGPoint(x: CGFloat($0 % 100), y: CGFloat($0 % 50)) })397 #expect(occ.count == 100)398 }399400 @Test func monotoneInterpolationNeverOvershoots() {401 let pts = [CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 0), CGPoint(x: 2, y: 10), CGPoint(x: 3, y: 10), CGPoint(x: 4, y: 3), CGPoint(x: 6, y: 3.5)]402 var path = ChartPath()403 Interpolation.addMonotoneCurve(pts, to: &path)404 var prev = pts[0]405 var k = 0406 for e in path.elements {407 guard case .curve(let p, let c1, let c2) = e else { continue }408 let lo = Swift.min(prev.y, p.y) - 1e-9, hi = Swift.max(prev.y, p.y) + 1e-9409 #expect(c1.y >= lo && c1.y <= hi && c2.y >= lo && c2.y <= hi)410 #expect(p == pts[k + 1])411 prev = p; k += 1412 }413 #expect(k == pts.count - 1)414 }415416 @Test func leastSquaresSolver() {417 let x = Regression.leastSquares([[1, 0], [0, 1], [1, 1]], [1, 2, 3])!418 #expect(abs(x[0] - 1) < 1e-12 && abs(x[1] - 2) < 1e-12)419 #expect(Regression.leastSquares([[1, 1], [2, 2], [3, 3]], [1, 2, 3]) == nil)420 }421}422