How to generate a matrix with the same correlation coefficient between all variables?
Show older comments
How would I go about generating a 10x4 matrix, with values in each column between 1-10, that has equal correlation (0.5) between all variables?
i.e. if the matrix is X, I would like the corr(X) to look like this:
1.0 0.5 0.5 0.5
0.5 1.0 0.5 0.5
0.5 0.5 1.0 0.5
0.5 0.5 0.5 1.0
4 Comments
John D'Errico
on 20 Mar 2018
Are you asking for EXACTLY that correlation matrix? Or are you asking for a random set that would approach that correlation asymptotically as the number of rows gets large?
Do you really want to see uniformly generated numbers over that domain? Are you asking for integers from 1:10?
David Lindén
on 20 Mar 2018
John D'Errico
on 20 Mar 2018
Edited: John D'Errico
on 20 Mar 2018
You still have not said if your goal is to have a "randomly" generated set with that property. If they must be integers, etc. For example, is your goal something like this:
V
V =
10 5.0059
9.9696 3.3314
5.69 10
1 1
1.6416 1.2878
2.4309 1.6418
2.9086 1.8561
7.6598 3.9871
3.9759 2.3348
5.5832 3.0557
corrcoef(V)
ans =
1 0.5
0.5 1
So, there is a pair of vectors that appear fairly random, lie entirely in [1,10], and have exactly a correlation of 0.5. Were there reason to do so, I could show a method by which one would generate a larger set of vectors with an exact correlation matrix. But I won't bother to do so without knowing if this is your goal, since David has provided a solution that is entirely sufficient for what you have said so far.
David Lindén
on 20 Mar 2018
Accepted Answer
More Answers (1)
David Goodmanson
on 20 Mar 2018
Edited: David Goodmanson
on 20 Mar 2018
Hi David,
MODIFIED
the next thing that comes to mind is
a = [2*ones(1,4);-2*eye(4);zeros(5,4)] + 5;
a =
7 7 7 7
3 5 5 5
5 3 5 5
5 5 3 5
5 5 5 3
5 5 5 5
5 5 5 5
5 5 5 5
5 5 5 5
5 5 5 5
corrcoef(a)
ans =
1.0000 0.5000 0.5000 0.5000
0.5000 1.0000 0.5000 0.5000
0.5000 0.5000 1.0000 0.5000
0.5000 0.5000 0.5000 1.0000
or of course any similar scheme where the multiplicative 2 and added 5 are different. The simplest case along these lines is probably
a = [ones(1,4);-eye(4);zeros(5,4)] + 2;
5 Comments
David Lindén
on 20 Mar 2018
David Goodmanson
on 20 Mar 2018
I forgot about 1-10, see modified answer
John D'Errico
on 20 Mar 2018
Edited: John D'Errico
on 21 Mar 2018
+1. A cute solution to produce an integer array with the designated correlation behavior. I wonder if a similar constructive solution could be found if the correlation matrix were more complex?
C = [1 .4 .5 .6;.4 1 .7 .8;.5 .7 1 .9;.6 .8 .9 1];
I think it could be done, though I wonder if it might require more rows than 10 to get all the independent correlations correct.
David Lindén
on 21 Mar 2018
David Goodmanson
on 21 Mar 2018
Edited: David Goodmanson
on 21 Mar 2018
Hi David,
Yes, since integer values are not required, John's method is definitely the way to do this. I looked briefly at what can be done with integer values, and although it's possible to introduce some randomness, I haven't been able to do better than solutions that have the same integer appearing six times in each column (different integers between columns at least).
If you dropped the 1 < n < 10 restriction which I am sure you would have to do, it would be interesting to see how far you could get on a more complicated matrix with rational entries like John mentioned.
Categories
Find more on Descriptive Statistics 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!