Source for file StatisticsHandler.class.php

Documentation is available at StatisticsHandler.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI.'utilities/StatisticsHandler.interface.php');
  4. /**
  5. * An interface to calculate various statistical information.
  6. *
  7. * @package harmoni.utilities
  8. *
  9. * @copyright Copyright &copy; 2005, Middlebury College
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  11. *
  12. * @version $Id: StatisticsHandler.class.php,v 1.4 2007/09/04 20:25:54 adamfranco Exp $
  13. */
  14. class StatisticsHandler {
  15.  
  16. var $_data;
  17. var $_sortedData;
  18.  
  19. var $_mean, $_median, $_discrimination, $_standardDeviation, $_max, $_min;
  20.  
  21. /**
  22. * Create a Add a handler with given information.
  23. * @param object $data The object, which contains all the data. It should be provided with methods: next(), getNext() and getSize().
  24. * @return boolean Wheter all data is numeric.
  25. * @access public
  26. */
  27.  
  28. function StatisticsHandler($data) {
  29. if(is_array($data)){
  30. $this->_data = $data;
  31. }
  32. else{
  33. $this->_data = array();
  34. while($data->hasNext())
  35. $this->_data[] = $data->next();
  36. }
  37. return $this->_checkdata();
  38. }
  39.  
  40. /**
  41. * Return the mean value of the data.
  42. * @return float The mean value of the data.
  43. * @access public
  44. */
  45. function getMean() {
  46. if(!isset($this->_mean)){
  47. $sum = 0;
  48. foreach($this->_data as $value)
  49. $sum += $value;
  50.  
  51. $sum = round($sum/count($this->_data),3);
  52. $this->_mean = $sum;
  53. }
  54.  
  55. return $this->_mean;
  56. }
  57.  
  58. /**
  59. * Return the median value of the data.
  60. * @return float The median value of the data.
  61. * @access public
  62. */
  63. function getMedian() {
  64. if(!isset($this->_median)){
  65. if(!isset($this->_sortedData)){
  66. $this->_sortedData = $this->_data;
  67. sort($this->_sortedData);
  68. }
  69. $this->_median = $this->_sortedData[count($this->_sortedData)/2];
  70. }
  71.  
  72. return $this->_median;
  73. }
  74.  
  75.  
  76. /**
  77. * Return the standard deviation of the data.
  78. * @return float The standard deviation of the data.
  79. * @access public
  80. */
  81. function getStandardDeviation() {
  82. if(!isset($this->_standardDeviation)) {
  83. $mean = $this->getMean();
  84. $variance = 0;
  85. foreach($this->_data as $value)
  86. $variance += ($mean-$value)*($mean-$value);
  87.  
  88. $this->_standardDeviation = round(sqrt($variance/count($this->_data)),3);
  89. }
  90.  
  91. return $this->_standardDeviation;
  92. }
  93.  
  94. /**
  95. * Return the modal (most frequent value) of the data.
  96. * @return float The modal (most frequent value) of the data.
  97. * @access public
  98. */
  99. function getModal() {
  100. if (!isset($this->_modal)) {
  101. $cf=0; $mf=0; $current = '';
  102. foreach ($this->_data as $value) {
  103. if ($value == $current)
  104. $cf++;
  105. else {
  106. if ($cf>$mf){
  107. $mf = $cf;
  108. $mv = $current;
  109. }
  110. $current = $value;
  111. $cf = 1;
  112. }
  113. }
  114. if ($cf>$mf)
  115. $mv = $cv;
  116. $this->_modal = $mv;
  117. }
  118. return $this->_modal;
  119. }
  120.  
  121. /**
  122. * Return the maximum value of the data.
  123. * @return float The maximum value of the data.
  124. * @access public
  125. */
  126. function getMax() {
  127. if(!isset($this->_max)){
  128. if(!isset($this->_sortedData)){
  129. $this->_sortedData = $this->_data;
  130. sort($this->_sortedData);
  131. }
  132. $this->_max = $this->_sortedData[count($this->_sortedData)-1];
  133. $this->_min = $this->_sortedData[0];
  134. }
  135.  
  136. return $this->_max;
  137. }
  138.  
  139. /**
  140. * Return the minimum value of the data.
  141. * @return float The minimum value of the data.
  142. * @access public
  143. */
  144. function getMin() {
  145. if(!isset($this->_min)){
  146. if(!isset($this->_sortedData)){
  147. $this->_sortedData = $this->_data;
  148. sort($this->_sortedData);
  149. }
  150. $this->_max = $this->_sortedData[count($this->_sortedData)-1];
  151. $this->_min = $this->_sortedData[0];
  152. }
  153.  
  154. return $this->_min;
  155. }
  156.  
  157. /**
  158. * Return the discrimination value of the data. Discrimination is the difference between
  159. * the mean value of the top 27% and the mean value of the bottom 27% calculated separately. In the case of test results
  160. * the discrimination ranges from 0 to 100, so divide the result by 100 before printing.
  161. * @return float The discrimination value of the data.
  162. * @access public
  163. */
  164. function getDiscrimination() {
  165. if(!isset($this->_discrimination)){
  166. if(!isset($this->_sortedData)){
  167. $this->_sortedData = $this->_data;
  168. sort($this->_sortedData);
  169. }
  170.  
  171. $sum1 = 0;
  172. $sum2 = 0;
  173.  
  174. $dataSize = count($this->_data);
  175. $size = round($dataSize*27.0/100);
  176.  
  177. for($ii=0;$ii<$size;$ii++){
  178. $sum1 += $this->_sortedData[$ii];
  179. $sum2 += $this->_sortedData[$dataSize-$ii-1];
  180. }
  181. $sum1 /= $size;
  182. $sum2 /= $size;
  183.  
  184. $this->_discrimination = round($sum2 - $sum1,3);
  185. }
  186.  
  187. return $this->_discrimination;
  188. }
  189.  
  190. /**
  191. * Return the Discrimination of a different set in accordance to the base set.
  192. * The function will take the results of the original, calculate who got the top 27% of them and see how these compare
  193. * to the results of the second data. The same will be done for the bottom 27%, after which the results will be substracted.
  194. * This should be used if the original data is the overall results of the test and the second set is the results of one particular question.
  195. * @param object $secondData The data to get the discrimination from. It should have the same number of elements as the original data and
  196. * The values should be in the same order as the original.
  197. * Clearly $secondData should either be an array or also have next() and hasNext().
  198. * @return float The discrimination of the second set in accordance with the first.
  199. * @access public
  200. */
  201. function getSecondaryDiscrimination($secondData) {
  202. if (is_array($secondData))
  203. $second = $secondData;
  204. else {
  205. $second = array();
  206. while($secondData->hasNext())
  207. $second[] = $secondData->next();
  208. }
  209.  
  210. $first = $this->_data;
  211.  
  212. array_multisort($first,$second);
  213.  
  214. $sum1 = 0;
  215. $sum2 = 0;
  216.  
  217. $dataSize = count($second);
  218. $size = round($dataSize*27.0/100);
  219.  
  220. for($ii=0;$ii<$size;$ii++){
  221. $sum1 += $second[$ii];
  222. $sum2 += $second[$dataSize-$ii-1];
  223. }
  224.  
  225. $sum1 /= $size;
  226. $sum2 /= $size;
  227.  
  228. return round($sum2 - $sum1,3);
  229. }
  230.  
  231. /**
  232. * Check if all the data is numerically interpretable (only contains numbers and numeric strings)
  233. * @return boolean Whether all data is float interpretable.
  234. * @access private
  235. */
  236. function _checkdata() {
  237. foreach ($this->_data as $value)
  238. if(!is_numeric($value))
  239. return false;
  240. return true;
  241. }
  242. }
  243.  
  244.  
  245.  
  246. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:42 -0400 by phpDocumentor 1.3.0RC3