twokeys 2.0.0 → 2.0.2
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/README.md +359 -0
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
# Twokeys
|
|
2
|
+
|
|
3
|
+
> A small data exploration and manipulation library, named after **John Tukey** — the legendary statistician who pioneered exploratory data analysis (EDA).
|
|
4
|
+
|
|
5
|
+
[](https://github.com/buley/twokeys/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/twokeys)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
## About John Tukey
|
|
10
|
+
|
|
11
|
+
John Wilder Tukey (1915–2000) revolutionized how we look at data. He invented the box plot, coined the terms "bit" and "software," and championed the idea that **looking at data** is just as important as modeling it. His book *Exploratory Data Analysis* (1977) changed statistics forever.
|
|
12
|
+
|
|
13
|
+
This library is named after him — a founding mind in [data exploration and analysis](http://en.wikipedia.org/wiki/Exploratory_data_analysis) and a personal hero of the author.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Summary Statistics**: Mean, median, mode, trimean, quartiles (hinges)
|
|
18
|
+
- **Outlier Detection**: Tukey fences (inner and outer)
|
|
19
|
+
- **Letter Values**: Extended quartiles (eighths, sixteenths, etc.)
|
|
20
|
+
- **Stem-and-Leaf**: Text-based distribution visualization
|
|
21
|
+
- **Ranking**: Full ranking with tie handling
|
|
22
|
+
- **Binning**: Histogram-style grouping
|
|
23
|
+
- **Smoothing**: Hanning filter, Tukey's 3RSSH smoothing
|
|
24
|
+
- **Transforms**: Logarithms, square roots, reciprocals
|
|
25
|
+
- **WASM Support**: Optional WebAssembly for maximum performance
|
|
26
|
+
- **Zero Dependencies**: Pure TypeScript, works everywhere
|
|
27
|
+
- **Tiny**: <3KB minified and gzipped
|
|
28
|
+
|
|
29
|
+
## Packages
|
|
30
|
+
|
|
31
|
+
| Package | Description |
|
|
32
|
+
|---------|-------------|
|
|
33
|
+
| `twokeys` | Core TypeScript library |
|
|
34
|
+
| `@buley/twokeys-wasm` | WebAssembly implementation with TypeScript fallback |
|
|
35
|
+
| `@buley/twokeys-types` | Shared Zod schemas for runtime validation |
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install twokeys
|
|
41
|
+
# or
|
|
42
|
+
bun add twokeys
|
|
43
|
+
# or
|
|
44
|
+
yarn add twokeys
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
For WASM acceleration (optional):
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install @buley/twokeys-wasm
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { Series } from 'twokeys';
|
|
57
|
+
|
|
58
|
+
// Create a series from your data
|
|
59
|
+
const series = new Series({ data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 100] });
|
|
60
|
+
|
|
61
|
+
// Get summary statistics
|
|
62
|
+
console.log(series.mean()); // 14.5
|
|
63
|
+
console.log(series.median()); // { datum: 5.5, depth: 5.5 }
|
|
64
|
+
console.log(series.trimean()); // Tukey's trimean
|
|
65
|
+
|
|
66
|
+
// Detect outliers (using Tukey fences)
|
|
67
|
+
console.log(series.outliers()); // [100]
|
|
68
|
+
|
|
69
|
+
// Get a full description
|
|
70
|
+
const desc = series.describe();
|
|
71
|
+
console.log(desc.summary);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Using WASM (Optional)
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { loadWasm, analyze, isWasmLoaded } from '@buley/twokeys-wasm';
|
|
78
|
+
|
|
79
|
+
// Load WASM (falls back to TypeScript if unavailable)
|
|
80
|
+
await loadWasm();
|
|
81
|
+
|
|
82
|
+
console.log(isWasmLoaded()); // true if WASM loaded
|
|
83
|
+
|
|
84
|
+
// Use the same API - automatically uses WASM when available
|
|
85
|
+
const result = analyze([1, 2, 3, 4, 5, 6, 7, 8, 9, 100]);
|
|
86
|
+
console.log(result.summary.outliers); // [100]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Benchmarks
|
|
90
|
+
|
|
91
|
+
Performance on different dataset sizes (operations per second, higher is better):
|
|
92
|
+
|
|
93
|
+
### TypeScript Implementation
|
|
94
|
+
|
|
95
|
+
| Method | 100 elements | 1,000 elements | 10,000 elements |
|
|
96
|
+
|--------|-------------:|---------------:|----------------:|
|
|
97
|
+
| `sorted()` | 224,599 | 14,121 | 874 |
|
|
98
|
+
| `median()` | 199,397 | 14,127 | 876 |
|
|
99
|
+
| `mean()` | 550,610 | 413,551 | 68,399 |
|
|
100
|
+
| `mode()` | 87,665 | 6,738 | 431 |
|
|
101
|
+
| `fences()` | 238,486 | 13,270 | 864 |
|
|
102
|
+
| `outliers()` | 210,058 | 12,584 | 854 |
|
|
103
|
+
| `smooth()` | 61,268 | 1,599 | 31 |
|
|
104
|
+
| `describe()` | 15,642 | 952 | 29 |
|
|
105
|
+
|
|
106
|
+
### v2.0 Performance Improvements
|
|
107
|
+
|
|
108
|
+
Compared to v1.x (CoffeeScript), v2.0 TypeScript is dramatically faster:
|
|
109
|
+
|
|
110
|
+
| Method | v1.x (10K) | v2.0 (10K) | Improvement |
|
|
111
|
+
|--------|------------|------------|-------------|
|
|
112
|
+
| `median()` | 6 ops/sec | 876 ops/sec | **146x faster** |
|
|
113
|
+
| `counts()` | 1 ops/sec | 606 ops/sec | **606x faster** |
|
|
114
|
+
| `fences()` | 5 ops/sec | 864 ops/sec | **173x faster** |
|
|
115
|
+
| `describe()` | 1 ops/sec | 29 ops/sec | **29x faster** |
|
|
116
|
+
|
|
117
|
+
Key optimizations:
|
|
118
|
+
- O(1) index-based median (was O(n²) recursive)
|
|
119
|
+
- Map-based frequency counting (was O(n²) recursive)
|
|
120
|
+
- Eliminated unnecessary array copying in smoothing
|
|
121
|
+
|
|
122
|
+
## Example Output
|
|
123
|
+
|
|
124
|
+
Applying `describe()` to a Series returns a complete analysis:
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
const series = new Series({ data: [48, 59, 63, 30, 57, 92, 73, 47, 31, 5] });
|
|
128
|
+
const analysis = series.describe();
|
|
129
|
+
|
|
130
|
+
// Result:
|
|
131
|
+
{
|
|
132
|
+
"original": [48, 59, 63, 30, 57, 92, 73, 47, 31, 5],
|
|
133
|
+
"summary": {
|
|
134
|
+
"median": { "datum": 52.5, "depth": 5.5 },
|
|
135
|
+
"mean": 50.5,
|
|
136
|
+
"hinges": [{ "datum": 31, "depth": 3 }, { "datum": 63, "depth": 8 }],
|
|
137
|
+
"adjacent": [30, 92],
|
|
138
|
+
"outliers": [],
|
|
139
|
+
"extremes": [5, 92],
|
|
140
|
+
"iqr": 32,
|
|
141
|
+
"fences": [4.5, 100.5]
|
|
142
|
+
},
|
|
143
|
+
"smooths": {
|
|
144
|
+
"smooth": [48, 30, 57, 57, 57, 47, 31, 5, 5, 5],
|
|
145
|
+
"hanning": [48, 61, 46.5, 43.5, 74.5, 82.5, 60, 39, 18, 5]
|
|
146
|
+
},
|
|
147
|
+
"transforms": {
|
|
148
|
+
"logs": [3.87, 4.08, 4.14, ...],
|
|
149
|
+
"roots": [6.93, 7.68, 7.94, ...],
|
|
150
|
+
"inverse": [0.021, 0.017, 0.016, ...]
|
|
151
|
+
},
|
|
152
|
+
"sorted": [5, 30, 31, 47, 48, 57, 59, 63, 73, 92],
|
|
153
|
+
"ranked": { "up": {...}, "down": {...}, "groups": {...} },
|
|
154
|
+
"binned": { "bins": 4, "width": 26, "binned": {...} }
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## API
|
|
159
|
+
|
|
160
|
+
### Series
|
|
161
|
+
|
|
162
|
+
The `Series` class provides methods for exploring 1-dimensional numerical data.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { Series } from 'twokeys';
|
|
166
|
+
|
|
167
|
+
const series = new Series({ data: [1, 2, 3, 4, 5] });
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### Summary Statistics
|
|
171
|
+
|
|
172
|
+
| Method | Description |
|
|
173
|
+
|--------|-------------|
|
|
174
|
+
| `mean()` | Arithmetic mean |
|
|
175
|
+
| `median()` | Median value and depth |
|
|
176
|
+
| `mode()` | Most frequent value(s) |
|
|
177
|
+
| `trimean()` | Tukey's trimean: (Q1 + 2×median + Q3) / 4 |
|
|
178
|
+
| `extremes()` | [min, max] values |
|
|
179
|
+
| `hinges()` | Quartile boundaries (Q1, Q3) |
|
|
180
|
+
| `iqr()` | Interquartile range |
|
|
181
|
+
|
|
182
|
+
#### Outlier Detection
|
|
183
|
+
|
|
184
|
+
| Method | Description |
|
|
185
|
+
|--------|-------------|
|
|
186
|
+
| `fences()` | Inner fence boundaries (1.5 × IQR) |
|
|
187
|
+
| `outer()` | Outer fence boundaries (3 × IQR) |
|
|
188
|
+
| `outliers()` | Values outside inner fences |
|
|
189
|
+
| `inside()` | Values within fences |
|
|
190
|
+
| `outside()` | Values outside outer fences |
|
|
191
|
+
| `adjacent()` | Most extreme non-outlier values |
|
|
192
|
+
|
|
193
|
+
#### Letter Values & Visualization
|
|
194
|
+
|
|
195
|
+
| Method | Description |
|
|
196
|
+
|--------|-------------|
|
|
197
|
+
| `letterValues()` | Extended quartiles (M, F, E, D, C, B, A...) |
|
|
198
|
+
| `stemLeaf()` | Stem-and-leaf text display |
|
|
199
|
+
| `midSummaries()` | Symmetric quantile pair averages |
|
|
200
|
+
|
|
201
|
+
#### Ranking & Counting
|
|
202
|
+
|
|
203
|
+
| Method | Description |
|
|
204
|
+
|--------|-------------|
|
|
205
|
+
| `sorted()` | Sorted copy of data |
|
|
206
|
+
| `ranked()` | Rank information with tie handling |
|
|
207
|
+
| `counts()` | Frequency of each value |
|
|
208
|
+
| `binned()` | Histogram-style bins |
|
|
209
|
+
|
|
210
|
+
#### Transforms
|
|
211
|
+
|
|
212
|
+
| Method | Description |
|
|
213
|
+
|--------|-------------|
|
|
214
|
+
| `logs()` | Natural logarithm of each value |
|
|
215
|
+
| `roots()` | Square root of each value |
|
|
216
|
+
| `inverse()` | Reciprocal (1/x) of each value |
|
|
217
|
+
|
|
218
|
+
#### Smoothing
|
|
219
|
+
|
|
220
|
+
| Method | Description |
|
|
221
|
+
|--------|-------------|
|
|
222
|
+
| `hanning()` | Hanning filter (running averages) |
|
|
223
|
+
| `smooth()` | Tukey's 3RSSH smoothing |
|
|
224
|
+
| `rough()` | Residuals (original - smooth) |
|
|
225
|
+
|
|
226
|
+
#### Full Description
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
series.describe();
|
|
230
|
+
// Returns complete analysis including all of the above
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Points
|
|
234
|
+
|
|
235
|
+
The `Points` class handles n-dimensional point data.
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
import { Points } from 'twokeys';
|
|
239
|
+
|
|
240
|
+
// 100 random 2D points
|
|
241
|
+
const points = new Points({ count: 100, dimensionality: 2 });
|
|
242
|
+
|
|
243
|
+
// Or provide your own data
|
|
244
|
+
const myPoints = new Points({ data: [[1, 2], [3, 4], [5, 6]] });
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Twokeys
|
|
248
|
+
|
|
249
|
+
The main class provides factory methods and utilities.
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import Twokeys from 'twokeys';
|
|
253
|
+
|
|
254
|
+
// Generate random data
|
|
255
|
+
const randomData = Twokeys.randomSeries(100, 50); // 100 values, max 50
|
|
256
|
+
const randomPoints = Twokeys.randomPoints(50, 3); // 50 3D points
|
|
257
|
+
|
|
258
|
+
// Access classes
|
|
259
|
+
const series = new Twokeys.Series({ data: [1, 2, 3] });
|
|
260
|
+
const points = new Twokeys.Points(100);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Examples
|
|
264
|
+
|
|
265
|
+
### Box Plot Data
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
const series = new Series({ data: myData });
|
|
269
|
+
|
|
270
|
+
const boxPlot = {
|
|
271
|
+
min: series.extremes()[0],
|
|
272
|
+
q1: series.hinges()[0].datum,
|
|
273
|
+
median: series.median().datum,
|
|
274
|
+
q3: series.hinges()[1].datum,
|
|
275
|
+
max: series.extremes()[1],
|
|
276
|
+
outliers: series.outliers(),
|
|
277
|
+
};
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Outlier Detection
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
const series = new Series({ data: measurements });
|
|
284
|
+
|
|
285
|
+
// Inner fences: 1.5 × IQR from hinges
|
|
286
|
+
const suspicious = series.outliers();
|
|
287
|
+
|
|
288
|
+
// Outer fences: 3 × IQR from hinges
|
|
289
|
+
const extreme = series.outside();
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### Letter Values Display
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
const series = new Series({ data: myData });
|
|
296
|
+
|
|
297
|
+
// Get extended quartiles
|
|
298
|
+
const lv = series.letterValues();
|
|
299
|
+
// [
|
|
300
|
+
// { letter: 'M', depth: 10.5, lower: 52.5, upper: 52.5, mid: 52.5, spread: 0 },
|
|
301
|
+
// { letter: 'F', depth: 5, lower: 31, upper: 73, mid: 52, spread: 42 },
|
|
302
|
+
// { letter: 'E', depth: 3, lower: 30, upper: 82, mid: 56, spread: 52 },
|
|
303
|
+
// ...
|
|
304
|
+
// ]
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Stem-and-Leaf Display
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
const series = new Series({ data: myData });
|
|
311
|
+
|
|
312
|
+
const { display } = series.stemLeaf();
|
|
313
|
+
// [
|
|
314
|
+
// " 0 | 5",
|
|
315
|
+
// " 3 | 0 1",
|
|
316
|
+
// " 4 | 7 8",
|
|
317
|
+
// " 5 | 7 9",
|
|
318
|
+
// " 6 | 3",
|
|
319
|
+
// " 7 | 3",
|
|
320
|
+
// " 9 | 2"
|
|
321
|
+
// ]
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### Data Transformation
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
const series = new Series({ data: skewedData });
|
|
328
|
+
|
|
329
|
+
// Try different transforms to normalize
|
|
330
|
+
const logTransformed = series.logs();
|
|
331
|
+
const sqrtTransformed = series.roots();
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Development
|
|
335
|
+
|
|
336
|
+
```bash
|
|
337
|
+
# Install dependencies
|
|
338
|
+
bun install
|
|
339
|
+
|
|
340
|
+
# Run tests
|
|
341
|
+
bun test
|
|
342
|
+
|
|
343
|
+
# Run tests with coverage
|
|
344
|
+
bun test --coverage
|
|
345
|
+
|
|
346
|
+
# Build all packages
|
|
347
|
+
bun run build
|
|
348
|
+
|
|
349
|
+
# Run benchmark
|
|
350
|
+
bun run bench.ts
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
## License
|
|
354
|
+
|
|
355
|
+
MIT
|
|
356
|
+
|
|
357
|
+
---
|
|
358
|
+
|
|
359
|
+
*"The best thing about being a statistician is that you get to play in everyone's backyard."* — John Tukey
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "twokeys",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "A small data exploration and manipulation library, named after John Tukey",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
18
19
|
],
|
|
19
20
|
"scripts": {
|
|
20
21
|
"build": "tsup",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
},
|
|
49
50
|
"homepage": "https://github.com/buley/twokeys#readme",
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"@buley/twokeys-types": "
|
|
52
|
+
"@buley/twokeys-types": "^2.0.0"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@types/bun": "latest",
|