@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,70 @@
|
|
|
1
|
+
import { type ForecastArimaResult } from "./forecastArima.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options of {@link autoArima}.
|
|
4
|
+
*
|
|
5
|
+
* @category Time series
|
|
6
|
+
*/
|
|
7
|
+
export interface AutoArimaOptions {
|
|
8
|
+
frequency?: number;
|
|
9
|
+
d?: number;
|
|
10
|
+
D?: number;
|
|
11
|
+
maxP?: number;
|
|
12
|
+
maxQ?: number;
|
|
13
|
+
maxSeasonalP?: number;
|
|
14
|
+
maxSeasonalQ?: number;
|
|
15
|
+
maxOrder?: number;
|
|
16
|
+
maxD?: number;
|
|
17
|
+
maxSeasonalD?: number;
|
|
18
|
+
startP?: number;
|
|
19
|
+
startQ?: number;
|
|
20
|
+
startSeasonalP?: number;
|
|
21
|
+
startSeasonalQ?: number;
|
|
22
|
+
stationary?: boolean;
|
|
23
|
+
seasonal?: boolean;
|
|
24
|
+
ic?: "aicc" | "aic" | "bic";
|
|
25
|
+
stepwise?: boolean;
|
|
26
|
+
nmodels?: number;
|
|
27
|
+
/** Rank the models by their CSS fits (default: n > 150 or frequency > 12), then refit the best without it. */
|
|
28
|
+
approximation?: boolean;
|
|
29
|
+
allowdrift?: boolean;
|
|
30
|
+
allowmean?: boolean;
|
|
31
|
+
lambda?: number | "auto";
|
|
32
|
+
biasadj?: boolean;
|
|
33
|
+
test?: "kpss" | "adf" | "pp";
|
|
34
|
+
seasonalTest?: "seas" | "ocsb";
|
|
35
|
+
alpha?: number;
|
|
36
|
+
xreg?: readonly (readonly number[])[];
|
|
37
|
+
xregNames?: readonly string[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* One model of the search (forecast's trace).
|
|
41
|
+
*
|
|
42
|
+
* @category Time series
|
|
43
|
+
*/
|
|
44
|
+
export interface AutoArimaTraceEntry {
|
|
45
|
+
order: [number, number, number];
|
|
46
|
+
seasonal: [number, number, number];
|
|
47
|
+
constant: boolean;
|
|
48
|
+
/** The criterion (Infinity for a rejected or failed fit; the CSS fit's value with approximation). */
|
|
49
|
+
ic: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Result of {@link autoArima}: the chosen model's fit and the search.
|
|
53
|
+
*
|
|
54
|
+
* @category Time series
|
|
55
|
+
*/
|
|
56
|
+
export interface AutoArimaResult extends ForecastArimaResult {
|
|
57
|
+
trace: AutoArimaTraceEntry[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Chooses and fits an ARIMA model by forecast's `auto.arima()` rules: d by {@link ndiffs} (after the seasonal
|
|
61
|
+
* differences), D by {@link nsdiffs}, then the stepwise search on the AICc (or all models up to maxOrder with
|
|
62
|
+
* `stepwise: false`).
|
|
63
|
+
* Equivalent to `forecast::auto.arima()` in R.
|
|
64
|
+
*
|
|
65
|
+
* With approximation, models are ranked by the CSS fits' AICc; R's printed trace adds a constant per series whose origin
|
|
66
|
+
* is not known (it does not change the choice), so the trace values here are the CSS AICc themselves.
|
|
67
|
+
*
|
|
68
|
+
* @category Time series
|
|
69
|
+
*/
|
|
70
|
+
export declare function autoArima(y: readonly number[], options?: AutoArimaOptions): AutoArimaResult;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// forecast's auto.arima. Written for rStats.js from the behaviour specification oracle/tsmeta-behaviour.md
|
|
2
|
+
// ("auto.arima") with the details found in experiments/tsmeta120-log.md (items 43-46): the stepwise search of Hyndman
|
|
3
|
+
// and Khandakar (2008) with forecast's neighbour order, Arima's CSS-ML AICc as the criterion, and the rejection of
|
|
4
|
+
// models with a root of modulus below 1.01. No R or package source was read.
|
|
5
|
+
import { minRootModulus } from "./arima.js";
|
|
6
|
+
import { forecastArima, forecastArimaFixed, ndiffs } from "./forecastArima.js";
|
|
7
|
+
import { nsdiffs } from "./stl.js";
|
|
8
|
+
import { tsInput } from "./input.js";
|
|
9
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
10
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
11
|
+
const AUTO_ARIMA_KEYS = /* @__PURE__ */ new Set([
|
|
12
|
+
"frequency", "d", "D", "maxP", "maxQ", "maxSeasonalP", "maxSeasonalQ", "maxOrder", "maxD", "maxSeasonalD", "startP",
|
|
13
|
+
"startQ", "startSeasonalP", "startSeasonalQ", "stationary", "seasonal", "ic", "stepwise", "nmodels", "approximation",
|
|
14
|
+
"allowdrift", "allowmean", "lambda", "biasadj", "test", "seasonalTest", "alpha", "xreg", "xregNames",
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* Chooses and fits an ARIMA model by forecast's `auto.arima()` rules: d by {@link ndiffs} (after the seasonal
|
|
18
|
+
* differences), D by {@link nsdiffs}, then the stepwise search on the AICc (or all models up to maxOrder with
|
|
19
|
+
* `stepwise: false`).
|
|
20
|
+
* Equivalent to `forecast::auto.arima()` in R.
|
|
21
|
+
*
|
|
22
|
+
* With approximation, models are ranked by the CSS fits' AICc; R's printed trace adds a constant per series whose origin
|
|
23
|
+
* is not known (it does not change the choice), so the trace values here are the CSS AICc themselves.
|
|
24
|
+
*
|
|
25
|
+
* @category Time series
|
|
26
|
+
*/
|
|
27
|
+
export function autoArima(y, options = {}) {
|
|
28
|
+
checkOptions(options, AUTO_ARIMA_KEYS, "autoArima"); // generated: option-keys.mjs
|
|
29
|
+
const m = options.frequency ?? 1;
|
|
30
|
+
const seasonalOn = (options.seasonal ?? true) && m > 1;
|
|
31
|
+
const x = tsInput(y, "autoArima", { missing: "allow" });
|
|
32
|
+
const n = x.filter(Number.isFinite).length;
|
|
33
|
+
const maxP = options.maxP ?? 5, maxQ = options.maxQ ?? 5;
|
|
34
|
+
// short series (R's limits, read off its traces; log 107): the seasonal orders at most floor(n / (3 m)) (none
|
|
35
|
+
// below three periods), and below four observations the AIC instead of the AICc (undefined there for a model with
|
|
36
|
+
// a mean); a model whose AICc has no positive denominator n - k - 1 is not a candidate
|
|
37
|
+
const maxSeasonal = Math.floor(n / (3 * m));
|
|
38
|
+
const maxSP = seasonalOn ? Math.min(options.maxSeasonalP ?? 2, maxSeasonal) : 0, maxSQ = seasonalOn ? Math.min(options.maxSeasonalQ ?? 2, maxSeasonal) : 0;
|
|
39
|
+
const maxOrder = options.maxOrder ?? 5;
|
|
40
|
+
const ic = options.ic ?? (n <= 3 ? "aic" : "aicc");
|
|
41
|
+
const nmodels = options.nmodels ?? 94;
|
|
42
|
+
const stationary = options.stationary ?? false;
|
|
43
|
+
const approximation = options.approximation ?? (n > 150 || m > 12);
|
|
44
|
+
const allowdrift = options.allowdrift ?? true, allowmean = options.allowmean ?? true;
|
|
45
|
+
const lambda = options.lambda === undefined ? null : options.lambda;
|
|
46
|
+
const z0 = x;
|
|
47
|
+
// differencing orders
|
|
48
|
+
let D = options.D ?? (stationary || !seasonalOn ? 0 : nsdiffs(z0, { frequency: m, test: options.seasonalTest ?? "seas", maxD: options.maxSeasonalD ?? 1 }));
|
|
49
|
+
if (!seasonalOn)
|
|
50
|
+
D = 0;
|
|
51
|
+
let dx = z0.filter(Number.isFinite);
|
|
52
|
+
for (let k = 0; k < D; k++)
|
|
53
|
+
dx = dx.slice(m).map((v, i) => v - dx[i]);
|
|
54
|
+
const d = options.d ?? (stationary ? 0 : ndiffs(dx, { test: options.test ?? "kpss", alpha: options.alpha ?? 0.05, maxD: options.maxD ?? 2 }));
|
|
55
|
+
const constantAllowed = d + D <= 1 && (d + D === 0 ? allowmean : allowdrift);
|
|
56
|
+
// an exact fit (the differenced series constant: a constant series, a straight line, an exact seasonal pattern):
|
|
57
|
+
// ARIMA(0,d,0)(0,D,0) with the constant (mean or drift) equal to that value and sigma2 = 0, as R's (log 108); the
|
|
58
|
+
// likelihood is unbounded there, so no search
|
|
59
|
+
let ddx = dx;
|
|
60
|
+
for (let k = 0; k < d; k++)
|
|
61
|
+
ddx = ddx.slice(1).map((v, i) => v - ddx[i]);
|
|
62
|
+
if (!options.xreg && lambda === null && ddx.length > 0 && ddx.every((v) => v === ddx[0])) {
|
|
63
|
+
const fit = forecastArimaFixed(z0, { order: [0, d, 0], seasonal: [0, D, 0], frequency: m, includeConstant: constantAllowed }, constantAllowed ? [ddx[0]] : []);
|
|
64
|
+
return { ...fit, trace: [{ order: [0, d, 0], seasonal: [0, D, 0], constant: constantAllowed, ic: fit.aicc }] };
|
|
65
|
+
}
|
|
66
|
+
const fitted = new Map();
|
|
67
|
+
const trace = [];
|
|
68
|
+
const key = (s) => `${s.p},${s.d},${s.q},${s.P},${s.D},${s.Q},${s.constant}`;
|
|
69
|
+
const evaluate = (s, approx) => {
|
|
70
|
+
let fit;
|
|
71
|
+
try {
|
|
72
|
+
fit = forecastArima(z0, {
|
|
73
|
+
order: [s.p, s.d, s.q], seasonal: [s.P, s.D, s.Q], frequency: m, includeConstant: s.constant,
|
|
74
|
+
...(lambda !== null ? { lambda } : {}), biasadj: options.biasadj ?? false, method: approx ? "CSS" : "CSS-ML",
|
|
75
|
+
...(options.xreg ? { xreg: options.xreg, ...(options.xregNames ? { xregNames: options.xregNames } : {}) } : {}),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return { ic: Infinity };
|
|
80
|
+
}
|
|
81
|
+
// the AR and MA polynomials with the seasonal parts multiplied out: a root of modulus < 1.01 rejects the model
|
|
82
|
+
const phi = fit.model.phi, theta = fit.model.theta;
|
|
83
|
+
if (phi.length && minRootModulus(phi.map((v) => -v)) < 1.01)
|
|
84
|
+
return { ic: Infinity, fit };
|
|
85
|
+
if (theta.length && minRootModulus(theta) < 1.01)
|
|
86
|
+
return { ic: Infinity, fit };
|
|
87
|
+
let val;
|
|
88
|
+
if (approx) {
|
|
89
|
+
const npar = fit.mask.filter(Boolean).length + 1;
|
|
90
|
+
const nobs = fit.nobs;
|
|
91
|
+
const aic = -2 * fit.loglik + 2 * npar;
|
|
92
|
+
val = ic === "aic" ? aic : ic === "bic" ? aic + npar * (Math.log(nobs) - 2) : nobs - npar - 1 > 0 ? aic + (2 * npar * (npar + 1)) / (nobs - npar - 1) : Infinity;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
const npar = fit.mask.filter(Boolean).length + 1;
|
|
96
|
+
val = ic === "aic" ? fit.aic : ic === "bic" ? fit.bic : fit.nobs - npar - 1 > 0 ? fit.aicc : Infinity;
|
|
97
|
+
}
|
|
98
|
+
return { ic: Number.isFinite(val) ? val : Infinity, fit };
|
|
99
|
+
};
|
|
100
|
+
const tryModel = (s) => {
|
|
101
|
+
const k = key(s);
|
|
102
|
+
const prev = fitted.get(k);
|
|
103
|
+
if (prev)
|
|
104
|
+
return prev.ic;
|
|
105
|
+
const r = evaluate(s, approximation);
|
|
106
|
+
fitted.set(k, r);
|
|
107
|
+
trace.push({ order: [s.p, s.d, s.q], seasonal: [s.P, s.D, s.Q], constant: s.constant, ic: r.ic });
|
|
108
|
+
return r.ic;
|
|
109
|
+
};
|
|
110
|
+
const inBounds = (s) => s.p >= 0 && s.q >= 0 && s.P >= 0 && s.Q >= 0 && s.p <= maxP && s.q <= maxQ && s.P <= maxSP && s.Q <= maxSQ;
|
|
111
|
+
let best = null;
|
|
112
|
+
let bestIc = Infinity;
|
|
113
|
+
const stepwise = options.stepwise ?? true;
|
|
114
|
+
const consider = (s) => {
|
|
115
|
+
// nmodels limits the stepwise search only (the fixture's full USAccDeaths search fits 96 models)
|
|
116
|
+
if (!inBounds(s) || fitted.has(key(s)) || (stepwise && fitted.size >= nmodels))
|
|
117
|
+
return false;
|
|
118
|
+
const v = tryModel(s);
|
|
119
|
+
if (v < bestIc) {
|
|
120
|
+
bestIc = v;
|
|
121
|
+
best = s;
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
if (stepwise) {
|
|
127
|
+
const c = constantAllowed;
|
|
128
|
+
const sp = (p, q, P, Q, constant) => ({ p, d, q, P: seasonalOn ? P : 0, D, Q: seasonalOn ? Q : 0, constant });
|
|
129
|
+
const p0 = Math.min(options.startP ?? 2, maxP), q0 = Math.min(options.startQ ?? 2, maxQ);
|
|
130
|
+
const P0 = Math.min(options.startSeasonalP ?? 1, maxSP), Q0 = Math.min(options.startSeasonalQ ?? 1, maxSQ);
|
|
131
|
+
for (const s of [sp(p0, q0, P0, Q0, c), sp(0, 0, 0, 0, c), sp(1, 0, 1, 0, c), sp(0, 1, 0, 1, c)])
|
|
132
|
+
consider(s);
|
|
133
|
+
// when this model becomes the best, the search goes on from it, without the constant. R continues from its orders
|
|
134
|
+
// with the constant flag of the earlier best (16 of 16 traces, log 81) and so skips neighbours; not imitated
|
|
135
|
+
// (Departures from R)
|
|
136
|
+
if (c)
|
|
137
|
+
consider(sp(0, 0, 0, 0, false));
|
|
138
|
+
let improved = true;
|
|
139
|
+
while (improved && fitted.size < nmodels) {
|
|
140
|
+
improved = false;
|
|
141
|
+
const b = best ?? sp(0, 0, 0, 0, c);
|
|
142
|
+
const nb = (dp, dq, dP, dQ) => ({ ...b, p: b.p + dp, q: b.q + dq, P: b.P + dP, Q: b.Q + dQ });
|
|
143
|
+
const cands = [];
|
|
144
|
+
// the diagonal neighbours in the order (-1, -1), (-1, +1), (+1, -1), (+1, +1) (the fixture's WWWusage, Nile and lynx
|
|
145
|
+
// traces; log 43 had the middle two the other way round)
|
|
146
|
+
if (seasonalOn)
|
|
147
|
+
cands.push(nb(0, 0, -1, 0), nb(0, 0, 0, -1), nb(0, 0, 1, 0), nb(0, 0, 0, 1), nb(0, 0, -1, -1), nb(0, 0, -1, 1), nb(0, 0, 1, -1), nb(0, 0, 1, 1));
|
|
148
|
+
cands.push(nb(-1, 0, 0, 0), nb(0, -1, 0, 0), nb(1, 0, 0, 0), nb(0, 1, 0, 0), nb(-1, -1, 0, 0), nb(-1, 1, 0, 0), nb(1, -1, 0, 0), nb(1, 1, 0, 0));
|
|
149
|
+
if (constantAllowed)
|
|
150
|
+
cands.push({ ...b, constant: !b.constant });
|
|
151
|
+
for (const s of cands)
|
|
152
|
+
if (consider(s)) {
|
|
153
|
+
improved = true;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const consts = constantAllowed ? [false, true] : [false];
|
|
160
|
+
for (let p = 0; p <= maxP; p++)
|
|
161
|
+
for (let q = 0; q <= maxQ; q++)
|
|
162
|
+
for (let P = 0; P <= maxSP; P++)
|
|
163
|
+
for (let Q = 0; Q <= maxSQ; Q++) {
|
|
164
|
+
if (p + q + P + Q > maxOrder)
|
|
165
|
+
continue;
|
|
166
|
+
for (const constant of consts)
|
|
167
|
+
consider({ p, d, q, P, D, Q, constant });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!best)
|
|
171
|
+
throw new Error("auto.arima: no suitable ARIMA model found");
|
|
172
|
+
let chosen;
|
|
173
|
+
if (approximation) {
|
|
174
|
+
// refit the best without approximation; when it is rejected, the next best
|
|
175
|
+
const ranked = [...fitted.entries()].filter(([, v]) => Number.isFinite(v.ic)).sort((a, b) => a[1].ic - b[1].ic);
|
|
176
|
+
for (const [k] of ranked) {
|
|
177
|
+
const [p, dd, q, P, DD, Q, cs] = k.split(",");
|
|
178
|
+
const s = { p: +p, d: +dd, q: +q, P: +P, D: +DD, Q: +Q, constant: cs === "true" };
|
|
179
|
+
const r = evaluate(s, false);
|
|
180
|
+
if (Number.isFinite(r.ic) && r.fit) {
|
|
181
|
+
chosen = r.fit;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (!chosen)
|
|
186
|
+
throw new Error("auto.arima: no suitable ARIMA model found");
|
|
187
|
+
}
|
|
188
|
+
else
|
|
189
|
+
chosen = fitted.get(key(best)).fit;
|
|
190
|
+
return { ...chosen, trace };
|
|
191
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/** @category Time series */
|
|
2
|
+
export type EtsError = "A" | "M";
|
|
3
|
+
/** @category Time series */
|
|
4
|
+
export type EtsTrend = "N" | "A" | "M";
|
|
5
|
+
/** @category Time series */
|
|
6
|
+
export type EtsSeason = "N" | "A" | "M";
|
|
7
|
+
/**
|
|
8
|
+
* A fully specified ETS model.
|
|
9
|
+
*
|
|
10
|
+
* @category Time series
|
|
11
|
+
*/
|
|
12
|
+
export interface EtsSpec {
|
|
13
|
+
error: EtsError;
|
|
14
|
+
trend: EtsTrend;
|
|
15
|
+
damped: boolean;
|
|
16
|
+
season: EtsSeason;
|
|
17
|
+
m: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parameters and initial states in R's order: alpha, beta, gamma, phi, l, b, s0 .. s_{m-2} (s0 the most recent).
|
|
21
|
+
*
|
|
22
|
+
* @category Time series
|
|
23
|
+
*/
|
|
24
|
+
export interface EtsParameters {
|
|
25
|
+
alpha: number;
|
|
26
|
+
beta?: number;
|
|
27
|
+
gamma?: number;
|
|
28
|
+
phi?: number;
|
|
29
|
+
l: number;
|
|
30
|
+
b?: number;
|
|
31
|
+
s?: number[];
|
|
32
|
+
}
|
|
33
|
+
interface FilterOut {
|
|
34
|
+
e: number[];
|
|
35
|
+
mu: number[];
|
|
36
|
+
states: number[][];
|
|
37
|
+
lik: number;
|
|
38
|
+
sse: number;
|
|
39
|
+
}
|
|
40
|
+
/** The filter of table 2.3 given the parameters and initial states (states rows 0..n: l, b, s1..sm). */
|
|
41
|
+
export declare function etsFilter(y: readonly number[], spec: EtsSpec, par: EtsParameters): FilterOut;
|
|
42
|
+
/** The admissible region (Hyndman et al. 2008, ch. 10): the discount matrix F - g w' of the linear model is stable. */
|
|
43
|
+
export declare function admissible(spec: EtsSpec, al: number, be: number, ga: number, ph: number): boolean;
|
|
44
|
+
interface FitOut {
|
|
45
|
+
spec: EtsSpec;
|
|
46
|
+
par: EtsParameters;
|
|
47
|
+
filter: FilterOut;
|
|
48
|
+
nParams: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Fits one model by minimising lik (= -2 loglik) within the usual bounds and the admissible region. `bounds: "simple"`
|
|
52
|
+
* is forecast's initial = "simple" (its own Holt-Winters filter, log 91): alpha, beta* = beta / alpha and gamma in
|
|
53
|
+
* [0, 1], no admissibility check.
|
|
54
|
+
*/
|
|
55
|
+
export declare function fitModel(y: readonly number[], spec: EtsSpec, fixed?: Partial<Record<"alpha" | "beta" | "gamma" | "phi", number>>, fixedInit?: {
|
|
56
|
+
l: number;
|
|
57
|
+
b?: number;
|
|
58
|
+
s?: number[];
|
|
59
|
+
}, optCrit?: "lik" | "mse", bounds?: "usual" | "simple", start?: Partial<Record<"alpha" | "beta" | "gamma" | "phi", number>>): FitOut | null;
|
|
60
|
+
/**
|
|
61
|
+
* Options of {@link ets}.
|
|
62
|
+
*
|
|
63
|
+
* @category Time series
|
|
64
|
+
*/
|
|
65
|
+
export interface EtsOptions {
|
|
66
|
+
frequency?: number;
|
|
67
|
+
/** Three letters (error, trend, season) of A, M, N and Z (chosen); default "ZZZ". */
|
|
68
|
+
model?: string;
|
|
69
|
+
/** true / false, or undefined to try both when the trend is chosen. */
|
|
70
|
+
damped?: boolean;
|
|
71
|
+
alpha?: number;
|
|
72
|
+
beta?: number;
|
|
73
|
+
gamma?: number;
|
|
74
|
+
phi?: number;
|
|
75
|
+
additiveOnly?: boolean;
|
|
76
|
+
allowMultiplicativeTrend?: boolean;
|
|
77
|
+
restrict?: boolean;
|
|
78
|
+
ic?: "aicc" | "aic" | "bic";
|
|
79
|
+
lambda?: number;
|
|
80
|
+
biasadj?: boolean;
|
|
81
|
+
nmse?: number;
|
|
82
|
+
/** The estimation criterion: the likelihood (default) or the mean squared one-step error. */
|
|
83
|
+
optCrit?: "lik" | "mse";
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Result of {@link ets}.
|
|
87
|
+
*
|
|
88
|
+
* @category Time series
|
|
89
|
+
*/
|
|
90
|
+
export interface EtsResult {
|
|
91
|
+
spec: EtsSpec;
|
|
92
|
+
/** "ETS(M,Ad,M)" etc. */
|
|
93
|
+
method: string;
|
|
94
|
+
par: EtsParameters;
|
|
95
|
+
/** Number of estimated parameters (smoothing parameters and initial states). */
|
|
96
|
+
nParams: number;
|
|
97
|
+
loglik: number;
|
|
98
|
+
aic: number;
|
|
99
|
+
bic: number;
|
|
100
|
+
aicc: number;
|
|
101
|
+
sigma2: number;
|
|
102
|
+
mse: number;
|
|
103
|
+
amse: number;
|
|
104
|
+
fitted: number[];
|
|
105
|
+
residuals: number[];
|
|
106
|
+
states: number[][];
|
|
107
|
+
x: number[];
|
|
108
|
+
lambda: number | null;
|
|
109
|
+
biasadj: boolean;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Exponential smoothing state space model (forecast's `ets()`): one model, or the smallest information criterion
|
|
113
|
+
* among the allowed models for the letters given as "Z". The parameters are estimated to the optimum of the
|
|
114
|
+
* likelihood within the usual bounds and the admissible region (R's optimiser can stop earlier).
|
|
115
|
+
* Equivalent to `forecast::ets()` in R.
|
|
116
|
+
*
|
|
117
|
+
* @category Time series
|
|
118
|
+
*/
|
|
119
|
+
export declare function ets(y: readonly number[], options?: EtsOptions): EtsResult;
|
|
120
|
+
/** Fits a given model with given smoothing parameters and initial states (no estimation): for ses/holt/hw "simple". */
|
|
121
|
+
export declare function etsFixedInitial(y: readonly number[], spec: EtsSpec, init: {
|
|
122
|
+
l: number;
|
|
123
|
+
b?: number;
|
|
124
|
+
s?: number[];
|
|
125
|
+
}, fixed?: Partial<Record<"alpha" | "beta" | "gamma" | "phi", number>>, optCrit?: "lik" | "mse", bounds?: "usual" | "simple"): EtsResult;
|
|
126
|
+
/** The fit at given parameters (no estimation; for checks against R's own estimates). */
|
|
127
|
+
export declare function etsAt(y: readonly number[], spec: EtsSpec, par: EtsParameters, nParams: number): EtsResult;
|
|
128
|
+
/**
|
|
129
|
+
* Result of {@link forecastEts}.
|
|
130
|
+
*
|
|
131
|
+
* @category Time series
|
|
132
|
+
*/
|
|
133
|
+
export interface EtsForecast {
|
|
134
|
+
mean: number[];
|
|
135
|
+
lower: number[][];
|
|
136
|
+
upper: number[][];
|
|
137
|
+
level: number[];
|
|
138
|
+
method: string;
|
|
139
|
+
variance?: number[];
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Point forecasts and prediction intervals from an {@link ets} fit (forecast's `forecast.ets()`): the analytic
|
|
143
|
+
* variances of classes 1 and 2, the exact moments of class 3 (MNM, MAM, MAdM), and simulation otherwise (npaths
|
|
144
|
+
* paths with rStats.js's R-compatible generator, seeded by `seed`). Two departures from R's forecast.ets (the
|
|
145
|
+
* model's formulas are used): damped class 3 forecasts use l + phi b at h = 1, and ANA / MNA put the seasonal term
|
|
146
|
+
* of c_j at j mod m = 0.
|
|
147
|
+
* Equivalent to `forecast::forecast()` in R.
|
|
148
|
+
*
|
|
149
|
+
* @category Time series
|
|
150
|
+
*/
|
|
151
|
+
export declare function forecastEts(fit: EtsResult, options?: {
|
|
152
|
+
h?: number;
|
|
153
|
+
level?: readonly number[];
|
|
154
|
+
npaths?: number;
|
|
155
|
+
seed?: number;
|
|
156
|
+
biasadj?: boolean;
|
|
157
|
+
}): EtsForecast;
|
|
158
|
+
export {};
|