Michael
Michael

Reputation: 1180

Inaccurate results while reconstructing covariance matrix from np.random.multivariate_normal

I have a need to simulate data from the 2-dimensional normal distribution along with a correlation parameter. To do this I have used np.random.multivariate_normal with a covariance matrix that has my squared sigmas as diagonal entries and product of sigmas and correlation coefficient elsewhere (I hope this is the right way to generate data with a correlation).

But I am afraid, I don't understand how to correctly reconstruct covariance matrix from the generated data. I have tried to get covariance matrix with np.cov and tried to reduce generated data to a zero-mean form and then create covariance matrix by a dot product of that data.

Here is my code:

import numpy as np
from matplotlib import pyplot as plt


class NormalDist:
    def __init__(self, *args):
        self.mu = args[:2]
        self.sigma = args[2:4]
        self.dist, self.cov = None, None

    def generate(self, rho=0., n=100):
        """ generate distributed data """
        self.cov = np.diag(np.array(self.sigma, np.float))
        self.cov = np.power(self.cov, 2)
        corr = rho * self.sigma[0] * self.sigma[1]
        self.cov[0, 1], self.cov[1, 0] = corr, corr
        self.dist = np.random.multivariate_normal(self.mu, self.cov, n)


if __name__ == '__main__':
    gauss = NormalDist(1, 2, 4, 9)
    gauss.generate(1/3)

    # covariance matrix from np.cov
    print(np.cov(gauss.dist.T), '\n')

    # covariance matrix from reducing data to zero-mean form
    zero_mean = gauss.dist - gauss.dist.mean(axis=0, keepdims=True)
    print(zero_mean.T @ zero_mean)

Output:

[[13.84078951  9.60607718]
 [ 9.60607718 79.33658308]] 

[[1370.23816181  951.00164066]
 [ 951.00164066 7854.32172506]]

Upvotes: 0

Views: 189

Answers (1)

Sam Mason
Sam Mason

Reputation: 16174

you just need to divide through by the sample size, i.e:

def np_mv_cov(X):
    X = X - X.mean(axis=0, keepdims=True)
    return (X.T @ X) / (X.shape[0] - 1)

can be tested with simplified version of your above code:

import numpy as np

dist = np.random.multivariate_normal([1, 2], [[16, 12], [12, 81]], 100)

d = np.cov(dist.T) - np_mv_cov(dist)

print(np.max(np.abs(d)))

gives me ~1.42e-14.

Upvotes: 1

Related Questions