In [1]:
from numpy.linalg import inv
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from scipy.linalg import eig
from sklearn.metrics import pairwise_distances
from diffmaps_util import k, diag
In [2]:
X = np.array([1.0, 1.1, 1.0, 1.2, 1.2, 1.3]).reshape(3,2)
X
Out[2]:
In [5]:
X, y = make_blobs(n_samples=3, cluster_std=.1, centers=[[1,1]])
X
Out[5]:
In [6]:
L = k(X, .7)
D = diag(L)
M = inv(D).dot(L)
Ms = diag(D, .5).dot(M).dot(diag(D,-.5))
print 'D\n %s' % D
print 'M\n %s' % M
print 'Ms\n %s' % Ms
In [7]:
p0 = np.array([1,0,0])
In [8]:
e = p0
for i in range(100):
e = e.dot(M)
print e, sum(e)
In [9]:
d = np.diag(D/ np.diag(D).sum())
d
Out[9]:
The chain is reversible following the detailed balanced condition
$ \pi(x) p(x,y) = \pi(y) p(y,x) $
In [10]:
print d[0] * p[0,1]
print d[1] * p[1,0]
In [11]:
e = p0
for i in range(1):
e = e.dot(M)
print e, sum(e)
In [12]:
w, v = eig(Ms)
w = w.real
print 'phi\n %s' % (v * (d ** .5))
print 'psi\n %s' % (v * (d ** -.5))
In [13]:
w, v = eig(Ms)
w = w.real
print w
print v
In [16]:
c = inv(v).dot(np.eye(3))
#c = inv(v).dot(np.array([1,0,0]))
c
Out[16]:
In [17]:
c.dot(p0)
Out[17]:
In [18]:
e
Out[18]:
In [19]:
c[0]*v[:,0] + c[2]*w[2]*v[:,2] + c[1]*w[1]*v[:,1]
Out[19]:
$ p(x,y) = \frac{k(x,y)}{d(x)} $
In [20]:
p = []
for x in range(3):
_ = []
for y in range(3):
_.append(L[x,y]/d[x])
p.append(_)
p = np.array(p)
print p
$ a(x,y) = \frac{k(x,y)}{\sqrt{\pi(x)} \sqrt{\pi(y)}} $
In [21]:
a = []
for i in range(3):
b = []
for j in range(3):
b.append(L[i,j]/(np.sqrt(d[i])*np.sqrt(d[j])))
a.append(b)
a = np.array(a)
print a
$ a(x,y) = \frac{\sqrt{\pi(x)}}{\sqrt{\pi(y)}} p(x, y) $
In [22]:
a = []
for x in range(3):
_ = []
for y in range(3):
_.append(np.sqrt(e[x])/np.sqrt(e[y])*p[x,y])
a.append(_)
a = np.array(a)
print a
$ \phi_j = \mathbf{v}_j D^{1/2} $
In [23]:
phi = v * (d ** .5)
print phi
$ \phi_l(x) = \Phi_l(x) \times \pi(x) $
In [24]:
print v * v[:,0]
$ \psi_j = \mathbf{v}_j D^{-1/2} $
In [25]:
psi = v * (d**-.5)
print psi
$ \psi_l(x) = \Phi_l(x) / \pi(x) $
In [26]:
psi = v / v[:,0]
print psi
$ a(x,y) = \sum_{l \geq 0} \lambda_l \phi_l(x) \phi_l(y) $
In [27]:
a = []
for _ in range(3):
a.append(w[_] * phi[:,0] * phi[:,0])
a = np.array(a)
a
Out[27]:
$ p(t,y|x) = \phi_0(y) + \sum_{j \geq 1} a_j(x) \lambda_j^t \phi_j(y) $
In [28]:
e = p0
for i in range(1):
e = e.dot(Ms)
print e
In [29]:
p = []
for _ in range(0,3):
p.append(inv(v).dot(p0)[_] * w[_] * v[:,_])
p = np.array(p)
print p
print p.sum(axis=0)
In [30]:
diffmap = w * psi.T
diffmap
Out[30]:
The diffusion distance is equal to Euclidean distance in the diffusion map space with all (n-1) eigenvectors.
In [32]:
pairwise_distances(diffmap[:,1:])
Out[32]:
In [ ]: