Generating multivariate normal random numbers without statistics and machine learning toolbox

How to Generate Multivariate Normal Random Numbers in Matlab without Statistics and Machine Learning Toolbox

Generating Additive White Gaussian Noise (AWGN) is an indispensable for simulating communication systems. In some cases, we need to produce sequences from a multivariate normal distribution.

The standard way to generate such random sequences in Matlab is to use the function mvnrnd( ). But we need the Statistics and Machine Learning Toolbox for this purpose. However, there is another way of producing multivariate normal random numbers in Matlab without this toolbox.

One alternative is to use the randn( ) function along with the Cholesky decomposition of the covariance matrix. The main idea is that when generating multivariate normal random numbers, we need to ensure the numbers have the correct mean and covariance structure as follows.

  • Standard Normal Random Numbers: The function randn(N, d) generates $N$ samples of $d$-dimensional standard normal random numbers (zero mean and identity covariance).
  • Transforming to Desired Covariance: The Cholesky decomposition of the covariance matrix (Sigma) provides a lower triangular matrix L such that: $\Sigma = L L^T$. Multiplying standard normal samples by $L$ ensures they follow the required covariance structure.
  • Applying Mean Shift: Since randn generates zero-mean data, adding the desired mean ($\mu$) shifts the distribution appropriately.

The following code produces a scatter plot where each point represents a sample from the multivariate normal distribution.


% Set parameters
mu = [1 5]; % Mean vector
Sigma = [1 1.5; 1.5 3]; % Covariance matrix
N = 200; % Number of samples

% Generate multivariate normal random samples
X = randn(N, length(mu)); % Standard normal samples
L = chol(Sigma, 'lower'); % Cholesky decomposition
R = X * L + mu; % Transform to desired distribution

% Plot the samples
figure;
scatter(R(:,1), R(:,2), 50, 'b', 'filled');
hold on;
plot(mu(1), mu(2), 'rx', 'MarkerSize', 12, 'LineWidth', 2); % Mean point
xlabel('X1');
ylabel('X2');
title('Multivariate Normal Distribution');
grid on;
yticks(2:1:8)

The output is plotted in the main figure above where the red “x” marks the mean of the distribution.

There are other techniques of achieving the same result in Matlab without Statistics and Machine Learning Toolbox. However, the Cholesky decomposition method demonstrates greater precision and speed compared to the other approaches.

Leave a Reply; You can use HTML (<>) or Latex ($$)

Your email address will not be published. Required fields are marked *