Ok let's assume that you have your data points in an array like this:
<?php $dataset = array(20.50, 80.30, 70.95, 15.25, 99.97, 85.56, 69.77); ?>
Then you can use the following function (see comments for what happens) to remove all numbers that go beyond the average +/- standard deviation time from the value you set (default is 1):
<?php function remove_outliers($dataset, $magnitude = 1) { $count = count($dataset); $mean = array_sum($dataset) / $count; // Calculate the mean $deviation = sqrt(array_sum(array_map("sd_square", $dataset, array_fill(0, $count, $mean))) / $count) * $magnitude; // Calculate standard deviation and times by magnitude return array_filter($dataset, function($x) use ($mean, $deviation) { return ($x <= $mean + $deviation && $x >= $mean - $deviation); }); // Return filtered array of values that lie within $mean +- $deviation. } function sd_square($x, $mean) { return pow($x - $mean, 2); } ?>
For your example, this function returns the following with a value of 1:
Array ( [1] => 80.3 [2] => 70.95 [5] => 85.56 [6] => 69.77 )
George Reith
source share