Search code examples
pythonnumpymatrixcovariancecovariance-matrix

Computing covariance matrix of complex array with defined function is not matching while comparing with np.cov


I am trying to write a simple covariance matrix function in Python.

import numpy as np

def manual_covariance(x):
    mean = x.mean(axis=1)
    print(x.shape[1])
    cov = np.zeros((len(x), len(x)), dtype='complex64')

    for i in range(len(mean)):
        for k in range(len(mean)):
            s = 0
            for j in range(len(x[1])):  # 5 Col
                s += np.dot((x[i][j] - mean[i]), (x[k][j] - mean[i]))
            cov[i, k] = s / ((x.shape[1]) - 1)
    return cov

With this function if I compute the covariance of:

A = np.array([[1, 2], [1, 5]])
man_cov = manual_covariance(A)
num_cov = np.cov(A)

My answer matches with the np.cov(), and there is no problem. But, when I use complex number instead, my answer does not match with np.cov()

A = np.array([[1+1j, 1+2j], [1+4j, 5+5j]])
man_cov = manual_covariance(A)
num_cov = cov(A)

Manual result:
[[-0.5+0.j -0.5+2.j]
 [-0.5+2.j  7.5+4.j]]

Numpy cov result:
[[0.5+0.j 0.5+2.j]
 [0.5-2.j 8.5+0.j]]

I have tried printing every statement, to check where it can go wrong, but I am not able to find a fault.


Solution

  • It is because the dot product of two complex vectors z1 and z2 is defined as z1 · z2*, where * means conjugation. If you use s += np.dot((x[i,j] - mean[i]), np.conj(x[k,j] - mean[i])) you should get the correct result, where we have used Numpy's conjugate function.