PHP两尾Z分数到P(概率)计算器

时间:2013-12-26 03:50:23

标签: php statistics formula

我一直在搜索,但却找不到任何相关内容。

我正在寻找一个能够使用双尾表将Z-Score转换为概率的函数,最好是在PHP中。 (像这一个:http://www.sjsu.edu/faculty/gerstman/StatPrimer/z-two-tails.pdf

我唯一的另一个选择是根据该表创建一个数组并比较Z-Score ..必须有更好的方法。

编辑:

下面是PHP.net统计功能页面上的一块代码。这些功能的记录很少。 以下函数将准确计算单尾z分数为概率。

function erf($x)
{
    $pi = 3.1415927;
    $a = (8*($pi - 3))/(3*$pi*(4 - $pi));
    $x2 = $x * $x;

    $ax2 = $a * $x2;
    $num = (4/$pi) + $ax2;
    $denom = 1 + $ax2;

    $inner = (-$x2)*$num/$denom;
    $erf2 = 1 - exp($inner);

    return sqrt($erf2);
}

function cdf($n)
{
    if($n < 0)
    {
            return (1 - erf($n / sqrt(2)))/2;
    }
    else
    {
            return (1 + erf($n / sqrt(2)))/2;
    }
}

1 个答案:

答案 0 :(得分:2)

找到解决方案:

function erf($x)
{
    $pi = 3.1415927;
    $a = (8*($pi - 3))/(3*$pi*(4 - $pi));
    $x2 = $x * $x;

    $ax2 = $a * $x2;
    $num = (4/$pi) + $ax2;
    $denom = 1 + $ax2;

    $inner = (-$x2)*$num/$denom;
    $erf2 = 1 - exp($inner);

    return sqrt($erf2);
}

function cdf($n)
{
         return (1 - erf($n / sqrt(2)))/2;
         //I removed the $n < 0 test which inverses the +1/-1
}

function cdf_2tail($n)
{
        return 2*cdf($n);
            //After a little more digging around, the two tail test is simply 2 x the cdf.
}

我测试了我的结果:http://vassarstats.net/tabs.html#z和z-score表。

0.1%是正确的

相关问题