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

From: Date: Thu, 29 Jan 2009 01:26:37 +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-149926@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*($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]) --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 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 (#149926) next »