For an educational website, my goal is to allow students to fool themselves with several sets of values ββand their college. For example, students can enter two arrays for which correlation is calculated:
$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);
The code for this works fine and can be found at the bottom of this post. However, I ran into a problem. I want the following:
- student enters $ array_x (5,3,6,7,4,2,9,5)
- student introduces correlation (0.9)
- the student enters the boundaries of $ array_y (for example, between 1 and 10 or between 50 and 80).
- script returns a random array (e.g.: 4,3,4,8,3,2,10,5) that has (about) a given correlation
So, in other words, the code should work as follows:
$array_x = array(5,3,6,7,4,2,9,5); $boundaries = array(1, 10); $correlation = 0.9; echo ySeries($array_x, $boundaries, $correlation);
On the Stackexchange Math forum, @ilya replied (inserted as an image since formatting latex fomulas does not work on stackoverflow):

PS Code used to calculate correlation:
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 < $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 < $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 < $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); }