©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PECL stats >= 1.0.0)
stats_stat_percentile — Not documented
$df
, float $xnonc
)本函数还未编写文档,仅有参数列表。
df
xnonc
[#1] aboulang2002 at yahoo dot com [2009-01-28 17:22:05]
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.]
[#2] yuvaraj dot v at gmail dot com [2007-12-11 04:37:44]
I have looked at the code available in the Math package for percentile. I compared the result obtained with percentile from Excel and found that it doesnt match. So i wrote my own percentile function and verified the result with the Excel's percentile.
For those of you who need the percentile calculation of Excel in php..
<?php
function mypercentile($data,$percentile){
if( 0 < $percentile && $percentile < 1 ) {
$p = $percentile;
}else if( 1 < $percentile && $percentile <= 100 ) {
$p = $percentile * .01;
}else {
return "";
}
$count = count($data);
$allindex = ($count-1)*$p;
$intvalindex = intval($allindex);
$floatval = $allindex - $intvalindex;
sort($data);
if(!is_float($floatval)){
$result = $data[$intvalindex];
}else {
if($count > $intvalindex+1)
$result = $floatval*($data[$intvalindex+1] - $data[$intvalindex]) + $data[$intvalindex];
else
$result = $data[$intvalindex];
}
return $result;
}
?>
The above code may not be elegant.. but it solves my problem..
yuvaraj