@compstats/core 0.2.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 (98) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/LICENSE +21 -0
  3. package/README.md +310 -0
  4. package/dist/3d.d.ts +33 -0
  5. package/dist/3d.d.ts.map +1 -0
  6. package/dist/3d.js +1984 -0
  7. package/dist/3d.js.map +22 -0
  8. package/dist/core/arith.d.ts +133 -0
  9. package/dist/core/arith.d.ts.map +1 -0
  10. package/dist/core/frame.d.ts +98 -0
  11. package/dist/core/frame.d.ts.map +1 -0
  12. package/dist/core/histogram.d.ts +68 -0
  13. package/dist/core/histogram.d.ts.map +1 -0
  14. package/dist/core/kde.d.ts +106 -0
  15. package/dist/core/kde.d.ts.map +1 -0
  16. package/dist/core/logit.d.ts +109 -0
  17. package/dist/core/logit.d.ts.map +1 -0
  18. package/dist/core/matrix.d.ts +135 -0
  19. package/dist/core/matrix.d.ts.map +1 -0
  20. package/dist/core/moderation.d.ts +127 -0
  21. package/dist/core/moderation.d.ts.map +1 -0
  22. package/dist/core/ols.d.ts +76 -0
  23. package/dist/core/ols.d.ts.map +1 -0
  24. package/dist/core/pca.d.ts +99 -0
  25. package/dist/core/pca.d.ts.map +1 -0
  26. package/dist/core/precision.d.ts +10 -0
  27. package/dist/core/precision.d.ts.map +1 -0
  28. package/dist/core/pretty.d.ts +51 -0
  29. package/dist/core/pretty.d.ts.map +1 -0
  30. package/dist/core/regression.d.ts +68 -0
  31. package/dist/core/regression.d.ts.map +1 -0
  32. package/dist/core/rng.d.ts +206 -0
  33. package/dist/core/rng.d.ts.map +1 -0
  34. package/dist/core/sampling.d.ts +157 -0
  35. package/dist/core/sampling.d.ts.map +1 -0
  36. package/dist/core/special.d.ts +92 -0
  37. package/dist/core/special.d.ts.map +1 -0
  38. package/dist/core/tdist.d.ts +53 -0
  39. package/dist/core/tdist.d.ts.map +1 -0
  40. package/dist/core/ttest.d.ts +146 -0
  41. package/dist/core/ttest.d.ts.map +1 -0
  42. package/dist/data/moderationData.d.ts +40 -0
  43. package/dist/data/moderationData.d.ts.map +1 -0
  44. package/dist/data/pcaDegenerate.d.ts +18 -0
  45. package/dist/data/pcaDegenerate.d.ts.map +1 -0
  46. package/dist/index.d.ts +66 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +4195 -0
  49. package/dist/index.js.map +47 -0
  50. package/dist/interactive/controls.d.ts +97 -0
  51. package/dist/interactive/controls.d.ts.map +1 -0
  52. package/dist/interactive/logit.d.ts +57 -0
  53. package/dist/interactive/logit.d.ts.map +1 -0
  54. package/dist/interactive/matrixInverse.d.ts +86 -0
  55. package/dist/interactive/matrixInverse.d.ts.map +1 -0
  56. package/dist/interactive/moderation3d.d.ts +103 -0
  57. package/dist/interactive/moderation3d.d.ts.map +1 -0
  58. package/dist/interactive/pca.d.ts +90 -0
  59. package/dist/interactive/pca.d.ts.map +1 -0
  60. package/dist/interactive/regression.d.ts +57 -0
  61. package/dist/interactive/regression.d.ts.map +1 -0
  62. package/dist/interactive/sampling.d.ts +78 -0
  63. package/dist/interactive/sampling.d.ts.map +1 -0
  64. package/dist/interactive/scatter3d.d.ts +124 -0
  65. package/dist/interactive/scatter3d.d.ts.map +1 -0
  66. package/dist/interactive/tTest.d.ts +69 -0
  67. package/dist/interactive/tTest.d.ts.map +1 -0
  68. package/dist/interactive/target.d.ts +132 -0
  69. package/dist/interactive/target.d.ts.map +1 -0
  70. package/dist/plot/axes.d.ts +128 -0
  71. package/dist/plot/axes.d.ts.map +1 -0
  72. package/dist/plot/draw.d.ts +46 -0
  73. package/dist/plot/draw.d.ts.map +1 -0
  74. package/dist/plot/format.d.ts +33 -0
  75. package/dist/plot/format.d.ts.map +1 -0
  76. package/dist/plot/logit.d.ts +68 -0
  77. package/dist/plot/logit.d.ts.map +1 -0
  78. package/dist/plot/matrixInverse.d.ts +62 -0
  79. package/dist/plot/matrixInverse.d.ts.map +1 -0
  80. package/dist/plot/moderation3d.d.ts +135 -0
  81. package/dist/plot/moderation3d.d.ts.map +1 -0
  82. package/dist/plot/pca.d.ts +79 -0
  83. package/dist/plot/pca.d.ts.map +1 -0
  84. package/dist/plot/plotly.d.ts +209 -0
  85. package/dist/plot/plotly.d.ts.map +1 -0
  86. package/dist/plot/regression.d.ts +52 -0
  87. package/dist/plot/regression.d.ts.map +1 -0
  88. package/dist/plot/sampleCi.d.ts +82 -0
  89. package/dist/plot/sampleCi.d.ts.map +1 -0
  90. package/dist/plot/sampling.d.ts +159 -0
  91. package/dist/plot/sampling.d.ts.map +1 -0
  92. package/dist/plot/scatter3d.d.ts +164 -0
  93. package/dist/plot/scatter3d.d.ts.map +1 -0
  94. package/dist/plot/tTest.d.ts +49 -0
  95. package/dist/plot/tTest.d.ts.map +1 -0
  96. package/dist/plot/target.d.ts +55 -0
  97. package/dist/plot/target.d.ts.map +1 -0
  98. package/package.json +71 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moderation.d.ts","sourceRoot":"","sources":["../../src/core/moderation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAGH,OAAO,EAAmC,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAS1E,oCAAoC;AACpC,MAAM,WAAW,iBAAiB;IAChC,gEAAgE;IAChE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,kDAAkD;IAClD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAC/B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAED,4CAA4C;AAC5C,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED,kDAAkD;AAClD,MAAM,WAAW,iBAAiB;IAChC,mDAAmD;IACnD,QAAQ,CAAC,YAAY,EAAE,SAAS,cAAc,EAAE,CAAC;IACjD;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,8EAA8E;IAC9E,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,+DAA+D;IAC/D,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IACxC;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,iEAAiE;IACjE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAClD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,SAAS,EACf,OAAO,EAAE,iBAAiB,GACzB,iBAAiB,CAoInB"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Dense (weighted) least squares for small designs.
3
+ *
4
+ * This is the equivalent of R's `lm.wfit()`, and of the solver R runs inside
5
+ * every step of `glm.fit()`'s IRLS loop. R factors the design with `dqrdc2`,
6
+ * a Householder QR with a limited column-pivoting rule: a column whose norm
7
+ * has collapsed against the columns to its left is moved to the right edge
8
+ * and its coefficient is reported as `NA`. The port reproduces that rule,
9
+ * because it is what makes `lm()` and `glm()` report an aliased coefficient
10
+ * instead of dividing by a near-zero pivot. Verified against R in
11
+ * `ols.test.ts`.
12
+ *
13
+ * Designs here are tiny — two columns for logit, four for a moderation
14
+ * surface — so the code follows the LINPACK routine plainly rather than
15
+ * blocking or vectorizing it.
16
+ */
17
+ /**
18
+ * The result of a fit.
19
+ *
20
+ * A `null` coefficient is the equivalent of R's `NA`: the column carried no
21
+ * information beyond the columns to its left, so R aliases it. `linearRegression`
22
+ * uses the same convention.
23
+ */
24
+ export interface LeastSquaresFit {
25
+ /**
26
+ * One coefficient per design column, in column order. An aliased column
27
+ * reports null.
28
+ */
29
+ readonly coefficients: readonly (number | null)[];
30
+ /** The fitted response of each row, in input order. */
31
+ readonly fitted: readonly number[];
32
+ /** Response minus fit, of each row, in input order. */
33
+ readonly residuals: readonly number[];
34
+ /** The number of columns the fit could identify. */
35
+ readonly rank: number;
36
+ }
37
+ export interface LeastSquaresOptions {
38
+ /**
39
+ * One weight per row, on R's `lm.wfit()` scale: the solver applies the
40
+ * square root itself.
41
+ *
42
+ * IRLS is the trap here. `glm.fit` carries square-root weights `w` and
43
+ * hands `x * w` to its QR routine, so a caller porting that loop passes
44
+ * `w * w` here, not `w`.
45
+ *
46
+ * A weight of zero drops the row from the fit. The row still gets a fitted
47
+ * value, predicted from the other rows, as it does in R.
48
+ */
49
+ readonly weights?: readonly number[];
50
+ /**
51
+ * How far a column's norm may collapse before the fit aliases it.
52
+ *
53
+ * A column is aliased when its norm, after the columns to its left are
54
+ * projected out, falls below this fraction of its original norm. The
55
+ * default is the value `lm.fit()` uses. `glm.fit()` passes
56
+ * `min(1e-7, epsilon / 1000)`, which is `1e-11` at R's default epsilon.
57
+ */
58
+ readonly tolerance?: number;
59
+ }
60
+ /** The rank tolerance of R's `lm.fit()`. */
61
+ export declare const DEFAULT_LEAST_SQUARES_TOLERANCE = 1e-7;
62
+ /**
63
+ * Fit `y` on the columns of `design` by least squares.
64
+ *
65
+ * @param design One row per observation, each row one value per column. A
66
+ * model with an intercept carries a leading column of ones. The function
67
+ * does not modify it.
68
+ * @param y The response, one value per row.
69
+ * @param options Weights and the rank tolerance.
70
+ * @returns The coefficients, the fit, and the rank.
71
+ * @throws RangeError if there are no rows, if the shapes disagree, or if a
72
+ * weight is negative. R refuses the same inputs: "0 (non-NA) cases" and
73
+ * "missing or negative weights not allowed".
74
+ */
75
+ export declare function leastSquares(design: readonly (readonly number[])[], y: readonly number[], options?: LeastSquaresOptions): LeastSquaresFit;
76
+ //# sourceMappingURL=ols.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ols.d.ts","sourceRoot":"","sources":["../../src/core/ols.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,QAAQ,CAAC,YAAY,EAAE,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC;IAClD,uDAAuD;IACvD,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,uDAAuD;IACvD,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,oDAAoD;IACpD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC;;;;;;;OAOG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,4CAA4C;AAC5C,eAAO,MAAM,+BAA+B,OAAO,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,EACtC,CAAC,EAAE,SAAS,MAAM,EAAE,EACpB,OAAO,GAAE,mBAAwB,GAChC,eAAe,CA2DjB"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Principal components of a set of two-dimensional points.
3
+ *
4
+ * This is the statistics half of `plot_pca()` in the R package, which calls
5
+ * `prcomp(mc_points, scale. = FALSE)`. Verified against R in `pca.test.ts`.
6
+ *
7
+ * Three things are worth knowing before reading the code.
8
+ *
9
+ * **The data is always centered.** R's `plot_pca()` has a `meancenter`
10
+ * argument, but it does not decide whether the components are computed on
11
+ * centered data: `prcomp()` centers again on its own, so `sdev`, `rotation`
12
+ * and the scores come out identical either way (fixture F7 shows the two runs
13
+ * bit for bit). All `meancenter` changes is where the arrows are anchored on
14
+ * screen, which is a drawing decision. So this module has no such option, and
15
+ * the plot layer owns the anchor.
16
+ *
17
+ * **The method is a closed form, not R's SVD.** R decomposes the centered
18
+ * data matrix; this port takes the eigenvectors of the 2x2 covariance matrix,
19
+ * which for two dimensions is a few lines of algebra with no iteration. The
20
+ * covariance route squares the data and so gives up a few bits that an SVD
21
+ * keeps, but at two dimensions the results agree with R far inside the
22
+ * tolerance the tests demand.
23
+ *
24
+ * **Signs are this port's own.** `?prcomp` states that the signs of the
25
+ * rotation columns are arbitrary and vary between programs and even between
26
+ * builds of R. Rather than chase LAPACK, this module fixes its own rule: in
27
+ * each column, the loading of larger magnitude is non-negative, and a tie
28
+ * goes to a non-negative x. Nothing on screen changes — `plot_pca()` draws
29
+ * each component as a two-headed arrow through the center, which is symmetric
30
+ * under a sign flip — but a caller reading the numbers gets one answer for
31
+ * one input instead of a platform's answer.
32
+ */
33
+ import type { Point } from "./regression";
34
+ /**
35
+ * One component's loading vector, as `[x, y]`.
36
+ *
37
+ * These are the two entries of one *column* of R's `rotation` matrix: R
38
+ * prints that matrix with a row per input variable, so R's `rotation["x",
39
+ * "PC1"]` is `rotation[0][0]` here and `rotation["y", "PC1"]` is
40
+ * `rotation[0][1]`.
41
+ */
42
+ export type Loadings = readonly [number, number];
43
+ /** The components of a point set, in the shape of R's `prcomp` result. */
44
+ export interface PcaResult {
45
+ /**
46
+ * The standard deviation along each component, largest first. R's `sdev`,
47
+ * computed with the n − 1 divisor.
48
+ */
49
+ readonly sdev: readonly [number, number];
50
+ /**
51
+ * The two loading vectors: `rotation[0]` is PC1, `rotation[1]` is PC2.
52
+ * They are orthonormal, so `rotation[1]` is `rotation[0]` turned a quarter
53
+ * turn, up to the sign rule described above.
54
+ */
55
+ readonly rotation: readonly [Loadings, Loadings];
56
+ /**
57
+ * The mean of each coordinate — the point the components pass through.
58
+ *
59
+ * R's own `pca$center` reports whatever `prcomp` had left to subtract,
60
+ * which is near zero when `plot_pca()` centered the data first. This field
61
+ * is instead the true column mean, which R prints as `mc_diff`.
62
+ */
63
+ readonly center: Point;
64
+ /**
65
+ * The points in component coordinates, in input order. R's `pca$x`, with
66
+ * `x` holding the PC1 score and `y` the PC2 score. Both coordinates are
67
+ * NaN where the point was dropped for a missing value.
68
+ */
69
+ readonly scores: readonly Point[];
70
+ }
71
+ /**
72
+ * Compute the two principal components of the points.
73
+ *
74
+ * A single point is a valid input: it has no spread, so both standard
75
+ * deviations are 0 and the components fall back to the coordinate axes,
76
+ * which is what `prcomp` reports for the one component it returns at that
77
+ * size. (R returns `min(n, p)` components and so gives one column there;
78
+ * this port always returns two, matching its behavior on every other
79
+ * degenerate input — see the note on rank below.) `plot_pca()` never gets
80
+ * that far anyway: it draws points and no arrows below three of them.
81
+ *
82
+ * Rank is never reduced. R's `prcomp` drops a component only when given a
83
+ * `tol`, which `plot_pca()` never passes, so collinear, identical and
84
+ * constant-column inputs all still return two components. This function does
85
+ * the same and lets a near-zero `sdev[1]` say that the second direction
86
+ * carries no spread.
87
+ *
88
+ * A point with a non-finite coordinate is dropped before the components are
89
+ * computed, R's `na.omit`; its scores report NaN in both coordinates,
90
+ * keeping input order (the `na.exclude` padding `moderationSurface` uses).
91
+ * R's own `prcomp()` errors on a missing value, so the R usage this mirrors
92
+ * is `prcomp(na.omit(points))` — the fold-in keeps a spreadsheet with one
93
+ * missing row from blanking the whole picture.
94
+ *
95
+ * @param points The observations. The function does not modify them.
96
+ * @returns The components, or null if no point is complete.
97
+ */
98
+ export declare function principalComponents(points: readonly Point[]): PcaResult | null;
99
+ //# sourceMappingURL=pca.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pca.d.ts","sourceRoot":"","sources":["../../src/core/pca.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAIH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEjD,0EAA0E;AAC1E,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjD;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAC;CACnC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,SAAS,KAAK,EAAE,GACvB,SAAS,GAAG,IAAI,CAuClB"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Report the machine precision of the runtime.
3
+ *
4
+ * The value is the smallest number x such that 1 + x is not equal to 1.
5
+ * It is the equivalent of `.Machine$double.eps` in R.
6
+ *
7
+ * @returns The double-precision epsilon.
8
+ */
9
+ export declare function machinePrecision(): number;
10
+ //# sourceMappingURL=precision.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"precision.d.ts","sourceRoot":"","sources":["../../src/core/precision.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Round numbers covering a range — R's `pretty()`.
3
+ *
4
+ * R places histogram cell edges and axis labels with this. It picks a cell
5
+ * size from the ladder 1, 2, 5, 10 times a power of ten, then rounds the low
6
+ * end down and the high end up to multiples of that size. The result
7
+ * therefore **covers** the range it was given, and usually extends past both
8
+ * ends.
9
+ *
10
+ * That widening is the whole point, and it is what separates this from
11
+ * `prettyTicks` in `src/plot/axes.ts`, which keeps ticks inside a window the
12
+ * caller has already fixed. Do not swap one for the other: a histogram drawn
13
+ * on ticks that stop short of the data loses its outer cells, and a fixed
14
+ * plot window drawn on widened ticks grows past its frame.
15
+ *
16
+ * The port follows R's C routine `R_pretty` step for step, including its
17
+ * rounding guard of 1e-10 and its bias toward 2 and 5 over stepping straight
18
+ * to the next power of ten. Verified against R 4.5.3 in `pretty.test.ts` and
19
+ * across a sweep of 5348 random ranges, every one of which it reproduces
20
+ * edge for edge. R's remaining tuning arguments (`shrink.sml`, `high.u.bias`,
21
+ * `u5.bias`, `eps.correct`, `f.min`) stay at their defaults: nothing in the
22
+ * package changes them, and each one is a branch that could only be guessed
23
+ * at rather than tested.
24
+ */
25
+ /** What the caller may change. R's tuning arguments are not ported. */
26
+ export interface RPrettyOptions {
27
+ /**
28
+ * About how many cells to produce. R's `n`, default 5. It is a wish, not a
29
+ * count: the returned array can hold more or fewer edges.
30
+ */
31
+ readonly n?: number;
32
+ /**
33
+ * The fewest cells to accept. R's `min.n`, default `floor(n / 3)`. R's
34
+ * `hist()` passes 1 here rather than taking this default.
35
+ */
36
+ readonly minN?: number;
37
+ }
38
+ /**
39
+ * Return round numbers that cover the range from `lo` to `up`.
40
+ *
41
+ * @param lo The low end of the range to cover.
42
+ * @param up The high end. It must not be below `lo`.
43
+ * @param options The wished-for cell count and the fewest cells to accept.
44
+ * @returns The edges, in increasing order. Always at least two, except for
45
+ * the degenerate request of no cells at all.
46
+ * @throws RangeError If an end is not finite, if `up` is below `lo`, if a
47
+ * count is negative or fractional, or if `minN` is above `n` (R stops with
48
+ * "invalid 'min.n' argument").
49
+ */
50
+ export declare function rPretty(lo: number, up: number, options?: RPrettyOptions): number[];
51
+ //# sourceMappingURL=pretty.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pretty.d.ts","sourceRoot":"","sources":["../../src/core/pretty.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAgBH,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CACrB,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,OAAO,GAAE,cAAmB,GAC3B,MAAM,EAAE,CAiCV"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Ordinary least-squares regression of y on x.
3
+ *
4
+ * This is the statistics half of `plot_regression()` in the R package. R gets these
5
+ * numbers from `lm()`, `cor()`, and `summary()$r.squared`. Verified against R
6
+ * in `regression.test.ts`.
7
+ */
8
+ /** One observation. R holds these as rows of a data frame. */
9
+ export interface Point {
10
+ readonly x: number;
11
+ readonly y: number;
12
+ }
13
+ /**
14
+ * The indices of the rows a fit may use: both coordinates finite.
15
+ *
16
+ * This is R's `na.omit` for a set of points. NaN is this library's missing
17
+ * value, and an infinity would poison a fit the same way, so "complete" means
18
+ * finite everywhere — the rule `moderationSurface` set for frames. The point
19
+ * cores (this module, `logit.ts`, `pca.ts`) share the rule through this
20
+ * helper.
21
+ */
22
+ export declare function completePointRows(points: readonly Point[]): readonly number[];
23
+ /**
24
+ * The result of a fit.
25
+ *
26
+ * A `null` field is the equivalent of R's `NA`. R drops a singular predictor
27
+ * and reports `NA` for its coefficient. This port reports `null`, which makes
28
+ * strict TypeScript force the caller to handle the degenerate fit.
29
+ */
30
+ export interface RegressionFit {
31
+ /** The y value where the line crosses x = 0. */
32
+ readonly intercept: number;
33
+ /** The change in y for each unit of x. Null if x has no variation. */
34
+ readonly slope: number | null;
35
+ /** Pearson r. Null if x or y has no variation. */
36
+ readonly correlation: number | null;
37
+ /** Sum of squares regression. */
38
+ readonly ssr: number;
39
+ /** Sum of squares error. */
40
+ readonly sse: number;
41
+ /** Sum of squares total. */
42
+ readonly sst: number;
43
+ /** The part of SST that the fit explains. Null if SST is 0. */
44
+ readonly rSquared: number | null;
45
+ /**
46
+ * The fitted y value of each point, in input order. NaN where the point
47
+ * was dropped for a missing value.
48
+ */
49
+ readonly fitted: readonly number[];
50
+ }
51
+ /**
52
+ * Fit a line to the points.
53
+ *
54
+ * With no variation in x, the function fits the mean of y and reports no
55
+ * slope. R does the same: it drops the singular predictor and fits an
56
+ * intercept-only model. A single point is that same case.
57
+ *
58
+ * A point with a non-finite coordinate is dropped before fitting, R's
59
+ * `na.action = na.omit`; its fitted value reports NaN, keeping input order
60
+ * (R's `na.exclude` padding, as `moderationSurface` does). R's `lm()` errors
61
+ * when every row is missing ("0 (non-NA) cases"); this port already answers
62
+ * null for "nothing to fit", and an all-missing input is that same answer.
63
+ *
64
+ * @param points The observations. The function does not modify them.
65
+ * @returns The fit, or null if no point is complete.
66
+ */
67
+ export declare function linearRegression(points: readonly Point[]): RegressionFit | null;
68
+ //# sourceMappingURL=regression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regression.d.ts","sourceRoot":"","sources":["../../src/core/regression.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,8DAA8D;AAC9D,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,SAAS,MAAM,EAAE,CAI7E;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,gDAAgD;IAChD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,sEAAsE;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,kDAAkD;IAClD,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,iCAAiC;IACjC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,4BAA4B;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,4BAA4B;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,SAAS,KAAK,EAAE,GACvB,aAAa,GAAG,IAAI,CAkDtB"}
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Seedable random draws for the sampling demonstrations.
3
+ *
4
+ * R gets these from `runif()`, `rnorm()`, and `sample()`, which all read one
5
+ * global Mersenne Twister stream. This port makes the generator an argument
6
+ * instead. A pure core cannot hold a stream, and a demonstration that a
7
+ * student reloads must show the same numbers.
8
+ *
9
+ * The generator does not reproduce R's stream. No JavaScript generator does,
10
+ * and the port plan does not ask for it. The requirement is that one seed
11
+ * always gives one sequence, in this run and in every later run.
12
+ *
13
+ * The generator is mulberry32 (Tommy Ettinger, public domain): 32 bits of
14
+ * state, four integer operations per draw, a period of 2^32, and a published
15
+ * result on the gjrand test suite. It is small enough to read in full below,
16
+ * which matters more here than statistical strength — these draws teach the
17
+ * sampling distribution, they do not protect anything. Do not use this module
18
+ * for cryptography.
19
+ */
20
+ /** A source of uniform values in the interval [0, 1). */
21
+ export type Rng = () => number;
22
+ /** The parameters of `runif`. The defaults are the defaults of R's `runif`. */
23
+ export interface RunifOptions {
24
+ /** The low end of the interval. */
25
+ readonly min?: number;
26
+ /** The high end of the interval. The generator never returns it. */
27
+ readonly max?: number;
28
+ }
29
+ /** The parameters of `rnorm`. The defaults are the defaults of R's `rnorm`. */
30
+ export interface RnormOptions {
31
+ /** The center of the distribution. */
32
+ readonly mean?: number;
33
+ /** The standard deviation. A negative value gives NaN, as in R. */
34
+ readonly sd?: number;
35
+ }
36
+ /**
37
+ * The parameters of `rlnorm`. The defaults are the defaults of R's `rlnorm`.
38
+ *
39
+ * Both name the normal distribution behind the exponential, not the lognormal
40
+ * distribution itself.
41
+ */
42
+ export interface RlnormOptions {
43
+ /** The center of the normal behind the exponential. */
44
+ readonly meanlog?: number;
45
+ /**
46
+ * The standard deviation of the normal behind the exponential. A negative
47
+ * value gives NaN, as in R.
48
+ */
49
+ readonly sdlog?: number;
50
+ }
51
+ /**
52
+ * The parameters of `rcauchy`. The defaults are the defaults of R's
53
+ * `rcauchy`.
54
+ */
55
+ export interface RcauchyOptions {
56
+ /** The center of the distribution. It is the median, not a mean. */
57
+ readonly location?: number;
58
+ /** The half-width at half-maximum. A negative value gives NaN, as in R. */
59
+ readonly scale?: number;
60
+ }
61
+ /**
62
+ * Make a generator from a seed.
63
+ *
64
+ * Two generators of one seed give the same sequence. The seed keeps only its
65
+ * integer part, and only the low 32 bits of that.
66
+ *
67
+ * @param seed The start state. It must be finite.
68
+ * @returns A generator of uniform values in [0, 1).
69
+ * @throws RangeError If the seed is not finite.
70
+ */
71
+ export declare function seededRng(seed: number): Rng;
72
+ /**
73
+ * Draw uniform values.
74
+ *
75
+ * The function takes one value from the generator for each result, in order.
76
+ *
77
+ * An interval with `min` above `max`, or with a bound that is not finite,
78
+ * gives NaN for every result and takes nothing from the generator. R does the
79
+ * same, with a warning that a library cannot give.
80
+ *
81
+ * @param rng The source of randomness.
82
+ * @param n How many values to draw. It must be a non-negative integer.
83
+ * @param options The interval. The default is [0, 1).
84
+ * @returns The drawn values.
85
+ * @throws RangeError If n is negative or is not an integer.
86
+ */
87
+ export declare function runif(rng: Rng, n: number, options?: RunifOptions): number[];
88
+ /**
89
+ * Draw normal values.
90
+ *
91
+ * The function uses the Box-Muller transform, which makes two independent
92
+ * standard normal values from two uniform values. It takes the values in
93
+ * pairs: the first value of a pair gives the radius, the second gives the
94
+ * angle. An odd count discards the second value of the last pair, so the
95
+ * count of draws from the generator is always `2 * ceil(n / 2)`.
96
+ *
97
+ * The radius is `sqrt(-2 * log(1 - u))`, not `sqrt(-2 * log(u))`. The
98
+ * generator can return an exact 0 but never returns 1, and `log(0)` is not
99
+ * finite. The subtraction moves the open end of the interval to the point
100
+ * where the logarithm needs it.
101
+ *
102
+ * A `mean` that is not finite, or an `sd` that is negative or not finite,
103
+ * gives NaN for every result and takes nothing from the generator, as in R.
104
+ *
105
+ * @param rng The source of randomness.
106
+ * @param n How many values to draw. It must be a non-negative integer.
107
+ * @param options The center and the spread. The default is the standard
108
+ * normal distribution.
109
+ * @returns The drawn values.
110
+ * @throws RangeError If n is negative or is not an integer.
111
+ */
112
+ export declare function rnorm(rng: Rng, n: number, options?: RnormOptions): number[];
113
+ /**
114
+ * Draw Student t values.
115
+ *
116
+ * Each value is `z / sqrt(chiSquare / df)`: a standard normal draw over the
117
+ * square root of a scaled chi-square. The chi-square is the sum of `df`
118
+ * squared standard normal draws. The draws come in bulk: one `rnorm` call of
119
+ * length `n` for the numerators, then `df` further calls of length `n`, one
120
+ * per chi-square component. Each `rnorm` call takes `2 * ceil(n / 2)` values
121
+ * from the generator, so `rt` takes `(df + 1) * 2 * ceil(n / 2)`.
122
+ *
123
+ * R's `rt` accepts any `df > 0`, through a gamma sampler. A gamma sampler
124
+ * rejects and redraws, so it cannot state a draw count. This port accepts a
125
+ * positive integer `df` only, which the normal construction covers with a
126
+ * fixed draw count. A `df` that is not a positive finite integer gives NaN
127
+ * for every result and takes nothing from the generator, as `rnorm` does for
128
+ * a bad `sd`.
129
+ *
130
+ * @param rng The source of randomness.
131
+ * @param n How many values to draw. It must be a non-negative integer.
132
+ * @param df The degrees of freedom. It must be a positive integer.
133
+ * @returns The drawn values.
134
+ * @throws RangeError If n is negative or is not an integer.
135
+ */
136
+ export declare function rt(rng: Rng, n: number, df: number): number[];
137
+ /**
138
+ * Draw lognormal values.
139
+ *
140
+ * A lognormal value is the exponential of a normal one, so this draws from
141
+ * `rnorm` at `meanlog` and `sdlog` and exponentiates each value. The draw
142
+ * count is therefore `rnorm`'s, `2 * ceil(n / 2)`.
143
+ *
144
+ * The parameters name the normal distribution behind the exponential, not the
145
+ * distribution the function returns. R names them the same way. Every draw is
146
+ * above zero, and the shape leans right: the median is `exp(meanlog)` and the
147
+ * mean sits above it.
148
+ *
149
+ * A `meanlog` that is not finite, or an `sdlog` that is negative or not
150
+ * finite, gives NaN for every result and takes nothing from the generator, as
151
+ * `rnorm` does.
152
+ *
153
+ * @param rng The source of randomness.
154
+ * @param n How many values to draw. It must be a non-negative integer.
155
+ * @param options The center and the spread of the normal behind the
156
+ * exponential. The default is the standard normal, as in R.
157
+ * @returns The drawn values.
158
+ * @throws RangeError If n is negative or is not an integer.
159
+ */
160
+ export declare function rlnorm(rng: Rng, n: number, options?: RlnormOptions): number[];
161
+ /**
162
+ * Draw Cauchy values.
163
+ *
164
+ * This is R's `rcauchy(n, location, scale)`, by the inverse rule: the Cauchy
165
+ * quantile function is `location + scale * tan(pi * (u - 0.5))`, so applying
166
+ * it to uniform draws gives Cauchy draws. The draws come in one `runif` call
167
+ * of length `n`, so the function takes exactly `n` values from the generator.
168
+ *
169
+ * The Cauchy has no mean and no variance. Its center is the `location`, which
170
+ * is the median, and its spread is the `scale`, the half-width at half of the
171
+ * peak density. The tails are heavy enough that averages of draws do not
172
+ * settle, which is what the sampling demonstrations use it for.
173
+ *
174
+ * A `location` that is not finite, or a `scale` that is negative or not
175
+ * finite, gives NaN for every result and takes nothing from the generator,
176
+ * as in R.
177
+ *
178
+ * @param rng The source of randomness.
179
+ * @param n How many values to draw. It must be a non-negative integer.
180
+ * @param options The center and the spread. The default is the standard
181
+ * Cauchy distribution.
182
+ * @returns The drawn values.
183
+ * @throws RangeError If n is negative or is not an integer.
184
+ */
185
+ export declare function rcauchy(rng: Rng, n: number, options?: RcauchyOptions): number[];
186
+ /**
187
+ * Take values from a population, without replacement.
188
+ *
189
+ * This is R's `sample(values, k)`, which also samples without replacement by
190
+ * default. The algorithm is a partial Fisher-Yates shuffle: step `i` takes one
191
+ * value from the generator and swaps position `i` with position
192
+ * `i + floor(u * (length - i))`, so each step selects from the values that no
193
+ * earlier step took. The function takes exactly `k` values from the generator.
194
+ *
195
+ * The function copies the population. It does not modify the input.
196
+ *
197
+ * @param rng The source of randomness.
198
+ * @param values The population. Any element type is permitted.
199
+ * @param k How many values to take. It must be a non-negative integer.
200
+ * @returns The taken values, in the order the algorithm found them.
201
+ * @throws RangeError If k is negative, is not an integer, or is more than the
202
+ * size of the population. R gives the same error: "cannot take a sample
203
+ * larger than the population when 'replace = FALSE'".
204
+ */
205
+ export declare function sampleWithoutReplacement<T>(rng: Rng, values: readonly T[], k: number): T[];
206
+ //# sourceMappingURL=rng.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rng.d.ts","sourceRoot":"","sources":["../../src/core/rng.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,yDAAyD;AACzD,MAAM,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC;AAE/B,+EAA+E;AAC/E,MAAM,WAAW,YAAY;IAC3B,mCAAmC;IACnC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,oEAAoE;IACpE,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,+EAA+E;AAC/E,MAAM,WAAW,YAAY;IAC3B,sCAAsC;IACtC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,mEAAmE;IACnE,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,uDAAuD;IACvD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,oEAAoE;IACpE,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAa3C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CACnB,GAAG,EAAE,GAAG,EACR,CAAC,EAAE,MAAM,EACT,OAAO,GAAE,YAAiB,GACzB,MAAM,EAAE,CASV;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,KAAK,CACnB,GAAG,EAAE,GAAG,EACR,CAAC,EAAE,MAAM,EACT,OAAO,GAAE,YAAiB,GACzB,MAAM,EAAE,CAeV;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,CAgB5D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,MAAM,CACpB,GAAG,EAAE,GAAG,EACR,CAAC,EAAE,MAAM,EACT,OAAO,GAAE,aAAkB,GAC1B,MAAM,EAAE,CAGV;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,OAAO,CACrB,GAAG,EAAE,GAAG,EACR,CAAC,EAAE,MAAM,EACT,OAAO,GAAE,cAAmB,GAC3B,MAAM,EAAE,CAWV;AASD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,CAAC,EAAE,MAAM,GACR,CAAC,EAAE,CAmBL"}