UNPKG

ts-arima-forecast

Version:

TypeScript library for ARIMA, SARIMA (soon), and SARIMAX (soon) forecasting

90 lines 3.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Diagnostics = void 0; const statistics_1 = __importDefault(require("./statistics")); class Diagnostics { static ljungBox(residuals, lags = 10) { const n = residuals.length; const autocorrs = []; for (let lag = 1; lag <= lags; lag++) { autocorrs.push(statistics_1.default.autocorrelation(residuals, lag)); } let statistic = 0; for (let i = 0; i < lags; i++) { statistic += Math.pow(autocorrs[i], 2) / (n - i - 1); } statistic *= n * (n + 2); const pValue = this.chiSquarePValue(statistic, lags); return { statistic, pValue }; } static jarqueBera(residuals) { const n = residuals.length; const mean = statistics_1.default.mean(residuals); const std = statistics_1.default.standardDeviation(residuals); let skewness = 0; for (const r of residuals) { skewness += Math.pow((r - mean) / std, 3); } skewness /= n; let kurtosis = 0; for (const r of residuals) { kurtosis += Math.pow((r - mean) / std, 4); } kurtosis = kurtosis / n - 3; const statistic = n / 6 * (Math.pow(skewness, 2) + Math.pow(kurtosis, 2) / 4); const pValue = this.chiSquarePValue(statistic, 2); return { statistic, pValue }; } static chiSquarePValue(statistic, degreesOfFreedom) { function gammaIncompleteUpper(s, x) { let sum = 1, term = 1; for (let k = 1; k < 100; k++) { term *= x / (s + k); sum += term; if (term < 1e-10) break; } return Math.exp(-x + s * Math.log(x) - logGamma(s)) * sum; } function logGamma(z) { const g = 7; // lanczos approximation const p = [ 0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 ]; if (z < 0.5) return Math.log(Math.PI) - Math.log(Math.sin(Math.PI * z)) - logGamma(1 - z); z -= 1; let x = p[0]; for (let i = 1; i < g + 2; i++) x += p[i] / (z + i); const t = z + g + 0.5; return 0.5 * Math.log(2 * Math.PI) + (z + 0.5) * Math.log(t) - t + Math.log(x) - Math.log(z + 1); } const x = statistic / 2; const s = degreesOfFreedom / 2; return gammaIncompleteUpper(s, x); } static plotResiduals(residuals) { const mean = statistics_1.default.mean(residuals); const variance = statistics_1.default.variance(residuals); const autocorrelations = []; for (let lag = 1; lag <= Math.min(20, Math.floor(residuals.length / 4)); lag++) { autocorrelations.push(statistics_1.default.autocorrelation(residuals, lag)); } return { mean, variance, autocorrelations, ljungBox: this.ljungBox(residuals), jarqueBera: this.jarqueBera(residuals) }; } } exports.Diagnostics = Diagnostics; //# sourceMappingURL=diagnostics.js.map