SciPy

scipy.stats.random_correlation

scipy.stats.random_correlation = <scipy.stats._multivariate.random_correlation_gen object>[source]

A random correlation matrix.

Return a random correlation matrix, given a vector of eigenvalues.

The eigs keyword specifies the eigenvalues of the correlation matrix, and implies the dimension.

Parameters:

eigs : 1d ndarray

Eigenvalues of correlation matrix.

Notes

Generates a random correlation matrix following a numerically stable algorithm spelled out by Davies & Higham. This algorithm uses a single O(N) similarity transformation to construct a symmetric positive semi-definite matrix, and applies a series of Givens rotations to scale it to have ones on the diagonal.

References

[R628]Davies, Philip I; Higham, Nicholas J; “Numerically stable generation of correlation matrices and their factors”, BIT 2000, Vol. 40, No. 4, pp. 640 651

Examples

>>> from scipy.stats import random_correlation
>>> np.random.seed(514)
>>> x = random_correlation.rvs((.5, .8, 1.2, 1.5))
>>> x
array([[ 1.        , -0.20387311,  0.18366501, -0.04953711],
       [-0.20387311,  1.        , -0.24351129,  0.06703474],
       [ 0.18366501, -0.24351129,  1.        ,  0.38530195],
       [-0.04953711,  0.06703474,  0.38530195,  1.        ]])
>>> import scipy.linalg
>>> e, v = scipy.linalg.eigh(x)
>>> e
array([ 0.5,  0.8,  1.2,  1.5])

Methods

rvs(eigs=None, random_state=None) Draw random correlation matrices, all with eigenvalues eigs.