PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / Statistical / Distributions / Gamma.php

Gamma.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Statistical/Distributions/Gamma.php

156 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception;
7 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9 class Gamma extends GammaBase
10 {
11 use ArrayEnabled;
12
13 /**
14 * GAMMA.
15 *
16 * Return the gamma function value.
17 *
18 * @param mixed $value Float value for which we want the probability
19 * Or can be an array of values
20 *
21 * @return array<mixed>|float|string The result, or a string containing an error
22 * If an array of numbers is passed as an argument, then the returned result will also be an array
23 * with the same dimensions
24 */
25 public static function gamma($value)
26 {
27 if (is_array($value)) {
28 return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
29 }
30
31 try {
32 $value = DistributionValidations::validateFloat($value);
33 } catch (Exception $e) {
34 return $e->getMessage();
35 }
36
37 if ((((int) $value) == ((float) $value)) && $value <= 0.0) {
38 return ExcelError::NAN();
39 }
40
41 return self::gammaValue($value);
42 }
43
44 /**
45 * GAMMADIST.
46 *
47 * Returns the gamma distribution.
48 *
49 * @param mixed $value Float Value at which you want to evaluate the distribution
50 * Or can be an array of values
51 * @param mixed $a Parameter to the distribution as a float
52 * Or can be an array of values
53 * @param mixed $b Parameter to the distribution as a float
54 * Or can be an array of values
55 * @param mixed $cumulative Boolean value indicating if we want the cdf (true) or the pdf (false)
56 * Or can be an array of values
57 *
58 * @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array
59 * with the same dimensions
60 */
61 public static function distribution($value, $a, $b, $cumulative)
62 {
63 if (is_array($value) || is_array($a) || is_array($b) || is_array($cumulative)) {
64 return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $a, $b, $cumulative);
65 }
66
67 try {
68 $value = DistributionValidations::validateFloat($value);
69 $a = DistributionValidations::validateFloat($a);
70 $b = DistributionValidations::validateFloat($b);
71 $cumulative = DistributionValidations::validateBool($cumulative);
72 } catch (Exception $e) {
73 return $e->getMessage();
74 }
75
76 if (($value < 0) || ($a <= 0) || ($b <= 0)) {
77 return ExcelError::NAN();
78 }
79
80 return self::calculateDistribution($value, $a, $b, $cumulative);
81 }
82
83 /**
84 * GAMMAINV.
85 *
86 * Returns the inverse of the Gamma distribution.
87 *
88 * For a probability so far into the upper tail that the forward-CDF series
89 * approximation plateaus below it, the root cannot be bracketed; the result
90 * is capped at the alpha*beta*5 search ceiling rather than diverging.
91 *
92 * @param mixed $probability Float probability at which you want to evaluate the distribution
93 * Or can be an array of values
94 * @param mixed $alpha Parameter to the distribution as a float
95 * Or can be an array of values
96 * @param mixed $beta Parameter to the distribution as a float
97 * Or can be an array of values
98 *
99 * @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array
100 * with the same dimensions
101 */
102 public static function inverse($probability, $alpha, $beta)
103 {
104 if (is_array($probability) || is_array($alpha) || is_array($beta)) {
105 return self::evaluateArrayArguments([self::class, __FUNCTION__], $probability, $alpha, $beta);
106 }
107
108 try {
109 $probability = DistributionValidations::validateProbability($probability);
110 $alpha = DistributionValidations::validateFloat($alpha);
111 $beta = DistributionValidations::validateFloat($beta);
112 } catch (Exception $e) {
113 return $e->getMessage();
114 }
115
116 if (($alpha <= 0.0) || ($beta <= 0.0)) {
117 return ExcelError::NAN();
118 }
119
120 return self::calculateInverse($probability, $alpha, $beta);
121 }
122
123 /**
124 * GAMMALN.
125 *
126 * Returns the natural logarithm of the gamma function.
127 *
128 * @param mixed $value Float Value at which you want to evaluate the distribution
129 * Or can be an array of values
130 *
131 * @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array
132 * with the same dimensions
133 */
134 public static function ln($value)
135 {
136 if (is_array($value)) {
137 return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
138 }
139
140 try {
141 $value = DistributionValidations::validateFloat($value);
142 } catch (Exception $e) {
143 return $e->getMessage();
144 }
145
146 if ($value <= 0) {
147 return ExcelError::NAN();
148 }
149
150 $result = self::logGamma($value);
151
152 // logGamma returns its MAX_VALUE sentinel when the result would overflow.
153 return ($result >= self::MAX_VALUE) ? ExcelError::NAN() : $result;
154 }
155 }
156