I would like to simulate some random data e.g. between 0 and 1, but with a given correlation. In other words some data where the correlation between two datasets is e.g. a constant or within a give range of correlations for example a minimum of 0.2 and a maximum of 0.8.

Any tips how to use e.g. RandomReal or another function to create such data?

thanks in advance

dist [rho_] := CopulaDistribution[{"Binormal", rho}, {UniformDistribution[{0, 1}], 
    UniformDistribution[{0, 1}]}];

data1 = RandomVariate[dist[-.9], 5000];
ListPlot[data1, Frame -> True, AspectRatio -> 1]

enter image description here

data2 = RandomVariate[dist[.6], 5000];
ListPlot[data2, Frame -> True, AspectRatio -> 1]

enter image description here

  • $\begingroup$ +1 nice. Checks with Correlation @@ Transpose[data1] $\endgroup$ – george2079 Mar 11 '15 at 14:22

Produces n pairs of numbers with the correlation r.


Brute Force:

 While[! ( .9 < 
      Correlation[ a = RandomReal[{0, 1}, 10], b = RandomReal[{0, 1}, 10  ]] < .91 ) ]
 Correlation[ a, b]


 {a, b} // MatrixForm

enter image description here

I expect this breaks down in a hurry for larger sets.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.