@numero-uno-jpn/rstats 0.12.0 → 0.14.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 +76 -0
- package/README.ja.md +26 -17
- package/README.md +25 -16
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/chunks/{chunk-VQHEOBTM.js → chunk-6JRESWVF.js} +2 -2
- package/dist/chunks/chunk-7CPM3M6Q.js +2 -0
- package/dist/chunks/chunk-7PSGKKOP.js +2 -0
- package/dist/chunks/{chunk-LEIH4U5X.js → chunk-7T74KLE5.js} +2 -2
- package/dist/chunks/{chunk-XJQKSR52.js → chunk-A76XBA3D.js} +1 -1
- package/dist/chunks/chunk-ACE64PMM.js +3 -0
- package/dist/chunks/chunk-DCGBYO44.js +2 -0
- package/dist/chunks/chunk-DWNGNTS6.js +2 -0
- package/dist/chunks/chunk-FBDZ6B2G.js +2 -0
- package/dist/chunks/chunk-IBJ7RI44.js +2 -0
- package/dist/chunks/chunk-IGSR4GIF.js +2 -0
- package/dist/chunks/{chunk-6BH65HGN.js → chunk-NTETWLP3.js} +1 -1
- package/dist/chunks/chunk-Q4JF4QHS.js +2 -0
- package/dist/chunks/chunk-RZIWVCHO.js +2 -0
- package/dist/chunks/chunk-SD7ML5PE.js +2 -0
- package/dist/chunks/chunk-SZ357HRC.js +3 -0
- package/dist/chunks/chunk-WM33ADZV.js +2 -0
- package/dist/chunks/{chunk-OX3SBG5X.js → chunk-XCAMWVNO.js} +1 -1
- package/dist/chunks/chunk-YQLEXH2Y.js +3 -0
- package/dist/chunks/chunk-YTV3RFCA.js +2 -0
- package/dist/chunks/chunk-ZUESNU7W.js +2 -0
- package/dist/data/grouping.js +11 -4
- package/dist/descriptive/basic.js +57 -49
- package/dist/descriptive/cov.js +7 -2
- package/dist/descriptive/factanal.d.ts +16 -0
- package/dist/descriptive/factanal.js +26 -0
- package/dist/descriptive/hist.d.ts +8 -2
- package/dist/descriptive/hist.js +10 -2
- package/dist/descriptive/quantile.d.ts +8 -3
- package/dist/descriptive/quantile.js +10 -6
- package/dist/descriptive/table.d.ts +7 -5
- package/dist/descriptive/table.js +39 -22
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +17 -0
- package/dist/distributions/binomial.js +3 -0
- package/dist/distributions/gamma.js +7 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +3 -1
- package/dist/entries/mixed.js +4 -1
- package/dist/entries/tests.d.ts +1 -1
- package/dist/htest/agreement.js +7 -2
- package/dist/htest/groups.js +7 -3
- package/dist/htest/mcnemarTest.js +6 -2
- package/dist/htest/pairwise.js +6 -3
- package/dist/htest/polycor.js +5 -3
- package/dist/htest/tTest.d.ts +3 -2
- package/dist/htest/tTest.js +6 -3
- package/dist/htest/trendTests.js +10 -5
- package/dist/internal/fround.d.ts +12 -0
- package/dist/internal/fround.js +89 -0
- package/dist/internal/levels.d.ts +11 -0
- package/dist/internal/levels.js +48 -0
- package/dist/internal/summary.d.ts +19 -1
- package/dist/internal/summary.js +69 -8
- package/dist/linalg/balance.d.ts +21 -0
- package/dist/linalg/balance.js +191 -0
- package/dist/linalg/eigen.d.ts +1 -1
- package/dist/linalg/eigen.js +8 -2
- package/dist/linalg/expm.d.ts +1 -11
- package/dist/linalg/expm.js +2 -158
- package/dist/linalg/kappa.d.ts +6 -2
- package/dist/linalg/kappa.js +16 -7
- package/dist/linalg/lu.d.ts +13 -2
- package/dist/linalg/lu.js +17 -3
- package/dist/linalg/qr.d.ts +4 -1
- package/dist/linalg/qr.js +2 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -3
- package/dist/models/aovEz.js +6 -4
- package/dist/models/coxph.js +5 -2
- package/dist/models/glm.d.ts +4 -1
- package/dist/models/glmFamily.d.ts +1 -1
- package/dist/models/glmFamily.js +24 -2
- package/dist/models/lmer.d.ts +37 -4
- package/dist/models/lmer.js +18 -1
- package/dist/models/lmerAnova.d.ts +138 -0
- package/dist/models/lmerAnova.js +134 -0
- package/dist/models/lmerHypotheses.d.ts +105 -0
- package/dist/models/lmerHypotheses.js +303 -0
- package/dist/models/lmerTest.d.ts +233 -0
- package/dist/models/lmerTest.js +306 -0
- package/dist/models/mauchly.js +2 -1
- package/dist/models/mixedDerivatives.d.ts +27 -0
- package/dist/models/mixedDerivatives.js +342 -0
- package/dist/models/mixedDeviance.d.ts +112 -0
- package/dist/models/mixedDeviance.js +216 -86
- package/dist/models/mixedStructure.d.ts +5 -3
- package/dist/models/modelFrame.js +2 -1
- package/dist/models/modelMatrix.d.ts +10 -3
- package/dist/models/modelMatrix.js +23 -2
- package/dist/models/survival.js +5 -1
- package/dist/numerics/integrate.d.ts +1 -1
- package/dist/numerics/integrate.js +13 -3
- package/dist/numerics/interp.d.ts +1 -1
- package/dist/numerics/interp.js +3 -0
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +10 -20
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -12
- package/llms.txt +6 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-3VMBBQLV.js +0 -3
- package/dist/chunks/chunk-4OKAZJJG.js +0 -3
- package/dist/chunks/chunk-ATC4F4BX.js +0 -2
- package/dist/chunks/chunk-EDH53DXN.js +0 -2
- package/dist/chunks/chunk-EZ2KD5NA.js +0 -2
- package/dist/chunks/chunk-GSPOO2G3.js +0 -2
- package/dist/chunks/chunk-K7AZ65H7.js +0 -2
- package/dist/chunks/chunk-SGZQDJRR.js +0 -2
- package/dist/chunks/chunk-UH2N3ZQW.js +0 -2
- package/dist/chunks/chunk-UV2MZYDQ.js +0 -2
- package/dist/chunks/chunk-WJQZZYOI.js +0 -2
- package/dist/chunks/chunk-WKEFHOFR.js +0 -2
- package/dist/chunks/chunk-WXC25OPJ.js +0 -2
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// The derivatives that lmerTest's Satterthwaite approximation needs (oracle/lmertest-behaviour.md, section 1): the
|
|
2
|
+
// Hessian H of the deviance d(θ, σ) in the variance parameters varpar = (θ, σ), and the Jacobians J_k of the covariance
|
|
3
|
+
// of the fixed effects C(varpar) = σ² (R_X'R_X)⁻¹, at a given θ (σ from the penalised residual sum of squares).
|
|
4
|
+
// Computed analytically and sparsely; nothing is differentiated numerically.
|
|
5
|
+
//
|
|
6
|
+
// Written for rStats.js (no ported code) from the specification and the penalised least squares of D. Bates,
|
|
7
|
+
// M. Mächler, B. Bolker, S. Walker (2015), "Fitting Linear Mixed-Effects Models Using lme4", Journal of Statistical
|
|
8
|
+
// Software 67(1). The notation is that of src/models/mixedDeviance.ts: A = Λ'Z'ZΛ + I = L L', S = R_X'R_X,
|
|
9
|
+
// W = A⁻¹ Λ'Z'X (q × p), E = X − ZΛW (n × p), res = y − Xβ̂ − ZΛû, r² = ‖res‖² + ‖û‖² (the penalised residual sum of
|
|
10
|
+
// squares). Λ is linear in θ, Λ = Σ_k θ_k D_k with D_k the 0/1 pattern of θ_k's entries.
|
|
11
|
+
//
|
|
12
|
+
// The deviance. With ν = n − p (REML) or n (ML) and Φ(θ) = log det A [+ log det S for REML],
|
|
13
|
+
// d(θ, σ) = Φ(θ) + r²(θ) / σ² + ν log(2π σ²),
|
|
14
|
+
// so ∂²d/∂θ_k∂θ_l = Φ_kl + r²_kl / σ², ∂²d/∂θ_k∂σ = −2 r²_k / σ³, ∂²d/∂σ² = 6 r² / σ⁴ − 2ν / σ².
|
|
15
|
+
//
|
|
16
|
+
// r²: r² is a minimum over (u, β), so r²_k = −2 res'Z D_k û (only the explicit dependence counts), and
|
|
17
|
+
// r²_kl = −2 (∂_l res' Z D_k û + res' Z D_k ∂_l û), ∂_l res = −Z D_l û − ZΛ ∂_l û − X ∂_l β,
|
|
18
|
+
// with (∂_l û, ∂_l β) from the normal equations M (∂u, ∂β) = (D_l'Z'res − Λ'Z'Z D_l û, −X'Z D_l û) (the derivative of
|
|
19
|
+
// M γ = G'y, G = [ZΛ, X]), solved with the factors of the fit and refined once with residuals from the data.
|
|
20
|
+
//
|
|
21
|
+
// log det A: ∂_k log det A = 2 tr(A⁻¹ Λ'Z'Z D_k) = 2 Σ_(r,c) (A⁻¹ Λ'Z'Z)[c, r] over the entries (r, c) of D_k (the
|
|
22
|
+
// gradient of mixedDeviance.ts), and its derivative along θ_l is
|
|
23
|
+
// 2 Σ_(r,c) (∂_l(A⁻¹) Λ'Z'Z + A⁻¹ D_l'Z'Z)[c, r].
|
|
24
|
+
// Both terms need their first factor only on the blocks of Z'Z, which lie on the pattern of L: A⁻¹ there is the selected
|
|
25
|
+
// inverse, and ∂_l(A⁻¹) there is the derivative of the selected inverse, obtained by differentiating, step by step, the
|
|
26
|
+
// left-looking factorisation (∂L from ∂A = D_l'Z'ZΛ + Λ'Z'Z D_l, a sparse matrix on the same pattern) and Takahashi's
|
|
27
|
+
// recurrences (forward-mode differentiation of the algorithms, exact up to rounding). Each direction costs about one
|
|
28
|
+
// factorisation and one selected inverse more.
|
|
29
|
+
//
|
|
30
|
+
// log det S (REML): ∂_k S = −E'Z D_k W − (E'Z D_k W)', so ∂_k log det S = tr(S⁻¹ ∂_k S), and
|
|
31
|
+
// ∂_kl log det S = tr(S⁻¹ ∂_l ∂_k S) − tr(S⁻¹ ∂_k S S⁻¹ ∂_l S),
|
|
32
|
+
// ∂_l ∂_k S = −(∂_l E' Z D_k W + E'Z D_k ∂_l W) − (...)',
|
|
33
|
+
// ∂_l W = A⁻¹ (D_l'Z'E − Λ'Z'Z D_l W), ∂_l E = −Z D_l W − ZΛ ∂_l W.
|
|
34
|
+
//
|
|
35
|
+
// The Jacobians: J_k = −σ² S⁻¹ ∂_k S S⁻¹ for θ_k and J_σ = 2σ S⁻¹.
|
|
36
|
+
import { factoriseSystemTangent, inverseFromFactor, lambdaTimes, lambdaTransposeTimes, levelTraceSums, plsState, selectedInverse, selectedInverseTangent, solveL, solveLt, } from "./mixedDeviance.js";
|
|
37
|
+
/** T1_g' Z'Z(g, h) T2_h for every block (g, h) of Z'Z (the layout of the blocks of A), from the blocks T1 and T2 */
|
|
38
|
+
function sandwich(sys, t1, t2) {
|
|
39
|
+
const out = new Float64Array(sys.zz.length);
|
|
40
|
+
let work = new Float64Array(64);
|
|
41
|
+
for (let h = 0; h < sys.nGroups; h++) {
|
|
42
|
+
const kc = sys.groupTerm[h];
|
|
43
|
+
const ncc = sys.termNc[kc];
|
|
44
|
+
const tc = t2[kc];
|
|
45
|
+
for (let b = sys.blockStart[h]; b < sys.blockStart[h + 1]; b++) {
|
|
46
|
+
const kr = sys.groupTerm[sys.blockRow[b]];
|
|
47
|
+
const ncr = sys.termNc[kr];
|
|
48
|
+
const tr = t1[kr];
|
|
49
|
+
const base = sys.blockValueStart[b];
|
|
50
|
+
if (work.length < ncr * ncc)
|
|
51
|
+
work = new Float64Array(ncr * ncc);
|
|
52
|
+
for (let c = 0; c < ncc; c++) {
|
|
53
|
+
for (let r = 0; r < ncr; r++) {
|
|
54
|
+
let sum = 0;
|
|
55
|
+
for (let m = 0; m < ncc; m++)
|
|
56
|
+
sum += sys.zz[base + r + m * ncr] * tc[m + c * ncc];
|
|
57
|
+
work[r + c * ncr] = sum;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (let c = 0; c < ncc; c++) {
|
|
61
|
+
for (let r = 0; r < ncr; r++) {
|
|
62
|
+
let sum = 0;
|
|
63
|
+
for (let m = 0; m < ncr; m++)
|
|
64
|
+
sum += tr[m + r * ncr] * work[m + c * ncr];
|
|
65
|
+
out[base + r + c * ncr] = sum;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
/** p × p column-major products */
|
|
73
|
+
function matMul(a, b, p) {
|
|
74
|
+
const out = new Float64Array(p * p);
|
|
75
|
+
for (let j = 0; j < p; j++) {
|
|
76
|
+
for (let k = 0; k < p; k++) {
|
|
77
|
+
const bkj = b[k + j * p];
|
|
78
|
+
if (bkj === 0)
|
|
79
|
+
continue;
|
|
80
|
+
for (let i = 0; i < p; i++)
|
|
81
|
+
out[i + j * p] += a[i + k * p] * bkj;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
const trace = (a, p) => {
|
|
87
|
+
let s = 0;
|
|
88
|
+
for (let i = 0; i < p; i++)
|
|
89
|
+
s += a[i + i * p];
|
|
90
|
+
return s;
|
|
91
|
+
};
|
|
92
|
+
const rowsOf = (a, p) => Array.from({ length: p }, (_, i) => Array.from({ length: p }, (_, j) => a[i + j * p]));
|
|
93
|
+
/**
|
|
94
|
+
* The deviance d(θ, σ) in the variance parameters, its gradient and Hessian, and the covariance of β̂ with its
|
|
95
|
+
* Jacobians, at θ with σ = sqrt(r²(θ) / ν) (see the header). Internal.
|
|
96
|
+
*/
|
|
97
|
+
export function varparDerivatives(structure, theta, reml) {
|
|
98
|
+
const st = plsState(structure, theta);
|
|
99
|
+
const { sys, t, l, rx, ws, xResiduals, res, uv } = st;
|
|
100
|
+
const { n, p, q } = sys;
|
|
101
|
+
const nt = theta.length;
|
|
102
|
+
const m = nt + 1;
|
|
103
|
+
// Z v, Z' v
|
|
104
|
+
const zTimes = (v) => {
|
|
105
|
+
const out = new Float64Array(n);
|
|
106
|
+
for (let i = 0; i < n; i++) {
|
|
107
|
+
let sum = 0;
|
|
108
|
+
for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
|
|
109
|
+
sum += sys.rowValues[c] * v[sys.rowCols[c]];
|
|
110
|
+
out[i] = sum;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
};
|
|
114
|
+
const zTransposeTimes = (v) => {
|
|
115
|
+
const out = new Float64Array(q);
|
|
116
|
+
for (let i = 0; i < n; i++) {
|
|
117
|
+
const vi = v[i];
|
|
118
|
+
if (vi === 0)
|
|
119
|
+
continue;
|
|
120
|
+
for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
|
|
121
|
+
out[sys.rowCols[c]] += sys.rowValues[c] * vi;
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
};
|
|
125
|
+
const xTransposeTimes = (v) => {
|
|
126
|
+
const out = new Float64Array(p);
|
|
127
|
+
for (let i = 0; i < n; i++) {
|
|
128
|
+
const xi = structure.x[i];
|
|
129
|
+
const vi = v[i];
|
|
130
|
+
for (let k = 0; k < p; k++)
|
|
131
|
+
out[k] += xi[k] * vi;
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
};
|
|
135
|
+
const xTimes = (v) => {
|
|
136
|
+
const out = new Float64Array(n);
|
|
137
|
+
for (let i = 0; i < n; i++) {
|
|
138
|
+
const xi = structure.x[i];
|
|
139
|
+
let sum = 0;
|
|
140
|
+
for (let k = 0; k < p; k++)
|
|
141
|
+
sum += xi[k] * v[k];
|
|
142
|
+
out[i] = sum;
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
};
|
|
146
|
+
const solveA = (v) => {
|
|
147
|
+
solveL(sys, l, v);
|
|
148
|
+
solveLt(sys, l, v);
|
|
149
|
+
};
|
|
150
|
+
// the entries (r, c) of each D_k
|
|
151
|
+
const entryRows = Array.from({ length: nt }, () => []);
|
|
152
|
+
const entryCols = Array.from({ length: nt }, () => []);
|
|
153
|
+
for (const { row, col, theta: k } of structure.lambda) {
|
|
154
|
+
entryRows[k].push(row);
|
|
155
|
+
entryCols[k].push(col);
|
|
156
|
+
}
|
|
157
|
+
/** D_k w */
|
|
158
|
+
const dTimes = (k, w) => {
|
|
159
|
+
const out = new Float64Array(q);
|
|
160
|
+
const rs = entryRows[k];
|
|
161
|
+
const cs = entryCols[k];
|
|
162
|
+
for (let e = 0; e < rs.length; e++)
|
|
163
|
+
out[rs[e]] += w[cs[e]];
|
|
164
|
+
return out;
|
|
165
|
+
};
|
|
166
|
+
/** D_k' v */
|
|
167
|
+
const dTransposeTimes = (k, v) => {
|
|
168
|
+
const out = new Float64Array(q);
|
|
169
|
+
const rs = entryRows[k];
|
|
170
|
+
const cs = entryCols[k];
|
|
171
|
+
for (let e = 0; e < rs.length; e++)
|
|
172
|
+
out[cs[e]] += v[rs[e]];
|
|
173
|
+
return out;
|
|
174
|
+
};
|
|
175
|
+
/** Σ over the entries (r, c) of D_k of a[r] b[c] */
|
|
176
|
+
const entrySum = (k, a, b) => {
|
|
177
|
+
const rs = entryRows[k];
|
|
178
|
+
const cs = entryCols[k];
|
|
179
|
+
let s = 0;
|
|
180
|
+
for (let e = 0; e < rs.length; e++)
|
|
181
|
+
s += a[rs[e]] * b[cs[e]];
|
|
182
|
+
return s;
|
|
183
|
+
};
|
|
184
|
+
/** −(F'Z D_k G) − (...)' for q-vectors per column: F = the columns zf (Z'·), G = the columns g (p × p, column-major) */
|
|
185
|
+
const symmetricEntryProduct = (k, zf, g, out, scale) => {
|
|
186
|
+
for (let a = 0; a < p; a++) {
|
|
187
|
+
for (let b = 0; b < p; b++) {
|
|
188
|
+
const v = scale * entrySum(k, zf[a], g[b]);
|
|
189
|
+
out[a + b * p] += v;
|
|
190
|
+
out[b + a * p] += v;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
let rss = 0;
|
|
195
|
+
for (const e of res)
|
|
196
|
+
rss += e * e;
|
|
197
|
+
let ussq = 0;
|
|
198
|
+
for (const v of uv)
|
|
199
|
+
ussq += v * v;
|
|
200
|
+
const prss = rss + ussq;
|
|
201
|
+
const nu = reml ? n - p : n;
|
|
202
|
+
const sigma2 = prss / nu;
|
|
203
|
+
const sigma = Math.sqrt(sigma2);
|
|
204
|
+
let logDetL = 0;
|
|
205
|
+
for (let j = 0; j < q; j++)
|
|
206
|
+
logDetL += Math.log(l[sys.colStart[j]]);
|
|
207
|
+
logDetL *= 2;
|
|
208
|
+
let logDetRX = 0;
|
|
209
|
+
for (let k = 0; k < p; k++)
|
|
210
|
+
logDetRX += Math.log(rx[k + k * p]);
|
|
211
|
+
logDetRX *= 2;
|
|
212
|
+
const phi = reml ? logDetL + logDetRX : logDetL;
|
|
213
|
+
const deviance = phi + prss / sigma2 + nu * Math.log(2 * Math.PI * sigma2);
|
|
214
|
+
const sInverse = inverseFromFactor(rx, p);
|
|
215
|
+
const inv = selectedInverse(sys, l);
|
|
216
|
+
const zRes = zTransposeTimes(res);
|
|
217
|
+
const zE = xResiduals.map(zTransposeTimes);
|
|
218
|
+
// first derivatives: Φ_k and r²_k; ∂_k S
|
|
219
|
+
const sums = levelTraceSums(sys, inv, t);
|
|
220
|
+
const phiGrad = new Array(nt).fill(0);
|
|
221
|
+
const addTrace = (s, into, factor) => {
|
|
222
|
+
sys.termTheta.forEach((idx, k) => {
|
|
223
|
+
const nc = sys.termNc[k];
|
|
224
|
+
for (let cl = 0; cl < nc; cl++)
|
|
225
|
+
for (let rl = cl; rl < nc; rl++)
|
|
226
|
+
into[idx[rl + cl * nc]] += factor * s[k][cl + rl * nc];
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
addTrace(sums, phiGrad, 2);
|
|
230
|
+
const r2Grad = Array.from({ length: nt }, (_, k) => -2 * entrySum(k, zRes, uv));
|
|
231
|
+
const dS = Array.from({ length: nt }, (_, k) => {
|
|
232
|
+
const out = new Float64Array(p * p);
|
|
233
|
+
symmetricEntryProduct(k, zE, ws, out, -1);
|
|
234
|
+
return out;
|
|
235
|
+
});
|
|
236
|
+
if (reml)
|
|
237
|
+
for (let k = 0; k < nt; k++)
|
|
238
|
+
phiGrad[k] += trace(matMul(sInverse, dS[k], p), p);
|
|
239
|
+
const sInvDS = dS.map((d) => matMul(sInverse, d, p));
|
|
240
|
+
const hessian = Array.from({ length: m }, () => new Array(m).fill(0));
|
|
241
|
+
for (let dir = 0; dir < nt; dir++) {
|
|
242
|
+
// ∂T along θ_dir
|
|
243
|
+
const dt = sys.termTheta.map((idx) => Float64Array.from(idx, (k) => (k === dir ? 1 : 0)));
|
|
244
|
+
// log det A
|
|
245
|
+
const da = sandwich(sys, dt, t);
|
|
246
|
+
const daT = sandwich(sys, t, dt);
|
|
247
|
+
for (let i = 0; i < da.length; i++)
|
|
248
|
+
da[i] += daT[i];
|
|
249
|
+
const dl = factoriseSystemTangent(sys, l, da);
|
|
250
|
+
const dInv = selectedInverseTangent(sys, l, dl, inv);
|
|
251
|
+
const column = new Array(nt).fill(0);
|
|
252
|
+
const s1 = levelTraceSums(sys, dInv, t);
|
|
253
|
+
const s2 = levelTraceSums(sys, inv, dt);
|
|
254
|
+
addTrace(s1, column, 2);
|
|
255
|
+
addTrace(s2, column, 2);
|
|
256
|
+
// log det S (REML)
|
|
257
|
+
if (reml && p > 0) {
|
|
258
|
+
const dW = [];
|
|
259
|
+
const zdE = [];
|
|
260
|
+
for (let a = 0; a < p; a++) {
|
|
261
|
+
const dw = dTransposeTimes(dir, zE[a]);
|
|
262
|
+
const zdw = lambdaTransposeTimes(sys, t, zTransposeTimes(zTimes(dTimes(dir, ws[a]))));
|
|
263
|
+
for (let i = 0; i < q; i++)
|
|
264
|
+
dw[i] -= zdw[i];
|
|
265
|
+
solveA(dw);
|
|
266
|
+
dW.push(dw);
|
|
267
|
+
const inner = dTimes(dir, ws[a]);
|
|
268
|
+
const lw = lambdaTimes(sys, t, dw);
|
|
269
|
+
for (let i = 0; i < q; i++)
|
|
270
|
+
inner[i] += lw[i];
|
|
271
|
+
const de = zTimes(inner);
|
|
272
|
+
for (let i = 0; i < n; i++)
|
|
273
|
+
de[i] = -de[i];
|
|
274
|
+
zdE.push(zTransposeTimes(de));
|
|
275
|
+
}
|
|
276
|
+
for (let k = 0; k < nt; k++) {
|
|
277
|
+
const d2 = new Float64Array(p * p);
|
|
278
|
+
symmetricEntryProduct(k, zdE, ws, d2, -1);
|
|
279
|
+
symmetricEntryProduct(k, zE, dW, d2, -1);
|
|
280
|
+
column[k] += trace(matMul(sInverse, d2, p), p) - trace(matMul(sInvDS[k], sInvDS[dir], p), p);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
// r²: the derivative of the penalised least-squares solution along θ_dir
|
|
284
|
+
const zdu = zTimes(dTimes(dir, uv));
|
|
285
|
+
const gu = dTransposeTimes(dir, zRes);
|
|
286
|
+
const lzz = lambdaTransposeTimes(sys, t, zTransposeTimes(zdu));
|
|
287
|
+
for (let i = 0; i < q; i++)
|
|
288
|
+
gu[i] -= lzz[i];
|
|
289
|
+
const gb = xTransposeTimes(zdu);
|
|
290
|
+
for (let k = 0; k < p; k++)
|
|
291
|
+
gb[k] = -gb[k];
|
|
292
|
+
const du = gu.slice();
|
|
293
|
+
const db = gb.slice();
|
|
294
|
+
st.solveNormal(du, db);
|
|
295
|
+
{
|
|
296
|
+
// one step of refinement: the residual of the normal equations, M (du, db) from the data
|
|
297
|
+
const fit = zTimes(lambdaTimes(sys, t, du));
|
|
298
|
+
const xdb = xTimes(db);
|
|
299
|
+
for (let i = 0; i < n; i++)
|
|
300
|
+
fit[i] += xdb[i];
|
|
301
|
+
const ru = lambdaTransposeTimes(sys, t, zTransposeTimes(fit));
|
|
302
|
+
const rb = xTransposeTimes(fit);
|
|
303
|
+
for (let i = 0; i < q; i++)
|
|
304
|
+
ru[i] = gu[i] - ru[i] - du[i];
|
|
305
|
+
for (let k = 0; k < p; k++)
|
|
306
|
+
rb[k] = gb[k] - rb[k];
|
|
307
|
+
st.solveNormal(ru, rb);
|
|
308
|
+
for (let i = 0; i < q; i++)
|
|
309
|
+
du[i] += ru[i];
|
|
310
|
+
for (let k = 0; k < p; k++)
|
|
311
|
+
db[k] += rb[k];
|
|
312
|
+
}
|
|
313
|
+
const dres = zTimes(lambdaTimes(sys, t, du));
|
|
314
|
+
const xdb = xTimes(db);
|
|
315
|
+
for (let i = 0; i < n; i++)
|
|
316
|
+
dres[i] = -(zdu[i] + dres[i] + xdb[i]);
|
|
317
|
+
const zdres = zTransposeTimes(dres);
|
|
318
|
+
for (let k = 0; k < nt; k++) {
|
|
319
|
+
const r2kl = -2 * (entrySum(k, zdres, uv) + entrySum(k, zRes, du));
|
|
320
|
+
hessian[k][dir] = column[k] + r2kl / sigma2;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// symmetric
|
|
324
|
+
for (let k = 0; k < nt; k++) {
|
|
325
|
+
for (let j = k + 1; j < nt; j++) {
|
|
326
|
+
const v = (hessian[k][j] + hessian[j][k]) / 2;
|
|
327
|
+
hessian[k][j] = v;
|
|
328
|
+
hessian[j][k] = v;
|
|
329
|
+
}
|
|
330
|
+
const v = (-2 * r2Grad[k]) / (sigma2 * sigma);
|
|
331
|
+
hessian[k][nt] = v;
|
|
332
|
+
hessian[nt][k] = v;
|
|
333
|
+
}
|
|
334
|
+
hessian[nt][nt] = (6 * prss) / (sigma2 * sigma2) - (2 * nu) / sigma2;
|
|
335
|
+
const gradient = [...phiGrad.map((g, k) => g + r2Grad[k] / sigma2), (-2 * prss) / (sigma2 * sigma) + (2 * nu) / sigma];
|
|
336
|
+
const vcov = rowsOf(sInverse.map((v) => sigma2 * v), p);
|
|
337
|
+
const jacobian = [
|
|
338
|
+
...dS.map((_, k) => rowsOf(matMul(sInvDS[k], sInverse, p).map((v) => -sigma2 * v), p)),
|
|
339
|
+
rowsOf(sInverse.map((v) => 2 * sigma * v), p),
|
|
340
|
+
];
|
|
341
|
+
return { varpar: [...theta, sigma], deviance, gradient, phiGradient: phiGrad, prssGradient: r2Grad, prss, hessian, beta: Array.from(st.beta), vcov, jacobian };
|
|
342
|
+
}
|
|
@@ -31,6 +31,63 @@ export interface ProfiledDevianceOptions {
|
|
|
31
31
|
/** Also (R_X' R_X)⁻¹. */
|
|
32
32
|
rxInverse?: boolean;
|
|
33
33
|
}
|
|
34
|
+
/** What does not depend on θ: the cross-products, Z by rows, the groups of columns and the pattern of the factor */
|
|
35
|
+
export interface SparseSystem {
|
|
36
|
+
n: number;
|
|
37
|
+
p: number;
|
|
38
|
+
q: number;
|
|
39
|
+
/** Number of groups of columns (one per level of each term) */
|
|
40
|
+
nGroups: number;
|
|
41
|
+
/** First column of each group, and q at the end (nGroups + 1) */
|
|
42
|
+
groupStart: Int32Array;
|
|
43
|
+
/** The term of each group */
|
|
44
|
+
groupTerm: Int32Array;
|
|
45
|
+
/** For each term: nc, and the index into θ of each element of T_k (nc × nc, column-major; −1 above the diagonal) */
|
|
46
|
+
termNc: number[];
|
|
47
|
+
termTheta: Int32Array[];
|
|
48
|
+
/**
|
|
49
|
+
* Z'Z by blocks, lower part: the blocks of column group h are blockStart[h] .. blockStart[h + 1] − 1, the diagonal
|
|
50
|
+
* block first, then the row groups blockRow[b] > h increasing; block b is nc(row group) × nc(h), column-major, at
|
|
51
|
+
* zz[blockValueStart[b]] (the diagonal block in full)
|
|
52
|
+
*/
|
|
53
|
+
blockStart: Int32Array;
|
|
54
|
+
blockRow: Int32Array;
|
|
55
|
+
blockValueStart: Int32Array;
|
|
56
|
+
zz: Float64Array;
|
|
57
|
+
/** Z'X, q × p, column-major */
|
|
58
|
+
zx: Float64Array;
|
|
59
|
+
/** Z'y */
|
|
60
|
+
zy: Float64Array;
|
|
61
|
+
/** X'y */
|
|
62
|
+
xy: Float64Array;
|
|
63
|
+
/** The nonzero entries of Z by rows: the columns and the values of row i are at rowStart[i] .. rowStart[i + 1] − 1 */
|
|
64
|
+
rowStart: Int32Array;
|
|
65
|
+
rowCols: Int32Array;
|
|
66
|
+
rowValues: Float64Array;
|
|
67
|
+
/** The pattern of L by columns: the rows of column j at colStart[j] .. colStart[j + 1] − 1, the diagonal first */
|
|
68
|
+
colStart: Int32Array;
|
|
69
|
+
colRows: Int32Array;
|
|
70
|
+
/** The entries L[j, k], k < j, of row j: their columns k (increasing) and positions in colRows, at rowListStart[j] .. */
|
|
71
|
+
rowListStart: Int32Array;
|
|
72
|
+
rowListCol: Int32Array;
|
|
73
|
+
rowListPos: Int32Array;
|
|
74
|
+
/**
|
|
75
|
+
* For each block b of Z'Z off the diagonal (row group g, column group h): the number of rows of L's column groups
|
|
76
|
+
* before g in the pattern of h, so that the entry (row r of g, column c of h) of L (and of the selected inverse) is
|
|
77
|
+
* at colStart[column] + (rows of the diagonal block from the column) + blockPatternOffset[b] + r (0 on the diagonal)
|
|
78
|
+
*/
|
|
79
|
+
blockPatternOffset: Int32Array;
|
|
80
|
+
}
|
|
81
|
+
/** Λ_θ w (q-vector) */
|
|
82
|
+
export declare function lambdaTimes(sys: SparseSystem, t: Float64Array[], w: Float64Array): Float64Array;
|
|
83
|
+
/** Λ_θ' M for a q × ncol column-major M */
|
|
84
|
+
export declare function lambdaTransposeTimes(sys: SparseSystem, t: Float64Array[], m: Float64Array, ncol?: number): Float64Array;
|
|
85
|
+
/** Solves L x = v in place (v of length q, at an offset) */
|
|
86
|
+
export declare function solveL(sys: SparseSystem, l: Float64Array, v: Float64Array, offset?: number): void;
|
|
87
|
+
/** Solves L' x = v in place */
|
|
88
|
+
export declare function solveLt(sys: SparseSystem, l: Float64Array, v: Float64Array, offset?: number): void;
|
|
89
|
+
/** The full symmetric inverse of R'R from an upper-triangular factor R (column-major, k × k; R is not changed) */
|
|
90
|
+
export declare function inverseFromFactor(r: Float64Array, k: number): Float64Array;
|
|
34
91
|
/**
|
|
35
92
|
* The profiled deviance of a linear mixed model at `theta`, with the penalised least-squares solution
|
|
36
93
|
* (oracle/lmer-behaviour.md, stage 1b; computed sparsely). Internal: not exported from the package.
|
|
@@ -42,3 +99,58 @@ export interface ProfiledDevianceOptions {
|
|
|
42
99
|
* @param options What to compute besides: the gradient with respect to θ, (R_X' R_X)⁻¹.
|
|
43
100
|
*/
|
|
44
101
|
export declare function profiledDeviance(structure: MixedModelStructure, theta: readonly number[], reml: boolean, options?: ProfiledDevianceOptions): ProfiledDeviance;
|
|
102
|
+
/**
|
|
103
|
+
* The state of the penalised least-squares solution at a given θ (internal): the system of the structure, the blocks
|
|
104
|
+
* T_k, the sparse factor L of A, C_X = L⁻¹ Λ'Z'X, the Cholesky factor R_X (upper triangle, column-major, p × p) of
|
|
105
|
+
* R_X'R_X = E'E + W'W, the columns of W = A⁻¹ Λ'Z'X and of E = X − ZΛW, the residual y − Xβ̂ − ZΛû, û, β̂, b = Λû,
|
|
106
|
+
* and the solver of the normal equations of the penalised least squares. {@link profiledDeviance} is computed from
|
|
107
|
+
* it, and src/models/mixedDerivatives.ts takes the second derivatives from it.
|
|
108
|
+
*/
|
|
109
|
+
export interface PlsState {
|
|
110
|
+
sys: SparseSystem;
|
|
111
|
+
t: Float64Array[];
|
|
112
|
+
l: Float64Array;
|
|
113
|
+
cx: Float64Array;
|
|
114
|
+
rx: Float64Array;
|
|
115
|
+
ws: Float64Array[];
|
|
116
|
+
xResiduals: Float64Array[];
|
|
117
|
+
res: Float64Array;
|
|
118
|
+
uv: Float64Array;
|
|
119
|
+
beta: Float64Array;
|
|
120
|
+
b: Float64Array;
|
|
121
|
+
/** Solves [A, Λ'Z'X; X'ZΛ, X'X] (u, β) = (gu, gβ) with the two factors, overwriting gu with u and gβ with β */
|
|
122
|
+
solveNormal: (gu: Float64Array, gb: Float64Array) => void;
|
|
123
|
+
}
|
|
124
|
+
/** The penalised least-squares solution at θ and the factors it was computed with (see {@link PlsState}) */
|
|
125
|
+
export declare function plsState(structure: MixedModelStructure, theta: readonly number[]): PlsState;
|
|
126
|
+
/**
|
|
127
|
+
* The selected inverse of A = L L': the entries of A⁻¹ on the pattern of L (lower part, the layout of L's values), by
|
|
128
|
+
* the recurrences of Takahashi, Fagan and Chin (1973). Since L' A⁻¹ = L⁻¹ is lower triangular with diagonal 1 / L_jj,
|
|
129
|
+
* row j of it gives, for i > j in the pattern of column j,
|
|
130
|
+
* A⁻¹[i, j] = −(1 / L_jj) Σ_k L[k, j] A⁻¹[k, i], A⁻¹[j, j] = 1 / L_jj² − (1 / L_jj) Σ_k L[k, j] A⁻¹[k, j],
|
|
131
|
+
* the sums over the rows k > j of column j. The rows of column j form a clique in the pattern of L (the closure of a
|
|
132
|
+
* Cholesky pattern), so every A⁻¹[k, i] needed is on the pattern, in column min(k, i), already computed when the
|
|
133
|
+
* columns are taken from the last to the first. The cost is that of the factorisation.
|
|
134
|
+
*/
|
|
135
|
+
export declare function selectedInverse(sys: SparseSystem, l: Float64Array): Float64Array;
|
|
136
|
+
/**
|
|
137
|
+
* The derivative ∂L of the Cholesky factor L of A along a direction in which A changes by ∂A (`da`, the layout of the
|
|
138
|
+
* blocks of A, both triangles of the diagonal blocks): {@link factorise} differentiated step by step (forward mode).
|
|
139
|
+
* In column j, after the columns k < j are subtracted, the work vector holds L_jj L[r, j] (L_jj² at r = j), so with
|
|
140
|
+
* ∂ of the same subtractions (∂L[r, k] L[j, k] + L[r, k] ∂L[j, k]),
|
|
141
|
+
* ∂L_jj = ∂work[j] / (2 L_jj), ∂L[r, j] = (∂work[r] − L[r, j] ∂L_jj) / L_jj.
|
|
142
|
+
* An entry L[j, k] that is 0 (a column of Λ_θ that is 0) is not skipped: its derivative need not be 0.
|
|
143
|
+
*/
|
|
144
|
+
export declare function factoriseSystemTangent(sys: SparseSystem, l: Float64Array, da: Float64Array): Float64Array;
|
|
145
|
+
/**
|
|
146
|
+
* The derivative of the selected inverse `inv` of A = L L' given the derivative ∂L of L (`dl`): the recurrences of
|
|
147
|
+
* {@link selectedInverse} differentiated step by step (forward mode). It is ∂(A⁻¹) = −A⁻¹ ∂A A⁻¹ on the pattern of L.
|
|
148
|
+
*/
|
|
149
|
+
export declare function selectedInverseTangent(sys: SparseSystem, l: Float64Array, dl: Float64Array, inv: Float64Array): Float64Array;
|
|
150
|
+
/**
|
|
151
|
+
* S_k = Σ over the levels G of term k of (Y Λ' Z'Z) on G's columns (nc_k × nc_k, column-major), for Y = A⁻¹ given by
|
|
152
|
+
* its selected inverse `inv` (the layout of L's values) and Λ by its blocks `t` (see {@link sparseGradient}). Linear
|
|
153
|
+
* in `inv` and in `t`, so that the derivatives of the sums (src/models/mixedDerivatives.ts) come from the derivative
|
|
154
|
+
* of the selected inverse and of the blocks.
|
|
155
|
+
*/
|
|
156
|
+
export declare function levelTraceSums(sys: SparseSystem, inv: Float64Array, t: Float64Array[]): Float64Array[];
|