jeudi 8 octobre 2020

Python Issue - Correlated Random Samples and Cholesky Decomposition

import numpy as np
from scipy.linalg import cholesky

X1_temp = np.random.normal(0, 1, (40, 10)) # with mean 0 and std. dev. 1
C = cholesky(C0, lower = False)
X1 = X1_temp @ np.transpose(C)

Thanks very much in advance. This is just a very simple task. I am given a desired covariance matrix C0. I want to generate random correlated data matrix X1 such that the covariance matrix of X1 is C0. I use Cholesky Decomposition. The codes are in Python.

However, if I then manually use np.cov() to calculate the covariance matrix of X1, it turns out to be quite different from C0. Please see below for the results.

C0
array([[0.00119545, 0.00055428, 0.00094478, 0.00057466, 0.00039038,
        0.0004846 , 0.00047505, 0.00039403, 0.00041767, 0.00053985],
       [0.00055428, 0.00055869, 0.0005272 , 0.00046757, 0.0002733 ,
        0.00037907, 0.00034639, 0.00030749, 0.00034975, 0.00041578],
       [0.00094478, 0.0005272 , 0.00163294, 0.00049306, 0.00032083,
        0.0004176 , 0.000403  , 0.00038588, 0.00037359, 0.00050818],
       [0.00057466, 0.00046757, 0.00049306, 0.00161212, 0.00065667,
        0.00080889, 0.00075579, 0.00048425, 0.00066279, 0.00044288],
       [0.00039038, 0.0002733 , 0.00032083, 0.00065667, 0.00076096,
        0.00054659, 0.00061203, 0.00032021, 0.00047247, 0.00025952],
       [0.0004846 , 0.00037907, 0.0004176 , 0.00080889, 0.00054659,
        0.00116606, 0.00056961, 0.00040392, 0.00053751, 0.00033647],
       [0.00047505, 0.00034639, 0.000403  , 0.00075579, 0.00061203,
        0.00056961, 0.0008417 , 0.0003747 , 0.00054147, 0.00033309],
       [0.00039403, 0.00030749, 0.00038588, 0.00048425, 0.00032021,
        0.00040392, 0.0003747 , 0.00042903, 0.00035186, 0.00030802],
       [0.00041767, 0.00034975, 0.00037359, 0.00066279, 0.00047247,
        0.00053751, 0.00054147, 0.00035186, 0.00061011, 0.00033744],
       [0.00053985, 0.00041578, 0.00050818, 0.00044288, 0.00025952,
        0.00033647, 0.00033309, 0.00030802, 0.00033744, 0.00048086]])

np.cov(X1,rowvar=False)
array([[ 2.73579114e-03,  1.11107458e-03,  7.39905489e-04,
         8.80909471e-04,  2.67940563e-04,  3.00456896e-04,
         2.78558784e-04,  1.95630006e-04, -8.84664656e-07,
         3.07873704e-04],
       [ 1.11107458e-03,  7.67420975e-04,  6.32049491e-05,
         7.29592024e-04,  2.30491196e-04,  2.16890868e-04,
         1.67582787e-04,  8.49125674e-05, -8.34866957e-06,
         1.68615033e-04],
       [ 7.39905489e-04,  6.32049491e-05,  1.14805709e-03,
        -2.24580150e-04,  8.49680219e-05, -1.42327782e-05,
        -1.19349618e-04, -1.75276890e-05,  3.43158455e-05,
         6.89819740e-05],
       [ 8.80909471e-04,  7.29592024e-04, -2.24580150e-04,
         1.44999733e-03,  1.27584928e-04,  3.49017835e-04,
         1.89078071e-04,  4.95665628e-05, -3.51667683e-05,
         1.25940711e-04],
       [ 2.67940563e-04,  2.30491196e-04,  8.49680219e-05,
         1.27584928e-04,  4.63007494e-04,  1.58792113e-04,
         5.37907654e-05,  5.71734068e-05,  2.18693890e-05,
         7.16343773e-05],
       [ 3.00456896e-04,  2.16890868e-04, -1.42327782e-05,
         3.49017835e-04,  1.58792113e-04,  4.23799861e-04,
         1.37080444e-05,  9.08977322e-06, -6.28273606e-05,
         6.64415798e-06],
       [ 2.78558784e-04,  1.67582787e-04, -1.19349618e-04,
         1.89078071e-04,  5.37907654e-05,  1.37080444e-05,
         2.33866264e-04,  6.65816973e-05,  1.84881869e-05,
         5.54885576e-05],
       [ 1.95630006e-04,  8.49125674e-05, -1.75276890e-05,
         4.95665628e-05,  5.71734068e-05,  9.08977322e-06,
         6.65816973e-05,  2.04723105e-04,  3.60286054e-05,
         4.11557090e-05],
       [-8.84664656e-07, -8.34866957e-06,  3.43158455e-05,
        -3.51667683e-05,  2.18693890e-05, -6.28273606e-05,
         1.84881869e-05,  3.60286054e-05,  1.43719872e-04,
         9.74047167e-06],
       [ 3.07873704e-04,  1.68615033e-04,  6.89819740e-05,
         1.25940711e-04,  7.16343773e-05,  6.64415798e-06,
         5.54885576e-05,  4.11557090e-05,  9.74047167e-06,
         1.24753756e-04]])



Aucun commentaire:

Enregistrer un commentaire