Created
June 8, 2018 16:07
-
-
Save righthandabacus/e5d3e46c1cd6bf1b21623060b4261a95 to your computer and use it in GitHub Desktop.
Generating multivariate normal distribution with plot
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
# 2D multivariate normal | |
# with covariance matrix [3 2; 2 5], i.e. Var(x)=3, Var(y)=5, Cov(x,y)=2 | |
# Break down covariacne matrix with Cholesky decomposition and multiply with iid std normal random numbers | |
Sigma = [4 9; 9 25] | |
S = chol(Sigma) # upper triangular part, i.e. S' * S == Sigma | |
iidnorm = randn(2,1000) | |
rv = S' * iidnorm # 2x1000 matrix of 1000 random number pairs conforms to the covariance matrix Sigma | |
# Plot in 2D | |
using Gadfly | |
plot(x=mvnorm[1,:], y=mvnorm[2,:], Geom.point) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment