| @@ 1077-1102 (lines=26) @@ | ||
| 1074 | * @param integer $n moment to calculate |
|
| 1075 | * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
| 1076 | */ |
|
| 1077 | public function sampleCentralMoment($n) |
|
| 1078 | { |
|
| 1079 | if (!is_int($n) || $n < 1) { |
|
| 1080 | throw new \PEAR_Exception('moment must be a positive integer >= 1.'); |
|
| 1081 | } |
|
| 1082 | ||
| 1083 | if ($n == 1) { |
|
| 1084 | return 0; |
|
| 1085 | } |
|
| 1086 | try { |
|
| 1087 | $count = $this->count(); |
|
| 1088 | } catch (\PEAR_Exception $e) { |
|
| 1089 | return $count; |
|
| 1090 | } |
|
| 1091 | if ($count == 0) { |
|
| 1092 | throw new \PEAR_Exception("Cannot calculate {$n}th sample moment, " . |
|
| 1093 | 'there are zero data entries'); |
|
| 1094 | } |
|
| 1095 | try { |
|
| 1096 | $sum = $this->__sumdiff($n); |
|
| 1097 | } catch (\PEAR_Exception $e) { |
|
| 1098 | return $sum; |
|
| 1099 | } |
|
| 1100 | return ($sum / $count); |
|
| 1101 | } |
|
| 1102 | ||
| 1103 | /** |
|
| 1104 | * Calculates the nth raw moment (m{n}) of a data set. |
|
| 1105 | * |
|
| @@ 1116-1138 (lines=23) @@ | ||
| 1113 | * @param integer $n moment to calculate |
|
| 1114 | * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
| 1115 | */ |
|
| 1116 | public function sampleRawMoment($n) |
|
| 1117 | { |
|
| 1118 | if (!is_int($n) || $n < 1) { |
|
| 1119 | throw new \PEAR_Exception('moment must be a positive integer >= 1.'); |
|
| 1120 | } |
|
| 1121 | ||
| 1122 | try { |
|
| 1123 | $count = $this->count(); |
|
| 1124 | } catch (\PEAR_Exception $e) { |
|
| 1125 | return $count; |
|
| 1126 | } |
|
| 1127 | if ($count == 0) { |
|
| 1128 | throw new \PEAR_Exception("Cannot calculate {$n}th raw moment, " . |
|
| 1129 | 'there are zero data entries.'); |
|
| 1130 | } |
|
| 1131 | try { |
|
| 1132 | $sum = $this->sumN($n); |
|
| 1133 | } catch (\PEAR_Exception $e) { |
|
| 1134 | return $sum; |
|
| 1135 | } |
|
| 1136 | return ($sum / $count); |
|
| 1137 | } |
|
| 1138 | ||
| 1139 | /** |
|
| 1140 | * Calculates the coefficient of variation of a data set. |
|
| 1141 | * The coefficient of variation measures the spread of a set of data |
|