@numero-uno-jpn/rstats 1.18.0 → 1.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/README.ja.md +7 -7
- package/README.md +7 -7
- package/dist/causal.js +2 -2
- package/dist/chunks/chunk-33ODAPMR.js +2 -0
- package/dist/chunks/chunk-3ST6RZXG.js +2 -0
- package/dist/chunks/{chunk-JF4PXFJE.js → chunk-4EQN74JN.js} +2 -2
- package/dist/chunks/chunk-5RRFO4NU.js +2 -0
- package/dist/chunks/{chunk-3JFRDXN7.js → chunk-64DDDGJU.js} +2 -2
- package/dist/chunks/chunk-66TEOHPF.js +2 -0
- package/dist/chunks/chunk-6ISDROPG.js +2 -0
- package/dist/chunks/{chunk-EC77KAAM.js → chunk-6LAKI2QG.js} +2 -2
- package/dist/chunks/{chunk-5CTWYFMB.js → chunk-6QCH4LAL.js} +2 -2
- package/dist/chunks/{chunk-H6Z7JVSD.js → chunk-6RA73LXX.js} +2 -2
- package/dist/chunks/{chunk-KJMLQHCY.js → chunk-7C2ECGT6.js} +1 -1
- package/dist/chunks/{chunk-SCFFHEDZ.js → chunk-7KPFNJBE.js} +2 -2
- package/dist/chunks/chunk-ARPXG2PG.js +2 -0
- package/dist/chunks/chunk-AWXW4WSX.js +2 -0
- package/dist/chunks/{chunk-XQCHC3SH.js → chunk-B5GSH3AK.js} +1 -1
- package/dist/chunks/{chunk-2JB7F646.js → chunk-BLDLJHUG.js} +2 -2
- package/dist/chunks/{chunk-IKMW63BE.js → chunk-BU4I544S.js} +2 -2
- package/dist/chunks/{chunk-LCIX76FR.js → chunk-CAUNJ7S6.js} +2 -2
- package/dist/chunks/chunk-DFGW4QWU.js +2 -0
- package/dist/chunks/{chunk-WYVUCQO3.js → chunk-DPCCSFCG.js} +1 -1
- package/dist/chunks/chunk-EXS5QD7V.js +2 -0
- package/dist/chunks/chunk-FOCHNNP3.js +2 -0
- package/dist/chunks/{chunk-MXZ64QME.js → chunk-HEPGGURG.js} +2 -2
- package/dist/chunks/chunk-INNFAVT3.js +2 -0
- package/dist/chunks/chunk-JEELIABM.js +2 -0
- package/dist/chunks/chunk-KNF2GPJV.js +2 -0
- package/dist/chunks/chunk-L4ZS7MAX.js +2 -0
- package/dist/chunks/{chunk-RXYZQ3UX.js → chunk-LZAB4M7P.js} +2 -2
- package/dist/chunks/chunk-LZTDYQRS.js +3 -0
- package/dist/chunks/{chunk-N7BUE54L.js → chunk-MTSU2QHB.js} +2 -2
- package/dist/chunks/{chunk-GIK34P23.js → chunk-MXRN2G7O.js} +2 -2
- package/dist/chunks/chunk-OFDD4GTZ.js +2 -0
- package/dist/chunks/chunk-OOHFFLMY.js +2 -0
- package/dist/chunks/chunk-OOO5AAM7.js +2 -0
- package/dist/chunks/chunk-PJJJR3H6.js +2 -0
- package/dist/chunks/chunk-PVRNAOO7.js +2 -0
- package/dist/chunks/chunk-Q2JLU5YD.js +2 -0
- package/dist/chunks/{chunk-Z33Q545U.js → chunk-QCQBK5AC.js} +2 -2
- package/dist/chunks/chunk-RXK4KSKH.js +2 -0
- package/dist/chunks/chunk-UC3YYJHM.js +4 -0
- package/dist/chunks/chunk-UPACHZ4F.js +2 -0
- package/dist/chunks/{chunk-VATAUY7N.js → chunk-VCXPGCA4.js} +2 -2
- package/dist/chunks/chunk-VFL4GGYT.js +2 -0
- package/dist/chunks/chunk-VL72MQOQ.js +2 -0
- package/dist/chunks/chunk-VSV5QGXX.js +2 -0
- package/dist/chunks/{chunk-CTZUYKVL.js → chunk-WMEF5XWO.js} +2 -2
- package/dist/chunks/chunk-X6JS6ZZB.js +2 -0
- package/dist/chunks/chunk-YEDVOX5V.js +2 -0
- package/dist/chunks/chunk-ZCTB4N5J.js +3 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions/normal.d.ts +4 -0
- package/dist/distributions/normal.js +4 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +6 -0
- package/dist/entries/tests.js +6 -0
- package/dist/entries/timeseries.d.ts +11 -0
- package/dist/entries/timeseries.js +11 -0
- package/dist/htest/timeROC.d.ts +564 -0
- package/dist/htest/timeROC.js +1106 -0
- package/dist/linalg.js +2 -2
- package/dist/meta/metabin.d.ts +32 -0
- package/dist/meta/metabin.js +137 -0
- package/dist/meta/metaextra.d.ts +153 -0
- package/dist/meta/metaextra.js +290 -0
- package/dist/meta/metagen.d.ts +124 -0
- package/dist/meta/metagen.js +263 -0
- package/dist/meta/metaother.d.ts +87 -0
- package/dist/meta/metaother.js +342 -0
- package/dist/meta/netmeta.d.ts +256 -0
- package/dist/meta/netmeta.js +547 -0
- package/dist/mixed.js +3 -4
- package/dist/models/glmFamily.js +4 -1
- package/dist/models/metaAnalysis.d.ts +4 -0
- package/dist/models/metaAnalysis.js +100 -18
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +20 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries/acf.js +5 -2
- package/dist/timeseries/arima.d.ts +154 -0
- package/dist/timeseries/arima.js +1007 -0
- package/dist/timeseries/autoArima.d.ts +70 -0
- package/dist/timeseries/autoArima.js +191 -0
- package/dist/timeseries/ets.d.ts +158 -0
- package/dist/timeseries/ets.js +883 -0
- package/dist/timeseries/forecastArima.d.ts +136 -0
- package/dist/timeseries/forecastArima.js +432 -0
- package/dist/timeseries/forecastDispatch.d.ts +83 -0
- package/dist/timeseries/forecastDispatch.js +130 -0
- package/dist/timeseries/forecastSimple.d.ts +177 -0
- package/dist/timeseries/forecastSimple.js +371 -0
- package/dist/timeseries/forecastUtils.d.ts +260 -0
- package/dist/timeseries/forecastUtils.js +619 -0
- package/dist/timeseries/holtWinters.d.ts +65 -0
- package/dist/timeseries/holtWinters.js +193 -0
- package/dist/timeseries/input.d.ts +8 -0
- package/dist/timeseries/input.js +27 -0
- package/dist/timeseries/ocsbCritical.d.ts +2 -0
- package/dist/timeseries/ocsbCritical.js +6 -0
- package/dist/timeseries/scale.d.ts +16 -0
- package/dist/timeseries/scale.js +48 -0
- package/dist/timeseries/statsTs.d.ts +128 -0
- package/dist/timeseries/statsTs.js +456 -0
- package/dist/timeseries/stl.d.ts +131 -0
- package/dist/timeseries/stl.js +489 -0
- package/dist/timeseries/tseries.d.ts +110 -0
- package/dist/timeseries/tseries.js +369 -0
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-53WQDB3C.js +0 -2
- package/dist/chunks/chunk-6I23C2YJ.js +0 -2
- package/dist/chunks/chunk-FLFNMSHI.js +0 -3
- package/dist/chunks/chunk-GEHMND3C.js +0 -2
- package/dist/chunks/chunk-HG4LJBNV.js +0 -2
- package/dist/chunks/chunk-HGAEVPVS.js +0 -2
- package/dist/chunks/chunk-HMLYEFQN.js +0 -2
- package/dist/chunks/chunk-HRRCC2BD.js +0 -2
- package/dist/chunks/chunk-IBHMM2SL.js +0 -2
- package/dist/chunks/chunk-ICDW3I2F.js +0 -2
- package/dist/chunks/chunk-IOJVFYHW.js +0 -2
- package/dist/chunks/chunk-IW4S634O.js +0 -2
- package/dist/chunks/chunk-JPWYGTUN.js +0 -2
- package/dist/chunks/chunk-KVJBCT3D.js +0 -2
- package/dist/chunks/chunk-LY5XGFPU.js +0 -2
- package/dist/chunks/chunk-MKZH65DK.js +0 -2
- package/dist/chunks/chunk-MVPHISOV.js +0 -2
- package/dist/chunks/chunk-NDZVVNBB.js +0 -2
- package/dist/chunks/chunk-NOTJ45G3.js +0 -4
- package/dist/chunks/chunk-QUD3374N.js +0 -2
- package/dist/chunks/chunk-V6FMGGUV.js +0 -2
- package/dist/chunks/chunk-W5M3R4NH.js +0 -2
- package/dist/chunks/chunk-XUMVTGMM.js +0 -2
- package/dist/chunks/chunk-YXMHWS5R.js +0 -2
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// tseries' tests: adf.test, kpss.test, pp.test, po.test, jarque.bera.test, runs.test, white.test and terasvirta.test.
|
|
2
|
+
// Written for rStats.js from the behaviour specification oracle/tsmeta-behaviour.md ("tseries") with the details found
|
|
3
|
+
// in experiments/tsmeta120-log.md (items 6-9, 14-15, 17-18, 94). The p-value tables are Fuller's (1976) Tables 8.5.1
|
|
4
|
+
// and 8.5.2 and Phillips and Ouliaris' (1990) Tables Ia and Ib, as published (log 18, 94). No R or package source was
|
|
5
|
+
// read.
|
|
6
|
+
import { pchisq } from "../distributions/chisq.js";
|
|
7
|
+
import { pf } from "../distributions/f.js";
|
|
8
|
+
import { pnorm } from "../distributions/normal.js";
|
|
9
|
+
import { prcomp } from "../linalg/lu.js";
|
|
10
|
+
import { lm } from "../models/lm.js";
|
|
11
|
+
import { runif } from "../random/continuous.js";
|
|
12
|
+
import { setSeed } from "../random/rng.js";
|
|
13
|
+
import { kpssPValue, kpssStatistic } from "./forecastArima.js";
|
|
14
|
+
import { centred, powerOfTwoScale, shiftReference, standardise } from "./scale.js";
|
|
15
|
+
import { tsInput } from "./input.js";
|
|
16
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
17
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
18
|
+
const ADF_TEST_KEYS = ["alternative", "k"];
|
|
19
|
+
const KPSS_TEST_KEYS = ["null", "lshort"];
|
|
20
|
+
const PP_TEST_KEYS = ["alternative", "type", "lshort"];
|
|
21
|
+
const PO_TEST_KEYS = ["demean", "lshort"];
|
|
22
|
+
const RUNS_TEST_KEYS = ["alternative"];
|
|
23
|
+
const TERASVIRTA_TEST_KEYS = ["lag", "type", "scale"];
|
|
24
|
+
const WHITE_TEST_KEYS = ["lag", "qstar", "q", "range", "type", "scale", "seed"];
|
|
25
|
+
const approx = (xs, ys, x) => {
|
|
26
|
+
if (x <= xs[0])
|
|
27
|
+
return ys[0];
|
|
28
|
+
if (x >= xs[xs.length - 1])
|
|
29
|
+
return ys[ys.length - 1];
|
|
30
|
+
let i = 0;
|
|
31
|
+
while (xs[i + 1] < x)
|
|
32
|
+
i++;
|
|
33
|
+
return ys[i] + ((ys[i + 1] - ys[i]) * (x - xs[i])) / (xs[i + 1] - xs[i]);
|
|
34
|
+
};
|
|
35
|
+
// Fuller (1976), with a trend: rows n = 25, 50, 100, 250, 500, infinity (coded 100000, log 94)
|
|
36
|
+
const FULLER_N = [25, 50, 100, 250, 500, 100000];
|
|
37
|
+
const FULLER_P = [0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99];
|
|
38
|
+
const TAU_TAU = [[-4.38, -3.95, -3.6, -3.24, -1.14, -0.8, -0.5, -0.15], [-4.15, -3.8, -3.5, -3.18, -1.19, -0.87, -0.58, -0.24], [-4.04, -3.73, -3.45, -3.15, -1.22, -0.9, -0.62, -0.28],
|
|
39
|
+
[-3.99, -3.69, -3.43, -3.13, -1.23, -0.92, -0.64, -0.31], [-3.98, -3.68, -3.42, -3.13, -1.24, -0.93, -0.65, -0.32], [-3.96, -3.66, -3.41, -3.12, -1.25, -0.94, -0.66, -0.33]];
|
|
40
|
+
const RHO_TAU = [[-22.5, -19.9, -17.9, -15.6, -3.66, -2.51, -1.53, -0.43], [-25.7, -22.4, -19.8, -16.8, -3.71, -2.6, -1.66, -0.65], [-27.4, -23.6, -20.7, -17.5, -3.74, -2.62, -1.73, -0.75],
|
|
41
|
+
[-28.4, -24.4, -21.3, -18, -3.75, -2.64, -1.78, -0.82], [-28.9, -24.8, -21.5, -18.1, -3.76, -2.65, -1.78, -0.84], [-29.5, -25.1, -21.8, -18.3, -3.77, -2.66, -1.79, -0.87]];
|
|
42
|
+
/** The table interpolated over n at N, then over the statistic (clamped to the table's p range). */
|
|
43
|
+
function fullerP(table, N, stat) {
|
|
44
|
+
const crit = FULLER_P.map((_, j) => approx(FULLER_N, table.map((r) => r[j]), N));
|
|
45
|
+
return approx(crit, FULLER_P, stat);
|
|
46
|
+
}
|
|
47
|
+
/** The Bartlett-weighted long-run variance of e (sums over m) with truncation lag l. */
|
|
48
|
+
function neweyWest(e, l, m = e.length) {
|
|
49
|
+
let v = e.reduce((a, x) => a + x * x, 0) / m;
|
|
50
|
+
for (let s = 1; s <= l; s++) {
|
|
51
|
+
let c = 0;
|
|
52
|
+
for (let t = s; t < e.length; t++)
|
|
53
|
+
c += e[t] * e[t - s];
|
|
54
|
+
v += (2 * (1 - s / (l + 1)) * c) / m;
|
|
55
|
+
}
|
|
56
|
+
return v;
|
|
57
|
+
}
|
|
58
|
+
const finite = (x, what) => {
|
|
59
|
+
const y = x.map(Number);
|
|
60
|
+
if (y.some((v) => !Number.isFinite(v)))
|
|
61
|
+
throw new RangeError(`${what}: x must not contain missing or infinite values`);
|
|
62
|
+
return y;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* The augmented Dickey-Fuller test (tseries' `adf.test()`): Delta y_t on 1, t, y_{t-1} and k lagged differences
|
|
66
|
+
* (default k = trunc((n - 1)^(1/3))), the t value of y_{t-1}; p from Fuller's tau_tau table at n - 1 (clamped to
|
|
67
|
+
* [0.01, 0.99]); "explosive" gives 1 - p.
|
|
68
|
+
* Equivalent to `tseries::adf.test()` in R.
|
|
69
|
+
*
|
|
70
|
+
* @category Time series
|
|
71
|
+
*/
|
|
72
|
+
export function adfTest(x, options = {}) {
|
|
73
|
+
checkOptions(options, ADF_TEST_KEYS, "adfTest"); // generated: option-keys.mjs
|
|
74
|
+
const y = standardise(centred(finite(x, "adfTest")));
|
|
75
|
+
const k = options.k ?? Math.trunc((y.length - 1) ** (1 / 3));
|
|
76
|
+
if (!(Number.isInteger(k) && k >= 0))
|
|
77
|
+
throw new RangeError("adfTest: k must be a non-negative integer");
|
|
78
|
+
const d = y.slice(1).map((v, i) => v - y[i]);
|
|
79
|
+
const n = d.length;
|
|
80
|
+
if (n - k < 4)
|
|
81
|
+
throw new RangeError("adfTest: the series is too short for k lags");
|
|
82
|
+
const rows = [], resp = [];
|
|
83
|
+
for (let t = k; t < n; t++) {
|
|
84
|
+
rows.push([y[t], t + 1, ...Array.from({ length: k }, (_, i) => d[t - 1 - i])]);
|
|
85
|
+
resp.push(d[t]);
|
|
86
|
+
}
|
|
87
|
+
const statistic = lm(rows, resp, {}).coefTable.tValue[1];
|
|
88
|
+
let p = fullerP(TAU_TAU, n, statistic);
|
|
89
|
+
if (options.alternative === "explosive")
|
|
90
|
+
p = 1 - p;
|
|
91
|
+
return { statistic, parameter: k, pValue: p, method: "Augmented Dickey-Fuller Test" };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The KPSS statistic with missing values as tseries' (log 108): the residuals of the observed values (the trend at
|
|
95
|
+
* their positions), the missing ones left out, and n the length of the series including them (in n^2 and in the
|
|
96
|
+
* long-run variance; equal to 1e-15).
|
|
97
|
+
*/
|
|
98
|
+
function kpssMissing(y, type, lag) {
|
|
99
|
+
const n = y.length;
|
|
100
|
+
const t = y.map((_, i) => i).filter((i) => !Number.isNaN(y[i]));
|
|
101
|
+
const v0 = t.map((i) => y[i]);
|
|
102
|
+
const e = type === "level" ? (() => { const m = v0.reduce((a, b) => a + b, 0) / v0.length; return v0.map((v) => v - m); })()
|
|
103
|
+
: lm(t.map((i) => [i + 1]), v0, {}).residuals;
|
|
104
|
+
let v = e.reduce((a, b) => a + b * b, 0) / n;
|
|
105
|
+
for (let s = 1; s <= lag; s++) {
|
|
106
|
+
let c = 0;
|
|
107
|
+
for (let k = s; k < e.length; k++)
|
|
108
|
+
c += e[k] * e[k - s];
|
|
109
|
+
v += (2 * (1 - s / (lag + 1)) * c) / n;
|
|
110
|
+
}
|
|
111
|
+
let cum = 0, sum = 0;
|
|
112
|
+
for (const ei of e) {
|
|
113
|
+
cum += ei;
|
|
114
|
+
sum += cum * cum;
|
|
115
|
+
}
|
|
116
|
+
return sum / (n * n * v);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The KPSS test (tseries' `kpss.test()`): the null "Level" or "Trend", truncation lag trunc(4 (n/100)^0.25) (`lshort`)
|
|
120
|
+
* or trunc(12 (n/100)^0.25); p interpolated in the KPSS table (clamped to [0.01, 0.10]).
|
|
121
|
+
* Equivalent to `tseries::kpss.test()` in R.
|
|
122
|
+
*
|
|
123
|
+
* @category Time series
|
|
124
|
+
*/
|
|
125
|
+
export function kpssTest(x, options = {}) {
|
|
126
|
+
checkOptions(options, KPSS_TEST_KEYS, "kpssTest"); // generated: option-keys.mjs
|
|
127
|
+
const y = standardise(centred(tsInput(x, "kpssTest", { missing: "allow", minLength: 2 })));
|
|
128
|
+
const n = y.length;
|
|
129
|
+
const type = options.null === "Trend" ? "trend" : "level";
|
|
130
|
+
const l = Math.trunc((options.lshort ?? true ? 4 : 12) * (n / 100) ** 0.25);
|
|
131
|
+
const statistic = y.some((v) => Number.isNaN(v)) ? kpssMissing(y, type, l) : kpssStatistic(y, type, l);
|
|
132
|
+
return { statistic, parameter: l, pValue: kpssPValue(statistic, type), method: `KPSS Test for ${type === "trend" ? "Trend" : "Level"} Stationarity` };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The Phillips-Perron test (tseries' `pp.test()`): y_t on 1, t, y_{t-1}; Phillips and Perron's closed forms with
|
|
136
|
+
* det(X'X) (log 8), type "Z(alpha)" (default, as R) or "Z(t_alpha)", truncation lag trunc(4 (n/100)^0.25) (`lshort`)
|
|
137
|
+
* or trunc(12 (n/100)^0.25); p from Fuller's n(rho - 1) or tau_tau table at n - 1; "explosive" gives 1 - p.
|
|
138
|
+
* Equivalent to `tseries::pp.test()` in R.
|
|
139
|
+
*
|
|
140
|
+
* @category Time series
|
|
141
|
+
*/
|
|
142
|
+
export function ppTest(x, options = {}) {
|
|
143
|
+
checkOptions(options, PP_TEST_KEYS, "ppTest"); // generated: option-keys.mjs
|
|
144
|
+
const y = standardise(centred(finite(x, "ppTest")));
|
|
145
|
+
const n = y.length;
|
|
146
|
+
const nn = n - 1;
|
|
147
|
+
const l = Math.trunc((options.lshort ?? true ? 4 : 12) * (n / 100) ** 0.25);
|
|
148
|
+
const rows = [], resp = [];
|
|
149
|
+
for (let t = 1; t < n; t++) {
|
|
150
|
+
rows.push([t, y[t - 1]]);
|
|
151
|
+
resp.push(y[t]);
|
|
152
|
+
}
|
|
153
|
+
const f = lm(rows, resp, {});
|
|
154
|
+
const rho = f.coefficients[2];
|
|
155
|
+
const tr = (rho - 1) / f.coefTable.stdError[2];
|
|
156
|
+
const u = f.residuals;
|
|
157
|
+
const su2 = u.reduce((a, b) => a + b * b, 0) / nn;
|
|
158
|
+
const lam = neweyWest(u, l, nn);
|
|
159
|
+
let s00 = 0, s01 = 0, s02 = 0, s11 = 0, s12 = 0, s22 = 0;
|
|
160
|
+
for (let t = 1; t < n; t++) {
|
|
161
|
+
const a = t, b = y[t - 1];
|
|
162
|
+
s00 += 1;
|
|
163
|
+
s01 += a;
|
|
164
|
+
s02 += b;
|
|
165
|
+
s11 += a * a;
|
|
166
|
+
s12 += a * b;
|
|
167
|
+
s22 += b * b;
|
|
168
|
+
}
|
|
169
|
+
const Dx = s00 * (s11 * s22 - s12 * s12) - s01 * (s01 * s22 - s12 * s02) + s02 * (s01 * s12 - s11 * s02);
|
|
170
|
+
const type = options.type ?? "Z(alpha)";
|
|
171
|
+
const statistic = type === "Z(alpha)"
|
|
172
|
+
? nn * (rho - 1) - (nn ** 6 / (24 * Dx)) * (lam - su2)
|
|
173
|
+
: Math.sqrt(su2 / lam) * tr - ((lam - su2) * nn ** 3) / (4 * Math.sqrt(3) * Math.sqrt(Dx) * Math.sqrt(lam));
|
|
174
|
+
// too short a series (n = 3: the regression on 1, t, y_{t-1} has no residual degree of freedom): an error, as R's (log 109)
|
|
175
|
+
if (!Number.isFinite(statistic))
|
|
176
|
+
throw new RangeError(`ppTest: the series is too short (${n} values)`);
|
|
177
|
+
let p = fullerP(type === "Z(alpha)" ? RHO_TAU : TAU_TAU, nn, statistic);
|
|
178
|
+
if (options.alternative === "explosive")
|
|
179
|
+
p = 1 - p;
|
|
180
|
+
return { statistic, parameter: l, pValue: p, method: "Phillips-Perron Unit Root Test" };
|
|
181
|
+
}
|
|
182
|
+
// Phillips and Ouliaris (1990), Table Ib (demeaned) and Ia, rows: 2..6 variables (log 18)
|
|
183
|
+
const PO_P = [0.01, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15];
|
|
184
|
+
const PO_TABLE = {
|
|
185
|
+
demean: [[-28.32, -23.81, -20.49, -18.48, -17.04, -15.93, -14.91], [-34.17, -29.74, -26.09, -23.87, -22.19, -21.04, -19.95], [-41.13, -35.71, -32.06, -29.51, -27.58, -26.23, -25.05],
|
|
186
|
+
[-47.51, -41.64, -37.15, -34.71, -32.74, -31.15, -29.88], [-52.17, -46.53, -41.94, -39.11, -37.01, -35.48, -34.2]],
|
|
187
|
+
plain: [[-22.83, -18.89, -15.64, -13.81, -12.54, -11.57, -10.74], [-29.27, -25.21, -21.48, -19.61, -18.18, -17.01, -16.02], [-36.16, -31.54, -27.85, -25.52, -23.92, -22.62, -21.53],
|
|
188
|
+
[-42.87, -37.48, -33.48, -30.93, -28.85, -27.4, -26.17], [-48.52, -42.55, -38.09, -35.51, -33.8, -32.27, -30.9]],
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* The Phillips-Ouliaris cointegration test (tseries' `po.test()`): x has 2 to 6 columns (variables) as rows of
|
|
192
|
+
* observations; u = the residuals of the first column on the others (with an intercept when `demean`, default),
|
|
193
|
+
* Z = (n - 1)(rho - 1) - (lambda^2 - s^2) / (2 sum u_{t-1}^2 / (n - 1)^2) from u_t on u_{t-1}; lag trunc((n - 1)/100)
|
|
194
|
+
* (`lshort`) or trunc((n - 1)/30); p from Table Ib / Ia (clamped to [0.01, 0.15]).
|
|
195
|
+
* Equivalent to `tseries::po.test()` in R.
|
|
196
|
+
*
|
|
197
|
+
* @category Time series
|
|
198
|
+
*/
|
|
199
|
+
export function poTest(x, options = {}) {
|
|
200
|
+
checkOptions(options, PO_TEST_KEYS, "poTest"); // generated: option-keys.mjs
|
|
201
|
+
const X1 = x.map((r) => finite(r, "poTest"));
|
|
202
|
+
const n = X1.length;
|
|
203
|
+
const k = X1[0]?.length ?? 0;
|
|
204
|
+
if (k < 2 || k > 6)
|
|
205
|
+
throw new RangeError("poTest: x must have 2 to 6 columns");
|
|
206
|
+
// with demean, each column less a large common value (log 110; the statistic does not depend on it)
|
|
207
|
+
const refs = Array.from({ length: k }, (_, j) => ((options.demean ?? true) ? shiftReference(X1.map((r) => r[j])) : 0));
|
|
208
|
+
const X0 = X1.map((r) => r.map((v, j) => v - refs[j]));
|
|
209
|
+
// each column divided by a power of two (exact; the statistic does not depend on the columns' scales)
|
|
210
|
+
const cs = Array.from({ length: k }, (_, j) => powerOfTwoScale(X0.map((r) => r[j])));
|
|
211
|
+
const X = X0.map((r) => r.map((v, j) => v / cs[j]));
|
|
212
|
+
const demean = options.demean ?? true;
|
|
213
|
+
const nn = n - 1;
|
|
214
|
+
const l = Math.trunc((nn) / (options.lshort ?? true ? 100 : 30));
|
|
215
|
+
const u = lm(X.map((r) => r.slice(1)), X.map((r) => r[0]), { intercept: demean }).residuals;
|
|
216
|
+
// collinear columns (an exact cointegrating relation): the residuals are rounding noise and the statistic with them
|
|
217
|
+
// (R's -222 against rStats.js's -217 for (x, 2x)); an error (log 109)
|
|
218
|
+
const ssy = X.reduce((a, r) => a + r[0] * r[0], 0);
|
|
219
|
+
if (!(u.reduce((a, v) => a + v * v, 0) > 1e-24 * ssy))
|
|
220
|
+
throw new RangeError("poTest: the columns are collinear (the cointegrating regression fits exactly)");
|
|
221
|
+
const g = lm(u.slice(0, n - 1).map((v) => [v]), u.slice(1), { intercept: false });
|
|
222
|
+
const rho = g.coefficients[0];
|
|
223
|
+
const e = g.residuals;
|
|
224
|
+
const su2 = e.reduce((a, b) => a + b * b, 0) / nn;
|
|
225
|
+
const lam = neweyWest(e, l, nn);
|
|
226
|
+
const sxx = u.slice(0, n - 1).reduce((a, b) => a + b * b, 0);
|
|
227
|
+
const statistic = nn * (rho - 1) - (lam - su2) / ((2 * sxx) / (nn * nn));
|
|
228
|
+
const pValue = approx(PO_TABLE[demean ? "demean" : "plain"][k - 2], PO_P, statistic);
|
|
229
|
+
return { statistic, parameter: l, pValue, method: "Phillips-Ouliaris Cointegration Test" };
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The Jarque-Bera test for normality (tseries' `jarque.bera.test()`): n (S^2/6 + (K - 3)^2/24) with the moments over n,
|
|
233
|
+
* chi-squared with 2 df.
|
|
234
|
+
* Equivalent to `tseries::jarque.bera.test()` in R.
|
|
235
|
+
*
|
|
236
|
+
* @category Time series
|
|
237
|
+
*/
|
|
238
|
+
export function jarqueBeraTest(x) {
|
|
239
|
+
const y = standardise(centred(finite(x, "jarqueBeraTest")));
|
|
240
|
+
const n = y.length;
|
|
241
|
+
const m = y.reduce((a, b) => a + b, 0) / n;
|
|
242
|
+
const c = y.map((v) => v - m);
|
|
243
|
+
const m2 = c.reduce((a, v) => a + v * v, 0) / n;
|
|
244
|
+
const S = c.reduce((a, v) => a + v ** 3, 0) / n / m2 ** 1.5;
|
|
245
|
+
const K = c.reduce((a, v) => a + v ** 4, 0) / n / m2 ** 2;
|
|
246
|
+
const statistic = n * (S * S / 6 + (K - 3) ** 2 / 24);
|
|
247
|
+
return { statistic, parameter: 2, pValue: pchisq(statistic, 2, { lowerTail: false }), method: "Jarque Bera Test" };
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* The Wald-Wolfowitz runs test (tseries' `runs.test()`) of a two-valued sequence: z = (R - mean) / sd with mean 1 +
|
|
251
|
+
* 2 n1 n2 / n and variance 2 n1 n2 (2 n1 n2 - n) / (n^2 (n - 1)), normal p for "two.sided" (default), "less" or
|
|
252
|
+
* "greater".
|
|
253
|
+
* Equivalent to `tseries::runs.test()` in R.
|
|
254
|
+
*
|
|
255
|
+
* @category Time series
|
|
256
|
+
*/
|
|
257
|
+
export function runsTest(x, options = {}) {
|
|
258
|
+
checkOptions(options, RUNS_TEST_KEYS, "runsTest"); // generated: option-keys.mjs
|
|
259
|
+
const levels = [...new Set(x)];
|
|
260
|
+
if (levels.length !== 2)
|
|
261
|
+
throw new RangeError("runsTest: x must take exactly two values");
|
|
262
|
+
const n = x.length;
|
|
263
|
+
const n1 = x.filter((v) => v === levels[0]).length;
|
|
264
|
+
const n2 = n - n1;
|
|
265
|
+
let R = 1;
|
|
266
|
+
for (let i = 1; i < n; i++)
|
|
267
|
+
if (x[i] !== x[i - 1])
|
|
268
|
+
R++;
|
|
269
|
+
const mean = 1 + (2 * n1 * n2) / n;
|
|
270
|
+
const v = (2 * n1 * n2 * (2 * n1 * n2 - n)) / (n * n * (n - 1));
|
|
271
|
+
const z = (R - mean) / Math.sqrt(v);
|
|
272
|
+
const alt = options.alternative ?? "two.sided";
|
|
273
|
+
const pValue = alt === "two.sided" ? 2 * pnorm(-Math.abs(z)) : alt === "less" ? pnorm(z) : pnorm(z, { lowerTail: false });
|
|
274
|
+
return { statistic: z, parameter: [], pValue, method: "Runs Test" };
|
|
275
|
+
}
|
|
276
|
+
const scaleSeries = (y) => {
|
|
277
|
+
const n = y.length;
|
|
278
|
+
const m = y.reduce((a, b) => a + b, 0) / n;
|
|
279
|
+
const s = Math.sqrt(y.reduce((a, b) => a + (b - m) ** 2, 0) / (n - 1));
|
|
280
|
+
return y.map((v) => (v - m) / s);
|
|
281
|
+
};
|
|
282
|
+
/** y_t and its lags 1..lag (rows t = lag..n-1). */
|
|
283
|
+
const embedLags = (y, lag) => {
|
|
284
|
+
const resp = [], lags = [];
|
|
285
|
+
for (let t = lag; t < y.length; t++) {
|
|
286
|
+
resp.push(y[t]);
|
|
287
|
+
lags.push(Array.from({ length: lag }, (_, i) => y[t - 1 - i]));
|
|
288
|
+
}
|
|
289
|
+
return { resp, lags };
|
|
290
|
+
};
|
|
291
|
+
const ssr = (X, y) => lm(X, y, {}).residuals.reduce((a, b) => a + b * b, 0);
|
|
292
|
+
function nonlinearityResult(n, m, df, ssr0, ssr1, type, method, lag = 1) {
|
|
293
|
+
// the augmented regression (an intercept, the lags and df terms) needs more rows than columns; R returns NaN or
|
|
294
|
+
// fails below that (log 109)
|
|
295
|
+
if (m - (1 + lag + df) <= 0)
|
|
296
|
+
throw new RangeError(`${method}: the series is too short (${n} values for lag ${lag})`);
|
|
297
|
+
if (type === "F") {
|
|
298
|
+
const statistic = ((ssr0 - ssr1) / df) / (ssr1 / (m - df));
|
|
299
|
+
return { statistic, parameter: [df, m - df], pValue: pf(statistic, df, m - df, { lowerTail: false }), method };
|
|
300
|
+
}
|
|
301
|
+
const statistic = n * Math.log(ssr0 / ssr1);
|
|
302
|
+
return { statistic, parameter: df, pValue: pchisq(statistic, df, { lowerTail: false }), method };
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Teraesvirta's neural network test for neglected nonlinearity (tseries' `terasvirta.test()`): y_t on 1 and its lags,
|
|
306
|
+
* then also on the second and third order products of the lags; Chisq = n log(SSR0 / SSR1) (n the series length) or
|
|
307
|
+
* F on (df, n - lag - df) (log 14). The series is standardised when `scale` (default).
|
|
308
|
+
* Equivalent to `tseries::terasvirta.test()` in R.
|
|
309
|
+
*
|
|
310
|
+
* @category Time series
|
|
311
|
+
*/
|
|
312
|
+
export function terasvirtaTest(x, options = {}) {
|
|
313
|
+
checkOptions(options, TERASVIRTA_TEST_KEYS, "terasvirtaTest"); // generated: option-keys.mjs
|
|
314
|
+
let y = finite(x, "terasvirtaTest");
|
|
315
|
+
const lag = options.lag ?? 1;
|
|
316
|
+
y = standardise((options.scale ?? true) ? centred(y) : y);
|
|
317
|
+
if (options.scale ?? true)
|
|
318
|
+
y = scaleSeries(y);
|
|
319
|
+
const { resp, lags } = embedLags(y, lag);
|
|
320
|
+
const m = resp.length;
|
|
321
|
+
const prods = lags.map((r) => {
|
|
322
|
+
const out = [];
|
|
323
|
+
for (let i = 0; i < lag; i++)
|
|
324
|
+
for (let j = i; j < lag; j++)
|
|
325
|
+
out.push(r[i] * r[j]);
|
|
326
|
+
for (let i = 0; i < lag; i++)
|
|
327
|
+
for (let j = i; j < lag; j++)
|
|
328
|
+
for (let k = j; k < lag; k++)
|
|
329
|
+
out.push(r[i] * r[j] * r[k]);
|
|
330
|
+
return out;
|
|
331
|
+
});
|
|
332
|
+
const df = prods[0].length;
|
|
333
|
+
return nonlinearityResult(y.length, m, df, ssr(lags, resp), ssr(lags.map((r, t) => [...r, ...prods[t]]), resp), options.type ?? "Chisq", "Teraesvirta Neural Network Test", lag);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* White's neural network test for neglected nonlinearity (tseries' `white.test()`): q logistic hidden units with
|
|
337
|
+
* weights runif((lag + 1) q, -range/2, range/2) (by columns, the first row on the constant), their principal components
|
|
338
|
+
* 2..qstar + 1 (standardised), then the statistic as {@link terasvirtaTest} (log 15). The weights come from rStats.js's
|
|
339
|
+
* R-compatible generator: with `seed` (R's `set.seed`) the value equals R's; without, the current stream is used.
|
|
340
|
+
* Equivalent to `tseries::white.test()` in R.
|
|
341
|
+
*
|
|
342
|
+
* @category Time series
|
|
343
|
+
*/
|
|
344
|
+
export function whiteTest(x, options = {}) {
|
|
345
|
+
checkOptions(options, WHITE_TEST_KEYS, "whiteTest"); // generated: option-keys.mjs
|
|
346
|
+
let y = finite(x, "whiteTest");
|
|
347
|
+
if (options.scale ?? true)
|
|
348
|
+
y = standardise(centred(y));
|
|
349
|
+
const lag = options.lag ?? 1;
|
|
350
|
+
const q = options.q ?? 10;
|
|
351
|
+
const qstar = options.qstar ?? 2;
|
|
352
|
+
const range = options.range ?? 4;
|
|
353
|
+
if (options.scale ?? true)
|
|
354
|
+
y = scaleSeries(y);
|
|
355
|
+
const { resp, lags } = embedLags(y, lag);
|
|
356
|
+
const m = resp.length;
|
|
357
|
+
if (options.seed !== undefined)
|
|
358
|
+
setSeed(options.seed);
|
|
359
|
+
const w = runif((lag + 1) * q, { min: -range / 2, max: range / 2 });
|
|
360
|
+
const W = (i, j) => w[j * (lag + 1) + i]; // column-major (lag + 1) x q
|
|
361
|
+
const hidden = lags.map((r) => Array.from({ length: q }, (_, j) => {
|
|
362
|
+
let s = W(0, j);
|
|
363
|
+
for (let i = 0; i < lag; i++)
|
|
364
|
+
s += r[i] * W(i + 1, j);
|
|
365
|
+
return 1 / (1 + Math.exp(-s));
|
|
366
|
+
}));
|
|
367
|
+
const pc = prcomp(hidden, { scale: true }).x.map((r) => r.slice(1, qstar + 1));
|
|
368
|
+
return nonlinearityResult(y.length, m, qstar, ssr(lags, resp), ssr(lags.map((r, t) => [...r, ...pc[t]]), resp), options.type ?? "Chisq", "White Neural Network Test", lag);
|
|
369
|
+
}
|