Last active
August 29, 2015 14:08
-
-
Save richjenks/e417c09f025159bb85f7 to your computer and use it in GitHub Desktop.
Attempts to simulate the addition or removal of a value from a mean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* modify_mean | |
* | |
* Add or subtract a value from a mean | |
* As if the original mean were calculate with/without it | |
* | |
* @param int $mean The current mean | |
* @param int $points The number of data points | |
* @param string $modifier The value to be added or subtracted, e.g. "-10" | |
* | |
* @return array Keys 'mean' and 'points' contain the resulting mean and number of points | |
*/ | |
function modify_mean($mean, $points, $modifier) { | |
// Calculate total of values | |
$total = $mean * $points; | |
// Add or subtract? | |
if (substr($modifier, 0, 1) === '-') { | |
// Subtract | |
$point = substr($modifier, 1); | |
$points--; | |
$total = $total - $point; | |
$mean = $total / $points; | |
return array( | |
'mean' => $mean, | |
'points' => $points, | |
); | |
} else { | |
// Add | |
$point = substr($modifier, 1); | |
$points++; | |
$total = $total + $point; | |
$mean = $total / $points; | |
return array( | |
'mean' => $mean, | |
'points' => $points, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment