PHP 8.4.1 Released!

stats_stat_percentile

(PECL stats >= 1.0.0)

stats_stat_percentileReturns the percentile value

Descrição

stats_stat_percentile(array $arr, float $perc): float

Returns the perc-th percentile value of the array arr.

Parâmetros

arr

The input array

perc

The percentile

Valor Retornado

Returns the percentile values of the input array.

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
8
aboulang2002 at yahoo dot com
15 years ago
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.]
up
4
yuvaraj dot v at gmail dot com
16 years ago
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
To Top