Skip to content

Instantly share code, notes, and snippets.

@larsmans
Created July 15, 2012 13:25
Show Gist options
  • Save larsmans/3116927 to your computer and use it in GitHub Desktop.
Save larsmans/3116927 to your computer and use it in GitHub Desktop.

Revisions

  1. larsmans revised this gist Jul 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hellinger.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    from scipy.spatial.distance import euclidean


    _SQRT2 = np.sqrt(2) # sqrt(2) with default precision np.float64)
    _SQRT2 = np.sqrt(2) # sqrt(2) with default precision np.float64


    def hellinger1(p, q):
  2. larsmans created this gist Jul 15, 2012.
    23 changes: 23 additions & 0 deletions hellinger.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    """
    Three ways of computing the Hellinger distance between two discrete
    probability distributions using NumPy and SciPy.
    """

    import numpy as np
    from scipy.linalg import norm
    from scipy.spatial.distance import euclidean


    _SQRT2 = np.sqrt(2) # sqrt(2) with default precision np.float64)


    def hellinger1(p, q):
    return norm(np.sqrt(p) - np.sqrt(q)) / _SQRT2


    def hellinger2(p, q):
    return euclidean(np.sqrt(p), np.sqrt(q)) / _SQRT2


    def hellinger3(p, q):
    return np.sqrt(np.sum((np.sqrt(p) - np.sqrt(q)) ** 2)) / _SQRT2