probabilistic_machines 0.1.3-aarch64-linux → 0.6.1-aarch64-linux
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +70 -0
- data/README.md +166 -58
- data/lib/probabilistic_machines/version.rb +1 -1
- data/lib/probabilistic_machines.rb +40 -2
- data/lib/probabilistic_machines_native/probabilistic_machines_native.so +0 -0
- data/sig/probabilistic_machines.rbs +129 -56
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f5d849e10a9c1b18c5cba176c44d2fae0ae4a1f17b197621ed141581144fc353
|
|
4
|
+
data.tar.gz: d8196a293206f8abcf635bd2778efee509aa1c2683907323379490a29c688332
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8dc697daa111e70b584a02bdfcf837665ba58379fcfcd4608c843f811ab2e3fab55baaa04941c8f1fa81e2d8798dd58a49577e0ac2dd626fd14808197bffba98
|
|
7
|
+
data.tar.gz: 5a258710ac2cee04cf6d739fdbe1e227bbd5d7e414ad19f473183f5f6150fe0eb6fb5efe3ffce8c866e1e3d1ef811727f07554df67d1a7446cfa8e1ec04cdba6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,75 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.6.1](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.6.0...probabilistic_machines/v0.6.1) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* fix wrapped classes at load so building a value runs no Ruby ([48b4a0b](https://github.com/seuros/probabilistic_machines/commit/48b4a0b4328216cb6700520f2c98e0c22da6f4aa))
|
|
9
|
+
|
|
10
|
+
## [0.6.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.5.0...probabilistic_machines/v0.6.0) (2026-11-16)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚠ BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* refuse a TDigest quantile outside 0..=1
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* refuse a TDigest quantile outside 0..=1 ([848858f](https://github.com/seuros/probabilistic_machines/commit/848858fd54ce7d158d419584dafa811efd25bda4))
|
|
20
|
+
|
|
21
|
+
## [0.5.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.4.0...probabilistic_machines/v0.5.0) (2026-11-14)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### ⚠ BREAKING CHANGES
|
|
25
|
+
|
|
26
|
+
* adopt matryona 0.4 utf8_with so key errors name the received class
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* adopt matryona 0.4 utf8_with so key errors name the received class ([b6e8546](https://github.com/seuros/probabilistic_machines/commit/b6e85463277321ac5363720bb94b1dc51d4adff6))
|
|
31
|
+
|
|
32
|
+
## [0.4.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.3.0...probabilistic_machines/v0.4.0) (2026-11-14)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### ⚠ BREAKING CHANGES
|
|
36
|
+
|
|
37
|
+
* adopt matryona 0.3 exact strings, real/finite and named merge partners
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* adopt matryona 0.3 exact strings, real/finite and named merge partners ([bea30f3](https://github.com/seuros/probabilistic_machines/commit/bea30f3f9ffac8d007535993fd6337e91113cc28))
|
|
42
|
+
|
|
43
|
+
## [0.3.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.2.0...probabilistic_machines/v0.3.0) (2026-11-14)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
### ⚠ BREAKING CHANGES
|
|
47
|
+
|
|
48
|
+
* adopt matryona-machines 0.2: exact merge partners, symbols! tables, caret rb-sys, missing_docs
|
|
49
|
+
|
|
50
|
+
### Features
|
|
51
|
+
|
|
52
|
+
* adopt matryona-machines 0.2: exact merge partners, symbols! tables, caret rb-sys, missing_docs ([0baae63](https://github.com/seuros/probabilistic_machines/commit/0baae63c30b5a53bdf57646cc590c81877b5ff62))
|
|
53
|
+
|
|
54
|
+
## [0.2.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.4...probabilistic_machines/v0.2.0) (2026-11-14)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### ⚠ BREAKING CHANGES
|
|
58
|
+
|
|
59
|
+
* private Hitter/Centroid fields, refuse non-UTF-8 keys and non-canonical HeavyHitters frames, sealed exact-class values
|
|
60
|
+
|
|
61
|
+
### Features
|
|
62
|
+
|
|
63
|
+
* private Hitter/Centroid fields, refuse non-UTF-8 keys and non-canonical HeavyHitters frames, sealed exact-class values ([acbb087](https://github.com/seuros/probabilistic_machines/commit/acbb0872a0c7fb67bffab4fca2859b40019650d0))
|
|
64
|
+
|
|
65
|
+
## [0.1.4](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.3...probabilistic_machines/v0.1.4) (2026-11-06)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Bug Fixes
|
|
69
|
+
|
|
70
|
+
* keep test modules out of the packaged crate ([e3c1a1f](https://github.com/seuros/probabilistic_machines/commit/e3c1a1f7376dbada6efe782c1d4d264273483432))
|
|
71
|
+
* share frame encode/decode helpers across sketches ([067d23d](https://github.com/seuros/probabilistic_machines/commit/067d23db55b8b9068fdef445a7f6dee483961a82))
|
|
72
|
+
|
|
3
73
|
## [0.1.3](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.2...probabilistic_machines/v0.1.3) (2026-11-01)
|
|
4
74
|
|
|
5
75
|
|
data/README.md
CHANGED
|
@@ -18,7 +18,7 @@ is the same size on the way home as it was at launch.
|
|
|
18
18
|
```ruby
|
|
19
19
|
require 'probabilistic_machines'
|
|
20
20
|
|
|
21
|
-
visitors = ProbabilisticMachines::HyperLogLog.new(
|
|
21
|
+
visitors = ProbabilisticMachines::HyperLogLog.new(4096) # 4 KiB
|
|
22
22
|
50_000.times { |id| visitors.add("user-#{id % 12_000}") }
|
|
23
23
|
|
|
24
24
|
visitors.estimate # => 12_361.2
|
|
@@ -41,20 +41,21 @@ and it needs Ruby 4.0 and a Rust toolchain (or a platform gem).
|
|
|
41
41
|
## 📐 Sizes Are a Fixed Menu
|
|
42
42
|
|
|
43
43
|
The crate sizes every structure by a const generic, so each size is a separately
|
|
44
|
-
compiled type. The
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
44
|
+
compiled type. The constructor takes the size positionally, as the Rust type
|
|
45
|
+
takes its parameters; the extension carries the sizes in each class's constant
|
|
46
|
+
and raises `RangeError` for anything else (`TypeError` for a non-Integer):
|
|
47
|
+
|
|
48
|
+
| Class | Constructor | Supported (constant) |
|
|
49
|
+
| ---------------- | -------------------------------- | ---------------------------------------------------- |
|
|
50
|
+
| `HyperLogLog` | `new(registers)` | `REGISTERS`: 1024, 4096, 16384, 65536 |
|
|
51
|
+
| `CountMinSketch` | `new(width, depth)` | `WIDTHS`: 1024, 4096 by `DEPTHS`: 4, 8 (every pair) |
|
|
52
|
+
| `CuckooFilter` | `new(buckets)` | `BUCKETS`: 1024, 4096, 16384, 65536 |
|
|
53
|
+
| `HeavyHitters` | `new(counters)` | `COUNTERS`: 16, 64, 256 |
|
|
54
|
+
| `TDigest` | `new(compression)` | `MIN_COMPRESSION` (10) through 65535 |
|
|
54
55
|
|
|
55
56
|
```ruby
|
|
56
|
-
ProbabilisticMachines::HyperLogLog.new(
|
|
57
|
-
#
|
|
57
|
+
ProbabilisticMachines::HyperLogLog.new(3000)
|
|
58
|
+
# RangeError: registers 3000 is not supported, expected one of [1024, 4096, 16384, 65536]
|
|
58
59
|
```
|
|
59
60
|
|
|
60
61
|
---
|
|
@@ -62,14 +63,22 @@ ProbabilisticMachines::HyperLogLog.new(registers: 3000)
|
|
|
62
63
|
## 🌌 How Many Distinct
|
|
63
64
|
|
|
64
65
|
```ruby
|
|
65
|
-
hll = ProbabilisticMachines::HyperLogLog.new(
|
|
66
|
+
hll = ProbabilisticMachines::HyperLogLog.new(4096)
|
|
66
67
|
hll.add('user-1')
|
|
67
68
|
hll.add_hash(0x9e37_79b9_7f4a_7c15) # for streams that ship hashes, not keys
|
|
68
69
|
hll.estimate # Float
|
|
69
70
|
hll.standard_error # 1.04 / sqrt(registers)
|
|
70
|
-
hll.merge(other) # exact union
|
|
71
|
+
hll.merge(other) # :merged, exact union; :wrong_shape for another size
|
|
71
72
|
```
|
|
72
73
|
|
|
74
|
+
Nothing here refuses but a merge across sizes: registers only rise, so `add`
|
|
75
|
+
and `merge` are idempotent and have no limit to hit. The estimate is the same
|
|
76
|
+
Float on every machine whose `f64` is IEEE 754 binary64, rounded per operation:
|
|
77
|
+
linear counting takes its logarithm in integer fixed point from
|
|
78
|
+
`sliderule-machines`, within `10^-10` items of the exact value. A target that
|
|
79
|
+
evaluates in wider registers (32-bit x86 with the x87 FPU, no SSE2) may round
|
|
80
|
+
differently in the last bit.
|
|
81
|
+
|
|
73
82
|
Memory is one byte per register, so 4096 registers cost 4 KiB whether the stream
|
|
74
83
|
held ten ids or ten billion.
|
|
75
84
|
|
|
@@ -78,13 +87,20 @@ held ten ids or ten billion.
|
|
|
78
87
|
## 📊 How Often Each
|
|
79
88
|
|
|
80
89
|
```ruby
|
|
81
|
-
cms = ProbabilisticMachines::CountMinSketch.new(
|
|
82
|
-
cms.add('/login'
|
|
83
|
-
cms.
|
|
84
|
-
cms.
|
|
85
|
-
cms.
|
|
90
|
+
cms = ProbabilisticMachines::CountMinSketch.new(4096, 4)
|
|
91
|
+
cms.add('/login') # :added
|
|
92
|
+
cms.add_count('/login', 3) # :added, or :zero_count / :overflow
|
|
93
|
+
cms.add_hash_count(hash, 3) # for streams that ship hashes
|
|
94
|
+
cms.estimate('/login') # never below the truth
|
|
95
|
+
cms.estimate_hash(hash)
|
|
96
|
+
cms.total # every count added
|
|
97
|
+
cms.merge(other) # :merged, exact sum; :overflow, :wrong_shape
|
|
86
98
|
```
|
|
87
99
|
|
|
100
|
+
A counter is 32 bits. An addition or merge that would carry one past
|
|
101
|
+
`2**32 - 1` answers `:overflow` and changes nothing, so a counter never sticks
|
|
102
|
+
below the truth and the lower bound always holds.
|
|
103
|
+
|
|
88
104
|
A count is over the truth by at most `e * total / width`, with probability
|
|
89
105
|
`1 - e ** -depth` of staying inside it. Width buys accuracy, depth buys
|
|
90
106
|
confidence.
|
|
@@ -94,47 +110,63 @@ confidence.
|
|
|
94
110
|
## 📏 Quantiles
|
|
95
111
|
|
|
96
112
|
```ruby
|
|
97
|
-
td = ProbabilisticMachines::TDigest.new(
|
|
98
|
-
td.add(12.5)
|
|
99
|
-
td.
|
|
113
|
+
td = ProbabilisticMachines::TDigest.new(100)
|
|
114
|
+
td.add(12.5) # :added, or :not_finite / :overflow
|
|
115
|
+
td.add_weighted(12.5, 1_000.0) # one point standing for a thousand observations
|
|
100
116
|
td.quantile(0.99)
|
|
101
117
|
td.cdf(12.5)
|
|
102
|
-
td.centroids
|
|
103
|
-
td.minimum
|
|
104
|
-
td.
|
|
118
|
+
td.centroids # Array of frozen Centroid(mean, weight)
|
|
119
|
+
td.minimum # exact, as is maximum
|
|
120
|
+
td.compress
|
|
121
|
+
td.merge(other) # :merged; :wrong_shape for another compression
|
|
105
122
|
```
|
|
106
123
|
|
|
124
|
+
A NaN or infinite value or weight is `:not_finite`, a zero or negative weight
|
|
125
|
+
`:invalid_weight`, a total weight that would become infinite `:overflow`; each
|
|
126
|
+
changes nothing. Digests of different compressions do not merge: compression
|
|
127
|
+
only joins clusters, so the coarser one's clusters would break the finer one's
|
|
128
|
+
weight bound.
|
|
129
|
+
|
|
107
130
|
A quantile is off by about `1 / compression` of the distribution in the middle
|
|
108
131
|
and is exact at the extremes, in `compression * ln(n)` clusters rather than `n`
|
|
109
|
-
samples. An empty digest answers `Float::NAN`.
|
|
132
|
+
samples. An empty digest answers `Float::NAN`. `quantile(q)` raises
|
|
133
|
+
`RangeError` for a `q` outside `0..=1`, NaN included; the Rust core answers
|
|
134
|
+
`f64::NAN` there instead.
|
|
110
135
|
|
|
111
136
|
---
|
|
112
137
|
|
|
113
138
|
## 🛰️ Have I Seen This Contact Before
|
|
114
139
|
|
|
115
140
|
```ruby
|
|
116
|
-
cf = ProbabilisticMachines::CuckooFilter.new(
|
|
117
|
-
cf.insert('device-9') #
|
|
118
|
-
cf.
|
|
119
|
-
cf.remove('device-9') # unlike a Bloom filter, it can forget
|
|
141
|
+
cf = ProbabilisticMachines::CuckooFilter.new(4096)
|
|
142
|
+
cf.insert('device-9') # :inserted, or :full when wedged
|
|
143
|
+
cf.contains?('device-9')
|
|
144
|
+
cf.remove('device-9') # :cleared, or :not_held; unlike a Bloom filter, it can forget
|
|
145
|
+
cf.insert_hash(hash) # and contains_hash?, remove_hash
|
|
120
146
|
```
|
|
121
147
|
|
|
122
148
|
`buckets * 4` items in `buckets * 4` bytes. Never a false negative; about 3%
|
|
123
|
-
false positives.
|
|
124
|
-
|
|
149
|
+
false positives. The filter stores fingerprints and nothing else, so `remove`
|
|
150
|
+
answers about the fingerprint: `:not_held` proves it is in neither of the
|
|
151
|
+
item's buckets (nothing changed), while `:cleared` is no proof the item was
|
|
152
|
+
inserted, because a colliding item's fingerprint looks the same. Only remove
|
|
153
|
+
items you inserted, or you take the colliding item's fingerprint with it.
|
|
154
|
+
Inserting an item twice stores two copies. A cuckoo filter cannot merge, so
|
|
155
|
+
there is no `merge` here.
|
|
125
156
|
|
|
126
157
|
---
|
|
127
158
|
|
|
128
159
|
## 👑 Who Dominates the Stream
|
|
129
160
|
|
|
130
161
|
```ruby
|
|
131
|
-
hh = ProbabilisticMachines::HeavyHitters.new(
|
|
132
|
-
hh.add('10.0.0.1')
|
|
133
|
-
hh.
|
|
134
|
-
hh.top
|
|
135
|
-
hh.ranked
|
|
136
|
-
hh.
|
|
137
|
-
hh.
|
|
162
|
+
hh = ProbabilisticMachines::HeavyHitters.new(64)
|
|
163
|
+
hh.add('10.0.0.1') # :added
|
|
164
|
+
hh.add_count('10.0.0.1', 5) # :added, or :zero_count / :overflow
|
|
165
|
+
hh.top # Hitter(key, count, overestimate), nil when empty
|
|
166
|
+
hh.ranked # every tracked key, most frequent first
|
|
167
|
+
hh.hitters # every tracked key, in table order
|
|
168
|
+
hh.observed # observations fed in
|
|
169
|
+
hh.merge(other) # :merged; :overflow, :wrong_shape
|
|
138
170
|
```
|
|
139
171
|
|
|
140
172
|
After `n` observations every key seen more than `n / k` times is tracked, in `k`
|
|
@@ -142,47 +174,123 @@ counters and no heap per key. A counter can inherit the count of the key it
|
|
|
142
174
|
evicted, or what a merged-in table's own evictions may have hidden, so the true
|
|
143
175
|
count lies between `count - overestimate` and `count`.
|
|
144
176
|
|
|
145
|
-
Keys are UTF-8 strings here
|
|
146
|
-
|
|
147
|
-
hash came from, to hand back
|
|
148
|
-
|
|
177
|
+
Keys are UTF-8 strings here. Space-Saving takes any `Clone + Eq` key in Rust;
|
|
178
|
+
the extension counts 64-bit key hashes, so the table stays one fixed array, and
|
|
179
|
+
keeps the string each tracked hash came from, to hand back in each `Hitter`. A
|
|
180
|
+
key that is not exactly a String (a subclass instance or a `to_str` object
|
|
181
|
+
included) is a `TypeError`, one that is not UTF-8 text a `RangeError` (a valid
|
|
182
|
+
UTF-8 String, or ASCII bytes in any ASCII-compatible encoding; a binary String
|
|
183
|
+
holding UTF-8 bytes is not transcoded), and the key a `Hitter` holds is a
|
|
184
|
+
frozen copy. The other four classes take exactly a String of any bytes in any
|
|
185
|
+
encoding, read natively (no `#bytes` or `#to_str` runs). A zero count is `:zero_count`: it observes nothing, yet on a full
|
|
186
|
+
table it would evict the weakest key.
|
|
149
187
|
|
|
150
188
|
---
|
|
151
189
|
|
|
152
190
|
## 📦 Transmitting a Reading
|
|
153
191
|
|
|
154
|
-
`
|
|
155
|
-
|
|
156
|
-
|
|
192
|
+
`encoded` returns the crate's own byte frame, `decode` reads one back, and
|
|
193
|
+
`decode_into` reads one over a sketch you already hold. The frame names its own
|
|
194
|
+
shape, so `decode` needs no size. It reads the frame's fields against each
|
|
157
195
|
other and refuses a state no stream could have left behind, so an edited blob
|
|
158
|
-
|
|
196
|
+
answers `:malformed` instead of handing back a sketch that counts more than it
|
|
197
|
+
ever saw. Like every refusal, these are symbols, never exceptions.
|
|
159
198
|
|
|
160
199
|
```ruby
|
|
161
|
-
blob = hll.
|
|
162
|
-
ProbabilisticMachines::HyperLogLog.
|
|
163
|
-
ProbabilisticMachines::TDigest.
|
|
200
|
+
blob = hll.encoded # binary String
|
|
201
|
+
ProbabilisticMachines::HyperLogLog.decode(blob) # a HyperLogLog
|
|
202
|
+
ProbabilisticMachines::TDigest.decode(blob) # => :wrong_structure
|
|
203
|
+
hll.decode_into(other_size.encoded) # => :wrong_shape, hll untouched
|
|
164
204
|
```
|
|
165
205
|
|
|
166
206
|
Anything that reads the crate reads these frames, so an embedded device can ship
|
|
167
207
|
a sketch a Rails app folds in. `HeavyHitters` is the one blob that is not purely
|
|
168
208
|
the crate's: the crate's frame counts 64-bit key hashes, so the extension appends
|
|
169
209
|
its key table after it, one record of a 64-bit byte length and that many UTF-8
|
|
170
|
-
bytes per tracked key. `
|
|
171
|
-
table is
|
|
210
|
+
bytes per tracked key. `decode` rehashes each name, and answers `:malformed` for
|
|
211
|
+
a key table that is cut short or does not name every key the frame tracks.
|
|
172
212
|
|
|
173
213
|
```ruby
|
|
174
|
-
restored = ProbabilisticMachines::HeavyHitters.
|
|
175
|
-
restored.top
|
|
214
|
+
restored = ProbabilisticMachines::HeavyHitters.decode(hh.encoded)
|
|
215
|
+
restored.top.key # => '10.0.0.1', the string, not its hash
|
|
176
216
|
```
|
|
177
217
|
|
|
178
218
|
---
|
|
179
219
|
|
|
220
|
+
## 📜 The Contract
|
|
221
|
+
|
|
222
|
+
- **A refusal is a symbol and changes nothing.** It is the snake_case of the
|
|
223
|
+
core's `Refusal` variant; there is no catch-all symbol and no refusal raises.
|
|
224
|
+
- **Exceptions are for malformed calls.** Wrong type: `TypeError`. An Integer
|
|
225
|
+
outside its range, a size off the menu, a compression below 10, a `Hitter`
|
|
226
|
+
or `Centroid` the core would not build: `RangeError`. Mutating a frozen
|
|
227
|
+
sketch: `FrozenError`. `ArgumentError` only for a wrong number of arguments.
|
|
228
|
+
- **Values are valid or do not exist.** `Hitter.new(key, count, overestimate)`
|
|
229
|
+
needs `overestimate < count`; `Centroid.new(mean, weight)` a finite mean and
|
|
230
|
+
a finite, positive weight. A value you build is held to the same rule, by the
|
|
231
|
+
core, as one the gem returns. Both classes are sealed: subclassing one raises
|
|
232
|
+
`TypeError` and `allocate` is undefined. Sealing stops accidents, not
|
|
233
|
+
reflection (a named subclass's constant is bound before the raise,
|
|
234
|
+
`Marshal.load` skips `initialize`); the sketches only build these values and
|
|
235
|
+
take none back, so a forged one has no way into a sketch. The classes are
|
|
236
|
+
resolved once, when the extension loads.
|
|
237
|
+
- **One honoured repeat.** `CuckooFilter#remove` of an item whose fingerprint
|
|
238
|
+
is in neither bucket answers `:not_held` and changes nothing. Nothing else is
|
|
239
|
+
a repeat: a second `insert` stores a second copy, a second `add` counts again.
|
|
240
|
+
- **Determinism assumes binary64.** Every Float answer (estimates,
|
|
241
|
+
quantiles, cdf) is the same bits for the same state on any machine whose
|
|
242
|
+
`f64` arithmetic is IEEE 754 binary64 with one rounding per operation,
|
|
243
|
+
which is every 64-bit target Ruby runs on. x87 extended-precision
|
|
244
|
+
evaluation is outside that promise. Frames carry raw bits, so a blob
|
|
245
|
+
decodes to the same state everywhere.
|
|
246
|
+
- **No clock, no ids, no restarts.** No sketch reads time or issues a handle;
|
|
247
|
+
what survives a restart is what you `encoded`.
|
|
248
|
+
|
|
249
|
+
### Every answer
|
|
250
|
+
|
|
251
|
+
| Call | Honoured | Refusals |
|
|
252
|
+
| --- | --- | --- |
|
|
253
|
+
| `HyperLogLog#add`, `#add_hash` | `nil` | none |
|
|
254
|
+
| `HyperLogLog#merge(other)` | `:merged` | `:wrong_shape` |
|
|
255
|
+
| `CountMinSketch#add(item)` | `:added` | `:overflow` |
|
|
256
|
+
| `CountMinSketch#add_count(item, n)`, `#add_hash_count(hash, n)` | `:added` | `:zero_count`, `:overflow` |
|
|
257
|
+
| `CountMinSketch#merge(other)` | `:merged` | `:overflow`, `:wrong_shape` |
|
|
258
|
+
| `CuckooFilter#insert(item)`, `#insert_hash(hash)` | `:inserted` | `:full` |
|
|
259
|
+
| `CuckooFilter#remove(item)`, `#remove_hash(hash)` | `:cleared`, `:not_held` (*repeat*) | none |
|
|
260
|
+
| `HeavyHitters#add(key)` | `:added` | `:overflow` |
|
|
261
|
+
| `HeavyHitters#add_count(key, n)` | `:added` | `:zero_count`, `:overflow` |
|
|
262
|
+
| `HeavyHitters#merge(other)` | `:merged` | `:overflow`, `:wrong_shape` |
|
|
263
|
+
| `TDigest#add(value)` | `:added` | `:not_finite`, `:overflow` |
|
|
264
|
+
| `TDigest#add_weighted(value, weight)` | `:added` | `:not_finite`, `:invalid_weight`, `:overflow` |
|
|
265
|
+
| `TDigest#merge(other)` | `:merged` | `:wrong_shape`, `:overflow` |
|
|
266
|
+
| `TDigest#compress` | `nil` | none |
|
|
267
|
+
| `.decode(bytes)` | the sketch | `:truncated`, `:wrong_structure`, `:wrong_shape` (not `TDigest`), `:malformed` (not `CuckooFilter`) |
|
|
268
|
+
| `#decode_into(bytes)` | `:decoded` | as `.decode` |
|
|
269
|
+
|
|
270
|
+
Queries (`estimate`, `contains?`, `count`, `top`, `quantile`, ...) answer values
|
|
271
|
+
and never refuse. No answer depends on or records a clock.
|
|
272
|
+
|
|
273
|
+
| Refusal | Meaning |
|
|
274
|
+
| --- | --- |
|
|
275
|
+
| `:truncated` | The blob is shorter than the frame it starts. |
|
|
276
|
+
| `:wrong_structure` | The blob is another sketch's frame. |
|
|
277
|
+
| `:wrong_shape` | The blob, or the sketch given to `merge`, has another size or compression. In Rust a merge across sizes does not compile. |
|
|
278
|
+
| `:malformed` | The blob holds contents no sequence of calls produces. |
|
|
279
|
+
| `:full` | The cuckoo filter found no slot after 64 evictions; expect it past about 95% of `capacity`. |
|
|
280
|
+
| `:overflow` | A counter or total would pass `2**32 - 1` (count-min), `2**64 - 1` (heavy hitters) or stay finite (t-digest weight). |
|
|
281
|
+
| `:zero_count` | A count of zero. |
|
|
282
|
+
| `:not_finite` | A t-digest value or weight is NaN or infinite. |
|
|
283
|
+
| `:invalid_weight` | A t-digest weight is zero or negative. |
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
180
287
|
## ⚙️ The Rust Core
|
|
181
288
|
|
|
182
289
|
The sketches themselves are the `probabilistic-machines` crate under
|
|
183
|
-
`ext/probabilistic_machines_native/core`: `no_std`,
|
|
184
|
-
|
|
185
|
-
|
|
290
|
+
`ext/probabilistic_machines_native/core`: `no_std`, `unsafe` forbidden, one
|
|
291
|
+
dependency (`sliderule-machines`, for integer logarithms), allocating only for
|
|
292
|
+
`TDigest` and for the `encoded` frames this extension returns, and verified on
|
|
293
|
+
bare metal. The binding crate beside it under
|
|
186
294
|
`ffi/` is the Ruby surface and is not published.
|
|
187
295
|
|
|
188
296
|
---
|
|
@@ -2,6 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'probabilistic_machines/version'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
#
|
|
5
|
+
module ProbabilisticMachines
|
|
6
|
+
# The core's `Hitter`: a tracked key, its counter, and how much of that
|
|
7
|
+
# counter it may have inherited. The true count lies in
|
|
8
|
+
# `count - overestimate` through `count`, so like `Hitter::new` it needs an
|
|
9
|
+
# overestimate below the count: anything else raises RangeError (TypeError
|
|
10
|
+
# for a key that is not a String or a count that is not an Integer).
|
|
11
|
+
Hitter = Data.define(:key, :count, :overestimate) do
|
|
12
|
+
def initialize(key:, count:, overestimate:)
|
|
13
|
+
key, count, overestimate = ProbabilisticMachines.__send__(:__hitter, key, count, overestimate)
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The core's `Centroid`: one cluster of a digest, the mean of the values that
|
|
19
|
+
# collapsed into it. Like `Centroid::new` it holds a finite mean and a
|
|
20
|
+
# finite, positive weight: anything else raises RangeError (TypeError for a
|
|
21
|
+
# non-number). Stored as Floats.
|
|
22
|
+
Centroid = Data.define(:mean, :weight) do
|
|
23
|
+
def initialize(mean:, weight:)
|
|
24
|
+
mean, weight = ProbabilisticMachines.__send__(:__centroid, mean, weight)
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Native is required: there is no pure-Ruby fallback. It defines every sketch
|
|
31
|
+
# class and validates the Data values above.
|
|
7
32
|
require 'probabilistic_machines_native/probabilistic_machines_native'
|
|
33
|
+
|
|
34
|
+
module ProbabilisticMachines
|
|
35
|
+
private_class_method :__hitter, :__centroid
|
|
36
|
+
|
|
37
|
+
# The values are final. `initialize` is where each validates, and a subclass
|
|
38
|
+
# could override it, or `allocate` build an instance that never ran it:
|
|
39
|
+
# subclassing raises TypeError (as subclassing Class does) and `allocate`
|
|
40
|
+
# is gone. `new`, keywords, `with` and `dup` all still run the check.
|
|
41
|
+
[Hitter, Centroid].each do |value|
|
|
42
|
+
value.singleton_class.undef_method(:allocate)
|
|
43
|
+
value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
Binary file
|
|
@@ -1,35 +1,62 @@
|
|
|
1
1
|
# Sketches that answer a counting question in memory fixed at construction
|
|
2
|
-
# time. Every class
|
|
2
|
+
# time. Every sketch class is defined by the native extension.
|
|
3
|
+
#
|
|
4
|
+
# Sizes come from a fixed menu (a class constant); anything else raises
|
|
5
|
+
# RangeError, a non-Integer TypeError. A call the core refuses answers the
|
|
6
|
+
# snake_case symbol of its `Refusal` variant, changes nothing and never raises.
|
|
7
|
+
# No answer depends on, or records, a clock.
|
|
3
8
|
module ProbabilisticMachines
|
|
4
9
|
VERSION: String
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
# Every refusal the crate answers.
|
|
12
|
+
type refusal = :truncated | :wrong_structure | :wrong_shape | :malformed | :full
|
|
13
|
+
| :overflow | :zero_count | :not_finite | :invalid_weight
|
|
8
14
|
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
# What `decode` refuses: a frame cut short, another structure's frame, a frame
|
|
16
|
+
# of another shape, or contents no sketch produces.
|
|
17
|
+
type decode_refusal = :truncated | :wrong_structure | :wrong_shape | :malformed
|
|
18
|
+
|
|
19
|
+
# Private native checks behind `Hitter.new` and `Centroid.new`: the members
|
|
20
|
+
# of a valid value (a frozen UTF-8 copy of the key; the centroid as Floats),
|
|
21
|
+
# TypeError/RangeError where the core has no such value.
|
|
22
|
+
private def self.__hitter: (String key, Integer count, Integer overestimate) -> [String, Integer, Integer]
|
|
23
|
+
private def self.__centroid: (Float | Integer mean, Float | Integer weight) -> [Float, Float]
|
|
12
24
|
|
|
13
|
-
#
|
|
14
|
-
# `count
|
|
25
|
+
# The core's `Hitter`. The true count lies in `count - overestimate` through
|
|
26
|
+
# `count`. Raises RangeError unless `overestimate < count` (both in
|
|
27
|
+
# `0...2**64`), TypeError for a non-String key or a non-Integer count.
|
|
15
28
|
class Hitter < ::Data
|
|
16
29
|
attr_reader key: String
|
|
17
30
|
attr_reader count: Integer
|
|
18
31
|
attr_reader overestimate: Integer
|
|
32
|
+
|
|
33
|
+
def self.new: (String key, Integer count, Integer overestimate) -> instance
|
|
34
|
+
| (key: String, count: Integer, overestimate: Integer) -> instance
|
|
19
35
|
end
|
|
20
36
|
|
|
21
|
-
#
|
|
37
|
+
# The core's `Centroid`: one cluster of a digest. Raises RangeError unless
|
|
38
|
+
# the mean is finite and the weight finite and positive, TypeError for a
|
|
39
|
+
# non-number. Stored as Floats.
|
|
22
40
|
class Centroid < ::Data
|
|
23
41
|
attr_reader mean: Float
|
|
24
42
|
attr_reader weight: Float
|
|
43
|
+
|
|
44
|
+
def self.new: (Float | Integer mean, Float | Integer weight) -> instance
|
|
45
|
+
| (mean: Float | Integer, weight: Float | Integer) -> instance
|
|
25
46
|
end
|
|
26
47
|
|
|
27
48
|
# Distinct counts in a fixed register array.
|
|
28
49
|
class HyperLogLog
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
REGISTERS: Array[Integer]
|
|
51
|
+
|
|
52
|
+
# RangeError outside REGISTERS.
|
|
53
|
+
def self.new: (Integer registers) -> instance
|
|
31
54
|
|
|
32
|
-
|
|
55
|
+
# `:wrong_shape` for a register count this build does not carry.
|
|
56
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
57
|
+
|
|
58
|
+
# Reads a frame of this sketch's register count over it.
|
|
59
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
33
60
|
|
|
34
61
|
def registers: () -> Integer
|
|
35
62
|
def add: (String item) -> nil
|
|
@@ -44,53 +71,74 @@ module ProbabilisticMachines
|
|
|
44
71
|
|
|
45
72
|
def empty?: () -> bool
|
|
46
73
|
|
|
47
|
-
#
|
|
48
|
-
def merge: (HyperLogLog other) ->
|
|
74
|
+
# Exact union. `:wrong_shape` for another register count.
|
|
75
|
+
def merge: (HyperLogLog other) -> (:merged | :wrong_shape)
|
|
49
76
|
|
|
50
|
-
def
|
|
77
|
+
def encoded: () -> String
|
|
51
78
|
def inspect: () -> String
|
|
52
79
|
end
|
|
53
80
|
|
|
54
81
|
# How often each item was seen, in `width * depth` counters.
|
|
55
82
|
class CountMinSketch
|
|
56
|
-
|
|
57
|
-
|
|
83
|
+
WIDTHS: Array[Integer]
|
|
84
|
+
DEPTHS: Array[Integer]
|
|
85
|
+
|
|
86
|
+
# RangeError outside WIDTHS by DEPTHS; every pair is carried.
|
|
87
|
+
def self.new: (Integer width, Integer depth) -> instance
|
|
58
88
|
|
|
59
|
-
def self.
|
|
89
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
90
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
60
91
|
|
|
61
92
|
def width: () -> Integer
|
|
62
93
|
def depth: () -> Integer
|
|
63
|
-
|
|
94
|
+
|
|
95
|
+
# `:overflow` when one of the item's counters would pass `2**32 - 1`.
|
|
96
|
+
def add: (String item) -> (:added | :overflow)
|
|
97
|
+
|
|
98
|
+
# `count` is in `0...2**32` (RangeError otherwise); zero is `:zero_count`.
|
|
99
|
+
def add_count: (String item, Integer count) -> (:added | :zero_count | :overflow)
|
|
100
|
+
|
|
101
|
+
def add_hash_count: (Integer hash, Integer count) -> (:added | :zero_count | :overflow)
|
|
64
102
|
|
|
65
103
|
# Never below the true count, and over it by at most
|
|
66
104
|
# `e * total / width` with probability `1 - e ** -depth`.
|
|
67
|
-
def
|
|
105
|
+
def estimate: (String item) -> Integer
|
|
106
|
+
|
|
107
|
+
def estimate_hash: (Integer hash) -> Integer
|
|
68
108
|
|
|
69
109
|
# Sum of every count added.
|
|
70
110
|
def total: () -> Integer
|
|
71
|
-
def saturated?: () -> bool
|
|
72
111
|
|
|
73
|
-
#
|
|
74
|
-
def merge: (CountMinSketch other) ->
|
|
112
|
+
# Exact sum. `:wrong_shape` for another width or depth.
|
|
113
|
+
def merge: (CountMinSketch other) -> (:merged | :overflow | :wrong_shape)
|
|
75
114
|
|
|
76
|
-
def
|
|
115
|
+
def encoded: () -> String
|
|
77
116
|
def inspect: () -> String
|
|
78
117
|
end
|
|
79
118
|
|
|
80
119
|
# Quantiles of a stream, in clusters instead of samples.
|
|
81
120
|
class TDigest
|
|
82
|
-
|
|
121
|
+
MIN_COMPRESSION: Integer
|
|
83
122
|
|
|
84
|
-
|
|
123
|
+
# RangeError below MIN_COMPRESSION or past 65535.
|
|
124
|
+
def self.new: (Integer compression) -> instance
|
|
125
|
+
|
|
126
|
+
def self.decode: (String bytes) -> (instance | :truncated | :wrong_structure | :malformed)
|
|
85
127
|
|
|
86
128
|
def compression: () -> Integer
|
|
87
|
-
def add: (Float value, ?weight: Float) -> nil
|
|
88
129
|
|
|
89
|
-
|
|
90
|
-
|
|
130
|
+
def add: (Float | Integer value) -> (:added | :not_finite | :overflow)
|
|
131
|
+
|
|
132
|
+
# One value standing for `weight` observations.
|
|
133
|
+
def add_weighted: (Float | Integer value, Float | Integer weight)
|
|
134
|
+
-> (:added | :not_finite | :invalid_weight | :overflow)
|
|
135
|
+
|
|
136
|
+
# Value at quantile `q`, `Float::NAN` when empty. RangeError for `q`
|
|
137
|
+
# outside 0..=1, NaN included.
|
|
138
|
+
def quantile: (Float | Integer q) -> Float
|
|
91
139
|
|
|
92
140
|
# Fraction of the stream at or below `value`, `Float::NAN` when empty.
|
|
93
|
-
def cdf: (Float value) -> Float
|
|
141
|
+
def cdf: (Float | Integer value) -> Float
|
|
94
142
|
|
|
95
143
|
# Total weight added.
|
|
96
144
|
def count: () -> Float
|
|
@@ -106,53 +154,75 @@ module ProbabilisticMachines
|
|
|
106
154
|
|
|
107
155
|
def empty?: () -> bool
|
|
108
156
|
def centroids: () -> Array[Centroid]
|
|
109
|
-
|
|
110
|
-
|
|
157
|
+
|
|
158
|
+
# Collapses adjacent clusters up to the weight their quantile allows.
|
|
159
|
+
def compress: () -> nil
|
|
160
|
+
|
|
161
|
+
# `:wrong_shape` for a digest of another compression.
|
|
162
|
+
def merge: (TDigest other) -> (:merged | :wrong_shape | :overflow)
|
|
163
|
+
|
|
164
|
+
def encoded: () -> String
|
|
111
165
|
def inspect: () -> String
|
|
112
166
|
end
|
|
113
167
|
|
|
114
168
|
# Membership that can forget, in `buckets * 4` fingerprints.
|
|
115
169
|
class CuckooFilter
|
|
116
|
-
|
|
117
|
-
def self.new: (buckets: Integer) -> instance
|
|
170
|
+
BUCKETS: Array[Integer]
|
|
118
171
|
|
|
119
|
-
|
|
172
|
+
# RangeError outside BUCKETS.
|
|
173
|
+
def self.new: (Integer buckets) -> instance
|
|
174
|
+
|
|
175
|
+
def self.decode: (String bytes) -> (instance | :truncated | :wrong_structure | :wrong_shape)
|
|
176
|
+
def decode_into: (String bytes) -> (:decoded | :truncated | :wrong_structure | :wrong_shape)
|
|
120
177
|
|
|
121
178
|
def buckets: () -> Integer
|
|
122
179
|
|
|
123
180
|
# Fingerprint slots in the table, `buckets * 4`.
|
|
124
181
|
def capacity: () -> Integer
|
|
125
182
|
|
|
126
|
-
#
|
|
127
|
-
def insert: (String item) ->
|
|
183
|
+
# Stores one more copy of the item's fingerprint.
|
|
184
|
+
def insert: (String item) -> (:inserted | :full)
|
|
185
|
+
|
|
186
|
+
def insert_hash: (Integer hash) -> (:inserted | :full)
|
|
128
187
|
|
|
129
188
|
# Never false for an item that was inserted; true for one that was not
|
|
130
189
|
# with probability about 3%.
|
|
131
|
-
def
|
|
190
|
+
def contains?: (String item) -> bool
|
|
191
|
+
|
|
192
|
+
def contains_hash?: (Integer hash) -> bool
|
|
132
193
|
|
|
133
|
-
|
|
194
|
+
# `:cleared` clears one copy of the item's fingerprint, which may have
|
|
195
|
+
# been a colliding item's: only remove what you inserted. `:not_held`
|
|
196
|
+
# proves the fingerprint is in neither bucket; nothing changed.
|
|
197
|
+
def remove: (String item) -> (:cleared | :not_held)
|
|
134
198
|
|
|
135
|
-
|
|
136
|
-
# with one takes the other item's fingerprint with it.
|
|
137
|
-
def remove: (String item) -> bool
|
|
199
|
+
def remove_hash: (Integer hash) -> (:cleared | :not_held)
|
|
138
200
|
|
|
139
201
|
def size: () -> Integer
|
|
140
202
|
def empty?: () -> bool
|
|
141
|
-
def
|
|
203
|
+
def encoded: () -> String
|
|
142
204
|
def inspect: () -> String
|
|
143
205
|
end
|
|
144
206
|
|
|
145
|
-
# Which keys dominate a stream, in
|
|
207
|
+
# Which keys dominate a stream, in a fixed number of counters.
|
|
146
208
|
class HeavyHitters
|
|
147
|
-
|
|
148
|
-
def self.new: (k: Integer) -> instance
|
|
209
|
+
COUNTERS: Array[Integer]
|
|
149
210
|
|
|
150
|
-
|
|
211
|
+
# RangeError outside COUNTERS.
|
|
212
|
+
def self.new: (Integer counters) -> instance
|
|
151
213
|
|
|
152
|
-
|
|
214
|
+
# `:malformed` also covers a key table that is cut short or does not name
|
|
215
|
+
# every tracked key.
|
|
216
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
217
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
153
218
|
|
|
154
|
-
|
|
155
|
-
|
|
219
|
+
def counters: () -> Integer
|
|
220
|
+
|
|
221
|
+
# Keys are UTF-8 strings, so the Hitter values can hand them back.
|
|
222
|
+
def add: (String key) -> (:added | :overflow)
|
|
223
|
+
|
|
224
|
+
# `count` is in `0...2**64` (RangeError otherwise); zero is `:zero_count`.
|
|
225
|
+
def add_count: (String key, Integer count) -> (:added | :zero_count | :overflow)
|
|
156
226
|
|
|
157
227
|
# Upper bound on how often `key` occurred, 0 when it is not tracked.
|
|
158
228
|
def count: (String key) -> Integer
|
|
@@ -160,22 +230,25 @@ module ProbabilisticMachines
|
|
|
160
230
|
# Observations fed in.
|
|
161
231
|
def observed: () -> Integer
|
|
162
232
|
|
|
163
|
-
# Keys currently tracked, at most `
|
|
233
|
+
# Keys currently tracked, at most `counters`.
|
|
164
234
|
def size: () -> Integer
|
|
165
235
|
|
|
166
236
|
def empty?: () -> bool
|
|
167
237
|
|
|
168
|
-
# The
|
|
169
|
-
def top: (
|
|
238
|
+
# The most frequent tracked key, nil when nothing is tracked.
|
|
239
|
+
def top: () -> Hitter?
|
|
170
240
|
|
|
171
241
|
# Every tracked key, most frequent first.
|
|
172
242
|
def ranked: () -> Array[Hitter]
|
|
173
243
|
|
|
174
|
-
#
|
|
175
|
-
def
|
|
244
|
+
# Every tracked key, in the table's own order.
|
|
245
|
+
def hitters: () -> Array[Hitter]
|
|
246
|
+
|
|
247
|
+
# `:wrong_shape` for another counter count.
|
|
248
|
+
def merge: (HeavyHitters other) -> (:merged | :overflow | :wrong_shape)
|
|
176
249
|
|
|
177
250
|
# The crate's frame followed by the key table that carries the strings.
|
|
178
|
-
def
|
|
251
|
+
def encoded: () -> String
|
|
179
252
|
|
|
180
253
|
def inspect: () -> String
|
|
181
254
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: probabilistic_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: aarch64-linux
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: HyperLogLog, Count-Min Sketch, t-digest, cuckoo filter and Space-Saving
|
|
14
14
|
top-k, bound to Ruby from the no_std probabilistic-machines crate. Each sketch answers
|