I have a function for calculating probability like below:
def multinormpdf(x, mu, var): # calculate probability of multi Gaussian distribution
k = len(x)
det = np.linalg.det(var)
inv = np.linalg.inv(var)
denominator = math.sqrt(((2*math.pi)**k)*det)
numerator = np.dot((x - mean).transpose(), inv)
numerator = np.dot(numerator, (x - mean))
numerator = math.exp(-0.5 * numerator)
return numerator/denominator
and I have mean vector, covariance matrix and 2D numpy array for test
mu = np.array([100, 105, 42]) # mean vector
var = np.array([[100, 124, 11], # covariance matrix
[124, 150, 44],
[11, 44, 130]])
arr = np.array([[42, 234, 124], # arr is 43923794 x 3 matrix
[123, 222, 112],
[42, 213, 11],
...(so many values about 40,000,000 rows),
[23, 55, 251]])
I have to calculate for probability for each value, so I used this code
for i in arr:
print(multinormpdf(i, mu, var)) # I already know mean_vector and variance_matrix
But it is so slow...
Is there any faster way to calculate probability? Or is there any way to calculate probability for test arr at once like 'batch'?