-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_distance.py
44 lines (32 loc) · 994 Bytes
/
functions_distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
def norm_data(data):
m = np.mean(data)
return np.array([x/m for x in data])
def compute_probs(data, n=10):
h, e = np.histogram(data, n)
p = h/data.shape[0]
return e, p
def support_intersection(p, q):
sup_int = (
list(
filter(
lambda x: (x[0]!=0) & (x[1]!=0), zip(p, q)
)
)
)
return sup_int
def get_probs(list_of_tuples):
p = np.array([p[0] for p in list_of_tuples])
q = np.array([p[1] for p in list_of_tuples])
return p, q
def get_probabilities(train_sample, test_sample, n_bins=10):
e, p = compute_probs(train_sample, n=n_bins)
_, q = compute_probs(test_sample, n=e)
list_of_tuples = support_intersection(p,q)
p, q = get_probs(list_of_tuples)
return p, q
def kl_divergence(p, q):
return np.sum(p*np.log(p/q))
def js_divergence(p, q):
m = (1./2.)*(p + q)
return (1./2.)*kl_divergence(p, m) + (1./2.)*kl_divergence(q, m)