Created
August 5, 2021 07:52
-
-
Save insaneyilin/f8487ac27e4e2b097de608be9f141f01 to your computer and use it in GitHub Desktop.
The Hellinger distance measures the similarity of two probability
This file contains hidden or 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
// The Hellinger distance measures the similarity of two probability | |
// distributions Reference: https://en.wikipedia.org/wiki/Hellinger_distance | |
double HellingerDistance( | |
const std::vector<double>& histogram1, | |
const std::vector<double>& histogram2) { | |
const int histogram_size = histogram1.size(); | |
CHECK_GT(histogram_size, 0); | |
CHECK_EQ(histogram_size, histogram2.size()); | |
const double mean1 = | |
std::accumulate(histogram1.begin(), histogram1.end(), 0.0) / | |
histogram_size; | |
const double mean2 = | |
std::accumulate(histogram2.begin(), histogram2.end(), 0.0) / | |
histogram_size; | |
double distance = 0.0; | |
for (int i = 0; i < histogram_size; ++i) { | |
distance += std::sqrt(histogram1[i] * histogram2[i]); | |
} | |
distance = std::sqrt( | |
1.0 - (1.0 / std::sqrt(mean1 * mean2 * histogram_size * histogram_size)) * | |
distance); | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment