9
$\begingroup$

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

$\endgroup$
14
$\begingroup$
ClearAll[dist]
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

$\endgroup$
  • $\begingroup$ +1 nice. Checks with Correlation @@ Transpose[data1] $\endgroup$ – george2079 Mar 11 '15 at 14:22
4
$\begingroup$
f[r_?NumberQ,n_Integer]:={First[#],#.{r,Sqrt[1-r^2]}}&/@RandomReal[NormalDistribution[0,1],{n,2}];

Produces n pairs of numbers with the correlation r.

$\endgroup$
1
$\begingroup$

Brute Force:

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

0.900731

 {a, b} // MatrixForm

enter image description here

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

$\endgroup$

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.