3

I need a z-score to percentile calculator in PHP. Is there a simple formula suitable to PHP or an already written function. What should I use?

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
Surfer on the fall
  • 721
  • 1
  • 8
  • 34
  • 2
    Maybe this is what you want?: [z-Scores(standard deviation and mean) in PHP](http://stackoverflow.com/questions/5434648/z-scoresstandard-deviation-and-mean-in-php) – Joshua Lückers Jul 22 '12 at 19:26
  • @Truth This is inappropriate for M.SE - it's a PHP question, not a mathematics question. – Chris Taylor Jul 23 '12 at 09:26
  • 2
    @JoshuaLückers That question isn't helpful for this one. That's about how to calculate a z-score from a sample, whereas this is about how to compute percentiles from z-scores (i.e. how to implement the [Normal CDF](http://en.wikipedia.org/wiki/Normal_distribution#Numerical_approximations_for_the_normal_CDF)). – Chris Taylor Jul 23 '12 at 09:29
  • 1
    @ChrisTaylor Sorry about that, I misunderstood your question. – Joshua Lückers Jul 25 '12 at 09:39

1 Answers1

6

Simplest way is aboulang2002 at yahoo dot com's contribute on PHP manual:

<?
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;
    }
}

$zscore = MYZSCORE;
print 'Percentile: ' . cdf($zscore) * 100 . "\n";
?> 
Surfer on the fall
  • 721
  • 1
  • 8
  • 34