note 88558 modified in function.stats-stat-percentile by danbrown

From: Date: Wed, 24 Feb 2010 16:09:12 +0000
Subject: note 88558 modified in function.stats-stat-percentile by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166320@lists.php.net to get a copy of this message
If you are looking to infer the percentile from a z-score, you can use this function. It's far from precise but does the job on most circumstances. The error function ( erf() )is based on the approximation on wikipedia: http://en.wikipedia.org/wiki/Error_function <?php 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; } } // EXAMPLE $sample = 90; $avg = 75; $stddev = 12; $zscore = ($sample - $avg) / $stddev; print 'Percentile: ' . cdf($zscore) * 100 . "\n"; ?> Where $n is the z-score The function cdf() returns the approximed cumulative standard normal distribution ([0..1]) [EDIT BY danbrown AT php DOT net: Contains a bugfix provided by (Ed) on 24-FEB-2010 which fixes the definition of $a in erf() with the note that it is "out by a factor of -1" in the original code.] --was-- If you are looking to infer the percentile from a z-score, you can use this function. It's far from precise but does the job on most circumstances. The error function ( erf() )is based on the approximation on wikipedia: http://en.wikipedia.org/wiki/Error_function <?php function erf($x) { $pi = 3.1415927; $a = (8*($pi - 3))/(3*$pi*($pi - 4)); $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; } } // EXAMPLE $sample = 90; $avg = 75; $stddev = 12; $zscore = ($sample - $avg) / $stddev; print 'Percentile: ' . cdf($zscore) * 100 . "\n"; ?> Where $n is the z-score The function cdf() returns the approximed cumulative standard normal distribution ([0..1]) http://php.net/manual/en/function.stats-stat-percentile.php

« previous php.notes (#166320) next »