Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  
   7  class Deviations
   8  {
   9      /**
  10       * DEVSQ.
  11       *
  12       * Returns the sum of squares of deviations of data points from their sample mean.
  13       *
  14       * Excel Function:
  15       *        DEVSQ(value1[,value2[, ...]])
  16       *
  17       * @param mixed ...$args Data values
  18       *
  19       * @return float|string
  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 Functions::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 ? Functions::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 array ...$args Data Series
  60       *
  61       * @return float|string
  62       */
  63      public static function kurtosis(...$args)
  64      {
  65          $aArgs = Functions::flattenArrayIndexed($args);
  66          $mean = Averages::average($aArgs);
  67          if (!is_numeric($mean)) {
  68              return Functions::DIV0();
  69          }
  70          $stdDev = StandardDeviations::STDEV($aArgs);
  71  
  72          if ($stdDev > 0) {
  73              $count = $summer = 0;
  74  
  75              foreach ($aArgs as $k => $arg) {
  76                  if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
  77                  } else {
  78                      // Is it a numeric value?
  79                      if ((is_numeric($arg)) && (!is_string($arg))) {
  80                          $summer += (($arg - $mean) / $stdDev) ** 4;
  81                          ++$count;
  82                      }
  83                  }
  84              }
  85  
  86              if ($count > 3) {
  87                  return $summer * ($count * ($count + 1) /
  88                          (($count - 1) * ($count - 2) * ($count - 3))) - (3 * ($count - 1) ** 2 /
  89                          (($count - 2) * ($count - 3)));
  90              }
  91          }
  92  
  93          return Functions::DIV0();
  94      }
  95  
  96      /**
  97       * SKEW.
  98       *
  99       * Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry
 100       * of a distribution around its mean. Positive skewness indicates a distribution with an
 101       * asymmetric tail extending toward more positive values. Negative skewness indicates a
 102       * distribution with an asymmetric tail extending toward more negative values.
 103       *
 104       * @param array ...$args Data Series
 105       *
 106       * @return float|int|string The result, or a string containing an error
 107       */
 108      public static function skew(...$args)
 109      {
 110          $aArgs = Functions::flattenArrayIndexed($args);
 111          $mean = Averages::average($aArgs);
 112          if (!is_numeric($mean)) {
 113              return Functions::DIV0();
 114          }
 115          $stdDev = StandardDeviations::STDEV($aArgs);
 116          if ($stdDev === 0.0 || is_string($stdDev)) {
 117              return Functions::DIV0();
 118          }
 119  
 120          $count = $summer = 0;
 121          // Loop through arguments
 122          foreach ($aArgs as $k => $arg) {
 123              if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
 124              } elseif (!is_numeric($arg)) {
 125                  return Functions::VALUE();
 126              } else {
 127                  // Is it a numeric value?
 128                  if ((is_numeric($arg)) && (!is_string($arg))) {
 129                      $summer += (($arg - $mean) / $stdDev) ** 3;
 130                      ++$count;
 131                  }
 132              }
 133          }
 134  
 135          if ($count > 2) {
 136              return $summer * ($count / (($count - 1) * ($count - 2)));
 137          }
 138  
 139          return Functions::DIV0();
 140      }
 141  }