Generating correlated series where the correlation is pre-specified

7 views (last 30 days)
Hi, I'm having trouble generating series that have a pre-set correlation. My program is essentially trying to simulate returns in a market, where the first column is some index that while the other columns are share whose correlation we would like to find with the respect to the index. Each row is a return. I start off by using the users input for correlation to find the variance-covariance matrix, and then decompose this matrix using Cholesky Decomposition. I then gernate my returns using this decomposition. This all works fine. However, the problem comes in when I generate returns that are <-100%, as this is obviously not a feasible return. The last part of my code attempts to go back through all the returns and correct for the ones that are less than -100% while trying to maintain the correct correlation structure. Reomving the incorrect returns is easy enough, however I can't seem to do so without changing the correlation structure. Could anyone give me some advice on how to do this? I've pasted my code below which is quite liberally commented in case anything is unclear, but even so, let me know if there is anything else you need me to make clearer. Thank you so much!
function [Returns,Z,C]= cholesky(numberShares,numberReturns,varMatrixInput,inducedCorrInput,meanInput)
var_matrix = diag(varMatrixInput); %This takes the user input for variance and puts it on the diagonals of a %matrix. inducedCorr_matrix = inducedCorrInput;
A = randn(length(var_matrix),length(var_matrix)); %Creates a matrix that has a size determined by the number of shares. This %matrix is used to create the variance-covariance matrix. B = tril(A,-1); %This creates a lower triangular matrix (the function tril does this) %without the diagonals (the minus one does this). for i = 2:length(var_matrix) B(i,1) = inducedCorr_matrix(1,i)*sqrt(var_matrix(i,i))*sqrt(var_matrix(1,1)); end %This for loop takes B (which has random entries), and ensures that the %first column has covariances such that the correlation is equal to that %inputted by the user. It leaves the rest of the covariances unchanged such %that the covariance between shares is random. Thus, we have control over %the correlation between each share and the index, but not between %individual shares. var_cov_matrix = B+ B' + var_matrix; %This takes the lower triangular matrix B(which has zeros on the diagonal) %and adds the transpose so that it is a full matrix with zeros only on the %diagonal. Then we add the variance matrix which has the values of the %variance on the diagonal. So this results in a variance-covariance matrix %with entries in the first column such that the correlation between the %index and each share is as inputted by the user, the diagonals are the %variances inputted by the user, and the correlation between the shares is %random. D = eig(var_cov_matrix); for i = 1:numberShares if D(i,1) < 0 error('variance-covariance matrix must be positive definite'); end end
C = chol(var_cov_matrix); %This performs Cholesky Decomposition. Z = randn(numberReturns,numberShares); Returns = zeros(numberReturns,numberShares); for j = 1:numberShares for i = 1:numberReturns Returns(i,j) = Z(i,:)*C(:,j)+ meanInput;
while Returns(i,1) < -1 || Returns(i,2) <-1 || Returns(i,3) <-1 || Returns(i,4) <-1 || Returns(i,5) <-1 || Returns(i,6) <-1
Z (i,:) = randn (1,numberShares);
for k = 1:j
%Returns(i,1) = Z(i,:)*C(:,1) + meanInput;
Returns(i,k) = Z(i,:)*C(:,k) + meanInput;
end
end
end
end

Answers (1)

Michael
Michael on 27 Jun 2011
oh man, just realise how badly the code came out, let me post it again with less commenting:
function [Returns,Z,C]= cholesky(numberShares,numberReturns,varMatrixInput,inducedCorrInput,meanInput)
var_matrix = diag(varMatrixInput); %creates a diagonal matrix of variances
A = randn(length(var_matrix),length(var_matrix));
B = tril(A,-1);
for i = 2:length(var_matrix) B(i,1) = inducedCorr_matrix(1,i)*sqrt(var_matrix(i,i))*sqrt(var_matrix(1,1)); end %This ensures that the correlation for eachs hare with the index (the first column) is as specified by the user.
var_cov_matrix = B+ B' + var_matrix; %Creates the covariance matrix using B.
C = chol(var_cov_matrix); %This performs Cholesky Decomposition. Z = randn(numberReturns,numberShares); Returns = zeros(numberReturns,numberShares); for j = 1:numberShares for i = 1:numberReturns Returns(i,j) = Z(i,:)*C(:,j)+ meanInput;
while Returns(i,1) < -1 || Returns(i,2) <-1 || Returns(i,3) <-1 || Returns(i,4) <-1 || Returns(i,5) <-1 || Returns(i,6) <-1
Z (i,:) = randn (1,numberShares);
for k = 1:j
%Returns(i,1) = Z(i,:)*C(:,1) + meanInput;
Returns(i,k) = Z(i,:)*C(:,k) + meanInput;
end
end
end
end
%This code is meant to correct for the -100% returns, but it doen't and so this is the bit where I really need help.
  2 Comments
Sean de Wolski
Sean de Wolski on 27 Jun 2011
Just edit the original question and add a double space before each new paragraph of code.
Sean de Wolski
Sean de Wolski on 27 Jun 2011
Also, perhaps you could show a small example - three lines one with a negative return; the operation and then the expected output for that small example. It's typically easier to follow than a large block of code

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!