Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Created April 2, 2020 14:24
Show Gist options
  • Select an option

  • Save twolodzko/c732e73cdb500673cddf900c3085d405 to your computer and use it in GitHub Desktop.

Select an option

Save twolodzko/c732e73cdb500673cddf900c3085d405 to your computer and use it in GitHub Desktop.
Sampling from multivariate normal distribution in Numpy
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