cvs: pear /Math_Stats Stats.php package.xml /Math_Stats/test instance_methods.php
| From: | Jesus M. Castagnetto | Date: | Sun, 01 Jun 2003 03:08:13 +0000 |
| Subject: | cvs: pear /Math_Stats Stats.php package.xml /Math_Stats/test instance_methods.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-10636@lists.php.net to get a copy of this message | ||
jmcastagnetto Sat May 31 23:08:13 2003 EDT
Modified files:
/pear/Math_Stats Stats.php package.xml
/pear/Math_Stats/test instance_methods.php
Log:
- Finished code for percentile and quartile related mehods
- Finished unit test class
TODO: double check the generality of the algorithms for calculation
percentiles and quartile variation coefficients
Index: pear/Math_Stats/Stats.php diff -u pear/Math_Stats/Stats.php:1.12 pear/Math_Stats/Stats.php:1.13 --- pear/Math_Stats/Stats.php:1.12 Sat May 31 01:02:29 2003 +++ pear/Math_Stats/Stats.php Sat May 31 23:08:12 2003 @@ -16,10 +16,10 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Stats.php,v 1.12 2003/05/31 05:02:29 jmcastagnetto Exp $ +// $Id: Stats.php,v 1.13 2003/06/01 03:08:12 jmcastagnetto Exp $ // -include_once "PEAR.php"; +include_once 'PEAR.php'; /** * @package Math_Stats @@ -29,44 +29,44 @@ /** * STATS_BASIC to generate the basic descriptive statistics */ -define("STATS_BASIC", 1); +define('STATS_BASIC', 1); /** * STATS_FULL to generate also higher moments, mode, median, etc. */ -define("STATS_FULL", 2); +define('STATS_FULL', 2); /*}}}*/ // Constants describing the data set format /*{{{*/ /** - * STATS_DATA_SIMPLE for an array of numeric values + * STATS_DATA_SIMPLE for an array of numeric values. This is the default. * e.g. $data = array(2,3,4,5,1,1,6); */ -define("STATS_DATA_SIMPLE", 0); +define('STATS_DATA_SIMPLE', 0); /** * STATS_DATA_CUMMULATIVE for an associative array of frequency values, * where in each array entry, the index is the data point and the * value the count (frequency): * e.g. $data = array(3=>4, 2.3=>5, 1.25=>6, 0.5=>3) */ -define("STATS_DATA_CUMMULATIVE", 1); +define('STATS_DATA_CUMMULATIVE', 1); /*}}}*/ // Constants defining how to handle nulls /*{{{*/ /** - * STATS_REJECT_NULL, reject data sets with null values. + * STATS_REJECT_NULL, reject data sets with null values. This is the default. * Any non-numeric value is considered a null in this context. */ -define("STATS_REJECT_NULL", -1); +define('STATS_REJECT_NULL', -1); /** * STATS_IGNORE_NULL, ignore null values and prune them from the data. * Any non-numeric value is considered a null in this context. */ -define("STATS_IGNORE_NULL", -2); +define('STATS_IGNORE_NULL', -2); /** * STATS_USE_NULL_AS_ZERO, assign the value of 0 (zero) to null values. * Any non-numeric value is considered a null in this context. */ -define("STATS_USE_NULL_AS_ZERO", -3); +define('STATS_USE_NULL_AS_ZERO', -3); /*}}}*/ /** @@ -82,23 +82,23 @@ * can be epxressed more compactly as: * * <pre> - * $data2 = array("1"=>9, "2"=>8, "3"=>5, "4.1"=>2); + * $data2 = array('1'=>9, '2'=>8, '3'=>5, '4.1'=>2); * </pre> * * Example of use: * * <pre> - * include_once "Math/Stats.php"; + * include_once 'Math/Stats.php'; * $s = new Math_Stats(); * $s->setData($data1); * // or * // $s->setData($data2, STATS_DATA_CUMMULATIVE); * $stats = $s->calcBasic(); - * echo "Mean: ".$stats["mean"]." StDev: ".$stats["stdev"]." <br />\n"; + * echo 'Mean: '.$stats['mean'].' StDev: '.$stats['stdev'].' <br />\n'; * * // using data with nulls * // first ignoring them: - * $data3 = array(1.2, "foo", 2.4, 3.1, 4.2, 3.2, null, 5.1, 6.2); + * $data3 = array(1.2, 'foo', 2.4, 3.1, 4.2, 3.2, null, 5.1, 6.2); * $s->setNullOption(STATS_IGNORE_NULL); * $s->setData($data3); * $stats3 = $s->calcFull(); @@ -129,6 +129,15 @@ var $_data = null; /** + * Expanded data set. Only set when cummulative data + * is being used. Null by default. + * + * @access private + * @var array + */ + var $_dataExpanded = null; + + /** * Flag for data type, one of STATS_DATA_SIMPLE or * STATS_DATA_CUMMULATIVE. Null by default. * @@ -179,9 +188,10 @@ */ function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ if (!is_array($arr)) { - return PEAR::raiseError("invalid data, an array of numeric data was expected"); + return PEAR::raiseError('invalid data, an array of numeric data was expected'); } $this->_data = null; + $this->_dataExpanded = null; $this->_dataOption = null; $this->_calculatedValues = array(); if ($opt == STATS_DATA_SIMPLE) { @@ -190,6 +200,7 @@ } else if ($opt == STATS_DATA_CUMMULATIVE) { $this->_dataOption = $opt; $this->_data = $arr; + $this->_dataExpanded = array(); } return $this->_validate(); }/*}}}*/ @@ -199,13 +210,19 @@ * according to the current null handling options. * * @access public + * @param boolean $expanded whether to return a expanded list, default is false * @return mixed array of data on success, a PEAR_Error object otherwise * @see _validate() */ - function getData() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->_data; + function getData($expanded=false) {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if ($this->_dataOption == STATS_DATA_CUMMULATIVE && $expanded) { + return $this->_dataExpanded; + } else { + return $this->_data; + } }/*}}}*/ /** @@ -223,8 +240,8 @@ $this->_nullOption = $nullOption; return true; } else { - return PEAR::raiseError("invalid null handling option expecting: ". - "STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO"); + return PEAR::raiseError('invalid null handling option expecting: '. + 'STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO'); } }/*}}}*/ @@ -233,60 +250,22 @@ * * @access public * @param int $mode one of STATS_BASIC or STATS_FULL + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calcBasic() * @see calcFull() */ - function calc($mode) {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); + function calc($mode, $returnErrorObject=true) {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } if ($mode == STATS_BASIC) { - return array ( - "min" => $this->min(), - "max" => $this->max(), - "sum" => $this->sum(), - "sum2" => $this->sum2(), - "count" => $this->count(), - "mean" => $this->mean(), - "stdev" => $this->stDev(), - "variance" => $this->variance() - ); - } else if ($mode == STATS_FULL) { - return array ( - "min" => $this->min(), - "max" => $this->max(), - "sum" => $this->sum(), - "sum2" => $this->sum2(), - "count" => $this->count(), - "mean" => $this->mean(), - "median" => $this->median(), - "mode" => $this->mode(), - "midrange" => $this->midrange(), - "stdev" => $this->stDev(), - "absdev" => $this->absDev(), - "variance" => $this->variance(), - "std_error_of_mean" => $this->stdErrorOfMean(), - "skewness" => $this->skewness(), - "kurtosis" => $this->kurtosis(), - "coeff_of_variation" => $this->coeffOfVariation(), - "sample_central_moments" => array ( - 1 => $this->sampleCentralMoment(1), - 2 => $this->sampleCentralMoment(2), - 3 => $this->sampleCentralMoment(3), - 4 => $this->sampleCentralMoment(4), - 5 => $this->sampleCentralMoment(5) - ), - "sample_raw_moments" => array ( - 1 => $this->sampleRawMoment(1), - 2 => $this->sampleRawMoment(2), - 3 => $this->sampleRawMoment(3), - 4 => $this->sampleRawMoment(4), - 5 => $this->sampleRawMoment(5) - ), - "frequency" => $this->frequency() - ); + return $this->calcBasic($returnErrorObject); + } elseif ($mode == STATS_FULL) { + return $this->calcFull($returnErrorObject); } else { - return PEAR::raiseError("incorrect mode, expected STATS_BASIC or STATS_FULL"); + return PEAR::raiseError('incorrect mode, expected STATS_BASIC or STATS_FULL'); } }/*}}}*/ @@ -294,24 +273,77 @@ * Calculates a basic set of statistics * * @access public + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calc() * @see calcFull() */ - function calcBasic() {/*{{{*/ - return $this->calc(STATS_BASIC); + function calcBasic($returnErrorObject=true) {/*{{{*/ + return array ( + 'min' => $this->__format($this->min(), $returnErrorObject), + 'max' => $this->__format($this->max(), $returnErrorObject), + 'sum' => $this->__format($this->sum(), $returnErrorObject), + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), + 'count' => $this->__format($this->count(), $returnErrorObject), + 'mean' => $this->__format($this->mean(), $returnErrorObject), + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), + 'variance' => $this->__format($this->variance(), $returnErrorObject), + 'range' => $this->__format($this->range(), $returnErrorObject) + ); }/*}}}*/ /** * Calculates a full set of statistics * * @access public + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calc() * @see calcBasic() */ - function calcFull() {/*{{{*/ - return $this->calc(STATS_FULL); + function calcFull($returnErrorObject=true) {/*{{{*/ + return array ( + 'min' => $this->__format($this->min(), $returnErrorObject), + 'max' => $this->__format($this->max(), $returnErrorObject), + 'sum' => $this->__format($this->sum(), $returnErrorObject), + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), + 'count' => $this->__format($this->count(), $returnErrorObject), + 'mean' => $this->__format($this->mean(), $returnErrorObject), + 'median' => $this->__format($this->median(), $returnErrorObject), + 'mode' => $this->__format($this->mode(), $returnErrorObject), + 'midrange' => $this->__format($this->midrange(), $returnErrorObject), + 'geometric_mean' => $this->__format($this->geometricMean(), $returnErrorObject), + 'harmonic_mean' => $this->__format($this->harmonicMean(), $returnErrorObject), + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), + 'absdev' => $this->__format($this->absDev(), $returnErrorObject), + 'variance' => $this->__format($this->variance(), $returnErrorObject), + 'range' => $this->__format($this->range(), $returnErrorObject), + 'std_error_of_mean' => $this->__format($this->stdErrorOfMean(), $returnErrorObject), + 'skewness' => $this->__format($this->skewness(), $returnErrorObject), + 'kurtosis' => $this->__format($this->kurtosis(), $returnErrorObject), + 'coeff_of_variation' => $this->__format($this->coeffOfVariation(), $returnErrorObject), + 'sample_central_moments' => array ( + 1 => $this->__format($this->sampleCentralMoment(1), $returnErrorObject), + 2 => $this->__format($this->sampleCentralMoment(2), $returnErrorObject), + 3 => $this->__format($this->sampleCentralMoment(3), $returnErrorObject), + 4 => $this->__format($this->sampleCentralMoment(4), $returnErrorObject), + 5 => $this->__format($this->sampleCentralMoment(5), $returnErrorObject) + ), + 'sample_raw_moments' => array ( + 1 => $this->__format($this->sampleRawMoment(1), $returnErrorObject), + 2 => $this->__format($this->sampleRawMoment(2), $returnErrorObject), + 3 => $this->__format($this->sampleRawMoment(3), $returnErrorObject), + 4 => $this->__format($this->sampleRawMoment(4), $returnErrorObject), + 5 => $this->__format($this->sampleRawMoment(5), $returnErrorObject) + ), + 'frequency' => $this->__format($this->frequency(), $returnErrorObject), + 'quartiles' => $this->__format($this->quartiles(), $returnErrorObject), + 'interquartile_range' => $this->__format($this->interquartileRange(), $returnErrorObject), + 'quartile_deviation' => $this->__format($this->quartileDeviation(), $returnErrorObject), + 'quartile_variation_coefficient' => $this->__format($this->quartileVariationCoefficient(), $returnErrorObject) + ); }/*}}}*/ /** @@ -325,7 +357,7 @@ */ function min() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('min', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -349,7 +381,7 @@ */ function max() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('max', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -396,7 +428,7 @@ */ function sum2() {/*{{{*/ if (!array_key_exists('sum2', $this->_calculatedValues)) { - $sum2 = $this->sumN(1); + $sum2 = $this->sumN(2); if (PEAR::isError($sum2)) { return $sum2; } else { @@ -419,7 +451,7 @@ */ function sumN($n) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $sumN = 0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -465,7 +497,7 @@ */ function productN($n) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $prodN = 1.0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -497,13 +529,11 @@ */ function count() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('count', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - foreach($this->_data as $freq) { - $count += $freq; - } + $count = count($this->_dataExpanded); } else { $count = count($this->_data); } @@ -538,6 +568,28 @@ }/*}}}*/ /** + * Calculates the range of the data set = max - min + * + * @access public + * @return mixed the value of the range on success, a PEAR_Error object otherwise. + */ + function range() { + if (!array_key_exists('range', $this->_calculatedValues)) { + $min = $this->min(); + if (PEAR::isError($min)) { + return $min; + } + $max = $this->max(); + if (PEAR::isError($max)) { + return $max; + } + $this->_calculatedValues['range'] = $max - $min; + } + return $this->_calculatedValues['range']; + + } + + /** * Calculates the variance (unbiased) of the data points in the set * Handles cummulative data sets correctly * @@ -690,12 +742,10 @@ return $this->_calculatedValues['skewness']; }/*}}}*/ - - // TODO : finish error handling and calc caching code /** * Calculates the kurtosis of the data distribution in the set * The kurtosis measures the degrees of peakedness of a distribution. - * It is also callesd the "excess" or "excess coefficient", and is + * It is also called the "excess" or "excess coefficient", and is * a normalized form of the fourth central moment of a distribution. * A normal distributions has kurtosis = 0 * A narrow and peaked (leptokurtic) distribution has a @@ -711,10 +761,22 @@ * @see calc() */ function kurtosis() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - $kurt = ($this->__sumdiff(4) / ($this->count() * pow($this->stDev(), 4))) - 3; - return $kurt; + if (!array_key_exists('kurtosis', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + $sumdiff4 = $this->__sumdiff(4); + if (PEAR::isError($sumdiff4)) { + return $sumdiff4; + } + $this->_calculatedValues['kurtosis'] = ($sumdiff4 / ($count * pow($stDev, 4))) - 3; + } + return $this->_calculatedValues['kurtosis']; }/*}}}*/ /** @@ -731,23 +793,28 @@ * @see calc() */ function median() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - $arr = array(); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) - foreach ($this->_data as $val=>$freq) - $arr = array_pad($arr, count($arr) + $freq, $val); - else - $arr = $this->_data; - sort($arr); - $n = count($arr); - $h = intval($n / 2); - if ($n % 2 == 0) { - $median = ($arr[$h] + $arr[$h - 1]) / 2; - } else { - $median = $arr[$h + 1]; + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); } - return $median; + if (!array_key_exists('median', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $arr =& $this->_dataExpanded; + } else { + $arr =& $this->_data; + } + $n = $this->count(); + if (PEAR::isError($n)) { + return $n; + } + $h = intval($n / 2); + if ($n % 2 == 0) { + $median = ($arr[$h] + $arr[$h - 1]) / 2; + } else { + $median = $arr[$h + 1]; + } + $this->_calculatedValues['median'] = $median; + } + return $this->_calculatedValues['median']; }/*}}}*/ /** @@ -762,27 +829,32 @@ * @see calc() */ function mode() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) - $arr = $this->_data; - else - $arr = $this->frequency(); - arsort($arr); - $mcount = 1; - foreach ($arr as $val=>$freq) { - if ($mcount == 1) { - $mode = array($val); - $mfreq = $freq; - $mcount++; - continue; - } - if ($mfreq == $freq) - $mode[] = $val; - if ($mfreq > $freq) - break; + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (!array_key_exists('mode', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $arr = $this->_data; + } else { + $arr = $this->frequency(); + } + arsort($arr); + $mcount = 1; + foreach ($arr as $val=>$freq) { + if ($mcount == 1) { + $mode = array($val); + $mfreq = $freq; + $mcount++; + continue; + } + if ($mfreq == $freq) + $mode[] = $val; + if ($mfreq > $freq) + break; + } + $this->_calculatedValues['mode'] = $mode; } - return $mode; + return $this->_calculatedValues['mode']; }/*}}}*/ /** @@ -797,25 +869,92 @@ * @see calc() */ function midrange() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return (($this->max() + $this->min()) / 2); + if (!array_key_exists('midrange', $this->_calculatedValues)) { + $min = $this->min(); + if (PEAR::isError($min)) { + return $min; + } + $max = $this->max(); + if (PEAR::isError($max)) { + return $max; + } + $this->_calculatedValues['midrange'] = (($max + $min) / 2); + } + return $this->_calculatedValues['midrange']; }/*}}}*/ - + + /** + * Calculates the geometrical mean of the data points in the set + * Handles cummulative data sets correctly + * + * @access public + * @return mixed the geometrical mean value on success, a PEAR_Error object otherwise + * @see calc() + * @see product() + * @see count() + */ function geometricMean() {/*{{{*/ - if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + if (!array_key_exists('geometricMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $prod = $this->product(); + if (PEAR::isError($prod)) { + return $prod; + } + if ($prod == 0.0) { + return 0.0; + } + if ($prod < 0) { + return PEAR::raiseError('The product of the data set is negative, geometric mean undefined.'); + } + $this->_calculatedValues['geometricMean'] = pow($prod , 1 / $count); } - $prod = $this->product(); - if (PEAR::isError($product)) { - return $product; + return $this->_calculatedValues['geometricMean']; + }/*}}}*/ + + /** + * Calculates the harmonic mean of the data points in the set + * Handles cummulative data sets correctly + * + * @access public + * @return mixed the harmonic mean value on success, a PEAR_Error object otherwise + * @see calc() + * @see count() + */ + function harmonicMean() {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); } - if ($product == 0.0) { - return 0.0; + if (!array_key_exists('harmonicMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $invsum = 0.0; + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + foreach($this->_data as $val=>$freq) { + if ($val == 0) { + return PEAR::raiseError('cannot calculate a '. + 'harmonic mean with data values of zero.'); + } + $invsum += $freq / $val; + } + } else { + foreach($this->_data as $val) { + if ($val == 0) { + return PEAR::raiseError('cannot calculate a '. + 'harmonic mean with data values of zero.'); + } + $invsum += 1 / $val; + } + } + $this->_calculatedValues['harmonicMean'] = $count / $invsum; } - return power($prod , -1 * $this->count()); + return $this->_calculatedValues['harmonicMean']; }/*}}}*/ - + /** * Calculates the nth central moment (m{n}) of a data set. * @@ -824,8 +963,16 @@ * m{n} = 1/N * SUM { (xi - avg)^n } * * where: N = sample size, avg = sample mean. + * + * @access public + * @param integer $n moment to calculate + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise */ function sampleCentralMoment($n) {/*{{{*/ + if (!is_int($n) || $n < 1) { + return PEAR::isError('moment must be a positive integer >= 1.'); + } + if ($n == 1) { return 0; } @@ -834,7 +981,8 @@ return $count; } if ($count == 0) { - return PEAR::raiseError("Cannot calculate {$n}th sample moment, there are zero data entries."); + return PEAR::raiseError("Cannot calculate {$n}th sample moment, ". + 'there are zero data entries'); } $sum = $this->__sumdiff($n); if (PEAR::isError($sum)) { @@ -851,14 +999,23 @@ * m{n} = 1/N * SUM { xi^n } * * where: N = sample size, avg = sample mean. + * + * @access public + * @param integer $n moment to calculate + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise */ function sampleRawMoment($n) {/*{{{*/ + if (!is_int($n) || $n < 1) { + return PEAR::isError('moment must be a positive integer >= 1.'); + } + $count = $this->count(); if (PEAR::isError($count)) { return $count; } if ($count == 0) { - return PEAR::raiseError("Cannot calculate {$n}th raw moment, there are zero data entries."); + return PEAR::raiseError("Cannot calculate {$n}th raw moment, ". + 'there are zero data entries.'); } $sum = $this->sumN($n); if (PEAR::isError($sum)) { @@ -867,28 +1024,6 @@ return ($sum / $count); }/*}}}*/ - /** - * Calculates the value frequency table of a data set. - * Handles cummulative data sets correctly - * - * @access public - * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise - * @see min() - * @see max() - * @see calc() - */ - function frequency() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - return $this->_data; - } else { - $freq = array(); - foreach ($this->_data as $val) - $freq["$val"]++; - return $freq; - } - }/*}}}*/ /** * Calculates the coefficient of variation of a data set. @@ -903,9 +1038,23 @@ * @see calc() */ function coeffOfVariation() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->stDev() / $this->mean(); + if (!array_key_exists('coeffOfVariation', $this->_calculatedValues)) { + $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + if ($mean == 0.0) { + return PEAR::raiseError('cannot calculate the coefficient '. + 'of variation, mean of sample is zero'); + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + + $this->_calculatedValues['coeffOfVariation'] = $stDev / $mean; + } + return $this->_calculatedValues['coeffOfVariation']; }/*}}}*/ /** @@ -926,12 +1075,210 @@ * @see calc() */ function stdErrorOfMean() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->stDev() / sqrt($this->count()); + if (!array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + $this->_calculatedValues['stdErrorOfMean'] = $stDev / sqrt($count); + } + return $this->_calculatedValues['stdErrorOfMean']; }/*}}}*/ /** + * Calculates the value frequency table of a data set. + * Handles cummulative data sets correctly + * + * @access public + * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise + * @see min() + * @see max() + * @see calc() + */ + function frequency() {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (!array_key_exists('frequency', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $freq = $this->_data; + } else { + $freq = array(); + foreach ($this->_data as $val) { + $freq["$val"]++; + } + ksort($freq); + } + $this->_calculatedValues['frequency'] = $freq; + } + return $this->_calculatedValues['frequency']; + }/*}}}*/ + + /** + * The quartiles are defined as the values that divide a sorted + * data set into four equal-sized subsets, and correspond to the + * 25th, 50th, and 75th percentiles. + * + * @access public + * @return mixed an associative array of quartiles on success, a PEAR_Error otherwise + * @see percentile() + */ + function quartiles() {/*{{{*/ + if (!array_key_exists('quartiles', $this->_calculatedValues)) { + $q1 = $this->percentile(25); + if (PEAR::isError($q1)) { + return $q1; + } + $q2 = $this->percentile(50); + if (PEAR::isError($q2)) { + return $q2; + } + $q3 = $this->percentile(75); + if (PEAR::isError($q3)) { + return $q3; + } + $this->_calculatedValues['quartiles'] = array ( + '25' => $q1, + '50' => $q2, + '75' => $q3 + ); + } + return $this->_calculatedValues['quartiles']; + }/*}}}*/ + + /** + * The interquartile range is the distance between the 75th and 25th + * percentiles. Basically the range of the middle 50% of the data set, + * and thus is not affected by outliers or extreme values. + * + * interquart range = P(75) - P(25) + * + * where: P = percentile + * + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + */ + function interquartileRange() {/*{{{*/ + if (!array_key_exists('interquartileRange', $this->_calculatedValues)) { + $quart = $this->quartiles(); + if (PEAR::isError($quart)) { + return $quart; + } + $q3 = $quart['75']; + $q1 = $quart['25']; + $this->_calculatedValues['interquartileRange'] = $q3 - $q1; + } + return $this->_calculatedValues['interquartileRange']; + }/*}}}*/ + + /** + * The quartile deviation is half of the interquartile range value + * + * quart dev = (P(75) - P(25)) / 2 + * + * where: P = percentile + * + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + * @see interquartileRange() + */ + function quartileDeviation() {/*{{{*/ + if (!array_key_exists('quartileDeviation', $this->_calculatedValues)) { + $iqr = $this->interquartileRange(); + if (PEAR::isError($iqr)) { + return $iqr; + } + $this->_calculatedValues['quartileDeviation'] = $iqr / 2; + } + return $this->_calculatedValues['quartileDeviation']; + }/*}}}*/ + + /** + * The quartile variation coefficient is the percent ratio of the + * interquartile range and the sum of the 75th and 25th percentiles: + * + * quart var coeff = 100 * (P(75) - P(25)) / (P(75) + P(25)) + * + * where: P = percentile + * + * @todo need to double check the equation + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + */ + function quartileVariationCoefficient() {/*{{{*/ + if (!array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { + $quart = $this->quartiles(); + if (PEAR::isError($quart)) { + return $quart; + } + $q3 = $quart['75']; + $q1 = $quart['25']; + $d = $q3 - $q1; + $s = $q3 + $q1; + echo "** D = $d :: S = $s\n"; + $this->_calculatedValues['quartileVariationCoefficient'] = 100 * $d / $s; + } + return $this->_calculatedValues['quartileVariationCoefficient']; + }/*}}}*/ + + /** + * The pth percentile is the value such that p% of the a sorted data set + * is smaller than it, and (100 - p)% of the data is larger. + * + * A quick algorithm to pick the appropriate value from a sorted data + * set is as follows: + * + * - Count the number of values: n + * - Calculate the position of the value in the data list: i = p * (n + 1) + * - if i is an integer, return the data at that position + * - if i < 1, return the minimum of the data set + * - if i > n, return the maximum of the data set + * - otherwise, average the entries at adjacent positions to i + * + * The median is the 50th percentile value. + * + * @todo need to double check generality of the algorithm + * + * @access public + * @param numeric $p the percentile to estimate, e.g. 25 for 25th percentile + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + * @see median() + */ + function percentile($p) {/*{{{*/ + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $data =& $this->_dataExpanded; + } else { + $data =& $this->_data; + } + $obsidx = $p * ($count + 1) / 100; + if (intval($obsidx) == $obsidx) { + return $data[($obsidx - 1)]; + } elseif ($obsidx < 1) { + return $data[0]; + } elseif ($obsidx > $count) { + return $data[($count - 1)]; + } else { + $left = floor($obsidx - 1); + $right = ceil($obsidx - 1); + return ($data[$left] + $data[$right]) / 2; + } + }/*}}}*/ + + // private methods + + /** * Utility function to calculate: SUM { (xi - mean)^n } * * @access private @@ -945,14 +1292,20 @@ * @see kurtosis(); */ function __sumdiff($power, $mean=null) {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if (is_null($mean)) + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (is_null($mean)) { $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + } $sdiff = 0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - foreach ($this->_data as $val=>$freq) + foreach ($this->_data as $val=>$freq) { $sdiff += $freq * pow((double)($val - $mean), (double)$power); + } } else { foreach ($this->_data as $val) $sdiff += pow((double)($val - $mean), (double)$power); @@ -964,7 +1317,7 @@ * Utility function to calculate the variance with or without * a fixed mean * - * @acess private + * @access private * @param $mean the fixed mean to use, null as default * @return mixed a numeric value on success, a PEAR_Error otherwise * @see variance() @@ -972,18 +1325,12 @@ */ function __calcVariance($mean = null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); - } - - if (is_null($mean)) { - $sumdiff2 = $this->__sumdiff(2); - } else { - $sumdiff2 = $this->__sumdiff(2, $mean); + return PEAR::raiseError('data has not been set'); } + $sumdiff2 = $this->__sumdiff(2, $mean); if (PEAR::isError($sumdiff2)) { return $sumdiff2; } - $count = $this->count(); if (PEAR::isError($count)) { return $count; @@ -994,24 +1341,30 @@ return ($sumdiff2 / ($count - 1)); }/*}}}*/ - function __calcAbsoluteDeviation($mean = null) { + /** + * Utility function to calculate the absolute deviation with or without + * a fixed mean + * + * @access private + * @param $mean the fixed mean to use, null as default + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see absDev() + * @see absDevWithMean() + */ + function __calcAbsoluteDeviation($mean = null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $count = $this->count(); if (PEAR::isError($count)) { return $count; } - if (is_null) { - $sumabsdev = $this->__sumabsdev(); - } else { - $sumabsdev = $this->__sumabsdev($mean); - } + $sumabsdev = $this->__sumabsdev($mean); if (PEAR::isError($sumabsdev)) { return $sumabsdev; } - return sqrt($sumabsdev / $count); - } + return $sumabsdev / $count; + }/*}}}*/ /** * Utility function to calculate: SUM { | xi - mean | } @@ -1025,7 +1378,7 @@ */ function __sumabsdev($mean=null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (is_null($mean)) { $mean = $this->mean(); @@ -1044,6 +1397,26 @@ }/*}}}*/ /** + * Utility function to format a PEAR_Error to be used by calc(), + * calcBasic() and calcFull() + * + * @access private + * @param mixed $v value to be formatted + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false) + * @return mixed if the value is a PEAR_Error object, and $useErrorObject + * is false, then a string with the error message will be returned, + * otherwise the value will not be modified and returned as passed. + */ + function __format($v, $useErrorObject=true) { + if (PEAR::isError($v) && $useErrorObject == false) { + return $v->getMessage(); + } else { + return $v; + } + } + + /** * Utility function to validate the data and modify it * according to the current null handling option * @@ -1072,10 +1445,20 @@ break; case STATS_REJECT_NULL : default: - return PEAR::raiseError("data rejected, contains NULL values"); + return PEAR::raiseError('data rejected, contains NULL values'); break; } } + } + if ($flag) { + ksort($this->_data); + $this->_dataExpanded = array(); + foreach ($this->_data as $val=>$freq) { + $this->_dataExpanded = array_pad($this->_dataExpanded, count($this->_dataExpanded) + $freq, $val); + } + sort($this->_dataExpanded); + } else { + sort($this->_data); } return true; }/*}}}*/ Index: pear/Math_Stats/package.xml diff -u pear/Math_Stats/package.xml:1.8 pear/Math_Stats/package.xml:1.9 --- pear/Math_Stats/package.xml:1.8 Tue May 27 19:34:11 2003 +++ pear/Math_Stats/package.xml Sat May 31 23:08:12 2003 @@ -7,7 +7,7 @@ <name>Math_Stats</name> <summary>Classes to calculate statistical parameters</summary> <description> - Classes to calculate statistical parameters of numerical arrays + Package to calculate statistical parameters of numerical arrays of data. The data can be in a simple numerical array, or in a cummulative numerical array. A cummulative array, has the value as the index and the number of repeats as the value for the @@ -26,6 +26,51 @@ <release> <license>PHP</license> + <version>0.9.0</version> + <date>2003-06-01</date> + <notes> +<![CDATA[ +Many changes in the existing methods, and a number of new ones: + +- Added better error handling. +- Refactored existing methods to delegate calculations better +- Added value caching for invariant statistical parameters +- Added optional parameters to some existing methods to enhance their + processing or output. Preserve the original behaviour as default so + as not breack backwards compatibility +- Added new methods: + - range() + - geometricMean() + - harmonicMean() + - product() + - productN() + - percentile() + - quartiles() + - quartileDeviation() + - interquartileRange() + - quartileVariationCoefficient() +- Added a unit test class based on PHPUnit +]]> + </notes> + <state>beta</state> + <filelist> + <dir name="/" baseinstalldir="Math"> + <file role="php">Stats.php</file> + <dir name="examples" role="example"> + <file role="php">ex_stats_simple.php</file> + <file role="php">ex_stats_cummulative_data.php</file> + </dir> + <dir name="test" role="test"> + <file role="php">instance_methods.php</file> + <file role="php">instance_methods-data.php</file> + </dir> + <file role="doc">README-Math_Stats</file> + </dir> + </filelist> + </release> + <changelog> + <release> + <license>PHP</license> <version>0.8.5</version> <date>2003-05-27</date> <notes> @@ -38,18 +83,7 @@ pointing this problem out. </notes> <state>stable</state> - <filelist> - <dir name="/" baseinstalldir="Math"> - <file role="php">Stats.php</file> - <dir name="examples" role="example"> - <file role="php">ex_stats_simple.php</file> - <file role="php">ex_stats_cummulative_data.php</file> - </dir> - <file role="doc">README-Math_Stats</file> - </dir> - </filelist> </release> - <changelog> <release> <version>0.8.4</version> <date>2003-05-16</date> Index: pear/Math_Stats/test/instance_methods.php diff -u pear/Math_Stats/test/instance_methods.php:1.1 pear/Math_Stats/test/instance_methods.php:1.2 --- pear/Math_Stats/test/instance_methods.php:1.1 Sat May 31 01:02:30 2003 +++ pear/Math_Stats/test/instance_methods.php Sat May 31 23:08:13 2003 @@ -18,34 +18,802 @@ // // Matrix definition and manipulation package // -// $Id: instance_methods.php,v 1.1 2003/05/31 05:02:30 jmcastagnetto Exp $ +// $Id: instance_methods.php,v 1.2 2003/06/01 03:08:13 jmcastagnetto Exp $ // +/** + * Unit test for the Math_Stats class + * + * @package Math_Stats + */ + require_once 'PHPUnit.php'; -require_once 'Math/Stats.php'; -//require_once '../Stats.php'; +//require_once 'Math/Stats.php'; +require_once '../Stats.php'; +require_once './instance_methods-data.php'; + +define('__PRECISION', 12); +define('__DELTA', pow(10, -1 * (__PRECISION - 4))); + +// make sure we have the correct number of decimal figures +ini_set('precision', __PRECISION); + +/** + * Unit test class Math_Stats + * + */ +class Math_Stats_Unit_Test extends PHPUnit_TestCase {/*{{{*/ -class Math_Stats_Instance_Methods_Test extends PHPUnit_TestCase {/*{{{*/ - var $stats; - var $data = array (2,2.3,4.5,2,2,3.2,5.3,3,4,5,1,6); - var $datawithnulls = array (1.1650,null, "foo",0.6268, 0.6268, 0.0751, 0.3516, -0.6965); - var $cummdata = array("3"=>4, "2.333"=>5, "1.22"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); - var $cummdatawithnulls = array("3"=>4, "plink"=>2, "bar is not foo"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + var $s1; + var $s2a; + var $s2b; + var $s3; + var $s4a; + var $s4b; - function Math_Matrix_Instance_Methods_Test($name) { + // simple data set + var $data1 = array (2,2.3,4.5,2,2,3.2,5.3,3,4,5,1,6); + // data set with nulls + var $data2 = array (1.1650,null, "foo",0.6268, 0.6268, 0.0751, 0.3516, -0.6965); + // cummulative data set + var $data3 = array("3"=>4, "2.333"=>5, "1.22"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + // cummulative data set with nulls + var $data4 = array("3"=>4, "plink"=>2, "bar is not foo"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + + function Math_Matrix_Instance_Methods_Test($name) {/*{{{*/ $this->PHPUnit_TestCase($name); - } + }/*}}}*/ + + function setUp() {/*{{{*/ + $this->setLooselyTyped(true); + // simple data sets + $this->s1 = new Math_Stats(STATS_REJECT_NULL); + $this->s1->setData($this->data1); + $this->s2a = new Math_Stats(STATS_IGNORE_NULL); + $this->s2a->setData($this->data2); + $this->s2b = new Math_Stats(STATS_USE_NULL_AS_ZERO); + $this->s2b->setData($this->data2); + // cummulative data sets + $this->s3 = new Math_Stats(STATS_REJECT_NULL); + $this->s3->setData($this->data3, STATS_DATA_CUMMULATIVE); + $this->s4a = new Math_Stats(STATS_IGNORE_NULL); + $this->s4a->setData($this->data4, STATS_DATA_CUMMULATIVE); + $this->s4b = new Math_Stats(STATS_USE_NULL_AS_ZERO); + $this->s4b->setData($this->data4, STATS_DATA_CUMMULATIVE); + }/*}}}*/ + + function tearDown() {/*{{{*/ + unset($this->s1); + unset($this->s2a); + unset($this->s2b); + unset($this->s3); + unset($this->s4a); + unset($this->s4b); + }/*}}}*/ + + function testGetData() {/*{{{*/ + $this->assertEquals($GLOBALS['testGetData_out1'], $this->formatArray($this->s1->getData())); + $this->assertEquals($GLOBALS['testGetData_out2'], $this->formatArray($this->s2a->getData())); + $this->assertEquals($GLOBALS['testGetData_out3'], $this->formatArray($this->s2b->getData())); + $this->assertEquals($GLOBALS['testGetData_out4'], $this->formatArray($this->s3->getData())); + $this->assertEquals($GLOBALS['testGetData_out5'], $this->formatArray($this->s4a->getData())); + $this->assertEquals($GLOBALS['testGetData_out6'], $this->formatArray($this->s4a->getData(true))); + $this->assertEquals($GLOBALS['testGetData_out7'], $this->formatArray($this->s4b->getData())); + }/*}}}*/ + + function testCalcBasic() {/*{{{*/ + $this->assertEquals($GLOBALS['testCalcBasic_out1'], + $this->formatArray($this->s1->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out2'], + $this->formatArray($this->s2a->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out3'], + $this->formatArray($this->s2b->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out4'], + $this->formatArray($this->s3->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out5'], + $this->formatArray($this->s4a->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out6'], + $this->formatArray($this->s4b->calcBasic(false))); + }/*}}}*/ + + function testCalcFull() {/*{{{*/ + $this->assertEquals($GLOBALS['testCalcFull_out1'], + $this->formatArray($this->s1->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out2'], + $this->formatArray($this->s2a->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out3'], + $this->formatArray($this->s2b->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out4'], + $this->formatArray($this->s3->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out5'], + $this->formatArray($this->s4a->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out6'], + $this->formatArray($this->s4b->calcFull(false))); + }/*}}}*/ + + function testMin() {/*{{{*/ + $this->assertEquals($this->formatNumber(1), + $this->formatNumber($this->s1->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.6965), + $this->formatNumber($this->s2a->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.6965), + $this->formatNumber($this->s2b->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.5), + $this->formatNumber($this->s3->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.5), + $this->formatNumber($this->s4a->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->min()), + '', __DELTA); + }/*}}}*/ + + function testMax() {/*{{{*/ + $this->assertEquals($this->formatNumber(6), + $this->formatNumber($this->s1->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.165), + $this->formatNumber($this->s2a->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.165), + $this->formatNumber($this->s2b->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s3->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4a->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4b->max()), + '', __DELTA); + }/*}}}*/ + + function testSum() {/*{{{*/ + $this->assertEquals($this->formatNumber(40.3), + $this->formatNumber($this->s1->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.1488), + $this->formatNumber($this->s2a->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.1488), + $this->formatNumber($this->s2b->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(51.085), + $this->formatNumber($this->s3->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.1), + $this->formatNumber($this->s4a->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.1), + $this->formatNumber($this->s4b->sum()), + '', __DELTA); + }/*}}}*/ + + function testSum2() {/*{{{*/ + $this->assertEquals($this->formatNumber(162.87), + $this->formatNumber($this->s1->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.7573563), + $this->formatNumber($this->s2a->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.7573563), + $this->formatNumber($this->s2b->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(114.834845), + $this->formatNumber($this->s3->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(78.69), + $this->formatNumber($this->s4a->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(78.69), + $this->formatNumber($this->s4b->sum2()), + '', __DELTA); + }/*}}}*/ + + function testSumN() {/*{{{*/ + $this->assertEquals($this->formatNumber(741.937), + $this->formatNumber($this->s1->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.77968786139), + $this->formatNumber($this->s2a->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.40141456571), + $this->formatNumber($this->s2b->sumN(4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(280.987388185), + $this->formatNumber($this->s3->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(206.601), + $this->formatNumber($this->s4a->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(557.7429), + $this->formatNumber($this->s4b->sumN(4)), + '', __DELTA); + }/*}}}*/ + + function testProduct() {/*{{{*/ + $this->assertEquals($this->formatNumber(505543.68), + $this->formatNumber($this->s1->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.00841770739124), + $this->formatNumber($this->s2a->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(46478.287296), + $this->formatNumber($this->s3->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(544.32), + $this->formatNumber($this->s4a->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->product()), + '', __DELTA); + }/*}}}*/ + + function testProductN() {/*{{{*/ + $this->assertEquals($this->formatNumber(255574412388), + $this->formatNumber($this->s1->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(7.08577977246E-05), + $this->formatNumber($this->s2a->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->productN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(428617.299597), + $this->formatNumber($this->s3->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(5714.053632), + $this->formatNumber($this->s4a->productN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->productN(2)), + '', __DELTA); + }/*}}}*/ + + function testCount() {/*{{{*/ + $this->assertEquals($this->formatNumber(12), + $this->formatNumber($this->s1->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(6), + $this->formatNumber($this->s2a->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(8), + $this->formatNumber($this->s2b->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(27), + $this->formatNumber($this->s3->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(16), + $this->formatNumber($this->s4a->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(24), + $this->formatNumber($this->s4b->count()), + '', __DELTA); + }/*}}}*/ + + function testMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.35833333333), + $this->formatNumber($this->s1->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.358133333333), + $this->formatNumber($this->s2a->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.2686), + $this->formatNumber($this->s2b->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.89203703704), + $this->formatNumber($this->s3->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.00625), + $this->formatNumber($this->s4a->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.3375), + $this->formatNumber($this->s4b->mean()), + '', __DELTA); + }/*}}}*/ + + function testRange() {/*{{{*/ + $this->assertEquals($this->formatNumber(5), + $this->formatNumber($this->s1->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8615), + $this->formatNumber($this->s2a->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8615), + $this->formatNumber($this->s2b->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.5), + $this->formatNumber($this->s3->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.5), + $this->formatNumber($this->s4a->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4b->range()), + '', __DELTA); + }/*}}}*/ + + function testVariance() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.50265151515), + $this->formatNumber($this->s1->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.397559878667), + $this->formatNumber($this->s2a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.311455517143), + $this->formatNumber($this->s2b->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.699235883191), + $this->formatNumber($this->s3->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.952625), + $this->formatNumber($this->s4a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.55461956522), + $this->formatNumber($this->s4b->variance()), + '', __DELTA); + }/*}}}*/ + + function testStDev() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.58197709059), + $this->formatNumber($this->s1->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.630523495729), + $this->formatNumber($this->s2a->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.558081998583), + $this->formatNumber($this->s2b->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.836203254712), + $this->formatNumber($this->s3->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.976025102136), + $this->formatNumber($this->s4a->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.24684384155), + $this->formatNumber($this->s4b->stDev()), + '', __DELTA); + }/*}}}*/ - function setUp() { - $this->stats = new Math_Stats(); - } + function testVarianceWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.50454545455), + $this->formatNumber($this->s1->varianceWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.39966326), + $this->formatNumber($this->s2a->varianceWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.312582328571), + $this->formatNumber($this->s2b->varianceWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.699301730769), + $this->formatNumber($this->s3->varianceWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.952666666667), + $this->formatNumber($this->s4a->varianceWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.55869565217), + $this->formatNumber($this->s4b->varianceWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + function testStDevWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.58257557625), + $this->formatNumber($this->s1->stDevWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.632189259637), + $this->formatNumber($this->s2a->stDevWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.559090626439), + $this->formatNumber($this->s2b->stDevWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.836242626735), + $this->formatNumber($this->s3->stDevWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.976046446982), + $this->formatNumber($this->s4a->stDevWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.24847733346), + $this->formatNumber($this->s4b->stDevWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + + function testAbsDev() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.33472222222), + $this->formatNumber($this->s1->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.448066666667), + $this->formatNumber($this->s2a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.42395), + $this->formatNumber($this->s2b->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.754993141289), + $this->formatNumber($this->s3->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.84140625), + $this->formatNumber($this->s4a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.17395833333), + $this->formatNumber($this->s4b->variance()), + '', __DELTA); + }/*}}}*/ + + function testAbsDevWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.34166666667), + $this->formatNumber($this->s1->absDevWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.448066666667), + $this->formatNumber($this->s2a->absDevWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.42395), + $this->formatNumber($this->s2b->absDevWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.753518518519), + $this->formatNumber($this->s3->absDevWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.84375), + $this->formatNumber($this->s4a->absDevWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.17916666667), + $this->formatNumber($this->s4b->absDevWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + + function testSkewness() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.211767803758), + $this->formatNumber($this->s1->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.419944986921), + $this->formatNumber($this->s2a->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.0950243805522), + $this->formatNumber($this->s2b->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.321743512221), + $this->formatNumber($this->s3->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.578555878286), + $this->formatNumber($this->s4a->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.122636601767), + $this->formatNumber($this->s4b->skewness()), + '', __DELTA); + }/*}}}*/ + + function testKurtosis() {/*{{{*/ + $this->assertEquals($this->formatNumber(-1.47708896609), + $this->formatNumber($this->s1->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.23078655976), + $this->formatNumber($this->s2a->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.992375284912), + $this->formatNumber($this->s2b->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.4009978017), + $this->formatNumber($this->s3->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.4499405466), + $this->formatNumber($this->s4a->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.8513180639), + $this->formatNumber($this->s4b->kurtosis()), + '', __DELTA); + }/*}}}*/ + + function testMedian() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.1), + $this->formatNumber($this->s1->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.4892), + $this->formatNumber($this->s2a->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.21335), + $this->formatNumber($this->s2b->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.333), + $this->formatNumber($this->s3->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4a->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.9), + $this->formatNumber($this->s4b->median()), + '', __DELTA); + }/*}}}*/ + + function testMode() {/*{{{*/ + $this->assertEquals($GLOBALS['testMode_out1'], $this->formatArray($this->s1->mode())); + $this->assertEquals($GLOBALS['testMode_out2'], $this->formatArray($this->s2a->mode())); + $this->assertEquals($GLOBALS['testMode_out3'], $this->formatArray($this->s2b->mode())); + $this->assertEquals($GLOBALS['testMode_out4'], $this->formatArray($this->s3->mode())); + $this->assertEquals($GLOBALS['testMode_out5'], $this->formatArray($this->s4a->mode())); + $this->assertEquals($GLOBALS['testMode_out6'], $this->formatArray($this->s4b->mode())); + }/*}}}*/ + + function testMidrange() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.5), + $this->formatNumber($this->s1->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.23425), + $this->formatNumber($this->s2a->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.23425), + $this->formatNumber($this->s2b->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.75), + $this->formatNumber($this->s3->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.75), + $this->formatNumber($this->s4a->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.5), + $this->formatNumber($this->s4b->midrange()), + '', __DELTA); + }/*}}}*/ + + function testGeometricMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.98753652642), + $this->formatNumber($this->s1->geometricMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s2a->geometricMean(),'PEAR_Error')); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48888486575), + $this->formatNumber($this->s3->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48248699844), + $this->formatNumber($this->s4a->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->geometricMean()), + '', __DELTA); + }/*}}}*/ + + function testHarmonicMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.60406264194), + $this->formatNumber($this->s1->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.319605399284), + $this->formatNumber($this->s2a->harmonicMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s2b->harmonicMean(),'PEAR_Error')); + $this->assertEquals($this->formatNumber(1.38224654591), + $this->formatNumber($this->s3->harmonicMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.28285077951), + $this->formatNumber($this->s4a->harmonicMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s4b->harmonicMean(),'PEAR_Error')); + }/*}}}*/ + + function testSampleCentralMoment() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.29409722222), + $this->formatNumber($this->s1->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.331299898889), + $this->formatNumber($this->s2a->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.0165169209322), + $this->formatNumber($this->s2b->sampleCentralMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.188124500214), + $this->formatNumber($this->s3->sampleCentralMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.8930859375), + $this->formatNumber($this->s4a->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48984375), + $this->formatNumber($this->s4b->sampleCentralMoment(2)), + '', __DELTA); + }/*}}}*/ + + function testSampleRawMoment() {/*{{{*/ + $this->assertEquals($this->formatNumber(13.5725), + $this->formatNumber($this->s1->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.459559383333), + $this->formatNumber($this->s2a->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.222460982673), + $this->formatNumber($this->s2b->sampleRawMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(10.4069403031), + $this->formatNumber($this->s3->sampleRawMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(4.918125), + $this->formatNumber($this->s4a->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(3.27875), + $this->formatNumber($this->s4b->sampleRawMoment(2)), + '', __DELTA); + }/*}}}*/ + + function testCoeffOfVariation() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.471060175858), + $this->formatNumber($this->s1->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.76058310423), + $this->formatNumber($this->s2a->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.07774385176), + $this->formatNumber($this->s2b->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.441959241993), + $this->formatNumber($this->s3->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.486492262747), + $this->formatNumber($this->s4a->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.932219694619), + $this->formatNumber($this->s4b->coeffOfVariation()), + '', __DELTA); + }/*}}}*/ + + function testStdErrorOfMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.456677449552), + $this->formatNumber($this->s1->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.257410139229), + $this->formatNumber($this->s2a->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.197311782828), + $this->formatNumber($this->s2b->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.160927391402), + $this->formatNumber($this->s3->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.244006275534), + $this->formatNumber($this->s4a->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.254510933395), + $this->formatNumber($this->s4b->stdErrorOfMean()), + '', __DELTA); + }/*}}}*/ + + function testFrequency() {/*{{{*/ + $this->assertEquals($GLOBALS['testFrequency_out1'], $this->formatArray($this->s1->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out2'], $this->formatArray($this->s2a->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out3'], $this->formatArray($this->s2b->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out4'], $this->formatArray($this->s3->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out5'], $this->formatArray($this->s4a->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out6'], $this->formatArray($this->s4b->frequency())); + }/*}}}*/ + + function testQuartiles() {/*{{{*/ + $this->assertEquals($GLOBALS['testQuartiles_out1'], $this->formatArray($this->s1->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out2'], $this->formatArray($this->s2a->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out3'], $this->formatArray($this->s2b->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out4'], $this->formatArray($this->s3->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out5'], $this->formatArray($this->s4a->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out6'], $this->formatArray($this->s4b->quartiles())); + }/*}}}*/ + + function testInterquartileRange() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.75), + $this->formatNumber($this->s1->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.2066), + $this->formatNumber($this->s2a->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6268), + $this->formatNumber($this->s2b->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.18), + $this->formatNumber($this->s3->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8), + $this->formatNumber($this->s4a->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4b->interquartileRange()), + '', __DELTA); + }/*}}}*/ + + function testQuartileDeviation() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.375), + $this->formatNumber($this->s1->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6033), + $this->formatNumber($this->s2a->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.3134), + $this->formatNumber($this->s2b->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.59), + $this->formatNumber($this->s3->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.9), + $this->formatNumber($this->s4a->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.2), + $this->formatNumber($this->s4b->quartileDeviation()), + '', __DELTA); + }/*}}}*/ + + function testQuartileVariationCoefficient() {/*{{{*/ + /* + echo "quartileVariationCoefficient\n"; + echo $this->s1->quartileVariationCoefficient() ."\n"; + echo $this->s2a->quartileVariationCoefficient()."\n"; + echo $this->s2b->quartileVariationCoefficient()."\n"; + echo $this->s3->quartileVariationCoefficient() ."\n"; + echo $this->s4a->quartileVariationCoefficient()."\n"; + echo $this->s4b->quartileVariationCoefficient()."\n"; + */ + $this->assertEquals($this->formatNumber(40.7407407407), + $this->formatNumber($this->s1->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(206.185919344), + $this->formatNumber($this->s2a->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(100), + $this->formatNumber($this->s2b->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.5966850829), + $this->formatNumber($this->s3->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(50), + $this->formatNumber($this->s4a->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(100), + $this->formatNumber($this->s4b->quartileVariationCoefficient()), + '', __DELTA); + }/*}}}*/ + + function testPercentile() {/*{{{*/ + $this->assertEquals($this->formatNumber(2), + $this->formatNumber($this->s1->percentile(25)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6268), + $this->formatNumber($this->s2a->percentile(60)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.8959), + $this->formatNumber($this->s2b->percentile(80)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.22), + $this->formatNumber($this->s3->percentile(25)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4a->percentile(60)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4b->percentile(80)), + '', __DELTA); + }/*}}}*/ + + function formatNumber($n) {/*{{{*/ + return (float) sprintf('%.'.(__PRECISION - 1), $n); }/*}}}*/ -$suite = new PHPUnit_TestSuite('Math_Stats_Instance_Methods_Test'); + function formatArray($arr,$spcs=0) {/*{{{*/ + $out = ''; + foreach ($arr as $key=>$val) { + $out .= str_repeat(" ",$spcs)."[$key : "; + if (is_array($val)) { + $out .= "\n".$this->formatArray($val, ($spcs + 1))."]\n"; + } else { + $out .= $val."]\n"; + } + } + return $out; + }/*}}}*/ + +}/*}}}*/ + +$suite = new PHPUnit_TestSuite('Math_Stats_Unit_Test'); $result = PHPUnit::run($suite); echo $result->toString()."\n"; // vim: ts=4:sw=4:et: -// vim6: fdl=0: fdm=marker: +// vim6: fdl=1: fdm=marker: ?>
Index: pear/Math_Stats/Stats.php diff -u pear/Math_Stats/Stats.php:1.12 pear/Math_Stats/Stats.php:1.13 --- pear/Math_Stats/Stats.php:1.12 Sat May 31 01:02:29 2003 +++ pear/Math_Stats/Stats.php Sat May 31 23:08:12 2003 @@ -16,10 +16,10 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Stats.php,v 1.12 2003/05/31 05:02:29 jmcastagnetto Exp $ +// $Id: Stats.php,v 1.13 2003/06/01 03:08:12 jmcastagnetto Exp $ // -include_once "PEAR.php"; +include_once 'PEAR.php'; /** * @package Math_Stats @@ -29,44 +29,44 @@ /** * STATS_BASIC to generate the basic descriptive statistics */ -define("STATS_BASIC", 1); +define('STATS_BASIC', 1); /** * STATS_FULL to generate also higher moments, mode, median, etc. */ -define("STATS_FULL", 2); +define('STATS_FULL', 2); /*}}}*/ // Constants describing the data set format /*{{{*/ /** - * STATS_DATA_SIMPLE for an array of numeric values + * STATS_DATA_SIMPLE for an array of numeric values. This is the default. * e.g. $data = array(2,3,4,5,1,1,6); */ -define("STATS_DATA_SIMPLE", 0); +define('STATS_DATA_SIMPLE', 0); /** * STATS_DATA_CUMMULATIVE for an associative array of frequency values, * where in each array entry, the index is the data point and the * value the count (frequency): * e.g. $data = array(3=>4, 2.3=>5, 1.25=>6, 0.5=>3) */ -define("STATS_DATA_CUMMULATIVE", 1); +define('STATS_DATA_CUMMULATIVE', 1); /*}}}*/ // Constants defining how to handle nulls /*{{{*/ /** - * STATS_REJECT_NULL, reject data sets with null values. + * STATS_REJECT_NULL, reject data sets with null values. This is the default. * Any non-numeric value is considered a null in this context. */ -define("STATS_REJECT_NULL", -1); +define('STATS_REJECT_NULL', -1); /** * STATS_IGNORE_NULL, ignore null values and prune them from the data. * Any non-numeric value is considered a null in this context. */ -define("STATS_IGNORE_NULL", -2); +define('STATS_IGNORE_NULL', -2); /** * STATS_USE_NULL_AS_ZERO, assign the value of 0 (zero) to null values. * Any non-numeric value is considered a null in this context. */ -define("STATS_USE_NULL_AS_ZERO", -3); +define('STATS_USE_NULL_AS_ZERO', -3); /*}}}*/ /** @@ -82,23 +82,23 @@ * can be epxressed more compactly as: * * <pre> - * $data2 = array("1"=>9, "2"=>8, "3"=>5, "4.1"=>2); + * $data2 = array('1'=>9, '2'=>8, '3'=>5, '4.1'=>2); * </pre> * * Example of use: * * <pre> - * include_once "Math/Stats.php"; + * include_once 'Math/Stats.php'; * $s = new Math_Stats(); * $s->setData($data1); * // or * // $s->setData($data2, STATS_DATA_CUMMULATIVE); * $stats = $s->calcBasic(); - * echo "Mean: ".$stats["mean"]." StDev: ".$stats["stdev"]." <br />\n"; + * echo 'Mean: '.$stats['mean'].' StDev: '.$stats['stdev'].' <br />\n'; * * // using data with nulls * // first ignoring them: - * $data3 = array(1.2, "foo", 2.4, 3.1, 4.2, 3.2, null, 5.1, 6.2); + * $data3 = array(1.2, 'foo', 2.4, 3.1, 4.2, 3.2, null, 5.1, 6.2); * $s->setNullOption(STATS_IGNORE_NULL); * $s->setData($data3); * $stats3 = $s->calcFull(); @@ -129,6 +129,15 @@ var $_data = null; /** + * Expanded data set. Only set when cummulative data + * is being used. Null by default. + * + * @access private + * @var array + */ + var $_dataExpanded = null; + + /** * Flag for data type, one of STATS_DATA_SIMPLE or * STATS_DATA_CUMMULATIVE. Null by default. * @@ -179,9 +188,10 @@ */ function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ if (!is_array($arr)) { - return PEAR::raiseError("invalid data, an array of numeric data was expected"); + return PEAR::raiseError('invalid data, an array of numeric data was expected'); } $this->_data = null; + $this->_dataExpanded = null; $this->_dataOption = null; $this->_calculatedValues = array(); if ($opt == STATS_DATA_SIMPLE) { @@ -190,6 +200,7 @@ } else if ($opt == STATS_DATA_CUMMULATIVE) { $this->_dataOption = $opt; $this->_data = $arr; + $this->_dataExpanded = array(); } return $this->_validate(); }/*}}}*/ @@ -199,13 +210,19 @@ * according to the current null handling options. * * @access public + * @param boolean $expanded whether to return a expanded list, default is false * @return mixed array of data on success, a PEAR_Error object otherwise * @see _validate() */ - function getData() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->_data; + function getData($expanded=false) {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if ($this->_dataOption == STATS_DATA_CUMMULATIVE && $expanded) { + return $this->_dataExpanded; + } else { + return $this->_data; + } }/*}}}*/ /** @@ -223,8 +240,8 @@ $this->_nullOption = $nullOption; return true; } else { - return PEAR::raiseError("invalid null handling option expecting: ". - "STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO"); + return PEAR::raiseError('invalid null handling option expecting: '. + 'STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO'); } }/*}}}*/ @@ -233,60 +250,22 @@ * * @access public * @param int $mode one of STATS_BASIC or STATS_FULL + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calcBasic() * @see calcFull() */ - function calc($mode) {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); + function calc($mode, $returnErrorObject=true) {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } if ($mode == STATS_BASIC) { - return array ( - "min" => $this->min(), - "max" => $this->max(), - "sum" => $this->sum(), - "sum2" => $this->sum2(), - "count" => $this->count(), - "mean" => $this->mean(), - "stdev" => $this->stDev(), - "variance" => $this->variance() - ); - } else if ($mode == STATS_FULL) { - return array ( - "min" => $this->min(), - "max" => $this->max(), - "sum" => $this->sum(), - "sum2" => $this->sum2(), - "count" => $this->count(), - "mean" => $this->mean(), - "median" => $this->median(), - "mode" => $this->mode(), - "midrange" => $this->midrange(), - "stdev" => $this->stDev(), - "absdev" => $this->absDev(), - "variance" => $this->variance(), - "std_error_of_mean" => $this->stdErrorOfMean(), - "skewness" => $this->skewness(), - "kurtosis" => $this->kurtosis(), - "coeff_of_variation" => $this->coeffOfVariation(), - "sample_central_moments" => array ( - 1 => $this->sampleCentralMoment(1), - 2 => $this->sampleCentralMoment(2), - 3 => $this->sampleCentralMoment(3), - 4 => $this->sampleCentralMoment(4), - 5 => $this->sampleCentralMoment(5) - ), - "sample_raw_moments" => array ( - 1 => $this->sampleRawMoment(1), - 2 => $this->sampleRawMoment(2), - 3 => $this->sampleRawMoment(3), - 4 => $this->sampleRawMoment(4), - 5 => $this->sampleRawMoment(5) - ), - "frequency" => $this->frequency() - ); + return $this->calcBasic($returnErrorObject); + } elseif ($mode == STATS_FULL) { + return $this->calcFull($returnErrorObject); } else { - return PEAR::raiseError("incorrect mode, expected STATS_BASIC or STATS_FULL"); + return PEAR::raiseError('incorrect mode, expected STATS_BASIC or STATS_FULL'); } }/*}}}*/ @@ -294,24 +273,77 @@ * Calculates a basic set of statistics * * @access public + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calc() * @see calcFull() */ - function calcBasic() {/*{{{*/ - return $this->calc(STATS_BASIC); + function calcBasic($returnErrorObject=true) {/*{{{*/ + return array ( + 'min' => $this->__format($this->min(), $returnErrorObject), + 'max' => $this->__format($this->max(), $returnErrorObject), + 'sum' => $this->__format($this->sum(), $returnErrorObject), + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), + 'count' => $this->__format($this->count(), $returnErrorObject), + 'mean' => $this->__format($this->mean(), $returnErrorObject), + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), + 'variance' => $this->__format($this->variance(), $returnErrorObject), + 'range' => $this->__format($this->range(), $returnErrorObject) + ); }/*}}}*/ /** * Calculates a full set of statistics * * @access public + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false), if an error happens. * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise * @see calc() * @see calcBasic() */ - function calcFull() {/*{{{*/ - return $this->calc(STATS_FULL); + function calcFull($returnErrorObject=true) {/*{{{*/ + return array ( + 'min' => $this->__format($this->min(), $returnErrorObject), + 'max' => $this->__format($this->max(), $returnErrorObject), + 'sum' => $this->__format($this->sum(), $returnErrorObject), + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), + 'count' => $this->__format($this->count(), $returnErrorObject), + 'mean' => $this->__format($this->mean(), $returnErrorObject), + 'median' => $this->__format($this->median(), $returnErrorObject), + 'mode' => $this->__format($this->mode(), $returnErrorObject), + 'midrange' => $this->__format($this->midrange(), $returnErrorObject), + 'geometric_mean' => $this->__format($this->geometricMean(), $returnErrorObject), + 'harmonic_mean' => $this->__format($this->harmonicMean(), $returnErrorObject), + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), + 'absdev' => $this->__format($this->absDev(), $returnErrorObject), + 'variance' => $this->__format($this->variance(), $returnErrorObject), + 'range' => $this->__format($this->range(), $returnErrorObject), + 'std_error_of_mean' => $this->__format($this->stdErrorOfMean(), $returnErrorObject), + 'skewness' => $this->__format($this->skewness(), $returnErrorObject), + 'kurtosis' => $this->__format($this->kurtosis(), $returnErrorObject), + 'coeff_of_variation' => $this->__format($this->coeffOfVariation(), $returnErrorObject), + 'sample_central_moments' => array ( + 1 => $this->__format($this->sampleCentralMoment(1), $returnErrorObject), + 2 => $this->__format($this->sampleCentralMoment(2), $returnErrorObject), + 3 => $this->__format($this->sampleCentralMoment(3), $returnErrorObject), + 4 => $this->__format($this->sampleCentralMoment(4), $returnErrorObject), + 5 => $this->__format($this->sampleCentralMoment(5), $returnErrorObject) + ), + 'sample_raw_moments' => array ( + 1 => $this->__format($this->sampleRawMoment(1), $returnErrorObject), + 2 => $this->__format($this->sampleRawMoment(2), $returnErrorObject), + 3 => $this->__format($this->sampleRawMoment(3), $returnErrorObject), + 4 => $this->__format($this->sampleRawMoment(4), $returnErrorObject), + 5 => $this->__format($this->sampleRawMoment(5), $returnErrorObject) + ), + 'frequency' => $this->__format($this->frequency(), $returnErrorObject), + 'quartiles' => $this->__format($this->quartiles(), $returnErrorObject), + 'interquartile_range' => $this->__format($this->interquartileRange(), $returnErrorObject), + 'quartile_deviation' => $this->__format($this->quartileDeviation(), $returnErrorObject), + 'quartile_variation_coefficient' => $this->__format($this->quartileVariationCoefficient(), $returnErrorObject) + ); }/*}}}*/ /** @@ -325,7 +357,7 @@ */ function min() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('min', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -349,7 +381,7 @@ */ function max() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('max', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -396,7 +428,7 @@ */ function sum2() {/*{{{*/ if (!array_key_exists('sum2', $this->_calculatedValues)) { - $sum2 = $this->sumN(1); + $sum2 = $this->sumN(2); if (PEAR::isError($sum2)) { return $sum2; } else { @@ -419,7 +451,7 @@ */ function sumN($n) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $sumN = 0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -465,7 +497,7 @@ */ function productN($n) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $prodN = 1.0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { @@ -497,13 +529,11 @@ */ function count() {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (!array_key_exists('count', $this->_calculatedValues)) { if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - foreach($this->_data as $freq) { - $count += $freq; - } + $count = count($this->_dataExpanded); } else { $count = count($this->_data); } @@ -538,6 +568,28 @@ }/*}}}*/ /** + * Calculates the range of the data set = max - min + * + * @access public + * @return mixed the value of the range on success, a PEAR_Error object otherwise. + */ + function range() { + if (!array_key_exists('range', $this->_calculatedValues)) { + $min = $this->min(); + if (PEAR::isError($min)) { + return $min; + } + $max = $this->max(); + if (PEAR::isError($max)) { + return $max; + } + $this->_calculatedValues['range'] = $max - $min; + } + return $this->_calculatedValues['range']; + + } + + /** * Calculates the variance (unbiased) of the data points in the set * Handles cummulative data sets correctly * @@ -690,12 +742,10 @@ return $this->_calculatedValues['skewness']; }/*}}}*/ - - // TODO : finish error handling and calc caching code /** * Calculates the kurtosis of the data distribution in the set * The kurtosis measures the degrees of peakedness of a distribution. - * It is also callesd the "excess" or "excess coefficient", and is + * It is also called the "excess" or "excess coefficient", and is * a normalized form of the fourth central moment of a distribution. * A normal distributions has kurtosis = 0 * A narrow and peaked (leptokurtic) distribution has a @@ -711,10 +761,22 @@ * @see calc() */ function kurtosis() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - $kurt = ($this->__sumdiff(4) / ($this->count() * pow($this->stDev(), 4))) - 3; - return $kurt; + if (!array_key_exists('kurtosis', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + $sumdiff4 = $this->__sumdiff(4); + if (PEAR::isError($sumdiff4)) { + return $sumdiff4; + } + $this->_calculatedValues['kurtosis'] = ($sumdiff4 / ($count * pow($stDev, 4))) - 3; + } + return $this->_calculatedValues['kurtosis']; }/*}}}*/ /** @@ -731,23 +793,28 @@ * @see calc() */ function median() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - $arr = array(); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) - foreach ($this->_data as $val=>$freq) - $arr = array_pad($arr, count($arr) + $freq, $val); - else - $arr = $this->_data; - sort($arr); - $n = count($arr); - $h = intval($n / 2); - if ($n % 2 == 0) { - $median = ($arr[$h] + $arr[$h - 1]) / 2; - } else { - $median = $arr[$h + 1]; + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); } - return $median; + if (!array_key_exists('median', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $arr =& $this->_dataExpanded; + } else { + $arr =& $this->_data; + } + $n = $this->count(); + if (PEAR::isError($n)) { + return $n; + } + $h = intval($n / 2); + if ($n % 2 == 0) { + $median = ($arr[$h] + $arr[$h - 1]) / 2; + } else { + $median = $arr[$h + 1]; + } + $this->_calculatedValues['median'] = $median; + } + return $this->_calculatedValues['median']; }/*}}}*/ /** @@ -762,27 +829,32 @@ * @see calc() */ function mode() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) - $arr = $this->_data; - else - $arr = $this->frequency(); - arsort($arr); - $mcount = 1; - foreach ($arr as $val=>$freq) { - if ($mcount == 1) { - $mode = array($val); - $mfreq = $freq; - $mcount++; - continue; - } - if ($mfreq == $freq) - $mode[] = $val; - if ($mfreq > $freq) - break; + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (!array_key_exists('mode', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $arr = $this->_data; + } else { + $arr = $this->frequency(); + } + arsort($arr); + $mcount = 1; + foreach ($arr as $val=>$freq) { + if ($mcount == 1) { + $mode = array($val); + $mfreq = $freq; + $mcount++; + continue; + } + if ($mfreq == $freq) + $mode[] = $val; + if ($mfreq > $freq) + break; + } + $this->_calculatedValues['mode'] = $mode; } - return $mode; + return $this->_calculatedValues['mode']; }/*}}}*/ /** @@ -797,25 +869,92 @@ * @see calc() */ function midrange() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return (($this->max() + $this->min()) / 2); + if (!array_key_exists('midrange', $this->_calculatedValues)) { + $min = $this->min(); + if (PEAR::isError($min)) { + return $min; + } + $max = $this->max(); + if (PEAR::isError($max)) { + return $max; + } + $this->_calculatedValues['midrange'] = (($max + $min) / 2); + } + return $this->_calculatedValues['midrange']; }/*}}}*/ - + + /** + * Calculates the geometrical mean of the data points in the set + * Handles cummulative data sets correctly + * + * @access public + * @return mixed the geometrical mean value on success, a PEAR_Error object otherwise + * @see calc() + * @see product() + * @see count() + */ function geometricMean() {/*{{{*/ - if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + if (!array_key_exists('geometricMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $prod = $this->product(); + if (PEAR::isError($prod)) { + return $prod; + } + if ($prod == 0.0) { + return 0.0; + } + if ($prod < 0) { + return PEAR::raiseError('The product of the data set is negative, geometric mean undefined.'); + } + $this->_calculatedValues['geometricMean'] = pow($prod , 1 / $count); } - $prod = $this->product(); - if (PEAR::isError($product)) { - return $product; + return $this->_calculatedValues['geometricMean']; + }/*}}}*/ + + /** + * Calculates the harmonic mean of the data points in the set + * Handles cummulative data sets correctly + * + * @access public + * @return mixed the harmonic mean value on success, a PEAR_Error object otherwise + * @see calc() + * @see count() + */ + function harmonicMean() {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); } - if ($product == 0.0) { - return 0.0; + if (!array_key_exists('harmonicMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $invsum = 0.0; + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + foreach($this->_data as $val=>$freq) { + if ($val == 0) { + return PEAR::raiseError('cannot calculate a '. + 'harmonic mean with data values of zero.'); + } + $invsum += $freq / $val; + } + } else { + foreach($this->_data as $val) { + if ($val == 0) { + return PEAR::raiseError('cannot calculate a '. + 'harmonic mean with data values of zero.'); + } + $invsum += 1 / $val; + } + } + $this->_calculatedValues['harmonicMean'] = $count / $invsum; } - return power($prod , -1 * $this->count()); + return $this->_calculatedValues['harmonicMean']; }/*}}}*/ - + /** * Calculates the nth central moment (m{n}) of a data set. * @@ -824,8 +963,16 @@ * m{n} = 1/N * SUM { (xi - avg)^n } * * where: N = sample size, avg = sample mean. + * + * @access public + * @param integer $n moment to calculate + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise */ function sampleCentralMoment($n) {/*{{{*/ + if (!is_int($n) || $n < 1) { + return PEAR::isError('moment must be a positive integer >= 1.'); + } + if ($n == 1) { return 0; } @@ -834,7 +981,8 @@ return $count; } if ($count == 0) { - return PEAR::raiseError("Cannot calculate {$n}th sample moment, there are zero data entries."); + return PEAR::raiseError("Cannot calculate {$n}th sample moment, ". + 'there are zero data entries'); } $sum = $this->__sumdiff($n); if (PEAR::isError($sum)) { @@ -851,14 +999,23 @@ * m{n} = 1/N * SUM { xi^n } * * where: N = sample size, avg = sample mean. + * + * @access public + * @param integer $n moment to calculate + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise */ function sampleRawMoment($n) {/*{{{*/ + if (!is_int($n) || $n < 1) { + return PEAR::isError('moment must be a positive integer >= 1.'); + } + $count = $this->count(); if (PEAR::isError($count)) { return $count; } if ($count == 0) { - return PEAR::raiseError("Cannot calculate {$n}th raw moment, there are zero data entries."); + return PEAR::raiseError("Cannot calculate {$n}th raw moment, ". + 'there are zero data entries.'); } $sum = $this->sumN($n); if (PEAR::isError($sum)) { @@ -867,28 +1024,6 @@ return ($sum / $count); }/*}}}*/ - /** - * Calculates the value frequency table of a data set. - * Handles cummulative data sets correctly - * - * @access public - * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise - * @see min() - * @see max() - * @see calc() - */ - function frequency() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - return $this->_data; - } else { - $freq = array(); - foreach ($this->_data as $val) - $freq["$val"]++; - return $freq; - } - }/*}}}*/ /** * Calculates the coefficient of variation of a data set. @@ -903,9 +1038,23 @@ * @see calc() */ function coeffOfVariation() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->stDev() / $this->mean(); + if (!array_key_exists('coeffOfVariation', $this->_calculatedValues)) { + $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + if ($mean == 0.0) { + return PEAR::raiseError('cannot calculate the coefficient '. + 'of variation, mean of sample is zero'); + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + + $this->_calculatedValues['coeffOfVariation'] = $stDev / $mean; + } + return $this->_calculatedValues['coeffOfVariation']; }/*}}}*/ /** @@ -926,12 +1075,210 @@ * @see calc() */ function stdErrorOfMean() {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - return $this->stDev() / sqrt($this->count()); + if (!array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + $stDev = $this->stDev(); + if (PEAR::isError($stDev)) { + return $stDev; + } + $this->_calculatedValues['stdErrorOfMean'] = $stDev / sqrt($count); + } + return $this->_calculatedValues['stdErrorOfMean']; }/*}}}*/ /** + * Calculates the value frequency table of a data set. + * Handles cummulative data sets correctly + * + * @access public + * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise + * @see min() + * @see max() + * @see calc() + */ + function frequency() {/*{{{*/ + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (!array_key_exists('frequency', $this->_calculatedValues)) { + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $freq = $this->_data; + } else { + $freq = array(); + foreach ($this->_data as $val) { + $freq["$val"]++; + } + ksort($freq); + } + $this->_calculatedValues['frequency'] = $freq; + } + return $this->_calculatedValues['frequency']; + }/*}}}*/ + + /** + * The quartiles are defined as the values that divide a sorted + * data set into four equal-sized subsets, and correspond to the + * 25th, 50th, and 75th percentiles. + * + * @access public + * @return mixed an associative array of quartiles on success, a PEAR_Error otherwise + * @see percentile() + */ + function quartiles() {/*{{{*/ + if (!array_key_exists('quartiles', $this->_calculatedValues)) { + $q1 = $this->percentile(25); + if (PEAR::isError($q1)) { + return $q1; + } + $q2 = $this->percentile(50); + if (PEAR::isError($q2)) { + return $q2; + } + $q3 = $this->percentile(75); + if (PEAR::isError($q3)) { + return $q3; + } + $this->_calculatedValues['quartiles'] = array ( + '25' => $q1, + '50' => $q2, + '75' => $q3 + ); + } + return $this->_calculatedValues['quartiles']; + }/*}}}*/ + + /** + * The interquartile range is the distance between the 75th and 25th + * percentiles. Basically the range of the middle 50% of the data set, + * and thus is not affected by outliers or extreme values. + * + * interquart range = P(75) - P(25) + * + * where: P = percentile + * + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + */ + function interquartileRange() {/*{{{*/ + if (!array_key_exists('interquartileRange', $this->_calculatedValues)) { + $quart = $this->quartiles(); + if (PEAR::isError($quart)) { + return $quart; + } + $q3 = $quart['75']; + $q1 = $quart['25']; + $this->_calculatedValues['interquartileRange'] = $q3 - $q1; + } + return $this->_calculatedValues['interquartileRange']; + }/*}}}*/ + + /** + * The quartile deviation is half of the interquartile range value + * + * quart dev = (P(75) - P(25)) / 2 + * + * where: P = percentile + * + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + * @see interquartileRange() + */ + function quartileDeviation() {/*{{{*/ + if (!array_key_exists('quartileDeviation', $this->_calculatedValues)) { + $iqr = $this->interquartileRange(); + if (PEAR::isError($iqr)) { + return $iqr; + } + $this->_calculatedValues['quartileDeviation'] = $iqr / 2; + } + return $this->_calculatedValues['quartileDeviation']; + }/*}}}*/ + + /** + * The quartile variation coefficient is the percent ratio of the + * interquartile range and the sum of the 75th and 25th percentiles: + * + * quart var coeff = 100 * (P(75) - P(25)) / (P(75) + P(25)) + * + * where: P = percentile + * + * @todo need to double check the equation + * @access public + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + */ + function quartileVariationCoefficient() {/*{{{*/ + if (!array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { + $quart = $this->quartiles(); + if (PEAR::isError($quart)) { + return $quart; + } + $q3 = $quart['75']; + $q1 = $quart['25']; + $d = $q3 - $q1; + $s = $q3 + $q1; + echo "** D = $d :: S = $s\n"; + $this->_calculatedValues['quartileVariationCoefficient'] = 100 * $d / $s; + } + return $this->_calculatedValues['quartileVariationCoefficient']; + }/*}}}*/ + + /** + * The pth percentile is the value such that p% of the a sorted data set + * is smaller than it, and (100 - p)% of the data is larger. + * + * A quick algorithm to pick the appropriate value from a sorted data + * set is as follows: + * + * - Count the number of values: n + * - Calculate the position of the value in the data list: i = p * (n + 1) + * - if i is an integer, return the data at that position + * - if i < 1, return the minimum of the data set + * - if i > n, return the maximum of the data set + * - otherwise, average the entries at adjacent positions to i + * + * The median is the 50th percentile value. + * + * @todo need to double check generality of the algorithm + * + * @access public + * @param numeric $p the percentile to estimate, e.g. 25 for 25th percentile + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see quartiles() + * @see median() + */ + function percentile($p) {/*{{{*/ + $count = $this->count(); + if (PEAR::isError($count)) { + return $count; + } + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + $data =& $this->_dataExpanded; + } else { + $data =& $this->_data; + } + $obsidx = $p * ($count + 1) / 100; + if (intval($obsidx) == $obsidx) { + return $data[($obsidx - 1)]; + } elseif ($obsidx < 1) { + return $data[0]; + } elseif ($obsidx > $count) { + return $data[($count - 1)]; + } else { + $left = floor($obsidx - 1); + $right = ceil($obsidx - 1); + return ($data[$left] + $data[$right]) / 2; + } + }/*}}}*/ + + // private methods + + /** * Utility function to calculate: SUM { (xi - mean)^n } * * @access private @@ -945,14 +1292,20 @@ * @see kurtosis(); */ function __sumdiff($power, $mean=null) {/*{{{*/ - if ($this->_data == null) - return PEAR::raiseError("data has not been set"); - if (is_null($mean)) + if ($this->_data == null) { + return PEAR::raiseError('data has not been set'); + } + if (is_null($mean)) { $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + } $sdiff = 0; if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { - foreach ($this->_data as $val=>$freq) + foreach ($this->_data as $val=>$freq) { $sdiff += $freq * pow((double)($val - $mean), (double)$power); + } } else { foreach ($this->_data as $val) $sdiff += pow((double)($val - $mean), (double)$power); @@ -964,7 +1317,7 @@ * Utility function to calculate the variance with or without * a fixed mean * - * @acess private + * @access private * @param $mean the fixed mean to use, null as default * @return mixed a numeric value on success, a PEAR_Error otherwise * @see variance() @@ -972,18 +1325,12 @@ */ function __calcVariance($mean = null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); - } - - if (is_null($mean)) { - $sumdiff2 = $this->__sumdiff(2); - } else { - $sumdiff2 = $this->__sumdiff(2, $mean); + return PEAR::raiseError('data has not been set'); } + $sumdiff2 = $this->__sumdiff(2, $mean); if (PEAR::isError($sumdiff2)) { return $sumdiff2; } - $count = $this->count(); if (PEAR::isError($count)) { return $count; @@ -994,24 +1341,30 @@ return ($sumdiff2 / ($count - 1)); }/*}}}*/ - function __calcAbsoluteDeviation($mean = null) { + /** + * Utility function to calculate the absolute deviation with or without + * a fixed mean + * + * @access private + * @param $mean the fixed mean to use, null as default + * @return mixed a numeric value on success, a PEAR_Error otherwise + * @see absDev() + * @see absDevWithMean() + */ + function __calcAbsoluteDeviation($mean = null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } $count = $this->count(); if (PEAR::isError($count)) { return $count; } - if (is_null) { - $sumabsdev = $this->__sumabsdev(); - } else { - $sumabsdev = $this->__sumabsdev($mean); - } + $sumabsdev = $this->__sumabsdev($mean); if (PEAR::isError($sumabsdev)) { return $sumabsdev; } - return sqrt($sumabsdev / $count); - } + return $sumabsdev / $count; + }/*}}}*/ /** * Utility function to calculate: SUM { | xi - mean | } @@ -1025,7 +1378,7 @@ */ function __sumabsdev($mean=null) {/*{{{*/ if ($this->_data == null) { - return PEAR::raiseError("data has not been set"); + return PEAR::raiseError('data has not been set'); } if (is_null($mean)) { $mean = $this->mean(); @@ -1044,6 +1397,26 @@ }/*}}}*/ /** + * Utility function to format a PEAR_Error to be used by calc(), + * calcBasic() and calcFull() + * + * @access private + * @param mixed $v value to be formatted + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), + * or only the error message will be returned (when false) + * @return mixed if the value is a PEAR_Error object, and $useErrorObject + * is false, then a string with the error message will be returned, + * otherwise the value will not be modified and returned as passed. + */ + function __format($v, $useErrorObject=true) { + if (PEAR::isError($v) && $useErrorObject == false) { + return $v->getMessage(); + } else { + return $v; + } + } + + /** * Utility function to validate the data and modify it * according to the current null handling option * @@ -1072,10 +1445,20 @@ break; case STATS_REJECT_NULL : default: - return PEAR::raiseError("data rejected, contains NULL values"); + return PEAR::raiseError('data rejected, contains NULL values'); break; } } + } + if ($flag) { + ksort($this->_data); + $this->_dataExpanded = array(); + foreach ($this->_data as $val=>$freq) { + $this->_dataExpanded = array_pad($this->_dataExpanded, count($this->_dataExpanded) + $freq, $val); + } + sort($this->_dataExpanded); + } else { + sort($this->_data); } return true; }/*}}}*/ Index: pear/Math_Stats/package.xml diff -u pear/Math_Stats/package.xml:1.8 pear/Math_Stats/package.xml:1.9 --- pear/Math_Stats/package.xml:1.8 Tue May 27 19:34:11 2003 +++ pear/Math_Stats/package.xml Sat May 31 23:08:12 2003 @@ -7,7 +7,7 @@ <name>Math_Stats</name> <summary>Classes to calculate statistical parameters</summary> <description> - Classes to calculate statistical parameters of numerical arrays + Package to calculate statistical parameters of numerical arrays of data. The data can be in a simple numerical array, or in a cummulative numerical array. A cummulative array, has the value as the index and the number of repeats as the value for the @@ -26,6 +26,51 @@ <release> <license>PHP</license> + <version>0.9.0</version> + <date>2003-06-01</date> + <notes> +<![CDATA[ +Many changes in the existing methods, and a number of new ones: + +- Added better error handling. +- Refactored existing methods to delegate calculations better +- Added value caching for invariant statistical parameters +- Added optional parameters to some existing methods to enhance their + processing or output. Preserve the original behaviour as default so + as not breack backwards compatibility +- Added new methods: + - range() + - geometricMean() + - harmonicMean() + - product() + - productN() + - percentile() + - quartiles() + - quartileDeviation() + - interquartileRange() + - quartileVariationCoefficient() +- Added a unit test class based on PHPUnit +]]> + </notes> + <state>beta</state> + <filelist> + <dir name="/" baseinstalldir="Math"> + <file role="php">Stats.php</file> + <dir name="examples" role="example"> + <file role="php">ex_stats_simple.php</file> + <file role="php">ex_stats_cummulative_data.php</file> + </dir> + <dir name="test" role="test"> + <file role="php">instance_methods.php</file> + <file role="php">instance_methods-data.php</file> + </dir> + <file role="doc">README-Math_Stats</file> + </dir> + </filelist> + </release> + <changelog> + <release> + <license>PHP</license> <version>0.8.5</version> <date>2003-05-27</date> <notes> @@ -38,18 +83,7 @@ pointing this problem out. </notes> <state>stable</state> - <filelist> - <dir name="/" baseinstalldir="Math"> - <file role="php">Stats.php</file> - <dir name="examples" role="example"> - <file role="php">ex_stats_simple.php</file> - <file role="php">ex_stats_cummulative_data.php</file> - </dir> - <file role="doc">README-Math_Stats</file> - </dir> - </filelist> </release> - <changelog> <release> <version>0.8.4</version> <date>2003-05-16</date> Index: pear/Math_Stats/test/instance_methods.php diff -u pear/Math_Stats/test/instance_methods.php:1.1 pear/Math_Stats/test/instance_methods.php:1.2 --- pear/Math_Stats/test/instance_methods.php:1.1 Sat May 31 01:02:30 2003 +++ pear/Math_Stats/test/instance_methods.php Sat May 31 23:08:13 2003 @@ -18,34 +18,802 @@ // // Matrix definition and manipulation package // -// $Id: instance_methods.php,v 1.1 2003/05/31 05:02:30 jmcastagnetto Exp $ +// $Id: instance_methods.php,v 1.2 2003/06/01 03:08:13 jmcastagnetto Exp $ // +/** + * Unit test for the Math_Stats class + * + * @package Math_Stats + */ + require_once 'PHPUnit.php'; -require_once 'Math/Stats.php'; -//require_once '../Stats.php'; +//require_once 'Math/Stats.php'; +require_once '../Stats.php'; +require_once './instance_methods-data.php'; + +define('__PRECISION', 12); +define('__DELTA', pow(10, -1 * (__PRECISION - 4))); + +// make sure we have the correct number of decimal figures +ini_set('precision', __PRECISION); + +/** + * Unit test class Math_Stats + * + */ +class Math_Stats_Unit_Test extends PHPUnit_TestCase {/*{{{*/ -class Math_Stats_Instance_Methods_Test extends PHPUnit_TestCase {/*{{{*/ - var $stats; - var $data = array (2,2.3,4.5,2,2,3.2,5.3,3,4,5,1,6); - var $datawithnulls = array (1.1650,null, "foo",0.6268, 0.6268, 0.0751, 0.3516, -0.6965); - var $cummdata = array("3"=>4, "2.333"=>5, "1.22"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); - var $cummdatawithnulls = array("3"=>4, "plink"=>2, "bar is not foo"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + var $s1; + var $s2a; + var $s2b; + var $s3; + var $s4a; + var $s4b; - function Math_Matrix_Instance_Methods_Test($name) { + // simple data set + var $data1 = array (2,2.3,4.5,2,2,3.2,5.3,3,4,5,1,6); + // data set with nulls + var $data2 = array (1.1650,null, "foo",0.6268, 0.6268, 0.0751, 0.3516, -0.6965); + // cummulative data set + var $data3 = array("3"=>4, "2.333"=>5, "1.22"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + // cummulative data set with nulls + var $data4 = array("3"=>4, "plink"=>2, "bar is not foo"=>6, "0.5"=>3, "0.9"=>2, "2.4"=>7); + + function Math_Matrix_Instance_Methods_Test($name) {/*{{{*/ $this->PHPUnit_TestCase($name); - } + }/*}}}*/ + + function setUp() {/*{{{*/ + $this->setLooselyTyped(true); + // simple data sets + $this->s1 = new Math_Stats(STATS_REJECT_NULL); + $this->s1->setData($this->data1); + $this->s2a = new Math_Stats(STATS_IGNORE_NULL); + $this->s2a->setData($this->data2); + $this->s2b = new Math_Stats(STATS_USE_NULL_AS_ZERO); + $this->s2b->setData($this->data2); + // cummulative data sets + $this->s3 = new Math_Stats(STATS_REJECT_NULL); + $this->s3->setData($this->data3, STATS_DATA_CUMMULATIVE); + $this->s4a = new Math_Stats(STATS_IGNORE_NULL); + $this->s4a->setData($this->data4, STATS_DATA_CUMMULATIVE); + $this->s4b = new Math_Stats(STATS_USE_NULL_AS_ZERO); + $this->s4b->setData($this->data4, STATS_DATA_CUMMULATIVE); + }/*}}}*/ + + function tearDown() {/*{{{*/ + unset($this->s1); + unset($this->s2a); + unset($this->s2b); + unset($this->s3); + unset($this->s4a); + unset($this->s4b); + }/*}}}*/ + + function testGetData() {/*{{{*/ + $this->assertEquals($GLOBALS['testGetData_out1'], $this->formatArray($this->s1->getData())); + $this->assertEquals($GLOBALS['testGetData_out2'], $this->formatArray($this->s2a->getData())); + $this->assertEquals($GLOBALS['testGetData_out3'], $this->formatArray($this->s2b->getData())); + $this->assertEquals($GLOBALS['testGetData_out4'], $this->formatArray($this->s3->getData())); + $this->assertEquals($GLOBALS['testGetData_out5'], $this->formatArray($this->s4a->getData())); + $this->assertEquals($GLOBALS['testGetData_out6'], $this->formatArray($this->s4a->getData(true))); + $this->assertEquals($GLOBALS['testGetData_out7'], $this->formatArray($this->s4b->getData())); + }/*}}}*/ + + function testCalcBasic() {/*{{{*/ + $this->assertEquals($GLOBALS['testCalcBasic_out1'], + $this->formatArray($this->s1->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out2'], + $this->formatArray($this->s2a->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out3'], + $this->formatArray($this->s2b->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out4'], + $this->formatArray($this->s3->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out5'], + $this->formatArray($this->s4a->calcBasic(false))); + $this->assertEquals($GLOBALS['testCalcBasic_out6'], + $this->formatArray($this->s4b->calcBasic(false))); + }/*}}}*/ + + function testCalcFull() {/*{{{*/ + $this->assertEquals($GLOBALS['testCalcFull_out1'], + $this->formatArray($this->s1->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out2'], + $this->formatArray($this->s2a->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out3'], + $this->formatArray($this->s2b->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out4'], + $this->formatArray($this->s3->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out5'], + $this->formatArray($this->s4a->calcFull(false))); + $this->assertEquals($GLOBALS['testCalcFull_out6'], + $this->formatArray($this->s4b->calcFull(false))); + }/*}}}*/ + + function testMin() {/*{{{*/ + $this->assertEquals($this->formatNumber(1), + $this->formatNumber($this->s1->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.6965), + $this->formatNumber($this->s2a->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.6965), + $this->formatNumber($this->s2b->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.5), + $this->formatNumber($this->s3->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.5), + $this->formatNumber($this->s4a->min()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->min()), + '', __DELTA); + }/*}}}*/ + + function testMax() {/*{{{*/ + $this->assertEquals($this->formatNumber(6), + $this->formatNumber($this->s1->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.165), + $this->formatNumber($this->s2a->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.165), + $this->formatNumber($this->s2b->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s3->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4a->max()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4b->max()), + '', __DELTA); + }/*}}}*/ + + function testSum() {/*{{{*/ + $this->assertEquals($this->formatNumber(40.3), + $this->formatNumber($this->s1->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.1488), + $this->formatNumber($this->s2a->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.1488), + $this->formatNumber($this->s2b->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(51.085), + $this->formatNumber($this->s3->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.1), + $this->formatNumber($this->s4a->sum()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.1), + $this->formatNumber($this->s4b->sum()), + '', __DELTA); + }/*}}}*/ + + function testSum2() {/*{{{*/ + $this->assertEquals($this->formatNumber(162.87), + $this->formatNumber($this->s1->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.7573563), + $this->formatNumber($this->s2a->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.7573563), + $this->formatNumber($this->s2b->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(114.834845), + $this->formatNumber($this->s3->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(78.69), + $this->formatNumber($this->s4a->sum2()), + '', __DELTA); + $this->assertEquals($this->formatNumber(78.69), + $this->formatNumber($this->s4b->sum2()), + '', __DELTA); + }/*}}}*/ + + function testSumN() {/*{{{*/ + $this->assertEquals($this->formatNumber(741.937), + $this->formatNumber($this->s1->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.77968786139), + $this->formatNumber($this->s2a->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.40141456571), + $this->formatNumber($this->s2b->sumN(4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(280.987388185), + $this->formatNumber($this->s3->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(206.601), + $this->formatNumber($this->s4a->sumN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(557.7429), + $this->formatNumber($this->s4b->sumN(4)), + '', __DELTA); + }/*}}}*/ + + function testProduct() {/*{{{*/ + $this->assertEquals($this->formatNumber(505543.68), + $this->formatNumber($this->s1->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.00841770739124), + $this->formatNumber($this->s2a->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(46478.287296), + $this->formatNumber($this->s3->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(544.32), + $this->formatNumber($this->s4a->product()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->product()), + '', __DELTA); + }/*}}}*/ + + function testProductN() {/*{{{*/ + $this->assertEquals($this->formatNumber(255574412388), + $this->formatNumber($this->s1->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(7.08577977246E-05), + $this->formatNumber($this->s2a->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->productN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(428617.299597), + $this->formatNumber($this->s3->productN(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(5714.053632), + $this->formatNumber($this->s4a->productN(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->productN(2)), + '', __DELTA); + }/*}}}*/ + + function testCount() {/*{{{*/ + $this->assertEquals($this->formatNumber(12), + $this->formatNumber($this->s1->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(6), + $this->formatNumber($this->s2a->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(8), + $this->formatNumber($this->s2b->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(27), + $this->formatNumber($this->s3->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(16), + $this->formatNumber($this->s4a->count()), + '', __DELTA); + $this->assertEquals($this->formatNumber(24), + $this->formatNumber($this->s4b->count()), + '', __DELTA); + }/*}}}*/ + + function testMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.35833333333), + $this->formatNumber($this->s1->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.358133333333), + $this->formatNumber($this->s2a->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.2686), + $this->formatNumber($this->s2b->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.89203703704), + $this->formatNumber($this->s3->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.00625), + $this->formatNumber($this->s4a->mean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.3375), + $this->formatNumber($this->s4b->mean()), + '', __DELTA); + }/*}}}*/ + + function testRange() {/*{{{*/ + $this->assertEquals($this->formatNumber(5), + $this->formatNumber($this->s1->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8615), + $this->formatNumber($this->s2a->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8615), + $this->formatNumber($this->s2b->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.5), + $this->formatNumber($this->s3->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.5), + $this->formatNumber($this->s4a->range()), + '', __DELTA); + $this->assertEquals($this->formatNumber(3), + $this->formatNumber($this->s4b->range()), + '', __DELTA); + }/*}}}*/ + + function testVariance() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.50265151515), + $this->formatNumber($this->s1->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.397559878667), + $this->formatNumber($this->s2a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.311455517143), + $this->formatNumber($this->s2b->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.699235883191), + $this->formatNumber($this->s3->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.952625), + $this->formatNumber($this->s4a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.55461956522), + $this->formatNumber($this->s4b->variance()), + '', __DELTA); + }/*}}}*/ + + function testStDev() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.58197709059), + $this->formatNumber($this->s1->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.630523495729), + $this->formatNumber($this->s2a->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.558081998583), + $this->formatNumber($this->s2b->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.836203254712), + $this->formatNumber($this->s3->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.976025102136), + $this->formatNumber($this->s4a->stDev()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.24684384155), + $this->formatNumber($this->s4b->stDev()), + '', __DELTA); + }/*}}}*/ - function setUp() { - $this->stats = new Math_Stats(); - } + function testVarianceWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.50454545455), + $this->formatNumber($this->s1->varianceWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.39966326), + $this->formatNumber($this->s2a->varianceWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.312582328571), + $this->formatNumber($this->s2b->varianceWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.699301730769), + $this->formatNumber($this->s3->varianceWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.952666666667), + $this->formatNumber($this->s4a->varianceWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.55869565217), + $this->formatNumber($this->s4b->varianceWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + function testStDevWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.58257557625), + $this->formatNumber($this->s1->stDevWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.632189259637), + $this->formatNumber($this->s2a->stDevWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.559090626439), + $this->formatNumber($this->s2b->stDevWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.836242626735), + $this->formatNumber($this->s3->stDevWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.976046446982), + $this->formatNumber($this->s4a->stDevWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.24847733346), + $this->formatNumber($this->s4b->stDevWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + + function testAbsDev() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.33472222222), + $this->formatNumber($this->s1->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.448066666667), + $this->formatNumber($this->s2a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.42395), + $this->formatNumber($this->s2b->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.754993141289), + $this->formatNumber($this->s3->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.84140625), + $this->formatNumber($this->s4a->variance()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.17395833333), + $this->formatNumber($this->s4b->variance()), + '', __DELTA); + }/*}}}*/ + + function testAbsDevWithMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.34166666667), + $this->formatNumber($this->s1->absDevWithMean(3.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.448066666667), + $this->formatNumber($this->s2a->absDevWithMean(0.4)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.42395), + $this->formatNumber($this->s2b->absDevWithMean(0.3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.753518518519), + $this->formatNumber($this->s3->absDevWithMean(1.9)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.84375), + $this->formatNumber($this->s4a->absDevWithMean(2.0)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.17916666667), + $this->formatNumber($this->s4b->absDevWithMean(1.4)), + '', __DELTA); + }/*}}}*/ + + function testSkewness() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.211767803758), + $this->formatNumber($this->s1->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.419944986921), + $this->formatNumber($this->s2a->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.0950243805522), + $this->formatNumber($this->s2b->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.321743512221), + $this->formatNumber($this->s3->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.578555878286), + $this->formatNumber($this->s4a->skewness()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.122636601767), + $this->formatNumber($this->s4b->skewness()), + '', __DELTA); + }/*}}}*/ + + function testKurtosis() {/*{{{*/ + $this->assertEquals($this->formatNumber(-1.47708896609), + $this->formatNumber($this->s1->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.23078655976), + $this->formatNumber($this->s2a->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.992375284912), + $this->formatNumber($this->s2b->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.4009978017), + $this->formatNumber($this->s3->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.4499405466), + $this->formatNumber($this->s4a->kurtosis()), + '', __DELTA); + $this->assertEquals($this->formatNumber(-1.8513180639), + $this->formatNumber($this->s4b->kurtosis()), + '', __DELTA); + }/*}}}*/ + + function testMedian() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.1), + $this->formatNumber($this->s1->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.4892), + $this->formatNumber($this->s2a->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.21335), + $this->formatNumber($this->s2b->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.333), + $this->formatNumber($this->s3->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4a->median()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.9), + $this->formatNumber($this->s4b->median()), + '', __DELTA); + }/*}}}*/ + + function testMode() {/*{{{*/ + $this->assertEquals($GLOBALS['testMode_out1'], $this->formatArray($this->s1->mode())); + $this->assertEquals($GLOBALS['testMode_out2'], $this->formatArray($this->s2a->mode())); + $this->assertEquals($GLOBALS['testMode_out3'], $this->formatArray($this->s2b->mode())); + $this->assertEquals($GLOBALS['testMode_out4'], $this->formatArray($this->s3->mode())); + $this->assertEquals($GLOBALS['testMode_out5'], $this->formatArray($this->s4a->mode())); + $this->assertEquals($GLOBALS['testMode_out6'], $this->formatArray($this->s4b->mode())); + }/*}}}*/ + + function testMidrange() {/*{{{*/ + $this->assertEquals($this->formatNumber(3.5), + $this->formatNumber($this->s1->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.23425), + $this->formatNumber($this->s2a->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.23425), + $this->formatNumber($this->s2b->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.75), + $this->formatNumber($this->s3->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.75), + $this->formatNumber($this->s4a->midrange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.5), + $this->formatNumber($this->s4b->midrange()), + '', __DELTA); + }/*}}}*/ + + function testGeometricMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.98753652642), + $this->formatNumber($this->s1->geometricMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s2a->geometricMean(),'PEAR_Error')); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s2b->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48888486575), + $this->formatNumber($this->s3->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48248699844), + $this->formatNumber($this->s4a->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0), + $this->formatNumber($this->s4b->geometricMean()), + '', __DELTA); + }/*}}}*/ + + function testHarmonicMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.60406264194), + $this->formatNumber($this->s1->geometricMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.319605399284), + $this->formatNumber($this->s2a->harmonicMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s2b->harmonicMean(),'PEAR_Error')); + $this->assertEquals($this->formatNumber(1.38224654591), + $this->formatNumber($this->s3->harmonicMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.28285077951), + $this->formatNumber($this->s4a->harmonicMean()), + '', __DELTA); + $this->assertTrue(is_a($this->s4b->harmonicMean(),'PEAR_Error')); + }/*}}}*/ + + function testSampleCentralMoment() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.29409722222), + $this->formatNumber($this->s1->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.331299898889), + $this->formatNumber($this->s2a->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.0165169209322), + $this->formatNumber($this->s2b->sampleCentralMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(-0.188124500214), + $this->formatNumber($this->s3->sampleCentralMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.8930859375), + $this->formatNumber($this->s4a->sampleCentralMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.48984375), + $this->formatNumber($this->s4b->sampleCentralMoment(2)), + '', __DELTA); + }/*}}}*/ + + function testSampleRawMoment() {/*{{{*/ + $this->assertEquals($this->formatNumber(13.5725), + $this->formatNumber($this->s1->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.459559383333), + $this->formatNumber($this->s2a->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.222460982673), + $this->formatNumber($this->s2b->sampleRawMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(10.4069403031), + $this->formatNumber($this->s3->sampleRawMoment(3)), + '', __DELTA); + $this->assertEquals($this->formatNumber(4.918125), + $this->formatNumber($this->s4a->sampleRawMoment(2)), + '', __DELTA); + $this->assertEquals($this->formatNumber(3.27875), + $this->formatNumber($this->s4b->sampleRawMoment(2)), + '', __DELTA); + }/*}}}*/ + + function testCoeffOfVariation() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.471060175858), + $this->formatNumber($this->s1->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.76058310423), + $this->formatNumber($this->s2a->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.07774385176), + $this->formatNumber($this->s2b->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.441959241993), + $this->formatNumber($this->s3->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.486492262747), + $this->formatNumber($this->s4a->coeffOfVariation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.932219694619), + $this->formatNumber($this->s4b->coeffOfVariation()), + '', __DELTA); + }/*}}}*/ + + function testStdErrorOfMean() {/*{{{*/ + $this->assertEquals($this->formatNumber(0.456677449552), + $this->formatNumber($this->s1->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.257410139229), + $this->formatNumber($this->s2a->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.197311782828), + $this->formatNumber($this->s2b->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.160927391402), + $this->formatNumber($this->s3->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.244006275534), + $this->formatNumber($this->s4a->stdErrorOfMean()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.254510933395), + $this->formatNumber($this->s4b->stdErrorOfMean()), + '', __DELTA); + }/*}}}*/ + + function testFrequency() {/*{{{*/ + $this->assertEquals($GLOBALS['testFrequency_out1'], $this->formatArray($this->s1->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out2'], $this->formatArray($this->s2a->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out3'], $this->formatArray($this->s2b->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out4'], $this->formatArray($this->s3->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out5'], $this->formatArray($this->s4a->frequency())); + $this->assertEquals($GLOBALS['testFrequency_out6'], $this->formatArray($this->s4b->frequency())); + }/*}}}*/ + + function testQuartiles() {/*{{{*/ + $this->assertEquals($GLOBALS['testQuartiles_out1'], $this->formatArray($this->s1->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out2'], $this->formatArray($this->s2a->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out3'], $this->formatArray($this->s2b->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out4'], $this->formatArray($this->s3->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out5'], $this->formatArray($this->s4a->quartiles())); + $this->assertEquals($GLOBALS['testQuartiles_out6'], $this->formatArray($this->s4b->quartiles())); + }/*}}}*/ + + function testInterquartileRange() {/*{{{*/ + $this->assertEquals($this->formatNumber(2.75), + $this->formatNumber($this->s1->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.2066), + $this->formatNumber($this->s2a->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6268), + $this->formatNumber($this->s2b->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.18), + $this->formatNumber($this->s3->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.8), + $this->formatNumber($this->s4a->interquartileRange()), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4b->interquartileRange()), + '', __DELTA); + }/*}}}*/ + + function testQuartileDeviation() {/*{{{*/ + $this->assertEquals($this->formatNumber(1.375), + $this->formatNumber($this->s1->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6033), + $this->formatNumber($this->s2a->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.3134), + $this->formatNumber($this->s2b->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.59), + $this->formatNumber($this->s3->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.9), + $this->formatNumber($this->s4a->quartileDeviation()), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.2), + $this->formatNumber($this->s4b->quartileDeviation()), + '', __DELTA); + }/*}}}*/ + + function testQuartileVariationCoefficient() {/*{{{*/ + /* + echo "quartileVariationCoefficient\n"; + echo $this->s1->quartileVariationCoefficient() ."\n"; + echo $this->s2a->quartileVariationCoefficient()."\n"; + echo $this->s2b->quartileVariationCoefficient()."\n"; + echo $this->s3->quartileVariationCoefficient() ."\n"; + echo $this->s4a->quartileVariationCoefficient()."\n"; + echo $this->s4b->quartileVariationCoefficient()."\n"; + */ + $this->assertEquals($this->formatNumber(40.7407407407), + $this->formatNumber($this->s1->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(206.185919344), + $this->formatNumber($this->s2a->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(100), + $this->formatNumber($this->s2b->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(32.5966850829), + $this->formatNumber($this->s3->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(50), + $this->formatNumber($this->s4a->quartileVariationCoefficient()), + '', __DELTA); + $this->assertEquals($this->formatNumber(100), + $this->formatNumber($this->s4b->quartileVariationCoefficient()), + '', __DELTA); + }/*}}}*/ + + function testPercentile() {/*{{{*/ + $this->assertEquals($this->formatNumber(2), + $this->formatNumber($this->s1->percentile(25)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.6268), + $this->formatNumber($this->s2a->percentile(60)), + '', __DELTA); + $this->assertEquals($this->formatNumber(0.8959), + $this->formatNumber($this->s2b->percentile(80)), + '', __DELTA); + $this->assertEquals($this->formatNumber(1.22), + $this->formatNumber($this->s3->percentile(25)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4a->percentile(60)), + '', __DELTA); + $this->assertEquals($this->formatNumber(2.4), + $this->formatNumber($this->s4b->percentile(80)), + '', __DELTA); + }/*}}}*/ + + function formatNumber($n) {/*{{{*/ + return (float) sprintf('%.'.(__PRECISION - 1), $n); }/*}}}*/ -$suite = new PHPUnit_TestSuite('Math_Stats_Instance_Methods_Test'); + function formatArray($arr,$spcs=0) {/*{{{*/ + $out = ''; + foreach ($arr as $key=>$val) { + $out .= str_repeat(" ",$spcs)."[$key : "; + if (is_array($val)) { + $out .= "\n".$this->formatArray($val, ($spcs + 1))."]\n"; + } else { + $out .= $val."]\n"; + } + } + return $out; + }/*}}}*/ + +}/*}}}*/ + +$suite = new PHPUnit_TestSuite('Math_Stats_Unit_Test'); $result = PHPUnit::run($suite); echo $result->toString()."\n"; // vim: ts=4:sw=4:et: -// vim6: fdl=0: fdm=marker: +// vim6: fdl=1: fdm=marker: ?>