MediaWiki
REL1_24
|
00001 <?php 00006 class RunningStatTest extends MediaWikiTestCase { 00007 00008 public $points = array( 00009 49.7168, 74.3804, 7.0115, 96.5769, 34.9458, 00010 36.9947, 33.8926, 89.0774, 23.7745, 73.5154, 00011 86.1322, 53.2124, 16.2046, 73.5130, 10.4209, 00012 42.7299, 49.3330, 47.0215, 34.9950, 18.2914, 00013 ); 00014 00024 public function testRunningStatAccuracy() { 00025 $rstat = new RunningStat(); 00026 foreach( $this->points as $point ) { 00027 $rstat->push( $point ); 00028 } 00029 00030 $mean = array_sum( $this->points ) / count( $this->points ); 00031 $variance = array_sum( array_map( function ( $x ) use ( $mean ) { 00032 return pow( $mean - $x, 2 ); 00033 }, $this->points ) ) / ( count( $rstat ) - 1 ); 00034 $stddev = sqrt( $variance ); 00035 00036 $this->assertEquals( count( $rstat ), count( $this->points ) ); 00037 $this->assertEquals( $rstat->min, min( $this->points ) ); 00038 $this->assertEquals( $rstat->max, max( $this->points ) ); 00039 $this->assertEquals( $rstat->getMean(), $mean ); 00040 $this->assertEquals( $rstat->getVariance(), $variance ); 00041 $this->assertEquals( $rstat->getStdDev(), $stddev ); 00042 } 00043 00051 public function testRunningStatMerge() { 00052 $expected = new RunningStat(); 00053 00054 foreach( $this->points as $point ) { 00055 $expected->push( $point ); 00056 } 00057 00058 // Split the data into two sets 00059 $sets = array_chunk( $this->points, floor( count( $this->points ) / 2 ) ); 00060 00061 // Accumulate the first half into one RunningStat object 00062 $first = new RunningStat(); 00063 foreach( $sets[0] as $point ) { 00064 $first->push( $point ); 00065 } 00066 00067 // Accumulate the second half into another RunningStat object 00068 $second = new RunningStat(); 00069 foreach( $sets[1] as $point ) { 00070 $second->push( $point ); 00071 } 00072 00073 // Merge the second RunningStat object into the first 00074 $first->merge( $second ); 00075 00076 $this->assertEquals( count( $first ), count( $this->points ) ); 00077 $this->assertEquals( $first, $expected ); 00078 } 00079 }