项目作者: tamaghnaroy

项目描述 :
Package for the simulation of random correlation matrix
高级语言: Python
项目地址: git://github.com/tamaghnaroy/RandomCorrMat.git
创建时间: 2017-11-28T17:06:28Z
项目社区:https://github.com/tamaghnaroy/RandomCorrMat

开源协议:MIT License

下载


RandomCorrMat

Implements several schemes to generate random correlation matrices, including
checks to validate a given matrix to be a proper correlation matrix.

Diagnostics

Diagnostics contain two main methods - isPD and isvalid_corr. These methods
check for the following conditions in a given matrix -

  1. The matrix is symmetric
  2. Diagonal == 1
  3. Off-Diagonal != 1
  4. The matrix is positive definite
Usage:
  1. import RandomCorrMat
  2. matrix_to_test = [[1 , 0.9 , -0.9], [0.9, 1, 0.25], [-0.9, 0.25, 1]]
  3. res = RandomCorrMat.isvalid_corr(matrix_to_test)
  4. res == True # if the correlation is valid
  5. res == False # if the correlation is invalid
  6. # To fetch more information in case the matrix is an invalid correlation matrix
  7. # we can use the following
  8. res.cause
Random Correlation Matrix Generation

To generate random correlation matrix, there are several schemes:

  1. Constant correlation matrix
  2. Simple random matrix
  3. Random matrix with given eigenvalues
  4. Random perturbation of a given correlation matrix
  5. Nearest correlation matrix to a given correlation matrix
Usages
  1. import RandomCorrMat
  2. import numpy
  3. size=10
  4. cor_value=0.5
  5. # Generate a constant correlation matrix
  6. RandomCorrMat.constantCorrMat(size, cor_value)
  7. # Generate a Random correalation matrix sampled uniformly from the space of corelation matrices
  8. RandomCorrMat.randCorrOnion(size)
  9. # Generate the Random correlation matrix, faster but no gaurantees
  10. RandomCorrMat.randCorr(size)
  11. # Generate a random correlation matrix with given eigenvalues
  12. e = numpy.r_[2, 1, 0.75, 0.25]
  13. corr_mat = RandomCorrMat.randCorrGivenEgienvalues(e)
  14. # Random perturbation of the correlation matrix
  15. given_corr_mat = numpy.array([[1, 0.5, 0.75],[0.5, 1, 0.75], [0.75, 0.75, 1]])
  16. new_corr = RandomCorrMat.perturb_randCorr(given_corr_mat)
  17. # Perturbation method can be used to generate correlation matrix with a given mean
  18. # For example: to generate a random correlation matrix with average corr = 0.75
  19. corr_mat = RandomCorrMat.constantCorrMat(4, 0.75)
  20. new_corr = RandomCorrMat.perturb_randCorr(corr_mat)
  21. # The variance of the perturbations obtained from the above method are not
  22. # sufficiently large, if we want to simulate random correlation with higher
  23. # variance or we need fine control on the noise added to the correlation matrix
  24. # Example to handle these
  25. manual_noisy_corr = given_corr_mat + numpy.random.normal(loc=0.0, scale=3.0, size=(3,3))
  26. valid_corr = RandomCorrMat.nearcorr(manual_noisy_corr)

References

Installing from PyPI

Try

pip install RandCorrMat

To install manually from the git repo, try this:

python setup.py install

The RandCorrMat codebase supports Python 2 and 3.

Attribution

If you happen to use RandCorrMat in your work or research, please cite its GitHub repository:

T. Roy, RandCorrMat, (2017), GitHub repository, https://github.com/tamaghnaroy/RandomCorrMat/

License

RandCorrMat is free software made available under the MIT License. For details see the LICENSE file.