@numero-uno-jpn/rstats 0.16.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/README.ja.md +57 -23
  3. package/README.md +60 -23
  4. package/dist/chunks/{chunk-5UQCRBAG.js → chunk-3W5NFFAA.js} +1 -1
  5. package/dist/chunks/chunk-4F2IALD4.js +3 -0
  6. package/dist/chunks/{chunk-QNNL7AZ3.js → chunk-4OLDGML4.js} +2 -2
  7. package/dist/chunks/chunk-63OPBEYO.js +2 -0
  8. package/dist/chunks/chunk-6XJBCRBV.js +2 -0
  9. package/dist/chunks/chunk-BQSIBJVX.js +2 -0
  10. package/dist/chunks/chunk-BWKHYRM6.js +3 -0
  11. package/dist/chunks/{chunk-W26LRYOJ.js → chunk-CKV4JWW2.js} +1 -1
  12. package/dist/chunks/chunk-DCAWMF4K.js +2 -0
  13. package/dist/chunks/chunk-F7YGX7PN.js +2 -0
  14. package/dist/chunks/chunk-H7I32ZZE.js +2 -0
  15. package/dist/chunks/chunk-IEOD5GWC.js +3 -0
  16. package/dist/chunks/chunk-KICBQGFN.js +2 -0
  17. package/dist/chunks/chunk-LROMWFAK.js +2 -0
  18. package/dist/chunks/chunk-NSWENS3S.js +2 -0
  19. package/dist/chunks/chunk-O2SQMGE3.js +2 -0
  20. package/dist/chunks/chunk-QQX46TCI.js +2 -0
  21. package/dist/chunks/chunk-QZK3CCBA.js +2 -0
  22. package/dist/chunks/chunk-UJ66VQIG.js +2 -0
  23. package/dist/chunks/chunk-UKNDX4BA.js +2 -0
  24. package/dist/chunks/chunk-WIBKS73G.js +2 -0
  25. package/dist/chunks/{chunk-URIO5LY5.js → chunk-WQWZ7FND.js} +2 -2
  26. package/dist/chunks/chunk-WWVMVZUL.js +2 -0
  27. package/dist/chunks/chunk-YG5S5AVP.js +2 -0
  28. package/dist/chunks/chunk-YHNMBYSW.js +2 -0
  29. package/dist/data/grouping.js +6 -2
  30. package/dist/descriptive/basic.js +20 -0
  31. package/dist/descriptive/cov.js +11 -0
  32. package/dist/descriptive/dataFrames.js +13 -5
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +10 -2
  35. package/dist/descriptive/hist.js +6 -0
  36. package/dist/descriptive/moments.js +14 -0
  37. package/dist/descriptive/omega.js +8 -3
  38. package/dist/descriptive/pivot.d.ts +4 -1
  39. package/dist/descriptive/pivot.js +27 -13
  40. package/dist/descriptive/quantile.js +8 -0
  41. package/dist/descriptive/rank.js +4 -0
  42. package/dist/descriptive/table.d.ts +20 -1
  43. package/dist/descriptive/table.js +14 -14
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.js +8 -0
  46. package/dist/distributions/beta.js +9 -0
  47. package/dist/distributions/binomial.js +9 -0
  48. package/dist/distributions/cauchy.js +9 -0
  49. package/dist/distributions/chisq.js +9 -0
  50. package/dist/distributions/exponential.js +9 -0
  51. package/dist/distributions/f.js +9 -0
  52. package/dist/distributions/gamma.js +8 -0
  53. package/dist/distributions/geometric.js +9 -0
  54. package/dist/distributions/hypergeometric.js +8 -0
  55. package/dist/distributions/invgamma.js +8 -0
  56. package/dist/distributions/kumaraswamy.js +9 -0
  57. package/dist/distributions/laplace.js +9 -0
  58. package/dist/distributions/logistic.js +9 -0
  59. package/dist/distributions/lognormal.js +8 -0
  60. package/dist/distributions/multinomial.js +4 -0
  61. package/dist/distributions/negbinomial.js +8 -0
  62. package/dist/distributions/normal.js +8 -0
  63. package/dist/distributions/pareto.js +8 -0
  64. package/dist/distributions/poisson.js +9 -0
  65. package/dist/distributions/signrank.js +9 -0
  66. package/dist/distributions/summary.d.ts +2 -1
  67. package/dist/distributions/summary.js +31 -1
  68. package/dist/distributions/t.js +9 -0
  69. package/dist/distributions/triangular.js +9 -0
  70. package/dist/distributions/tukey.js +6 -0
  71. package/dist/distributions/uniform.js +8 -0
  72. package/dist/distributions/weibull.js +8 -0
  73. package/dist/distributions/wilcox.js +9 -0
  74. package/dist/distributions.js +2 -2
  75. package/dist/entries/descriptive.d.ts +3 -1
  76. package/dist/entries/descriptive.js +1 -1
  77. package/dist/entries/distributions.d.ts +1 -0
  78. package/dist/entries/distributions.js +2 -0
  79. package/dist/entries/linalg.d.ts +3 -1
  80. package/dist/entries/mixed.d.ts +9 -0
  81. package/dist/entries/mixed.js +8 -1
  82. package/dist/entries/numerics.d.ts +2 -1
  83. package/dist/entries/tests.d.ts +8 -5
  84. package/dist/entries/tests.js +3 -1
  85. package/dist/htest/agreement.d.ts +13 -0
  86. package/dist/htest/agreement.js +46 -7
  87. package/dist/htest/ansariTest.d.ts +2 -1
  88. package/dist/htest/ansariTest.js +43 -12
  89. package/dist/htest/aov.d.ts +28 -22
  90. package/dist/htest/aov.js +42 -13
  91. package/dist/htest/binomTest.js +6 -0
  92. package/dist/htest/brunnerMunzelTest.js +5 -0
  93. package/dist/htest/chisqTest.d.ts +3 -1
  94. package/dist/htest/chisqTest.js +21 -3
  95. package/dist/htest/cor.d.ts +7 -1
  96. package/dist/htest/cor.js +40 -7
  97. package/dist/htest/core/simulate.d.ts +6 -0
  98. package/dist/htest/core/simulate.js +20 -0
  99. package/dist/htest/effectSize.d.ts +3 -0
  100. package/dist/htest/effectSize.js +28 -8
  101. package/dist/htest/fisherTest.d.ts +2 -1
  102. package/dist/htest/fisherTest.js +52 -6
  103. package/dist/htest/ksTest.d.ts +4 -0
  104. package/dist/htest/ksTest.js +13 -4
  105. package/dist/htest/leveneTest.d.ts +2 -0
  106. package/dist/htest/leveneTest.js +9 -3
  107. package/dist/htest/mantelhaenTest.js +7 -0
  108. package/dist/htest/mcnemarTest.js +8 -0
  109. package/dist/htest/multipleComparisons.d.ts +16 -25
  110. package/dist/htest/multipleComparisons.js +26 -43
  111. package/dist/htest/normalityTests.js +10 -4
  112. package/dist/htest/onewayTest.js +10 -0
  113. package/dist/htest/pAdjust.js +4 -0
  114. package/dist/htest/pairwise.d.ts +2 -0
  115. package/dist/htest/pairwise.js +29 -6
  116. package/dist/htest/poissonTest.js +6 -0
  117. package/dist/htest/polycor.js +20 -10
  118. package/dist/htest/power.d.ts +4 -0
  119. package/dist/htest/power.js +16 -2
  120. package/dist/htest/propTest.d.ts +3 -0
  121. package/dist/htest/propTest.js +23 -2
  122. package/dist/htest/pwr.d.ts +38 -1
  123. package/dist/htest/pwr.js +38 -15
  124. package/dist/htest/rankTests.js +7 -1
  125. package/dist/htest/tTest.d.ts +2 -0
  126. package/dist/htest/tTest.js +9 -0
  127. package/dist/htest/testFormula.js +11 -11
  128. package/dist/htest/trendTests.d.ts +21 -1
  129. package/dist/htest/trendTests.js +13 -18
  130. package/dist/htest/types.d.ts +2 -0
  131. package/dist/htest/varTest.js +7 -0
  132. package/dist/htest/wilcoxTest.js +51 -9
  133. package/dist/internal/checkOptions.d.ts +11 -0
  134. package/dist/internal/checkOptions.js +31 -0
  135. package/dist/internal/optionalArgs.d.ts +12 -0
  136. package/dist/internal/optionalArgs.js +46 -0
  137. package/dist/internal/warnings.d.ts +66 -0
  138. package/dist/internal/warnings.js +122 -0
  139. package/dist/linalg/basic.js +7 -0
  140. package/dist/linalg/chol.js +6 -0
  141. package/dist/linalg/eigen.js +4 -0
  142. package/dist/linalg/expm.js +4 -0
  143. package/dist/linalg/kappa.js +6 -0
  144. package/dist/linalg/lu.js +5 -0
  145. package/dist/linalg/norm.js +4 -0
  146. package/dist/linalg/ops.d.ts +27 -2
  147. package/dist/linalg/ops.js +14 -13
  148. package/dist/linalg/qr.js +14 -0
  149. package/dist/linalg/solve.js +9 -0
  150. package/dist/linalg/svd.js +6 -2
  151. package/dist/linalg.js +2 -2
  152. package/dist/mixed.js +5 -4
  153. package/dist/models/anovaSequential.d.ts +16 -1
  154. package/dist/models/anovaSequential.js +50 -7
  155. package/dist/models/aovEz.js +16 -11
  156. package/dist/models/aovStrata.js +15 -4
  157. package/dist/models/carAnova.js +11 -17
  158. package/dist/models/coxph.d.ts +6 -0
  159. package/dist/models/coxph.js +21 -6
  160. package/dist/models/formula.d.ts +5 -2
  161. package/dist/models/formula.js +31 -8
  162. package/dist/models/glm.d.ts +22 -2
  163. package/dist/models/glm.js +32 -22
  164. package/dist/models/glmAnova.js +8 -3
  165. package/dist/models/glmConfint.js +6 -2
  166. package/dist/models/glmFamily.d.ts +1 -6
  167. package/dist/models/glmFamily.js +3 -22
  168. package/dist/models/glmer.d.ts +255 -0
  169. package/dist/models/glmer.js +635 -0
  170. package/dist/models/glmerAnova.d.ts +81 -0
  171. package/dist/models/glmerAnova.js +139 -0
  172. package/dist/models/glmerDeviance.d.ts +98 -0
  173. package/dist/models/glmerDeviance.js +576 -0
  174. package/dist/models/glmerFamily.d.ts +57 -0
  175. package/dist/models/glmerFamily.js +236 -0
  176. package/dist/models/glmerGradient.d.ts +46 -0
  177. package/dist/models/glmerGradient.js +301 -0
  178. package/dist/models/glmerStructure.d.ts +45 -0
  179. package/dist/models/glmerStructure.js +168 -0
  180. package/dist/models/glmerSummary.d.ts +102 -0
  181. package/dist/models/glmerSummary.js +94 -0
  182. package/dist/models/lm.js +13 -3
  183. package/dist/models/lmer.d.ts +27 -6
  184. package/dist/models/lmer.js +75 -321
  185. package/dist/models/lmerAnova.d.ts +52 -3
  186. package/dist/models/lmerAnova.js +101 -45
  187. package/dist/models/lmerTest.d.ts +12 -5
  188. package/dist/models/lmerTest.js +39 -18
  189. package/dist/models/mauchly.d.ts +2 -0
  190. package/dist/models/mauchly.js +7 -2
  191. package/dist/models/mixedDeviance.d.ts +28 -2
  192. package/dist/models/mixedDeviance.js +84 -56
  193. package/dist/models/mixedMinimiser.d.ts +83 -0
  194. package/dist/models/mixedMinimiser.js +438 -0
  195. package/dist/models/mixedPredict.d.ts +121 -0
  196. package/dist/models/mixedPredict.js +530 -0
  197. package/dist/models/mixedStructure.d.ts +62 -3
  198. package/dist/models/mixedStructure.js +74 -29
  199. package/dist/models/modelFrame.js +2 -2
  200. package/dist/models/modelMatrix.d.ts +8 -1
  201. package/dist/models/modelMatrix.js +56 -39
  202. package/dist/models/survival.d.ts +6 -0
  203. package/dist/models/survival.js +9 -2
  204. package/dist/numerics/deriv.js +8 -0
  205. package/dist/numerics/integrate.js +8 -0
  206. package/dist/numerics/interp.d.ts +32 -7
  207. package/dist/numerics/interp.js +27 -9
  208. package/dist/numerics/ode.js +4 -0
  209. package/dist/numerics.js +2 -2
  210. package/dist/optim/optim.js +17 -2
  211. package/dist/random/beta.js +5 -0
  212. package/dist/random/continuous.js +17 -0
  213. package/dist/random/extra.js +13 -0
  214. package/dist/random/gamma.js +11 -0
  215. package/dist/random/poisson.js +5 -0
  216. package/dist/random/rng.js +4 -0
  217. package/dist/random/sample.js +6 -0
  218. package/dist/rstats.js +17 -14
  219. package/dist/special/besselIK.js +7 -0
  220. package/dist/special/choose.js +7 -0
  221. package/dist/special/psigamma.js +7 -0
  222. package/dist/special.js +2 -2
  223. package/dist/tests.js +14 -11
  224. package/llms.txt +15 -4
  225. package/package.json +19 -10
  226. package/dist/chunks/chunk-2D2IQWUQ.js +0 -2
  227. package/dist/chunks/chunk-6CSTWA5E.js +0 -2
  228. package/dist/chunks/chunk-72Y3HZID.js +0 -2
  229. package/dist/chunks/chunk-BG2KLEHM.js +0 -2
  230. package/dist/chunks/chunk-CMEKBNHQ.js +0 -2
  231. package/dist/chunks/chunk-FLJNSXSW.js +0 -2
  232. package/dist/chunks/chunk-GM53LWSR.js +0 -2
  233. package/dist/chunks/chunk-JBCA56ZS.js +0 -3
  234. package/dist/chunks/chunk-K3RJAD2T.js +0 -2
  235. package/dist/chunks/chunk-KAZRWIB5.js +0 -2
  236. package/dist/chunks/chunk-KBVVIMXX.js +0 -3
  237. package/dist/chunks/chunk-N3SIFCZX.js +0 -2
  238. package/dist/chunks/chunk-SC6CV4Z3.js +0 -2
  239. package/dist/chunks/chunk-SYWOBOTR.js +0 -2
  240. package/dist/chunks/chunk-VTUGG2SV.js +0 -2
  241. package/dist/chunks/chunk-XHETEYVC.js +0 -2
  242. package/dist/chunks/chunk-YRZWONZB.js +0 -3
  243. package/dist/chunks/chunk-ZGABZMWX.js +0 -2
@@ -61,7 +61,7 @@ import { potf2, potri } from "../linalg/chol.js";
61
61
  const REFINEMENT_STEPS = 1;
62
62
  const systemCache = new WeakMap();
63
63
  /** The groups of columns, the cross-products and the pattern of the factor of a structure (cached) */
64
- function sparseSystemOf(s) {
64
+ export function sparseSystemOf(s) {
65
65
  const cached = systemCache.get(s);
66
66
  if (cached !== undefined)
67
67
  return cached;
@@ -171,31 +171,14 @@ function sparseSystemOf(s) {
171
171
  });
172
172
  for (let h = 0; h < nGroups; h++)
173
173
  blockStart[h + 1] += blockStart[h];
174
- const zz = new Float64Array(blockValueStart[nBlocks]);
174
+ const rowBlocks = rowPairs.map((b) => position[b]);
175
+ const zz = crossProductBlocks(s, termNc, blockValueStart, rowBlocks, undefined);
175
176
  const zx = new Float64Array(q * p);
176
177
  const zy = new Float64Array(q);
177
178
  const xy = new Float64Array(p);
178
179
  for (let i = 0; i < n; i++) {
179
180
  const xi = s.x[i];
180
181
  const yi = s.y[i];
181
- for (let k = 0; k < nTerms; k++) {
182
- const mk = s.terms[k].matrix[i];
183
- const nck = termNc[k];
184
- for (let l = 0; l <= k; l++) {
185
- const ml = s.terms[l].matrix[i];
186
- const ncl = termNc[l];
187
- const at = position[rowPairs[i * nPairs + (k * (k + 1)) / 2 + l]];
188
- const base = blockValueStart[at];
189
- // block (group of k, group of l): nck × ncl, column-major
190
- for (let c = 0; c < ncl; c++) {
191
- const v = ml[c];
192
- if (v === 0)
193
- continue;
194
- for (let r = 0; r < nck; r++)
195
- zz[base + r + c * nck] += mk[r] * v;
196
- }
197
- }
198
- }
199
182
  for (let a = rowStart[i]; a < rowStart[i + 1]; a++) {
200
183
  const ja = rowCols[a];
201
184
  const va = rowValues[a];
@@ -304,12 +287,71 @@ function sparseSystemOf(s) {
304
287
  rowListCol,
305
288
  rowListPos,
306
289
  blockPatternOffset,
290
+ rowBlocks,
307
291
  };
308
292
  systemCache.set(s, result);
309
293
  return result;
310
294
  }
295
+ /**
296
+ * Z'WZ by blocks, in the layout of the system's Z'Z (W the diagonal matrix of the weights `w`; Z'Z without them),
297
+ * from the terms' left-hand side matrices and the block of each row and pair of terms
298
+ */
299
+ function crossProductBlocks(s, termNc, blockValueStart, rowBlocks, w) {
300
+ const n = s.y.length;
301
+ const nTerms = s.terms.length;
302
+ const nPairs = (nTerms * (nTerms + 1)) / 2;
303
+ const zz = new Float64Array(blockValueStart[blockValueStart.length - 1]);
304
+ for (let i = 0; i < n; i++) {
305
+ const wi = w === undefined ? 1 : w[i];
306
+ if (wi === 0)
307
+ continue;
308
+ for (let k = 0; k < nTerms; k++) {
309
+ const mk = s.terms[k].matrix[i];
310
+ const nck = termNc[k];
311
+ for (let l = 0; l <= k; l++) {
312
+ const ml = s.terms[l].matrix[i];
313
+ const ncl = termNc[l];
314
+ const base = blockValueStart[rowBlocks[i * nPairs + (k * (k + 1)) / 2 + l]];
315
+ // block (group of k, group of l): nck × ncl, column-major
316
+ for (let c = 0; c < ncl; c++) {
317
+ const v = w === undefined ? ml[c] : wi * ml[c];
318
+ if (v === 0)
319
+ continue;
320
+ for (let r = 0; r < nck; r++)
321
+ zz[base + r + c * nck] += mk[r] * v;
322
+ }
323
+ }
324
+ }
325
+ }
326
+ return zz;
327
+ }
328
+ /** Z'WZ by blocks, in the layout of the system's Z'Z (glmer's working weights `w`, one per row) */
329
+ export function weightedCrossProduct(sys, s, w) {
330
+ return crossProductBlocks(s, sys.termNc, sys.blockValueStart, sys.rowBlocks, w);
331
+ }
332
+ /** Z v (n-vector) from the sparse Z, for a q-vector v */
333
+ export function zTimes(sys, v) {
334
+ const out = new Float64Array(sys.n);
335
+ for (let i = 0; i < sys.n; i++) {
336
+ let sum = 0;
337
+ for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
338
+ sum += sys.rowValues[c] * v[sys.rowCols[c]];
339
+ out[i] = sum;
340
+ }
341
+ return out;
342
+ }
343
+ /** Z' v (q-vector) from the sparse Z, for an n-vector v */
344
+ export function zTransposeTimes(sys, v) {
345
+ const zv = new Float64Array(sys.q);
346
+ for (let i = 0; i < sys.n; i++) {
347
+ const vi = v[i];
348
+ for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
349
+ zv[sys.rowCols[c]] += sys.rowValues[c] * vi;
350
+ }
351
+ return zv;
352
+ }
311
353
  /** The blocks T_k at θ (nc × nc, column-major, lower triangular) */
312
- function blocksOf(sys, theta) {
354
+ export function blocksOf(sys, theta) {
313
355
  return sys.termTheta.map((idx) => Float64Array.from(idx, (t) => (t < 0 ? 0 : theta[t])));
314
356
  }
315
357
  /** Λ_θ w (q-vector) */
@@ -351,9 +393,12 @@ export function lambdaTransposeTimes(sys, t, m, ncol = 1) {
351
393
  }
352
394
  return out;
353
395
  }
354
- /** The blocks of A = Λ_θ' Z'Z Λ_θ + I (the layout of Z'Z), each T_g' Z'Z(g, h) T_h, the identity added on the diagonal */
355
- function blocksOfA(sys, t) {
356
- const a = new Float64Array(sys.zz.length);
396
+ /**
397
+ * The blocks of A = Λ_θ' Z'Z Λ_θ + I (the layout of Z'Z), each T_g' Z'Z(g, h) T_h, the identity added on the diagonal
398
+ * (with `zz`, Z'WZ by blocks instead of Z'Z: glmer's Λ_θ' Z'WZ Λ_θ + I)
399
+ */
400
+ export function blocksOfA(sys, t, zz = sys.zz) {
401
+ const a = new Float64Array(zz.length);
357
402
  const tmp = new Float64Array(64);
358
403
  for (let h = 0; h < sys.nGroups; h++) {
359
404
  const kc = sys.groupTerm[h];
@@ -365,7 +410,7 @@ function blocksOfA(sys, t) {
365
410
  const tr = t[kr];
366
411
  const base = sys.blockValueStart[b];
367
412
  if (ncr === 1 && ncc === 1) {
368
- a[base] = tr[0] * sys.zz[base] * tc[0];
413
+ a[base] = tr[0] * zz[base] * tc[0];
369
414
  }
370
415
  else {
371
416
  // tmp = Z'Z(g, h) T_h (ncr × ncc), then T_g' tmp
@@ -374,7 +419,7 @@ function blocksOfA(sys, t) {
374
419
  for (let r = 0; r < ncr; r++) {
375
420
  let sum = 0;
376
421
  for (let m = c; m < ncc; m++)
377
- sum += sys.zz[base + r + m * ncr] * tc[m + c * ncc];
422
+ sum += zz[base + r + m * ncr] * tc[m + c * ncc];
378
423
  work[r + c * ncr] = sum;
379
424
  }
380
425
  }
@@ -395,7 +440,7 @@ function blocksOfA(sys, t) {
395
440
  return a;
396
441
  }
397
442
  /** The values of the Cholesky factor L of A (A = L L', the pattern of the system), from the blocks of A */
398
- function factorise(sys, a) {
443
+ export function factorise(sys, a) {
399
444
  const { q, colStart, colRows, rowListStart, rowListCol, rowListPos } = sys;
400
445
  const l = new Float64Array(colRows.length);
401
446
  const work = new Float64Array(q);
@@ -464,7 +509,7 @@ export function solveLt(sys, l, v, offset = 0) {
464
509
  }
465
510
  }
466
511
  /** Solves L x = v in place, with L = R' and R the upper-triangular factor (column-major, k × k) */
467
- function forwardSolve(r, k, v) {
512
+ export function forwardSolve(r, k, v) {
468
513
  for (let i = 0; i < k; i++) {
469
514
  const ci = i * k;
470
515
  let sum = v[i];
@@ -474,7 +519,7 @@ function forwardSolve(r, k, v) {
474
519
  }
475
520
  }
476
521
  /** Solves R x = v in place, R upper triangular (column-major, k × k) */
477
- function backSolve(r, k, v) {
522
+ export function backSolve(r, k, v) {
478
523
  for (let i = k - 1; i >= 0; i--) {
479
524
  const xi = (v[i] /= r[i + i * k]);
480
525
  const ci = i * k;
@@ -549,26 +594,9 @@ export function plsState(structure, theta) {
549
594
  const { n, p, q } = sys;
550
595
  const t = blocksOf(sys, theta);
551
596
  const l = factorise(sys, blocksOfA(sys, t));
552
- // Z v (from the sparse Z) and Λ'Z' v
553
- const zTimes = (v) => {
554
- const out = new Float64Array(n);
555
- for (let i = 0; i < n; i++) {
556
- let sum = 0;
557
- for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
558
- sum += sys.rowValues[c] * v[sys.rowCols[c]];
559
- out[i] = sum;
560
- }
561
- return out;
562
- };
563
- const zTransposeTimes = (v) => {
564
- const zv = new Float64Array(q);
565
- for (let i = 0; i < n; i++) {
566
- const vi = v[i];
567
- for (let c = sys.rowStart[i]; c < sys.rowStart[i + 1]; c++)
568
- zv[sys.rowCols[c]] += sys.rowValues[c] * vi;
569
- }
570
- return zv;
571
- };
597
+ // Z v (from the sparse Z) and Z' v
598
+ const zTimesV = (v) => zTimes(sys, v);
599
+ const zTransposeTimesV = (v) => zTransposeTimes(sys, v);
572
600
  /** Solves A w = v in place */
573
601
  const solveA = (v) => {
574
602
  solveL(sys, l, v);
@@ -587,12 +615,12 @@ export function plsState(structure, theta) {
587
615
  solveLt(sys, l, w);
588
616
  const xk = Float64Array.from(structure.x, (row) => row[k]);
589
617
  const residualOf = () => {
590
- const fitted = zTimes(lambdaTimes(sys, t, w));
618
+ const fitted = zTimesV(lambdaTimes(sys, t, w));
591
619
  return xk.map((v, i) => v - fitted[i]);
592
620
  };
593
621
  let e = residualOf();
594
622
  for (let step = 0; step < REFINEMENT_STEPS; step++) {
595
- const g = lambdaTransposeTimes(sys, t, zTransposeTimes(e));
623
+ const g = lambdaTransposeTimes(sys, t, zTransposeTimesV(e));
596
624
  for (let i = 0; i < q; i++)
597
625
  g[i] -= w[i];
598
626
  solveA(g);
@@ -643,7 +671,7 @@ export function plsState(structure, theta) {
643
671
  };
644
672
  // the residual y − Xβ − ZΛu, from X and the sparse Z (not from the cross-products)
645
673
  const residual = (beta, bv) => {
646
- const zb = zTimes(bv);
674
+ const zb = zTimesV(bv);
647
675
  const res = new Float64Array(n);
648
676
  for (let i = 0; i < n; i++) {
649
677
  const xi = structure.x[i];
@@ -664,7 +692,7 @@ export function plsState(structure, theta) {
664
692
  let res = residual(betav, bv);
665
693
  for (let step = 0; step < REFINEMENT_STEPS; step++) {
666
694
  // the gradient of the penalised residual sum of squares: (Λ'Z'res − u, X'res)
667
- const du = lambdaTransposeTimes(sys, t, zTransposeTimes(res));
695
+ const du = lambdaTransposeTimes(sys, t, zTransposeTimesV(res));
668
696
  for (let i = 0; i < q; i++)
669
697
  du[i] -= uv[i];
670
698
  const db = new Float64Array(p);
@@ -910,10 +938,10 @@ function sparseGradient(structure, sys, t, l, theta, reml, { res, uv, prss, ws,
910
938
  * 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
911
939
  * its selected inverse `inv` (the layout of L's values) and Λ by its blocks `t` (see {@link sparseGradient}). Linear
912
940
  * in `inv` and in `t`, so that the derivatives of the sums (src/models/mixedDerivatives.ts) come from the derivative
913
- * of the selected inverse and of the blocks.
941
+ * of the selected inverse and of the blocks. With `zz` (Z'WZ by blocks, glmer's gradient), Λ'Z'WZ in place of Λ'Z'Z.
914
942
  */
915
- export function levelTraceSums(sys, inv, t) {
916
- const { colStart, zz } = sys;
943
+ export function levelTraceSums(sys, inv, t, zz = sys.zz) {
944
+ const { colStart } = sys;
917
945
  const sums = sys.termNc.map((nc) => new Float64Array(nc * nc));
918
946
  let y = new Float64Array(16);
919
947
  let f = new Float64Array(16);
@@ -0,0 +1,83 @@
1
+ /** The tolerances and steps of the minimiser (relative to max(1, max |x|) unless said otherwise). */
2
+ export interface MinimiserSettings {
3
+ /** Step of the central differences that give the Hessian, relative to |x_j| ... */
4
+ hessianStep: number;
5
+ /** ... and at least this (relative to max(1, max |x|)), for elements at or near 0 */
6
+ hessianStepFloor: number;
7
+ /** Converged when the Newton step is below this ... */
8
+ stepTolerance: number;
9
+ /** ... or when the steps stop decreasing (rounding level) below this; steps this small are always accepted */
10
+ stepFloor: number;
11
+ /** The gradient on the free elements at convergence must be below this times max(1, |criterion|) */
12
+ gradientTolerance: number;
13
+ /** An element held at 0 is released when its derivative is above this times max(1, |criterion|) */
14
+ releaseTolerance: number;
15
+ }
16
+ /** lmer's settings (an analytic gradient) */
17
+ export declare const LMER_SETTINGS: MinimiserSettings;
18
+ /**
19
+ * A criterion to minimise (internal): its value and gradient at x, which elements are diagonal elements of θ (lower
20
+ * bound 0, held at exactly 0 on the boundary), and the columns of the blocks T_k (lists of indices of x, the diagonal
21
+ * element first) whose sign may change without changing the criterion. Elements that are neither (off-diagonal
22
+ * elements of θ, fixed effects) are free.
23
+ */
24
+ export interface MinimiserProblem {
25
+ /** The criterion and its gradient at x (all elements) */
26
+ evaluate: (x: readonly number[]) => {
27
+ f: number;
28
+ g: number[];
29
+ };
30
+ /**
31
+ * The Hessian on the elements `set` at x (optional; default: central differences of `evaluate`'s gradient with
32
+ * the settings' steps). `scale` is max(1, max |x|).
33
+ */
34
+ hessian?: (x: readonly number[], set: readonly number[], scale: number) => number[][];
35
+ diagonal: readonly boolean[];
36
+ columns: readonly (readonly number[])[];
37
+ settings: MinimiserSettings;
38
+ /**
39
+ * What a non-finite criterion at a trial point means: false, an error (lmer, whose criterion is finite wherever
40
+ * defined); true, a rejected step (glmer, whose evaluations can fail far from the optimum)
41
+ */
42
+ rejectNonFinite: boolean;
43
+ /** The message of the Error thrown when the minimisation does not converge */
44
+ failure: string;
45
+ /**
46
+ * glmer's safeguards (lmer's minimisation is unchanged without them): a boundary sub-problem of
47
+ * {@link minimiseWithRestarts} that fails is left out of the comparison instead of failing the minimisation; a
48
+ * gradient that fails at a point of the differences of the Hessian is replaced by the one-sided difference; and
49
+ * steps rejected again and again at the smallest radius (the criterion cannot tell the trial from the point to
50
+ * rounding) end the minimisation there, converged if the gradient is below the tolerance (see localMinimum)
51
+ */
52
+ robust?: boolean;
53
+ }
54
+ /** The result of a local minimisation: the point, its criterion and whether it converged */
55
+ export interface MinimiserResult {
56
+ theta: number[];
57
+ f: number;
58
+ /** false only with `bestOnFailure`: the best point reached before the minimisation failed */
59
+ converged: boolean;
60
+ }
61
+ /** Eigenvalues and eigenvectors of a small symmetric matrix by cyclic Jacobi rotations; vectors[k] is the k-th vector */
62
+ export declare function symmetricEigen(a0: readonly (readonly number[])[]): {
63
+ values: number[];
64
+ vectors: number[][];
65
+ };
66
+ /**
67
+ * A local minimiser of the criterion, subject to the lower bounds of the diagonal elements (see src/models/lmer.ts),
68
+ * started at `start`, with the elements in `held` kept at exactly 0 throughout (never free, never released); with its
69
+ * criterion
70
+ */
71
+ export declare function localMinimum(problem: MinimiserProblem, start: readonly number[], held?: ReadonlySet<number>): {
72
+ theta: number[];
73
+ f: number;
74
+ };
75
+ /**
76
+ * The minimiser: the local minimum reached from `start`, then compared with the local minima reached from its
77
+ * boundary points with one whole term removed (`blocks`: the indices of each term's block); a clearly better one is
78
+ * the start of a new local minimisation, until none is (see src/models/lmer.ts). Returns the minimiser and its
79
+ * criterion.
80
+ */
81
+ export declare function minimiseWithRestarts(problem: MinimiserProblem, start: readonly number[], blocks: readonly (readonly number[])[], options?: {
82
+ bestOnFailure?: boolean;
83
+ }): MinimiserResult;