Search code examples
pythoncovariancecovariance-matrix

Computing covariance matrix without using numpy


I am trying to compute the covariance matrix which maximises the likelihood estimate manually without using the numpy library,but I cannot seem to get the right answer. I am trying to go by this formula:

Maximum Likelihood estimate for covariance

I know i'm calculating the means correctly. So there must be an issue with the part where I actually compute the covariance but I have no idea where? This is my code:

mat = [[1,2,3],[4,6,8],[3,5,7]]

#now calc covariance for each element of the matrix
Cov = []
for j in range(len(means)):
    sum = 0
    covs = []
    for k in range(len(means)):
        for i in range(len(means)):
            sum += ((mat[i][j] - means[j]) * (mat[i][k] - means[k]))
        result  = sum/ len(means)
        covs.append(result)
    Cov.append(covs)
print(np.reshape(S,(3,3)))

This is what I get:

[[ 1.55555556  3.66666667  6.33333333]
[ 2.11111111  5.          8.66666667]
[ 2.66666667  6.33333333 11.        ]]

This is what i'm supposed to get:

[[1.55555556 2.11111111 2.66666667]
[2.11111111 2.88888889 3.66666667]
[2.66666667 3.66666667 4.66666667]]

Solution

  • You should reset the sum for each entry of the covariance matrix,

        covs = []
        for k in range(len(means)):
            sum = 0
            for i in range(len(means)):
                sum += ((mat[i][j] - means[j]) * (mat[i][k] - means[k]))
            covariance  = sum/ len(means)
            covs.append(covariance)
    

    You could shorten that a bit as

        covs = []
        for k in range(len(means)):
            terms = ( (mat[i][j] - means[j]) * (mat[i][k] - means[k]) for i in range(len(means)) )
            covariance  = sum(terms) / len(means)
            covs.append(covariance)
    

    Be sure to clear the workspace so that sum is again a built-in function and not a number.