-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwise.py
More file actions
154 lines (122 loc) · 4.47 KB
/
Copy pathpwise.py
File metadata and controls
154 lines (122 loc) · 4.47 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python3
import unittest
import numpy as np
import scipy
from scipy.stats import entropy
def gauss(sample_phi,phi,sample_psi,psi,sigma):
return np.exp((-1.0*((sample_phi-phi)**2 + (sample_psi-psi)**2))/(2.0*(sigma**2)))
def ParzenWindow(w, h, d=1):
"""""""""""""""""""""""
Average of the gaussian window functions centered on each data point of w for marginal or joint density
estimation at a particular point x
Parameters
----------
w = vector of distances from a point x to the other points
h = window width
d = length or the variable dimension (1 by default if unit variable for marginal density and 2 if bivariate
variable for joint density.)
Returns
-------
pw = Estimation of the parzen window function for the density estimation f(w)
"""""""""""""""""""""""
if d>1:
pw = np.sum(np.prod(GaussianWindow(list(w),h),1))
else: pw = np.sum(GaussianWindow(w,h))#np.sum(np.exp(-w**2/(2*phi))/den)
return pw
def GaussianWindow(w,h):
"""
Gaussian kernel function with a variance of 2h^2
:param w: vector of distances from a point x to the other points
:param h: Window width
:return: VAlue of the gaussian function
"""
phi = 2*h**2
den = (2*np.pi*phi)**(1/2)
return den*np.exp(-np.power(w,2)/(2*phi))
def getPairWiseArray(dims):
_unity = 1.0
_diag = _unity
data = np.zeros(dims, dtype=float)
for i in range(min(dims)):
data[i, i] = _diag
return data
def setPairWiseArrayPair(data, row, col, p, q):
"""Set both P(i,j) and P(j,i) to p """
assert (p >= 0.0 and p <= 1.0)
data[row, col] = p
data[col, row] = p if q is None else q
def getSelfSimilarity(data):
rows, cols = data.shape
ss = np.zeros((1,cols), dtype=float)
count_entropy = np.log2(rows)
# stats.entropy will normalize if row does not add up to 1
# we normalize ourseleves
for row in range(rows):
pk = data[row,:]
pk = 1.0 * pk / np.sum(pk, axis=0, keepdims=True)
ss[0,row] = scipy.stats.entropy(pk, None, base=2) / count_entropy
return ss
def getPairWiseArrayStats(data):
"""Return row means """
stats = {}
stats['means'] = np.mean(data, axis=0)
stats['ranks'] = np.argsort(stats['means'])
return stats
class TestMethods(unittest.TestCase):
def test_array_create(self):
dim = 3
foo = getPairWiseArray((dim,dim))
for row in range(dim):
for col in range(dim):
if row == col:
self.assertEqual(foo[row,col], 1.0)
continue
self.assertEqual(foo[row,col], 0.0)
def test_array_roll(self):
dim = 3
foo = getPairWiseArray((dim, dim))
## fill up with random
for row in range(dim):
for col in range(dim):
if row == col:
continue
setPairWiseArrayPair(foo, row, col, np.random.random_sample())
# make a copy
fcopy = np.copy(foo)
# fill in for the new entry in the first tow
for col in range(dim):
if col == 0: continue
setPairWiseArrayPair(fcopy, 0, col, np.random.random_sample())
# roll by the number of unchanged that is dim - 1
np.roll(fcopy, ((dim - 1) * dim, 0))
# compare
cmp = foo == fcopy
for row in range(dim):
for col in range(dim):
if row == col or (row > 0 and col > 0):
self.assertEqual(cmp[row, col], True)
continue
self.assertEqual(cmp[row, col], False)
def test_scipy_entropy(self):
data = [0.05, 0.5, 0.9, 0.3, 0.2]
sum = np.sum(data)
normed = np.divide(data,sum)
print((sum, normed))
entropy = 0
for ee in normed:
vv = ee * np.log2(ee) * -1.0
entropy += vv
entropy = entropy
sci_entropy = scipy.stats.entropy(normed, None, base=2)
self.assertAlmostEqual(entropy,sci_entropy)
def test_get_ss(self):
data = [[1. , 0.99822814, 0.99088965],
[0.99822814 ,1. , 0.99486904],
[0.99088965 ,0.99486904 ,1.]]
ss = getSelfSimilarity(np.asarray(data))
print(('ss',ss))
self.assertAlmostEqual(ss[0,0], 0.99999286, 5)
self.assertAlmostEqual(ss[0,1], 0.99999793, 5)
self.assertAlmostEqual(ss[0,2], 0.99999361, 5)
if __name__ == '__main__':
unittest.main()