distance function

double distance(
  1. List<double> p,
  2. List<double> q
)

Cramer Distance

The Cramer Distance is a measure of dissimilarity between two distributions and is calculated by taking the square root of the sum of the squared differences between the two distributions. The function assumes that both p and q are lists of probabilities with the same length. If the lists are of different lengths, the function throws an ArgumentError.

Implementation

double distance(List<double> p, List<double> q) {
  if (p.length != q.length) {
    throw ArgumentError('The length of p and q must be the same.');
  }
  double sum = 0;
  for (int i = 0; i < p.length; i++) {
    sum += pow(p[i] - q[i], 2);
  }
  return sqrt(sum);
}