Skip to content
Jonas Almeida edited this page Nov 6, 2025 · 29 revisions

Welcome to the metrics wiki!

Basic

Let's start by defining a ES6 module at basic.mjs for basic metrics, like Euclidean Distance.

https://epiverse.github.io/metrics/basic.mjs

Euclidean Distance

We can now import this module, or any individual basic metric:

euclid = (await import('https://epiverse.github.io/metrics/basic.mjs')).euclid

One can also retrieve the full module

mod = (await import('https://epiverse.github.io/metrics/basic.mjs'))
mod.euclid()
5.196152422706632

Note how execution of, say, the Euclidean Distance, is entirely performed on the client side:

dist = ((await import('https://epiverse.github.io/metrics/basic.mjs')).euclid)([1,2,3],[4,5,6])
5.196152422706632

note in these demonstrations that, when not specified, the vectors [1,2,3] and [4,5,6] will be used as default inputs.

Cosine distance

A quick inspection of the module will show other metrics. such as cosine distance:

mod.cosine([1,2,3],[4,5,6])
0.025368153802923787

True metrics

By definition, metrics are functions that have to satisfy a number of properties, a) non-negative; b) permutable; and c) triangular inequality. The two metrics approached here, Euclidean and Cosine, provide examples of the distinction.

Example data:

A=[1,0];
B=[0.6,0.8];
C=[-0.8,0.6];

Load module:

mod = (await import('https://epiverse.github.io/metrics/basic.mjs'))

Test triangular inequality

Euclidean Distance --> passed triangular inequality test

dAB = mod.euclid(A,B)
0.894427190999916
dBC = mod.euclid(B,C)
1.414213562373095
dAC = mod.euclid(A,C)
1.8973665961010275
dAB<=dAB+dAC
true

Cosine Distance --> did not pass triangular inequality test

A=[1,0];
B=[0.6,0.8];
C=[-0.8,0.6];
dAB = mod.cosine(A,B) = 0.4
dBC = mod.cosine(B,C) = 1
dAC = mod.cosine(A,C) = 1.8
dAC<=(dAB+dBC)
false