make correlated array with cholesky decomposition python
import numpy as np
#desired expected rho (of the distribution of the corr matrix)
rho = 0.5
# desired correlation matrix
cor_matrix = np.ones((5,5))* rho
np.fill_diagonal(cor_matrix,1) # 1s in diagonal
print(cor_matrix)
# this is artificial case but it will result in the derired distribution.
array([[1. , 0.5, 0.5, 0.5, 0.5],
[0.5, 1. , 0.5, 0.5, 0.5],
[0.5, 0.5, 1. , 0.5, 0.5],
[0.5, 0.5, 0.5, 1. , 0.5],
[0.5, 0.5, 0.5, 0.5, 1. ]])
L = np.linalg.cholesky(cor_matrix)
# build some signals that will result in the desired correlation matrix
X_synthetic = L.dot(np.random.normal(0,1, (5,2000)))
# estimate their correlation matrix
np.corrcoef(X_synthetic)
array([[1. , 0.50576661, 0.51472813, 0.47208374, 0.49260528],
[0.50576661, 1. , 0.4798111 , 0.48540114, 0.47225243],
[0.51472813, 0.4798111 , 1. , 0.4649033 , 0.4745259 ],
[0.47208374, 0.48540114, 0.4649033 , 1. , 0.50059795],
[0.49260528, 0.47225243, 0.4745259 , 0.50059795, 1. ]])
#* Very good approximation. All values are fluctuating around 0.5.
#* So the distribution of the correlation values of `X_synthetic` is around the expected value `0.5`.
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types