-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vectors): add new distance metrics
- add distBrayCurtis() - add distCanberra() - add distHamming() - add distJaccard() - add distMinkowski() - add distSorensenDice()
- Loading branch information
1 parent
1de245b
commit 24aa2f4
Showing
9 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { DistanceFn } from "./api"; | ||
|
||
/** | ||
* Bray-Curtis **dissimilarity** metric. Result always in [0..1] interval. | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity | ||
* | ||
* @example | ||
* ```ts | ||
* distBrayCurtis([6, 7, 4], [10, 0, 6]) | ||
* // 0.393939... | ||
* ``` | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const distBrayCurtis: DistanceFn = (a, b) => { | ||
let c = 0; | ||
let s = 0; | ||
for (let i = a.length; i-- > 0; ) { | ||
c += Math.abs(a[i] - b[i]); | ||
s += Math.abs(a[i] + b[i]); | ||
} | ||
return s > 0 ? c / s : 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { DistanceFn } from "./api"; | ||
|
||
/** | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Canberra_distance | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const distCanberra: DistanceFn = (a, b) => { | ||
let delta = 0; | ||
for (let i = a.length; --i >= 0; ) { | ||
const aa = a[i]; | ||
const bb = b[i]; | ||
const d = Math.abs(aa) + Math.abs(bb); | ||
d > 0 && (delta += Math.abs(aa - bb) / d); | ||
} | ||
return delta; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { DistanceFn } from "./api"; | ||
|
||
/** | ||
* Normalized Hamming distance between `a` and `b`, i.e. number of differing | ||
* components divided by vector size. | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Hamming_distance | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const distHamming: DistanceFn = (a, b) => { | ||
let delta = 0; | ||
for (let i = a.length; --i >= 0; ) { | ||
a[i] !== b[i] && delta++; | ||
} | ||
return delta / a.length; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { DistanceFn } from "./api"; | ||
|
||
/** | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Jaccard_index | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const distJaccard: DistanceFn = (a, b) => { | ||
let numNZ = 0; | ||
let numEQ = 0; | ||
for (let i = a.length; --i >= 0; ) { | ||
const aa = a[i] !== 0; | ||
const bb = b[i] !== 0; | ||
numNZ += ~~(aa || bb); | ||
numEQ += ~~(aa && bb); | ||
} | ||
return numNZ ? (numNZ - numEQ) / numNZ : 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ReadonlyVec } from "./api"; | ||
|
||
/** | ||
* @remarks | ||
* The Minkowski power `p` MUST be > 0. | ||
* | ||
* Reference: https://en.wikipedia.org/wiki/Minkowski_distance | ||
* | ||
* @param a | ||
* @param b | ||
* @param p | ||
*/ | ||
export const distMinkowski = (a: ReadonlyVec, b: ReadonlyVec, p: number) => { | ||
let delta = 0; | ||
for (let i = a.length; --i >= 0; ) { | ||
delta += Math.abs(a[i] - b[i]) ** p; | ||
} | ||
return delta ** (1 / p); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { DistanceFn } from "./api"; | ||
|
||
/** | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const distSorensenDice: DistanceFn = (a, b) => { | ||
let numTP = 0; | ||
let numFP = 0; | ||
for (let i = a.length; i-- > 0; ) { | ||
const aa = a[i] !== 0; | ||
const bb = b[i] !== 0; | ||
numTP += ~~(aa && bb); | ||
numFP += ~~(aa !== bb); | ||
} | ||
return numFP ? numFP / (2 * numTP + numFP) : 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters