Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
Created June 8, 2018 16:07
Show Gist options
  • Save righthandabacus/e5d3e46c1cd6bf1b21623060b4261a95 to your computer and use it in GitHub Desktop.
Save righthandabacus/e5d3e46c1cd6bf1b21623060b4261a95 to your computer and use it in GitHub Desktop.
Generating multivariate normal distribution with plot
# 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