AlSub
AlSub

Reputation: 1155

how to properly accomodate a matrix in order to convert it to a positive semi-definite matrix?

I am trying to convert a matrix to a semi-definite matrix by using nearPSD() function:

import numpy as np


phi_zero, phi_one= 0.7, -0.2

A=[[phi_zero, phi_one],
  [1, 0]]


def nearPSD(A,epsilon=0):
   n = A.shape[0]
   eigval, eigvec = np.linalg.eig(A)
   val = np.matrix(np.maximum(eigval,epsilon))
   vec = np.matrix(eigvec)
   T = 1/(np.multiply(vec,vec) * val.T)
   T = np.matrix(np.sqrt(np.diag(np.array(T).reshape((n)) )))
   B = T * vec * np.diag(np.array(np.sqrt(val)).reshape((n)))
   out = B*B.T
   return out

However, when applied:

nearPSD(A=A)

The next error arises:

AttributeError: 'list' object has no attribute 'shape'

How could I accomodate primitive A matrix in order to get a semi-definite matrix?

Upvotes: 0

Views: 122

Answers (1)

Woodford
Woodford

Reputation: 4449

You're passing a list to a function that expects a np.array. Simple fix:

nearPSD(A=np.array(A))

Upvotes: 1

Related Questions