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\Averages;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Averages;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Counts;
   9  use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Minimum;
  10  
  11  class Mean
  12  {
  13      /**
  14       * GEOMEAN.
  15       *
  16       * Returns the geometric mean of an array or range of positive data. For example, you
  17       *        can use GEOMEAN to calculate average growth rate given compound interest with
  18       *        variable rates.
  19       *
  20       * Excel Function:
  21       *        GEOMEAN(value1[,value2[, ...]])
  22       *
  23       * @param mixed ...$args Data values
  24       *
  25       * @return float|string
  26       */
  27      public static function geometric(...$args)
  28      {
  29          $aArgs = Functions::flattenArray($args);
  30  
  31          $aMean = MathTrig\Operations::product($aArgs);
  32          if (is_numeric($aMean) && ($aMean > 0)) {
  33              $aCount = Counts::COUNT($aArgs);
  34              if (Minimum::min($aArgs) > 0) {
  35                  return $aMean ** (1 / $aCount);
  36              }
  37          }
  38  
  39          return Functions::NAN();
  40      }
  41  
  42      /**
  43       * HARMEAN.
  44       *
  45       * Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the
  46       *        arithmetic mean of reciprocals.
  47       *
  48       * Excel Function:
  49       *        HARMEAN(value1[,value2[, ...]])
  50       *
  51       * @param mixed ...$args Data values
  52       *
  53       * @return float|string
  54       */
  55      public static function harmonic(...$args)
  56      {
  57          // Loop through arguments
  58          $aArgs = Functions::flattenArray($args);
  59          if (Minimum::min($aArgs) < 0) {
  60              return Functions::NAN();
  61          }
  62  
  63          $returnValue = 0;
  64          $aCount = 0;
  65          foreach ($aArgs as $arg) {
  66              // Is it a numeric value?
  67              if ((is_numeric($arg)) && (!is_string($arg))) {
  68                  if ($arg <= 0) {
  69                      return Functions::NAN();
  70                  }
  71                  $returnValue += (1 / $arg);
  72                  ++$aCount;
  73              }
  74          }
  75  
  76          // Return
  77          if ($aCount > 0) {
  78              return 1 / ($returnValue / $aCount);
  79          }
  80  
  81          return Functions::NA();
  82      }
  83  
  84      /**
  85       * TRIMMEAN.
  86       *
  87       * Returns the mean of the interior of a data set. TRIMMEAN calculates the mean
  88       *        taken by excluding a percentage of data points from the top and bottom tails
  89       *        of a data set.
  90       *
  91       * Excel Function:
  92       *        TRIMEAN(value1[,value2[, ...]], $discard)
  93       *
  94       * @param mixed $args Data values
  95       *
  96       * @return float|string
  97       */
  98      public static function trim(...$args)
  99      {
 100          $aArgs = Functions::flattenArray($args);
 101  
 102          // Calculate
 103          $percent = array_pop($aArgs);
 104  
 105          if ((is_numeric($percent)) && (!is_string($percent))) {
 106              if (($percent < 0) || ($percent > 1)) {
 107                  return Functions::NAN();
 108              }
 109  
 110              $mArgs = [];
 111              foreach ($aArgs as $arg) {
 112                  // Is it a numeric value?
 113                  if ((is_numeric($arg)) && (!is_string($arg))) {
 114                      $mArgs[] = $arg;
 115                  }
 116              }
 117  
 118              $discard = floor(Counts::COUNT($mArgs) * $percent / 2);
 119              sort($mArgs);
 120  
 121              for ($i = 0; $i < $discard; ++$i) {
 122                  array_pop($mArgs);
 123                  array_shift($mArgs);
 124              }
 125  
 126              return Averages::average($mArgs);
 127          }
 128  
 129          return Functions::VALUE();
 130      }
 131  }