Created
April 2, 2020 14:24
-
-
Save twolodzko/c732e73cdb500673cddf900c3085d405 to your computer and use it in GitHub Desktop.
Sampling from multivariate normal distribution in Numpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import scipy.stats as sp | |
| C = np.array([ | |
| [3, 2, 3], | |
| [2, 4, 2], | |
| [3, 2, 3] | |
| ]) | |
| n = 100_000 | |
| k = C.shape[0] | |
| U, S, V = np.linalg.svd(C, full_matrices=False) | |
| S = np.sqrt(np.diag(S)) | |
| X = (U @ S) @ sp.norm.rvs(size=(k, n)) | |
| np.corrcoef(C.flatten(), np.cov(X).flatten())[0,1] | |
| # 0.9999093788272446 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment