Skip to content

ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:68

A Theta sketch: counts distinct keys exactly up to k, and past that estimates from the k smallest key hashes, within about 1 / sqrt(k).

Hashes live in a table of up to 2k slots. Once it is full to 3/4, a quickselect keeps the k smallest and lowers theta to the next, so a key hashing above theta is rejected without touching the table (QuickSelect, as in Apache DataSketches).

When those rebuilds run depends on the order keys arrive in, so the table itself does not. Every rebuild’s theta is the (k+1)-th smallest hash seen so far, never below the true one, so the table always holds the true k smallest. Every read goes through that canonical sample: the k smallest hashes, theta the next. Equal key sets then answer alike whatever the order.

const sketch = ThetaSketch.create(0.02);
sketch.add("alice");

new ThetaSketch(params?): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:89

Creates a sketch at an explicit k.

ThetaParams = {}

Sample size and optional hash seed.

ThetaSketch

get isEmpty(): boolean

Defined in: packages/distillate/src/theta/theta.ts:261

Whether no key has been added.

boolean


get isEstimating(): boolean

Defined in: packages/distillate/src/theta/theta.ts:266

Whether count() estimates rather than counts.

boolean


get k(): number

Defined in: packages/distillate/src/theta/theta.ts:240

Hashes kept once the sketch is estimating.

number


get retained(): number

Defined in: packages/distillate/src/theta/theta.ts:256

Hashes the estimate is computed from, at most k.

number


get seed(): number

Defined in: packages/distillate/src/theta/theta.ts:245

Hash seed the sketch was built with.

number


get theta(): number

Defined in: packages/distillate/src/theta/theta.ts:250

Fraction of the hash range sampled, in (0, 1]; 1 while exact.

number

add(key): void

Defined in: packages/distillate/src/theta/theta.ts:275

Records a key. Adding a key already seen leaves the sketch unchanged.

BytesLike

The key to record.

void


count(): number

Defined in: packages/distillate/src/theta/theta.ts:306

Counts the distinct keys added: exact while not isEstimating.

number

The distinct count, or its estimate.


difference(other): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:421

Subtracts a sketch from this one, giving a sketch of the keys this one has seen and the other has not. Both are left untouched.

ThetaSketch

The sketch to subtract; must share this one’s seed.

ThetaSketch

A new sketch of the difference, at the smaller k.

ParamError if the seeds differ.


equals(other): boolean

Defined in: packages/distillate/src/theta/theta.ts:453

Whether two sketches hold the same canonical sample at the same k and seed. The order keys arrived in does not enter into it.

ThetaSketch

The sketch to compare against.

boolean

true if the two would answer every query identically.


intersect(other): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:386

Intersects two sketches, giving a sketch of the keys both have seen. Both are left untouched. Two disjoint sets read 0, with an upper bound above it: a sample cannot rule out a small overlap.

ThetaSketch

The sketch to intersect with; must share this one’s seed.

ThetaSketch

A new sketch of the intersection, at the smaller k.

ParamError if the seeds differ.


lowerBound(sd): number

Defined in: packages/distillate/src/theta/theta.ts:319

A lower bound on the distinct count, at sd standard deviations. Over many sketches the two bounds together hold about as often as a normal interval of that width: 68%, 95% and 99.7%.

number

Standard deviations: 1, 2 or 3.

number

The bound; count() while exact.

ParamError for any other sd.


toBytes(): Uint8Array

Defined in: packages/distillate/src/theta/theta.ts:199

Serializes the sketch’s canonical sample to a portable little-endian byte layout.

Uint8Array

The serialized sketch, readable by ThetaSketch.fromBytes.


toJSON(): FilterJSON

Defined in: packages/distillate/src/theta/theta.ts:225

Serializes the sketch to a JSON-safe envelope wrapping ThetaSketch.toBytes.

FilterJSON

The envelope, readable by ThetaSketch.fromJSON.


union(other): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:349

Merges two sketches, giving a sketch of the keys either has seen. Both are left untouched.

The result keeps the smaller k, as HLL keeps the coarser precision: with the larger, a union cut to a small k before a third sketch joins would differ from one cut after, and grouping would change the answer.

ThetaSketch

The sketch to merge with; must share this one’s seed.

ThetaSketch

A new sketch of the union, which takes keys and further operations.

ParamError if the seeds differ, since the same key would then hash differently on each side.


upperBound(sd): number

Defined in: packages/distillate/src/theta/theta.ts:332

An upper bound on the distinct count, at sd standard deviations. Over many sketches the two bounds together hold about as often as a normal interval of that width: 68%, 95% and 99.7%.

number

Standard deviations: 1, 2 or 3.

number

The bound; count() while exact.

ParamError for any other sd.


static create(relativeError): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:111

Creates a sketch whose standard error meets relativeError.

number

Target relative error, e.g. 0.01 for 1%.

ThetaSketch

A new, empty sketch.


static from(keys, relativeError): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:122

Creates a sketch counting keys, sized to meet relativeError.

Iterable<BytesLike>

The keys to record.

number

Target relative error, e.g. 0.01 for 1%.

ThetaSketch

A new sketch holding every key.


static fromBytes(bytes): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:137

Restores a sketch from ThetaSketch.toBytes. It takes keys and set operations like the sketch it was written from.

Uint8Array

The serialized sketch.

ThetaSketch

The reconstructed sketch.


static fromJSON(value): ThetaSketch

Defined in: packages/distillate/src/theta/theta.ts:235

Restores a sketch from its ThetaSketch.toJSON envelope.

unknown

The parsed envelope.

ThetaSketch

The reconstructed sketch.