cvs: pear /Math_Stats Stats.php package.xml /Math_Stats/test basic_stats.oct instance_methods.php

From: Date: Sun, 01 Jun 2003 09:44:22 +0000
Subject: cvs: pear /Math_Stats Stats.php package.xml /Math_Stats/test basic_stats.oct instance_methods.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-10637@lists.php.net to get a copy of this message
jmcastagnetto Sun Jun 1 05:44:22 2003 EDT Added files: /pear/Math_Stats/test basic_stats.oct Modified files: /pear/Math_Stats Stats.php package.xml /pear/Math_Stats/test instance_methods.php Log: Added studentize() and center() to transform data Updated unit testing class Added an octave script to test against the results from Math_Stats

Index: pear/Math_Stats/Stats.php diff -u pear/Math_Stats/Stats.php:1.13 pear/Math_Stats/Stats.php:1.14 --- pear/Math_Stats/Stats.php:1.13 Sat May 31 23:08:12 2003 +++ pear/Math_Stats/Stats.php Sun Jun 1 05:44:21 2003 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Stats.php,v 1.13 2003/06/01 03:08:12 jmcastagnetto Exp $ +// $Id: Stats.php,v 1.14 2003/06/01 09:44:21 jmcastagnetto Exp $ // include_once 'PEAR.php'; @@ -246,6 +246,73 @@ }/*}}}*/ /** + * Transforms the data by substracting each entry from the mean and + * dividing by its standard deviation. This will reset all pre-calculated + * values to their original (unset) defaults. + * + * @access public + * @return mixed true on success, a PEAR_Error object otherwise + * @see mean() + * @see stDev() + * @see setData() + */ + function studentize() {/*{{{*/ + $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + $std = $this->stDev(); + if (PEAR::isError($std)) { + return $std; + } + if ($std == 0) { + return PEAR::raiseError('cannot studentize data, standard deviation is zero.'); + } + $arr = array(); + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + foreach ($this->_data as $val=>$freq) { + $newval = ($val - $mean) / $std; + $arr["$newval"] = $freq; + } + } else { + foreach ($this->_data as $val) { + $newval = ($val - $mean) / $std; + $arr[] = $newval; + } + } + return $this->setData($arr, $this->_dataOption); + }/*}}}*/ + + /** + * Transforms the data by substracting each entry from the mean. + * This will reset all pre-calculated values to their original (unset) defaults. + * + * @access public + * @return mixed true on success, a PEAR_Error object otherwise + * @see mean() + * @see setData() + */ + function center() {/*{{{*/ + $mean = $this->mean(); + if (PEAR::isError($mean)) { + return $mean; + } + $arr = array(); + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { + foreach ($this->_data as $val=>$freq) { + $newval = $val - $mean; + $arr["$newval"] = $freq; + } + } else { + foreach ($this->_data as $val) { + $newval = $val - $mean; + $arr[] = $newval; + } + } + return $this->setData($arr, $this->_dataOption); + }/*}}}*/ + + /** * Calculates the basic or full statistics for the data set * * @access public @@ -573,7 +640,7 @@ * @access public * @return mixed the value of the range on success, a PEAR_Error object otherwise. */ - function range() { + function range() {/*{{{*/ if (!array_key_exists('range', $this->_calculatedValues)) { $min = $this->min(); if (PEAR::isError($min)) { @@ -587,7 +654,7 @@ } return $this->_calculatedValues['range']; - } + }/*}}}*/ /** * Calculates the variance (unbiased) of the data points in the set @@ -1222,7 +1289,6 @@ $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']; @@ -1408,13 +1474,13 @@ * 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) { + 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 Index: pear/Math_Stats/package.xml diff -u pear/Math_Stats/package.xml:1.9 pear/Math_Stats/package.xml:1.10 --- pear/Math_Stats/package.xml:1.9 Sat May 31 23:08:12 2003 +++ pear/Math_Stats/package.xml Sun Jun 1 05:44:21 2003 @@ -39,6 +39,8 @@ processing or output. Preserve the original behaviour as default so as not breack backwards compatibility - Added new methods: + - center() + - studentize() - range() - geometricMean() - harmonicMean() @@ -49,7 +51,7 @@ - quartileDeviation() - interquartileRange() - quartileVariationCoefficient() -- Added a unit test class based on PHPUnit +- Added a unit test class based on PEAR::PHPUnit ]]> </notes> <state>beta</state> Index: pear/Math_Stats/test/instance_methods.php diff -u pear/Math_Stats/test/instance_methods.php:1.2 pear/Math_Stats/test/instance_methods.php:1.3 --- pear/Math_Stats/test/instance_methods.php:1.2 Sat May 31 23:08:13 2003 +++ pear/Math_Stats/test/instance_methods.php Sun Jun 1 05:44:21 2003 @@ -18,7 +18,7 @@ // // Matrix definition and manipulation package // -// $Id: instance_methods.php,v 1.2 2003/06/01 03:08:13 jmcastagnetto Exp $ +// $Id: instance_methods.php,v 1.3 2003/06/01 09:44:21 jmcastagnetto Exp $ // /** @@ -791,6 +791,32 @@ '', __DELTA); }/*}}}*/ + function testStudentize() {/*{{{*/ + $this->s1->studentize(); + $this->assertEquals($GLOBALS['testStudentize_out1'], + $this->formatArray($this->s1->getData())); + $this->assertEquals($GLOBALS['testStudentize_out2'], + $this->formatArray($this->s1->calcFull(false))); + $this->s3->studentize(); + $this->assertEquals($GLOBALS['testStudentize_out3'], + $this->formatArray($this->s3->getData())); + $this->assertEquals($GLOBALS['testStudentize_out4'], + $this->formatArray($this->s3->calcFull(false))); + }/*}}}*/ + + function testCenter() {/*{{{*/ + $this->s1->center(); + $this->assertEquals($GLOBALS['testCenter_out1'], + $this->formatArray($this->s1->getData())); + $this->assertEquals($GLOBALS['testCenter_out2'], + $this->formatArray($this->s1->calcFull(false))); + $this->s3->center(); + $this->assertEquals($GLOBALS['testCenter_out3'], + $this->formatArray($this->s3->getData())); + $this->assertEquals($GLOBALS['testCenter_out4'], + $this->formatArray($this->s3->calcFull(false))); + }/*}}}*/ + function formatNumber($n) {/*{{{*/ return (float) sprintf('%.'.(__PRECISION - 1), $n); }/*}}}*/ @@ -812,7 +838,7 @@ $suite = new PHPUnit_TestSuite('Math_Stats_Unit_Test'); $result = PHPUnit::run($suite); -echo $result->toString()."\n"; +echo $result->toString(); // vim: ts=4:sw=4:et: // vim6: fdl=1: fdm=marker: Index: pear/Math_Stats/test/basic_stats.oct +++ pear/Math_Stats/test/basic_stats.oct # # Octave script to calculate basic statistics of a data vector # # $Id: basic_stats.oct,v 1.1 2003/06/01 09:44:21 jmcastagnetto Exp $ # disp("Statistics using octave") disp("-----------------------\n") data = [2,2.3,4.5,2,2,3.2,5.3,3,4,5,1,6] disp(sprintf("mean = %.14f", mean(data))) disp(sprintf("geometric mean = %.14f", mean(data, 'g'))) disp(sprintf("harmonic mean = %.14f", mean(data, 'h'))) disp(sprintf("median = %.14f", median(data))) disp(sprintf("stDev = %.14f", std(data))) disp(sprintf("variance = %.14f", var(data))) disp(sprintf("kurtosis = %.14f", kurtosis(data))) disp(sprintf("skewness = %.14f", skewness(data))) disp(sprintf("range = %.14f", range(data))) disp(sprintf("moment(2) = %.14f", moment(data, 2))) disp(sprintf("moment(3) = %.14f", moment(data, 3))) disp(sprintf("moment(4) = %.14f", moment(data, 4))) disp(sprintf("interquartile range = %.14f", iqr(data))) disp(sprintf("mean square = %.14f", meansq(data))) s = statistics(data) disp(sprintf("min = %.14f", s(1))) disp(sprintf("quartile(1) = %.14f", s(2))) disp(sprintf("median (quartile(2)) = %.14f", s(3))) disp(sprintf("quartile(3) = %.14f", s(4))) disp(sprintf("max = %.14f", s(5))) disp(sprintf("mean = %.14f", s(6))) disp(sprintf("std dev = %.14f", s(7))) disp(sprintf("skewness = %.14f", s(8))) disp(sprintf("kurtosis = %.14f", s(9))) disp("\n* Studentize the data") newdata = studentize(data) s = statistics(newdata) disp(sprintf("min = %.14f", s(1))) disp(sprintf("quartile(1) = %.14f", s(2))) disp(sprintf("median (quartile(2)) = %.14f", s(3))) disp(sprintf("quartile(3) = %.14f", s(4))) disp(sprintf("max = %.14f", s(5))) disp(sprintf("mean = %.14f", s(6))) disp(sprintf("std dev = %.14f", s(7))) disp(sprintf("skewness = %.14f", s(8))) disp(sprintf("kurtosis = %.14f", s(9))) disp("\n* Center the data") newdata = center(data) s = statistics(newdata) disp(sprintf("min = %.14f", s(1))) disp(sprintf("quartile(1) = %.14f", s(2))) disp(sprintf("median (quartile(2)) = %.14f", s(3))) disp(sprintf("quartile(3) = %.14f", s(4))) disp(sprintf("max = %.14f", s(5))) disp(sprintf("mean = %.14f", s(6))) disp(sprintf("std dev = %.14f", s(7))) disp(sprintf("skewness = %.14f", s(8))) disp(sprintf("kurtosis = %.14f", s(9)))
« previous php.pear.cvs (#10637) next »