If you mean the individual distributions are Gaussian, then sampling from a multivariate normal with mean vector $\mathbf{\mu}$ and covariance matrix $\mathbf{\Sigma}$ will generate such data.
Here is an R example using the function mvrnorm()
in package MASS (which comes with R):
## means of individual distributions
mu1 <- 5
mu2 <- 10
mu3 <- 0
## variance
sigma1 <- 5
sigma2 <- 1
sigma3 <- 0.5
## Correlations
X1 <- 0.5
X2 <- 0.1
X3 <- 0.8
## load package
require("MASS")
We need to supply n
, the number of values from each distribution, mu
the mean vector, and Sigma
the covariance matrix. In the code below I form these from the scalars entered above.
set.seed(1)
dat <- mvrnorm(100, mu = c(mu1, mu2, mu3),
Sigma = matrix(c(sigma1, X1 , X3,
X1 , sigma2, X2,
X3 , X2 , sigma3),
ncol = 3, byrow = TRUE),
empirical = TRUE)
I used empirical = TRUE
to specify empirical not population parameters for $\mathbf{\mu}$ and $\mathbf{\Sigma}$. This results in the covariance matrix of dat
having exactly the values we specified:
R> cov(dat)
[,1] [,2] [,3]
[1,] 5.0 0.5 0.8
[2,] 0.5 1.0 0.1
[3,] 0.8 0.1 0.5
as do the column means:
R> colMeans(dat)
[1] 5.000e+00 1.000e+01 -8.882e-18
If you use the default, empirical = FALSE
, then you get random samples from a population which will have different sample mean vector and sample covariance matrix from the specified one as you have only seen n
examples from that larger population:
set.seed(1)
dat2 <- mvrnorm(100, mu = c(mu1, mu2, mu3),
Sigma = matrix(c(sigma1, X1 , X3,
X1 , sigma2, X2,
X3 , X2 , sigma3),
ncol = 3, byrow = TRUE))
R> cov(dat2)
[,1] [,2] [,3]
[1,] 4.0441 0.39858 0.61120
[2,] 0.3986 0.91110 0.04842
[3,] 0.6112 0.04842 0.48782
R> colMeans(dat2)
[1] 5.24138 10.06668 0.02448
R
code: see stats.stackexchange.com/q/24257 for instance. $\endgroup$