2
$\begingroup$

I have a $100$x$100$ covariance matrix that looks like this. enter image description here

Some rows/cols are all-zero because those corresponding elements are not present in the sample from which covariance is calculated. I'm doing it this way:

    ...
    adjmats = [get_adjmat(graph) for graph in samples]                   # array of adjacency matrices
    reduced = functools.reduce(lambda x, y: np.add(x, y), adjmats)       # add all elem-wise
    adjacency = np.divide(reduced, len(adjmats))                         # divide by number: "mean"


    fig, ax = plt.subplots()

    covariance= np.cov(adjacency)                                        # getting covariance 


    def correlation_from_covariance(covariance):
        v = np.sqrt(np.diag(covariance))
        outer_v = np.outer(v, v)
        correlation = covariance / outer_v                   <<<<<<      # complains here!
        correlation[covariance == 0] = 0
        return correlation

    correlation = correlation_from_covariance(covariance)                # attempting to convert

    im = ax.imshow(correlation)

When i try to get the correlation matrix, which i vaguely know to be the std-"normalized" version of covariance matrix, numpy complains : subunit_graph.py:218: RuntimeWarning: invalid value encountered in true_divide correlation = covariance / outer_v, but i still get a sensible correlation matrix. Can somebody explain to me what exactly is going on with true_divide in there?
Thank you very much!

enter image description here

$\endgroup$
2
$\begingroup$

What about the following?

Dinv = np.diag(1.0 / np.sqrt(np.diag(cov_matrix)))
corr = Dinv @ cov_matrix @ Dinv

The above avoids any division except by the diagonal values, which should be nonzero anyway.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.