Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to calculate a random series of values (B) that have a given correlation with a given series (A)
    text
    copied!<p>For an educational website, it is my purpose to let students fool around somewhat with value series and their collelation. For instance, students can enter two arrays for which the correlation is calculated:</p> <pre><code>$array_x = array(5,3,6,7,4,2,9,5); $array_y = array(4,3,4,8,3,2,10,5); echo Correlation($array_x, $array_y); // 0.93439982209434 </code></pre> <p>The code for this works perfectly and can be found at the bottom of this post. I'm however now facing a challenge. What I want is the following:</p> <ul> <li>student inputs a $array_x (5,3,6,7,4,2,9,5)</li> <li>student inputs a correlation (0.9)</li> <li>student inputs the boundaries of $array_y (for instance, between 1 and 10 or between 50 and 80)</li> <li>the script returns a random array (for instance: 4,3,4,8,3,2,10,5) which has (about) the given correlation</li> </ul> <p>So, in other words, the code would have to work like:</p> <pre><code>$array_x = array(5,3,6,7,4,2,9,5); $boundaries = array(1, 10); $correlation = 0.9; echo ySeries($array_x, $boundaries, $correlation); // array(4,3,4,8,3,2,10,5) </code></pre> <p>At the Stackexchange Math forum, @ilya answered (inserted as an image, since Latex formatting of fomulas don't seem to work on stackoverflow):</p> <p><img src="https://i.stack.imgur.com/Q3krG.png" alt="enter image description here"></p> <p>P.S. The code used to calculate the correlation:</p> <pre><code>function Correlation($arr1, $arr2) { $correlation = 0; $k = SumProductMeanDeviation($arr1, $arr2); $ssmd1 = SumSquareMeanDeviation($arr1); $ssmd2 = SumSquareMeanDeviation($arr2); $product = $ssmd1 * $ssmd2; $res = sqrt($product); $correlation = $k / $res; return $correlation; } function SumProductMeanDeviation($arr1, $arr2) { $sum = 0; $num = count($arr1); for($i=0; $i &lt; $num; $i++) { $sum = $sum + ProductMeanDeviation($arr1, $arr2, $i); } return $sum; } function ProductMeanDeviation($arr1, $arr2, $item) { return (MeanDeviation($arr1, $item) * MeanDeviation($arr2, $item)); } function SumSquareMeanDeviation($arr) { $sum = 0; $num = count($arr); for($i = 0; $i &lt; $num; $i++) { $sum = $sum + SquareMeanDeviation($arr, $i); } return $sum; } function SquareMeanDeviation($arr, $item) { return MeanDeviation($arr, $item) * MeanDeviation($arr, $item); } function SumMeanDeviation($arr) { $sum = 0; $num = count($arr); for($i = 0; $i &lt; $num; $i++) { $sum = $sum + MeanDeviation($arr, $i); } return $sum; } function MeanDeviation($arr, $item) { $average = Average($arr); return $arr[$item] - $average; } function Average($arr) { $sum = Sum($arr); $num = count($arr); return $sum/$num; } function Sum($arr) { return array_sum($arr); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload