Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

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