Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function upper_bound ($a)
- {
- //Use this many standard deviations as upper bound
- $max_std_devs = 3;
- $max_val = 0;
- $removed_items = 1;
- while ($removed_items && count($a) > 0) { // Continue as long as 'outliers' are found
- $removed_items = 0;
- $the_variance = 0.0;
- $the_mean = 0.0;
- $the_array_sum = array_sum($a); //sum the elements
- $number_elements = count($a); //count the number of elements
- //calculate the mean
- $the_mean = $the_array_sum / $number_elements;
- //calculate the variance
- for ($i = 0; $i < $number_elements; $i++)
- {
- //sum the array
- $the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
- }
- $the_variance = $the_variance / $number_elements;
- $max_val = $the_mean + ($max_std_devs * sqrt($the_variance));
- foreach($a as $key => $value) {
- if ($value > $max_val) {
- unset($a[$key]);
- $removed_items = 1;
- }
- }
- $array = array_values($a);
- }
- return ceil($max_val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement