I start with 4 time series, labelled A, B, C, D.
I generate the following:
- A 4×1 matrix of means.
- A 4×1 matrix of standard deviations.
- A 4×4 correlation matrix, by taking 30 samples from each time series.
What is the Matlab code to generate a 4×1 matrix of random variables, keeping the correlations between the time series intact?
(reason: this is the first stage in a Monte Carlo simulation).
You need just the means vector (call it
m) and the covariance matrix (call itC). Note that you can get the covariance matrix from the correlation using the equationC = R - m*m'(or just compute it directly by computing the correlation of the sequences after subtracting their mean).Then, to get a vector with the covariance C, you generate an IID random vector (say gaussian):
Then multiply it with the square root of the covariance matrix (call it Q) and add the mean:
You can either use Matlab’s sqrt function to compute sqrt(C) or compute it using the SVD or EIG.
The covariance of
vwill beQ*Q'(=C) and the mean will bemSee the wikipedia article for the properties of the covariance matrix.