I have the following function in python which I am not able to figure out how to express in vectorized form. For me, cov is a numpy array of shape (2,2), mu is the mean vector with shape (2,) and xtp is of shape (~50000,2). I know scipy provides scipy.stats.multivariate_normal.pdf but I am trying to learn how to write efficient vectorized code. Please
def mvnpdf(xtp, mu, cov):
temp = np.zeros(xtp.shape[0])
i = 0
length = xtp.shape[0]
const = 1 / ( ((2* np.pi)**(len(mu)/2)) * (np.linalg.det(cov)**(1/2)) )
inv = np.linalg.inv(cov)
while i < length:
x = xtp[i]-mu
exponent = (-1/2) * (x.dot(inv).dot(x))
temp[i] = (const * np.exp(exponent))
i+=1
return temp