source-function-F_getArrayStatistics

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_statistics.php
  4: // Begin       : 2008-12-25
  5: // Last Update : 2010-02-17
  6: //
  7: // Description : Functions to calculate descriptive statistics.
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               www.tecnick.com
 15: //               info@tecnick.com
 16: //
 17: // License:
 18: //    Copyright (C) 2004-2010 Nicola Asuni - Tecnick.com LTD
 19: //    See LICENSE.TXT file for more information.
 20: //============================================================+
 21: 
 22: /**
 23:  * @file
 24:  * Functions to calculate descriptive statistics.
 25:  * @package com.tecnick.tcexam.admin
 26:  * @author Nicola Asuni
 27:  * @since 2008-12-25
 28:  */
 29: 
 30: /**
 31:  * Return an array containing descriptive statistics for the bidimensional input array.
 32:  * @author Nicola Asuni
 33:  * @since 2008-12-25
 34:  * @param $data (array) input data as bidimesional array. The first dimension is a set of data, the second contains data.
 35:  * @return array of statistical results. The keys of the input data are peserved.
 36:  */
 37: function F_getArrayStatistics($data)
 38: {
 39:     $stats = array();
 40:     $stats['number'] = array(); // number of items
 41:     $stats['sum'] = array(); // sum of all elements
 42:     $stats['mean'] = array(); // mean or average value
 43:     $stats['median'] = array(); // median
 44:     $stats['mode'] = array(); // mode
 45:     $stats['minimum'] = array(); // minimum value
 46:     $stats['maximum'] = array(); // maximum value
 47:     $stats['range'] = array(); // range
 48:     $stats['variance'] = array(); // variance
 49:     $stats['standard_deviation'] = array(); // standard deviation
 50:     $stats['skewness'] = array(); // skewness
 51:     $stats['kurtosi'] = array(); // kurtosi
 52:     foreach ($data as $set => $dataset) {
 53:         sort($dataset);
 54:         $stats['number'][$set] = 0;
 55:         $stats['minimum'][$set] = $dataset[0];
 56:         $stats['sum'][$set] = 0;
 57:         $datastr = array();
 58:         foreach ($dataset as $num => $value) {
 59:             $stats['number'][$set]++;
 60:             $stats['sum'][$set] += $value;
 61:             $datastr[] = ''.$value.''; // convert value to string
 62:         }
 63:         if ($stats['number'][$set] > 0) {
 64:             $stats['maximum'][$set] = $dataset[($stats['number'][$set] - 1)];
 65:             $stats['range'][$set] = $stats['maximum'][$set] - $stats['minimum'][$set];
 66:             $stats['mean'][$set] = $stats['sum'][$set] / $stats['number'][$set];
 67:             if (($stats['number'][$set] % 2) == 0) {
 68:                 $stats['median'][$set] = ($dataset[($stats['number'][$set] / 2)] + $dataset[(($stats['number'][$set] / 2) - 1)]) / 2 ;
 69:             } else {
 70:                 $stats['median'][$set] = $dataset[(($stats['number'][$set] - 1) / 2)];
 71:             }
 72:             $freq = array_count_values($datastr);
 73:             arsort($freq, SORT_NUMERIC);
 74:             $freq = array_keys($freq);
 75:             $stats['mode'][$set] = floatval($freq[0]);
 76:             $dev = 0;
 77:             foreach ($dataset as $num => $value) {
 78:                 // deviance
 79:                 $dev += pow(($value - $stats['mean'][$set]), 2);
 80:             }
 81:             $stats['variance'][$set] = $dev / $stats['number'][$set];
 82:             $stats['standard_deviation'][$set] = sqrt($stats['variance'][$set]);
 83:             $stats['skewness'][$set] = 0;
 84:             $stats['kurtosi'][$set] = 0;
 85:             if ($stats['standard_deviation'][$set] != 0) {
 86:                 foreach ($dataset as $num => $value) {
 87:                     $tmpval = (($value - $stats['mean'][$set]) / $stats['standard_deviation'][$set]);
 88:                     $stats['skewness'][$set] += pow($tmpval, 3);
 89:                     $stats['kurtosi'][$set] += pow($tmpval, 4);
 90:                 }
 91:                 $stats['skewness'][$set] /= $stats['number'][$set];
 92:                 $stats['kurtosi'][$set] /= $stats['number'][$set];
 93:             }
 94:         }
 95:     }
 96:     return $stats;
 97: }
 98: 
 99: //============================================================+
100: // END OF FILE
101: //============================================================+
102: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy