2

假设我有这个 JSON 数组

$json = '[["1258765200","12350"],["1259370000","13000"],["1259974800","11840"],["1260579600","16359"],["1261184400","14230"],["1261789200","07406"],["1262394000","12846"],["1262998800","11204"],["1263603600","10234"]]';

其中第一个值是时间戳,第二个是值。我如何计算每个值是否超出 PHP 中位数的 1 个标准差(95%)

4

2 回答 2

2

不确定这是否是您想要的

$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%)";
}

输出

outside 1 standard deviation (95%)

职能

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;
}
于 2012-04-24T23:53:26.187 回答
0

爸爸打败了我,但这是我用来计算标准偏差的算法。最初,该Statistics模块看起来很有前途,但它似乎不是 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);
}

要获得 > 95% 的阈值,请取中位数 + 2 倍标准差。任何大于阈值的值都大于样本大小的 95%。

于 2012-04-24T23:57:26.527 回答