@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.
Files changed (139) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.ja.md +7 -7
  3. package/README.md +7 -7
  4. package/dist/causal.js +2 -2
  5. package/dist/chunks/chunk-33ODAPMR.js +2 -0
  6. package/dist/chunks/chunk-3ST6RZXG.js +2 -0
  7. package/dist/chunks/{chunk-JF4PXFJE.js → chunk-4EQN74JN.js} +2 -2
  8. package/dist/chunks/chunk-5RRFO4NU.js +2 -0
  9. package/dist/chunks/{chunk-3JFRDXN7.js → chunk-64DDDGJU.js} +2 -2
  10. package/dist/chunks/chunk-66TEOHPF.js +2 -0
  11. package/dist/chunks/chunk-6ISDROPG.js +2 -0
  12. package/dist/chunks/{chunk-EC77KAAM.js → chunk-6LAKI2QG.js} +2 -2
  13. package/dist/chunks/{chunk-5CTWYFMB.js → chunk-6QCH4LAL.js} +2 -2
  14. package/dist/chunks/{chunk-H6Z7JVSD.js → chunk-6RA73LXX.js} +2 -2
  15. package/dist/chunks/{chunk-KJMLQHCY.js → chunk-7C2ECGT6.js} +1 -1
  16. package/dist/chunks/{chunk-SCFFHEDZ.js → chunk-7KPFNJBE.js} +2 -2
  17. package/dist/chunks/chunk-ARPXG2PG.js +2 -0
  18. package/dist/chunks/chunk-AWXW4WSX.js +2 -0
  19. package/dist/chunks/{chunk-XQCHC3SH.js → chunk-B5GSH3AK.js} +1 -1
  20. package/dist/chunks/{chunk-2JB7F646.js → chunk-BLDLJHUG.js} +2 -2
  21. package/dist/chunks/{chunk-IKMW63BE.js → chunk-BU4I544S.js} +2 -2
  22. package/dist/chunks/{chunk-LCIX76FR.js → chunk-CAUNJ7S6.js} +2 -2
  23. package/dist/chunks/chunk-DFGW4QWU.js +2 -0
  24. package/dist/chunks/{chunk-WYVUCQO3.js → chunk-DPCCSFCG.js} +1 -1
  25. package/dist/chunks/chunk-EXS5QD7V.js +2 -0
  26. package/dist/chunks/chunk-FOCHNNP3.js +2 -0
  27. package/dist/chunks/{chunk-MXZ64QME.js → chunk-HEPGGURG.js} +2 -2
  28. package/dist/chunks/chunk-INNFAVT3.js +2 -0
  29. package/dist/chunks/chunk-JEELIABM.js +2 -0
  30. package/dist/chunks/chunk-KNF2GPJV.js +2 -0
  31. package/dist/chunks/chunk-L4ZS7MAX.js +2 -0
  32. package/dist/chunks/{chunk-RXYZQ3UX.js → chunk-LZAB4M7P.js} +2 -2
  33. package/dist/chunks/chunk-LZTDYQRS.js +3 -0
  34. package/dist/chunks/{chunk-N7BUE54L.js → chunk-MTSU2QHB.js} +2 -2
  35. package/dist/chunks/{chunk-GIK34P23.js → chunk-MXRN2G7O.js} +2 -2
  36. package/dist/chunks/chunk-OFDD4GTZ.js +2 -0
  37. package/dist/chunks/chunk-OOHFFLMY.js +2 -0
  38. package/dist/chunks/chunk-OOO5AAM7.js +2 -0
  39. package/dist/chunks/chunk-PJJJR3H6.js +2 -0
  40. package/dist/chunks/chunk-PVRNAOO7.js +2 -0
  41. package/dist/chunks/chunk-Q2JLU5YD.js +2 -0
  42. package/dist/chunks/{chunk-Z33Q545U.js → chunk-QCQBK5AC.js} +2 -2
  43. package/dist/chunks/chunk-RXK4KSKH.js +2 -0
  44. package/dist/chunks/chunk-UC3YYJHM.js +4 -0
  45. package/dist/chunks/chunk-UPACHZ4F.js +2 -0
  46. package/dist/chunks/{chunk-VATAUY7N.js → chunk-VCXPGCA4.js} +2 -2
  47. package/dist/chunks/chunk-VFL4GGYT.js +2 -0
  48. package/dist/chunks/chunk-VL72MQOQ.js +2 -0
  49. package/dist/chunks/chunk-VSV5QGXX.js +2 -0
  50. package/dist/chunks/{chunk-CTZUYKVL.js → chunk-WMEF5XWO.js} +2 -2
  51. package/dist/chunks/chunk-X6JS6ZZB.js +2 -0
  52. package/dist/chunks/chunk-YEDVOX5V.js +2 -0
  53. package/dist/chunks/chunk-ZCTB4N5J.js +3 -0
  54. package/dist/descriptive.js +2 -2
  55. package/dist/distributions/normal.d.ts +4 -0
  56. package/dist/distributions/normal.js +4 -2
  57. package/dist/distributions.js +2 -2
  58. package/dist/entries/tests.d.ts +6 -0
  59. package/dist/entries/tests.js +6 -0
  60. package/dist/entries/timeseries.d.ts +11 -0
  61. package/dist/entries/timeseries.js +11 -0
  62. package/dist/htest/timeROC.d.ts +564 -0
  63. package/dist/htest/timeROC.js +1106 -0
  64. package/dist/linalg.js +2 -2
  65. package/dist/meta/metabin.d.ts +32 -0
  66. package/dist/meta/metabin.js +137 -0
  67. package/dist/meta/metaextra.d.ts +153 -0
  68. package/dist/meta/metaextra.js +290 -0
  69. package/dist/meta/metagen.d.ts +124 -0
  70. package/dist/meta/metagen.js +263 -0
  71. package/dist/meta/metaother.d.ts +87 -0
  72. package/dist/meta/metaother.js +342 -0
  73. package/dist/meta/netmeta.d.ts +256 -0
  74. package/dist/meta/netmeta.js +547 -0
  75. package/dist/mixed.js +3 -4
  76. package/dist/models/glmFamily.js +4 -1
  77. package/dist/models/metaAnalysis.d.ts +4 -0
  78. package/dist/models/metaAnalysis.js +100 -18
  79. package/dist/multivariate.js +2 -2
  80. package/dist/numerics.js +2 -2
  81. package/dist/regression.js +2 -2
  82. package/dist/rstats.js +20 -18
  83. package/dist/special.js +2 -2
  84. package/dist/tests.js +15 -15
  85. package/dist/timeseries/acf.js +5 -2
  86. package/dist/timeseries/arima.d.ts +154 -0
  87. package/dist/timeseries/arima.js +1007 -0
  88. package/dist/timeseries/autoArima.d.ts +70 -0
  89. package/dist/timeseries/autoArima.js +191 -0
  90. package/dist/timeseries/ets.d.ts +158 -0
  91. package/dist/timeseries/ets.js +883 -0
  92. package/dist/timeseries/forecastArima.d.ts +136 -0
  93. package/dist/timeseries/forecastArima.js +432 -0
  94. package/dist/timeseries/forecastDispatch.d.ts +83 -0
  95. package/dist/timeseries/forecastDispatch.js +130 -0
  96. package/dist/timeseries/forecastSimple.d.ts +177 -0
  97. package/dist/timeseries/forecastSimple.js +371 -0
  98. package/dist/timeseries/forecastUtils.d.ts +260 -0
  99. package/dist/timeseries/forecastUtils.js +619 -0
  100. package/dist/timeseries/holtWinters.d.ts +65 -0
  101. package/dist/timeseries/holtWinters.js +193 -0
  102. package/dist/timeseries/input.d.ts +8 -0
  103. package/dist/timeseries/input.js +27 -0
  104. package/dist/timeseries/ocsbCritical.d.ts +2 -0
  105. package/dist/timeseries/ocsbCritical.js +6 -0
  106. package/dist/timeseries/scale.d.ts +16 -0
  107. package/dist/timeseries/scale.js +48 -0
  108. package/dist/timeseries/statsTs.d.ts +128 -0
  109. package/dist/timeseries/statsTs.js +456 -0
  110. package/dist/timeseries/stl.d.ts +131 -0
  111. package/dist/timeseries/stl.js +489 -0
  112. package/dist/timeseries/tseries.d.ts +110 -0
  113. package/dist/timeseries/tseries.js +369 -0
  114. package/dist/timeseries.js +2 -2
  115. package/package.json +1 -1
  116. package/dist/chunks/chunk-53WQDB3C.js +0 -2
  117. package/dist/chunks/chunk-6I23C2YJ.js +0 -2
  118. package/dist/chunks/chunk-FLFNMSHI.js +0 -3
  119. package/dist/chunks/chunk-GEHMND3C.js +0 -2
  120. package/dist/chunks/chunk-HG4LJBNV.js +0 -2
  121. package/dist/chunks/chunk-HGAEVPVS.js +0 -2
  122. package/dist/chunks/chunk-HMLYEFQN.js +0 -2
  123. package/dist/chunks/chunk-HRRCC2BD.js +0 -2
  124. package/dist/chunks/chunk-IBHMM2SL.js +0 -2
  125. package/dist/chunks/chunk-ICDW3I2F.js +0 -2
  126. package/dist/chunks/chunk-IOJVFYHW.js +0 -2
  127. package/dist/chunks/chunk-IW4S634O.js +0 -2
  128. package/dist/chunks/chunk-JPWYGTUN.js +0 -2
  129. package/dist/chunks/chunk-KVJBCT3D.js +0 -2
  130. package/dist/chunks/chunk-LY5XGFPU.js +0 -2
  131. package/dist/chunks/chunk-MKZH65DK.js +0 -2
  132. package/dist/chunks/chunk-MVPHISOV.js +0 -2
  133. package/dist/chunks/chunk-NDZVVNBB.js +0 -2
  134. package/dist/chunks/chunk-NOTJ45G3.js +0 -4
  135. package/dist/chunks/chunk-QUD3374N.js +0 -2
  136. package/dist/chunks/chunk-V6FMGGUV.js +0 -2
  137. package/dist/chunks/chunk-W5M3R4NH.js +0 -2
  138. package/dist/chunks/chunk-XUMVTGMM.js +0 -2
  139. package/dist/chunks/chunk-YXMHWS5R.js +0 -2
@@ -0,0 +1,193 @@
1
+ // stats' HoltWinters and predict.HoltWinters. Written for rStats.js from the behaviour specification
2
+ // oracle/tsmeta-behaviour.md with the details found in experiments/tsmeta120-log.md (items 12-13, 16). No R or package
3
+ // source was read.
4
+ import { optim } from "../optim/optim.js";
5
+ import { optimize } from "../numerics/integrate.js";
6
+ import { qnorm } from "../distributions/normal.js";
7
+ import { decompose } from "./statsTs.js";
8
+ import { powerOfTwoScale } from "./scale.js";
9
+ import { tsInput } from "./input.js";
10
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
11
+ import { checkOptions } from "../internal/checkOptions.js";
12
+ const HOLT_WINTERS_KEYS = /* @__PURE__ */ new Set([
13
+ "frequency", "alpha", "beta", "gamma", "seasonal", "startPeriods", "lStart", "bStart", "sStart",
14
+ ]);
15
+ const PREDICT_HOLT_WINTERS_KEYS = ["nAhead", "predictionInterval", "level"];
16
+ /**
17
+ * Holt-Winters filtering (stats' `HoltWinters()`): exponential smoothing with optional trend and seasonal components,
18
+ * the start values from decompose() of the first start.periods periods, the parameters minimising the SSE.
19
+ * Equivalent to R's `HoltWinters()`.
20
+ *
21
+ * @category Time series
22
+ */
23
+ export function holtWinters(x, options = {}) {
24
+ checkOptions(options, HOLT_WINTERS_KEYS, "holtWinters"); // generated: option-keys.mjs
25
+ // far from unit scale (the squares near the ends of the range), the fit of the series divided by a power of two c
26
+ // (exact), mapped back (1.20.0, log 104); at ordinary scales the series itself, as R
27
+ const xs = tsInput(x, "holtWinters");
28
+ const c0 = powerOfTwoScale(xs);
29
+ const c = Math.abs(Math.log2(c0)) > 200 ? c0 : 1;
30
+ const add = (options.seasonal ?? "additive") === "additive";
31
+ const r = holtWintersScaled(xs.map((v) => v / c), {
32
+ ...options,
33
+ ...(options.lStart !== undefined ? { lStart: options.lStart / c } : {}),
34
+ ...(options.bStart !== undefined ? { bStart: options.bStart / c } : {}),
35
+ ...(options.sStart ? { sStart: add ? options.sStart.map((v) => v / c) : options.sStart } : {}),
36
+ });
37
+ const hasB = r.beta !== false;
38
+ const nb = hasB ? 2 : 1;
39
+ return { ...r, coefficients: r.coefficients.map((v, i) => (i < nb || add ? v * c : v)), SSE: r.SSE * c * c,
40
+ fitted: r.fitted.map((v) => v * c), x: xs };
41
+ }
42
+ function holtWintersScaled(xs, options) {
43
+ const f = options.frequency ?? 1;
44
+ const n = xs.length;
45
+ const seasonal = options.seasonal ?? "additive";
46
+ const useGamma = options.gamma !== false && f > 1;
47
+ const useBeta = options.beta !== false;
48
+ let l0, b0 = 0, s0 = [];
49
+ let startT;
50
+ if (useGamma) {
51
+ // decompose of the first startPeriods periods; l and b = intercept and slope of lm(trend ~ 1..k) (log 12)
52
+ const sp = options.startPeriods ?? 2;
53
+ const d = decompose(xs.slice(0, sp * f), { frequency: f, type: seasonal });
54
+ const tr = d.trend.filter((v) => !Number.isNaN(v));
55
+ const k = tr.length;
56
+ const tm = (k + 1) / 2;
57
+ const ym = tr.reduce((a, b) => a + b, 0) / k;
58
+ let sxy = 0, sxx = 0;
59
+ tr.forEach((v, i) => { sxy += (i + 1 - tm) * (v - ym); sxx += (i + 1 - tm) ** 2; });
60
+ const slope = sxy / sxx;
61
+ l0 = options.lStart ?? ym - slope * tm;
62
+ b0 = options.bStart ?? slope;
63
+ s0 = options.sStart ? [...options.sStart] : d.figure;
64
+ startT = f; // the recursions from t = f + 1 (1-based)
65
+ }
66
+ else if (useBeta) {
67
+ l0 = options.lStart ?? xs[1];
68
+ b0 = options.bStart ?? xs[1] - xs[0];
69
+ startT = 2;
70
+ }
71
+ else {
72
+ l0 = options.lStart ?? xs[0];
73
+ startT = 1;
74
+ }
75
+ const run = (al, be, ga) => {
76
+ let level = l0, trend = useBeta ? b0 : 0;
77
+ const season = s0.slice();
78
+ const fitted = [];
79
+ let sse = 0;
80
+ for (let t = startT; t < n; t++) {
81
+ const sIdx = (t - startT) % f;
82
+ const st = useGamma ? season[sIdx] : seasonal === "multiplicative" ? 1 : 0;
83
+ const xhat = useGamma ? (seasonal === "additive" ? level + trend + st : (level + trend) * st) : level + trend;
84
+ fitted.push(xhat);
85
+ const e = xs[t] - xhat;
86
+ sse += e * e;
87
+ const lPrev = level;
88
+ level = useGamma ? (seasonal === "additive" ? al * (xs[t] - st) + (1 - al) * (level + trend) : (al * xs[t]) / st + (1 - al) * (level + trend)) : al * xs[t] + (1 - al) * (level + trend);
89
+ if (useBeta)
90
+ trend = be * (level - lPrev) + (1 - be) * trend;
91
+ if (useGamma)
92
+ season[sIdx] = seasonal === "additive" ? ga * (xs[t] - level) + (1 - ga) * st : (ga * xs[t]) / level + (1 - ga) * st;
93
+ }
94
+ return { sse, level, trend, season, fitted };
95
+ };
96
+ let al = options.alpha ?? NaN, be = useBeta ? options.beta ?? NaN : 0, ga = useGamma ? options.gamma ?? NaN : 0;
97
+ const free = [];
98
+ if (Number.isNaN(al))
99
+ free.push("a");
100
+ if (useBeta && Number.isNaN(be))
101
+ free.push("b");
102
+ if (useGamma && Number.isNaN(ga))
103
+ free.push("g");
104
+ if (free.length === 1) {
105
+ const which = free[0];
106
+ const r = optimize((v) => run(which === "a" ? v : al, which === "b" ? v : be, which === "g" ? v : ga).sse, 0, 1);
107
+ if (which === "a")
108
+ al = r.optimum;
109
+ else if (which === "b")
110
+ be = r.optimum;
111
+ else
112
+ ga = r.optimum;
113
+ }
114
+ else if (free.length > 1) {
115
+ const start = free.map((k) => (k === "a" ? 0.3 : k === "b" ? 0.1 : 0.1));
116
+ const set = (u) => { let a2 = al, b2 = be, g2 = ga; free.forEach((k, i) => { if (k === "a")
117
+ a2 = u[i];
118
+ else if (k === "b")
119
+ b2 = u[i];
120
+ else
121
+ g2 = u[i]; }); return [a2, b2, g2]; };
122
+ // L-BFGS-B on [0, 1] (R's default start 0.3, 0.1, 0.1); restarted to a tight optimum. Its stopping rule
123
+ // factr eps max(|f|, |f_old|, 1) is absolute for an SSE below 1, which made the fit depend on the scale (5% at 1e-10
124
+ // times USAccDeaths, R's HoltWinters 40% at 1e-150; log 104): such an SSE is divided by a power of two near it.
125
+ const sse0 = run(...set(start)).sse;
126
+ const fnscale = sse0 > 0 && sse0 < 1 ? 2 ** Math.round(Math.log2(sse0)) : 1;
127
+ let r = optim(start, (u) => run(...set(u)).sse, { method: "L-BFGS-B", lower: 0, upper: 1, control: { fnscale } });
128
+ for (let k = 0; k < 5; k++) {
129
+ const r2 = optim(r.par, (u) => run(...set(u)).sse, { method: "L-BFGS-B", lower: 0, upper: 1, control: { factr: 1, fnscale } });
130
+ const better = r2.value < r.value;
131
+ r = better ? r2 : r;
132
+ if (!better)
133
+ break;
134
+ }
135
+ [al, be, ga] = set(r.par);
136
+ }
137
+ const fin = run(al, be, ga);
138
+ // coefficients: a, b, s1..sf with s1 the season of the next period
139
+ const nextIdx = (n - startT) % f;
140
+ const seasonOut = useGamma ? Array.from({ length: f }, (_, i) => fin.season[(nextIdx + i) % f]) : [];
141
+ const coefficients = [fin.level, ...(useBeta ? [fin.trend] : []), ...seasonOut];
142
+ const coefficientNames = ["a", ...(useBeta ? ["b"] : []), ...seasonOut.map((_, i) => `s${i + 1}`)];
143
+ return { alpha: al, beta: useBeta ? be : false, gamma: useGamma ? ga : false, coefficients, coefficientNames, SSE: fin.sse, fitted: fin.fitted,
144
+ start: startT, seasonal, frequency: f, x: xs.slice() };
145
+ }
146
+ /**
147
+ * Predictions from a {@link holtWinters} fit with intervals (stats' `predict.HoltWinters()`): additive and
148
+ * non-seasonal models var(residuals) (1 + sum_{j<h} psi_j^2), psi_j = alpha (1 + j beta) + gamma (1 - alpha) [j mod f
149
+ * = 0]; multiplicative models var(residuals) sum_{j=0}^{h-1} (psi_j s_h / s_{h-j})^2 with cyclic seasonal indices
150
+ * (R reads the seasonal factors at fixed positions of its coefficient vector instead: R bug 19369; equal for h < f with
151
+ * a trend).
152
+ * Equivalent to R's `predict()`.
153
+ *
154
+ * @category Time series
155
+ */
156
+ export function predictHoltWinters(fit, options = {}) {
157
+ checkOptions(options, PREDICT_HOLT_WINTERS_KEYS, "predictHoltWinters"); // generated: option-keys.mjs
158
+ const h = options.nAhead ?? 1;
159
+ const f = fit.frequency;
160
+ const hasB = fit.beta !== false;
161
+ const hasS = fit.gamma !== false;
162
+ const a = fit.coefficients[0];
163
+ const b = hasB ? fit.coefficients[1] : 0;
164
+ const s = hasS ? fit.coefficients.slice(hasB ? 2 : 1) : [];
165
+ const sAt = (k) => s[(k - 1) % f]; // k = 1..: the season of horizon k
166
+ const pred = Array.from({ length: h }, (_, i) => {
167
+ const k = i + 1;
168
+ const base = a + k * b;
169
+ return !hasS ? base : fit.seasonal === "additive" ? base + sAt(k) : base * sAt(k);
170
+ });
171
+ if (!options.predictionInterval)
172
+ return { fit: pred };
173
+ const res = fit.x.slice(fit.start).map((v, i) => v - fit.fitted[i]);
174
+ const mean = res.reduce((p, q) => p + q, 0) / res.length;
175
+ const v = res.reduce((p, q) => p + (q - mean) ** 2, 0) / (res.length - 1);
176
+ const al = fit.alpha, be = hasB ? fit.beta : 0, ga = hasS ? fit.gamma : 0;
177
+ const psi = (j) => al * (1 + j * be) + (hasS && j % f === 0 ? ga * (1 - al) : 0);
178
+ const q = qnorm(0.5 + (options.level ?? 0.95) / 2);
179
+ const half = pred.map((_, i) => {
180
+ const k = i + 1;
181
+ if (!hasS || fit.seasonal === "additive") {
182
+ let acc = 1;
183
+ for (let j = 1; j < k; j++)
184
+ acc += psi(j) ** 2;
185
+ return q * Math.sqrt(v * acc);
186
+ }
187
+ let acc = 0;
188
+ for (let j = 0; j < k; j++)
189
+ acc += ((psi(j) * sAt(k)) / sAt(k - j === 0 ? f : k - j)) ** 2;
190
+ return q * Math.sqrt(v * acc);
191
+ });
192
+ return { fit: pred, upr: pred.map((p, i) => p + half[i]), lwr: pred.map((p, i) => p - half[i]) };
193
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The series as numbers. Throws a RangeError naming the function when a value is infinite, when a value is missing
3
+ * (NaN) and `missing` is "fail", or when fewer than `minLength` values are observed.
4
+ */
5
+ export declare function tsInput(x: readonly number[], what: string, options?: {
6
+ missing?: "allow" | "fail";
7
+ minLength?: number;
8
+ }): number[];
@@ -0,0 +1,27 @@
1
+ // Checks of the series given to the time-series functions (1.20.0's edge-case review, experiments/tsmeta120-log.md
2
+ // 107): an infinite value is an error everywhere (R's functions fail with various messages or return NaN, and an
3
+ // infinite value cannot be a missing one); a missing value (NaN) is an error where the method has no rule for it
4
+ // (R's own functions fail there too); and the series must be long enough for the method. Written for rStats.js (no
5
+ // ported code).
6
+ /**
7
+ * The series as numbers. Throws a RangeError naming the function when a value is infinite, when a value is missing
8
+ * (NaN) and `missing` is "fail", or when fewer than `minLength` values are observed.
9
+ */
10
+ export function tsInput(x, what, options = {}) {
11
+ const y = x.map(Number);
12
+ let observed = 0;
13
+ for (const v of y) {
14
+ if (v === Infinity || v === -Infinity)
15
+ throw new RangeError(`${what}: the series contains infinite values`);
16
+ if (Number.isNaN(v)) {
17
+ if (options.missing !== "allow")
18
+ throw new RangeError(`${what}: the series contains missing values`);
19
+ }
20
+ else
21
+ observed++;
22
+ }
23
+ const min = options.minLength ?? 1;
24
+ if (observed < min)
25
+ throw new RangeError(`${what}: at least ${min} observation${min === 1 ? " is" : "s are"} needed (got ${observed})`);
26
+ return y;
27
+ }
@@ -0,0 +1,2 @@
1
+ export declare const OCSB_M: readonly number[];
2
+ export declare const OCSB_CRITICAL: readonly number[];
@@ -0,0 +1,6 @@
1
+ // Critical values of forecast's ocsb.test (5%), read off R's output for these seasonal periods m (forecast 9.0.2,
2
+ // experiments/tsmeta120-ocsbcrit.R, log 42): the smoothing function behind them is not shown, so they are data.
3
+ // Beyond the table: linear interpolation in log m between the recorded points, the last value after 8766 (Choice 7).
4
+ // Generated from experiments/tsmeta120-ocsbcrit.json.
5
+ export const OCSB_M = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 500, 730, 1000, 8766];
6
+ export const OCSB_CRITICAL = [-1.9519830458446257, -1.9175761775349098, -1.8926999246557656, -1.8735072817994582, -1.8580558017295434, -1.8452364865328739, -1.8343598034911663, -1.8249694581015068, -1.8167488043273357, -1.8094697178775636, -1.8029627911815704, -1.7970990295703597, -1.7917781204680912, -1.7869206423965618, -1.782462731788382, -1.7783523361723883, -1.774546521771276, -1.7710095000944828, -1.7677111559651668, -1.7646259322991922, -1.7617319732625452, -1.7590104575797822, -1.756445073821355, -1.754021603103417, -1.751727584029237, -1.7495520412951737, -1.7474852640806524, -1.745518623732409, -1.7436444227322359, -1.7418557687708898, -1.7401464691214419, -1.738510941540261, -1.7369441387125795, -1.7354414838659682, -1.7339988156450699, -1.7326123407080332, -1.731278592793897, -1.7299943972389156, -1.7287568401021725, -1.7275632412070607, -1.7264111305231837, -1.7252982274088957, -1.7242224223126932, -1.723181760595577, -1.722174428189088, -1.7211987388472132, -1.7202531227864641, -1.7193361165385295, -1.718446353865107, -1.7175825576056787, -1.7167435323468483, -1.7159281578169494, -1.7151353829224611, -1.7143642203536664, -1.7136137416963224, -1.7128830729940852, -1.7121713907132996, -1.7114779180676654, -1.7108019216653971, -1.7101427084459107, -1.7094996228769057, -1.7088720443860501, -1.7082593850043717, -1.7076610872010232, -1.7070766218912898, -1.7065054866016836, -1.7059472037776702, -1.7054013192211002, -1.7048674006457436, -1.7043450363405126, -1.7038338339310062, -1.7033334192309346, -1.70284343517581, -1.7023635408320255, -1.7018934104751011, -1.7014327327314562, -1.7009812097786003, -1.7005385565990951, -1.7001045002840656, -1.6996787793824193, -1.6992611432922673, -1.6988513516913473, -1.6984491740035357, -1.6980543888987676, -1.6976667838239285, -1.6972861545624707, -1.6969123048206984, -1.69654504583884, -1.6961841960251622, -1.6958295806115398, -1.6954810313290027, -1.69513838610191, -1.6948014887594987, -1.694470188763653, -1.69414434095183, -1.6938238052941497, -1.6935084466637436, -1.6931981346195082, -1.6928927432004863, -1.6925921507311437, -1.6922962396368662, -1.6920048962690537, -1.6917180107392207, -1.6914354767615676, -1.6911571915035097, -1.690883055443701, -1.6906129722371053, -1.690346848586711, -1.690084594121505, -1.6898261212803471, -1.6895713452014125, -1.6893201836168894, -1.68907255675264, -1.6888283872325487, -1.6885875999873068, -1.6883501221673844, -1.688115883059973, -1.6878848140096807, -1.6876568483427854, -1.6874319212948559, -1.6872099699415688, -1.6869909331325537, -1.686774751428111, -1.6865613670386614, -1.6863507237667794, -1.6861427669516933, -1.6859374434161183, -1.685734701415316, -1.6855344905882645, -1.6853367619108435, -1.6851414676509298, -1.6849485613253177, -1.6847579976583749, -1.6845697325423516, -1.684383722999268, -1.6841999271443016, -1.6840183041506123, -1.6838388142155325, -1.683661418528065, -1.6834860792376245, -1.6833127594239743, -1.6831414230682955, -1.6829720350253472, -1.6828045609966644, -1.682638967504749, -1.682475221868213, -1.6823132921778305, -1.68215314727346, -1.681994756721801, -1.6818380907949488, -1.6816831204497134, -1.681529817307672, -1.6813781536359236, -1.681228102328516, -1.6810796368885212, -1.680932731410727, -1.680787360564925, -1.68064349957977, -1.6805011242271843, -1.6803602108072893, -1.6802207361338437, -1.680082677520165, -1.6799460127655177, -1.67981072014195, -1.6796767783815625, -1.679544166664189, -1.6794128646054782, -1.6792828522453573, -1.6791541100368663, -1.6790266188353449, -1.6789003598879646, -1.6787753148235878, -1.6786514656429445, -1.6785287947091152, -1.678407284738308, -1.6782869187909184, -1.6781676802628647, -1.6780495528771844, -1.677932520675888, -1.6778165680120545, -1.6777016795421678, -1.6775878402186768, -1.6774750352827796, -1.6773632502574187, -1.6772524709404817, -1.6771426833981997, -1.67703387395874, -1.676926029205979, -1.6768191359734592, -1.6767131813385148, -1.6766081526165664, -1.6765040373555764, -1.6764008233306606, -1.6762984985388512, -1.6761970511940039, -1.676096469721849, -1.675996742755177, -1.6758978591291565, -1.6757998078767813, -1.6757025782244401, -1.675606159587607, -1.6755105415666467, -1.675415713942734, -1.67532166667388, -1.675228389891067, -1.675135873894481, -1.6750441091498485, -1.6749530862848638, -1.674862796085715, -1.6747732294936948, -1.6746843776019034, -1.674596231652032, -1.6745087830312326, -1.6744220232690645, -1.6743359440345198, -1.6742505371331227, -1.674165794504104, -1.6740817082176431, -1.6739982704721823, -1.673915473591804, -1.673833310023676, -1.6737517723355566, -1.6736708532133644, -1.673590545458803, -1.6735108419870466, -1.673431735824481, -1.6733532201064973, -1.6732752880753405, -1.6731979330780085, -1.6731211485641997, -1.6730449280843125, -1.672969265287489, -1.6728941539197049, -1.6728195878219063, -1.6727455609281863, -1.6726720672640072, -1.672599100944462, -1.672526656172576, -1.6724547272376473, -1.6723833085136255, -1.672312394457527, -1.672241979607887, -1.6721720585832431, -1.6721026260806584, -1.6720336768742727, -1.6719652058138883, -1.6718972078235865, -1.671829677900375, -1.671762611112865, -1.6716960025999774, -1.6716298475696763, -1.6715641412977316, -1.671498879126507, -1.6714340564637757, -1.6713696687815591, -1.6713057116149936, -1.6712421805612183, -1.6711790712782895, -1.6711163794841155, -1.6710541009554152, -1.6709922315266994, -1.6709307670892706, -1.6708697035902473, -1.6708090370316058, -1.670748763469243, -1.670688879012059, -1.6706293798210574, -1.6705702621084642, -1.670511522136866, -1.6704531562183649, -1.670395160713749, -1.6703375320316831, -1.6702802666279124, -1.6702233610044839, -1.6701668117089827, -1.6701106153337844, -1.6700547685153209, -1.669999267933362, -1.6699441103103105, -1.6698892924105118, -1.6698348110395758, -1.6697806630437129, -1.6697268453090834, -1.6696733547611586, -1.6696201883640938, -1.6695673431201152, -1.6695148160689168, -1.6694626042870695, -1.6694107048874414, -1.6693591150186298, -1.669307831864402, -1.6692568526431493, -1.6692061746073488, -1.6691557950430373, -1.6691057112692935, -1.6690559206377302, -1.6690064205319968, -1.6689572083672906, -1.6689082815898753, -1.668859637676612, -1.6688112741344954, -1.6687631885001997, -1.6687153783396336, -1.6686678412475018, -1.6686205748468765, -1.6685735767887735, -1.6685268447517394, -1.6684803764414438, -1.6684341695902796, -1.6683882219569703, -1.6683425313261855, -1.6682970955081595, -1.668251912338322, -1.66820697967693, -1.6681622954087112, -1.6681178574425084, -1.6680736637109344, -1.6680297121700303, -1.6679860007989316, -1.6679425275995383, -1.667899290596192, -1.6678562878353573, -1.6678135173853104, -1.667770977335831, -1.6677286657979007, -1.6676865809034067, -1.6676447208048497, -1.6676030836750566, -1.6675616677068996, -1.6675204711130174, -1.6674794921255442, -1.6674387289958401, -1.6673981799942283, -1.6673578434097356, -1.6673177175498373, -1.6672778007402065, -1.667238091324467, -1.6671985876639515, -1.6671592881374615, -1.6671201911410343, -1.66708129508771, -1.667042598407306, -1.6670040995461926, -1.6669657969670721, -1.6669276891487643, -1.6668897745859919, -1.6668520517891712, -1.6668145192842068, -1.666777175612287, -1.6667400193296855, -1.6667030490075636, -1.6666662632317772, -1.6666296606026865, -1.6665932397349679, -1.6665569992574303, -1.6665209378128323, -1.6664850540577036, -1.6664493466621695, -1.666413814309777, -1.6663784556973242, -1.6663432695346918, -1.6663082545446775, -1.6662734094628342, -1.666238733037308, -1.6662042240286812, -1.666169881209815, -1.6661357033656985, -1.6661016892932958, -1.6660678378013978, -1.6660341477104759, -1.6660006178525375, -1.6659672470709836, -1.6659340342204692, -1.6659009781667649, -1.6658680777866206, -1.665835331967633, -1.6658027396081116, -1.6657702996169506, -1.6657380109134992, -1.6657058724274367, -1.6656738830986468, -1.6656420418770963, -1.6656103477227127, -1.6655787996052667, -1.6655473965042538, -1.6655161374087792, -1.6654850213174437, -1.6654540472382304, -1.6654232141883956, -1.665392521194358, -1.6653619672915925, -1.6653315515245226, -1.665301272946417, -1.6652711306192856, -1.6652411236137785, -1.6652112510090855, -1.6651815118928368, -1.6651519053610062, -1.6651224305178145, -1.6627155399221638, -1.6595133802114004, -1.6575371274971082, -1.6526414379742407];
@@ -0,0 +1,16 @@
1
+ /** 2^round(log2(max |y|)) over the finite values (1 when there is none or all are zero). */
2
+ export declare function powerOfTwoScale(y: readonly number[]): number;
3
+ /**
4
+ * The series divided by {@link powerOfTwoScale}: for statistics that do not depend on the scale, the same values bit
5
+ * for bit wherever the squares (and fourth powers) stay in range, and right beyond.
6
+ */
7
+ export declare function standardise(y: readonly number[]): number[];
8
+ /**
9
+ * A reference value to subtract from a series with a large common value (1.20.0, log 110): the first finite value
10
+ * when it exceeds 1024 times the range of the finite values (y + 1e15: the sums of the values pass 2^53 and the
11
+ * statistics lose digits), else 0, so that ordinary series are left as they are. y - r is exact for such values
12
+ * (Sterbenz: r and y within a factor 2).
13
+ */
14
+ export declare function shiftReference(y: readonly number[]): number;
15
+ /** The series minus {@link shiftReference} (NaN kept), for statistics that do not depend on a common value. */
16
+ export declare function centred(y: readonly number[]): number[];
@@ -0,0 +1,48 @@
1
+ // Standardising a series by a power of two (1.20.0, experiments/tsmeta120-log.md 104): exact, so the fitting
2
+ // functions can work on a series of unit scale and map the results back. Written for rStats.js (no ported code).
3
+ /** 2^round(log2(max |y|)) over the finite values (1 when there is none or all are zero). */
4
+ export function powerOfTwoScale(y) {
5
+ let mx = 0;
6
+ for (const v of y)
7
+ if (Number.isFinite(v) && Math.abs(v) > mx)
8
+ mx = Math.abs(v);
9
+ if (!(mx > 0))
10
+ return 1;
11
+ const k = Math.round(Math.log2(mx));
12
+ // clamped to the normal powers of two (max |y| near 2^1024 or below 2^-1022)
13
+ return k > 1023 ? 2 ** 1023 : k < -1022 ? 2 ** -1022 : 2 ** k;
14
+ }
15
+ /**
16
+ * The series divided by {@link powerOfTwoScale}: for statistics that do not depend on the scale, the same values bit
17
+ * for bit wherever the squares (and fourth powers) stay in range, and right beyond.
18
+ */
19
+ export function standardise(y) {
20
+ const c = powerOfTwoScale(y);
21
+ return c === 1 ? y.slice() : y.map((v) => v / c);
22
+ }
23
+ /**
24
+ * A reference value to subtract from a series with a large common value (1.20.0, log 110): the first finite value
25
+ * when it exceeds 1024 times the range of the finite values (y + 1e15: the sums of the values pass 2^53 and the
26
+ * statistics lose digits), else 0, so that ordinary series are left as they are. y - r is exact for such values
27
+ * (Sterbenz: r and y within a factor 2).
28
+ */
29
+ export function shiftReference(y) {
30
+ let first = NaN, lo = Infinity, hi = -Infinity;
31
+ for (const v of y)
32
+ if (Number.isFinite(v)) {
33
+ if (Number.isNaN(first))
34
+ first = v;
35
+ if (v < lo)
36
+ lo = v;
37
+ if (v > hi)
38
+ hi = v;
39
+ }
40
+ if (Number.isNaN(first))
41
+ return 0;
42
+ return Math.abs(first) > 1024 * (hi - lo) && hi - lo > 0 ? first : 0;
43
+ }
44
+ /** The series minus {@link shiftReference} (NaN kept), for statistics that do not depend on a common value. */
45
+ export function centred(y) {
46
+ const r = shiftReference(y);
47
+ return r === 0 ? y.slice() : y.map((v) => v - r);
48
+ }
@@ -0,0 +1,128 @@
1
+ import { type ArimaResult } from "./arima.js";
2
+ /**
3
+ * Linear filtering of a series (stats' `filter()`): convolution, y_i = sum_j f_j x_{i + o - (j - 1)} with o = 0 for
4
+ * `sides: 1` and floor(p / 2) for `sides: 2` (NaN where an index falls outside, or wrapped with `circular`), or
5
+ * recursive, y_i = x_i + sum_j f_j y_{i - j} with `init` (the most recent value first, as R).
6
+ * Equivalent to R's `filter()`.
7
+ *
8
+ * @category Time series
9
+ */
10
+ export declare function tsFilter(x: readonly number[], f: readonly number[], options?: {
11
+ method?: "convolution" | "recursive";
12
+ sides?: 1 | 2;
13
+ circular?: boolean;
14
+ init?: readonly number[];
15
+ }): number[];
16
+ /**
17
+ * Box-Pierce or Ljung-Box test of white noise (stats' `Box.test()`): r_k the autocorrelations (mean removed, divided by
18
+ * n), Q = n sum r_k^2 or n (n + 2) sum r_k^2 / (n - k), chi-squared on lag - fitdf degrees of freedom.
19
+ * Equivalent to R's `Box.test()`.
20
+ *
21
+ * @category Time series
22
+ */
23
+ export declare function boxTest(x: readonly number[], options?: {
24
+ lag?: number;
25
+ type?: "Box-Pierce" | "Ljung-Box";
26
+ fitdf?: number;
27
+ }): {
28
+ statistic: number;
29
+ parameter: number;
30
+ pValue: number;
31
+ method: string;
32
+ };
33
+ /**
34
+ * The inverse of differencing (stats' `diffinv()`): the cumulative sums at the given lag, repeated `differences`
35
+ * times, starting from `xi` (zeros by default).
36
+ * Equivalent to R's `diffinv()`.
37
+ *
38
+ * @category Time series
39
+ */
40
+ export declare function diffinv(x: readonly number[], options?: {
41
+ lag?: number;
42
+ differences?: number;
43
+ xi?: readonly number[];
44
+ }): number[];
45
+ /**
46
+ * Result of {@link decompose}.
47
+ *
48
+ * @category Time series
49
+ */
50
+ export interface DecomposeResult {
51
+ x: number[];
52
+ seasonal: number[];
53
+ trend: number[];
54
+ random: number[];
55
+ figure: number[];
56
+ type: "additive" | "multiplicative";
57
+ }
58
+ /**
59
+ * Classical seasonal decomposition by moving averages (stats' `decompose()`). `start` is the position (1..frequency)
60
+ * of the first observation in the cycle; `figure` starts at that position, as R's.
61
+ * Equivalent to R's `decompose()`.
62
+ *
63
+ * @category Time series
64
+ */
65
+ export declare function decompose(x: readonly number[], options: {
66
+ frequency: number;
67
+ type?: "additive" | "multiplicative";
68
+ filter?: readonly number[];
69
+ }): DecomposeResult;
70
+ /**
71
+ * Result of {@link ar}.
72
+ *
73
+ * @category Time series
74
+ */
75
+ export interface ArResult {
76
+ order: number;
77
+ ar: number[];
78
+ varPred: number;
79
+ xMean: number;
80
+ /** ar.ols's intercept (of the demeaned series). */
81
+ xIntercept?: number;
82
+ /** aic minus its minimum, for orders 0..orderMax. */
83
+ aic: number[];
84
+ partialacf?: number[];
85
+ residuals: number[];
86
+ method: string;
87
+ orderMax: number;
88
+ /** The series (for predictions). */
89
+ x: number[];
90
+ }
91
+ /**
92
+ * Fits an autoregressive model with the order chosen by AIC (stats' `ar()`): Yule-Walker (default), Burg, OLS or MLE
93
+ * (arima0 for each order).
94
+ * Equivalent to R's `ar()`.
95
+ *
96
+ * @category Time series
97
+ */
98
+ export declare function ar(x: readonly number[], options?: {
99
+ method?: "yule-walker" | "burg" | "ols" | "mle";
100
+ orderMax?: number;
101
+ aic?: boolean;
102
+ demean?: boolean;
103
+ varMethod?: 1 | 2;
104
+ }): ArResult;
105
+ /**
106
+ * Predictions from an {@link ar} fit (stats' `predict.ar`): the AR recursion about the mean, se = sqrt(var.pred cumsum(psi^2)). Equivalent to R's `predict()`.
107
+ *
108
+ * @category Time series
109
+ */
110
+ export declare function predictAr(fit: ArResult, nAhead?: number): {
111
+ pred: number[];
112
+ se: number[];
113
+ };
114
+ /**
115
+ * stats' `arima0()`: an ARIMA model by maximum likelihood of the differenced series (no diffuse prior; the mean with
116
+ * `includeMean` when d + D = 0), with R's fast recursions once the prediction variance is within `delta` of its limit
117
+ * (default 0.01, as R; a negative delta gives the exact likelihood, equal to arima's). Log 84, 87.
118
+ * Equivalent to R's `arima0()`.
119
+ *
120
+ * @category Time series
121
+ */
122
+ export declare function arima0(x: readonly number[], options?: {
123
+ order?: readonly [number, number, number];
124
+ seasonal?: readonly [number, number, number];
125
+ frequency?: number;
126
+ includeMean?: boolean;
127
+ delta?: number;
128
+ }): ArimaResult;