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

From: Date: Thu, 29 Jan 2009 01:26:07 +0000
Subject: note 79752 modified in function.stats-stat-percentile by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149925@lists.php.net to get a copy of this message
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 --was-- 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.. 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 http://php.net/manual/en/function.stats-stat-percentile.php

« previous php.notes (#149925) next »