Skip to content

Commit f1cb75e

Browse files
author
Mark Baker
authored
Start work on Array-enabling the Statistical functions (#2588)
1 parent 7901eb0 commit f1cb75e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1169
-138
lines changed

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,6 @@ parameters:
10151015
count: 1
10161016
path: src/PhpSpreadsheet/Calculation/Statistical/Distributions/Beta.php
10171017

1018-
-
1019-
message: "#^Static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Statistical\\\\Distributions\\\\Beta\\:\\:regularizedIncompleteBeta\\(\\) is unused\\.$#"
1020-
count: 1
1021-
path: src/PhpSpreadsheet/Calculation/Statistical/Distributions/Beta.php
1022-
10231018
-
10241019
message: "#^Constant PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Statistical\\\\Distributions\\\\ChiSquared\\:\\:MAX_ITERATIONS is unused\\.$#"
10251020
count: 1

src/PhpSpreadsheet/Calculation/Statistical.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static function AVERAGEIF($range, $condition, $averageRange = [])
130130
* @param mixed $rMin
131131
* @param mixed $rMax
132132
*
133-
* @return float|string
133+
* @return array|float|string
134134
*/
135135
public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1)
136136
{
@@ -153,7 +153,7 @@ public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1)
153153
* @param float $rMin Minimum value
154154
* @param float $rMax Maximum value
155155
*
156-
* @return float|string
156+
* @return array|float|string
157157
*/
158158
public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1)
159159
{
@@ -179,7 +179,7 @@ public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1
179179
* @param mixed $probability Probability of success on each trial
180180
* @param mixed $cumulative
181181
*
182-
* @return float|string
182+
* @return array|float|string
183183
*/
184184
public static function BINOMDIST($value, $trials, $probability, $cumulative)
185185
{
@@ -199,7 +199,7 @@ public static function BINOMDIST($value, $trials, $probability, $cumulative)
199199
* @param float $value Value for the function
200200
* @param float $degrees degrees of freedom
201201
*
202-
* @return float|string
202+
* @return array|float|string
203203
*/
204204
public static function CHIDIST($value, $degrees)
205205
{
@@ -219,7 +219,7 @@ public static function CHIDIST($value, $degrees)
219219
* @param float $probability Probability for the function
220220
* @param float $degrees degrees of freedom
221221
*
222-
* @return float|string
222+
* @return array|float|string
223223
*/
224224
public static function CHIINV($probability, $degrees)
225225
{
@@ -240,7 +240,7 @@ public static function CHIINV($probability, $degrees)
240240
* @param float $stdDev Standard Deviation
241241
* @param float $size
242242
*
243-
* @return float|string
243+
* @return array|float|string
244244
*/
245245
public static function CONFIDENCE($alpha, $stdDev, $size)
246246
{
@@ -415,7 +415,7 @@ public static function COVAR($yValues, $xValues)
415415
* @param float $probability probability of a success on each trial
416416
* @param float $alpha criterion value
417417
*
418-
* @return int|string
418+
* @return array|int|string
419419
*/
420420
public static function CRITBINOM($trials, $probability, $alpha)
421421
{
@@ -460,7 +460,7 @@ public static function DEVSQ(...$args)
460460
* @param float $lambda The parameter value
461461
* @param bool $cumulative
462462
*
463-
* @return float|string
463+
* @return array|float|string
464464
*/
465465
public static function EXPONDIST($value, $lambda, $cumulative)
466466
{
@@ -486,7 +486,7 @@ public static function EXPONDIST($value, $lambda, $cumulative)
486486
* @param bool $cumulative If cumulative is TRUE, F.DIST returns the cumulative distribution function;
487487
* if FALSE, it returns the probability density function.
488488
*
489-
* @return float|string
489+
* @return array|float|string
490490
*/
491491
public static function FDIST2($value, $u, $v, $cumulative)
492492
{
@@ -507,7 +507,7 @@ public static function FDIST2($value, $u, $v, $cumulative)
507507
*
508508
* @param float $value
509509
*
510-
* @return float|string
510+
* @return array|float|string
511511
*/
512512
public static function FISHER($value)
513513
{
@@ -528,7 +528,7 @@ public static function FISHER($value)
528528
*
529529
* @param float $value
530530
*
531-
* @return float|string
531+
* @return array|float|string
532532
*/
533533
public static function FISHERINV($value)
534534
{
@@ -549,7 +549,7 @@ public static function FISHERINV($value)
549549
* @param mixed $yValues array of mixed Data Series Y
550550
* @param mixed $xValues of mixed Data Series X
551551
*
552-
* @return bool|float|string
552+
* @return array|bool|float|string
553553
*/
554554
public static function FORECAST($xValue, $yValues, $xValues)
555555
{
@@ -568,7 +568,7 @@ public static function FORECAST($xValue, $yValues, $xValues)
568568
*
569569
* @param float $value
570570
*
571-
* @return float|string The result, or a string containing an error
571+
* @return array|float|string The result, or a string containing an error
572572
*/
573573
public static function GAMMAFunction($value)
574574
{
@@ -590,7 +590,7 @@ public static function GAMMAFunction($value)
590590
* @param float $b Parameter to the distribution
591591
* @param bool $cumulative
592592
*
593-
* @return float|string
593+
* @return array|float|string
594594
*/
595595
public static function GAMMADIST($value, $a, $b, $cumulative)
596596
{
@@ -611,7 +611,7 @@ public static function GAMMADIST($value, $a, $b, $cumulative)
611611
* @param float $alpha Parameter to the distribution
612612
* @param float $beta Parameter to the distribution
613613
*
614-
* @return float|string
614+
* @return array|float|string
615615
*/
616616
public static function GAMMAINV($probability, $alpha, $beta)
617617
{
@@ -630,7 +630,7 @@ public static function GAMMAINV($probability, $alpha, $beta)
630630
*
631631
* @param float $value
632632
*
633-
* @return float|string
633+
* @return array|float|string
634634
*/
635635
public static function GAMMALN($value)
636636
{
@@ -650,7 +650,7 @@ public static function GAMMALN($value)
650650
*
651651
* @param float $value
652652
*
653-
* @return float|string The result, or a string containing an error
653+
* @return array|float|string The result, or a string containing an error
654654
*/
655655
public static function GAUSS($value)
656656
{
@@ -742,7 +742,7 @@ public static function HARMEAN(...$args)
742742
* @param mixed $populationSuccesses Number of successes in the population
743743
* @param mixed $populationNumber Population size
744744
*
745-
* @return float|string
745+
* @return array|float|string
746746
*/
747747
public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber)
748748
{
@@ -879,7 +879,7 @@ public static function LOGEST($yValues, $xValues = null, $const = true, $stats =
879879
* @param float $mean
880880
* @param float $stdDev
881881
*
882-
* @return float|string The result, or a string containing an error
882+
* @return array|float|string The result, or a string containing an error
883883
*
884884
* @TODO Try implementing P J Acklam's refinement algorithm for greater
885885
* accuracy if I can get my head round the mathematics
@@ -905,7 +905,7 @@ public static function LOGINV($probability, $mean, $stdDev)
905905
* @param float $mean
906906
* @param float $stdDev
907907
*
908-
* @return float|string The result, or a string containing an error
908+
* @return array|float|string The result, or a string containing an error
909909
*/
910910
public static function LOGNORMDIST($value, $mean, $stdDev)
911911
{
@@ -928,7 +928,7 @@ public static function LOGNORMDIST($value, $mean, $stdDev)
928928
* @param float $stdDev
929929
* @param bool $cumulative
930930
*
931-
* @return float|string The result, or a string containing an error
931+
* @return array|float|string The result, or a string containing an error
932932
*/
933933
public static function LOGNORMDIST2($value, $mean, $stdDev, $cumulative = false)
934934
{
@@ -1131,7 +1131,7 @@ public static function MODE(...$args)
11311131
* @param mixed $successes Threshold number of Successes
11321132
* @param mixed $probability Probability of success on each trial
11331133
*
1134-
* @return float|string The result, or a string containing an error
1134+
* @return array|float|string The result, or a string containing an error
11351135
*/
11361136
public static function NEGBINOMDIST($failures, $successes, $probability)
11371137
{
@@ -1308,7 +1308,7 @@ public static function PERCENTRANK($valueSet, $value, $significance = 3)
13081308
* @param int $numObjs Number of different objects
13091309
* @param int $numInSet Number of objects in each permutation
13101310
*
1311-
* @return float|int|string Number of permutations, or a string containing an error
1311+
* @return array|float|int|string Number of permutations, or a string containing an error
13121312
*/
13131313
public static function PERMUT($numObjs, $numInSet)
13141314
{
@@ -1480,7 +1480,7 @@ public static function SMALL(...$args)
14801480
* @param float $mean Mean Value
14811481
* @param float $stdDev Standard Deviation
14821482
*
1483-
* @return float|string Standardized value, or a string containing an error
1483+
* @return array|float|string Standardized value, or a string containing an error
14841484
*/
14851485
public static function STANDARDIZE($value, $mean, $stdDev)
14861486
{

src/PhpSpreadsheet/Calculation/Statistical/Confidence.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Confidence
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* CONFIDENCE.
1215
*
1316
* Returns the confidence interval for a population mean
1417
*
1518
* @param mixed $alpha As a float
19+
* Or can be an array of values
1620
* @param mixed $stdDev Standard Deviation as a float
21+
* Or can be an array of values
1722
* @param mixed $size As an integer
23+
* Or can be an array of values
1824
*
19-
* @return float|string
25+
* @return array|float|string
26+
* If an array of numbers is passed as an argument, then the returned result will also be an array
27+
* with the same dimensions
2028
*/
2129
public static function CONFIDENCE($alpha, $stdDev, $size)
2230
{
23-
$alpha = Functions::flattenSingleValue($alpha);
24-
$stdDev = Functions::flattenSingleValue($stdDev);
25-
$size = Functions::flattenSingleValue($size);
31+
if (is_array($alpha) || is_array($stdDev) || is_array($size)) {
32+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $alpha, $stdDev, $size);
33+
}
2634

2735
try {
2836
$alpha = StatisticalValidations::validateFloat($alpha);
@@ -36,6 +44,6 @@ public static function CONFIDENCE($alpha, $stdDev, $size)
3644
return Functions::NAN();
3745
}
3846

39-
return Distributions\StandardNormal::inverse(1 - $alpha / 2) * $stdDev / sqrt($size);
47+
return Functions::scalar(Distributions\StandardNormal::inverse(1 - $alpha / 2) * $stdDev / sqrt($size));
4048
}
4149
}

src/PhpSpreadsheet/Calculation/Statistical/Distributions/Beta.php

+31-12
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Beta
910
{
11+
use ArrayEnabled;
12+
1013
private const MAX_ITERATIONS = 256;
1114

1215
private const LOG_GAMMA_X_MAX_VALUE = 2.55e305;
@@ -19,20 +22,28 @@ class Beta
1922
* Returns the beta distribution.
2023
*
2124
* @param mixed $value Float value at which you want to evaluate the distribution
25+
* Or can be an array of values
2226
* @param mixed $alpha Parameter to the distribution as a float
27+
* Or can be an array of values
2328
* @param mixed $beta Parameter to the distribution as a float
29+
* Or can be an array of values
2430
* @param mixed $rMin as an float
31+
* Or can be an array of values
2532
* @param mixed $rMax as an float
33+
* Or can be an array of values
2634
*
27-
* @return float|string
35+
* @return array|float|string
36+
* If an array of numbers is passed as an argument, then the returned result will also be an array
37+
* with the same dimensions
2838
*/
2939
public static function distribution($value, $alpha, $beta, $rMin = 0.0, $rMax = 1.0)
3040
{
31-
$value = Functions::flattenSingleValue($value);
32-
$alpha = Functions::flattenSingleValue($alpha);
33-
$beta = Functions::flattenSingleValue($beta);
34-
$rMin = ($rMin === null) ? 0.0 : Functions::flattenSingleValue($rMin);
35-
$rMax = ($rMax === null) ? 1.0 : Functions::flattenSingleValue($rMax);
41+
if (is_array($value) || is_array($alpha) || is_array($beta) || is_array($rMin) || is_array($rMax)) {
42+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $alpha, $beta, $rMin, $rMax);
43+
}
44+
45+
$rMin = $rMin ?? 0.0;
46+
$rMax = $rMax ?? 1.0;
3647

3748
try {
3849
$value = DistributionValidations::validateFloat($value);
@@ -65,20 +76,28 @@ public static function distribution($value, $alpha, $beta, $rMin = 0.0, $rMax =
6576
* Returns the inverse of the Beta distribution.
6677
*
6778
* @param mixed $probability Float probability at which you want to evaluate the distribution
79+
* Or can be an array of values
6880
* @param mixed $alpha Parameter to the distribution as a float
81+
* Or can be an array of values
6982
* @param mixed $beta Parameter to the distribution as a float
83+
* Or can be an array of values
7084
* @param mixed $rMin Minimum value as a float
85+
* Or can be an array of values
7186
* @param mixed $rMax Maximum value as a float
87+
* Or can be an array of values
7288
*
73-
* @return float|string
89+
* @return array|float|string
90+
* If an array of numbers is passed as an argument, then the returned result will also be an array
91+
* with the same dimensions
7492
*/
7593
public static function inverse($probability, $alpha, $beta, $rMin = 0.0, $rMax = 1.0)
7694
{
77-
$probability = Functions::flattenSingleValue($probability);
78-
$alpha = Functions::flattenSingleValue($alpha);
79-
$beta = Functions::flattenSingleValue($beta);
80-
$rMin = ($rMin === null) ? 0.0 : Functions::flattenSingleValue($rMin);
81-
$rMax = ($rMax === null) ? 1.0 : Functions::flattenSingleValue($rMax);
95+
if (is_array($probability) || is_array($alpha) || is_array($beta) || is_array($rMin) || is_array($rMax)) {
96+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $probability, $alpha, $beta, $rMin, $rMax);
97+
}
98+
99+
$rMin = $rMin ?? 0.0;
100+
$rMax = $rMax ?? 1.0;
82101

83102
try {
84103
$probability = DistributionValidations::validateProbability($probability);

0 commit comments

Comments
 (0)