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?
Asked
Active
Viewed 3,461 times
3
-
2Maybe 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 Answers
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
-
-
just a note that it is from the PHP manual stats_stat_percentile function. Link is here: http://php.net/manual/en/function.stats-stat-percentile.php – Michael Shang Jun 01 '16 at 09:04
-
2Does anyone have any idea what the function names `cdf` and `erf` represent? – Oscar Chambers Dec 15 '20 at 13:54