| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 7 |
|
| 8 |
class Deviations |
| 9 |
{ |
| 10 |
/** |
| 11 |
* DEVSQ. |
| 12 |
* |
| 13 |
* Returns the sum of squares of deviations of data points from their sample mean. |
| 14 |
* |
| 15 |
* Excel Function: |
| 16 |
* DEVSQ(value1[,value2[, ...]]) |
| 17 |
* |
| 18 |
* @param mixed ...$args Data values |
| 19 |
* @return string|float |
| 20 |
*/ |
| 21 |
public static function sumSquares(...$args) |
| 22 |
{ |
| 23 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 24 |
|
| 25 |
$aMean = Averages::average($aArgs); |
| 26 |
if (!is_numeric($aMean)) { |
| 27 |
return ExcelError::NAN(); |
| 28 |
} |
| 29 |
|
| 30 |
// Return value |
| 31 |
$returnValue = 0.0; |
| 32 |
$aCount = -1; |
| 33 |
foreach ($aArgs as $k => $arg) { |
| 34 |
// Is it a numeric value? |
| 35 |
if ( |
| 36 |
(is_bool($arg)) |
| 37 |
&& ((!Functions::isCellValue($k)) |
| 38 |
|| (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE)) |
| 39 |
) { |
| 40 |
$arg = (int) $arg; |
| 41 |
} |
| 42 |
if ((is_numeric($arg)) && (!is_string($arg))) { |
| 43 |
$returnValue += ($arg - $aMean) ** 2; |
| 44 |
++$aCount; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $aCount === 0 ? ExcelError::VALUE() : $returnValue; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* KURT. |
| 53 |
* |
| 54 |
* Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness |
| 55 |
* or flatness of a distribution compared with the normal distribution. Positive |
| 56 |
* kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a |
| 57 |
* relatively flat distribution. |
| 58 |
* |
| 59 |
* @param mixed[] ...$args Data Series |
| 60 |
* @return string|int|float |
| 61 |
*/ |
| 62 |
public static function kurtosis(...$args) |
| 63 |
{ |
| 64 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 65 |
$mean = Averages::average($aArgs); |
| 66 |
if (!is_numeric($mean)) { |
| 67 |
return ExcelError::DIV0(); |
| 68 |
} |
| 69 |
$stdDev = (float) StandardDeviations::STDEV($aArgs); |
| 70 |
|
| 71 |
if ($stdDev > 0) { |
| 72 |
$count = $summer = 0; |
| 73 |
|
| 74 |
foreach ($aArgs as $k => $arg) { |
| 75 |
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { |
| 76 |
} else { |
| 77 |
// Is it a numeric value? |
| 78 |
if ((is_numeric($arg)) && (!is_string($arg))) { |
| 79 |
$summer += (($arg - $mean) / $stdDev) ** 4; |
| 80 |
++$count; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ($count > 3) { |
| 86 |
return $summer * ($count * ($count + 1) |
| 87 |
/ (($count - 1) * ($count - 2) * ($count - 3))) - (3 * ($count - 1) ** 2 |
| 88 |
/ (($count - 2) * ($count - 3))); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return ExcelError::DIV0(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* SKEW. |
| 97 |
* |
| 98 |
* Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry |
| 99 |
* of a distribution around its mean. Positive skewness indicates a distribution with an |
| 100 |
* asymmetric tail extending toward more positive values. Negative skewness indicates a |
| 101 |
* distribution with an asymmetric tail extending toward more negative values. |
| 102 |
* |
| 103 |
* @param mixed[] ...$args Data Series |
| 104 |
* |
| 105 |
* @return float|int|string The result, or a string containing an error |
| 106 |
*/ |
| 107 |
public static function skew(...$args) |
| 108 |
{ |
| 109 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 110 |
$mean = Averages::average($aArgs); |
| 111 |
if (!is_numeric($mean)) { |
| 112 |
return ExcelError::DIV0(); |
| 113 |
} |
| 114 |
$stdDev = StandardDeviations::STDEV($aArgs); |
| 115 |
if ($stdDev === 0.0 || is_string($stdDev)) { |
| 116 |
return ExcelError::DIV0(); |
| 117 |
} |
| 118 |
|
| 119 |
$count = $summer = 0; |
| 120 |
// Loop through arguments |
| 121 |
foreach ($aArgs as $k => $arg) { |
| 122 |
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { |
| 123 |
} elseif (!is_numeric($arg)) { |
| 124 |
return ExcelError::VALUE(); |
| 125 |
} else { |
| 126 |
// Is it a numeric value? |
| 127 |
if (!is_string($arg)) { |
| 128 |
$summer += (($arg - $mean) / $stdDev) ** 3; |
| 129 |
++$count; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ($count > 2) { |
| 135 |
return $summer * ($count / (($count - 1) * ($count - 2))); |
| 136 |
} |
| 137 |
|
| 138 |
return ExcelError::DIV0(); |
| 139 |
} |
| 140 |
} |
| 141 |
|