Skip to content

Instantly share code, notes, and snippets.

@miku
Last active December 30, 2017 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/1671b2014b003ee7b9054c0618c805f7 to your computer and use it in GitHub Desktop.
Save miku/1671b2014b003ee7b9054c0618c805f7 to your computer and use it in GitHub Desktop.
Bhattacharyya python.
import math
def bhattacharyya(a, b):
""" Bhattacharyya distance between distributions (lists of floats). """
if not len(a) == len(b):
raise ValueError("a and b must be of the same size")
return -math.log(sum((math.sqrt(u * w) for u, w in zip(a, b))))
x = bhattacharyya([0.1, 0.9], [0.1, 0.9])
print(x)
x = bhattacharyya([0.2, 0.8], [0.5, 0.5])
print(x)
x = bhattacharyya([0.1, 0.9], [0.5, 0.5])
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment