Reputation: 76880
Let's say i have this JSON array
$json = '[["1258765200","12350"],["1259370000","13000"],["1259974800","11840"],["1260579600","16359"],["1261184400","14230"],["1261789200","07406"],["1262394000","12846"],["1262998800","11204"],["1263603600","10234"]]';
Where the first value is a timestamp and the second is the value. how do i calculate if each value is outside 1 standard deviation (95%) of the median in PHP
Upvotes: 2
Views: 1949
Reputation: 5701
Baba beat me to the punch, but this was the algorithm that I used for computing the standard deviation. Originally, the Statistics
module seemed promising, but it doesn't seem to come standard with PHP.
function php_standard_dev($data)
{
$n = 0;
$sum = 0;
$sum_squared = 0;
foreach ($data as $x)
{
$n += 1;
$sum += $x;
$sum_squared += $x*$x;
}
$mean = $sum / $n;
$variance = ($sum_squared - $sum * $mean)/($n - 1);
return sqrt($variance);
}
To get > 95% threshold, take the median + 2 times the standard deviation. Any values greater than the threshold is larger than 95% of the sample size.
Upvotes: 0
Reputation: 95121
Not sure if this is what you want
$json = '[["1258765200","12350"],["1259370000","13000"],["1259974800","11840"],["1260579600","16359"],["1261184400","14230"],["1261789200","07406"],["1262394000","12846"],["1262998800","11204"],["1263603600","10234"]]';
$json = json_decode ( $json, true );
$values = array ();
foreach ( $json as $value ) {
$values [] = $value [1]; // Get Values
}
$median = median ( $values );
$sd = stddev ( $values );
$percentage = ($sd / $median) * 100;
$benchmark = 95 / 100;
if($percentage > $benchmark)
{
echo "outside 1 standard deviation (95%)";
}
Output
outside 1 standard deviation (95%)
Functions
function stddev($array) {
$n = 0;
$mean = 0;
$M2 = 0;
foreach ( $array as $x ) {
$n ++;
$delta = $x - $mean;
$mean = $mean + $delta / $n;
$M2 = $M2 + $delta * ($x - $mean);
}
$variance = $M2 / ($n - 1);
return sqrt ( $variance );
}
function median($arr) {
sort($arr);
$count = count($arr); //total numbers in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
Upvotes: 2