@sipemu/anofox-statistics 0.4.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/LICENSE +21 -0
- package/README.md +281 -0
- package/anofox_statistics_js.d.ts +744 -0
- package/anofox_statistics_js.js +1758 -0
- package/anofox_statistics_js_bg.wasm +0 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Simon Müller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# @sipemu/anofox-statistics
|
|
2
|
+
|
|
3
|
+
A comprehensive statistical hypothesis testing library compiled to WebAssembly for JavaScript/TypeScript applications. All tests are validated against R.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @sipemu/anofox-statistics
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Web/Browser
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import init, {
|
|
17
|
+
tTest,
|
|
18
|
+
shapiroWilk,
|
|
19
|
+
mannWhitneyU,
|
|
20
|
+
oneWayAnova,
|
|
21
|
+
JsTTestKind,
|
|
22
|
+
JsAlternative,
|
|
23
|
+
JsAnovaKind
|
|
24
|
+
} from '@sipemu/anofox-statistics';
|
|
25
|
+
|
|
26
|
+
// Initialize the WASM module
|
|
27
|
+
await init();
|
|
28
|
+
|
|
29
|
+
// Two-sample t-test
|
|
30
|
+
const result = tTest(
|
|
31
|
+
new Float64Array([1.2, 2.3, 3.1, 4.5, 5.2]),
|
|
32
|
+
new Float64Array([2.1, 3.4, 4.2, 5.6, 6.1]),
|
|
33
|
+
JsTTestKind.Welch,
|
|
34
|
+
JsAlternative.TwoSided,
|
|
35
|
+
0.0, // null hypothesis value
|
|
36
|
+
0.95 // confidence level
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
console.log(result);
|
|
40
|
+
// { statistic: -2.34, df: 7.89, p_value: 0.047, mean_x: 3.26, mean_y: 4.28, ... }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Node.js
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const { tTest, JsTTestKind, JsAlternative } = require('@sipemu/anofox-statistics');
|
|
47
|
+
|
|
48
|
+
// Use directly (WASM auto-initializes)
|
|
49
|
+
const result = tTest(
|
|
50
|
+
new Float64Array([1.2, 2.3, 3.1]),
|
|
51
|
+
new Float64Array([2.1, 3.4, 4.2]),
|
|
52
|
+
JsTTestKind.Welch,
|
|
53
|
+
JsAlternative.TwoSided
|
|
54
|
+
);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Available Tests
|
|
58
|
+
|
|
59
|
+
### Parametric Tests
|
|
60
|
+
|
|
61
|
+
| Function | Description |
|
|
62
|
+
|----------|-------------|
|
|
63
|
+
| `tTest` | Student's, Welch's, or paired t-test |
|
|
64
|
+
| `yuenTest` | Yuen's test for trimmed means (robust) |
|
|
65
|
+
| `oneWayAnova` | One-way ANOVA (Fisher or Welch) |
|
|
66
|
+
| `brownForsythe` | Brown-Forsythe test for homogeneity of variances |
|
|
67
|
+
|
|
68
|
+
### Nonparametric Tests
|
|
69
|
+
|
|
70
|
+
| Function | Description |
|
|
71
|
+
|----------|-------------|
|
|
72
|
+
| `mannWhitneyU` | Mann-Whitney U test (Wilcoxon rank-sum) |
|
|
73
|
+
| `wilcoxonSignedRank` | Wilcoxon signed-rank test |
|
|
74
|
+
| `kruskalWallis` | Kruskal-Wallis H test |
|
|
75
|
+
| `brunnerMunzel` | Brunner-Munzel test |
|
|
76
|
+
|
|
77
|
+
### Distributional Tests
|
|
78
|
+
|
|
79
|
+
| Function | Description |
|
|
80
|
+
|----------|-------------|
|
|
81
|
+
| `shapiroWilk` | Shapiro-Wilk normality test |
|
|
82
|
+
| `kolmogorovSmirnov` | Kolmogorov-Smirnov test |
|
|
83
|
+
| `andersonDarling` | Anderson-Darling normality test |
|
|
84
|
+
| `jarqueBera` | Jarque-Bera normality test |
|
|
85
|
+
|
|
86
|
+
### Categorical Tests
|
|
87
|
+
|
|
88
|
+
| Function | Description |
|
|
89
|
+
|----------|-------------|
|
|
90
|
+
| `chiSquaredTest` | Chi-squared test for independence |
|
|
91
|
+
| `fisherExact` | Fisher's exact test |
|
|
92
|
+
| `binomialTest` | Exact binomial test |
|
|
93
|
+
| `mcnemarTest` | McNemar's test for paired proportions |
|
|
94
|
+
|
|
95
|
+
### Correlation Tests
|
|
96
|
+
|
|
97
|
+
| Function | Description |
|
|
98
|
+
|----------|-------------|
|
|
99
|
+
| `pearsonCorrelation` | Pearson correlation with significance |
|
|
100
|
+
| `spearmanCorrelation` | Spearman rank correlation |
|
|
101
|
+
| `kendallCorrelation` | Kendall's tau correlation |
|
|
102
|
+
| `icc` | Intraclass correlation coefficient |
|
|
103
|
+
|
|
104
|
+
### Equivalence Tests (TOST)
|
|
105
|
+
|
|
106
|
+
| Function | Description |
|
|
107
|
+
|----------|-------------|
|
|
108
|
+
| `tostTwoSample` | TOST for two independent samples |
|
|
109
|
+
| `tostPaired` | TOST for paired samples |
|
|
110
|
+
| `tostCorrelation` | TOST for correlation equivalence |
|
|
111
|
+
| `tostBootstrap` | Bootstrap TOST |
|
|
112
|
+
|
|
113
|
+
### Modern/Kernel Tests
|
|
114
|
+
|
|
115
|
+
| Function | Description |
|
|
116
|
+
|----------|-------------|
|
|
117
|
+
| `energyDistanceTest1d` | Energy distance test (1D) |
|
|
118
|
+
| `energyDistanceTest` | Energy distance test (multivariate) |
|
|
119
|
+
| `mmdTest1d` | Maximum Mean Discrepancy test (1D) |
|
|
120
|
+
| `mmdTest` | MMD test (multivariate) |
|
|
121
|
+
|
|
122
|
+
### Forecast Comparison Tests
|
|
123
|
+
|
|
124
|
+
| Function | Description |
|
|
125
|
+
|----------|-------------|
|
|
126
|
+
| `dieboldMariano` | Diebold-Mariano test |
|
|
127
|
+
| `clarkWest` | Clark-West test for nested models |
|
|
128
|
+
| `spaTest` | Superior Predictive Ability test |
|
|
129
|
+
| `mspeAdjustedSpa` | MSPE-adjusted SPA test |
|
|
130
|
+
| `modelConfidenceSet` | Model Confidence Set procedure |
|
|
131
|
+
|
|
132
|
+
### Resampling Tests
|
|
133
|
+
|
|
134
|
+
| Function | Description |
|
|
135
|
+
|----------|-------------|
|
|
136
|
+
| `permutationTTest` | Permutation t-test |
|
|
137
|
+
| `permutationCorrelation` | Permutation correlation test |
|
|
138
|
+
| `bootstrapCI` | Bootstrap confidence intervals |
|
|
139
|
+
|
|
140
|
+
## Enums
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
// T-test variants
|
|
144
|
+
enum JsTTestKind { Welch, Student, Paired }
|
|
145
|
+
|
|
146
|
+
// Alternative hypotheses
|
|
147
|
+
enum JsAlternative { TwoSided, Less, Greater }
|
|
148
|
+
|
|
149
|
+
// ANOVA variants
|
|
150
|
+
enum JsAnovaKind { Fisher, Welch }
|
|
151
|
+
|
|
152
|
+
// Kernel types for MMD
|
|
153
|
+
enum JsKernel { Gaussian, Laplacian, Linear }
|
|
154
|
+
|
|
155
|
+
// Loss functions for forecast comparison
|
|
156
|
+
enum JsLossFunction { SquaredError, AbsoluteError }
|
|
157
|
+
|
|
158
|
+
// Variance estimators
|
|
159
|
+
enum JsVarEstimator { Acf, Bartlett }
|
|
160
|
+
|
|
161
|
+
// MCS statistics
|
|
162
|
+
enum JsMCSStatistic { Max, Range }
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Examples
|
|
166
|
+
|
|
167
|
+
### Normality Testing
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
import init, { shapiroWilk, andersonDarling } from '@sipemu/anofox-statistics';
|
|
171
|
+
|
|
172
|
+
await init();
|
|
173
|
+
|
|
174
|
+
const data = new Float64Array([2.3, 3.1, 2.8, 3.5, 2.9, 3.2, 2.7]);
|
|
175
|
+
|
|
176
|
+
const sw = shapiroWilk(data);
|
|
177
|
+
console.log(`Shapiro-Wilk: W=${sw.statistic.toFixed(4)}, p=${sw.p_value.toFixed(4)}`);
|
|
178
|
+
|
|
179
|
+
const ad = andersonDarling(data);
|
|
180
|
+
console.log(`Anderson-Darling: A=${ad.statistic.toFixed(4)}, p=${ad.p_value.toFixed(4)}`);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### ANOVA with Post-hoc
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
import init, { oneWayAnova, JsAnovaKind } from '@sipemu/anofox-statistics';
|
|
187
|
+
|
|
188
|
+
await init();
|
|
189
|
+
|
|
190
|
+
const groups = [
|
|
191
|
+
new Float64Array([23, 25, 28, 31, 27]),
|
|
192
|
+
new Float64Array([31, 33, 35, 37, 34]),
|
|
193
|
+
new Float64Array([41, 43, 45, 47, 44])
|
|
194
|
+
];
|
|
195
|
+
|
|
196
|
+
const result = oneWayAnova(groups, JsAnovaKind.Fisher);
|
|
197
|
+
console.log(`F(${result.df_between}, ${result.df_within}) = ${result.statistic.toFixed(2)}`);
|
|
198
|
+
console.log(`p-value: ${result.p_value.toFixed(6)}`);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Equivalence Testing (TOST)
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
import init, { tostTwoSample, JsAlternative } from '@sipemu/anofox-statistics';
|
|
205
|
+
|
|
206
|
+
await init();
|
|
207
|
+
|
|
208
|
+
const treatment = new Float64Array([10.2, 11.1, 9.8, 10.5, 10.9]);
|
|
209
|
+
const control = new Float64Array([10.0, 10.8, 9.9, 10.3, 10.7]);
|
|
210
|
+
|
|
211
|
+
// Test equivalence within bounds of -1 to +1
|
|
212
|
+
const result = tostTwoSample(treatment, control, -1.0, 1.0, 0.95);
|
|
213
|
+
console.log(`Equivalent: ${result.equivalent}`);
|
|
214
|
+
console.log(`TOST p-value: ${result.p_value.toFixed(4)}`);
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Forecast Model Comparison
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
import init, {
|
|
221
|
+
dieboldMariano,
|
|
222
|
+
JsLossFunction,
|
|
223
|
+
JsAlternative,
|
|
224
|
+
JsVarEstimator
|
|
225
|
+
} from '@sipemu/anofox-statistics';
|
|
226
|
+
|
|
227
|
+
await init();
|
|
228
|
+
|
|
229
|
+
const errors1 = new Float64Array([0.5, -0.3, 0.2, -0.1, 0.4]);
|
|
230
|
+
const errors2 = new Float64Array([0.3, -0.5, 0.1, 0.2, -0.2]);
|
|
231
|
+
|
|
232
|
+
const dm = dieboldMariano(
|
|
233
|
+
errors1,
|
|
234
|
+
errors2,
|
|
235
|
+
JsLossFunction.SquaredError,
|
|
236
|
+
1, // horizon
|
|
237
|
+
JsAlternative.TwoSided,
|
|
238
|
+
JsVarEstimator.Bartlett
|
|
239
|
+
);
|
|
240
|
+
console.log(`DM statistic: ${dm.statistic.toFixed(4)}, p-value: ${dm.p_value.toFixed(4)}`);
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## TypeScript Support
|
|
244
|
+
|
|
245
|
+
Full TypeScript definitions are included. All functions and enums are properly typed:
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import init, {
|
|
249
|
+
tTest,
|
|
250
|
+
JsTTestKind,
|
|
251
|
+
JsAlternative
|
|
252
|
+
} from '@sipemu/anofox-statistics';
|
|
253
|
+
|
|
254
|
+
await init();
|
|
255
|
+
|
|
256
|
+
const result = tTest(
|
|
257
|
+
new Float64Array([1, 2, 3]),
|
|
258
|
+
new Float64Array([4, 5, 6]),
|
|
259
|
+
JsTTestKind.Welch,
|
|
260
|
+
JsAlternative.TwoSided,
|
|
261
|
+
0.0,
|
|
262
|
+
0.95
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
// TypeScript knows result has: statistic, df, p_value, mean_x, mean_y, conf_int, etc.
|
|
266
|
+
console.log(result.p_value);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Validation
|
|
270
|
+
|
|
271
|
+
All statistical tests are validated against R's implementation using extensive test suites. Results match R's output within numerical precision (typically 10^-10).
|
|
272
|
+
|
|
273
|
+
## License
|
|
274
|
+
|
|
275
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
276
|
+
|
|
277
|
+
## Links
|
|
278
|
+
|
|
279
|
+
- [GitHub Repository](https://github.com/sipemu/anofox-statistics-rs)
|
|
280
|
+
- [Rust crate on crates.io](https://crates.io/crates/anofox-statistics)
|
|
281
|
+
- [API Documentation](https://docs.rs/anofox-statistics)
|