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