Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, here's the php implementation of your algorithm that uses Dawkins' weasel to reduce the error gradually until the desired threshold.</p> <pre><code>&lt;?php function sqrMeanDeviation($array, $avg) { $sqrMeanDeviation = 0; for($i=0; $i&lt;count($array); $i++) { $dev = $array[$i] - $avg; $sqrMeanDeviation += $dev * $dev; } return $sqrMeanDeviation; } // z values are non-0 an can value between [-abs_z_bound, abs_z_bound] function random_z_element($abs_z_bound = 1) { $a = (mt_rand() % (2*$abs_z_bound) ) - ($abs_z_bound-1); if($a &lt;= 0) $a--; return $a; } // change z a little function copy_z_weasel($old_array_z, $error_probability = 20 /*error possible is 1 in error_probability*/, $abs_z_bound = 1) { $new_z = array(); for($i = 0; $i &lt; count($old_array_z); $i++) if(mt_rand() % $error_probability == 0 ) $new_z[$i] = random_z_element($abs_z_bound); else $new_z[$i] = $old_array_z[$i]; return $new_z; } function correlation_error($array_y, $array_x, $avg_x, $sqrMeanDeviation_x, $correlation) { // checking correlation $avg_y = array_sum($array_y)/count($array_y); $sqrMeanDeviation_y = 0; $covariance_xy = 0; for($i=0; $i&lt;count($array_x); $i++) { $dev_y = $array_y[$i] - $avg_y; $sqrMeanDeviation_y += $dev_y * $dev_y; $dev_x = $array_x[$i] - $avg_x; $covariance_xy += $dev_y * $dev_x; } $correlation_xy = $covariance_xy/sqrt($sqrMeanDeviation_x*$sqrMeanDeviation_y); return abs($correlation_xy - $correlation); } function ySeries($array_x, $low_bound, $high_bound, $correlation, $threshold) { $array_y = array(); $avg_x = array_sum($array_x)/count($array_x); $sqrMeanDeviation_x = sqrMeanDeviation($array_x, $avg_x); // pre-compute beta $beta_x_sQMz = $sqrMeanDeviation_x * sqrt( 1 / ($correlation*$correlation) - 1 ); $best_array_z = array(); $n = 0; $error = $threshold + 1; while($error &gt; $threshold) { ++$n; // generate z $array_z = array(); if(count($best_array_z) == 0) for($i=0; $i&lt;count($array_x); $i++) $array_z[$i] = random_z_element(); else $array_z = copy_z_weasel($best_array_z); $sqm_z = sqrMeanDeviation($array_z, array_sum($array_z)/count($array_z) ); // this being 0 implies that for every beta correlation(x,y) = 1 so just give it any random beta if($sqm_z) $beta = $beta_x_sQMz / $sqm_z; else $beta = 10; // and now we have y for($i=0; $i&lt;count($array_x); $i++) $array_y[$i] = $array_x[$i] + ($array_z[$i] * $beta); // now, change bounds (we could do this afterwards but we want precision and y to be integers) // rounding $min_y = $array_y[0]; $max_y = $array_y[0]; for( $i=1; $i&lt;count($array_x); $i++ ) { if($array_y[$i] &lt; $min_y) $min_y = $array_y[$i]; if($array_y[$i] &gt; $max_y) $max_y = $array_y[$i]; } $range = ($high_bound - $low_bound) / ($max_y - $min_y); $shift = $low_bound - $min_y; for( $i=0; $i&lt;count($array_x); $i++ ) $array_y[$i] = round($array_y[$i] * $range + $shift); // get the error $new_error = correlation_error($array_y, $array_x, $avg_x, $sqrMeanDeviation_x, $correlation); if($new_error &lt; $error) { $best_array_z = $array_z; $error = $new_error; } } echo "Correlation ", $correlation, " approched within " , $new_error, " in ", $n ," iterations.\n"; return $array_y; } ?&gt; </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