How to calculate the Cholesky decomposition of a non-square matrix in order to calculate the Mahalanobis distance with numpy?
def get_fitting_function(G):
print(G.shape)
g_mu = G.mean(axis=0)
L = np.linalg.cholesky(G)
def fitting_function(g):
x = g - g_mu
z = np.linalg.solve(L, x)
MD = z.T*z
return math.sqrt(MD)
return fitting_function
C:\Users\Matthias\CV\src\fitting_function.py in get_fitting_function(G)
22
23
---> 24 L = np.linalg.cholesky(G)
25
26 def fitting_function(g):
C:\Users\Matthias\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\linalg\linalg.pyc in cholesky(a)
598 a, wrap = _makearray(a)
599 _assertRankAtLeast2(a)
--> 600 _assertNdSquareness(a)
601 t, result_t = _commonType(a)
602 signature = 'D->D' if isComplexType(t) else 'd->d'
C:\Users\Matthias\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\linalg\linalg.pyc in _assertNdSquareness(*arrays)
210 for a in arrays:
211 if max(a.shape[-2:]) != min(a.shape[-2:]):
--> 212 raise LinAlgError('Last 2 dimensions of the array must be square')
213
214 def _assertFinite(*arrays):
LinAlgError: Last 2 dimensions of the array must be square
LinAlgError: Last 2 dimensions of the array must be square
Based on Matlab implementation: Mahalanobis distance modifying the covariance matrix
Edit: chol(a) = linalg.cholesky(a).T
cholesky factorization of the matrix ( chol(a)in matlab returns the upper triangular matrix, but linalg.cholesky(a)returns the lower triangular matrix) (source: http://wiki.scipy.org/NumPy_for_Matlab_Users )
Edit2:
G -= G.mean(axis=0)[None, :]
C = (np.dot(G, G.T) / float(G.shape[0]))
L = np.linalg.cholesky(C).T
therefore, if D = x ^ tS ^ -1.x = x ^ t. (LL ^ t) ^ - 1.x = x ^ tLL ^ tx = z ^ tz