Exact fraction of the union that occurs in only one set. Two empty sets have distance zero.
Uses O(min(a.size, b.size)) membership checks and O(1) space.
Largest absolute difference in event probability for two probability vectors on the
same ordered outcomes. Accepted vectors are normalized by their totals. Returns a
distance in [0, 1] in O(n) time and O(1) extra space.
@throws ― {DistanceInvalidInputError} With INVALID_DIMENSION or INVALID_PROBABILITIES.
Exact fraction of the union that occurs in only one set. Two empty sets have distance zero.
Uses O(min(a.size, b.size)) membership checks and O(1) space.
jaccardDistance(new
var Set:SetConstructor
new <string>(iterable?:Iterable<string> |null|undefined) =>Set<string> (+1 overload)
Set(['a', 'b']), new
var Set:SetConstructor
new <string>(iterable?:Iterable<string> |null|undefined) =>Set<string> (+1 overload)
Largest absolute difference in event probability for two probability vectors on the
same ordered outcomes. Accepted vectors are normalized by their totals. Returns a
distance in [0, 1] in O(n) time and O(1) extra space.
@throws ― {DistanceInvalidInputError} With INVALID_DIMENSION or INVALID_PROBABILITIES.
Calls a defined callback function on each element of an array. Then, flattens the result into
a new array.
This is identical to a map followed by flat with depth 1.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Measured by the suite in packages/distance/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.
Use knotkit/distance for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/distance'.
Exports:
- `manhattanDistance` → `number`: Sum of absolute coordinate differences.
- `euclideanDistance` → `number`: Straight-line distance with a scaled sum of squares.
- `squaredEuclideanDistance` → `number`: Sum of squared coordinate differences.
- `meanSquaredError` → `number`: Mean of squared coordinate differences.
- `cosineSimilarity` → `number`: Cosine of the angle between nonzero vectors.
- `cosineDistance` → `number`: One minus cosine similarity, computed from unit differences.
- `kullbackLeiblerDivergence` → `number`: Directional discrete relative entropy in nats, with infinity for missing support.
- `totalVariationDistance` → `number`: Largest absolute difference in event probability between aligned probability vectors.
- `jaccardSimilarity` → `number`: Exact fraction of the union also in the intersection.
- `jaccardDistance` → `number`: Exact fraction of the union present in only one set.
- `jaccardOverlap` → `JaccardOverlap`: Return exact intersection and union counts and the corresponding Jaccard similarity in one pass.
- `uncheckedEuclideanDistance2D` → `number`: Straight-line distance between two points with caller-validated finite coordinates.
- `uncheckedSquaredEuclideanDistance` → `number`: Sum squared differences in coordinate order over a caller-validated window, with an optional offset into the second vector.
- `uncheckedSquaredEuclideanDistance2D` → `number`: Sum squared differences between two caller-validated planar points in ordinary binary64 arithmetic.
Pick the layout by these rules:
- `manhattanDistance` (kind 'manhattan') when cost accumulates along coordinate axes
- `euclideanDistance` (kind 'euclidean') when you need straight-line distance or a Euclidean metric
- `squaredEuclideanDistance` (kind 'squared-euclidean') when you need squared errors or Euclidean rankings without a square root
- `meanSquaredError` (kind 'mean-squared-error') when loss must average squared coordinate errors
- `cosineSimilarity` (kind 'cosine-similarity') when a higher score should mean closer vector directions
- `cosineDistance` (kind 'cosine-distance') when a lower score should mean closer vector directions
- `kullbackLeiblerDivergence` (kind 'kullback-leibler-divergence') when the inputs are aligned probability vectors and exchanging them can change the result
- `totalVariationDistance` (kind 'total-variation-distance') when the inputs are aligned probability vectors and you need the largest absolute difference in event probability
- `jaccardSimilarity` (kind 'jaccard-similarity') when you have both sets and a higher score should mean greater overlap
- `jaccardDistance` (kind 'jaccard-distance') when you have both sets and need an overlap distance that obeys the triangle inequality
Use something else when:
- estimate overlap when only set signatures fit in memory → knotkit/scalar-sketch
- find least-cost paths between graph vertices → knotkit/graph
- test whether samples plausibly came from a given distribution → knotkit/hypothesis-test
Errors are DistanceInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/distance.md
Types: node_modules/knotkit/distance/ and https://knotkit.jkomyno.dev/reference/knotkit/distance/