/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Statistical/Deviations.php

https://gitlab.com/Japang-Jawara/jawara-penilaian · PHP · 141 lines · 85 code · 15 blank · 41 comment · 22 complexity · ad2839dcfb9c3c0322b6faeee8c59c4d MD5 · raw file

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