Last active
January 26, 2021 17:07
-
-
Save omarfsosa/e291a6de41bbfcea97eac9853804a8d6 to your computer and use it in GitHub Desktop.
Sample from a multinormal distribution when given the correlation coefficient
This file contains 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 | |
# given | |
mu_x = 10 | |
mu_y = 20 | |
sigma_x = 2 | |
sigma_y = 3 | |
corr = 0.3 | |
# --- | |
mu = [mu_x, mu_y] | |
cov = [[sigma_x**2, corr * sigma_x * sigma_y], [corr * sigma_x * sigma_y, sigma_y**2]] | |
n_samples = 10_000 | |
samples = np.random.multivariate_normal(mu, cov, size=n_samples) | |
hat_corr = np.corrcoef(samples[:, 0], samples[:, 1])[0, 1] | |
print(hat_corr - corr) # Should be close to zero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment