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)
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COWhat is interesting about this problem is that you are looking for a way to provide students with a tool that, if I'm reading the question right, does not have a pre-defined mathematical solution (which is to say, there isn't an equation or function some Greek or French guy came up with 500+ years ago). While the goal of wanting to pull this off is noble (and I'm going to think on it), you are basically providing a tool/function that the student couldn't actually achieve on their on (in other words, they can't plug in the numbers to confirm having already made a list of their own to confirm).
      singulars
    2. COIf I understand your comment correctly, the purpose of this tool would be to, for instance, demonstrate that if series A is between 300 and 500, it might correlate with a series B whether the boundaries of B are 1 to 10 or much higher, say 700 to 1000. I studied economics myself, and much calculations were best understood by having tools that let you fiddle with some properties to see the result on the other. That would be the purpose of this tool, rather than to provide proof to some Greek or Frenchs guys theorem :-)
      singulars
    3. COI definitely understand the benefit of getting to play with a theorem from all angles. It definitely has helped me grasp advanced geometry (re: basic trig) better, amongst other models/systems. I just wonder if your question isn't so much a programming issue as it is a mathematical one. I have one a few occasions realized that about dilemmas of my own, and found that asking at [math](math.stackexchange.com) helped me build a better foundation to build the code on top of. Like I said, I think the question is valid and the goal is noble, but the answer is probably more fundamental than coding.
      singulars
 

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