How to generate random numbers correlated to a given dataset in matlab

37 views (last 30 days)
I have a matrix x with 10,000 rows and 20 columns. I want to generate another new matrix of random numbers , y, where y is correlated to x with correlation coefficient q.
Note that the matrix x is not normally distributed - it has the power law distribution.

Answers (2)

John D'Errico
John D'Errico on 28 Jul 2015
Edited: John D'Errico on 29 Jul 2015
Ah. Every once in a while, I see a question come up that is interesting. In this case, it should not be difficult to do. In fact, I can see at least one solution, and maybe a second way to do so. (I'll post an answer later today. Must run out now. Sorry, but at least you can know to expect an answer if nobody else gives you one.)
A question first though. Since the mean of a variable has no impact on the correlation, do you care what the mean of y will be? Or can it simply have mean 0?
Next, I assume you mean the traditional correlation coefficient, thus the Pearson version?
Hmm, as I think, a more interesting question is, given a set of n variables, with their own set of inter-correlations, is can we choose a new variable that has a given set of n specified correlations with each of those n variables? And I think the answer is yes, of course we can do so, as long as we have sufficient degrees of freedom.
Before I go for now though, here is a fun paper on the subject.
Later... (m-file solution attached to this answer.)
The basic idea is for a variable x, find a new vector y0, such that y0 is orthogonal to x. Then choose some linear combination of x and y0 that has the desired correlation.
  7 Comments
Aditya Nanda
Aditya Nanda on 17 Jan 2019
Thanks for the great answer, John.
I am working on the exact problem you mention in your answer.
Hmm, as I think, a more interesting question is, given a set of n variables, with their own set of inter-correlations, is can we choose a new variable that has a given set of n specified correlations with each of those n variables?
Can this be done? Please help! The correlation is the cosine of angle between two vectors (length is n). Can we construct a new vector that has specified correlations to existing vectors . All the vectors $x_1, x_2 \,\, \mathrm{etc. } $ are n-dimensional vectors and n>m
Dian Fan
Dian Fan on 21 Apr 2020
John, the file only works for rho=1 or -1? It is strange that a non-unity rho gives errors.

Sign in to comment.


Josué Ortega
Josué Ortega on 14 Sep 2017
Edited: Josué Ortega on 14 Sep 2017
This is a great answer @John D'Errico, but I have a further question. Suppose I have a vector 2 1 3 5 4 ... 10 of 10 numbers, from 1 to 10, ordered randomly, could be generated by
x=randperm(10).'
Now I want to generate another vector of 10 numbers, containing all numbers from 1 to 10 again, that has a correlation of at least p with my previous vector x. Any ideas? Your code works but of course produces real numbers between -1 and 1.

Categories

Find more on Creating and Concatenating 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!