Chi-square distance in Python

Chi-square distance in Python

The Chi-square distance between two probability distributions P and Q is defined as:

D(P,Q)=∑i​P(i)+Q(i)(P(i)−Q(i))2​

This distance metric is especially useful in cases like histogram comparison.

To compute the Chi-square distance in Python, you can use the following code:

def chi_square_distance(p, q): """ Compute the Chi-square distance between two distributions. :param p: List of probabilities from distribution P :param q: List of probabilities from distribution Q :return: Chi-square distance """ return sum([(pi - qi) ** 2 / (pi + qi) for pi, qi in zip(p, q)]) # Example usage: p = [0.2, 0.3, 0.5] q = [0.25, 0.25, 0.5] print(chi_square_distance(p, q)) 

Make sure that both p and q are valid probability distributions (i.e., they sum up to 1) and are of the same length.

Note: The provided function calculates the unnormalized Chi-square distance. In some contexts, you might want to normalize the value, depending on the application.


More Tags

variable-names parceljs web-audio-api ffmpeg uiview-hierarchy mapi excel-2007 jcreator linq-to-nhibernate bluetooth-gatt

More Programming Guides

Other Guides

More Programming Examples