@numero-uno-jpn/rstats 0.10.0 → 0.11.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 +24 -0
- package/README.ja.md +11 -8
- package/README.md +10 -7
- package/dist/chunks/{chunk-7HPVUVZB.js → chunk-5WDJPHYY.js} +1 -1
- package/dist/chunks/{chunk-EWC6I75B.js → chunk-7GQMKH3Z.js} +1 -1
- package/dist/chunks/chunk-AHHS6AC4.js +3 -0
- package/dist/chunks/chunk-ALM3VHFP.js +2 -0
- package/dist/chunks/{chunk-QFULZNHL.js → chunk-C5XTWMGB.js} +1 -1
- package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
- package/dist/chunks/{chunk-QULPY6YM.js → chunk-EZAFN5CD.js} +1 -1
- package/dist/chunks/{chunk-2XKG3DX4.js → chunk-EZWBRCJL.js} +1 -1
- package/dist/chunks/{chunk-O6PT7IVU.js → chunk-F5EDJFEO.js} +2 -2
- package/dist/chunks/{chunk-4NM37MGA.js → chunk-JWBO5WVN.js} +2 -2
- package/dist/chunks/{chunk-ENL43FNO.js → chunk-TOGXTCZO.js} +2 -2
- package/dist/chunks/{chunk-IT354SHV.js → chunk-VJ4BDBMU.js} +2 -2
- package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-Y5CNQ266.js} +2 -2
- package/dist/chunks/{chunk-LIMZKONM.js → chunk-Y5PYRAYP.js} +2 -2
- package/dist/chunks/{chunk-QJJWGNJP.js → chunk-YSWGDOWK.js} +2 -2
- package/dist/data/grouping.d.ts +79 -0
- package/dist/data/grouping.js +262 -0
- package/dist/descriptive/dataFrames.d.ts +110 -0
- package/dist/descriptive/dataFrames.js +392 -0
- package/dist/descriptive/missing.d.ts +35 -0
- package/dist/descriptive/missing.js +50 -0
- package/dist/descriptive/pivot.d.ts +89 -0
- package/dist/descriptive/pivot.js +343 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +4 -0
- package/dist/entries/descriptive.js +4 -0
- package/dist/htest/ansariTest.d.ts +12 -0
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/cor.d.ts +12 -0
- package/dist/htest/cor.js +9 -27
- package/dist/htest/ksTest.d.ts +13 -0
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/onewayTest.d.ts +14 -0
- package/dist/htest/onewayTest.js +5 -7
- package/dist/htest/rankTests.d.ts +57 -0
- package/dist/htest/rankTests.js +19 -31
- package/dist/htest/tTest.d.ts +20 -0
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/varTest.d.ts +23 -0
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +14 -0
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/linalg/ops.d.ts +13 -2
- package/dist/linalg/ops.js +20 -5
- package/dist/linalg.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -13
- package/llms.txt +12 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-AECGWZHD.js +0 -2
- package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Formula forms of the hypothesis tests (R's formula methods of t.test, wilcox.test, kruskal.test, friedman.test,
|
|
2
|
+
// cor.test, ...): the data a formula selects from a data frame, which the tests then pass to their default forms.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (no ported code) from oracle/formulatests-behaviour.md. Only the variables of the formula
|
|
5
|
+
// count: the rows where one of them is missing are dropped (src/models/modelFrame.ts), and a grouping variable becomes
|
|
6
|
+
// a factor of the remaining values, with sorted levels (numbers as numbers, strings by code point) and no unused
|
|
7
|
+
// level. The formula is parsed with src/models/formula.ts; `Pair(a, b)` on the left and `|` on the right, which that
|
|
8
|
+
// parser does not know, are split off the text first.
|
|
9
|
+
import { parseFormula, termsOf } from "../models/formula.js";
|
|
10
|
+
import { modelFrame } from "../models/modelFrame.js";
|
|
11
|
+
import { formatR } from "./core/simulate.js";
|
|
12
|
+
/** A variable of a model frame as a factor: numbers sorted as numbers and labelled as R prints them */
|
|
13
|
+
function groupFactor(v) {
|
|
14
|
+
if (v.kind === "factor")
|
|
15
|
+
return { levels: v.levels, codes: v.codes };
|
|
16
|
+
const sorted = [...new Set(v.values)].sort((a, b) => a - b);
|
|
17
|
+
const levels = [...new Set(sorted.map(formatR))];
|
|
18
|
+
const position = new Map(levels.map((l, i) => [l, i]));
|
|
19
|
+
return { levels, codes: v.values.map((x) => position.get(formatR(x))) };
|
|
20
|
+
}
|
|
21
|
+
/** The values of a numeric variable of a model frame (a response or a variable of a correlation) */
|
|
22
|
+
function numericValues(v, name) {
|
|
23
|
+
if (v.kind !== "numeric")
|
|
24
|
+
throw new TypeError(`'${name}' must be a numeric column`);
|
|
25
|
+
return v.values;
|
|
26
|
+
}
|
|
27
|
+
/** The terms of a formula without `Pair()` or `|`, and the names of the variables of its right-hand side */
|
|
28
|
+
function plainTerms(formula, data) {
|
|
29
|
+
const parsed = parseFormula(formula);
|
|
30
|
+
const terms = termsOf(parsed, Object.keys(data));
|
|
31
|
+
return { terms, rhsVariables: terms.variables.slice(terms.response === 0 ? 1 : 0) };
|
|
32
|
+
}
|
|
33
|
+
/** The text on each side of the formula's `~` (the left side empty for a one-sided formula) */
|
|
34
|
+
function splitTilde(formula) {
|
|
35
|
+
if (typeof formula !== "string")
|
|
36
|
+
throw new TypeError("the formula must be a string");
|
|
37
|
+
const at = formula.indexOf("~");
|
|
38
|
+
return at < 0 ? undefined : { lhs: formula.slice(0, at).trim(), rhs: formula.slice(at + 1) };
|
|
39
|
+
}
|
|
40
|
+
const PAIR = /^Pair\s*\(\s*([A-Za-z.][A-Za-z0-9._]*)\s*,\s*([A-Za-z.][A-Za-z0-9._]*)\s*\)$/;
|
|
41
|
+
const MISSING_OR_INCORRECT = "'formula' missing or incorrect";
|
|
42
|
+
/** Whether a right-hand side is the intercept alone (`~ 1`) */
|
|
43
|
+
const interceptOnly = (terms, rhsVariables) => rhsVariables.length === 0 && terms.terms.length === 0 && terms.intercept;
|
|
44
|
+
/**
|
|
45
|
+
* The samples of a two-sample test's formula. `oneSample` allows `y ~ 1` and `Pair(a, b) ~ 1` (t.test and
|
|
46
|
+
* wilcox.test).
|
|
47
|
+
*/
|
|
48
|
+
export function twoSampleData(formula, data, oneSample) {
|
|
49
|
+
const sides = splitTilde(formula);
|
|
50
|
+
const pair = sides === undefined ? null : PAIR.exec(sides.lhs);
|
|
51
|
+
if (pair !== null) {
|
|
52
|
+
const { terms, rhsVariables } = plainTerms(`~${sides.rhs}`, data);
|
|
53
|
+
if (!oneSample || !interceptOnly(terms, rhsVariables))
|
|
54
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
55
|
+
const frame = modelFrame(data, [pair[1], pair[2]]);
|
|
56
|
+
return { kind: "paired", x: numericValues(frame.variables[0], pair[1]), y: numericValues(frame.variables[1], pair[2]) };
|
|
57
|
+
}
|
|
58
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
59
|
+
if (terms.response < 0)
|
|
60
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
61
|
+
const response = terms.variables[0];
|
|
62
|
+
if (oneSample && interceptOnly(terms, rhsVariables)) {
|
|
63
|
+
return { kind: "one", x: numericValues(modelFrame(data, [response]).variables[0], response) };
|
|
64
|
+
}
|
|
65
|
+
if (rhsVariables.length !== 1 || terms.terms.length !== 1)
|
|
66
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
67
|
+
const frame = modelFrame(data, [response, rhsVariables[0]], { dropUnusedLevels: true });
|
|
68
|
+
const values = numericValues(frame.variables[0], response);
|
|
69
|
+
const group = groupFactor(frame.variables[1]);
|
|
70
|
+
if (group.levels.length !== 2)
|
|
71
|
+
throw new Error("grouping factor must have exactly 2 levels");
|
|
72
|
+
return {
|
|
73
|
+
kind: "two",
|
|
74
|
+
x: values.filter((_, i) => group.codes[i] === 0),
|
|
75
|
+
y: values.filter((_, i) => group.codes[i] === 1),
|
|
76
|
+
levels: [group.levels[0], group.levels[1]],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** The two samples of a formula `y ~ g` whose group has two levels (var.test, ansari.test, mood.test, ks.test) */
|
|
80
|
+
export function twoGroupData(formula, data) {
|
|
81
|
+
const d = twoSampleData(formula, data, false);
|
|
82
|
+
if (d.kind !== "two")
|
|
83
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
84
|
+
return d;
|
|
85
|
+
}
|
|
86
|
+
/** The response and group of a formula `y ~ g` with one variable on the right (kruskal.test, fligner.test, bartlett.test) */
|
|
87
|
+
export function kSampleData(formula, data) {
|
|
88
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
89
|
+
if (terms.response < 0)
|
|
90
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
91
|
+
if (rhsVariables.length !== 1)
|
|
92
|
+
throw new Error("'formula' should be of the form response ~ group");
|
|
93
|
+
const response = terms.variables[0];
|
|
94
|
+
const frame = modelFrame(data, [response, rhsVariables[0]], { dropUnusedLevels: true });
|
|
95
|
+
return { values: numericValues(frame.variables[0], response), group: groupFactor(frame.variables[1]) };
|
|
96
|
+
}
|
|
97
|
+
/** The samples of each level, in the order of the levels */
|
|
98
|
+
export function samplesByLevel(values, group) {
|
|
99
|
+
const out = group.levels.map(() => []);
|
|
100
|
+
values.forEach((v, i) => out[group.codes[i]].push(v));
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The response and groups of oneway.test's formula `y ~ A` or `y ~ A + B + ...`: with several factors the groups are
|
|
105
|
+
* the combinations present in the data, numbered with the first factor varying fastest (R's interaction()).
|
|
106
|
+
*/
|
|
107
|
+
export function onewayData(formula, data) {
|
|
108
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
109
|
+
if (terms.response < 0 || rhsVariables.length === 0)
|
|
110
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
111
|
+
const response = terms.variables[0];
|
|
112
|
+
const frame = modelFrame(data, [response, ...rhsVariables], { dropUnusedLevels: true });
|
|
113
|
+
const factors = frame.variables.slice(1).map(groupFactor);
|
|
114
|
+
const groups = frame.rows.map((_, i) => {
|
|
115
|
+
let code = 0;
|
|
116
|
+
let stride = 1;
|
|
117
|
+
for (const f of factors) {
|
|
118
|
+
code += f.codes[i] * stride;
|
|
119
|
+
stride *= f.levels.length;
|
|
120
|
+
}
|
|
121
|
+
return code;
|
|
122
|
+
});
|
|
123
|
+
return { values: numericValues(frame.variables[0], response), groups };
|
|
124
|
+
}
|
|
125
|
+
const INCORRECT_BLOCKS = "incorrect specification for 'formula'";
|
|
126
|
+
/**
|
|
127
|
+
* The matrix (one row per block, one column per group, levels in order) of a blocked design's formula `y ~ w | b`
|
|
128
|
+
* (friedman.test, quade.test). Rows with a missing value are dropped first; every combination of group and block must
|
|
129
|
+
* then occur exactly once.
|
|
130
|
+
*/
|
|
131
|
+
export function blockData(formula, data) {
|
|
132
|
+
const sides = splitTilde(formula);
|
|
133
|
+
const parts = sides?.rhs.split("|");
|
|
134
|
+
if (sides === undefined || sides.lhs === "" || parts.length !== 2)
|
|
135
|
+
throw new Error(INCORRECT_BLOCKS);
|
|
136
|
+
const one = (text) => {
|
|
137
|
+
const { terms, rhsVariables } = plainTerms(text, data);
|
|
138
|
+
if (rhsVariables.length !== 1 || terms.terms.length !== 1)
|
|
139
|
+
throw new Error(INCORRECT_BLOCKS);
|
|
140
|
+
return rhsVariables[0];
|
|
141
|
+
};
|
|
142
|
+
const { terms } = plainTerms(`${sides.lhs} ~ 1`, data);
|
|
143
|
+
const response = terms.variables[0];
|
|
144
|
+
const groupName = one(`~${parts[0]}`);
|
|
145
|
+
const blockName = one(`~${parts[1]}`);
|
|
146
|
+
const frame = modelFrame(data, [response, groupName, blockName], { dropUnusedLevels: true });
|
|
147
|
+
const values = numericValues(frame.variables[0], response);
|
|
148
|
+
const group = groupFactor(frame.variables[1]);
|
|
149
|
+
const block = groupFactor(frame.variables[2]);
|
|
150
|
+
const matrix = block.levels.map(() => group.levels.map(() => NaN));
|
|
151
|
+
const counts = block.levels.map(() => group.levels.map(() => 0));
|
|
152
|
+
values.forEach((v, i) => {
|
|
153
|
+
const b = block.codes[i];
|
|
154
|
+
const w = group.codes[i];
|
|
155
|
+
counts[b][w]++;
|
|
156
|
+
matrix[b][w] = v;
|
|
157
|
+
});
|
|
158
|
+
if (counts.some((row) => row.some((c) => c !== 1)))
|
|
159
|
+
throw new Error("not an unreplicated complete block design");
|
|
160
|
+
return matrix;
|
|
161
|
+
}
|
|
162
|
+
/** The two variables of cor.test's one-sided formula `~ x + z` */
|
|
163
|
+
export function correlationData(formula, data) {
|
|
164
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
165
|
+
if (terms.response >= 0)
|
|
166
|
+
throw new Error("'formula' missing or invalid");
|
|
167
|
+
if (rhsVariables.length !== 2)
|
|
168
|
+
throw new Error("invalid formula");
|
|
169
|
+
const frame = modelFrame(data, rhsVariables);
|
|
170
|
+
return { x: numericValues(frame.variables[0], rhsVariables[0]), y: numericValues(frame.variables[1], rhsVariables[1]) };
|
|
171
|
+
}
|
package/dist/htest/varTest.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
2
3
|
/** Options for {@link varTest}. */
|
|
3
4
|
export interface VarTestOptions {
|
|
@@ -27,8 +28,19 @@ export interface VarTestResult extends Omit<HTest, "parameter" | "parameterName"
|
|
|
27
28
|
* ```ts
|
|
28
29
|
* varTest([1, 3, 2, 5, 4], [2, 9, 4, 12, 7, 1]).statistic; // 0.2307692
|
|
29
30
|
* ```
|
|
31
|
+
*
|
|
32
|
+
* There is also a formula form, `varTest("y ~ g", data, options)` (next overload).
|
|
30
33
|
*/
|
|
31
34
|
export declare function varTest(x: Sample, y: Sample, options?: VarTestOptions): VarTestResult;
|
|
35
|
+
/**
|
|
36
|
+
* F test of two variances from a formula and a data frame. Equivalent to R's `var.test(formula, data)`: `"y ~ g"`
|
|
37
|
+
* compares the two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where
|
|
38
|
+
* one of them is missing are dropped.
|
|
39
|
+
*
|
|
40
|
+
* @param formula `"y ~ g"`.
|
|
41
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
42
|
+
*/
|
|
43
|
+
export declare function varTest(formula: string, data: DataFrame, options?: VarTestOptions): VarTestResult;
|
|
32
44
|
/** Result of {@link bartlettTest}. Has no `alternative`, as R's `bartlett.test()` returns none. */
|
|
33
45
|
export interface BartlettTestResult extends Omit<HTest, "alternative"> {
|
|
34
46
|
/** Degrees of freedom (`parameterName` is `"df"`). */
|
|
@@ -44,5 +56,16 @@ export interface BartlettTestResult extends Omit<HTest, "alternative"> {
|
|
|
44
56
|
* ```ts
|
|
45
57
|
* bartlettTest([[1, 3, 2, 5], [2, 9, 4, 12, 7], [3, 3.5, 4, 2]]).statistic;
|
|
46
58
|
* ```
|
|
59
|
+
*
|
|
60
|
+
* There is also a formula form, `bartlettTest("y ~ g", data)` (next overload).
|
|
47
61
|
*/
|
|
48
62
|
export declare function bartlettTest(groups: readonly Sample[]): BartlettTestResult;
|
|
63
|
+
/**
|
|
64
|
+
* Bartlett's test from a formula and a data frame. Equivalent to R's `bartlett.test(formula, data)`: `"y ~ g"`, one
|
|
65
|
+
* group per level of `g`, in the order of the levels. Only the variables of the formula count: the rows where one of
|
|
66
|
+
* them is missing are dropped.
|
|
67
|
+
*
|
|
68
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
69
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
70
|
+
*/
|
|
71
|
+
export declare function bartlettTest(formula: string, data: DataFrame): BartlettTestResult;
|
package/dist/htest/varTest.js
CHANGED
|
@@ -8,17 +8,13 @@
|
|
|
8
8
|
import { pf1, qf1 } from "../distributions/f.js";
|
|
9
9
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
10
|
import { variance } from "../internal/summary.js";
|
|
11
|
+
import { kSampleData, samplesByLevel, twoGroupData } from "./testFormula.js";
|
|
11
12
|
import { checkConfLevelOpen, matchAlternative } from "./types.js";
|
|
12
|
-
/**
|
|
13
|
-
* F test that the variances of two normal samples have a given ratio. Equivalent to R's `var.test()`. `NaN` values
|
|
14
|
-
* are dropped.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* varTest([1, 3, 2, 5, 4], [2, 9, 4, 12, 7, 1]).statistic; // 0.2307692
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
13
|
export function varTest(x, y, options = {}) {
|
|
14
|
+
if (typeof x === "string") {
|
|
15
|
+
const d = twoGroupData(x, y);
|
|
16
|
+
return varTest(d.x, d.y, options);
|
|
17
|
+
}
|
|
22
18
|
// as in R, infinite values are dropped along with NaN
|
|
23
19
|
const xs = Array.from(x).filter(Number.isFinite);
|
|
24
20
|
const ys = Array.from(y).filter(Number.isFinite);
|
|
@@ -69,18 +65,12 @@ export function varTest(x, y, options = {}) {
|
|
|
69
65
|
nullValueName: "ratio of variances",
|
|
70
66
|
};
|
|
71
67
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
* @example
|
|
79
|
-
* ```ts
|
|
80
|
-
* bartlettTest([[1, 3, 2, 5], [2, 9, 4, 12, 7], [3, 3.5, 4, 2]]).statistic;
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export function bartlettTest(groups) {
|
|
68
|
+
export function bartlettTest(x, data) {
|
|
69
|
+
if (typeof x === "string") {
|
|
70
|
+
const d = kSampleData(x, data);
|
|
71
|
+
return bartlettTest(samplesByLevel(d.values, d.group));
|
|
72
|
+
}
|
|
73
|
+
const groups = x;
|
|
84
74
|
const k = groups.length;
|
|
85
75
|
if (k < 2)
|
|
86
76
|
throw new RangeError("'x' must be a list with at least 2 elements");
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
2
3
|
/** Options for {@link wilcoxTest}. */
|
|
3
4
|
export interface WilcoxTestOptions {
|
|
@@ -57,5 +58,18 @@ export interface WilcoxTestResult extends HTest {
|
|
|
57
58
|
* r.statistic; // 40 (V)
|
|
58
59
|
* r.pValue; // 0.01953125
|
|
59
60
|
* ```
|
|
61
|
+
*
|
|
62
|
+
* There is also a formula form, `wilcoxTest("y ~ g", data, options)` (next overload).
|
|
60
63
|
*/
|
|
61
64
|
export declare function wilcoxTest(x: Sample, y?: Sample | WilcoxTestOptions, options?: WilcoxTestOptions): WilcoxTestResult;
|
|
65
|
+
/**
|
|
66
|
+
* Wilcoxon tests from a formula and a data frame. Equivalent to R's `wilcox.test(formula, data)`: `"y ~ g"` is the
|
|
67
|
+
* rank sum test of the two levels of `g` (the first level is `x`), `"y ~ 1"` the signed rank test and
|
|
68
|
+
* `"Pair(a, b) ~ 1"` the paired test of `a` and `b` (the option `paired` is not allowed). Only the variables of the
|
|
69
|
+
* formula count: the rows where one of them is missing are dropped.
|
|
70
|
+
*
|
|
71
|
+
* @param formula `"y ~ g"`, `"y ~ 1"` or `"Pair(a, b) ~ 1"`.
|
|
72
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
73
|
+
* @param options See {@link WilcoxTestOptions}.
|
|
74
|
+
*/
|
|
75
|
+
export declare function wilcoxTest(formula: string, data: DataFrame, options?: WilcoxTestOptions): WilcoxTestResult;
|
package/dist/htest/wilcoxTest.js
CHANGED
|
@@ -23,32 +23,17 @@ import { pwilcox1, qwilcox1 } from "../distributions/wilcox.js";
|
|
|
23
23
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
24
24
|
import { completePairs, dropNaN, mean, rank, tieCounts, } from "../internal/summary.js";
|
|
25
25
|
import { uniroot } from "../internal/uniroot.js";
|
|
26
|
+
import { twoSampleData } from "./testFormula.js";
|
|
26
27
|
import { matchAlternative } from "./types.js";
|
|
27
|
-
/**
|
|
28
|
-
* Wilcoxon signed rank and rank sum (Mann-Whitney) tests. Equivalent to R's `wilcox.test()`.
|
|
29
|
-
*
|
|
30
|
-
* With one sample (or `paired: true`) performs the signed rank test; with two samples the
|
|
31
|
-
* rank sum test. The choice between exact and normal-approximation p-values, the
|
|
32
|
-
* Hodges-Lehmann estimates and the confidence intervals follow R, including R's behavior
|
|
33
|
-
* when the requested confidence level cannot be reached. Only `NaN` is dropped; infinite
|
|
34
|
-
* values are kept and ranked (as R actually does).
|
|
35
|
-
*
|
|
36
|
-
* @param x Sample.
|
|
37
|
-
* @param y Second sample for a two-sample or paired test. May be omitted, and the options
|
|
38
|
-
* passed in its place: `wilcoxTest(x, { mu: 1 })`.
|
|
39
|
-
* @param options See {@link WilcoxTestOptions}.
|
|
40
|
-
* @returns The test result; see {@link WilcoxTestResult}.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* const x = [1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30];
|
|
45
|
-
* const y = [0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29];
|
|
46
|
-
* const r = wilcoxTest(x, y, { paired: true, alternative: "greater" });
|
|
47
|
-
* r.statistic; // 40 (V)
|
|
48
|
-
* r.pValue; // 0.01953125
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
28
|
export function wilcoxTest(x, y, options = {}) {
|
|
29
|
+
if (typeof x === "string") {
|
|
30
|
+
if (options.paired !== undefined)
|
|
31
|
+
throw new Error("cannot use 'paired' in formula method");
|
|
32
|
+
const d = twoSampleData(x, y, true);
|
|
33
|
+
if (d.kind === "one")
|
|
34
|
+
return wilcoxTest(d.x, options);
|
|
35
|
+
return wilcoxTest(d.x, d.y, d.kind === "paired" ? { ...options, paired: true } : options);
|
|
36
|
+
}
|
|
52
37
|
// Also accept the form wilcoxTest(x, { mu: 1 })
|
|
53
38
|
let ys;
|
|
54
39
|
if (y !== undefined && !ArrayBuffer.isView(y) && !Array.isArray(y))
|
package/dist/linalg/ops.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Sample } from "../htest/types.js";
|
|
2
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
2
3
|
/** A vector (plain array) or a matrix (array of rows). */
|
|
3
4
|
export type Operand = number | Sample | readonly (readonly number[])[];
|
|
4
5
|
type Matrix = number[][];
|
|
@@ -45,10 +46,20 @@ export interface DiagOptions {
|
|
|
45
46
|
* ```
|
|
46
47
|
*/
|
|
47
48
|
export declare function diag(x: Operand, options?: DiagOptions): number[] | Matrix;
|
|
48
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Combine vectors and matrices as columns. Equivalent to R's `cbind()` (vectors recycled to the number of rows).
|
|
51
|
+
* Given data frames (objects of columns), puts their columns side by side, as R's `cbind()` of data frames: all must
|
|
52
|
+
* have the same number of rows, and a column name may occur only once.
|
|
53
|
+
*/
|
|
49
54
|
export declare function cbind(...args: Operand[]): Matrix;
|
|
50
|
-
|
|
55
|
+
export declare function cbind(...frames: DataFrame[]): DataFrame;
|
|
56
|
+
/**
|
|
57
|
+
* Combine vectors and matrices as rows. Equivalent to R's `rbind()`. Given data frames (objects of columns), stacks
|
|
58
|
+
* their rows, as R's `rbind()` of data frames: all must have the same column names, matched by name (the result has
|
|
59
|
+
* the first frame's column order).
|
|
60
|
+
*/
|
|
51
61
|
export declare function rbind(...args: Operand[]): Matrix;
|
|
62
|
+
export declare function rbind(...frames: DataFrame[]): DataFrame;
|
|
52
63
|
/** Options for {@link matrix}. */
|
|
53
64
|
export interface MatrixOptions {
|
|
54
65
|
nrow?: number;
|
package/dist/linalg/ops.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Matrix arithmetic and construction: R's %*% (matMul), element-wise +, -, *, / with R's recycling (add, subtract,
|
|
2
|
-
// multiply, divide), outer, t, diag, cbind, rbind
|
|
2
|
+
// multiply, divide), outer, t, diag, cbind, rbind (also of data frames: src/descriptive/dataFrames.ts), matrix,
|
|
3
|
+
// kronecker and seq.
|
|
3
4
|
//
|
|
4
5
|
// Written for rStats.js (no ported code). Matrices are arrays of rows, as everywhere in rStats.js; a plain array is a
|
|
5
6
|
// vector, which R treats as a row or a column as the operation needs. Element-wise operations recycle a shorter
|
|
6
7
|
// vector over a matrix in column-major order (R's storage order), as R does.
|
|
8
|
+
import { cbindFrames, rbindFrames } from "../descriptive/dataFrames.js";
|
|
7
9
|
function isMatrix(x) {
|
|
8
10
|
return typeof x !== "number" && x.length > 0 && Array.isArray(x[0]);
|
|
9
11
|
}
|
|
@@ -198,13 +200,26 @@ function bindParts(args, byColumn) {
|
|
|
198
200
|
});
|
|
199
201
|
return parts.flatMap((p) => p.map((v) => Array.from({ length: len }, (_, i) => v[i % v.length])));
|
|
200
202
|
}
|
|
201
|
-
/**
|
|
203
|
+
/** A data frame (object of columns) rather than a number, vector or matrix */
|
|
204
|
+
function isDataFrame(x) {
|
|
205
|
+
return typeof x === "object" && !Array.isArray(x) && !ArrayBuffer.isView(x);
|
|
206
|
+
}
|
|
207
|
+
/** The data frames, if all arguments are data frames (and there is at least one); a mix is an error */
|
|
208
|
+
function framesOf(args) {
|
|
209
|
+
const frames = args.filter(isDataFrame);
|
|
210
|
+
if (frames.length === 0)
|
|
211
|
+
return undefined;
|
|
212
|
+
if (frames.length < args.length)
|
|
213
|
+
throw new TypeError("cannot combine data frames with vectors or matrices");
|
|
214
|
+
return frames;
|
|
215
|
+
}
|
|
202
216
|
export function cbind(...args) {
|
|
203
|
-
|
|
217
|
+
const frames = framesOf(args);
|
|
218
|
+
return frames === undefined ? t(bindParts(args, true)) : cbindFrames(frames);
|
|
204
219
|
}
|
|
205
|
-
/** Combine vectors and matrices as rows. Equivalent to R's `rbind()`. */
|
|
206
220
|
export function rbind(...args) {
|
|
207
|
-
|
|
221
|
+
const frames = framesOf(args);
|
|
222
|
+
return frames === undefined ? bindParts(args, false) : rbindFrames(frames);
|
|
208
223
|
}
|
|
209
224
|
/**
|
|
210
225
|
* Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`: filled by columns unless `byrow`.
|
package/dist/linalg.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{A as Rt,B as Ft,C as rt,D as et,a as A,b as tt,c as S,d as P,e as q,f as V,g as U,h as X,i as gt,j as Mt,k as bt,l as vt,m as kt,n as yt,o as z,p as xt,q as qt,r as j,s as H,t as D,u as nt,v as _,w as Nt,x as Et,y as B,z as At}from"./chunks/chunk-EWC6I75B.js";import{u as ot}from"./chunks/chunk-AECGWZHD.js";import"./chunks/chunk-LIMZKONM.js";import"./chunks/chunk-2XKG3DX4.js";function St(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let f=t.nrow,c=A(t.ncol,e.ncol);for(let i=0;i<e.ncol;i++)for(let l=0;l<t.ncol;l++){if(n===void 0&&l>i)continue;let s=0;for(let a=0;a<f;a++)s+=t.data[a+l*f]*e.data[a+i*f];c.data[l+i*t.ncol]=s}return n===void 0&&at(c),q(c)}function It(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let f=A(t.nrow,e.nrow);for(let c=0;c<e.nrow;c++)for(let i=0;i<t.nrow;i++){if(n===void 0&&i>c)continue;let l=0;for(let s=0;s<t.ncol;s++)l+=t.data[i+s*t.nrow]*e.data[c+s*e.nrow];f.data[i+c*t.nrow]=l}return n===void 0&&at(f),q(f)}function at(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function Ot(r,n,t={}){return st(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function Pt(r,n,t={}){return st(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function st(r,n,t,e,f){let c=S(r),i=P(n),l=t??c.ncol;if(!Number.isInteger(l)||l<1||l>c.nrow||l>c.ncol||l>i.nrow)throw new RangeError(`invalid k (got ${l})`);for(let a=0;a<l;a++)if(c.data[a+a*c.nrow]===0)throw new Error(`singular matrix: diagonal element ${a+1} is zero`);let s=A(l,i.ncol);for(let a=0;a<i.ncol;a++)for(let u=0;u<l;u++)s.data[u+a*l]=i.data[u+a*i.nrow];return jt(c,l,s,e,f),tt(n)?q(s):Array.from(s.data)}function jt(r,n,t,e,f){let c=r.nrow,i=r.data,l=t.nrow,s=t.data;for(let a=0;a<t.ncol;a++){let u=a*l;if(!f&&e){for(let h=n-1;h>=0;h--)if(s[u+h]!==0){s[u+h]/=i[h+h*c];let m=s[u+h];for(let o=0;o<h;o++)s[u+o]-=m*i[o+h*c]}}else if(f)if(e)for(let h=0;h<n;h++){let m=s[u+h];for(let o=0;o<h;o++)m-=i[o+h*c]*s[u+o];s[u+h]=m/i[h+h*c]}else for(let h=n-1;h>=0;h--){let m=s[u+h];for(let o=h+1;o<n;o++)m-=i[o+h*c]*s[u+o];s[u+h]=m/i[h+h*c]}else for(let h=0;h<n;h++)if(s[u+h]!==0){s[u+h]/=i[h+h*c];let m=s[u+h];for(let o=h+1;o<n;o++)s[u+o]-=m*i[o+h*c]}}}function _t(r,n={}){let t=n.norm===void 0?n.exact?"2":"O":Tt(n.norm);if(n.exact){if(t==="2"){let c=j(r,{nu:0,nv:0}).d,i=Math.min(...c);return i===0?1/0:Math.max(...c)/i}return H(r,{type:t})*H(_(r),{type:t})}if(t!=="O"&&t!=="I")throw new RangeError(`norm must be "1", "O" or "I" when exact is false (got "${n.norm}")`);if((n.method??"qr")==="direct")return n.triangular?1/ct(S(r),(n.uplo??"U")==="U",t):1/B(r,{norm:t});let e=S(r),f=e.nrow<e.ncol?q(V(e)):r;return 1/ct(S(z(X(f))),!0,t)}function ct(r,n,t){return $t(t==="I"?V(r):r,n)}function Tt(r){let n=r.toUpperCase();if(n==="1"||n==="O")return"O";if(n==="E")return"F";if(n==="I"||n==="F"||n==="M"||n==="2")return n;throw new RangeError(`invalid norm "${r}"`)}function $t(r,n){let t=Math.min(r.nrow,r.ncol),e=r.nrow,f=(m,o)=>r.data[m+o*e],c=!n,i=0;for(let m=0;m<t;m++){let[o,w]=c?[m,t-m]:[0,m+1],p=0;for(let M=o;M<o+w;M++)p+=Math.abs(f(M,m));i=Math.max(i,p)}let l=1,s=new Float64Array(t),a=m=>{for(let o=0;o<t;o++)s[o]*=m};for(let m=0;m<t;m++){let o=c?t-1-m:m,w=f(o,o);if(s[o]!==0&&(l=s[o]>0?-Math.abs(l):Math.abs(l)),Math.abs(l-s[o])>Math.abs(w)){let b=Math.abs(w)/Math.abs(l-s[o]);a(b),l*=b}let p=l-s[o],M=-l-s[o],d=Math.abs(p),g=Math.abs(M);if(w!==0?(p/=w,M/=w):(p=1,M=1),m!==t-1){let[b,v]=c?[0,o-1]:[o+1,t-1];for(let k=b;k<=v;k++)g+=Math.abs(s[k]+M*f(o,k)),s[k]+=p*f(o,k),d+=Math.abs(s[k]);if(d<g){let k=M-p;p=M;for(let y=b;y<=v;y++)s[y]+=k*f(o,y)}}s[o]=p}a(1/lt(s));let u=1;for(let m=0;m<t;m++){let o=c?m:t-1-m,w=f(o,o);if(Math.abs(s[o])>Math.abs(w)){let p=Math.abs(w)/Math.abs(s[o]);a(p),u*=p}if(w!==0?s[o]/=w:s[o]=1,m<t-1){let p=-s[o];if(c)for(let M=o+1;M<t;M++)s[M]+=p*f(M,o);else for(let M=0;M<o;M++)s[M]+=p*f(M,o)}}let h=1/lt(s);return a(h),u*=h,i!==0?u/i:0}function lt(r){let n=0;for(let t of r)n+=Math.abs(t);return n}function Lt(r,n={}){let t=n.tol??Math.sqrt(Number.EPSILON),e=S(r),{d:f,u:c,v:i}=j(r),l=e.nrow,s=e.ncol,a=A(s,l),u=Math.max(t*(f[0]??0),0);for(let h=0;h<f.length;h++){if(!(f[h]>u))continue;let m=1/f[h];for(let o=0;o<l;o++){let w=c[o][h]*m;for(let p=0;p<s;p++)a.data[p+o*s]+=i[p][h]*w}}return q(a)}function Ut(r){let n=S(r);U(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let o of n.data)if(!Number.isFinite(o))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},f=A(t,t),c=new Float64Array(t),i=new Float64Array(t);rt(e,f),et(e,f,c,i,!0);let l=e.data,s=f.data,a=(o,w)=>o+w*t,u=new Uint8Array(t);for(let o=0;o+1<t;o++)i[o]>0&&i[o+1]<0&&(u[o++]=1);for(let o=0;o<t;o++)for(let w=o+1;w<t;w++)w===o+1&&u[o]||(l[a(w,o)]=0);let h=new Array(t),m=new Array(t);for(let o=0;o<t;o++){if(!u[o]){h[o]=l[a(o,o)],m[o]=0;continue}let w=Ht(l[a(o,o)],l[a(o,o+1)],l[a(o+1,o)],l[a(o+1,o+1)]);l[a(o,o)]=w.a,l[a(o,o+1)]=w.b,l[a(o+1,o)]=w.c,l[a(o+1,o+1)]=w.d;let{cs:p,sn:M}=w;for(let d=o+2;d<t;d++){let g=l[a(o,d)],b=l[a(o+1,d)];l[a(o,d)]=p*g+M*b,l[a(o+1,d)]=p*b-M*g}for(let d=0;d<o;d++){let g=l[a(d,o)],b=l[a(d,o+1)];l[a(d,o)]=p*g+M*b,l[a(d,o+1)]=p*b-M*g}for(let d=0;d<t;d++){let g=s[a(d,o)],b=s[a(d,o+1)];s[a(d,o)]=p*g+M*b,s[a(d,o+1)]=p*b-M*g}h[o]=w.rt1r,m[o]=w.rt1i,h[o+1]=w.rt2r,m[o+1]=w.rt2i,o++}return{T:q(e),Q:q(f),valuesRe:h,valuesIm:m}}var O=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function Ht(r,n,t,e){let f=22250738585072014e-324,c=Number.EPSILON,i=4,l=2**Math.trunc(Math.log(f/c)/Math.log(2)/2),s=1/l,a,u;if(t===0)a=1,u=0;else if(n===0){a=0,u=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&O(1,n)!==O(1,t))a=1,u=0;else{let m=r-e,o=.5*m,w=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*O(1,n)*O(1,t),M=Math.max(Math.abs(o),w),d=o/M*o+w/M*p;if(d>=i*c){d=o+O(Math.sqrt(M)*Math.sqrt(d),o),r=e+d,e=e-w/d*p;let g=Math.hypot(t,d);a=d/g,u=t/g,n=n-t,t=0}else{let g=0,b=n+t;for(;g++,M=Math.max(Math.abs(m),Math.abs(b)),!(!(M>=s&&(b*=l,m*=l,g<=20))&&!(M<=l&&(b*=s,m*=s,g<=20))););o=.5*m;let v=Math.hypot(b,m);a=Math.sqrt(.5*(1+Math.abs(b)/v)),u=-(o/(v*a))*O(1,b);let k=r*a+n*u,y=-r*u+n*a,E=t*a+e*u,L=-t*u+e*a;if(r=k*a+E*u,n=y*a+L*u,t=-k*u+E*a,e=-y*u+L*a,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(O(1,n)===O(1,t)){let x=Math.sqrt(Math.abs(n)),W=Math.sqrt(Math.abs(t));o=O(x*W,t),v=1/Math.sqrt(Math.abs(n+t)),r=m+o,e=m-o,n=n-t,t=0;let Y=x*v,Z=W*v;m=a*Y-u*Z,u=a*Z+u*Y,a=m}}else n=-t,t=0,m=a,a=-u,u=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:a,sn:u,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Ct=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Qt(r,n={}){let t=n.method??"Higham08.b";if(!Ct.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let f=S(r);U(f);let c=f.nrow;if(c===0)return[];if(c===1)return[[Math.exp(f.data[0])]];if(t==="Taylor"||t==="TaylorO")return q(Dt(f,e));if(t==="Pade"||t==="PadeO")return q(Bt(f,e));let i,l;t==="Higham08.b"&&(i=ut("P",f),l=ut("S",f));let s=zt(f),a=s.data,u=(h,m)=>h+m*c;if(l){let h=l.scale;for(let m=0;m<c;m++)for(let o=0;o<c;o++)a[u(o,m)]=a[u(o,m)]*h[o]/h[m]}if(i){let h=i.scale,m=o=>{let w=h[o];if(w!==o){for(let p=0;p<c;p++)[a[u(p,o)],a[u(p,w)]]=[a[u(p,w)],a[u(p,o)]];for(let p=0;p<c;p++)[a[u(o,p)],a[u(w,p)]]=[a[u(w,p)],a[u(o,p)]]}};for(let o=i.ilo-1;o>=0;o--)m(o);for(let o=i.ihi+1;o<c;o++)m(o)}return q(s)}var Vt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Xt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function zt(r){let n=r.nrow,t=D(r),e=G(n),f,c,i=0;if(t<=2.1){let u=t<=.015?3:t<=.25?5:t<=.95?7:9,h=Vt[u],m=F(r,r),o=C([[h[1],e]]),w=C([[h[0],e]]),p=m;for(let M=1;M<=(u-1)/2;M++)M>1&&(p=F(p,m)),T(o,h[2*M+1],p),T(w,h[2*M],p);f=F(r,o),c=w}else{i=Math.max(0,Math.ceil(Math.log2(t/5.4)));let u=$(r,1/2**i),h=F(u,u),m=F(h,h),o=F(h,m),w=Xt,p=C([[w[13],o],[w[11],m],[w[9],h]]),M=F(o,p);for(let[g,b]of[[7,o],[5,m],[3,h],[1,e]])T(M,w[g],b);f=F(u,M);let d=C([[w[12],o],[w[10],m],[w[8],h]]);c=F(o,d);for(let[g,b]of[[6,o],[4,m],[2,h],[0,e]])T(c,w[g],b)}let l=A(n,n),s=A(n,n);for(let u=0;u<n*n;u++)l.data[u]=c.data[u]-f.data[u],s.data[u]=c.data[u]+f.data[u];let a=S(_(q(l),q(s)));for(let u=0;u<i;u++)a=F(a,a);return a}function mt(r){let n=Math.sqrt(D(r)*nt(r)),t=1;for(;2**t<n;)t++;return t+4}function ht(r,n){for(let t=0;t<n;t++)r=F(r,r);return r}function dt(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Dt(r,n){let t=mt(r),e=2**t,f=G(r.nrow);for(let c=n;c>=1;c--)f=$(F(f,r),1/(c*e)),dt(f);return ht(f,t)}function it(r,n,t,e){let f=e===1?r:$(r,-1),c=G(r.nrow);for(let i=n;i>=1;i--){let l=(n-i+1)/(i*(2*n-i+1)*t);c=$(F(c,f),l),dt(c)}return c}function Bt(r,n){let t=r.nrow,e=mt(r),f=2**e,c=it(r,n,f,1),i=it(r,n,f,-1),l=A(t,t);for(let s=0;s<t;s++)l.data.set(Gt(i,c.data.subarray(s*t,(s+1)*t)),s*t);return ht(l,e)}function Gt(r,n){let t=r.nrow,e=1e-100,f=w=>{let p=new Float64Array(t);for(let M=0;M<t;M++){let d=0;for(let g=0;g<t;g++)d+=r.data[M+g*t]*w[g];p[M]=d}return p},c=(w,p)=>{let M=0;for(let d=0;d<t;d++)M+=w[d]*p[d];return M},i=(w,p,M)=>{let d=new Float64Array(t);for(let g=0;g<t;g++)d[g]=w[g]+p*M[g];return d},l=new Float64Array(t),s=Float64Array.from(n),a=Float64Array.from(n),u=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(c(s,s)),o=c(h,n);if(Math.abs(o)<=e)return l;for(let w=0;w<t;w++){let p=f(a),M=c(h,p);if(Math.abs(M)<=e)return l;let d=o/M;if(Math.abs(d)<=e)return l;let g=i(u,-d,p);p=i(u,1,g);let b=f(p);s=i(s,-d,b);let v=Math.sqrt(c(s,s));if(l=i(l,d,p),Math.abs(v/m)<=1e-30)return l;let k=c(h,s);if(Math.abs(k)<=e)return l;let y=k/o;o=k,u=i(s,y,g),b=i(g,y,a),a=i(u,y,b)}return l}function G(r){let n=A(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function F(r,n){let t=r.nrow,e=r.ncol,f=n.ncol,c=A(t,f);for(let i=0;i<f;i++)for(let l=0;l<t;l++){let s=0;for(let a=0;a<e;a++)s+=r.data[l+a*t]*n.data[a+i*e];c.data[l+i*t]=s}return c}function $(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function T(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function C(r){let[n,t]=r[0],e=$(t,n);for(let f=1;f<r.length;f++)T(e,r[f][0],r[f][1]);return e}function ft(r,n,t,e){let f=0,c=1;for(let i=0;i<t;i++){let l=r[n+i*e];if(l===0)continue;let s=Math.abs(l);f<s?(c=1+c*(f/s)**2,f=s):c+=(s/f)**2}return f*Math.sqrt(c)}function ut(r,n){let t=n.nrow,e=n.data,f=(d,g)=>d+g*t,c=new Float64Array(t),i=(d,g,b)=>{for(let v=0;v<b;v++)[e[f(v,d)],e[f(v,g)]]=[e[f(v,g)],e[f(v,d)]]},l=(d,g,b)=>{for(let v=b;v<t;v++)[e[f(d,v)],e[f(g,v)]]=[e[f(g,v)],e[f(d,v)]]},s=0,a=t-1;if(r==="P"){let d=!0;for(;d;){d=!1;for(let g=a;g>=0;g--){let b=!0;for(let v=0;v<=a;v++)if(g!==v&&e[f(g,v)]!==0){b=!1;break}if(b){if(c[a]=g,g!==a&&(i(g,a,a+1),l(g,a,s)),d=!0,a===0)return{ilo:0,ihi:0,scale:c};a--}}}for(d=!0;d;){d=!1;for(let g=s,b=a;g<=b;g++){let v=!0;for(let k=s;k<=a;k++)if(k!==g&&e[f(k,g)]!==0){v=!1;break}v&&(c[s]=g,g!==s&&(i(g,s,a+1),l(g,s,s)),d=!0,s++)}}}for(let d=s;d<=a;d++)c[d]=1;if(r==="P")return{ilo:s,ihi:a,scale:c};let u=2,h=.95,m=2**-1022/Number.EPSILON,o=1/m,w=m*u,p=1/w,M=!0;for(;M;){M=!1;for(let d=s;d<=a;d++){let g=ft(e,f(s,d),a-s+1,1),b=ft(e,f(d,s),a-s+1,t),v=0;for(let x=0;x<=a;x++)v=Math.max(v,Math.abs(e[f(x,d)]));let k=0;for(let x=s;x<t;x++)k=Math.max(k,Math.abs(e[f(d,x)]));if(g===0||b===0)continue;if(Number.isNaN(g+v+b+k))throw new RangeError("the matrix contains NaN or infinite values");let y=b/u,E=1,L=g+b;for(;g<y&&Math.max(E,g,v)<p&&Math.min(b,y,k)>w;)E*=u,g*=u,v*=u,b/=u,y/=u,k/=u;for(y=g/u;y>=b&&Math.max(b,k)<p&&Math.min(E,g,y,v)>w;)E/=u,g/=u,y/=u,v/=u,b*=u,k*=u;if(!(g+b>=h*L)&&!(E<1&&c[d]<1&&E*c[d]<=m)&&!(E>1&&c[d]>1&&c[d]>=o/E)){y=1/E,c[d]=c[d]*E,M=!0;for(let x=s;x<t;x++)e[f(d,x)]=e[f(d,x)]*y;for(let x=0;x<=a;x++)e[f(x,d)]=e[f(x,d)]*E}}}return{ilo:s,ihi:a,scale:c}}function R(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function N(r){return typeof r=="number"?[r]:Array.from(r)}function I(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function J(r,n){let t,e;if(R(r)&&R(n))t=r.map(i=>[...i]),e=n.map(i=>[...i]);else if(R(r)){let i=N(n),l=I(r);t=r.map(s=>[...s]),e=l===i.length?i.map(s=>[s]):l===1?[i]:[]}else if(R(n)){let i=N(r);e=n.map(l=>[...l]),t=n.length===i.length?[i]:n.length===1?i.map(l=>[l]):[]}else{let i=N(r),l=N(n);if(i.length===l.length)t=[i],e=l.map(s=>[s]);else if(i.length===1)t=[i],e=[l];else if(l.length===1)t=i.map(s=>[s]),e=[l];else throw new RangeError("non-conformable arguments")}let f=t[0]?.length??0;if(t.length===0||e.length!==f)throw new RangeError("non-conformable arguments");I(t);let c=I(e);return t.map(i=>{let l=new Array(c).fill(0);for(let s=0;s<c;s++){let a=0;for(let u=0;u<f;u++)a+=i[u]*e[u][s];l[s]=a}return l})}function Q(r,n,t){let e=R(r),f=R(n);if(e&&f){let s=I(r);if(r.length!==n.length||s!==I(n))throw new RangeError("non-conformable arrays");return r.map((a,u)=>a.map((h,m)=>t(h,n[u][m])))}if(e||f){let s=e?r:n,a=N(e?n:r),u=s.length,h=I(s);if(a.length===0)return s.map(m=>m.map(()=>NaN));if(a.length>u*h)throw new RangeError("dims [product "+u*h+"] do not match the length of object ["+a.length+"]");return s.map((m,o)=>m.map((w,p)=>e?t(w,a[(o+p*u)%a.length]):t(a[(o+p*u)%a.length],w)))}let c=N(r),i=N(n);if(c.length===0||i.length===0)return[];let l=Math.max(c.length,i.length);return Array.from({length:l},(s,a)=>t(c[a%c.length],i[a%i.length]))}function Jt(r,n){return Q(r,n,(t,e)=>t+e)}function Kt(r,n){return Q(r,n,(t,e)=>t-e)}function Wt(r,n){return Q(r,n,(t,e)=>t*e)}function Yt(r,n){return Q(r,n,(t,e)=>t/e)}var Zt={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function tn(r,n,t="*"){let e=typeof t=="function"?t:Zt[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let f=Array.from(r),c=Array.from(n);return f.map(i=>c.map(l=>e(i,l)))}function K(r){if(!R(r))return[N(r)];let n=I(r);return Array.from({length:n},(t,e)=>r.map(f=>f[e]))}function nn(r,n={}){if(R(r)){let l=I(r);return Array.from({length:Math.min(r.length,l)},(s,a)=>r[a][a])}let t=N(r),e,f;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(f=Math.trunc(t[0]),!(f>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,f=n.nrow??t.length;let c=n.ncol??f,i=Array.from({length:f},()=>new Array(c).fill(0));for(let l=0;l<Math.min(f,c);l++)i[l][l]=e[l%e.length];return f===0?[]:i}function pt(r,n){let t=r.map(c=>R(c)?n?K(c):c.map(i=>[...i]):[N(c)]),e=r.filter(R),f=e.length>0?n?e[0].length:I(e[0]):Math.max(...t.map(c=>c[0].length));return e.forEach((c,i)=>{if((n?c.length:I(c))!==f)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${i+2})`)}),t.flatMap(c=>c.map(i=>Array.from({length:f},(l,s)=>i[s%i.length])))}function rn(...r){return K(pt(r,!0))}function en(...r){return pt(r,!1)}function on(r,n={}){let t=N(r),e=t.length,{nrow:f,ncol:c}=n;f===void 0&&c===void 0?(f=e,c=1):f===void 0?f=Math.ceil(e/c):c===void 0&&(c=Math.ceil(e/f));let i=n.byrow??!1;return Array.from({length:f},(l,s)=>Array.from({length:c},(a,u)=>e===0?NaN:t[(i?s*c+u:s+u*f)%e]))}function an(r,n){if(!R(r)&&!R(n)){let c=N(r),i=N(n);return c.flatMap(l=>i.map(s=>l*s))}let t=R(r)?r:N(r).map(c=>[c]),e=R(n)?n:N(n).map(c=>[c]),f=[];for(let c of t)for(let i of e)f.push(c.flatMap(l=>i.map(s=>l*s)));return f}function sn(r,n,t={}){let{by:e,lengthOut:f}=t;if(n===void 0&&e===void 0&&f===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return wt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(f===void 0){if(e===void 0)return wt(r??1,n??1);let i=r??1,l=n??1,s=l-i;if(s===0&&l===0)return[l];let a=s/e;if(!Number.isFinite(a)){if(e===0&&s===0)return[i];throw new RangeError("invalid '(to - from)/by'")}if(a<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(s)/Math.max(Math.abs(l),Math.abs(i))<100*Number.EPSILON)return[i];let u=Math.trunc(a+1e-10);return Array.from({length:u+1},(h,m)=>e>0?Math.min(i+m*e,l):Math.max(i+m*e,l))}if(!Number.isFinite(f)||f<0)throw new RangeError("'length.out' must be a non-negative number");let c=Math.ceil(f);if(c===0)return[];if(e===void 0){let i=n??(r??1)+(c-1),l=r??i-(c-1);if(c<=2)return[l,i].slice(0,c);if(l===i)return new Array(c).fill(l);let s=(i-l)/(c-1);return Array.from({length:c},(a,u)=>u===0?l:u===c-1?i:l+u*s)}if(n===void 0){let i=r??1;return Array.from({length:c},(l,s)=>i+s*e)}if(r===void 0)return Array.from({length:c},(i,l)=>n-(c-1-l)*e);throw new RangeError("too many arguments")}function wt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(f,c)=>r+e*c)}var cn=22250738585072014e-324;function ln(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(a=>{if(a.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(a)}),f=Array.from({length:n},(a,u)=>u),c=Math.min(n,t);for(let a=0;a<c;a++){let u=a,h=Math.abs(e[a][a]);for(let w=a+1;w<n;w++){let p=Math.abs(e[w][a]);(p>h||Number.isNaN(h))&&(h=p,u=w)}u!==a&&([e[u],e[a]]=[e[a],e[u]],[f[u],f[a]]=[f[a],f[u]]);let m=e[a][a];if(m===0)continue;let o=Math.abs(m)>=cn?1/m:NaN;for(let w=a+1;w<n;w++){let p=Number.isNaN(o)?e[w][a]/m:e[w][a]*o;e[w][a]=p;for(let M=a+1;M<t;M++)e[w][M]=e[w][M]-p*e[a][M]}}let i=Array.from({length:n},(a,u)=>Array.from({length:c},(h,m)=>u===m?1:u>m?e[u][m]:0)),l=Array.from({length:c},(a,u)=>Array.from({length:t},(h,m)=>m>=u?e[u][m]:0)),s=Array.from({length:n},()=>new Array(n).fill(0));return f.forEach((a,u)=>s[a][u]=1),{P:s,L:i,U:l}}function fn(r,n={}){let t=r.map(s=>Array.from(s)),e=ot(t,{center:n.center??!0,scale:n.scale??!1});if(e.scale?.some(s=>s===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let f=j(e.scaled,{nu:0}),c=t.length,i=f.v,l={sdev:f.d.map(s=>s/Math.sqrt(Math.max(1,c-1))),rotation:i,x:J(e.scaled,i)};return e.center!==void 0&&(l.center=e.center),e.scale!==void 0&&(l.scale=e.scale),l}export{Jt as add,Ot as backsolve,rn as cbind,At as chol,Rt as chol2inv,St as crossprod,Et as det,Nt as determinant,nn as diag,Yt as divide,Ft as eigen,Qt as expm,Pt as forwardsolve,Lt as ginv,_t as kappa,an as kronecker,ln as lu,J as matMul,on as matrix,Wt as multiply,H as norm,tn as outer,fn as prcomp,X as qr,bt as qrCoef,kt as qrFitted,yt as qrQ,gt as qrQty,Mt as qrQy,z as qrR,vt as qrResid,qt as qrSolve,xt as qrX,en as rbind,B as rcond,Ut as schur,sn as seq,_ as solve,Kt as subtract,j as svd,K as t,It as tcrossprod};
|
|
1
|
+
/*! rStats.js 0.11.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{A as Ot,B as It,C as rt,D as et,a as A,b as tt,c as S,d as P,e as x,f as D,g as U,h as Q,i as vt,j as yt,k as kt,l as qt,m as xt,n as Et,o as X,p as Nt,q as At,r as j,s as H,t as z,u as nt,v as _,w as Rt,x as Ft,y as B,z as St}from"./chunks/chunk-7GQMKH3Z.js";import{F as st,h as ot,i as at}from"./chunks/chunk-CGBNKHNZ.js";import"./chunks/chunk-ALM3VHFP.js";import"./chunks/chunk-Y5PYRAYP.js";import"./chunks/chunk-EZWBRCJL.js";function Pt(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let f=t.nrow,c=A(t.ncol,e.ncol);for(let l=0;l<e.ncol;l++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>l)continue;let s=0;for(let a=0;a<f;a++)s+=t.data[a+i*f]*e.data[a+l*f];c.data[i+l*t.ncol]=s}return n===void 0&&ct(c),x(c)}function jt(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let f=A(t.nrow,e.nrow);for(let c=0;c<e.nrow;c++)for(let l=0;l<t.nrow;l++){if(n===void 0&&l>c)continue;let i=0;for(let s=0;s<t.ncol;s++)i+=t.data[l+s*t.nrow]*e.data[c+s*e.nrow];f.data[l+c*t.nrow]=i}return n===void 0&&ct(f),x(f)}function ct(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function _t(r,n,t={}){return it(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function Tt(r,n,t={}){return it(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function it(r,n,t,e,f){let c=S(r),l=P(n),i=t??c.ncol;if(!Number.isInteger(i)||i<1||i>c.nrow||i>c.ncol||i>l.nrow)throw new RangeError(`invalid k (got ${i})`);for(let a=0;a<i;a++)if(c.data[a+a*c.nrow]===0)throw new Error(`singular matrix: diagonal element ${a+1} is zero`);let s=A(i,l.ncol);for(let a=0;a<l.ncol;a++)for(let u=0;u<i;u++)s.data[u+a*i]=l.data[u+a*l.nrow];return $t(c,i,s,e,f),tt(n)?x(s):Array.from(s.data)}function $t(r,n,t,e,f){let c=r.nrow,l=r.data,i=t.nrow,s=t.data;for(let a=0;a<t.ncol;a++){let u=a*i;if(!f&&e){for(let h=n-1;h>=0;h--)if(s[u+h]!==0){s[u+h]/=l[h+h*c];let m=s[u+h];for(let o=0;o<h;o++)s[u+o]-=m*l[o+h*c]}}else if(f)if(e)for(let h=0;h<n;h++){let m=s[u+h];for(let o=0;o<h;o++)m-=l[o+h*c]*s[u+o];s[u+h]=m/l[h+h*c]}else for(let h=n-1;h>=0;h--){let m=s[u+h];for(let o=h+1;o<n;o++)m-=l[o+h*c]*s[u+o];s[u+h]=m/l[h+h*c]}else for(let h=0;h<n;h++)if(s[u+h]!==0){s[u+h]/=l[h+h*c];let m=s[u+h];for(let o=h+1;o<n;o++)s[u+o]-=m*l[o+h*c]}}}function Lt(r,n={}){let t=n.norm===void 0?n.exact?"2":"O":Ut(n.norm);if(n.exact){if(t==="2"){let c=j(r,{nu:0,nv:0}).d,l=Math.min(...c);return l===0?1/0:Math.max(...c)/l}return H(r,{type:t})*H(_(r),{type:t})}if(t!=="O"&&t!=="I")throw new RangeError(`norm must be "1", "O" or "I" when exact is false (got "${n.norm}")`);if((n.method??"qr")==="direct")return n.triangular?1/lt(S(r),(n.uplo??"U")==="U",t):1/B(r,{norm:t});let e=S(r),f=e.nrow<e.ncol?x(D(e)):r;return 1/lt(S(X(Q(f))),!0,t)}function lt(r,n,t){return Ht(t==="I"?D(r):r,n)}function Ut(r){let n=r.toUpperCase();if(n==="1"||n==="O")return"O";if(n==="E")return"F";if(n==="I"||n==="F"||n==="M"||n==="2")return n;throw new RangeError(`invalid norm "${r}"`)}function Ht(r,n){let t=Math.min(r.nrow,r.ncol),e=r.nrow,f=(m,o)=>r.data[m+o*e],c=!n,l=0;for(let m=0;m<t;m++){let[o,w]=c?[m,t-m]:[0,m+1],p=0;for(let M=o;M<o+w;M++)p+=Math.abs(f(M,m));l=Math.max(l,p)}let i=1,s=new Float64Array(t),a=m=>{for(let o=0;o<t;o++)s[o]*=m};for(let m=0;m<t;m++){let o=c?t-1-m:m,w=f(o,o);if(s[o]!==0&&(i=s[o]>0?-Math.abs(i):Math.abs(i)),Math.abs(i-s[o])>Math.abs(w)){let b=Math.abs(w)/Math.abs(i-s[o]);a(b),i*=b}let p=i-s[o],M=-i-s[o],d=Math.abs(p),g=Math.abs(M);if(w!==0?(p/=w,M/=w):(p=1,M=1),m!==t-1){let[b,v]=c?[0,o-1]:[o+1,t-1];for(let y=b;y<=v;y++)g+=Math.abs(s[y]+M*f(o,y)),s[y]+=p*f(o,y),d+=Math.abs(s[y]);if(d<g){let y=M-p;p=M;for(let k=b;k<=v;k++)s[k]+=y*f(o,k)}}s[o]=p}a(1/ft(s));let u=1;for(let m=0;m<t;m++){let o=c?m:t-1-m,w=f(o,o);if(Math.abs(s[o])>Math.abs(w)){let p=Math.abs(w)/Math.abs(s[o]);a(p),u*=p}if(w!==0?s[o]/=w:s[o]=1,m<t-1){let p=-s[o];if(c)for(let M=o+1;M<t;M++)s[M]+=p*f(M,o);else for(let M=0;M<o;M++)s[M]+=p*f(M,o)}}let h=1/ft(s);return a(h),u*=h,l!==0?u/l:0}function ft(r){let n=0;for(let t of r)n+=Math.abs(t);return n}function Ct(r,n={}){let t=n.tol??Math.sqrt(Number.EPSILON),e=S(r),{d:f,u:c,v:l}=j(r),i=e.nrow,s=e.ncol,a=A(s,i),u=Math.max(t*(f[0]??0),0);for(let h=0;h<f.length;h++){if(!(f[h]>u))continue;let m=1/f[h];for(let o=0;o<i;o++){let w=c[o][h]*m;for(let p=0;p<s;p++)a.data[p+o*s]+=l[p][h]*w}}return x(a)}function Vt(r){let n=S(r);U(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let o of n.data)if(!Number.isFinite(o))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},f=A(t,t),c=new Float64Array(t),l=new Float64Array(t);rt(e,f),et(e,f,c,l,!0);let i=e.data,s=f.data,a=(o,w)=>o+w*t,u=new Uint8Array(t);for(let o=0;o+1<t;o++)l[o]>0&&l[o+1]<0&&(u[o++]=1);for(let o=0;o<t;o++)for(let w=o+1;w<t;w++)w===o+1&&u[o]||(i[a(w,o)]=0);let h=new Array(t),m=new Array(t);for(let o=0;o<t;o++){if(!u[o]){h[o]=i[a(o,o)],m[o]=0;continue}let w=Dt(i[a(o,o)],i[a(o,o+1)],i[a(o+1,o)],i[a(o+1,o+1)]);i[a(o,o)]=w.a,i[a(o,o+1)]=w.b,i[a(o+1,o)]=w.c,i[a(o+1,o+1)]=w.d;let{cs:p,sn:M}=w;for(let d=o+2;d<t;d++){let g=i[a(o,d)],b=i[a(o+1,d)];i[a(o,d)]=p*g+M*b,i[a(o+1,d)]=p*b-M*g}for(let d=0;d<o;d++){let g=i[a(d,o)],b=i[a(d,o+1)];i[a(d,o)]=p*g+M*b,i[a(d,o+1)]=p*b-M*g}for(let d=0;d<t;d++){let g=s[a(d,o)],b=s[a(d,o+1)];s[a(d,o)]=p*g+M*b,s[a(d,o+1)]=p*b-M*g}h[o]=w.rt1r,m[o]=w.rt1i,h[o+1]=w.rt2r,m[o+1]=w.rt2i,o++}return{T:x(e),Q:x(f),valuesRe:h,valuesIm:m}}var I=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function Dt(r,n,t,e){let f=22250738585072014e-324,c=Number.EPSILON,l=4,i=2**Math.trunc(Math.log(f/c)/Math.log(2)/2),s=1/i,a,u;if(t===0)a=1,u=0;else if(n===0){a=0,u=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&I(1,n)!==I(1,t))a=1,u=0;else{let m=r-e,o=.5*m,w=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*I(1,n)*I(1,t),M=Math.max(Math.abs(o),w),d=o/M*o+w/M*p;if(d>=l*c){d=o+I(Math.sqrt(M)*Math.sqrt(d),o),r=e+d,e=e-w/d*p;let g=Math.hypot(t,d);a=d/g,u=t/g,n=n-t,t=0}else{let g=0,b=n+t;for(;g++,M=Math.max(Math.abs(m),Math.abs(b)),!(!(M>=s&&(b*=i,m*=i,g<=20))&&!(M<=i&&(b*=s,m*=s,g<=20))););o=.5*m;let v=Math.hypot(b,m);a=Math.sqrt(.5*(1+Math.abs(b)/v)),u=-(o/(v*a))*I(1,b);let y=r*a+n*u,k=-r*u+n*a,N=t*a+e*u,L=-t*u+e*a;if(r=y*a+N*u,n=k*a+L*u,t=-y*u+N*a,e=-k*u+L*a,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(I(1,n)===I(1,t)){let q=Math.sqrt(Math.abs(n)),W=Math.sqrt(Math.abs(t));o=I(q*W,t),v=1/Math.sqrt(Math.abs(n+t)),r=m+o,e=m-o,n=n-t,t=0;let Y=q*v,Z=W*v;m=a*Y-u*Z,u=a*Z+u*Y,a=m}}else n=-t,t=0,m=a,a=-u,u=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:a,sn:u,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Qt=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Xt(r,n={}){let t=n.method??"Higham08.b";if(!Qt.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let f=S(r);U(f);let c=f.nrow;if(c===0)return[];if(c===1)return[[Math.exp(f.data[0])]];if(t==="Taylor"||t==="TaylorO")return x(Jt(f,e));if(t==="Pade"||t==="PadeO")return x(Kt(f,e));let l,i;t==="Higham08.b"&&(l=ht("P",f),i=ht("S",f));let s=Gt(f),a=s.data,u=(h,m)=>h+m*c;if(i){let h=i.scale;for(let m=0;m<c;m++)for(let o=0;o<c;o++)a[u(o,m)]=a[u(o,m)]*h[o]/h[m]}if(l){let h=l.scale,m=o=>{let w=h[o];if(w!==o){for(let p=0;p<c;p++)[a[u(p,o)],a[u(p,w)]]=[a[u(p,w)],a[u(p,o)]];for(let p=0;p<c;p++)[a[u(o,p)],a[u(w,p)]]=[a[u(w,p)],a[u(o,p)]]}};for(let o=l.ilo-1;o>=0;o--)m(o);for(let o=l.ihi+1;o<c;o++)m(o)}return x(s)}var zt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Bt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function Gt(r){let n=r.nrow,t=z(r),e=G(n),f,c,l=0;if(t<=2.1){let u=t<=.015?3:t<=.25?5:t<=.95?7:9,h=zt[u],m=F(r,r),o=C([[h[1],e]]),w=C([[h[0],e]]),p=m;for(let M=1;M<=(u-1)/2;M++)M>1&&(p=F(p,m)),T(o,h[2*M+1],p),T(w,h[2*M],p);f=F(r,o),c=w}else{l=Math.max(0,Math.ceil(Math.log2(t/5.4)));let u=$(r,1/2**l),h=F(u,u),m=F(h,h),o=F(h,m),w=Bt,p=C([[w[13],o],[w[11],m],[w[9],h]]),M=F(o,p);for(let[g,b]of[[7,o],[5,m],[3,h],[1,e]])T(M,w[g],b);f=F(u,M);let d=C([[w[12],o],[w[10],m],[w[8],h]]);c=F(o,d);for(let[g,b]of[[6,o],[4,m],[2,h],[0,e]])T(c,w[g],b)}let i=A(n,n),s=A(n,n);for(let u=0;u<n*n;u++)i.data[u]=c.data[u]-f.data[u],s.data[u]=c.data[u]+f.data[u];let a=S(_(x(i),x(s)));for(let u=0;u<l;u++)a=F(a,a);return a}function dt(r){let n=Math.sqrt(z(r)*nt(r)),t=1;for(;2**t<n;)t++;return t+4}function wt(r,n){for(let t=0;t<n;t++)r=F(r,r);return r}function pt(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Jt(r,n){let t=dt(r),e=2**t,f=G(r.nrow);for(let c=n;c>=1;c--)f=$(F(f,r),1/(c*e)),pt(f);return wt(f,t)}function ut(r,n,t,e){let f=e===1?r:$(r,-1),c=G(r.nrow);for(let l=n;l>=1;l--){let i=(n-l+1)/(l*(2*n-l+1)*t);c=$(F(c,f),i),pt(c)}return c}function Kt(r,n){let t=r.nrow,e=dt(r),f=2**e,c=ut(r,n,f,1),l=ut(r,n,f,-1),i=A(t,t);for(let s=0;s<t;s++)i.data.set(Wt(l,c.data.subarray(s*t,(s+1)*t)),s*t);return wt(i,e)}function Wt(r,n){let t=r.nrow,e=1e-100,f=w=>{let p=new Float64Array(t);for(let M=0;M<t;M++){let d=0;for(let g=0;g<t;g++)d+=r.data[M+g*t]*w[g];p[M]=d}return p},c=(w,p)=>{let M=0;for(let d=0;d<t;d++)M+=w[d]*p[d];return M},l=(w,p,M)=>{let d=new Float64Array(t);for(let g=0;g<t;g++)d[g]=w[g]+p*M[g];return d},i=new Float64Array(t),s=Float64Array.from(n),a=Float64Array.from(n),u=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(c(s,s)),o=c(h,n);if(Math.abs(o)<=e)return i;for(let w=0;w<t;w++){let p=f(a),M=c(h,p);if(Math.abs(M)<=e)return i;let d=o/M;if(Math.abs(d)<=e)return i;let g=l(u,-d,p);p=l(u,1,g);let b=f(p);s=l(s,-d,b);let v=Math.sqrt(c(s,s));if(i=l(i,d,p),Math.abs(v/m)<=1e-30)return i;let y=c(h,s);if(Math.abs(y)<=e)return i;let k=y/o;o=y,u=l(s,k,g),b=l(g,k,a),a=l(u,k,b)}return i}function G(r){let n=A(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function F(r,n){let t=r.nrow,e=r.ncol,f=n.ncol,c=A(t,f);for(let l=0;l<f;l++)for(let i=0;i<t;i++){let s=0;for(let a=0;a<e;a++)s+=r.data[i+a*t]*n.data[a+l*e];c.data[i+l*t]=s}return c}function $(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function T(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function C(r){let[n,t]=r[0],e=$(t,n);for(let f=1;f<r.length;f++)T(e,r[f][0],r[f][1]);return e}function mt(r,n,t,e){let f=0,c=1;for(let l=0;l<t;l++){let i=r[n+l*e];if(i===0)continue;let s=Math.abs(i);f<s?(c=1+c*(f/s)**2,f=s):c+=(s/f)**2}return f*Math.sqrt(c)}function ht(r,n){let t=n.nrow,e=n.data,f=(d,g)=>d+g*t,c=new Float64Array(t),l=(d,g,b)=>{for(let v=0;v<b;v++)[e[f(v,d)],e[f(v,g)]]=[e[f(v,g)],e[f(v,d)]]},i=(d,g,b)=>{for(let v=b;v<t;v++)[e[f(d,v)],e[f(g,v)]]=[e[f(g,v)],e[f(d,v)]]},s=0,a=t-1;if(r==="P"){let d=!0;for(;d;){d=!1;for(let g=a;g>=0;g--){let b=!0;for(let v=0;v<=a;v++)if(g!==v&&e[f(g,v)]!==0){b=!1;break}if(b){if(c[a]=g,g!==a&&(l(g,a,a+1),i(g,a,s)),d=!0,a===0)return{ilo:0,ihi:0,scale:c};a--}}}for(d=!0;d;){d=!1;for(let g=s,b=a;g<=b;g++){let v=!0;for(let y=s;y<=a;y++)if(y!==g&&e[f(y,g)]!==0){v=!1;break}v&&(c[s]=g,g!==s&&(l(g,s,a+1),i(g,s,s)),d=!0,s++)}}}for(let d=s;d<=a;d++)c[d]=1;if(r==="P")return{ilo:s,ihi:a,scale:c};let u=2,h=.95,m=2**-1022/Number.EPSILON,o=1/m,w=m*u,p=1/w,M=!0;for(;M;){M=!1;for(let d=s;d<=a;d++){let g=mt(e,f(s,d),a-s+1,1),b=mt(e,f(d,s),a-s+1,t),v=0;for(let q=0;q<=a;q++)v=Math.max(v,Math.abs(e[f(q,d)]));let y=0;for(let q=s;q<t;q++)y=Math.max(y,Math.abs(e[f(d,q)]));if(g===0||b===0)continue;if(Number.isNaN(g+v+b+y))throw new RangeError("the matrix contains NaN or infinite values");let k=b/u,N=1,L=g+b;for(;g<k&&Math.max(N,g,v)<p&&Math.min(b,k,y)>w;)N*=u,g*=u,v*=u,b/=u,k/=u,y/=u;for(k=g/u;k>=b&&Math.max(b,y)<p&&Math.min(N,g,k,v)>w;)N/=u,g/=u,k/=u,v/=u,b*=u,y*=u;if(!(g+b>=h*L)&&!(N<1&&c[d]<1&&N*c[d]<=m)&&!(N>1&&c[d]>1&&c[d]>=o/N)){k=1/N,c[d]=c[d]*N,M=!0;for(let q=s;q<t;q++)e[f(d,q)]=e[f(d,q)]*k;for(let q=0;q<=a;q++)e[f(q,d)]=e[f(q,d)]*N}}}return{ilo:s,ihi:a,scale:c}}function R(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function E(r){return typeof r=="number"?[r]:Array.from(r)}function O(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function J(r,n){let t,e;if(R(r)&&R(n))t=r.map(l=>[...l]),e=n.map(l=>[...l]);else if(R(r)){let l=E(n),i=O(r);t=r.map(s=>[...s]),e=i===l.length?l.map(s=>[s]):i===1?[l]:[]}else if(R(n)){let l=E(r);e=n.map(i=>[...i]),t=n.length===l.length?[l]:n.length===1?l.map(i=>[i]):[]}else{let l=E(r),i=E(n);if(l.length===i.length)t=[l],e=i.map(s=>[s]);else if(l.length===1)t=[l],e=[i];else if(i.length===1)t=l.map(s=>[s]),e=[i];else throw new RangeError("non-conformable arguments")}let f=t[0]?.length??0;if(t.length===0||e.length!==f)throw new RangeError("non-conformable arguments");O(t);let c=O(e);return t.map(l=>{let i=new Array(c).fill(0);for(let s=0;s<c;s++){let a=0;for(let u=0;u<f;u++)a+=l[u]*e[u][s];i[s]=a}return i})}function V(r,n,t){let e=R(r),f=R(n);if(e&&f){let s=O(r);if(r.length!==n.length||s!==O(n))throw new RangeError("non-conformable arrays");return r.map((a,u)=>a.map((h,m)=>t(h,n[u][m])))}if(e||f){let s=e?r:n,a=E(e?n:r),u=s.length,h=O(s);if(a.length===0)return s.map(m=>m.map(()=>NaN));if(a.length>u*h)throw new RangeError("dims [product "+u*h+"] do not match the length of object ["+a.length+"]");return s.map((m,o)=>m.map((w,p)=>e?t(w,a[(o+p*u)%a.length]):t(a[(o+p*u)%a.length],w)))}let c=E(r),l=E(n);if(c.length===0||l.length===0)return[];let i=Math.max(c.length,l.length);return Array.from({length:i},(s,a)=>t(c[a%c.length],l[a%l.length]))}function Yt(r,n){return V(r,n,(t,e)=>t+e)}function Zt(r,n){return V(r,n,(t,e)=>t-e)}function tn(r,n){return V(r,n,(t,e)=>t*e)}function nn(r,n){return V(r,n,(t,e)=>t/e)}var rn={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function en(r,n,t="*"){let e=typeof t=="function"?t:rn[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let f=Array.from(r),c=Array.from(n);return f.map(l=>c.map(i=>e(l,i)))}function K(r){if(!R(r))return[E(r)];let n=O(r);return Array.from({length:n},(t,e)=>r.map(f=>f[e]))}function on(r,n={}){if(R(r)){let i=O(r);return Array.from({length:Math.min(r.length,i)},(s,a)=>r[a][a])}let t=E(r),e,f;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(f=Math.trunc(t[0]),!(f>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,f=n.nrow??t.length;let c=n.ncol??f,l=Array.from({length:f},()=>new Array(c).fill(0));for(let i=0;i<Math.min(f,c);i++)l[i][i]=e[i%e.length];return f===0?[]:l}function Mt(r,n){let t=r.map(c=>R(c)?n?K(c):c.map(l=>[...l]):[E(c)]),e=r.filter(R),f=e.length>0?n?e[0].length:O(e[0]):Math.max(...t.map(c=>c[0].length));return e.forEach((c,l)=>{if((n?c.length:O(c))!==f)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${l+2})`)}),t.flatMap(c=>c.map(l=>Array.from({length:f},(i,s)=>l[s%l.length])))}function an(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function bt(r){let n=r.filter(an);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function sn(...r){let n=bt(r);return n===void 0?K(Mt(r,!0)):at(n)}function cn(...r){let n=bt(r);return n===void 0?Mt(r,!1):ot(n)}function ln(r,n={}){let t=E(r),e=t.length,{nrow:f,ncol:c}=n;f===void 0&&c===void 0?(f=e,c=1):f===void 0?f=Math.ceil(e/c):c===void 0&&(c=Math.ceil(e/f));let l=n.byrow??!1;return Array.from({length:f},(i,s)=>Array.from({length:c},(a,u)=>e===0?NaN:t[(l?s*c+u:s+u*f)%e]))}function fn(r,n){if(!R(r)&&!R(n)){let c=E(r),l=E(n);return c.flatMap(i=>l.map(s=>i*s))}let t=R(r)?r:E(r).map(c=>[c]),e=R(n)?n:E(n).map(c=>[c]),f=[];for(let c of t)for(let l of e)f.push(c.flatMap(i=>l.map(s=>i*s)));return f}function un(r,n,t={}){let{by:e,lengthOut:f}=t;if(n===void 0&&e===void 0&&f===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return gt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(f===void 0){if(e===void 0)return gt(r??1,n??1);let l=r??1,i=n??1,s=i-l;if(s===0&&i===0)return[i];let a=s/e;if(!Number.isFinite(a)){if(e===0&&s===0)return[l];throw new RangeError("invalid '(to - from)/by'")}if(a<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(s)/Math.max(Math.abs(i),Math.abs(l))<100*Number.EPSILON)return[l];let u=Math.trunc(a+1e-10);return Array.from({length:u+1},(h,m)=>e>0?Math.min(l+m*e,i):Math.max(l+m*e,i))}if(!Number.isFinite(f)||f<0)throw new RangeError("'length.out' must be a non-negative number");let c=Math.ceil(f);if(c===0)return[];if(e===void 0){let l=n??(r??1)+(c-1),i=r??l-(c-1);if(c<=2)return[i,l].slice(0,c);if(i===l)return new Array(c).fill(i);let s=(l-i)/(c-1);return Array.from({length:c},(a,u)=>u===0?i:u===c-1?l:i+u*s)}if(n===void 0){let l=r??1;return Array.from({length:c},(i,s)=>l+s*e)}if(r===void 0)return Array.from({length:c},(l,i)=>n-(c-1-i)*e);throw new RangeError("too many arguments")}function gt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(f,c)=>r+e*c)}var mn=22250738585072014e-324;function hn(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(a=>{if(a.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(a)}),f=Array.from({length:n},(a,u)=>u),c=Math.min(n,t);for(let a=0;a<c;a++){let u=a,h=Math.abs(e[a][a]);for(let w=a+1;w<n;w++){let p=Math.abs(e[w][a]);(p>h||Number.isNaN(h))&&(h=p,u=w)}u!==a&&([e[u],e[a]]=[e[a],e[u]],[f[u],f[a]]=[f[a],f[u]]);let m=e[a][a];if(m===0)continue;let o=Math.abs(m)>=mn?1/m:NaN;for(let w=a+1;w<n;w++){let p=Number.isNaN(o)?e[w][a]/m:e[w][a]*o;e[w][a]=p;for(let M=a+1;M<t;M++)e[w][M]=e[w][M]-p*e[a][M]}}let l=Array.from({length:n},(a,u)=>Array.from({length:c},(h,m)=>u===m?1:u>m?e[u][m]:0)),i=Array.from({length:c},(a,u)=>Array.from({length:t},(h,m)=>m>=u?e[u][m]:0)),s=Array.from({length:n},()=>new Array(n).fill(0));return f.forEach((a,u)=>s[a][u]=1),{P:s,L:l,U:i}}function dn(r,n={}){let t=r.map(s=>Array.from(s)),e=st(t,{center:n.center??!0,scale:n.scale??!1});if(e.scale?.some(s=>s===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let f=j(e.scaled,{nu:0}),c=t.length,l=f.v,i={sdev:f.d.map(s=>s/Math.sqrt(Math.max(1,c-1))),rotation:l,x:J(e.scaled,l)};return e.center!==void 0&&(i.center=e.center),e.scale!==void 0&&(i.scale=e.scale),i}export{Yt as add,_t as backsolve,sn as cbind,St as chol,Ot as chol2inv,Pt as crossprod,Ft as det,Rt as determinant,on as diag,nn as divide,It as eigen,Xt as expm,Tt as forwardsolve,Ct as ginv,Lt as kappa,fn as kronecker,hn as lu,J as matMul,ln as matrix,tn as multiply,H as norm,en as outer,dn as prcomp,Q as qr,kt as qrCoef,xt as qrFitted,Et as qrQ,vt as qrQty,yt as qrQy,X as qrR,qt as qrResid,At as qrSolve,Nt as qrX,cn as rbind,B as rcond,Vt as schur,un as seq,_ as solve,Zt as subtract,j as svd,K as t,jt as tcrossprod};
|
package/dist/numerics.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{a as $,b as T,c as D,d as F,e as O}from"./chunks/chunk-IT354SHV.js";import"./chunks/chunk-QULPY6YM.js";import{a as x,f as j}from"./chunks/chunk-QFULZNHL.js";import"./chunks/chunk-7HPVUVZB.js";import{b as S}from"./chunks/chunk-LIMZKONM.js";import"./chunks/chunk-2XKG3DX4.js";function M(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function X(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function J(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function P(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=M(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=x(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?X(g,u,E,b):J(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function Y(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function B(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function G(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=M(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&K(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?Y(e.x,e.y):t==="periodic"?B(e.x,e.y):t==="hyman"?G(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function K(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function H(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function Q(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=x(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:H(a,f,0)}}function U(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=M(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=V(f.x,f.y);return(s,c=0)=>I(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>H(a,f,r)}function V(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function I(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function W(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>I(a,f,r,s,c)}function Z(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function tt(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function nt(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function k(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function L(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function rt(o,l,t={}){let n=[...l],e=n.length;if(L(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(k(c))}return r}function et(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if(L(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=k(i[h]))}return f}function ot(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(k(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=k(d)}}return c}export{P as approx,Z as barylag,rt as grad,tt as hermiteInterp,ot as hessian,$ as integrate,et as jacobian,nt as ode,O as optim,F as optimHess,T as optimize,Q as spline,U as splinefun,W as splinefunH,D as uniroot};
|
|
1
|
+
/*! rStats.js 0.11.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as $,b as T,c as D,d as F,e as O}from"./chunks/chunk-VJ4BDBMU.js";import"./chunks/chunk-EZAFN5CD.js";import{a as x,f as j}from"./chunks/chunk-C5XTWMGB.js";import"./chunks/chunk-5WDJPHYY.js";import{b as S}from"./chunks/chunk-Y5PYRAYP.js";import"./chunks/chunk-EZWBRCJL.js";function M(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function X(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function J(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function P(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=M(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=x(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?X(g,u,E,b):J(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function Y(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function B(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function G(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=M(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&K(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?Y(e.x,e.y):t==="periodic"?B(e.x,e.y):t==="hyman"?G(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function K(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function H(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function Q(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=x(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:H(a,f,0)}}function U(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=M(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=V(f.x,f.y);return(s,c=0)=>I(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>H(a,f,r)}function V(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function I(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function W(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>I(a,f,r,s,c)}function Z(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function tt(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function nt(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function k(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function L(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function rt(o,l,t={}){let n=[...l],e=n.length;if(L(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(k(c))}return r}function et(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if(L(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=k(i[h]))}return f}function ot(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(k(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=k(d)}}return c}export{P as approx,Z as barylag,rt as grad,tt as hermiteInterp,ot as hessian,$ as integrate,et as jacobian,nt as ode,O as optim,F as optimHess,T as optimize,Q as spline,U as splinefun,W as splinefunH,D as uniroot};
|