Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.9.1

Bug #12465 Median Formula is Incorrect
Submitted: 2007-11-17 05:15 UTC
From: breck Assigned: jmcastagnetto
Status: Closed Package: Math_Stats (version 0.9.0beta3)
PHP Version: Irrelevant OS: Irrelevant
Roadmaps: (Not assigned)    
Subscription  


 [2007-11-17 05:15 UTC] breck (Breck Yunits)
Description: ------------ The median function returns a value one too high. The median is the middle value in the sorted array with length n. Stats uses n/2 to find the halfway point and then returns array[n/2]. The problem is if you use n/2 as the halfway point you are forgetting about array[0]. So the actual halfway point is array[(n/2)-1]. Test script: --------------- <?php /** * @author Breck Yunits */ include_once 'Math/Stats.php'; $s = new Math_Stats(); $s->setData(array(1,2,3,4,5,6,7,8,9,10,11)); if ($s->median() != 6) {echo "7 is too many";} else {echo "6 is okay!";} ?> Expected result: ---------------- The median should be 6 Actual result: -------------- The median is 7

Comments

 [2007-11-19 06:57 UTC] jmcastagnetto (Jesus M. Castagnetto)
Thank you for your bug report. This issue has been fixed in the latest released version of the package, which you can download at http://pear.php.net/get/Math_Stats This issue was a known bug back in 2003, and has been fixed in CVS since Revision 1.16 (June 22, 2003) for version 0.9.0beta2 (see: http://pear.php.net/package/Math_Stats/download/0.9.0beta2) BTW, your definition of the median assumes that there is an even number of members in the array, the general case is the one that is handled in the version released on the aforementioned version. For more details on the correct algorithm read: http://mathworld.wolfram.com/StatisticalMedian.html I suggest you use version 0.9.0beta3 of Math_Stats.
 [2007-11-19 07:01 UTC] jmcastagnetto (Jesus M. Castagnetto)
Ah!, make sure that you are using 0.9.0beta3 and not the 0.8.5 release. The latter one has the bug you mention. Your code runs OK with the code from 0.9.0beta3 and from CVS.
 [2007-11-19 14:22 UTC] breck (Breck Yunits)
Aha. I didn't realize that the pear install Math_Stats command doesn't download the very latest versions(new user). Thanks!