statsmodels.stats.moment_helpers.cov2corr#

statsmodels.stats.moment_helpers.cov2corr(cov, return_std=False, *, use_namedtuple=None)[source]#

Convert covariance matrix to correlation matrix

Parameters:
covarray_like, 2d

Covariance matrix, see Notes.

return_stdbool

If this is true then the standard deviation is also returned. By default only the correlation matrix is returned.

use_namedtuplebool, optional

Flag controlling whether a Cov2CorrResult NamedTuple is returned. When return_std=True a Cov2CorrResult is always returned; it holds the same two elements as the legacy tuple, so it unpacks and indexes identically. When return_std=False a bare correlation matrix is returned unless use_namedtuple=True, which yields a Cov2CorrResult carrying the standard deviations too.

Returns:
Cov2CorrResult or ndarray

When return_std=True (or use_namedtuple=True), a NamedTuple with fields:

corrndarray (subclass)

Correlation matrix.

stdndarray

Standard deviation from the diagonal of cov.

Cov2CorrResult has the same length and contents as the plain (corr, std_) tuple it replaces, so it unpacks and indexes identically. See Cov2CorrResult.

When return_std=False and use_namedtuple is not True, a bare correlation matrix is returned instead.

Notes

This function does not convert subclasses of ndarrays. This requires that division is defined elementwise. np.ma.array and np.matrix are allowed.