Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I want to generate a Gaussian dataset. The dataset includes a total of 800 samples drawn randomly from four two-dimensional Gaussian classes with following distribution:

How can I do that in MATLAB? I'm not expert in MATLAB.

share|improve this question
    
Rather than "800 samples", one should speak of "800 observations", which might make up four samples, each containing 200 observations. –  Michael Hardy Apr 9 '12 at 21:49
    
I disagree. Speaking of samples is perfectly natural and extremely common in the literature related to drawing values from a random variable. See most introductory texts on Metropolis-Hastings, Acceptance-Rejection, Gibbs sampling, etc., and the language is usually phrased in terms of "drawing samples" where 'sample' does not refer to the entire population of observations. If these data were coming from an experiment, rather than a simulation, I might be more inclined to agree, though even still it would be a mostly pedantic distinction that you won't see in the literature. –  prpl.mnky.dshwshr Apr 9 '12 at 22:46

2 Answers 2

up vote 2 down vote accepted

This question is more appropriate for Stack Overflow, but that's OK. It still is a mathematics question, after all.

The right way to do this in Matlab is to use the mvnrnd() function. It accepts a vector of the coordinate means and a covariance matrix, and can return the results into an array of any shape that you'd like. In your care, you'd do the following:

mi = -3;                         % Or the other values you want to use.
mu = [mi 0];                     % The mean vector.
cov_mat = [0.5 0.05; 0.05 0.5];  % The covariance matrix.

num_samples = 800;               % The number of samples you want.

% Generate the draws.
generated_data = mvnrnd(mu, cov_mat, num_samples);

Now, the array generated_data will be an 800-by-2 matrix, where each row is a random draw from the distribution. See this link for more details.

Note that this claims to be part of the Matlab Statistics Toolbox. This should be a standard part of most Matlab licenses at an academic or professional institution. However, if you're using a personal copy of Matlab (such as the student version), you may not have access. In that case, there are several basic implementations of the same function available at the Matlab Central File Exchange, which you can download and use.

If that still doesn't work, let me know in a comment and I can go over the algorithm for actually drawing from a multivariate Gaussian based only on the inverse-CDF method and uniform draws.

Alternatively, much of the same functionality is provided in SciPy/NumPy for Python. It's free and is a good alternative to learn given that not much practical mathematical software is ever developed in Matlab.

Added:

As you mentioned not having the Statistics Toolbox, and then downloading the linked mvg() function from the file exchange, here is the code that would work with that function:

mi = -3;                        % Or the other values you want to use.
mu = [mi 0];                    % The mean vector.
cov_mat = [0.5 0.05; 0.05 0.5]  % The covariance matrix.

num_samples = 800;              % The number of samples you want.

% Generate the draws.
generated_data = mvg(mu, cov_mat, num_samples)

This time, generated_data will be a 2-by-800 array, so each column will be a random sample, instead of the rows as listed above.

You may want to try this for a smaller number of samples, like 10 or 15, and then just print out the result by typing:

generated_data

without a semicolon, to see what the output array looks like.

share|improve this answer
    
Thanks a lot. I got the following error: "??? Undefined function or method 'mvnrand' for input arguments of type 'double'." –  PHPst Apr 9 '12 at 19:15
    
This is probably because you don't have the Statistics Toolbox. As an alternative, you can download the function from this link and put it into your working directory. Then you can use that function in a basically similar way as above. –  prpl.mnky.dshwshr Apr 9 '12 at 19:19
    
I downloaded "mvg.m" from mentioned link and added it to working directory. But the file define mvg function and "??? Undefined function or variable 'mvnrand'." –  PHPst Apr 9 '12 at 19:56
    
You've got to try a little harder. Obviously if you're downloading a new function named mvg, then using mvnrand isn't sensible. It should be clear that downloading a file won't cause mvnrand() to work. You'll need to modify the code above to make mvg work. I will add some code to help. –  prpl.mnky.dshwshr Apr 9 '12 at 19:58
    
"Number of samples" is an incorrect usage. "Number of observations in the sample" or "size of the sample" or "sample size" is correct. –  Michael Hardy Apr 9 '12 at 22:15

The finite-dimensional version of the spectral theorem implies that a symmetric nonnegative-definite matrix $\Sigma$ with real entries as a symmetric nonnegative-definite square root $\Sigma^{1/2}$. Say the variance (or "covariance matrix", if you like) is a $2\times2$ matrix $$ \Sigma = \begin{bmatrix} \sigma^2 & \rho\sigma\tau \\ \rho\sigma\tau & \tau^2 \end{bmatrix} = \begin{bmatrix} \sigma & 0 \\ 0 & \tau \end{bmatrix}\begin{bmatrix} 1 & \rho \\ \rho & 1 \end{bmatrix}\begin{bmatrix} \sigma & 0 \\ 0 & \tau \end{bmatrix}.\tag{1} $$ If $U$, $V$ are independent standard $1$-dimensional Gaussian random variables (thus the variance of the random vector $\begin{bmatrix} U \\ V \end{bmatrix}$ is the $2\times2$ identity matrix), then $$ \Sigma^{1/2} \begin{bmatrix} U \\ V \end{bmatrix} $$ has variance $\Sigma$. (It also has expected value $\begin{bmatrix} 0 \\ 0 \end{bmatrix}$, and you can just add whatever vector you want as the expected value to the random vector that has the right variance and expected value $\begin{bmatrix} 0 \\ 0 \end{bmatrix}$.

A sample of $200$ from this $2$-dimensional Gaussian distribution will on average have a sample mean and a sample variance equal to this given population mean and the given population variance respectively, but with probability $1$, the sample mean and sample variance will differ from those. But with similar methods, one can get the sample mean and sample variance exactly equal to prescribed values. The part of doing that that I haven't yet described here is how to get the sample correlation to be exactly $0$. The way to do that is to regress the vector of $y$-values in the sample on the vector of $x$-values, then replace the $y$-values with the observed residuals.

Finding the square root of the middle matrix in $(1)$ can be done as follows: write it as $$ \alpha\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} + \beta \begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix} $$ then use the fact that these latter two matrices are symmetric and idempotent.

I don't know the matlab commands, though.

share|improve this answer
    
Note that this is precisely what the mvg() and mvnrand() functions do, and can be stated more simply by appealing to the Cholesky decomposition of the covariance. I don't think the question has any relevance to population means, after all the poster says the need is to draw actual random samples. –  prpl.mnky.dshwshr Apr 9 '12 at 22:19
    
Population means are relevant to the question, because the question explicitly says there are to be four samples, each with a prescribed population mean. –  Michael Hardy Apr 10 '12 at 0:07
    
That's misleading. I'm not familiar with anyone in the applied statistics literature who would refer to the means of generated basic random variables as population means. Unless those parameters were estimated from data, I think this would be extremely uncommon. 'Population' is almost always used to refer to the superset of individuals from which your observations have come, and it is not used to refer to random values that you have yourself generated. Of course there will be occasional places where this convention doesn't apply, but using 'population mean' here is not useful. –  prpl.mnky.dshwshr Apr 10 '12 at 0:25
    
I was NOT using it to refer to the mean of the set of observations, but rather to a superset, i.e. to the expected values. I suggest you are confused. –  Michael Hardy Apr 10 '12 at 3:02
    
I disagree. The superset that I'm referring to is the idealized population that something is drawn from. In this case, the specified normal distributions would not qualify as a population description. A population description is something like saying that my observations of wage data come from an infinite population of American citizens, and then I will place an assumed distribution over the observations, after specifying the larger population. –  prpl.mnky.dshwshr Apr 10 '12 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.