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