How to assess the similarity of two histograms?

Given two histograms, how do we assess whether they are similar or not?

Is it sufficient to simply look at the two histograms? The simple one to one mapping has the problem that if a histogram is slightly different and slightly shifted then we'll not get the desired result.

Any suggestions?

migrated from stackoverflow.comFeb 19 '11 at 20:27

This question came from our site for professional and enthusiast programmers.

• What does "similar" mean? The chi-squared test and the KS test, for example, test whether two histograms are close to identical. But "similar" might mean "have the same shape," ignoring any differences of location and/or scale. Could you clarify your intent? – whuber Feb 21 '11 at 14:32

A recent paper that may be worth reading is:

Cao, Y. Petzold, L. Accuracy limitations and the measurement of errors in the stochastic simulation of chemically reacting systems, 2006.

Although this paper's focus is on comparing stochastic simulation algorithms, essentially the main idea is how to compare two histogram.

You can access the pdf from the author's webpage.

• Hi, its nice paper, thanx for giving pdf link.. I'll surely go through this paper.. – Mew 3.4 Feb 20 '11 at 3:52
• Instead of providing a reference it would be good if you'd summarize the main points of the paper. Links die, so in the future your answer could become useless for non-subscribers of this journal (and the vast majority of human population is the non-subscribers). – Tim May 8 '15 at 7:16

There are plenty of distance measures between two histogram. You can read a good categorization of these measures in:

K. Meshgi, and S. Ishii, “Expanding Histogram of Colors with Gridding to Improve Tracking Accuracy,” in Proc. of MVA’15, Tokyo, Japan, May 2015.

The most popular distance functions are listed here for your convenience:

• $$L_0$$　or Hellinger Distance

$$D_{L0} = \sum\limits_{i} h_1(i) \neq h_2(i)$$

• $$L_1$$, Manhattan, or City Block Distance

$$D_{L1} = \sum_{i}\lvert h_1(i) - h_2(i) \rvert$$

• $$L=2$$ or Euclidean Distance

$$D_{L2} = \sqrt{\sum_{i}\left( h_1(i) - h_2(i) \right) ^2 }$$

• L$$_{\infty}$$ or Chybyshev Distance

$$D_{L\infty} = max_{i}\lvert h_1(i) - h_2(i) \rvert$$

• L$$_p$$ or Fractional Distance (part of Minkowski distance family)

$$D_{Lp} = \left(\sum\limits_{i}\lvert h_1(i) - h_2(i) \rvert ^p \right)^{1/p}$$ and $$0

• Histogram Intersection

$$D_{\cap} = 1 - \frac{\sum_{i} \left(min(h_1(i),h_2(i) \right)}{min\left(\vert h_1(i)\vert,\vert h_2(i) \vert \right)}$$

• Cosine Distance

$$D_{CO} = 1 - \sum_i h_1(i)h2_(i)$$

• Canberra Distance

$$D_{CB} = \sum_i \frac{\lvert h_1(i)-h_2(i) \rvert}{min\left( \lvert h_1(i)\rvert,\lvert h_2(i)\rvert \right)}$$

• Pearson's Correlation Coefficient

$$D_{CR} = \frac{\sum_i \left(h_1(i)- \frac{1}{n} \right)\left(h_2(i)- \frac{1}{n} \right)}{\sqrt{\sum_i \left(h_1(i)- \frac{1}{n} \right)^2\sum_i \left(h_2(i)- \frac{1}{n} \right)^2}}$$

• Kolmogorov-Smirnov Divergance

$$D_{KS} = max_{i}\lvert h_1(i) - h_2(i) \rvert$$

• Match Distance

$$D_{MA} = \sum\limits_{i}\lvert h_1(i) - h_2(i) \rvert$$

• Cramer-von Mises Distance

$$D_{CM} = \sum\limits_{i}\left( h_1(i) - h_2(i) \right)^2$$

• $$\chi^2$$ Statistics

$$D_{\chi^2} = \sum_i \frac{\left(h_1(i) - h_2(i)\right)^2}{h_1(i) + h_2(i)}$$

• Bhattacharyya Distance

$$D_{BH} = \sqrt{1-\sum_i \sqrt{h_1(i)h_2(i)}}$$ & hellinger

• Squared Chord

$$D_{SC} = \sum_i\left(\sqrt{h_1(i)}-\sqrt{h_2(i)}\right)^2$$

• Kullback-Liebler Divergance

$$D_{KL} = \sum_i h_1(i)log\frac{h_1(i)}{m(i)}$$

• Jefferey Divergence

$$D_{JD} = \sum_i \left(h_1(i)log\frac{h_1(i)}{m(i)}+h_2(i)log\frac{h_2(i)}{m(i)}\right)$$

• Earth Mover's Distance (this is the first member of Transportation distances that embed binning information $$A$$ into the distance, for more information please refer to the abovementioned paper or Wikipedia entry.

$$D_{EM} = \frac{min_{f_{ij}}\sum_{i,j}f_{ij}A_{ij}}{sum_{i,j}f_{ij}}$$ $$\sum_j f_{ij} \leq h_1(i) , \sum_j f_{ij} \leq h_2(j) , \sum_{i,j} f_{ij} = min\left( \sum_i h_1(i) \sum_j h_2(j) \right)$$ and $$f_{ij}$$ represents the flow from $$i$$ to $$j$$

$$D_{QU} = \sqrt{\sum_{i,j} A_{ij}\left(h_1(i) - h_2(j)\right)^2}$$

$$D_{QC} = \sqrt{\sum_{i,j} A_{ij}\left(\frac{h_1(i) - h_2(i)}{\left(\sum_c A_{ci}\left(h_1(c)+h_2(c)\right)\right)^m}\right)\left(\frac{h_1(j) - h_2(j)}{\left(\sum_c A_{cj}\left(h_1(c)+h_2(c)\right)\right)^m}\right)}$$ and $$\frac{0}{0} \equiv 0$$

A Matlab implementation of some of these distances is available from my GitHub repository: https://github.com/meshgi/Histogram_of_Color_Advancements/tree/master/distance Also you can search guys like Yossi Rubner, Ofir Pele, Marco Cuturi and Haibin Ling for more state-of-the-art distances.

Update: Alternative explaination for the distances appears here and there in the literature, so I list them here for sake of completeness.

• Canberra distance (another version)

$$D_{CB}=\sum_i \frac{|h_1(i)-h_2(i)|}{|h_1(i)|+|h_2(i)|}$$

• Bray-Curtis Dissimilarity, Sorensen Distance (since the sum of histograms are equal to one, it equals to $$D_{L0}$$)

$$D_{BC} = 1 - \frac{2 \sum_i h_1(i) = h_2(i)}{\sum_i h_1(i) + \sum_i h_2(i)}$$

• Jaccard Distance (i.e. intersection over union, another version)

$$D_{IOU} = 1 - \frac{\sum_i min(h_1(i),h_2(i))}{\sum_i max(h_1(i),h_2(i))}$$

The standard answer to this question is the chi-squared test. The KS test is for unbinned data, not binned data. (If you have the unbinned data, then by all means use a KS-style test, but if you only have the histogram, the KS test is not appropriate.)

• You are correct that the KS test is not appropriate for histograms when it is understood as a hypothesis test about the distribution of the underlying data, but I see no reason why the KS statistic wouldn't work well as a measure of sameness of any two histograms. – whuber Feb 21 '11 at 14:34
• An explanation of why the Kolmogorov-Smirnov test is not appropriate with binned data would be useful. – naught101 Aug 7 '12 at 6:56
• This may not be as useful in image processing as in statistical fit assessment. Often in image processing, a histogram of data is used as a descriptor for a region of an image, and the goal is for a distance between histograms to reflect the distance between image patches. Little, or possibly nothing at all, may be known about the general population statistics of the underlying image data used to get the histogram. For example, the underlying population statistics when using histograms of oriented gradients would differ considerably based on the actual content of the images. – ely Aug 17 '15 at 19:33
• naught101's question was answered by Stochtastic: stats.stackexchange.com/a/108523/37373 – Lapis Aug 22 '15 at 10:33

You're looking for the Kolmogorov-Smirnov test. Don't forget to divide the bar heights by the sum of all observations of each histogram.

Note that the KS-test is also reporting a difference if e.g. the means of the distributions are shifted relative to one another. If translation of the histogram along the x-axis is not meaningful in your application, you may want to subtract the mean from each histogram first.

• Subtracting the mean changes the null distribution of the KS statistic. @David Wright raises a valid objection to the application of the KS test to histograms anyway. – whuber Feb 21 '11 at 14:36

As David's answer points out, the chi-squared test is necessary for binned data as the KS test assumes continuous distributions. Regarding why the KS test is inappropriate (naught101's comment), there has been some discussion of the issue in the applied statistics literature that is worth raising here.

An amusing exchange began with the claim (García-Berthou and Alcaraz, 2004) that one third of Nature papers contain statistical errors. However, a subsequent paper (Jeng, 2006, "Error in statistical tests of error in statistical tests" -- perhaps my all-time favorite paper title) showed that Garcia-Berthou and Alcaraz (2005) used KS tests on discrete data, leading to their reporting inaccurate p-values in their meta-study. The Jeng (2006) paper provides a nice discussion of the issue, even showing that one can modify the KS test to work for discrete data. In this specific case, the distinction boils down to the difference between a uniform distribution of the trailing digit on [0,9], $$P(x) = \frac{1}{9},\ (0 \leq x \leq 9)$$ (in the incorrect KS test) and a comb distribution of delta functions, $$P(x) = \frac{1}{10}\sum_{j=0}^9 \delta(x-j)$$ (in the correct, modified form). As a result of the original error, Garcia-Berthou and Alcaraz (2004) incorrectly rejected the null, while the chi-squared and modified KS test do not. In any case, the chi-squared test is the standard choice in this scenario, even if KS can be modified to work here.

You can compute the cross-correlation (convolution) between both histograms. That will take into account slight traslations.

• This is being automatically flagged as low quality, probably because it is so short. At present it is more of a comment than an answer by our standards. Can you expand on it? We can also turn it into a comment. – gung Mar 5 '18 at 14:32
• Since histograms are fairly unstable representations of data, and also because they do not represent probabilities using height alone (they use area), one might reasonably question the applicability, generality, or usefulness of this approach unless more specific guidance is provided. – whuber Mar 5 '18 at 15:07