Location: PHPKode > scripts > Stable > stable/stable.php
<?php

	/**
	 * Stable
	 *
	 * @todo
	 *  * pocitani prumeru a vsech ostatnich veci delat jiz pri pridavani dat
	 *  * optimalizovat funkci getTableInfo
	 * @version 0.2
	 * @author Tomas Lang
	 */
	class StatisticTable {
		private $_data;
		
		public function __construct() {
			$this->_data = array();
		}
		
		public function add( $value ) {
			$this->_data[] = $value;
		}
		
		public function getData() {
			return $this->_data;
		}
		
		public function clear() {
			$this->_data = array();
		}
		
		public function getSortedData() {
			$sortedData = array();
			$data = $this->_data;
			sort( $data );
			foreach ( $data as $item ) {
				if ( !array_key_exists( (string)$item, $sortedData ) ) {
					$sortedData[ (string)$item ] = 1;
				} else {
					$sortedData[ $item ]++;
				}
			}
			return $sortedData;
		}
		
		public function getTableInfo() {
			if ( count( $this->_data ) == 0 ) {
				return false;
			}
			
			$result = array();
			
			$sortedData = array();
			$data = $this->_data;
			sort( $data );
			$result[ 'min' ] = $data[ 0 ];
			$result[ 'max' ] = $data[ 0 ];
			$result[ 'arithmeticMean' ] = 0;
			$result[ 'geometricMean' ] = 0;
			$result[ 'quadraticMean' ] = 0;
			$result[ 'harmonicMean' ] = 0;
			$result[ 'median' ] = array();
			foreach ( $data as $item ) {
				if ( !array_key_exists( (string)$item, $sortedData ) ) {
					$sortedData[ (string)$item ] = 1;
					if ( $result[ 'median' ] == array() ) {
						$result[ 'median' ][ 'value' ] = $item;
						$result[ 'median' ][ 'count' ] = 1;
					}
				} else {
					$sortedData[ $item ]++;
					if ( $sortedData[ (string)$item ] > $result[ 'median' ][ 'count' ] ) {
						$result[ 'median' ][ 'value' ] = $item;
						$result[ 'median' ][ 'count' ] = $sortedData[ $item ];						
					}
				}
				
				if ( $item > $result[ 'max' ] ) {
					$result[ 'max' ] = $item;
				}
				if ( $item < $result[ 'min' ] ) {
					$result[ 'min' ] = $item;
				}
				
				$result[ 'arithmeticMean' ] += $item;
				if ( $item !=  0 ) {
					if ( $result[ 'geometricMean' ] == 0 ) {
						$result[ 'geometricMean' ] = 1;
					}
					$result[ 'geometricMean' ] *= $item;
					$result[ 'quadraticMean' ] += ( $item ^ 2 );
					$result[ 'harmonicMean' ] += 1 / $item;
				}
			}
			
			// aritmeticky prumer
			$result[ 'arithmeticMean' ] = $result[ 'arithmeticMean' ] / count( $data );
			// geometricky prumer
			if ( $result[ 'geometricMean' ] != 0 ) {
				$result[ 'geometricMean' ] = pow( $result[ 'geometricMean' ], 1 / count( $data ) );
			}
			// kvadraticky prumer
			$result[ 'quadraticMean' ] = ( 1 / sqrt( count( $data ) ) ) * sqrt( $result[ 'quadraticMean' ] );
			// harmonicky prumer
			if ( $result[ 'harmonicMean' ] != 0 ) {
				$result[ 'harmonicMean' ] = count( $data ) / $result[ 'harmonicMean' ]; 
			}
			// median
			$result[ 'median' ] = $result[ 'median' ][ 'value' ];
			// modus
			if ( count( $data ) % 2 == 0 ) {
				$result[ 'modus' ] = ( $data[ (int)( count( $data ) / 2 ) - 1 ] + $data[ (int)( count( $data ) / 2 ) ] ) / 2;
			} else {
				$result[ 'modus' ] = $data[ (int)( count( $data ) / 2 ) ];
			}
			// rozpeti; vyberove rozpeti
			$result[ 'expansion' ] = $result[ 'max' ] - $result[ 'min' ];
			
			// nema to byt middleDifference?
			$result[ 'middleDeviation' ] = 0;
			foreach ( $data as $item ) {
				$result[ 'middleDeviation' ] += abs( $item - $result[ 'arithmeticMean'] ) * $sortedData[ $item ];
			}
			$sum = 0;
			foreach ( $sortedData as $item ) {
				$sum += $item;
			}
			// stredni odchylka
			$result[ 'middleDeviation' ] = $result[ 'middleDeviation' ] / $sum;
			
			// nema to byt standartDifference?
			$result[ 'standartDeviation' ] = 0;
			foreach ( $data as $item ) {
				$result[ 'standartDeviation' ] += ( $item - $result[ 'arithmeticMean'] ) * ( $item - $result[ 'arithmeticMean'] ) * $sortedData[ $item ];
			}
			// smerodatna odchylka
			$result[ 'standartDeviation' ] = sqrt( $result[ 'standartDeviation' ] / ( $sum - 1 ) );
			
			// variacni koeficient
			$result[ 'coefficientOfVariation'] = ( $result[ 'standartDeviation' ] / $result[ 'arithmeticMean'] ) * 100;
			
			return $result;
		}
	}

?>
Return current item: Stable