Repository Public
1import Foundation2import Testing3import AnalyticsTestSupport4@testable import Statistics56private let tsModelFixture = Fixture.load("timeseries", .module)7private func modelSeries(_ name: String) -> [Double] {8 name == "trend_stationary" ? tsModelFixture["adf_series"][name].arr : tsModelFixture["series"][name].arr9}1011@Suite struct TimeSeriesARIMATests {12 @Test func exactLikelihoodAtFixedParameters() throws {13 for f in tsModelFixture["arima_fixed"].list {14 let o = f["order"].ints15 let order = ARIMA.Order(p: o[0], d: o[1], q: o[2])16 let label = "ARIMA(\(o[0]),\(o[1]),\(o[2])) on \(f["series"].s)"17 let prm = ARIMA.Parameters(ar: f["ar"].arr, ma: f["ma"].arr, mean: f["mean"].isNull ? nil : f["mean"].d, sigma2: f["sigma2"].d)18 let y = modelSeries(f["series"].s)19 let fit = try ARIMA.fit(y, order: order, fixed: prm)20 #expect(isClose(fit.logLikelihood, f["loglike"].d, rel: 1e-10), "\(label) loglike \(fit.logLikelihood) vs \(f["loglike"].d)")21 #expect(isClose(try ARIMA.logLikelihood(y, order: order, parameters: prm), f["loglike"].d, rel: 1e-10))22 #expect(isClose(fit.aic, f["aic"].d, rel: 1e-10), "\(label) aic")23 #expect(isClose(fit.bic, f["bic"].d, rel: 1e-10), "\(label) bic")24 #expect(allClose(fit.residuals, f["resid"].arr, rel: 1e-8, abs: 1e-9), "\(label) innovations")25 #expect(fit.nobs == y.count - o[1] && fit.fitted.count == fit.nobs)26 #expect(!fit.estimated && fit.standardErrors.allSatisfy { $0 == nil })27 let fc = try fit.forecast(12)28 if o[1] == 0 {29 #expect(allClose(fc.mean, f["w_forecast"].arr, rel: 1e-9), "\(label) forecast")30 #expect(allClose(fc.standardError, f["w_se"].arr, rel: 1e-9), "\(label) forecast se")31 } else {32 // mean: integrate statsmodels' forecasts of the differenced series (tight)33 var want = f["w_forecast"].arr34 var cur = y35 var lasts = [Double]()36 for _ in 0..<o[1] { lasts.append(cur.last!); cur = TimeSeries.difference(cur) }37 for l in lasts.reversed() { var acc = l; want = want.map { acc += $0; return acc } }38 #expect(allClose(fc.mean, want, rel: 1e-9), "\(label) integrated forecast")39 #expect(isClose(fc.standardError[0], f["w_se"][0].d, rel: 1e-9), "\(label) one-step se")40 // statsmodels' full model starts from an approximate diffuse prior, hence the looser tolerance41 if !f["y_forecast"].isNull {42 #expect(allClose(fc.mean, f["y_forecast"].arr, rel: 1e-4), "\(label) vs full model mean \(maxRelativeError(fc.mean, f["y_forecast"].arr))")43 #expect(allClose(fc.standardError, f["y_se"].arr, rel: 1e-4), "\(label) vs full model se \(maxRelativeError(fc.standardError, f["y_se"].arr))")44 }45 // large-sample ψ-weight standard errors: equal once the filter has reached its steady state46 #expect(allClose(fc.standardError, f["psi_se"].arr, rel: 1e-6), "\(label) psi se \(maxRelativeError(fc.standardError, f["psi_se"].arr))")47 }48 #expect(isClose(fc.upper[4] - fc.mean[4], 1.959963984540054 * fc.standardError[4], rel: 1e-11))49 }50 }5152 @Test func estimatesMatchStatsmodels() throws {53 for f in tsModelFixture["arima_est"].list {54 let o = f["order"].ints55 let label = "ARIMA(\(o[0]),\(o[1]),\(o[2])) on \(f["series"].s)"56 let fit = try ARIMA.fit(modelSeries(f["series"].s), order: .init(p: o[0], d: o[1], q: o[2]), includeMean: f["const"].b)57 #expect(fit.estimated && fit.converged, "\(label)")58 // the likelihood we reach must not be below statsmodels' optimum59 #expect(fit.logLikelihood >= f["loglike"].d - 1e-6, "\(label) loglike \(fit.logLikelihood) vs \(f["loglike"].d)")60 #expect(isClose(fit.logLikelihood, f["loglike"].d, rel: 0, abs: 1e-4), "\(label) loglike")61 let want = f["params"].arr62 #expect(fit.estimates.count == want.count)63 for i in want.indices {64 #expect(isClose(fit.estimates[i], want[i], rel: 2e-3, abs: 2e-3), "\(label) \(fit.parameterNames[i]): \(fit.estimates[i]) vs \(want[i])")65 let se = fit.standardErrors[i] ?? .nan66 #expect(isClose(se, f["bse"][i].d, rel: 0.02), "\(label) se \(fit.parameterNames[i]): \(se) vs \(f["bse"][i].d)")67 }68 #expect(isClose(fit.aic, f["aic"].d, rel: 0, abs: 1e-3))69 #expect(fit.pValues.allSatisfy { ($0 ?? -1) >= 0 && ($0 ?? 2) <= 1 })70 #expect(fit.table().count == 6 + want.count)71 }72 }7374 @Test func recoversSimulatedProcess() throws {75 // AR(1) with φ = 0.7, mean 10, σ = 2, simulated with the package generator76 var g = Xoshiro256StarStar(seed: 4242)77 var x = [Double]()78 var prev = 0.079 for _ in 0..<3000 { prev = 0.7 * prev + 2 * g.nextNormal(); x.append(10 + prev) }80 let fit = try ARIMA.fit(x, order: .init(p: 1, d: 0, q: 0))81 #expect(abs(fit.parameters.ar[0] - 0.7) < 4 * (fit.standardErrors[1] ?? 1))82 #expect(abs((fit.parameters.mean ?? 0) - 10) < 4 * (fit.standardErrors[0] ?? 1))83 #expect(abs(fit.parameters.sigma2 - 4) < 0.4)84 // long-horizon forecast converges to the mean with the stationary standard deviation85 let fc = try fit.forecast(200)86 #expect(isClose(fc.mean[199], fit.parameters.mean!, rel: 1e-8))87 let stationarySD = (fit.parameters.sigma2 / (1 - fit.parameters.ar[0] * fit.parameters.ar[0])).squareRoot()88 #expect(isClose(fc.standardError[199], stationarySD, rel: 1e-8))89 // random walk: forecast = last value, se = σ√h90 let rw = try ARIMA.fit([1, 2, 4, 3, 5, 6, 8, 7, 9, 12, 11, 13], order: .init(p: 0, d: 1, q: 0), fixed: .init(sigma2: 2.25))91 let rf = try rw.forecast(4)92 #expect(rf.mean == [13, 13, 13, 13])93 #expect(allClose(rf.standardError, [1.5, 1.5 * 2.0.squareRoot(), 1.5 * 3.0.squareRoot(), 3], rel: 1e-13))94 }9596 @Test func transformsRoundTripAndStayStationary() {97 let u = [0.3, -1.2, 2.0, 0.05]98 let phi = ARIMA.ARMAKalman.stationaryCoefficients(u)99 let back = ARIMA.ARMAKalman.unconstrained(phi)!100 #expect(allClose(back, u, rel: 1e-10))101 #expect(ARIMA.ARMAKalman(ar: phi, ma: []).stationaryCovariance() != nil)102 #expect(ARIMA.ARMAKalman.unconstrained([1.2]) == nil)103 #expect(ARIMA.ARMAKalman(ar: [1.1], ma: []).stationaryCovariance() == nil)104 // ψ-weights of an ARMA(1,1): ψ_j = φ^{j−1}(φ + θ)105 let psi = ARIMA.ARMAKalman.psiWeights(ar: [0.5], ma: [0.4], count: 5)106 #expect(allClose(psi, [1, 0.9, 0.45, 0.225, 0.1125], rel: 1e-14))107 }108109 @Test func invalidInputs() {110 let y = modelSeries("arma")111 #expect(throws: StatError.self) { try ARIMA.fit(y, order: .init(p: 1, d: 0, q: 0), fixed: .init(ar: [1.05], sigma2: 1)) }112 #expect(throws: StatError.self) { try ARIMA.fit(y, order: .init(p: 1, d: 0, q: 0), fixed: .init(ar: [], sigma2: 1)) }113 #expect(throws: StatError.self) { try ARIMA.fit(y, order: .init(p: 1, d: 0, q: 0), fixed: .init(ar: [0.5], sigma2: -1)) }114 #expect(throws: StatError.self) { try ARIMA.fit([1, 2, 3, 4, 5], order: .init(p: 2, d: 1, q: 1)) }115 #expect(throws: StatError.self) { try ARIMA.fit(y, order: .init(p: 1, d: 4, q: 0)) }116 }117}118119@Suite struct TimeSeriesGARCHTests {120 let f = tsModelFixture["garch"]121122 @Test func likelihoodAndVarianceAtFixedParameters() throws {123 let r = f["returns"].arr124 for (key, mean) in [("constant", GARCH.MeanModel.constant), ("zero", .zero)] {125 for c in f[key]["fixed"].list {126 let p = c["params"].arr127 let prm = GARCH.Parameters(mu: mean == .constant ? p[0] : 0, omega: p[1], alpha: p[2], beta: p[3])128 #expect(isClose(GARCH.logLikelihood(r, parameters: prm, mean: mean), c["loglike"].d, rel: 1e-11), "\(key) loglike")129 let fit = try GARCH.fit(r, mean: mean, fixed: prm)130 #expect(isClose(fit.logLikelihood, c["loglike"].d, rel: 1e-11))131 #expect(allClose(fit.conditionalVariance, c["variance"].arr, rel: 1e-10), "\(key) variance")132 #expect(allClose(fit.forecastVariance(10), c["forecast"].arr, rel: 1e-10), "\(key) forecast")133 #expect(!fit.estimated)134 #expect(isClose(fit.persistence, p[2] + p[3], rel: 1e-15))135 }136 }137 }138139 @Test func estimatesMatchArch() throws {140 let r = f["returns"].arr141 for (key, mean) in [("constant", GARCH.MeanModel.constant), ("zero", .zero)] {142 let fit = try GARCH.fit(r, mean: mean)143 let ref = f[key]144 #expect(fit.estimated && fit.converged)145 #expect(fit.logLikelihood >= ref["fit_loglike"].d - 1e-6, "\(key): \(fit.logLikelihood) vs arch \(ref["fit_loglike"].d)")146 #expect(isClose(fit.logLikelihood, ref["fit_loglike"].d, rel: 0, abs: 1e-4))147 let want = ref["fit_params"].arr148 let got = mean == .constant ? [fit.parameters.mu, fit.parameters.omega, fit.parameters.alpha, fit.parameters.beta]149 : [fit.parameters.omega, fit.parameters.alpha, fit.parameters.beta]150 for i in want.indices { #expect(isClose(got[i], want[i], rel: 5e-3, abs: 1e-4), "\(key) param \(i): \(got[i]) vs \(want[i])") }151 #expect(isClose(fit.aic, ref["aic"].d, rel: 0, abs: 1e-3))152 #expect(isClose(fit.bic, ref["bic"].d, rel: 0, abs: 1e-3))153 #expect(allClose(fit.forecastVariance(10), ref["fit_forecast"].arr, rel: 5e-3))154 #expect(fit.persistence < 1)155 }156 }157158 @Test func derivedQuantities() throws {159 let r = f["returns"].arr160 let fit = try GARCH.fit(r, mean: .zero, fixed: .init(omega: 0.1, alpha: 0.1, beta: 0.8))161 #expect(isClose(fit.unconditionalVariance!, 1.0, rel: 1e-12))162 #expect(isClose(fit.halfLife!, log(0.5) / log(0.9), rel: 1e-14))163 // forecasts converge to the unconditional variance164 #expect(isClose(fit.forecastVariance(400)[399], 1.0, rel: 1e-9))165 #expect(isClose(fit.conditionalVariance[0], 0.1 + 0.9 * fit.backcast, rel: 1e-14))166 #expect(allClose(fit.standardizedResiduals, zip(r, fit.conditionalVolatility).map { $0 / $1 }, rel: 1e-14))167 let igarch = try GARCH.fit(r, mean: .zero, fixed: .init(omega: 0.1, alpha: 0.2, beta: 0.8))168 #expect(igarch.unconditionalVariance == nil && igarch.halfLife == nil)169 #expect(fit.table().count == 11)170 #expect(throws: StatError.self) { try GARCH.fit([1, 2, 3]) }171 #expect(throws: StatError.self) { try GARCH.fit(r, fixed: .init(omega: -1, alpha: 0.1, beta: 0.8)) }172 #expect(throws: StatError.self) { try GARCH.fit([Double](repeating: 1, count: 50)) }173 }174}175176@Suite struct TimeSeriesUnitRootTests {177 @Test func adfMatchesStatsmodels() throws {178 for f in tsModelFixture["adf"].list {179 let reg = UnitRootTest.Regression(rawValue: f["regression"].s)!180 let sel: UnitRootTest.LagSelection181 switch f["select"].s {182 case "fixed": sel = .fixed(f["maxlag"].i)183 case "aic": sel = .aic(maxLag: f["maxlag"].isNull ? nil : f["maxlag"].i)184 default: sel = .bic(maxLag: f["maxlag"].isNull ? nil : f["maxlag"].i)185 }186 let label = "\(f["series"].s) \(reg.rawValue) \(f["select"].s) \(f["maxlag"].isNull ? -1 : f["maxlag"].i)"187 let r = try UnitRootTest.augmentedDickeyFuller(modelSeries(f["series"].s), regression: reg, lags: sel)188 #expect(r.usedLag == f["lag"].i, "\(label) lag \(r.usedLag) vs \(f["lag"].i)")189 #expect(r.nobs == f["nobs"].i, "\(label) nobs")190 #expect(isClose(r.statistic, f["stat"].d, rel: 1e-9), "\(label) stat \(r.statistic) vs \(f["stat"].d)")191 #expect(isClose(r.pValue, f["p"].d, rel: 1e-8, abs: 1e-14), "\(label) p \(r.pValue) vs \(f["p"].d)")192 #expect(allClose([r.critical1, r.critical5, r.critical10], f["crit"].arr, rel: 1e-12), "\(label) crit")193 if !f["ic"].isNull { #expect(isClose(r.informationCriterion ?? .nan, f["ic"].d, rel: 1e-9), "\(label) ic") }194 }195 }196197 @Test func pValueSurfaceAndCriticalValues() {198 #expect(UnitRootTest.mackinnonP(3, .constant) == 1)199 #expect(UnitRootTest.mackinnonP(-20, .constant) == 0)200 #expect(UnitRootTest.mackinnonP(5, .none) < 1)201 // p-value at the asymptotic 5 % critical value is ≈ 0.05202 for reg in UnitRootTest.Regression.allCases {203 let c = UnitRootTest.criticalValues(reg, nobs: nil)204 #expect(abs(UnitRootTest.mackinnonP(c[1], reg) - 0.05) < 0.003, "\(reg)")205 #expect(abs(UnitRootTest.mackinnonP(c[0], reg) - 0.01) < 0.002, "\(reg)")206 #expect(c[0] < c[1] && c[1] < c[2])207 }208 // p is monotone in the statistic209 var last = 0.0210 for s in stride(from: -6.0, through: 2.0, by: 0.25) {211 let p = UnitRootTest.mackinnonP(s, .constant)212 #expect(p >= last)213 last = p214 }215 }216217 @Test func conclusionsAndErrors() throws {218 let stationary = try UnitRootTest.augmentedDickeyFuller(modelSeries("arma"))219 #expect(stationary.pValue < 0.01 && stationary.statistic < stationary.critical1)220 let walk = try UnitRootTest.augmentedDickeyFuller(modelSeries("walk"))221 #expect(walk.pValue > 0.1)222 #expect(walk.table().count == 10)223 #expect(throws: StatError.self) { try UnitRootTest.augmentedDickeyFuller([1, 2, 3]) }224 #expect(throws: StatError.self) { try UnitRootTest.augmentedDickeyFuller([Double](repeating: 2, count: 30)) }225 #expect(throws: StatError.self) { try UnitRootTest.augmentedDickeyFuller(modelSeries("walk"), lags: .fixed(-1)) }226 }227}228