1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPExcel |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2006 - 2012 PHPExcel |
6
|
|
|
* |
7
|
|
|
* This library is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU Lesser General Public |
9
|
|
|
* License as published by the Free Software Foundation; either |
10
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This library is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15
|
|
|
* Lesser General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Lesser General Public |
18
|
|
|
* License along with this library; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20
|
|
|
* |
21
|
|
|
* @category PHPExcel |
22
|
|
|
* @package PHPExcel_Calculation |
23
|
|
|
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel) |
24
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
25
|
|
|
* @version 1.7.7, 2012-05-19 |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** PHPExcel root directory */ |
30
|
|
View Code Duplication |
if (!defined('PHPEXCEL_ROOT')) { |
31
|
|
|
/** |
32
|
|
|
* @ignore |
33
|
|
|
*/ |
34
|
|
|
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../'); |
35
|
|
|
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/trendClass.php'; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** LOG_GAMMA_X_MAX_VALUE */ |
43
|
|
|
define('LOG_GAMMA_X_MAX_VALUE', 2.55e305); |
44
|
|
|
|
45
|
|
|
/** XMININ */ |
46
|
|
|
define('XMININ', 2.23e-308); |
47
|
|
|
|
48
|
|
|
/** EPS */ |
49
|
|
|
define('EPS', 2.22e-16); |
50
|
|
|
|
51
|
|
|
/** SQRT2PI */ |
52
|
|
|
define('SQRT2PI', 2.5066282746310005024157652848110452530069867406099); |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* PHPExcel_Calculation_Statistical |
57
|
|
|
* |
58
|
|
|
* @category PHPExcel |
59
|
|
|
* @package PHPExcel_Calculation |
60
|
|
|
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel) |
61
|
|
|
*/ |
62
|
|
|
class PHPExcel_Calculation_Statistical { |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
private static function _checkTrendArrays(&$array1,&$array2) { |
66
|
|
|
if (!is_array($array1)) { $array1 = array($array1); } |
67
|
|
|
if (!is_array($array2)) { $array2 = array($array2); } |
68
|
|
|
|
69
|
|
|
$array1 = PHPExcel_Calculation_Functions::flattenArray($array1); |
70
|
|
|
$array2 = PHPExcel_Calculation_Functions::flattenArray($array2); |
71
|
|
View Code Duplication |
foreach($array1 as $key => $value) { |
72
|
|
|
if ((is_bool($value)) || (is_string($value)) || (is_null($value))) { |
73
|
|
|
unset($array1[$key]); |
74
|
|
|
unset($array2[$key]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
View Code Duplication |
foreach($array2 as $key => $value) { |
78
|
|
|
if ((is_bool($value)) || (is_string($value)) || (is_null($value))) { |
79
|
|
|
unset($array1[$key]); |
80
|
|
|
unset($array2[$key]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$array1 = array_merge($array1); |
84
|
|
|
$array2 = array_merge($array2); |
85
|
|
|
|
86
|
|
|
return True; |
87
|
|
|
} // function _checkTrendArrays() |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Beta function. |
92
|
|
|
* |
93
|
|
|
* @author Jaco van Kooten |
94
|
|
|
* |
95
|
|
|
* @param p require p>0 |
96
|
|
|
* @param q require q>0 |
97
|
|
|
* @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
private static function _beta($p, $q) { |
|
|
|
|
100
|
|
|
if ($p <= 0.0 || $q <= 0.0 || ($p + $q) > LOG_GAMMA_X_MAX_VALUE) { |
101
|
|
|
return 0.0; |
102
|
|
|
} else { |
103
|
|
|
return exp(self::_logBeta($p, $q)); |
104
|
|
|
} |
105
|
|
|
} // function _beta() |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Incomplete beta function |
110
|
|
|
* |
111
|
|
|
* @author Jaco van Kooten |
112
|
|
|
* @author Paul Meagher |
113
|
|
|
* |
114
|
|
|
* The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992). |
115
|
|
|
* @param x require 0<=x<=1 |
116
|
|
|
* @param p require p>0 |
117
|
|
|
* @param q require q>0 |
118
|
|
|
* @return 0 if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
private static function _incompleteBeta($x, $p, $q) { |
121
|
|
|
if ($x <= 0.0) { |
122
|
|
|
return 0.0; |
123
|
|
|
} elseif ($x >= 1.0) { |
124
|
|
|
return 1.0; |
125
|
|
|
} elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE)) { |
126
|
|
|
return 0.0; |
127
|
|
|
} |
128
|
|
|
$beta_gam = exp((0 - self::_logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x)); |
129
|
|
|
if ($x < ($p + 1.0) / ($p + $q + 2.0)) { |
130
|
|
|
return $beta_gam * self::_betaFraction($x, $p, $q) / $p; |
131
|
|
|
} else { |
132
|
|
|
return 1.0 - ($beta_gam * self::_betaFraction(1 - $x, $q, $p) / $q); |
133
|
|
|
} |
134
|
|
|
} // function _incompleteBeta() |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
// Function cache for _logBeta function |
138
|
|
|
private static $_logBetaCache_p = 0.0; |
139
|
|
|
private static $_logBetaCache_q = 0.0; |
140
|
|
|
private static $_logBetaCache_result = 0.0; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* The natural logarithm of the beta function. |
144
|
|
|
* |
145
|
|
|
* @param p require p>0 |
146
|
|
|
* @param q require q>0 |
147
|
|
|
* @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow |
|
|
|
|
148
|
|
|
* @author Jaco van Kooten |
149
|
|
|
*/ |
150
|
|
|
private static function _logBeta($p, $q) { |
151
|
|
|
if ($p != self::$_logBetaCache_p || $q != self::$_logBetaCache_q) { |
152
|
|
|
self::$_logBetaCache_p = $p; |
153
|
|
|
self::$_logBetaCache_q = $q; |
154
|
|
|
if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE)) { |
155
|
|
|
self::$_logBetaCache_result = 0.0; |
156
|
|
|
} else { |
157
|
|
|
self::$_logBetaCache_result = self::_logGamma($p) + self::_logGamma($q) - self::_logGamma($p + $q); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return self::$_logBetaCache_result; |
161
|
|
|
} // function _logBeta() |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Evaluates of continued fraction part of incomplete beta function. |
166
|
|
|
* Based on an idea from Numerical Recipes (W.H. Press et al, 1992). |
167
|
|
|
* @author Jaco van Kooten |
168
|
|
|
*/ |
169
|
|
|
private static function _betaFraction($x, $p, $q) { |
170
|
|
|
$c = 1.0; |
171
|
|
|
$sum_pq = $p + $q; |
172
|
|
|
$p_plus = $p + 1.0; |
173
|
|
|
$p_minus = $p - 1.0; |
174
|
|
|
$h = 1.0 - $sum_pq * $x / $p_plus; |
175
|
|
|
if (abs($h) < XMININ) { |
176
|
|
|
$h = XMININ; |
177
|
|
|
} |
178
|
|
|
$h = 1.0 / $h; |
179
|
|
|
$frac = $h; |
180
|
|
|
$m = 1; |
181
|
|
|
$delta = 0.0; |
182
|
|
|
while ($m <= MAX_ITERATIONS && abs($delta-1.0) > PRECISION ) { |
183
|
|
|
$m2 = 2 * $m; |
184
|
|
|
// even index for d |
185
|
|
|
$d = $m * ($q - $m) * $x / ( ($p_minus + $m2) * ($p + $m2)); |
186
|
|
|
$h = 1.0 + $d * $h; |
187
|
|
|
if (abs($h) < XMININ) { |
188
|
|
|
$h = XMININ; |
189
|
|
|
} |
190
|
|
|
$h = 1.0 / $h; |
191
|
|
|
$c = 1.0 + $d / $c; |
192
|
|
|
if (abs($c) < XMININ) { |
193
|
|
|
$c = XMININ; |
194
|
|
|
} |
195
|
|
|
$frac *= $h * $c; |
196
|
|
|
// odd index for d |
197
|
|
|
$d = -($p + $m) * ($sum_pq + $m) * $x / (($p + $m2) * ($p_plus + $m2)); |
198
|
|
|
$h = 1.0 + $d * $h; |
199
|
|
|
if (abs($h) < XMININ) { |
200
|
|
|
$h = XMININ; |
201
|
|
|
} |
202
|
|
|
$h = 1.0 / $h; |
203
|
|
|
$c = 1.0 + $d / $c; |
204
|
|
|
if (abs($c) < XMININ) { |
205
|
|
|
$c = XMININ; |
206
|
|
|
} |
207
|
|
|
$delta = $h * $c; |
208
|
|
|
$frac *= $delta; |
209
|
|
|
++$m; |
210
|
|
|
} |
211
|
|
|
return $frac; |
212
|
|
|
} // function _betaFraction() |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* logGamma function |
217
|
|
|
* |
218
|
|
|
* @version 1.1 |
219
|
|
|
* @author Jaco van Kooten |
220
|
|
|
* |
221
|
|
|
* Original author was Jaco van Kooten. Ported to PHP by Paul Meagher. |
222
|
|
|
* |
223
|
|
|
* The natural logarithm of the gamma function. <br /> |
224
|
|
|
* Based on public domain NETLIB (Fortran) code by W. J. Cody and L. Stoltz <br /> |
225
|
|
|
* Applied Mathematics Division <br /> |
226
|
|
|
* Argonne National Laboratory <br /> |
227
|
|
|
* Argonne, IL 60439 <br /> |
228
|
|
|
* <p> |
229
|
|
|
* References: |
230
|
|
|
* <ol> |
231
|
|
|
* <li>W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for the Natural |
232
|
|
|
* Logarithm of the Gamma Function,' Math. Comp. 21, 1967, pp. 198-203.</li> |
233
|
|
|
* <li>K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May, 1969.</li> |
234
|
|
|
* <li>Hart, Et. Al., Computer Approximations, Wiley and sons, New York, 1968.</li> |
235
|
|
|
* </ol> |
236
|
|
|
* </p> |
237
|
|
|
* <p> |
238
|
|
|
* From the original documentation: |
239
|
|
|
* </p> |
240
|
|
|
* <p> |
241
|
|
|
* This routine calculates the LOG(GAMMA) function for a positive real argument X. |
242
|
|
|
* Computation is based on an algorithm outlined in references 1 and 2. |
243
|
|
|
* The program uses rational functions that theoretically approximate LOG(GAMMA) |
244
|
|
|
* to at least 18 significant decimal digits. The approximation for X > 12 is from |
245
|
|
|
* reference 3, while approximations for X < 12.0 are similar to those in reference |
246
|
|
|
* 1, but are unpublished. The accuracy achieved depends on the arithmetic system, |
247
|
|
|
* the compiler, the intrinsic functions, and proper selection of the |
248
|
|
|
* machine-dependent constants. |
249
|
|
|
* </p> |
250
|
|
|
* <p> |
251
|
|
|
* Error returns: <br /> |
252
|
|
|
* The program returns the value XINF for X .LE. 0.0 or when overflow would occur. |
253
|
|
|
* The computation is believed to be free of underflow and overflow. |
254
|
|
|
* </p> |
255
|
|
|
* @return MAX_VALUE for x < 0.0 or when overflow would occur, i.e. x > 2.55E305 |
256
|
|
|
*/ |
257
|
|
|
|
258
|
|
|
// Function cache for logGamma |
259
|
|
|
private static $_logGammaCache_result = 0.0; |
260
|
|
|
private static $_logGammaCache_x = 0.0; |
261
|
|
|
|
262
|
|
|
private static function _logGamma($x) { |
263
|
|
|
// Log Gamma related constants |
264
|
|
|
static $lg_d1 = -0.5772156649015328605195174; |
265
|
|
|
static $lg_d2 = 0.4227843350984671393993777; |
266
|
|
|
static $lg_d4 = 1.791759469228055000094023; |
267
|
|
|
|
268
|
|
|
static $lg_p1 = array( 4.945235359296727046734888, |
269
|
|
|
201.8112620856775083915565, |
270
|
|
|
2290.838373831346393026739, |
271
|
|
|
11319.67205903380828685045, |
272
|
|
|
28557.24635671635335736389, |
273
|
|
|
38484.96228443793359990269, |
274
|
|
|
26377.48787624195437963534, |
275
|
|
|
7225.813979700288197698961 ); |
276
|
|
|
static $lg_p2 = array( 4.974607845568932035012064, |
277
|
|
|
542.4138599891070494101986, |
278
|
|
|
15506.93864978364947665077, |
279
|
|
|
184793.2904445632425417223, |
280
|
|
|
1088204.76946882876749847, |
281
|
|
|
3338152.967987029735917223, |
282
|
|
|
5106661.678927352456275255, |
283
|
|
|
3074109.054850539556250927 ); |
284
|
|
|
static $lg_p4 = array( 14745.02166059939948905062, |
285
|
|
|
2426813.369486704502836312, |
286
|
|
|
121475557.4045093227939592, |
287
|
|
|
2663432449.630976949898078, |
288
|
|
|
29403789566.34553899906876, |
289
|
|
|
170266573776.5398868392998, |
290
|
|
|
492612579337.743088758812, |
291
|
|
|
560625185622.3951465078242 ); |
292
|
|
|
|
293
|
|
|
static $lg_q1 = array( 67.48212550303777196073036, |
294
|
|
|
1113.332393857199323513008, |
295
|
|
|
7738.757056935398733233834, |
296
|
|
|
27639.87074403340708898585, |
297
|
|
|
54993.10206226157329794414, |
298
|
|
|
61611.22180066002127833352, |
299
|
|
|
36351.27591501940507276287, |
300
|
|
|
8785.536302431013170870835 ); |
301
|
|
|
static $lg_q2 = array( 183.0328399370592604055942, |
302
|
|
|
7765.049321445005871323047, |
303
|
|
|
133190.3827966074194402448, |
304
|
|
|
1136705.821321969608938755, |
305
|
|
|
5267964.117437946917577538, |
306
|
|
|
13467014.54311101692290052, |
307
|
|
|
17827365.30353274213975932, |
308
|
|
|
9533095.591844353613395747 ); |
309
|
|
|
static $lg_q4 = array( 2690.530175870899333379843, |
310
|
|
|
639388.5654300092398984238, |
311
|
|
|
41355999.30241388052042842, |
312
|
|
|
1120872109.61614794137657, |
313
|
|
|
14886137286.78813811542398, |
314
|
|
|
101680358627.2438228077304, |
315
|
|
|
341747634550.7377132798597, |
316
|
|
|
446315818741.9713286462081 ); |
317
|
|
|
|
318
|
|
|
static $lg_c = array( -0.001910444077728, |
319
|
|
|
8.4171387781295e-4, |
320
|
|
|
-5.952379913043012e-4, |
321
|
|
|
7.93650793500350248e-4, |
322
|
|
|
-0.002777777777777681622553, |
323
|
|
|
0.08333333333333333331554247, |
324
|
|
|
0.0057083835261 ); |
325
|
|
|
|
326
|
|
|
// Rough estimate of the fourth root of logGamma_xBig |
327
|
|
|
static $lg_frtbig = 2.25e76; |
328
|
|
|
static $pnt68 = 0.6796875; |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
if ($x == self::$_logGammaCache_x) { |
332
|
|
|
return self::$_logGammaCache_result; |
333
|
|
|
} |
334
|
|
|
$y = $x; |
335
|
|
|
if ($y > 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE) { |
336
|
|
|
if ($y <= EPS) { |
337
|
|
|
$res = -log(y); |
338
|
|
|
} elseif ($y <= 1.5) { |
339
|
|
|
// --------------------- |
340
|
|
|
// EPS .LT. X .LE. 1.5 |
341
|
|
|
// --------------------- |
342
|
|
|
if ($y < $pnt68) { |
343
|
|
|
$corr = -log($y); |
344
|
|
|
$xm1 = $y; |
345
|
|
|
} else { |
346
|
|
|
$corr = 0.0; |
347
|
|
|
$xm1 = $y - 1.0; |
348
|
|
|
} |
349
|
|
|
if ($y <= 0.5 || $y >= $pnt68) { |
350
|
|
|
$xden = 1.0; |
351
|
|
|
$xnum = 0.0; |
352
|
|
|
for ($i = 0; $i < 8; ++$i) { |
353
|
|
|
$xnum = $xnum * $xm1 + $lg_p1[$i]; |
354
|
|
|
$xden = $xden * $xm1 + $lg_q1[$i]; |
355
|
|
|
} |
356
|
|
|
$res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden)); |
357
|
|
View Code Duplication |
} else { |
358
|
|
|
$xm2 = $y - 1.0; |
359
|
|
|
$xden = 1.0; |
360
|
|
|
$xnum = 0.0; |
361
|
|
|
for ($i = 0; $i < 8; ++$i) { |
362
|
|
|
$xnum = $xnum * $xm2 + $lg_p2[$i]; |
363
|
|
|
$xden = $xden * $xm2 + $lg_q2[$i]; |
364
|
|
|
} |
365
|
|
|
$res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); |
366
|
|
|
} |
367
|
|
View Code Duplication |
} elseif ($y <= 4.0) { |
368
|
|
|
// --------------------- |
369
|
|
|
// 1.5 .LT. X .LE. 4.0 |
370
|
|
|
// --------------------- |
371
|
|
|
$xm2 = $y - 2.0; |
372
|
|
|
$xden = 1.0; |
373
|
|
|
$xnum = 0.0; |
374
|
|
|
for ($i = 0; $i < 8; ++$i) { |
375
|
|
|
$xnum = $xnum * $xm2 + $lg_p2[$i]; |
376
|
|
|
$xden = $xden * $xm2 + $lg_q2[$i]; |
377
|
|
|
} |
378
|
|
|
$res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); |
379
|
|
|
} elseif ($y <= 12.0) { |
380
|
|
|
// ---------------------- |
381
|
|
|
// 4.0 .LT. X .LE. 12.0 |
382
|
|
|
// ---------------------- |
383
|
|
|
$xm4 = $y - 4.0; |
384
|
|
|
$xden = -1.0; |
385
|
|
|
$xnum = 0.0; |
386
|
|
|
for ($i = 0; $i < 8; ++$i) { |
387
|
|
|
$xnum = $xnum * $xm4 + $lg_p4[$i]; |
388
|
|
|
$xden = $xden * $xm4 + $lg_q4[$i]; |
389
|
|
|
} |
390
|
|
|
$res = $lg_d4 + $xm4 * ($xnum / $xden); |
391
|
|
|
} else { |
392
|
|
|
// --------------------------------- |
393
|
|
|
// Evaluate for argument .GE. 12.0 |
394
|
|
|
// --------------------------------- |
395
|
|
|
$res = 0.0; |
396
|
|
|
if ($y <= $lg_frtbig) { |
397
|
|
|
$res = $lg_c[6]; |
398
|
|
|
$ysq = $y * $y; |
399
|
|
|
for ($i = 0; $i < 6; ++$i) |
400
|
|
|
$res = $res / $ysq + $lg_c[$i]; |
401
|
|
|
} |
402
|
|
|
$res /= $y; |
403
|
|
|
$corr = log($y); |
404
|
|
|
$res = $res + log(SQRT2PI) - 0.5 * $corr; |
405
|
|
|
$res += $y * ($corr - 1.0); |
406
|
|
|
} |
407
|
|
|
} else { |
408
|
|
|
// -------------------------- |
409
|
|
|
// Return for bad arguments |
410
|
|
|
// -------------------------- |
411
|
|
|
$res = MAX_VALUE; |
412
|
|
|
} |
413
|
|
|
// ------------------------------ |
414
|
|
|
// Final adjustments and return |
415
|
|
|
// ------------------------------ |
416
|
|
|
self::$_logGammaCache_x = $x; |
417
|
|
|
self::$_logGammaCache_result = $res; |
418
|
|
|
return $res; |
419
|
|
|
} // function _logGamma() |
420
|
|
|
|
421
|
|
|
|
422
|
|
|
// |
423
|
|
|
// Private implementation of the incomplete Gamma function |
424
|
|
|
// |
425
|
|
|
private static function _incompleteGamma($a,$x) { |
426
|
|
|
static $max = 32; |
427
|
|
|
$summer = 0; |
428
|
|
|
for ($n=0; $n<=$max; ++$n) { |
429
|
|
|
$divisor = $a; |
430
|
|
|
for ($i=1; $i<=$n; ++$i) { |
431
|
|
|
$divisor *= ($a + $i); |
432
|
|
|
} |
433
|
|
|
$summer += (pow($x,$n) / $divisor); |
434
|
|
|
} |
435
|
|
|
return pow($x,$a) * exp(0-$x) * $summer; |
436
|
|
|
} // function _incompleteGamma() |
437
|
|
|
|
438
|
|
|
|
439
|
|
|
// |
440
|
|
|
// Private implementation of the Gamma function |
441
|
|
|
// |
442
|
|
|
private static function _gamma($data) { |
443
|
|
|
if ($data == 0.0) return 0; |
444
|
|
|
|
445
|
|
|
static $p0 = 1.000000000190015; |
446
|
|
|
static $p = array ( 1 => 76.18009172947146, |
447
|
|
|
2 => -86.50532032941677, |
448
|
|
|
3 => 24.01409824083091, |
449
|
|
|
4 => -1.231739572450155, |
450
|
|
|
5 => 1.208650973866179e-3, |
451
|
|
|
6 => -5.395239384953e-6 |
452
|
|
|
); |
453
|
|
|
|
454
|
|
|
$y = $x = $data; |
455
|
|
|
$tmp = $x + 5.5; |
456
|
|
|
$tmp -= ($x + 0.5) * log($tmp); |
457
|
|
|
|
458
|
|
|
$summer = $p0; |
459
|
|
|
for ($j=1;$j<=6;++$j) { |
460
|
|
|
$summer += ($p[$j] / ++$y); |
461
|
|
|
} |
462
|
|
|
return exp(0 - $tmp + log(SQRT2PI * $summer / $x)); |
463
|
|
|
} // function _gamma() |
464
|
|
|
|
465
|
|
|
|
466
|
|
|
/*************************************************************************** |
467
|
|
|
* inverse_ncdf.php |
468
|
|
|
* ------------------- |
469
|
|
|
* begin : Friday, January 16, 2004 |
470
|
|
|
* copyright : (C) 2004 Michael Nickerson |
471
|
|
|
* email : [email protected] |
472
|
|
|
* |
473
|
|
|
***************************************************************************/ |
474
|
|
|
private static function _inverse_ncdf($p) { |
475
|
|
|
// Inverse ncdf approximation by Peter J. Acklam, implementation adapted to |
476
|
|
|
// PHP by Michael Nickerson, using Dr. Thomas Ziegler's C implementation as |
477
|
|
|
// a guide. http://home.online.no/~pjacklam/notes/invnorm/index.html |
478
|
|
|
// I have not checked the accuracy of this implementation. Be aware that PHP |
479
|
|
|
// will truncate the coeficcients to 14 digits. |
480
|
|
|
|
481
|
|
|
// You have permission to use and distribute this function freely for |
482
|
|
|
// whatever purpose you want, but please show common courtesy and give credit |
483
|
|
|
// where credit is due. |
484
|
|
|
|
485
|
|
|
// Input paramater is $p - probability - where 0 < p < 1. |
486
|
|
|
|
487
|
|
|
// Coefficients in rational approximations |
488
|
|
|
static $a = array( 1 => -3.969683028665376e+01, |
489
|
|
|
2 => 2.209460984245205e+02, |
490
|
|
|
3 => -2.759285104469687e+02, |
491
|
|
|
4 => 1.383577518672690e+02, |
492
|
|
|
5 => -3.066479806614716e+01, |
493
|
|
|
6 => 2.506628277459239e+00 |
494
|
|
|
); |
495
|
|
|
|
496
|
|
|
static $b = array( 1 => -5.447609879822406e+01, |
497
|
|
|
2 => 1.615858368580409e+02, |
498
|
|
|
3 => -1.556989798598866e+02, |
499
|
|
|
4 => 6.680131188771972e+01, |
500
|
|
|
5 => -1.328068155288572e+01 |
501
|
|
|
); |
502
|
|
|
|
503
|
|
|
static $c = array( 1 => -7.784894002430293e-03, |
504
|
|
|
2 => -3.223964580411365e-01, |
505
|
|
|
3 => -2.400758277161838e+00, |
506
|
|
|
4 => -2.549732539343734e+00, |
507
|
|
|
5 => 4.374664141464968e+00, |
508
|
|
|
6 => 2.938163982698783e+00 |
509
|
|
|
); |
510
|
|
|
|
511
|
|
|
static $d = array( 1 => 7.784695709041462e-03, |
512
|
|
|
2 => 3.224671290700398e-01, |
513
|
|
|
3 => 2.445134137142996e+00, |
514
|
|
|
4 => 3.754408661907416e+00 |
515
|
|
|
); |
516
|
|
|
|
517
|
|
|
// Define lower and upper region break-points. |
518
|
|
|
$p_low = 0.02425; //Use lower region approx. below this |
519
|
|
|
$p_high = 1 - $p_low; //Use upper region approx. above this |
520
|
|
|
|
521
|
|
|
if (0 < $p && $p < $p_low) { |
522
|
|
|
// Rational approximation for lower region. |
523
|
|
|
$q = sqrt(-2 * log($p)); |
524
|
|
|
return ((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) / |
525
|
|
|
(((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1); |
526
|
|
|
} elseif ($p_low <= $p && $p <= $p_high) { |
527
|
|
|
// Rational approximation for central region. |
528
|
|
|
$q = $p - 0.5; |
529
|
|
|
$r = $q * $q; |
530
|
|
|
return ((((($a[1] * $r + $a[2]) * $r + $a[3]) * $r + $a[4]) * $r + $a[5]) * $r + $a[6]) * $q / |
531
|
|
|
((((($b[1] * $r + $b[2]) * $r + $b[3]) * $r + $b[4]) * $r + $b[5]) * $r + 1); |
532
|
|
|
} elseif ($p_high < $p && $p < 1) { |
533
|
|
|
// Rational approximation for upper region. |
534
|
|
|
$q = sqrt(-2 * log(1 - $p)); |
535
|
|
|
return -((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) / |
536
|
|
|
(((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1); |
537
|
|
|
} |
538
|
|
|
// If 0 < p < 1, return a null value |
539
|
|
|
return PHPExcel_Calculation_Functions::NULL(); |
540
|
|
|
} // function _inverse_ncdf() |
541
|
|
|
|
542
|
|
|
|
543
|
|
|
private static function _inverse_ncdf2($prob) { |
|
|
|
|
544
|
|
|
// Approximation of inverse standard normal CDF developed by |
545
|
|
|
// B. Moro, "The Full Monte," Risk 8(2), Feb 1995, 57-58. |
546
|
|
|
|
547
|
|
|
$a1 = 2.50662823884; |
548
|
|
|
$a2 = -18.61500062529; |
549
|
|
|
$a3 = 41.39119773534; |
550
|
|
|
$a4 = -25.44106049637; |
551
|
|
|
|
552
|
|
|
$b1 = -8.4735109309; |
553
|
|
|
$b2 = 23.08336743743; |
554
|
|
|
$b3 = -21.06224101826; |
555
|
|
|
$b4 = 3.13082909833; |
556
|
|
|
|
557
|
|
|
$c1 = 0.337475482272615; |
558
|
|
|
$c2 = 0.976169019091719; |
559
|
|
|
$c3 = 0.160797971491821; |
560
|
|
|
$c4 = 2.76438810333863E-02; |
561
|
|
|
$c5 = 3.8405729373609E-03; |
562
|
|
|
$c6 = 3.951896511919E-04; |
563
|
|
|
$c7 = 3.21767881768E-05; |
564
|
|
|
$c8 = 2.888167364E-07; |
565
|
|
|
$c9 = 3.960315187E-07; |
566
|
|
|
|
567
|
|
|
$y = $prob - 0.5; |
568
|
|
|
if (abs($y) < 0.42) { |
569
|
|
|
$z = ($y * $y); |
570
|
|
|
$z = $y * ((($a4 * $z + $a3) * $z + $a2) * $z + $a1) / (((($b4 * $z + $b3) * $z + $b2) * $z + $b1) * $z + 1); |
571
|
|
|
} else { |
572
|
|
|
if ($y > 0) { |
573
|
|
|
$z = log(-log(1 - $prob)); |
574
|
|
|
} else { |
575
|
|
|
$z = log(-log($prob)); |
576
|
|
|
} |
577
|
|
|
$z = $c1 + $z * ($c2 + $z * ($c3 + $z * ($c4 + $z * ($c5 + $z * ($c6 + $z * ($c7 + $z * ($c8 + $z * $c9))))))); |
578
|
|
|
if ($y < 0) { |
579
|
|
|
$z = -$z; |
580
|
|
|
} |
581
|
|
|
} |
582
|
|
|
return $z; |
583
|
|
|
} // function _inverse_ncdf2() |
584
|
|
|
|
585
|
|
|
|
586
|
|
|
private static function _inverse_ncdf3($p) { |
|
|
|
|
587
|
|
|
// ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3. |
588
|
|
|
// Produces the normal deviate Z corresponding to a given lower |
589
|
|
|
// tail area of P; Z is accurate to about 1 part in 10**16. |
590
|
|
|
// |
591
|
|
|
// This is a PHP version of the original FORTRAN code that can |
592
|
|
|
// be found at http://lib.stat.cmu.edu/apstat/ |
593
|
|
|
$split1 = 0.425; |
594
|
|
|
$split2 = 5; |
595
|
|
|
$const1 = 0.180625; |
596
|
|
|
$const2 = 1.6; |
597
|
|
|
|
598
|
|
|
// coefficients for p close to 0.5 |
599
|
|
|
$a0 = 3.3871328727963666080; |
600
|
|
|
$a1 = 1.3314166789178437745E+2; |
601
|
|
|
$a2 = 1.9715909503065514427E+3; |
602
|
|
|
$a3 = 1.3731693765509461125E+4; |
603
|
|
|
$a4 = 4.5921953931549871457E+4; |
604
|
|
|
$a5 = 6.7265770927008700853E+4; |
605
|
|
|
$a6 = 3.3430575583588128105E+4; |
606
|
|
|
$a7 = 2.5090809287301226727E+3; |
607
|
|
|
|
608
|
|
|
$b1 = 4.2313330701600911252E+1; |
609
|
|
|
$b2 = 6.8718700749205790830E+2; |
610
|
|
|
$b3 = 5.3941960214247511077E+3; |
611
|
|
|
$b4 = 2.1213794301586595867E+4; |
612
|
|
|
$b5 = 3.9307895800092710610E+4; |
613
|
|
|
$b6 = 2.8729085735721942674E+4; |
614
|
|
|
$b7 = 5.2264952788528545610E+3; |
615
|
|
|
|
616
|
|
|
// coefficients for p not close to 0, 0.5 or 1. |
617
|
|
|
$c0 = 1.42343711074968357734; |
618
|
|
|
$c1 = 4.63033784615654529590; |
619
|
|
|
$c2 = 5.76949722146069140550; |
620
|
|
|
$c3 = 3.64784832476320460504; |
621
|
|
|
$c4 = 1.27045825245236838258; |
622
|
|
|
$c5 = 2.41780725177450611770E-1; |
623
|
|
|
$c6 = 2.27238449892691845833E-2; |
624
|
|
|
$c7 = 7.74545014278341407640E-4; |
625
|
|
|
|
626
|
|
|
$d1 = 2.05319162663775882187; |
627
|
|
|
$d2 = 1.67638483018380384940; |
628
|
|
|
$d3 = 6.89767334985100004550E-1; |
629
|
|
|
$d4 = 1.48103976427480074590E-1; |
630
|
|
|
$d5 = 1.51986665636164571966E-2; |
631
|
|
|
$d6 = 5.47593808499534494600E-4; |
632
|
|
|
$d7 = 1.05075007164441684324E-9; |
633
|
|
|
|
634
|
|
|
// coefficients for p near 0 or 1. |
635
|
|
|
$e0 = 6.65790464350110377720; |
636
|
|
|
$e1 = 5.46378491116411436990; |
637
|
|
|
$e2 = 1.78482653991729133580; |
638
|
|
|
$e3 = 2.96560571828504891230E-1; |
639
|
|
|
$e4 = 2.65321895265761230930E-2; |
640
|
|
|
$e5 = 1.24266094738807843860E-3; |
641
|
|
|
$e6 = 2.71155556874348757815E-5; |
642
|
|
|
$e7 = 2.01033439929228813265E-7; |
643
|
|
|
|
644
|
|
|
$f1 = 5.99832206555887937690E-1; |
645
|
|
|
$f2 = 1.36929880922735805310E-1; |
646
|
|
|
$f3 = 1.48753612908506148525E-2; |
647
|
|
|
$f4 = 7.86869131145613259100E-4; |
648
|
|
|
$f5 = 1.84631831751005468180E-5; |
649
|
|
|
$f6 = 1.42151175831644588870E-7; |
650
|
|
|
$f7 = 2.04426310338993978564E-15; |
651
|
|
|
|
652
|
|
|
$q = $p - 0.5; |
653
|
|
|
|
654
|
|
|
// computation for p close to 0.5 |
655
|
|
|
if (abs($q) <= split1) { |
656
|
|
|
$R = $const1 - $q * $q; |
657
|
|
|
$z = $q * ((((((($a7 * $R + $a6) * $R + $a5) * $R + $a4) * $R + $a3) * $R + $a2) * $R + $a1) * $R + $a0) / |
658
|
|
|
((((((($b7 * $R + $b6) * $R + $b5) * $R + $b4) * $R + $b3) * $R + $b2) * $R + $b1) * $R + 1); |
659
|
|
|
} else { |
660
|
|
|
if ($q < 0) { |
661
|
|
|
$R = $p; |
662
|
|
|
} else { |
663
|
|
|
$R = 1 - $p; |
664
|
|
|
} |
665
|
|
|
$R = pow(-log($R),2); |
666
|
|
|
|
667
|
|
|
// computation for p not close to 0, 0.5 or 1. |
668
|
|
|
If ($R <= $split2) { |
669
|
|
|
$R = $R - $const2; |
670
|
|
|
$z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) / |
671
|
|
|
((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1); |
672
|
|
|
} else { |
673
|
|
|
// computation for p near 0 or 1. |
674
|
|
|
$R = $R - $split2; |
675
|
|
|
$z = ((((((($e7 * $R + $e6) * $R + $e5) * $R + $e4) * $R + $e3) * $R + $e2) * $R + $e1) * $R + $e0) / |
676
|
|
|
((((((($f7 * $R + $f6) * $R + $f5) * $R + $f4) * $R + $f3) * $R + $f2) * $R + $f1) * $R + 1); |
677
|
|
|
} |
678
|
|
|
if ($q < 0) { |
679
|
|
|
$z = -$z; |
680
|
|
|
} |
681
|
|
|
} |
682
|
|
|
return $z; |
683
|
|
|
} // function _inverse_ncdf3() |
684
|
|
|
|
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* AVEDEV |
688
|
|
|
* |
689
|
|
|
* Returns the average of the absolute deviations of data points from their mean. |
690
|
|
|
* AVEDEV is a measure of the variability in a data set. |
691
|
|
|
* |
692
|
|
|
* Excel Function: |
693
|
|
|
* AVEDEV(value1[,value2[, ...]]) |
694
|
|
|
* |
695
|
|
|
* @access public |
696
|
|
|
* @category Statistical Functions |
697
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
698
|
|
|
* @return float |
699
|
|
|
*/ |
700
|
|
View Code Duplication |
public static function AVEDEV() { |
701
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
702
|
|
|
|
703
|
|
|
// Return value |
704
|
|
|
$returnValue = null; |
705
|
|
|
|
706
|
|
|
$aMean = self::AVERAGE($aArgs); |
707
|
|
|
if ($aMean != PHPExcel_Calculation_Functions::DIV0()) { |
708
|
|
|
$aCount = 0; |
709
|
|
|
foreach ($aArgs as $k => $arg) { |
710
|
|
|
if ((is_bool($arg)) && |
711
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
712
|
|
|
$arg = (integer) $arg; |
713
|
|
|
} |
714
|
|
|
// Is it a numeric value? |
715
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
716
|
|
|
if (is_null($returnValue)) { |
717
|
|
|
$returnValue = abs($arg - $aMean); |
718
|
|
|
} else { |
719
|
|
|
$returnValue += abs($arg - $aMean); |
720
|
|
|
} |
721
|
|
|
++$aCount; |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
// Return |
726
|
|
|
if ($aCount == 0) { |
727
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
728
|
|
|
} |
729
|
|
|
return $returnValue / $aCount; |
730
|
|
|
} |
731
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
732
|
|
|
} // function AVEDEV() |
733
|
|
|
|
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* AVERAGE |
737
|
|
|
* |
738
|
|
|
* Returns the average (arithmetic mean) of the arguments |
739
|
|
|
* |
740
|
|
|
* Excel Function: |
741
|
|
|
* AVERAGE(value1[,value2[, ...]]) |
742
|
|
|
* |
743
|
|
|
* @access public |
744
|
|
|
* @category Statistical Functions |
745
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
746
|
|
|
* @return float |
747
|
|
|
*/ |
748
|
|
|
public static function AVERAGE() { |
749
|
|
|
$returnValue = $aCount = 0; |
750
|
|
|
|
751
|
|
|
// Loop through arguments |
752
|
|
|
foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg) { |
753
|
|
|
if ((is_bool($arg)) && |
754
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
755
|
|
|
$arg = (integer) $arg; |
756
|
|
|
} |
757
|
|
|
// Is it a numeric value? |
758
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
759
|
|
|
if (is_null($returnValue)) { |
760
|
|
|
$returnValue = $arg; |
761
|
|
|
} else { |
762
|
|
|
$returnValue += $arg; |
763
|
|
|
} |
764
|
|
|
++$aCount; |
765
|
|
|
} |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
// Return |
769
|
|
|
if ($aCount > 0) { |
770
|
|
|
return $returnValue / $aCount; |
771
|
|
|
} else { |
772
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
773
|
|
|
} |
774
|
|
|
} // function AVERAGE() |
775
|
|
|
|
776
|
|
|
|
777
|
|
|
/** |
778
|
|
|
* AVERAGEA |
779
|
|
|
* |
780
|
|
|
* Returns the average of its arguments, including numbers, text, and logical values |
781
|
|
|
* |
782
|
|
|
* Excel Function: |
783
|
|
|
* AVERAGEA(value1[,value2[, ...]]) |
784
|
|
|
* |
785
|
|
|
* @access public |
786
|
|
|
* @category Statistical Functions |
787
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
788
|
|
|
* @return float |
789
|
|
|
*/ |
790
|
|
|
public static function AVERAGEA() { |
791
|
|
|
// Return value |
792
|
|
|
$returnValue = null; |
793
|
|
|
|
794
|
|
|
$aCount = 0; |
795
|
|
|
// Loop through arguments |
796
|
|
|
foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg) { |
797
|
|
|
if ((is_bool($arg)) && |
|
|
|
|
798
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
799
|
|
|
} else { |
800
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) { |
801
|
|
|
if (is_bool($arg)) { |
802
|
|
|
$arg = (integer) $arg; |
803
|
|
|
} elseif (is_string($arg)) { |
804
|
|
|
$arg = 0; |
805
|
|
|
} |
806
|
|
|
if (is_null($returnValue)) { |
807
|
|
|
$returnValue = $arg; |
808
|
|
|
} else { |
809
|
|
|
$returnValue += $arg; |
810
|
|
|
} |
811
|
|
|
++$aCount; |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
// Return |
817
|
|
|
if ($aCount > 0) { |
818
|
|
|
return $returnValue / $aCount; |
819
|
|
|
} else { |
820
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
821
|
|
|
} |
822
|
|
|
} // function AVERAGEA() |
823
|
|
|
|
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* AVERAGEIF |
827
|
|
|
* |
828
|
|
|
* Returns the average value from a range of cells that contain numbers within the list of arguments |
829
|
|
|
* |
830
|
|
|
* Excel Function: |
831
|
|
|
* AVERAGEIF(value1[,value2[, ...]],condition) |
832
|
|
|
* |
833
|
|
|
* @access public |
834
|
|
|
* @category Mathematical and Trigonometric Functions |
835
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
836
|
|
|
* @param string $condition The criteria that defines which cells will be checked. |
837
|
|
|
* @return float |
838
|
|
|
*/ |
839
|
|
|
public static function AVERAGEIF($aArgs,$condition,$averageArgs = array()) { |
840
|
|
|
// Return value |
841
|
|
|
$returnValue = 0; |
842
|
|
|
|
843
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); |
844
|
|
|
$averageArgs = PHPExcel_Calculation_Functions::flattenArray($averageArgs); |
845
|
|
|
if (empty($averageArgs)) { |
846
|
|
|
$averageArgs = $aArgs; |
847
|
|
|
} |
848
|
|
|
$condition = PHPExcel_Calculation_Functions::_ifCondition($condition); |
849
|
|
|
// Loop through arguments |
850
|
|
|
$aCount = 0; |
851
|
|
|
foreach ($aArgs as $key => $arg) { |
852
|
|
|
if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } |
853
|
|
|
$testCondition = '='.$arg.$condition; |
854
|
|
|
if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { |
855
|
|
|
if ((is_null($returnValue)) || ($arg > $returnValue)) { |
856
|
|
|
$returnValue += $arg; |
857
|
|
|
++$aCount; |
858
|
|
|
} |
859
|
|
|
} |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
// Return |
863
|
|
|
if ($aCount > 0) { |
864
|
|
|
return $returnValue / $aCount; |
865
|
|
|
} else { |
866
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
867
|
|
|
} |
868
|
|
|
} // function AVERAGEIF() |
869
|
|
|
|
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* BETADIST |
873
|
|
|
* |
874
|
|
|
* Returns the beta distribution. |
875
|
|
|
* |
876
|
|
|
* @param float $value Value at which you want to evaluate the distribution |
877
|
|
|
* @param float $alpha Parameter to the distribution |
878
|
|
|
* @param float $beta Parameter to the distribution |
879
|
|
|
* @param boolean $cumulative |
|
|
|
|
880
|
|
|
* @return float |
881
|
|
|
* |
882
|
|
|
*/ |
883
|
|
|
public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) { |
884
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
885
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
886
|
|
|
$beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); |
887
|
|
|
$rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); |
888
|
|
|
$rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); |
889
|
|
|
|
890
|
|
|
if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) { |
891
|
|
|
if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) { |
892
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
893
|
|
|
} |
894
|
|
|
if ($rMin > $rMax) { |
895
|
|
|
$tmp = $rMin; |
896
|
|
|
$rMin = $rMax; |
897
|
|
|
$rMax = $tmp; |
898
|
|
|
} |
899
|
|
|
$value -= $rMin; |
900
|
|
|
$value /= ($rMax - $rMin); |
901
|
|
|
return self::_incompleteBeta($value,$alpha,$beta); |
902
|
|
|
} |
903
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
904
|
|
|
} // function BETADIST() |
905
|
|
|
|
906
|
|
|
|
907
|
|
|
/** |
908
|
|
|
* BETAINV |
909
|
|
|
* |
910
|
|
|
* Returns the inverse of the beta distribution. |
911
|
|
|
* |
912
|
|
|
* @param float $probability Probability at which you want to evaluate the distribution |
913
|
|
|
* @param float $alpha Parameter to the distribution |
914
|
|
|
* @param float $beta Parameter to the distribution |
915
|
|
|
* @param boolean $cumulative |
|
|
|
|
916
|
|
|
* @return float |
917
|
|
|
* |
918
|
|
|
*/ |
919
|
|
|
public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) { |
920
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
921
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
922
|
|
|
$beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); |
923
|
|
|
$rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); |
924
|
|
|
$rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); |
925
|
|
|
|
926
|
|
|
if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) { |
927
|
|
View Code Duplication |
if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) { |
928
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
929
|
|
|
} |
930
|
|
|
if ($rMin > $rMax) { |
931
|
|
|
$tmp = $rMin; |
932
|
|
|
$rMin = $rMax; |
933
|
|
|
$rMax = $tmp; |
934
|
|
|
} |
935
|
|
|
$a = 0; |
936
|
|
|
$b = 2; |
937
|
|
|
|
938
|
|
|
$i = 0; |
939
|
|
|
while ((($b - $a) > PRECISION) && ($i++ < MAX_ITERATIONS)) { |
940
|
|
|
$guess = ($a + $b) / 2; |
941
|
|
|
$result = self::BETADIST($guess, $alpha, $beta); |
942
|
|
|
if (($result == $probability) || ($result == 0)) { |
943
|
|
|
$b = $a; |
944
|
|
|
} elseif ($result > $probability) { |
945
|
|
|
$b = $guess; |
946
|
|
|
} else { |
947
|
|
|
$a = $guess; |
948
|
|
|
} |
949
|
|
|
} |
950
|
|
|
if ($i == MAX_ITERATIONS) { |
951
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
952
|
|
|
} |
953
|
|
|
return round($rMin + $guess * ($rMax - $rMin),12); |
|
|
|
|
954
|
|
|
} |
955
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
956
|
|
|
} // function BETAINV() |
957
|
|
|
|
958
|
|
|
|
959
|
|
|
/** |
960
|
|
|
* BINOMDIST |
961
|
|
|
* |
962
|
|
|
* Returns the individual term binomial distribution probability. Use BINOMDIST in problems with |
963
|
|
|
* a fixed number of tests or trials, when the outcomes of any trial are only success or failure, |
964
|
|
|
* when trials are independent, and when the probability of success is constant throughout the |
965
|
|
|
* experiment. For example, BINOMDIST can calculate the probability that two of the next three |
966
|
|
|
* babies born are male. |
967
|
|
|
* |
968
|
|
|
* @param float $value Number of successes in trials |
969
|
|
|
* @param float $trials Number of trials |
970
|
|
|
* @param float $probability Probability of success on each trial |
971
|
|
|
* @param boolean $cumulative |
972
|
|
|
* @return float |
973
|
|
|
* |
974
|
|
|
* @todo Cumulative distribution function |
975
|
|
|
* |
976
|
|
|
*/ |
977
|
|
|
public static function BINOMDIST($value, $trials, $probability, $cumulative) { |
978
|
|
|
$value = floor(PHPExcel_Calculation_Functions::flattenSingleValue($value)); |
979
|
|
|
$trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); |
980
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
981
|
|
|
|
982
|
|
|
if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) { |
983
|
|
|
if (($value < 0) || ($value > $trials)) { |
984
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
985
|
|
|
} |
986
|
|
|
if (($probability < 0) || ($probability > 1)) { |
987
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
988
|
|
|
} |
989
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
990
|
|
|
if ($cumulative) { |
991
|
|
|
$summer = 0; |
992
|
|
|
for ($i = 0; $i <= $value; ++$i) { |
993
|
|
|
$summer += PHPExcel_Calculation_MathTrig::COMBIN($trials,$i) * pow($probability,$i) * pow(1 - $probability,$trials - $i); |
994
|
|
|
} |
995
|
|
|
return $summer; |
996
|
|
|
} else { |
997
|
|
|
return PHPExcel_Calculation_MathTrig::COMBIN($trials,$value) * pow($probability,$value) * pow(1 - $probability,$trials - $value) ; |
998
|
|
|
} |
999
|
|
|
} |
1000
|
|
|
} |
1001
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1002
|
|
|
} // function BINOMDIST() |
1003
|
|
|
|
1004
|
|
|
|
1005
|
|
|
/** |
1006
|
|
|
* CHIDIST |
1007
|
|
|
* |
1008
|
|
|
* Returns the one-tailed probability of the chi-squared distribution. |
1009
|
|
|
* |
1010
|
|
|
* @param float $value Value for the function |
1011
|
|
|
* @param float $degrees degrees of freedom |
1012
|
|
|
* @return float |
1013
|
|
|
*/ |
1014
|
|
|
public static function CHIDIST($value, $degrees) { |
1015
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1016
|
|
|
$degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); |
1017
|
|
|
|
1018
|
|
|
if ((is_numeric($value)) && (is_numeric($degrees))) { |
1019
|
|
|
if ($degrees < 1) { |
1020
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1021
|
|
|
} |
1022
|
|
|
if ($value < 0) { |
1023
|
|
|
if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) { |
1024
|
|
|
return 1; |
1025
|
|
|
} |
1026
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1027
|
|
|
} |
1028
|
|
|
return 1 - (self::_incompleteGamma($degrees/2,$value/2) / self::_gamma($degrees/2)); |
1029
|
|
|
} |
1030
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1031
|
|
|
} // function CHIDIST() |
1032
|
|
|
|
1033
|
|
|
|
1034
|
|
|
/** |
1035
|
|
|
* CHIINV |
1036
|
|
|
* |
1037
|
|
|
* Returns the one-tailed probability of the chi-squared distribution. |
1038
|
|
|
* |
1039
|
|
|
* @param float $probability Probability for the function |
1040
|
|
|
* @param float $degrees degrees of freedom |
1041
|
|
|
* @return float |
1042
|
|
|
*/ |
1043
|
|
View Code Duplication |
public static function CHIINV($probability, $degrees) { |
1044
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
1045
|
|
|
$degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); |
1046
|
|
|
|
1047
|
|
|
if ((is_numeric($probability)) && (is_numeric($degrees))) { |
1048
|
|
|
|
1049
|
|
|
$xLo = 100; |
1050
|
|
|
$xHi = 0; |
1051
|
|
|
|
1052
|
|
|
$x = $xNew = 1; |
1053
|
|
|
$dx = 1; |
1054
|
|
|
$i = 0; |
1055
|
|
|
|
1056
|
|
|
while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) { |
1057
|
|
|
// Apply Newton-Raphson step |
1058
|
|
|
$result = self::CHIDIST($x, $degrees); |
1059
|
|
|
$error = $result - $probability; |
1060
|
|
|
if ($error == 0.0) { |
1061
|
|
|
$dx = 0; |
1062
|
|
|
} elseif ($error < 0.0) { |
1063
|
|
|
$xLo = $x; |
1064
|
|
|
} else { |
1065
|
|
|
$xHi = $x; |
1066
|
|
|
} |
1067
|
|
|
// Avoid division by zero |
1068
|
|
|
if ($result != 0.0) { |
1069
|
|
|
$dx = $error / $result; |
1070
|
|
|
$xNew = $x - $dx; |
1071
|
|
|
} |
1072
|
|
|
// If the NR fails to converge (which for example may be the |
1073
|
|
|
// case if the initial guess is too rough) we apply a bisection |
1074
|
|
|
// step to determine a more narrow interval around the root. |
1075
|
|
|
if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) { |
1076
|
|
|
$xNew = ($xLo + $xHi) / 2; |
1077
|
|
|
$dx = $xNew - $x; |
1078
|
|
|
} |
1079
|
|
|
$x = $xNew; |
1080
|
|
|
} |
1081
|
|
|
if ($i == MAX_ITERATIONS) { |
1082
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1083
|
|
|
} |
1084
|
|
|
return round($x,12); |
1085
|
|
|
} |
1086
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1087
|
|
|
} // function CHIINV() |
1088
|
|
|
|
1089
|
|
|
|
1090
|
|
|
/** |
1091
|
|
|
* CONFIDENCE |
1092
|
|
|
* |
1093
|
|
|
* Returns the confidence interval for a population mean |
1094
|
|
|
* |
1095
|
|
|
* @param float $alpha |
1096
|
|
|
* @param float $stdDev Standard Deviation |
1097
|
|
|
* @param float $size |
1098
|
|
|
* @return float |
1099
|
|
|
* |
1100
|
|
|
*/ |
1101
|
|
|
public static function CONFIDENCE($alpha,$stdDev,$size) { |
1102
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
1103
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
1104
|
|
|
$size = floor(PHPExcel_Calculation_Functions::flattenSingleValue($size)); |
1105
|
|
|
|
1106
|
|
|
if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) { |
1107
|
|
|
if (($alpha <= 0) || ($alpha >= 1)) { |
1108
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1109
|
|
|
} |
1110
|
|
|
if (($stdDev <= 0) || ($size < 1)) { |
1111
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1112
|
|
|
} |
1113
|
|
|
return self::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size); |
1114
|
|
|
} |
1115
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1116
|
|
|
} // function CONFIDENCE() |
1117
|
|
|
|
1118
|
|
|
|
1119
|
|
|
/** |
1120
|
|
|
* CORREL |
1121
|
|
|
* |
1122
|
|
|
* Returns covariance, the average of the products of deviations for each data point pair. |
1123
|
|
|
* |
1124
|
|
|
* @param array of mixed Data Series Y |
1125
|
|
|
* @param array of mixed Data Series X |
1126
|
|
|
* @return float |
1127
|
|
|
*/ |
1128
|
|
|
public static function CORREL($yValues,$xValues=null) { |
1129
|
|
|
if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) { |
1130
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1131
|
|
|
} |
1132
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
1133
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1134
|
|
|
} |
1135
|
|
|
$yValueCount = count($yValues); |
1136
|
|
|
$xValueCount = count($xValues); |
1137
|
|
|
|
1138
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
1139
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1140
|
|
|
} elseif ($yValueCount == 1) { |
1141
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
1142
|
|
|
} |
1143
|
|
|
|
1144
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
1145
|
|
|
return $bestFitLinear->getCorrelation(); |
1146
|
|
|
} // function CORREL() |
1147
|
|
|
|
1148
|
|
|
|
1149
|
|
|
/** |
1150
|
|
|
* COUNT |
1151
|
|
|
* |
1152
|
|
|
* Counts the number of cells that contain numbers within the list of arguments |
1153
|
|
|
* |
1154
|
|
|
* Excel Function: |
1155
|
|
|
* COUNT(value1[,value2[, ...]]) |
1156
|
|
|
* |
1157
|
|
|
* @access public |
1158
|
|
|
* @category Statistical Functions |
1159
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1160
|
|
|
* @return int |
1161
|
|
|
*/ |
1162
|
|
|
public static function COUNT() { |
1163
|
|
|
// Return value |
1164
|
|
|
$returnValue = 0; |
1165
|
|
|
|
1166
|
|
|
// Loop through arguments |
1167
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
1168
|
|
|
foreach ($aArgs as $k => $arg) { |
1169
|
|
|
if ((is_bool($arg)) && |
1170
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
1171
|
|
|
$arg = (integer) $arg; |
1172
|
|
|
} |
1173
|
|
|
// Is it a numeric value? |
1174
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
1175
|
|
|
++$returnValue; |
1176
|
|
|
} |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
// Return |
1180
|
|
|
return $returnValue; |
1181
|
|
|
} // function COUNT() |
1182
|
|
|
|
1183
|
|
|
|
1184
|
|
|
/** |
1185
|
|
|
* COUNTA |
1186
|
|
|
* |
1187
|
|
|
* Counts the number of cells that are not empty within the list of arguments |
1188
|
|
|
* |
1189
|
|
|
* Excel Function: |
1190
|
|
|
* COUNTA(value1[,value2[, ...]]) |
1191
|
|
|
* |
1192
|
|
|
* @access public |
1193
|
|
|
* @category Statistical Functions |
1194
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1195
|
|
|
* @return int |
1196
|
|
|
*/ |
1197
|
|
View Code Duplication |
public static function COUNTA() { |
1198
|
|
|
// Return value |
1199
|
|
|
$returnValue = 0; |
1200
|
|
|
|
1201
|
|
|
// Loop through arguments |
1202
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
1203
|
|
|
foreach ($aArgs as $arg) { |
1204
|
|
|
// Is it a numeric, boolean or string value? |
1205
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) { |
1206
|
|
|
++$returnValue; |
1207
|
|
|
} |
1208
|
|
|
} |
1209
|
|
|
|
1210
|
|
|
// Return |
1211
|
|
|
return $returnValue; |
1212
|
|
|
} // function COUNTA() |
1213
|
|
|
|
1214
|
|
|
|
1215
|
|
|
/** |
1216
|
|
|
* COUNTBLANK |
1217
|
|
|
* |
1218
|
|
|
* Counts the number of empty cells within the list of arguments |
1219
|
|
|
* |
1220
|
|
|
* Excel Function: |
1221
|
|
|
* COUNTBLANK(value1[,value2[, ...]]) |
1222
|
|
|
* |
1223
|
|
|
* @access public |
1224
|
|
|
* @category Statistical Functions |
1225
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1226
|
|
|
* @return int |
1227
|
|
|
*/ |
1228
|
|
View Code Duplication |
public static function COUNTBLANK() { |
1229
|
|
|
// Return value |
1230
|
|
|
$returnValue = 0; |
1231
|
|
|
|
1232
|
|
|
// Loop through arguments |
1233
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
1234
|
|
|
foreach ($aArgs as $arg) { |
1235
|
|
|
// Is it a blank cell? |
1236
|
|
|
if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) { |
1237
|
|
|
++$returnValue; |
1238
|
|
|
} |
1239
|
|
|
} |
1240
|
|
|
|
1241
|
|
|
// Return |
1242
|
|
|
return $returnValue; |
1243
|
|
|
} // function COUNTBLANK() |
1244
|
|
|
|
1245
|
|
|
|
1246
|
|
|
/** |
1247
|
|
|
* COUNTIF |
1248
|
|
|
* |
1249
|
|
|
* Counts the number of cells that contain numbers within the list of arguments |
1250
|
|
|
* |
1251
|
|
|
* Excel Function: |
1252
|
|
|
* COUNTIF(value1[,value2[, ...]],condition) |
1253
|
|
|
* |
1254
|
|
|
* @access public |
1255
|
|
|
* @category Statistical Functions |
1256
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1257
|
|
|
* @param string $condition The criteria that defines which cells will be counted. |
1258
|
|
|
* @return int |
1259
|
|
|
*/ |
1260
|
|
|
public static function COUNTIF($aArgs,$condition) { |
1261
|
|
|
// Return value |
1262
|
|
|
$returnValue = 0; |
1263
|
|
|
|
1264
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); |
1265
|
|
|
$condition = PHPExcel_Calculation_Functions::_ifCondition($condition); |
1266
|
|
|
// Loop through arguments |
1267
|
|
|
foreach ($aArgs as $arg) { |
1268
|
|
|
if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } |
1269
|
|
|
$testCondition = '='.$arg.$condition; |
1270
|
|
|
if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { |
1271
|
|
|
// Is it a value within our criteria |
1272
|
|
|
++$returnValue; |
1273
|
|
|
} |
1274
|
|
|
} |
1275
|
|
|
|
1276
|
|
|
// Return |
1277
|
|
|
return $returnValue; |
1278
|
|
|
} // function COUNTIF() |
1279
|
|
|
|
1280
|
|
|
|
1281
|
|
|
/** |
1282
|
|
|
* COVAR |
1283
|
|
|
* |
1284
|
|
|
* Returns covariance, the average of the products of deviations for each data point pair. |
1285
|
|
|
* |
1286
|
|
|
* @param array of mixed Data Series Y |
1287
|
|
|
* @param array of mixed Data Series X |
1288
|
|
|
* @return float |
1289
|
|
|
*/ |
1290
|
|
View Code Duplication |
public static function COVAR($yValues,$xValues) { |
1291
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
1292
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1293
|
|
|
} |
1294
|
|
|
$yValueCount = count($yValues); |
1295
|
|
|
$xValueCount = count($xValues); |
1296
|
|
|
|
1297
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
1298
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1299
|
|
|
} elseif ($yValueCount == 1) { |
1300
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
1301
|
|
|
} |
1302
|
|
|
|
1303
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
1304
|
|
|
return $bestFitLinear->getCovariance(); |
1305
|
|
|
} // function COVAR() |
1306
|
|
|
|
1307
|
|
|
|
1308
|
|
|
/** |
1309
|
|
|
* CRITBINOM |
1310
|
|
|
* |
1311
|
|
|
* Returns the smallest value for which the cumulative binomial distribution is greater |
1312
|
|
|
* than or equal to a criterion value |
1313
|
|
|
* |
1314
|
|
|
* See http://support.microsoft.com/kb/828117/ for details of the algorithm used |
1315
|
|
|
* |
1316
|
|
|
* @param float $trials number of Bernoulli trials |
1317
|
|
|
* @param float $probability probability of a success on each trial |
1318
|
|
|
* @param float $alpha criterion value |
1319
|
|
|
* @return int |
1320
|
|
|
* |
1321
|
|
|
* @todo Warning. This implementation differs from the algorithm detailed on the MS |
1322
|
|
|
* web site in that $CumPGuessMinus1 = $CumPGuess - 1 rather than $CumPGuess - $PGuess |
1323
|
|
|
* This eliminates a potential endless loop error, but may have an adverse affect on the |
1324
|
|
|
* accuracy of the function (although all my tests have so far returned correct results). |
1325
|
|
|
* |
1326
|
|
|
*/ |
1327
|
|
|
public static function CRITBINOM($trials, $probability, $alpha) { |
1328
|
|
|
$trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); |
1329
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
1330
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
1331
|
|
|
|
1332
|
|
|
if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) { |
1333
|
|
|
if ($trials < 0) { |
1334
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1335
|
|
|
} |
1336
|
|
|
if (($probability < 0) || ($probability > 1)) { |
1337
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1338
|
|
|
} |
1339
|
|
|
if (($alpha < 0) || ($alpha > 1)) { |
1340
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1341
|
|
|
} |
1342
|
|
|
if ($alpha <= 0.5) { |
1343
|
|
|
$t = sqrt(log(1 / ($alpha * $alpha))); |
1344
|
|
|
$trialsApprox = 0 - ($t + (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t)); |
1345
|
|
|
} else { |
1346
|
|
|
$t = sqrt(log(1 / pow(1 - $alpha,2))); |
1347
|
|
|
$trialsApprox = $t - (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t); |
1348
|
|
|
} |
1349
|
|
|
$Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability))); |
1350
|
|
|
if ($Guess < 0) { |
1351
|
|
|
$Guess = 0; |
1352
|
|
|
} elseif ($Guess > $trials) { |
1353
|
|
|
$Guess = $trials; |
1354
|
|
|
} |
1355
|
|
|
|
1356
|
|
|
$TotalUnscaledProbability = $UnscaledPGuess = $UnscaledCumPGuess = 0.0; |
1357
|
|
|
$EssentiallyZero = 10e-12; |
1358
|
|
|
|
1359
|
|
|
$m = floor($trials * $probability); |
1360
|
|
|
++$TotalUnscaledProbability; |
1361
|
|
|
if ($m == $Guess) { ++$UnscaledPGuess; } |
1362
|
|
|
if ($m <= $Guess) { ++$UnscaledCumPGuess; } |
1363
|
|
|
|
1364
|
|
|
$PreviousValue = 1; |
1365
|
|
|
$Done = False; |
1366
|
|
|
$k = $m + 1; |
1367
|
|
View Code Duplication |
while ((!$Done) && ($k <= $trials)) { |
1368
|
|
|
$CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability)); |
1369
|
|
|
$TotalUnscaledProbability += $CurrentValue; |
1370
|
|
|
if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; } |
1371
|
|
|
if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; } |
1372
|
|
|
if ($CurrentValue <= $EssentiallyZero) { $Done = True; } |
1373
|
|
|
$PreviousValue = $CurrentValue; |
1374
|
|
|
++$k; |
1375
|
|
|
} |
1376
|
|
|
|
1377
|
|
|
$PreviousValue = 1; |
1378
|
|
|
$Done = False; |
1379
|
|
|
$k = $m - 1; |
1380
|
|
View Code Duplication |
while ((!$Done) && ($k >= 0)) { |
1381
|
|
|
$CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability); |
1382
|
|
|
$TotalUnscaledProbability += $CurrentValue; |
1383
|
|
|
if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; } |
1384
|
|
|
if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; } |
1385
|
|
|
if ($CurrentValue <= $EssentiallyZero) { $Done = True; } |
1386
|
|
|
$PreviousValue = $CurrentValue; |
1387
|
|
|
--$k; |
1388
|
|
|
} |
1389
|
|
|
|
1390
|
|
|
$PGuess = $UnscaledPGuess / $TotalUnscaledProbability; |
1391
|
|
|
$CumPGuess = $UnscaledCumPGuess / $TotalUnscaledProbability; |
1392
|
|
|
|
1393
|
|
|
// $CumPGuessMinus1 = $CumPGuess - $PGuess; |
1394
|
|
|
$CumPGuessMinus1 = $CumPGuess - 1; |
1395
|
|
|
|
1396
|
|
|
while (True) { |
1397
|
|
|
if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) { |
1398
|
|
|
return $Guess; |
1399
|
|
|
} elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) { |
1400
|
|
|
$PGuessPlus1 = $PGuess * ($trials - $Guess) * $probability / $Guess / (1 - $probability); |
1401
|
|
|
$CumPGuessMinus1 = $CumPGuess; |
1402
|
|
|
$CumPGuess = $CumPGuess + $PGuessPlus1; |
1403
|
|
|
$PGuess = $PGuessPlus1; |
1404
|
|
|
++$Guess; |
1405
|
|
|
} elseif (($CumPGuessMinus1 >= $alpha) && ($CumPGuess >= $alpha)) { |
1406
|
|
|
$PGuessMinus1 = $PGuess * $Guess * (1 - $probability) / ($trials - $Guess + 1) / $probability; |
1407
|
|
|
$CumPGuess = $CumPGuessMinus1; |
1408
|
|
|
$CumPGuessMinus1 = $CumPGuessMinus1 - $PGuess; |
1409
|
|
|
$PGuess = $PGuessMinus1; |
1410
|
|
|
--$Guess; |
1411
|
|
|
} |
1412
|
|
|
} |
1413
|
|
|
} |
1414
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1415
|
|
|
} // function CRITBINOM() |
1416
|
|
|
|
1417
|
|
|
|
1418
|
|
|
/** |
1419
|
|
|
* DEVSQ |
1420
|
|
|
* |
1421
|
|
|
* Returns the sum of squares of deviations of data points from their sample mean. |
1422
|
|
|
* |
1423
|
|
|
* Excel Function: |
1424
|
|
|
* DEVSQ(value1[,value2[, ...]]) |
1425
|
|
|
* |
1426
|
|
|
* @access public |
1427
|
|
|
* @category Statistical Functions |
1428
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1429
|
|
|
* @return float |
1430
|
|
|
*/ |
1431
|
|
View Code Duplication |
public static function DEVSQ() { |
1432
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
1433
|
|
|
|
1434
|
|
|
// Return value |
1435
|
|
|
$returnValue = null; |
1436
|
|
|
|
1437
|
|
|
$aMean = self::AVERAGE($aArgs); |
1438
|
|
|
if ($aMean != PHPExcel_Calculation_Functions::DIV0()) { |
1439
|
|
|
$aCount = -1; |
1440
|
|
|
foreach ($aArgs as $k => $arg) { |
1441
|
|
|
// Is it a numeric value? |
1442
|
|
|
if ((is_bool($arg)) && |
1443
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
1444
|
|
|
$arg = (integer) $arg; |
1445
|
|
|
} |
1446
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
1447
|
|
|
if (is_null($returnValue)) { |
1448
|
|
|
$returnValue = pow(($arg - $aMean),2); |
1449
|
|
|
} else { |
1450
|
|
|
$returnValue += pow(($arg - $aMean),2); |
1451
|
|
|
} |
1452
|
|
|
++$aCount; |
1453
|
|
|
} |
1454
|
|
|
} |
1455
|
|
|
|
1456
|
|
|
// Return |
1457
|
|
|
if (is_null($returnValue)) { |
1458
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1459
|
|
|
} else { |
1460
|
|
|
return $returnValue; |
1461
|
|
|
} |
1462
|
|
|
} |
1463
|
|
|
return self::NA(); |
|
|
|
|
1464
|
|
|
} // function DEVSQ() |
1465
|
|
|
|
1466
|
|
|
|
1467
|
|
|
/** |
1468
|
|
|
* EXPONDIST |
1469
|
|
|
* |
1470
|
|
|
* Returns the exponential distribution. Use EXPONDIST to model the time between events, |
1471
|
|
|
* such as how long an automated bank teller takes to deliver cash. For example, you can |
1472
|
|
|
* use EXPONDIST to determine the probability that the process takes at most 1 minute. |
1473
|
|
|
* |
1474
|
|
|
* @param float $value Value of the function |
1475
|
|
|
* @param float $lambda The parameter value |
1476
|
|
|
* @param boolean $cumulative |
1477
|
|
|
* @return float |
1478
|
|
|
*/ |
1479
|
|
|
public static function EXPONDIST($value, $lambda, $cumulative) { |
1480
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1481
|
|
|
$lambda = PHPExcel_Calculation_Functions::flattenSingleValue($lambda); |
1482
|
|
|
$cumulative = PHPExcel_Calculation_Functions::flattenSingleValue($cumulative); |
1483
|
|
|
|
1484
|
|
|
if ((is_numeric($value)) && (is_numeric($lambda))) { |
1485
|
|
|
if (($value < 0) || ($lambda < 0)) { |
1486
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1487
|
|
|
} |
1488
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
1489
|
|
|
if ($cumulative) { |
1490
|
|
|
return 1 - exp(0-$value*$lambda); |
1491
|
|
|
} else { |
1492
|
|
|
return $lambda * exp(0-$value*$lambda); |
1493
|
|
|
} |
1494
|
|
|
} |
1495
|
|
|
} |
1496
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1497
|
|
|
} // function EXPONDIST() |
1498
|
|
|
|
1499
|
|
|
|
1500
|
|
|
/** |
1501
|
|
|
* FISHER |
1502
|
|
|
* |
1503
|
|
|
* Returns the Fisher transformation at x. This transformation produces a function that |
1504
|
|
|
* is normally distributed rather than skewed. Use this function to perform hypothesis |
1505
|
|
|
* testing on the correlation coefficient. |
1506
|
|
|
* |
1507
|
|
|
* @param float $value |
1508
|
|
|
* @return float |
1509
|
|
|
*/ |
1510
|
|
|
public static function FISHER($value) { |
1511
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1512
|
|
|
|
1513
|
|
|
if (is_numeric($value)) { |
1514
|
|
|
if (($value <= -1) || ($value >= 1)) { |
1515
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1516
|
|
|
} |
1517
|
|
|
return 0.5 * log((1+$value)/(1-$value)); |
1518
|
|
|
} |
1519
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1520
|
|
|
} // function FISHER() |
1521
|
|
|
|
1522
|
|
|
|
1523
|
|
|
/** |
1524
|
|
|
* FISHERINV |
1525
|
|
|
* |
1526
|
|
|
* Returns the inverse of the Fisher transformation. Use this transformation when |
1527
|
|
|
* analyzing correlations between ranges or arrays of data. If y = FISHER(x), then |
1528
|
|
|
* FISHERINV(y) = x. |
1529
|
|
|
* |
1530
|
|
|
* @param float $value |
1531
|
|
|
* @return float |
1532
|
|
|
*/ |
1533
|
|
|
public static function FISHERINV($value) { |
1534
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1535
|
|
|
|
1536
|
|
|
if (is_numeric($value)) { |
1537
|
|
|
return (exp(2 * $value) - 1) / (exp(2 * $value) + 1); |
1538
|
|
|
} |
1539
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1540
|
|
|
} // function FISHERINV() |
1541
|
|
|
|
1542
|
|
|
|
1543
|
|
|
/** |
1544
|
|
|
* FORECAST |
1545
|
|
|
* |
1546
|
|
|
* Calculates, or predicts, a future value by using existing values. The predicted value is a y-value for a given x-value. |
1547
|
|
|
* |
1548
|
|
|
* @param float Value of X for which we want to find Y |
1549
|
|
|
* @param array of mixed Data Series Y |
1550
|
|
|
* @param array of mixed Data Series X |
1551
|
|
|
* @return float |
1552
|
|
|
*/ |
1553
|
|
|
public static function FORECAST($xValue,$yValues,$xValues) { |
1554
|
|
|
$xValue = PHPExcel_Calculation_Functions::flattenSingleValue($xValue); |
1555
|
|
|
if (!is_numeric($xValue)) { |
1556
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1557
|
|
|
} |
1558
|
|
|
|
1559
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
1560
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1561
|
|
|
} |
1562
|
|
|
$yValueCount = count($yValues); |
1563
|
|
|
$xValueCount = count($xValues); |
1564
|
|
|
|
1565
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
1566
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1567
|
|
|
} elseif ($yValueCount == 1) { |
1568
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
1569
|
|
|
} |
1570
|
|
|
|
1571
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
1572
|
|
|
return $bestFitLinear->getValueOfYForX($xValue); |
1573
|
|
|
} // function FORECAST() |
1574
|
|
|
|
1575
|
|
|
|
1576
|
|
|
/** |
1577
|
|
|
* GAMMADIST |
1578
|
|
|
* |
1579
|
|
|
* Returns the gamma distribution. |
1580
|
|
|
* |
1581
|
|
|
* @param float $value Value at which you want to evaluate the distribution |
1582
|
|
|
* @param float $a Parameter to the distribution |
1583
|
|
|
* @param float $b Parameter to the distribution |
1584
|
|
|
* @param boolean $cumulative |
1585
|
|
|
* @return float |
1586
|
|
|
* |
1587
|
|
|
*/ |
1588
|
|
|
public static function GAMMADIST($value,$a,$b,$cumulative) { |
1589
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1590
|
|
|
$a = PHPExcel_Calculation_Functions::flattenSingleValue($a); |
1591
|
|
|
$b = PHPExcel_Calculation_Functions::flattenSingleValue($b); |
1592
|
|
|
|
1593
|
|
|
if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) { |
1594
|
|
|
if (($value < 0) || ($a <= 0) || ($b <= 0)) { |
1595
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1596
|
|
|
} |
1597
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
1598
|
|
|
if ($cumulative) { |
1599
|
|
|
return self::_incompleteGamma($a,$value / $b) / self::_gamma($a); |
1600
|
|
|
} else { |
1601
|
|
|
return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b)); |
1602
|
|
|
} |
1603
|
|
|
} |
1604
|
|
|
} |
1605
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1606
|
|
|
} // function GAMMADIST() |
1607
|
|
|
|
1608
|
|
|
|
1609
|
|
|
/** |
1610
|
|
|
* GAMMAINV |
1611
|
|
|
* |
1612
|
|
|
* Returns the inverse of the beta distribution. |
1613
|
|
|
* |
1614
|
|
|
* @param float $probability Probability at which you want to evaluate the distribution |
1615
|
|
|
* @param float $alpha Parameter to the distribution |
1616
|
|
|
* @param float $beta Parameter to the distribution |
1617
|
|
|
* @return float |
1618
|
|
|
* |
1619
|
|
|
*/ |
1620
|
|
|
public static function GAMMAINV($probability,$alpha,$beta) { |
1621
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
1622
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
1623
|
|
|
$beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); |
1624
|
|
|
|
1625
|
|
|
if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) { |
1626
|
|
View Code Duplication |
if (($alpha <= 0) || ($beta <= 0) || ($probability < 0) || ($probability > 1)) { |
1627
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1628
|
|
|
} |
1629
|
|
|
|
1630
|
|
|
$xLo = 0; |
1631
|
|
|
$xHi = $alpha * $beta * 5; |
1632
|
|
|
|
1633
|
|
|
$x = $xNew = 1; |
1634
|
|
|
$error = $pdf = 0; |
1635
|
|
|
$dx = 1024; |
1636
|
|
|
$i = 0; |
1637
|
|
|
|
1638
|
|
|
while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) { |
1639
|
|
|
// Apply Newton-Raphson step |
1640
|
|
|
$error = self::GAMMADIST($x, $alpha, $beta, True) - $probability; |
1641
|
|
|
if ($error < 0.0) { |
1642
|
|
|
$xLo = $x; |
1643
|
|
|
} else { |
1644
|
|
|
$xHi = $x; |
1645
|
|
|
} |
1646
|
|
|
$pdf = self::GAMMADIST($x, $alpha, $beta, False); |
1647
|
|
|
// Avoid division by zero |
1648
|
|
|
if ($pdf != 0.0) { |
1649
|
|
|
$dx = $error / $pdf; |
1650
|
|
|
$xNew = $x - $dx; |
1651
|
|
|
} |
1652
|
|
|
// If the NR fails to converge (which for example may be the |
1653
|
|
|
// case if the initial guess is too rough) we apply a bisection |
1654
|
|
|
// step to determine a more narrow interval around the root. |
1655
|
|
|
if (($xNew < $xLo) || ($xNew > $xHi) || ($pdf == 0.0)) { |
1656
|
|
|
$xNew = ($xLo + $xHi) / 2; |
1657
|
|
|
$dx = $xNew - $x; |
1658
|
|
|
} |
1659
|
|
|
$x = $xNew; |
1660
|
|
|
} |
1661
|
|
|
if ($i == MAX_ITERATIONS) { |
1662
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1663
|
|
|
} |
1664
|
|
|
return $x; |
1665
|
|
|
} |
1666
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1667
|
|
|
} // function GAMMAINV() |
1668
|
|
|
|
1669
|
|
|
|
1670
|
|
|
/** |
1671
|
|
|
* GAMMALN |
1672
|
|
|
* |
1673
|
|
|
* Returns the natural logarithm of the gamma function. |
1674
|
|
|
* |
1675
|
|
|
* @param float $value |
1676
|
|
|
* @return float |
1677
|
|
|
*/ |
1678
|
|
View Code Duplication |
public static function GAMMALN($value) { |
1679
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
1680
|
|
|
|
1681
|
|
|
if (is_numeric($value)) { |
1682
|
|
|
if ($value <= 0) { |
1683
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1684
|
|
|
} |
1685
|
|
|
return log(self::_gamma($value)); |
1686
|
|
|
} |
1687
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1688
|
|
|
} // function GAMMALN() |
1689
|
|
|
|
1690
|
|
|
|
1691
|
|
|
/** |
1692
|
|
|
* GEOMEAN |
1693
|
|
|
* |
1694
|
|
|
* Returns the geometric mean of an array or range of positive data. For example, you |
1695
|
|
|
* can use GEOMEAN to calculate average growth rate given compound interest with |
1696
|
|
|
* variable rates. |
1697
|
|
|
* |
1698
|
|
|
* Excel Function: |
1699
|
|
|
* GEOMEAN(value1[,value2[, ...]]) |
1700
|
|
|
* |
1701
|
|
|
* @access public |
1702
|
|
|
* @category Statistical Functions |
1703
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1704
|
|
|
* @return float |
1705
|
|
|
*/ |
1706
|
|
|
public static function GEOMEAN() { |
1707
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
1708
|
|
|
|
1709
|
|
|
$aMean = PHPExcel_Calculation_MathTrig::PRODUCT($aArgs); |
1710
|
|
|
if (is_numeric($aMean) && ($aMean > 0)) { |
1711
|
|
|
$aCount = self::COUNT($aArgs) ; |
1712
|
|
|
if (self::MIN($aArgs) > 0) { |
1713
|
|
|
return pow($aMean, (1 / $aCount)); |
1714
|
|
|
} |
1715
|
|
|
} |
1716
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1717
|
|
|
} // GEOMEAN() |
1718
|
|
|
|
1719
|
|
|
|
1720
|
|
|
/** |
1721
|
|
|
* GROWTH |
1722
|
|
|
* |
1723
|
|
|
* Returns values along a predicted emponential trend |
1724
|
|
|
* |
1725
|
|
|
* @param array of mixed Data Series Y |
1726
|
|
|
* @param array of mixed Data Series X |
1727
|
|
|
* @param array of mixed Values of X for which we want to find Y |
1728
|
|
|
* @param boolean A logical value specifying whether to force the intersect to equal 0. |
1729
|
|
|
* @return array of float |
1730
|
|
|
*/ |
1731
|
|
View Code Duplication |
public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True) { |
1732
|
|
|
$yValues = PHPExcel_Calculation_Functions::flattenArray($yValues); |
1733
|
|
|
$xValues = PHPExcel_Calculation_Functions::flattenArray($xValues); |
1734
|
|
|
$newValues = PHPExcel_Calculation_Functions::flattenArray($newValues); |
1735
|
|
|
$const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); |
1736
|
|
|
|
1737
|
|
|
$bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const); |
1738
|
|
|
if (empty($newValues)) { |
1739
|
|
|
$newValues = $bestFitExponential->getXValues(); |
1740
|
|
|
} |
1741
|
|
|
|
1742
|
|
|
$returnArray = array(); |
1743
|
|
|
foreach($newValues as $xValue) { |
1744
|
|
|
$returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue); |
1745
|
|
|
} |
1746
|
|
|
|
1747
|
|
|
return $returnArray; |
1748
|
|
|
} // function GROWTH() |
1749
|
|
|
|
1750
|
|
|
|
1751
|
|
|
/** |
1752
|
|
|
* HARMEAN |
1753
|
|
|
* |
1754
|
|
|
* Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the |
1755
|
|
|
* arithmetic mean of reciprocals. |
1756
|
|
|
* |
1757
|
|
|
* Excel Function: |
1758
|
|
|
* HARMEAN(value1[,value2[, ...]]) |
1759
|
|
|
* |
1760
|
|
|
* @access public |
1761
|
|
|
* @category Statistical Functions |
1762
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1763
|
|
|
* @return float |
1764
|
|
|
*/ |
1765
|
|
|
public static function HARMEAN() { |
1766
|
|
|
// Return value |
1767
|
|
|
$returnValue = PHPExcel_Calculation_Functions::NA(); |
1768
|
|
|
|
1769
|
|
|
// Loop through arguments |
1770
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
1771
|
|
|
if (self::MIN($aArgs) < 0) { |
1772
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1773
|
|
|
} |
1774
|
|
|
$aCount = 0; |
1775
|
|
|
foreach ($aArgs as $arg) { |
1776
|
|
|
// Is it a numeric value? |
1777
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
1778
|
|
|
if ($arg <= 0) { |
1779
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1780
|
|
|
} |
1781
|
|
|
if (is_null($returnValue)) { |
1782
|
|
|
$returnValue = (1 / $arg); |
1783
|
|
|
} else { |
1784
|
|
|
$returnValue += (1 / $arg); |
1785
|
|
|
} |
1786
|
|
|
++$aCount; |
1787
|
|
|
} |
1788
|
|
|
} |
1789
|
|
|
|
1790
|
|
|
// Return |
1791
|
|
|
if ($aCount > 0) { |
1792
|
|
|
return 1 / ($returnValue / $aCount); |
1793
|
|
|
} else { |
1794
|
|
|
return $returnValue; |
1795
|
|
|
} |
1796
|
|
|
} // function HARMEAN() |
1797
|
|
|
|
1798
|
|
|
|
1799
|
|
|
/** |
1800
|
|
|
* HYPGEOMDIST |
1801
|
|
|
* |
1802
|
|
|
* Returns the hypergeometric distribution. HYPGEOMDIST returns the probability of a given number of |
1803
|
|
|
* sample successes, given the sample size, population successes, and population size. |
1804
|
|
|
* |
1805
|
|
|
* @param float $sampleSuccesses Number of successes in the sample |
1806
|
|
|
* @param float $sampleNumber Size of the sample |
1807
|
|
|
* @param float $populationSuccesses Number of successes in the population |
1808
|
|
|
* @param float $populationNumber Population size |
1809
|
|
|
* @return float |
1810
|
|
|
* |
1811
|
|
|
*/ |
1812
|
|
|
public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) { |
1813
|
|
|
$sampleSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleSuccesses)); |
1814
|
|
|
$sampleNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleNumber)); |
1815
|
|
|
$populationSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationSuccesses)); |
1816
|
|
|
$populationNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationNumber)); |
1817
|
|
|
|
1818
|
|
|
if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) { |
1819
|
|
|
if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) { |
1820
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1821
|
|
|
} |
1822
|
|
|
if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) { |
1823
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1824
|
|
|
} |
1825
|
|
|
if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) { |
1826
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1827
|
|
|
} |
1828
|
|
|
return PHPExcel_Calculation_MathTrig::COMBIN($populationSuccesses,$sampleSuccesses) * |
1829
|
|
|
PHPExcel_Calculation_MathTrig::COMBIN($populationNumber - $populationSuccesses,$sampleNumber - $sampleSuccesses) / |
1830
|
|
|
PHPExcel_Calculation_MathTrig::COMBIN($populationNumber,$sampleNumber); |
1831
|
|
|
} |
1832
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1833
|
|
|
} // function HYPGEOMDIST() |
1834
|
|
|
|
1835
|
|
|
|
1836
|
|
|
/** |
1837
|
|
|
* INTERCEPT |
1838
|
|
|
* |
1839
|
|
|
* Calculates the point at which a line will intersect the y-axis by using existing x-values and y-values. |
1840
|
|
|
* |
1841
|
|
|
* @param array of mixed Data Series Y |
1842
|
|
|
* @param array of mixed Data Series X |
1843
|
|
|
* @return float |
1844
|
|
|
*/ |
1845
|
|
View Code Duplication |
public static function INTERCEPT($yValues,$xValues) { |
1846
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
1847
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1848
|
|
|
} |
1849
|
|
|
$yValueCount = count($yValues); |
1850
|
|
|
$xValueCount = count($xValues); |
1851
|
|
|
|
1852
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
1853
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1854
|
|
|
} elseif ($yValueCount == 1) { |
1855
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
1856
|
|
|
} |
1857
|
|
|
|
1858
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
1859
|
|
|
return $bestFitLinear->getIntersect(); |
1860
|
|
|
} // function INTERCEPT() |
1861
|
|
|
|
1862
|
|
|
|
1863
|
|
|
/** |
1864
|
|
|
* KURT |
1865
|
|
|
* |
1866
|
|
|
* Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness |
1867
|
|
|
* or flatness of a distribution compared with the normal distribution. Positive |
1868
|
|
|
* kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a |
1869
|
|
|
* relatively flat distribution. |
1870
|
|
|
* |
1871
|
|
|
* @param array Data Series |
1872
|
|
|
* @return float |
1873
|
|
|
*/ |
1874
|
|
|
public static function KURT() { |
1875
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
1876
|
|
|
$mean = self::AVERAGE($aArgs); |
1877
|
|
|
$stdDev = self::STDEV($aArgs); |
1878
|
|
|
|
1879
|
|
|
if ($stdDev > 0) { |
1880
|
|
|
$count = $summer = 0; |
1881
|
|
|
// Loop through arguments |
1882
|
|
View Code Duplication |
foreach ($aArgs as $k => $arg) { |
1883
|
|
|
if ((is_bool($arg)) && |
|
|
|
|
1884
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
1885
|
|
|
} else { |
1886
|
|
|
// Is it a numeric value? |
1887
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
1888
|
|
|
$summer += pow((($arg - $mean) / $stdDev),4) ; |
1889
|
|
|
++$count; |
1890
|
|
|
} |
1891
|
|
|
} |
1892
|
|
|
} |
1893
|
|
|
|
1894
|
|
|
// Return |
1895
|
|
|
if ($count > 3) { |
1896
|
|
|
return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1,2) / (($count-2) * ($count-3))); |
1897
|
|
|
} |
1898
|
|
|
} |
1899
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
1900
|
|
|
} // function KURT() |
1901
|
|
|
|
1902
|
|
|
|
1903
|
|
|
/** |
1904
|
|
|
* LARGE |
1905
|
|
|
* |
1906
|
|
|
* Returns the nth largest value in a data set. You can use this function to |
1907
|
|
|
* select a value based on its relative standing. |
1908
|
|
|
* |
1909
|
|
|
* Excel Function: |
1910
|
|
|
* LARGE(value1[,value2[, ...]],entry) |
1911
|
|
|
* |
1912
|
|
|
* @access public |
1913
|
|
|
* @category Statistical Functions |
1914
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
1915
|
|
|
* @param int $entry Position (ordered from the largest) in the array or range of data to return |
|
|
|
|
1916
|
|
|
* @return float |
1917
|
|
|
* |
1918
|
|
|
*/ |
1919
|
|
View Code Duplication |
public static function LARGE() { |
1920
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
1921
|
|
|
|
1922
|
|
|
// Calculate |
1923
|
|
|
$entry = floor(array_pop($aArgs)); |
1924
|
|
|
|
1925
|
|
|
if ((is_numeric($entry)) && (!is_string($entry))) { |
1926
|
|
|
$mArgs = array(); |
1927
|
|
|
foreach ($aArgs as $arg) { |
1928
|
|
|
// Is it a numeric value? |
1929
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
1930
|
|
|
$mArgs[] = $arg; |
1931
|
|
|
} |
1932
|
|
|
} |
1933
|
|
|
$count = self::COUNT($mArgs); |
1934
|
|
|
$entry = floor(--$entry); |
1935
|
|
|
if (($entry < 0) || ($entry >= $count) || ($count == 0)) { |
1936
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
1937
|
|
|
} |
1938
|
|
|
rsort($mArgs); |
1939
|
|
|
return $mArgs[$entry]; |
1940
|
|
|
} |
1941
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1942
|
|
|
} // function LARGE() |
1943
|
|
|
|
1944
|
|
|
|
1945
|
|
|
/** |
1946
|
|
|
* LINEST |
1947
|
|
|
* |
1948
|
|
|
* Calculates the statistics for a line by using the "least squares" method to calculate a straight line that best fits your data, |
1949
|
|
|
* and then returns an array that describes the line. |
1950
|
|
|
* |
1951
|
|
|
* @param array of mixed Data Series Y |
1952
|
|
|
* @param array of mixed Data Series X |
1953
|
|
|
* @param boolean A logical value specifying whether to force the intersect to equal 0. |
1954
|
|
|
* @param boolean A logical value specifying whether to return additional regression statistics. |
1955
|
|
|
* @return array |
1956
|
|
|
*/ |
1957
|
|
|
public static function LINEST($yValues,$xValues=null,$const=True,$stats=False) { |
1958
|
|
|
$const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); |
1959
|
|
|
$stats = (is_null($stats)) ? False : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); |
1960
|
|
|
if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues))); |
1961
|
|
|
|
1962
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
1963
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
1964
|
|
|
} |
1965
|
|
|
$yValueCount = count($yValues); |
1966
|
|
|
$xValueCount = count($xValues); |
1967
|
|
|
|
1968
|
|
|
|
1969
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
1970
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
1971
|
|
|
} elseif ($yValueCount == 1) { |
1972
|
|
|
return 0; |
1973
|
|
|
} |
1974
|
|
|
|
1975
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const); |
1976
|
|
View Code Duplication |
if ($stats) { |
1977
|
|
|
return array( array( $bestFitLinear->getSlope(), |
1978
|
|
|
$bestFitLinear->getSlopeSE(), |
1979
|
|
|
$bestFitLinear->getGoodnessOfFit(), |
1980
|
|
|
$bestFitLinear->getF(), |
1981
|
|
|
$bestFitLinear->getSSRegression(), |
1982
|
|
|
), |
1983
|
|
|
array( $bestFitLinear->getIntersect(), |
1984
|
|
|
$bestFitLinear->getIntersectSE(), |
1985
|
|
|
$bestFitLinear->getStdevOfResiduals(), |
1986
|
|
|
$bestFitLinear->getDFResiduals(), |
1987
|
|
|
$bestFitLinear->getSSResiduals() |
1988
|
|
|
) |
1989
|
|
|
); |
1990
|
|
|
} else { |
1991
|
|
|
return array( $bestFitLinear->getSlope(), |
1992
|
|
|
$bestFitLinear->getIntersect() |
1993
|
|
|
); |
1994
|
|
|
} |
1995
|
|
|
} // function LINEST() |
1996
|
|
|
|
1997
|
|
|
|
1998
|
|
|
/** |
1999
|
|
|
* LOGEST |
2000
|
|
|
* |
2001
|
|
|
* Calculates an exponential curve that best fits the X and Y data series, |
2002
|
|
|
* and then returns an array that describes the line. |
2003
|
|
|
* |
2004
|
|
|
* @param array of mixed Data Series Y |
2005
|
|
|
* @param array of mixed Data Series X |
2006
|
|
|
* @param boolean A logical value specifying whether to force the intersect to equal 0. |
2007
|
|
|
* @param boolean A logical value specifying whether to return additional regression statistics. |
2008
|
|
|
* @return array |
2009
|
|
|
*/ |
2010
|
|
|
public static function LOGEST($yValues,$xValues=null,$const=True,$stats=False) { |
2011
|
|
|
$const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); |
2012
|
|
|
$stats = (is_null($stats)) ? False : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); |
2013
|
|
|
if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues))); |
2014
|
|
|
|
2015
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
2016
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2017
|
|
|
} |
2018
|
|
|
$yValueCount = count($yValues); |
2019
|
|
|
$xValueCount = count($xValues); |
2020
|
|
|
|
2021
|
|
|
foreach($yValues as $value) { |
2022
|
|
|
if ($value <= 0.0) { |
2023
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2024
|
|
|
} |
2025
|
|
|
} |
2026
|
|
|
|
2027
|
|
|
|
2028
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
2029
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2030
|
|
|
} elseif ($yValueCount == 1) { |
2031
|
|
|
return 1; |
2032
|
|
|
} |
2033
|
|
|
|
2034
|
|
|
$bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const); |
2035
|
|
View Code Duplication |
if ($stats) { |
2036
|
|
|
return array( array( $bestFitExponential->getSlope(), |
2037
|
|
|
$bestFitExponential->getSlopeSE(), |
2038
|
|
|
$bestFitExponential->getGoodnessOfFit(), |
2039
|
|
|
$bestFitExponential->getF(), |
2040
|
|
|
$bestFitExponential->getSSRegression(), |
2041
|
|
|
), |
2042
|
|
|
array( $bestFitExponential->getIntersect(), |
2043
|
|
|
$bestFitExponential->getIntersectSE(), |
2044
|
|
|
$bestFitExponential->getStdevOfResiduals(), |
2045
|
|
|
$bestFitExponential->getDFResiduals(), |
2046
|
|
|
$bestFitExponential->getSSResiduals() |
2047
|
|
|
) |
2048
|
|
|
); |
2049
|
|
|
} else { |
2050
|
|
|
return array( $bestFitExponential->getSlope(), |
2051
|
|
|
$bestFitExponential->getIntersect() |
2052
|
|
|
); |
2053
|
|
|
} |
2054
|
|
|
} // function LOGEST() |
2055
|
|
|
|
2056
|
|
|
|
2057
|
|
|
/** |
2058
|
|
|
* LOGINV |
2059
|
|
|
* |
2060
|
|
|
* Returns the inverse of the normal cumulative distribution |
2061
|
|
|
* |
2062
|
|
|
* @param float $value |
|
|
|
|
2063
|
|
|
* @return float |
2064
|
|
|
* |
2065
|
|
|
* @todo Try implementing P J Acklam's refinement algorithm for greater |
2066
|
|
|
* accuracy if I can get my head round the mathematics |
2067
|
|
|
* (as described at) http://home.online.no/~pjacklam/notes/invnorm/ |
2068
|
|
|
*/ |
2069
|
|
View Code Duplication |
public static function LOGINV($probability, $mean, $stdDev) { |
2070
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
2071
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2072
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
2073
|
|
|
|
2074
|
|
|
if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) { |
2075
|
|
|
if (($probability < 0) || ($probability > 1) || ($stdDev <= 0)) { |
2076
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2077
|
|
|
} |
2078
|
|
|
return exp($mean + $stdDev * self::NORMSINV($probability)); |
2079
|
|
|
} |
2080
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2081
|
|
|
} // function LOGINV() |
2082
|
|
|
|
2083
|
|
|
|
2084
|
|
|
/** |
2085
|
|
|
* LOGNORMDIST |
2086
|
|
|
* |
2087
|
|
|
* Returns the cumulative lognormal distribution of x, where ln(x) is normally distributed |
2088
|
|
|
* with parameters mean and standard_dev. |
2089
|
|
|
* |
2090
|
|
|
* @param float $value |
2091
|
|
|
* @return float |
2092
|
|
|
*/ |
2093
|
|
|
public static function LOGNORMDIST($value, $mean, $stdDev) { |
2094
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2095
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2096
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
2097
|
|
|
|
2098
|
|
|
if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { |
2099
|
|
|
if (($value <= 0) || ($stdDev <= 0)) { |
2100
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2101
|
|
|
} |
2102
|
|
|
return self::NORMSDIST((log($value) - $mean) / $stdDev); |
2103
|
|
|
} |
2104
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2105
|
|
|
} // function LOGNORMDIST() |
2106
|
|
|
|
2107
|
|
|
|
2108
|
|
|
/** |
2109
|
|
|
* MAX |
2110
|
|
|
* |
2111
|
|
|
* MAX returns the value of the element of the values passed that has the highest value, |
2112
|
|
|
* with negative numbers considered smaller than positive numbers. |
2113
|
|
|
* |
2114
|
|
|
* Excel Function: |
2115
|
|
|
* MAX(value1[,value2[, ...]]) |
2116
|
|
|
* |
2117
|
|
|
* @access public |
2118
|
|
|
* @category Statistical Functions |
2119
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2120
|
|
|
* @return float |
2121
|
|
|
*/ |
2122
|
|
View Code Duplication |
public static function MAX() { |
2123
|
|
|
// Return value |
2124
|
|
|
$returnValue = null; |
2125
|
|
|
|
2126
|
|
|
// Loop through arguments |
2127
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2128
|
|
|
foreach ($aArgs as $arg) { |
2129
|
|
|
// Is it a numeric value? |
2130
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2131
|
|
|
if ((is_null($returnValue)) || ($arg > $returnValue)) { |
2132
|
|
|
$returnValue = $arg; |
2133
|
|
|
} |
2134
|
|
|
} |
2135
|
|
|
} |
2136
|
|
|
|
2137
|
|
|
// Return |
2138
|
|
|
if(is_null($returnValue)) { |
2139
|
|
|
return 0; |
2140
|
|
|
} |
2141
|
|
|
return $returnValue; |
2142
|
|
|
} // function MAX() |
2143
|
|
|
|
2144
|
|
|
|
2145
|
|
|
/** |
2146
|
|
|
* MAXA |
2147
|
|
|
* |
2148
|
|
|
* Returns the greatest value in a list of arguments, including numbers, text, and logical values |
2149
|
|
|
* |
2150
|
|
|
* Excel Function: |
2151
|
|
|
* MAXA(value1[,value2[, ...]]) |
2152
|
|
|
* |
2153
|
|
|
* @access public |
2154
|
|
|
* @category Statistical Functions |
2155
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2156
|
|
|
* @return float |
2157
|
|
|
*/ |
2158
|
|
View Code Duplication |
public static function MAXA() { |
2159
|
|
|
// Return value |
2160
|
|
|
$returnValue = null; |
2161
|
|
|
|
2162
|
|
|
// Loop through arguments |
2163
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2164
|
|
|
foreach ($aArgs as $arg) { |
2165
|
|
|
// Is it a numeric value? |
2166
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) { |
2167
|
|
|
if (is_bool($arg)) { |
2168
|
|
|
$arg = (integer) $arg; |
2169
|
|
|
} elseif (is_string($arg)) { |
2170
|
|
|
$arg = 0; |
2171
|
|
|
} |
2172
|
|
|
if ((is_null($returnValue)) || ($arg > $returnValue)) { |
2173
|
|
|
$returnValue = $arg; |
2174
|
|
|
} |
2175
|
|
|
} |
2176
|
|
|
} |
2177
|
|
|
|
2178
|
|
|
// Return |
2179
|
|
|
if(is_null($returnValue)) { |
2180
|
|
|
return 0; |
2181
|
|
|
} |
2182
|
|
|
return $returnValue; |
2183
|
|
|
} // function MAXA() |
2184
|
|
|
|
2185
|
|
|
|
2186
|
|
|
/** |
2187
|
|
|
* MAXIF |
2188
|
|
|
* |
2189
|
|
|
* Counts the maximum value within a range of cells that contain numbers within the list of arguments |
2190
|
|
|
* |
2191
|
|
|
* Excel Function: |
2192
|
|
|
* MAXIF(value1[,value2[, ...]],condition) |
2193
|
|
|
* |
2194
|
|
|
* @access public |
2195
|
|
|
* @category Mathematical and Trigonometric Functions |
2196
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2197
|
|
|
* @param string $condition The criteria that defines which cells will be checked. |
2198
|
|
|
* @return float |
2199
|
|
|
*/ |
2200
|
|
View Code Duplication |
public static function MAXIF($aArgs,$condition,$sumArgs = array()) { |
2201
|
|
|
// Return value |
2202
|
|
|
$returnValue = null; |
2203
|
|
|
|
2204
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); |
2205
|
|
|
$sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs); |
2206
|
|
|
if (empty($sumArgs)) { |
2207
|
|
|
$sumArgs = $aArgs; |
2208
|
|
|
} |
2209
|
|
|
$condition = PHPExcel_Calculation_Functions::_ifCondition($condition); |
2210
|
|
|
// Loop through arguments |
2211
|
|
|
foreach ($aArgs as $key => $arg) { |
2212
|
|
|
if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } |
2213
|
|
|
$testCondition = '='.$arg.$condition; |
2214
|
|
|
if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { |
2215
|
|
|
if ((is_null($returnValue)) || ($arg > $returnValue)) { |
2216
|
|
|
$returnValue = $arg; |
2217
|
|
|
} |
2218
|
|
|
} |
2219
|
|
|
} |
2220
|
|
|
|
2221
|
|
|
// Return |
2222
|
|
|
return $returnValue; |
2223
|
|
|
} // function MAXIF() |
2224
|
|
|
|
2225
|
|
|
|
2226
|
|
|
/** |
2227
|
|
|
* MEDIAN |
2228
|
|
|
* |
2229
|
|
|
* Returns the median of the given numbers. The median is the number in the middle of a set of numbers. |
2230
|
|
|
* |
2231
|
|
|
* Excel Function: |
2232
|
|
|
* MEDIAN(value1[,value2[, ...]]) |
2233
|
|
|
* |
2234
|
|
|
* @access public |
2235
|
|
|
* @category Statistical Functions |
2236
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2237
|
|
|
* @return float |
2238
|
|
|
*/ |
2239
|
|
|
public static function MEDIAN() { |
2240
|
|
|
// Return value |
2241
|
|
|
$returnValue = PHPExcel_Calculation_Functions::NaN(); |
2242
|
|
|
|
2243
|
|
|
$mArgs = array(); |
2244
|
|
|
// Loop through arguments |
2245
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2246
|
|
|
foreach ($aArgs as $arg) { |
2247
|
|
|
// Is it a numeric value? |
2248
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2249
|
|
|
$mArgs[] = $arg; |
2250
|
|
|
} |
2251
|
|
|
} |
2252
|
|
|
|
2253
|
|
|
$mValueCount = count($mArgs); |
2254
|
|
|
if ($mValueCount > 0) { |
2255
|
|
|
sort($mArgs,SORT_NUMERIC); |
2256
|
|
|
$mValueCount = $mValueCount / 2; |
2257
|
|
|
if ($mValueCount == floor($mValueCount)) { |
2258
|
|
|
$returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2; |
2259
|
|
|
} else { |
2260
|
|
|
$mValueCount == floor($mValueCount); |
2261
|
|
|
$returnValue = $mArgs[$mValueCount]; |
2262
|
|
|
} |
2263
|
|
|
} |
2264
|
|
|
|
2265
|
|
|
// Return |
2266
|
|
|
return $returnValue; |
2267
|
|
|
} // function MEDIAN() |
2268
|
|
|
|
2269
|
|
|
|
2270
|
|
|
/** |
2271
|
|
|
* MIN |
2272
|
|
|
* |
2273
|
|
|
* MIN returns the value of the element of the values passed that has the smallest value, |
2274
|
|
|
* with negative numbers considered smaller than positive numbers. |
2275
|
|
|
* |
2276
|
|
|
* Excel Function: |
2277
|
|
|
* MIN(value1[,value2[, ...]]) |
2278
|
|
|
* |
2279
|
|
|
* @access public |
2280
|
|
|
* @category Statistical Functions |
2281
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2282
|
|
|
* @return float |
2283
|
|
|
*/ |
2284
|
|
View Code Duplication |
public static function MIN() { |
2285
|
|
|
// Return value |
2286
|
|
|
$returnValue = null; |
2287
|
|
|
|
2288
|
|
|
// Loop through arguments |
2289
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2290
|
|
|
foreach ($aArgs as $arg) { |
2291
|
|
|
// Is it a numeric value? |
2292
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2293
|
|
|
if ((is_null($returnValue)) || ($arg < $returnValue)) { |
2294
|
|
|
$returnValue = $arg; |
2295
|
|
|
} |
2296
|
|
|
} |
2297
|
|
|
} |
2298
|
|
|
|
2299
|
|
|
// Return |
2300
|
|
|
if(is_null($returnValue)) { |
2301
|
|
|
return 0; |
2302
|
|
|
} |
2303
|
|
|
return $returnValue; |
2304
|
|
|
} // function MIN() |
2305
|
|
|
|
2306
|
|
|
|
2307
|
|
|
/** |
2308
|
|
|
* MINA |
2309
|
|
|
* |
2310
|
|
|
* Returns the smallest value in a list of arguments, including numbers, text, and logical values |
2311
|
|
|
* |
2312
|
|
|
* Excel Function: |
2313
|
|
|
* MINA(value1[,value2[, ...]]) |
2314
|
|
|
* |
2315
|
|
|
* @access public |
2316
|
|
|
* @category Statistical Functions |
2317
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2318
|
|
|
* @return float |
2319
|
|
|
*/ |
2320
|
|
View Code Duplication |
public static function MINA() { |
2321
|
|
|
// Return value |
2322
|
|
|
$returnValue = null; |
2323
|
|
|
|
2324
|
|
|
// Loop through arguments |
2325
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2326
|
|
|
foreach ($aArgs as $arg) { |
2327
|
|
|
// Is it a numeric value? |
2328
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) { |
2329
|
|
|
if (is_bool($arg)) { |
2330
|
|
|
$arg = (integer) $arg; |
2331
|
|
|
} elseif (is_string($arg)) { |
2332
|
|
|
$arg = 0; |
2333
|
|
|
} |
2334
|
|
|
if ((is_null($returnValue)) || ($arg < $returnValue)) { |
2335
|
|
|
$returnValue = $arg; |
2336
|
|
|
} |
2337
|
|
|
} |
2338
|
|
|
} |
2339
|
|
|
|
2340
|
|
|
// Return |
2341
|
|
|
if(is_null($returnValue)) { |
2342
|
|
|
return 0; |
2343
|
|
|
} |
2344
|
|
|
return $returnValue; |
2345
|
|
|
} // function MINA() |
2346
|
|
|
|
2347
|
|
|
|
2348
|
|
|
/** |
2349
|
|
|
* MINIF |
2350
|
|
|
* |
2351
|
|
|
* Returns the minimum value within a range of cells that contain numbers within the list of arguments |
2352
|
|
|
* |
2353
|
|
|
* Excel Function: |
2354
|
|
|
* MINIF(value1[,value2[, ...]],condition) |
2355
|
|
|
* |
2356
|
|
|
* @access public |
2357
|
|
|
* @category Mathematical and Trigonometric Functions |
2358
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2359
|
|
|
* @param string $condition The criteria that defines which cells will be checked. |
2360
|
|
|
* @return float |
2361
|
|
|
*/ |
2362
|
|
View Code Duplication |
public static function MINIF($aArgs,$condition,$sumArgs = array()) { |
2363
|
|
|
// Return value |
2364
|
|
|
$returnValue = null; |
2365
|
|
|
|
2366
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); |
2367
|
|
|
$sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs); |
2368
|
|
|
if (empty($sumArgs)) { |
2369
|
|
|
$sumArgs = $aArgs; |
2370
|
|
|
} |
2371
|
|
|
$condition = PHPExcel_Calculation_Functions::_ifCondition($condition); |
2372
|
|
|
// Loop through arguments |
2373
|
|
|
foreach ($aArgs as $key => $arg) { |
2374
|
|
|
if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } |
2375
|
|
|
$testCondition = '='.$arg.$condition; |
2376
|
|
|
if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { |
2377
|
|
|
if ((is_null($returnValue)) || ($arg < $returnValue)) { |
2378
|
|
|
$returnValue = $arg; |
2379
|
|
|
} |
2380
|
|
|
} |
2381
|
|
|
} |
2382
|
|
|
|
2383
|
|
|
// Return |
2384
|
|
|
return $returnValue; |
2385
|
|
|
} // function MINIF() |
2386
|
|
|
|
2387
|
|
|
|
2388
|
|
|
// |
2389
|
|
|
// Special variant of array_count_values that isn't limited to strings and integers, |
2390
|
|
|
// but can work with floating point numbers as values |
2391
|
|
|
// |
2392
|
|
|
private static function _modeCalc($data) { |
2393
|
|
|
$frequencyArray = array(); |
2394
|
|
|
foreach($data as $datum) { |
2395
|
|
|
$found = False; |
2396
|
|
|
foreach($frequencyArray as $key => $value) { |
2397
|
|
|
if ((string) $value['value'] == (string) $datum) { |
2398
|
|
|
++$frequencyArray[$key]['frequency']; |
2399
|
|
|
$found = True; |
2400
|
|
|
break; |
2401
|
|
|
} |
2402
|
|
|
} |
2403
|
|
|
if (!$found) { |
2404
|
|
|
$frequencyArray[] = array('value' => $datum, |
2405
|
|
|
'frequency' => 1 ); |
2406
|
|
|
} |
2407
|
|
|
} |
2408
|
|
|
|
2409
|
|
|
foreach($frequencyArray as $key => $value) { |
2410
|
|
|
$frequencyList[$key] = $value['frequency']; |
|
|
|
|
2411
|
|
|
$valueList[$key] = $value['value']; |
|
|
|
|
2412
|
|
|
} |
2413
|
|
|
array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray); |
|
|
|
|
2414
|
|
|
|
2415
|
|
|
if ($frequencyArray[0]['frequency'] == 1) { |
2416
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2417
|
|
|
} |
2418
|
|
|
return $frequencyArray[0]['value']; |
2419
|
|
|
} // function _modeCalc() |
2420
|
|
|
|
2421
|
|
|
|
2422
|
|
|
/** |
2423
|
|
|
* MODE |
2424
|
|
|
* |
2425
|
|
|
* Returns the most frequently occurring, or repetitive, value in an array or range of data |
2426
|
|
|
* |
2427
|
|
|
* Excel Function: |
2428
|
|
|
* MODE(value1[,value2[, ...]]) |
2429
|
|
|
* |
2430
|
|
|
* @access public |
2431
|
|
|
* @category Statistical Functions |
2432
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2433
|
|
|
* @return float |
2434
|
|
|
*/ |
2435
|
|
|
public static function MODE() { |
2436
|
|
|
// Return value |
2437
|
|
|
$returnValue = PHPExcel_Calculation_Functions::NA(); |
2438
|
|
|
|
2439
|
|
|
// Loop through arguments |
2440
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2441
|
|
|
|
2442
|
|
|
$mArgs = array(); |
2443
|
|
|
foreach ($aArgs as $arg) { |
2444
|
|
|
// Is it a numeric value? |
2445
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2446
|
|
|
$mArgs[] = $arg; |
2447
|
|
|
} |
2448
|
|
|
} |
2449
|
|
|
|
2450
|
|
|
if (!empty($mArgs)) { |
2451
|
|
|
return self::_modeCalc($mArgs); |
2452
|
|
|
} |
2453
|
|
|
|
2454
|
|
|
// Return |
2455
|
|
|
return $returnValue; |
2456
|
|
|
} // function MODE() |
2457
|
|
|
|
2458
|
|
|
|
2459
|
|
|
/** |
2460
|
|
|
* NEGBINOMDIST |
2461
|
|
|
* |
2462
|
|
|
* Returns the negative binomial distribution. NEGBINOMDIST returns the probability that |
2463
|
|
|
* there will be number_f failures before the number_s-th success, when the constant |
2464
|
|
|
* probability of a success is probability_s. This function is similar to the binomial |
2465
|
|
|
* distribution, except that the number of successes is fixed, and the number of trials is |
2466
|
|
|
* variable. Like the binomial, trials are assumed to be independent. |
2467
|
|
|
* |
2468
|
|
|
* @param float $failures Number of Failures |
2469
|
|
|
* @param float $successes Threshold number of Successes |
2470
|
|
|
* @param float $probability Probability of success on each trial |
2471
|
|
|
* @return float |
2472
|
|
|
* |
2473
|
|
|
*/ |
2474
|
|
|
public static function NEGBINOMDIST($failures, $successes, $probability) { |
2475
|
|
|
$failures = floor(PHPExcel_Calculation_Functions::flattenSingleValue($failures)); |
2476
|
|
|
$successes = floor(PHPExcel_Calculation_Functions::flattenSingleValue($successes)); |
2477
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
2478
|
|
|
|
2479
|
|
|
if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) { |
2480
|
|
|
if (($failures < 0) || ($successes < 1)) { |
2481
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2482
|
|
|
} |
2483
|
|
|
if (($probability < 0) || ($probability > 1)) { |
2484
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2485
|
|
|
} |
2486
|
|
|
if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) { |
2487
|
|
|
if (($failures + $successes - 1) <= 0) { |
2488
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2489
|
|
|
} |
2490
|
|
|
} |
2491
|
|
|
return (PHPExcel_Calculation_MathTrig::COMBIN($failures + $successes - 1,$successes - 1)) * (pow($probability,$successes)) * (pow(1 - $probability,$failures)) ; |
2492
|
|
|
} |
2493
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2494
|
|
|
} // function NEGBINOMDIST() |
2495
|
|
|
|
2496
|
|
|
|
2497
|
|
|
/** |
2498
|
|
|
* NORMDIST |
2499
|
|
|
* |
2500
|
|
|
* Returns the normal distribution for the specified mean and standard deviation. This |
2501
|
|
|
* function has a very wide range of applications in statistics, including hypothesis |
2502
|
|
|
* testing. |
2503
|
|
|
* |
2504
|
|
|
* @param float $value |
2505
|
|
|
* @param float $mean Mean Value |
2506
|
|
|
* @param float $stdDev Standard Deviation |
2507
|
|
|
* @param boolean $cumulative |
2508
|
|
|
* @return float |
2509
|
|
|
* |
2510
|
|
|
*/ |
2511
|
|
|
public static function NORMDIST($value, $mean, $stdDev, $cumulative) { |
2512
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2513
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2514
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
2515
|
|
|
|
2516
|
|
|
if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { |
2517
|
|
|
if ($stdDev < 0) { |
2518
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2519
|
|
|
} |
2520
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
2521
|
|
|
if ($cumulative) { |
2522
|
|
|
return 0.5 * (1 + PHPExcel_Calculation_Engineering::_erfVal(($value - $mean) / ($stdDev * sqrt(2)))); |
2523
|
|
|
} else { |
2524
|
|
|
return (1 / (SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean,2) / (2 * ($stdDev * $stdDev)))); |
2525
|
|
|
} |
2526
|
|
|
} |
2527
|
|
|
} |
2528
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2529
|
|
|
} // function NORMDIST() |
2530
|
|
|
|
2531
|
|
|
|
2532
|
|
|
/** |
2533
|
|
|
* NORMINV |
2534
|
|
|
* |
2535
|
|
|
* Returns the inverse of the normal cumulative distribution for the specified mean and standard deviation. |
2536
|
|
|
* |
2537
|
|
|
* @param float $value |
|
|
|
|
2538
|
|
|
* @param float $mean Mean Value |
2539
|
|
|
* @param float $stdDev Standard Deviation |
2540
|
|
|
* @return float |
2541
|
|
|
* |
2542
|
|
|
*/ |
2543
|
|
View Code Duplication |
public static function NORMINV($probability,$mean,$stdDev) { |
2544
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
2545
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2546
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
2547
|
|
|
|
2548
|
|
|
if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) { |
2549
|
|
|
if (($probability < 0) || ($probability > 1)) { |
2550
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2551
|
|
|
} |
2552
|
|
|
if ($stdDev < 0) { |
2553
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2554
|
|
|
} |
2555
|
|
|
return (self::_inverse_ncdf($probability) * $stdDev) + $mean; |
2556
|
|
|
} |
2557
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2558
|
|
|
} // function NORMINV() |
2559
|
|
|
|
2560
|
|
|
|
2561
|
|
|
/** |
2562
|
|
|
* NORMSDIST |
2563
|
|
|
* |
2564
|
|
|
* Returns the standard normal cumulative distribution function. The distribution has |
2565
|
|
|
* a mean of 0 (zero) and a standard deviation of one. Use this function in place of a |
2566
|
|
|
* table of standard normal curve areas. |
2567
|
|
|
* |
2568
|
|
|
* @param float $value |
2569
|
|
|
* @return float |
2570
|
|
|
*/ |
2571
|
|
|
public static function NORMSDIST($value) { |
2572
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2573
|
|
|
|
2574
|
|
|
return self::NORMDIST($value, 0, 1, True); |
2575
|
|
|
} // function NORMSDIST() |
2576
|
|
|
|
2577
|
|
|
|
2578
|
|
|
/** |
2579
|
|
|
* NORMSINV |
2580
|
|
|
* |
2581
|
|
|
* Returns the inverse of the standard normal cumulative distribution |
2582
|
|
|
* |
2583
|
|
|
* @param float $value |
2584
|
|
|
* @return float |
2585
|
|
|
*/ |
2586
|
|
|
public static function NORMSINV($value) { |
2587
|
|
|
return self::NORMINV($value, 0, 1); |
2588
|
|
|
} // function NORMSINV() |
2589
|
|
|
|
2590
|
|
|
|
2591
|
|
|
/** |
2592
|
|
|
* PERCENTILE |
2593
|
|
|
* |
2594
|
|
|
* Returns the nth percentile of values in a range.. |
2595
|
|
|
* |
2596
|
|
|
* Excel Function: |
2597
|
|
|
* PERCENTILE(value1[,value2[, ...]],entry) |
2598
|
|
|
* |
2599
|
|
|
* @access public |
2600
|
|
|
* @category Statistical Functions |
2601
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2602
|
|
|
* @param float $entry Percentile value in the range 0..1, inclusive. |
|
|
|
|
2603
|
|
|
* @return float |
2604
|
|
|
*/ |
2605
|
|
|
public static function PERCENTILE() { |
2606
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2607
|
|
|
|
2608
|
|
|
// Calculate |
2609
|
|
|
$entry = array_pop($aArgs); |
2610
|
|
|
|
2611
|
|
|
if ((is_numeric($entry)) && (!is_string($entry))) { |
2612
|
|
|
if (($entry < 0) || ($entry > 1)) { |
2613
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2614
|
|
|
} |
2615
|
|
|
$mArgs = array(); |
2616
|
|
|
foreach ($aArgs as $arg) { |
2617
|
|
|
// Is it a numeric value? |
2618
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2619
|
|
|
$mArgs[] = $arg; |
2620
|
|
|
} |
2621
|
|
|
} |
2622
|
|
|
$mValueCount = count($mArgs); |
2623
|
|
|
if ($mValueCount > 0) { |
2624
|
|
|
sort($mArgs); |
2625
|
|
|
$count = self::COUNT($mArgs); |
2626
|
|
|
$index = $entry * ($count-1); |
2627
|
|
|
$iBase = floor($index); |
2628
|
|
|
if ($index == $iBase) { |
2629
|
|
|
return $mArgs[$index]; |
2630
|
|
|
} else { |
2631
|
|
|
$iNext = $iBase + 1; |
2632
|
|
|
$iProportion = $index - $iBase; |
2633
|
|
|
return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ; |
2634
|
|
|
} |
2635
|
|
|
} |
2636
|
|
|
} |
2637
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2638
|
|
|
} // function PERCENTILE() |
2639
|
|
|
|
2640
|
|
|
|
2641
|
|
|
/** |
2642
|
|
|
* PERCENTRANK |
2643
|
|
|
* |
2644
|
|
|
* Returns the rank of a value in a data set as a percentage of the data set. |
2645
|
|
|
* |
2646
|
|
|
* @param array of number An array of, or a reference to, a list of numbers. |
2647
|
|
|
* @param number The number whose rank you want to find. |
2648
|
|
|
* @param number The number of significant digits for the returned percentage value. |
2649
|
|
|
* @return float |
2650
|
|
|
*/ |
2651
|
|
|
public static function PERCENTRANK($valueSet,$value,$significance=3) { |
2652
|
|
|
$valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet); |
2653
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2654
|
|
|
$significance = (is_null($significance)) ? 3 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($significance); |
2655
|
|
|
|
2656
|
|
|
foreach($valueSet as $key => $valueEntry) { |
2657
|
|
|
if (!is_numeric($valueEntry)) { |
2658
|
|
|
unset($valueSet[$key]); |
2659
|
|
|
} |
2660
|
|
|
} |
2661
|
|
|
sort($valueSet,SORT_NUMERIC); |
2662
|
|
|
$valueCount = count($valueSet); |
2663
|
|
|
if ($valueCount == 0) { |
2664
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2665
|
|
|
} |
2666
|
|
|
|
2667
|
|
|
$valueAdjustor = $valueCount - 1; |
2668
|
|
|
if (($value < $valueSet[0]) || ($value > $valueSet[$valueAdjustor])) { |
2669
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2670
|
|
|
} |
2671
|
|
|
|
2672
|
|
|
$pos = array_search($value,$valueSet); |
2673
|
|
|
if ($pos === False) { |
2674
|
|
|
$pos = 0; |
2675
|
|
|
$testValue = $valueSet[0]; |
2676
|
|
|
while ($testValue < $value) { |
2677
|
|
|
$testValue = $valueSet[++$pos]; |
2678
|
|
|
} |
2679
|
|
|
--$pos; |
2680
|
|
|
$pos += (($value - $valueSet[$pos]) / ($testValue - $valueSet[$pos])); |
2681
|
|
|
} |
2682
|
|
|
|
2683
|
|
|
return round($pos / $valueAdjustor,$significance); |
2684
|
|
|
} // function PERCENTRANK() |
2685
|
|
|
|
2686
|
|
|
|
2687
|
|
|
/** |
2688
|
|
|
* PERMUT |
2689
|
|
|
* |
2690
|
|
|
* Returns the number of permutations for a given number of objects that can be |
2691
|
|
|
* selected from number objects. A permutation is any set or subset of objects or |
2692
|
|
|
* events where internal order is significant. Permutations are different from |
2693
|
|
|
* combinations, for which the internal order is not significant. Use this function |
2694
|
|
|
* for lottery-style probability calculations. |
2695
|
|
|
* |
2696
|
|
|
* @param int $numObjs Number of different objects |
2697
|
|
|
* @param int $numInSet Number of objects in each permutation |
2698
|
|
|
* @return int Number of permutations |
2699
|
|
|
*/ |
2700
|
|
View Code Duplication |
public static function PERMUT($numObjs,$numInSet) { |
2701
|
|
|
$numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs); |
2702
|
|
|
$numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet); |
2703
|
|
|
|
2704
|
|
|
if ((is_numeric($numObjs)) && (is_numeric($numInSet))) { |
2705
|
|
|
$numInSet = floor($numInSet); |
2706
|
|
|
if ($numObjs < $numInSet) { |
2707
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2708
|
|
|
} |
2709
|
|
|
return round(PHPExcel_Calculation_MathTrig::FACT($numObjs) / PHPExcel_Calculation_MathTrig::FACT($numObjs - $numInSet)); |
2710
|
|
|
} |
2711
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2712
|
|
|
} // function PERMUT() |
2713
|
|
|
|
2714
|
|
|
|
2715
|
|
|
/** |
2716
|
|
|
* POISSON |
2717
|
|
|
* |
2718
|
|
|
* Returns the Poisson distribution. A common application of the Poisson distribution |
2719
|
|
|
* is predicting the number of events over a specific time, such as the number of |
2720
|
|
|
* cars arriving at a toll plaza in 1 minute. |
2721
|
|
|
* |
2722
|
|
|
* @param float $value |
2723
|
|
|
* @param float $mean Mean Value |
2724
|
|
|
* @param boolean $cumulative |
2725
|
|
|
* @return float |
2726
|
|
|
* |
2727
|
|
|
*/ |
2728
|
|
|
public static function POISSON($value, $mean, $cumulative) { |
2729
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2730
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2731
|
|
|
|
2732
|
|
|
if ((is_numeric($value)) && (is_numeric($mean))) { |
2733
|
|
|
if (($value <= 0) || ($mean <= 0)) { |
2734
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2735
|
|
|
} |
2736
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
2737
|
|
|
if ($cumulative) { |
2738
|
|
|
$summer = 0; |
2739
|
|
|
for ($i = 0; $i <= floor($value); ++$i) { |
2740
|
|
|
$summer += pow($mean,$i) / PHPExcel_Calculation_MathTrig::FACT($i); |
2741
|
|
|
} |
2742
|
|
|
return exp(0-$mean) * $summer; |
2743
|
|
|
} else { |
2744
|
|
|
return (exp(0-$mean) * pow($mean,$value)) / PHPExcel_Calculation_MathTrig::FACT($value); |
2745
|
|
|
} |
2746
|
|
|
} |
2747
|
|
|
} |
2748
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2749
|
|
|
} // function POISSON() |
2750
|
|
|
|
2751
|
|
|
|
2752
|
|
|
/** |
2753
|
|
|
* QUARTILE |
2754
|
|
|
* |
2755
|
|
|
* Returns the quartile of a data set. |
2756
|
|
|
* |
2757
|
|
|
* Excel Function: |
2758
|
|
|
* QUARTILE(value1[,value2[, ...]],entry) |
2759
|
|
|
* |
2760
|
|
|
* @access public |
2761
|
|
|
* @category Statistical Functions |
2762
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2763
|
|
|
* @param int $entry Quartile value in the range 1..3, inclusive. |
|
|
|
|
2764
|
|
|
* @return float |
2765
|
|
|
*/ |
2766
|
|
|
public static function QUARTILE() { |
2767
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2768
|
|
|
|
2769
|
|
|
// Calculate |
2770
|
|
|
$entry = floor(array_pop($aArgs)); |
2771
|
|
|
|
2772
|
|
|
if ((is_numeric($entry)) && (!is_string($entry))) { |
2773
|
|
|
$entry /= 4; |
2774
|
|
|
if (($entry < 0) || ($entry > 1)) { |
2775
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2776
|
|
|
} |
2777
|
|
|
return self::PERCENTILE($aArgs,$entry); |
2778
|
|
|
} |
2779
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2780
|
|
|
} // function QUARTILE() |
2781
|
|
|
|
2782
|
|
|
|
2783
|
|
|
/** |
2784
|
|
|
* RANK |
2785
|
|
|
* |
2786
|
|
|
* Returns the rank of a number in a list of numbers. |
2787
|
|
|
* |
2788
|
|
|
* @param number The number whose rank you want to find. |
2789
|
|
|
* @param array of number An array of, or a reference to, a list of numbers. |
2790
|
|
|
* @param mixed Order to sort the values in the value set |
2791
|
|
|
* @return float |
2792
|
|
|
*/ |
2793
|
|
|
public static function RANK($value,$valueSet,$order=0) { |
2794
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2795
|
|
|
$valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet); |
2796
|
|
|
$order = (is_null($order)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($order); |
2797
|
|
|
|
2798
|
|
|
foreach($valueSet as $key => $valueEntry) { |
2799
|
|
|
if (!is_numeric($valueEntry)) { |
2800
|
|
|
unset($valueSet[$key]); |
2801
|
|
|
} |
2802
|
|
|
} |
2803
|
|
|
|
2804
|
|
|
if ($order == 0) { |
2805
|
|
|
rsort($valueSet,SORT_NUMERIC); |
2806
|
|
|
} else { |
2807
|
|
|
sort($valueSet,SORT_NUMERIC); |
2808
|
|
|
} |
2809
|
|
|
$pos = array_search($value,$valueSet); |
2810
|
|
|
if ($pos === False) { |
2811
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2812
|
|
|
} |
2813
|
|
|
|
2814
|
|
|
return ++$pos; |
2815
|
|
|
} // function RANK() |
2816
|
|
|
|
2817
|
|
|
|
2818
|
|
|
/** |
2819
|
|
|
* RSQ |
2820
|
|
|
* |
2821
|
|
|
* Returns the square of the Pearson product moment correlation coefficient through data points in known_y's and known_x's. |
2822
|
|
|
* |
2823
|
|
|
* @param array of mixed Data Series Y |
2824
|
|
|
* @param array of mixed Data Series X |
2825
|
|
|
* @return float |
2826
|
|
|
*/ |
2827
|
|
View Code Duplication |
public static function RSQ($yValues,$xValues) { |
2828
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
2829
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2830
|
|
|
} |
2831
|
|
|
$yValueCount = count($yValues); |
2832
|
|
|
$xValueCount = count($xValues); |
2833
|
|
|
|
2834
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
2835
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2836
|
|
|
} elseif ($yValueCount == 1) { |
2837
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
2838
|
|
|
} |
2839
|
|
|
|
2840
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
2841
|
|
|
return $bestFitLinear->getGoodnessOfFit(); |
2842
|
|
|
} // function RSQ() |
2843
|
|
|
|
2844
|
|
|
|
2845
|
|
|
/** |
2846
|
|
|
* SKEW |
2847
|
|
|
* |
2848
|
|
|
* Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry |
2849
|
|
|
* of a distribution around its mean. Positive skewness indicates a distribution with an |
2850
|
|
|
* asymmetric tail extending toward more positive values. Negative skewness indicates a |
2851
|
|
|
* distribution with an asymmetric tail extending toward more negative values. |
2852
|
|
|
* |
2853
|
|
|
* @param array Data Series |
2854
|
|
|
* @return float |
2855
|
|
|
*/ |
2856
|
|
|
public static function SKEW() { |
2857
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
2858
|
|
|
$mean = self::AVERAGE($aArgs); |
2859
|
|
|
$stdDev = self::STDEV($aArgs); |
2860
|
|
|
|
2861
|
|
|
$count = $summer = 0; |
2862
|
|
|
// Loop through arguments |
2863
|
|
View Code Duplication |
foreach ($aArgs as $k => $arg) { |
2864
|
|
|
if ((is_bool($arg)) && |
|
|
|
|
2865
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
2866
|
|
|
} else { |
2867
|
|
|
// Is it a numeric value? |
2868
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2869
|
|
|
$summer += pow((($arg - $mean) / $stdDev),3) ; |
2870
|
|
|
++$count; |
2871
|
|
|
} |
2872
|
|
|
} |
2873
|
|
|
} |
2874
|
|
|
|
2875
|
|
|
// Return |
2876
|
|
|
if ($count > 2) { |
2877
|
|
|
return $summer * ($count / (($count-1) * ($count-2))); |
2878
|
|
|
} |
2879
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
2880
|
|
|
} // function SKEW() |
2881
|
|
|
|
2882
|
|
|
|
2883
|
|
|
/** |
2884
|
|
|
* SLOPE |
2885
|
|
|
* |
2886
|
|
|
* Returns the slope of the linear regression line through data points in known_y's and known_x's. |
2887
|
|
|
* |
2888
|
|
|
* @param array of mixed Data Series Y |
2889
|
|
|
* @param array of mixed Data Series X |
2890
|
|
|
* @return float |
2891
|
|
|
*/ |
2892
|
|
View Code Duplication |
public static function SLOPE($yValues,$xValues) { |
2893
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
2894
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2895
|
|
|
} |
2896
|
|
|
$yValueCount = count($yValues); |
2897
|
|
|
$xValueCount = count($xValues); |
2898
|
|
|
|
2899
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
2900
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
2901
|
|
|
} elseif ($yValueCount == 1) { |
2902
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
2903
|
|
|
} |
2904
|
|
|
|
2905
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
2906
|
|
|
return $bestFitLinear->getSlope(); |
2907
|
|
|
} // function SLOPE() |
2908
|
|
|
|
2909
|
|
|
|
2910
|
|
|
/** |
2911
|
|
|
* SMALL |
2912
|
|
|
* |
2913
|
|
|
* Returns the nth smallest value in a data set. You can use this function to |
2914
|
|
|
* select a value based on its relative standing. |
2915
|
|
|
* |
2916
|
|
|
* Excel Function: |
2917
|
|
|
* SMALL(value1[,value2[, ...]],entry) |
2918
|
|
|
* |
2919
|
|
|
* @access public |
2920
|
|
|
* @category Statistical Functions |
2921
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2922
|
|
|
* @param int $entry Position (ordered from the smallest) in the array or range of data to return |
|
|
|
|
2923
|
|
|
* @return float |
2924
|
|
|
*/ |
2925
|
|
View Code Duplication |
public static function SMALL() { |
2926
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
2927
|
|
|
|
2928
|
|
|
// Calculate |
2929
|
|
|
$entry = array_pop($aArgs); |
2930
|
|
|
|
2931
|
|
|
if ((is_numeric($entry)) && (!is_string($entry))) { |
2932
|
|
|
$mArgs = array(); |
2933
|
|
|
foreach ($aArgs as $arg) { |
2934
|
|
|
// Is it a numeric value? |
2935
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
2936
|
|
|
$mArgs[] = $arg; |
2937
|
|
|
} |
2938
|
|
|
} |
2939
|
|
|
$count = self::COUNT($mArgs); |
2940
|
|
|
$entry = floor(--$entry); |
2941
|
|
|
if (($entry < 0) || ($entry >= $count) || ($count == 0)) { |
2942
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2943
|
|
|
} |
2944
|
|
|
sort($mArgs); |
2945
|
|
|
return $mArgs[$entry]; |
2946
|
|
|
} |
2947
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2948
|
|
|
} // function SMALL() |
2949
|
|
|
|
2950
|
|
|
|
2951
|
|
|
/** |
2952
|
|
|
* STANDARDIZE |
2953
|
|
|
* |
2954
|
|
|
* Returns a normalized value from a distribution characterized by mean and standard_dev. |
2955
|
|
|
* |
2956
|
|
|
* @param float $value Value to normalize |
2957
|
|
|
* @param float $mean Mean Value |
2958
|
|
|
* @param float $stdDev Standard Deviation |
2959
|
|
|
* @return float Standardized value |
2960
|
|
|
*/ |
2961
|
|
View Code Duplication |
public static function STANDARDIZE($value,$mean,$stdDev) { |
2962
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
2963
|
|
|
$mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); |
2964
|
|
|
$stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); |
2965
|
|
|
|
2966
|
|
|
if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { |
2967
|
|
|
if ($stdDev <= 0) { |
2968
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
2969
|
|
|
} |
2970
|
|
|
return ($value - $mean) / $stdDev ; |
2971
|
|
|
} |
2972
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
2973
|
|
|
} // function STANDARDIZE() |
2974
|
|
|
|
2975
|
|
|
|
2976
|
|
|
/** |
2977
|
|
|
* STDEV |
2978
|
|
|
* |
2979
|
|
|
* Estimates standard deviation based on a sample. The standard deviation is a measure of how |
2980
|
|
|
* widely values are dispersed from the average value (the mean). |
2981
|
|
|
* |
2982
|
|
|
* Excel Function: |
2983
|
|
|
* STDEV(value1[,value2[, ...]]) |
2984
|
|
|
* |
2985
|
|
|
* @access public |
2986
|
|
|
* @category Statistical Functions |
2987
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
2988
|
|
|
* @return float |
2989
|
|
|
*/ |
2990
|
|
View Code Duplication |
public static function STDEV() { |
2991
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
2992
|
|
|
|
2993
|
|
|
// Return value |
2994
|
|
|
$returnValue = null; |
2995
|
|
|
|
2996
|
|
|
$aMean = self::AVERAGE($aArgs); |
2997
|
|
|
if (!is_null($aMean)) { |
2998
|
|
|
$aCount = -1; |
2999
|
|
|
foreach ($aArgs as $k => $arg) { |
3000
|
|
|
if ((is_bool($arg)) && |
3001
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
3002
|
|
|
$arg = (integer) $arg; |
3003
|
|
|
} |
3004
|
|
|
// Is it a numeric value? |
3005
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
3006
|
|
|
if (is_null($returnValue)) { |
3007
|
|
|
$returnValue = pow(($arg - $aMean),2); |
3008
|
|
|
} else { |
3009
|
|
|
$returnValue += pow(($arg - $aMean),2); |
3010
|
|
|
} |
3011
|
|
|
++$aCount; |
3012
|
|
|
} |
3013
|
|
|
} |
3014
|
|
|
|
3015
|
|
|
// Return |
3016
|
|
|
if (($aCount > 0) && ($returnValue >= 0)) { |
3017
|
|
|
return sqrt($returnValue / $aCount); |
3018
|
|
|
} |
3019
|
|
|
} |
3020
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
3021
|
|
|
} // function STDEV() |
3022
|
|
|
|
3023
|
|
|
|
3024
|
|
|
/** |
3025
|
|
|
* STDEVA |
3026
|
|
|
* |
3027
|
|
|
* Estimates standard deviation based on a sample, including numbers, text, and logical values |
3028
|
|
|
* |
3029
|
|
|
* Excel Function: |
3030
|
|
|
* STDEVA(value1[,value2[, ...]]) |
3031
|
|
|
* |
3032
|
|
|
* @access public |
3033
|
|
|
* @category Statistical Functions |
3034
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3035
|
|
|
* @return float |
3036
|
|
|
*/ |
3037
|
|
View Code Duplication |
public static function STDEVA() { |
3038
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
3039
|
|
|
|
3040
|
|
|
// Return value |
3041
|
|
|
$returnValue = null; |
3042
|
|
|
|
3043
|
|
|
$aMean = self::AVERAGEA($aArgs); |
3044
|
|
|
if (!is_null($aMean)) { |
3045
|
|
|
$aCount = -1; |
3046
|
|
|
foreach ($aArgs as $k => $arg) { |
3047
|
|
|
if ((is_bool($arg)) && |
|
|
|
|
3048
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
3049
|
|
|
} else { |
3050
|
|
|
// Is it a numeric value? |
3051
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) { |
3052
|
|
|
if (is_bool($arg)) { |
3053
|
|
|
$arg = (integer) $arg; |
3054
|
|
|
} elseif (is_string($arg)) { |
3055
|
|
|
$arg = 0; |
3056
|
|
|
} |
3057
|
|
|
if (is_null($returnValue)) { |
3058
|
|
|
$returnValue = pow(($arg - $aMean),2); |
3059
|
|
|
} else { |
3060
|
|
|
$returnValue += pow(($arg - $aMean),2); |
3061
|
|
|
} |
3062
|
|
|
++$aCount; |
3063
|
|
|
} |
3064
|
|
|
} |
3065
|
|
|
} |
3066
|
|
|
|
3067
|
|
|
// Return |
3068
|
|
|
if (($aCount > 0) && ($returnValue >= 0)) { |
3069
|
|
|
return sqrt($returnValue / $aCount); |
3070
|
|
|
} |
3071
|
|
|
} |
3072
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
3073
|
|
|
} // function STDEVA() |
3074
|
|
|
|
3075
|
|
|
|
3076
|
|
|
/** |
3077
|
|
|
* STDEVP |
3078
|
|
|
* |
3079
|
|
|
* Calculates standard deviation based on the entire population |
3080
|
|
|
* |
3081
|
|
|
* Excel Function: |
3082
|
|
|
* STDEVP(value1[,value2[, ...]]) |
3083
|
|
|
* |
3084
|
|
|
* @access public |
3085
|
|
|
* @category Statistical Functions |
3086
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3087
|
|
|
* @return float |
3088
|
|
|
*/ |
3089
|
|
View Code Duplication |
public static function STDEVP() { |
3090
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
3091
|
|
|
|
3092
|
|
|
// Return value |
3093
|
|
|
$returnValue = null; |
3094
|
|
|
|
3095
|
|
|
$aMean = self::AVERAGE($aArgs); |
3096
|
|
|
if (!is_null($aMean)) { |
3097
|
|
|
$aCount = 0; |
3098
|
|
|
foreach ($aArgs as $k => $arg) { |
3099
|
|
|
if ((is_bool($arg)) && |
3100
|
|
|
((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { |
3101
|
|
|
$arg = (integer) $arg; |
3102
|
|
|
} |
3103
|
|
|
// Is it a numeric value? |
3104
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
3105
|
|
|
if (is_null($returnValue)) { |
3106
|
|
|
$returnValue = pow(($arg - $aMean),2); |
3107
|
|
|
} else { |
3108
|
|
|
$returnValue += pow(($arg - $aMean),2); |
3109
|
|
|
} |
3110
|
|
|
++$aCount; |
3111
|
|
|
} |
3112
|
|
|
} |
3113
|
|
|
|
3114
|
|
|
// Return |
3115
|
|
|
if (($aCount > 0) && ($returnValue >= 0)) { |
3116
|
|
|
return sqrt($returnValue / $aCount); |
3117
|
|
|
} |
3118
|
|
|
} |
3119
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
3120
|
|
|
} // function STDEVP() |
3121
|
|
|
|
3122
|
|
|
|
3123
|
|
|
/** |
3124
|
|
|
* STDEVPA |
3125
|
|
|
* |
3126
|
|
|
* Calculates standard deviation based on the entire population, including numbers, text, and logical values |
3127
|
|
|
* |
3128
|
|
|
* Excel Function: |
3129
|
|
|
* STDEVPA(value1[,value2[, ...]]) |
3130
|
|
|
* |
3131
|
|
|
* @access public |
3132
|
|
|
* @category Statistical Functions |
3133
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3134
|
|
|
* @return float |
3135
|
|
|
*/ |
3136
|
|
View Code Duplication |
public static function STDEVPA() { |
3137
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
3138
|
|
|
|
3139
|
|
|
// Return value |
3140
|
|
|
$returnValue = null; |
3141
|
|
|
|
3142
|
|
|
$aMean = self::AVERAGEA($aArgs); |
3143
|
|
|
if (!is_null($aMean)) { |
3144
|
|
|
$aCount = 0; |
3145
|
|
|
foreach ($aArgs as $k => $arg) { |
3146
|
|
|
if ((is_bool($arg)) && |
|
|
|
|
3147
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
3148
|
|
|
} else { |
3149
|
|
|
// Is it a numeric value? |
3150
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) { |
3151
|
|
|
if (is_bool($arg)) { |
3152
|
|
|
$arg = (integer) $arg; |
3153
|
|
|
} elseif (is_string($arg)) { |
3154
|
|
|
$arg = 0; |
3155
|
|
|
} |
3156
|
|
|
if (is_null($returnValue)) { |
3157
|
|
|
$returnValue = pow(($arg - $aMean),2); |
3158
|
|
|
} else { |
3159
|
|
|
$returnValue += pow(($arg - $aMean),2); |
3160
|
|
|
} |
3161
|
|
|
++$aCount; |
3162
|
|
|
} |
3163
|
|
|
} |
3164
|
|
|
} |
3165
|
|
|
|
3166
|
|
|
// Return |
3167
|
|
|
if (($aCount > 0) && ($returnValue >= 0)) { |
3168
|
|
|
return sqrt($returnValue / $aCount); |
3169
|
|
|
} |
3170
|
|
|
} |
3171
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
3172
|
|
|
} // function STDEVPA() |
3173
|
|
|
|
3174
|
|
|
|
3175
|
|
|
/** |
3176
|
|
|
* STEYX |
3177
|
|
|
* |
3178
|
|
|
* Returns the standard error of the predicted y-value for each x in the regression. |
3179
|
|
|
* |
3180
|
|
|
* @param array of mixed Data Series Y |
3181
|
|
|
* @param array of mixed Data Series X |
3182
|
|
|
* @return float |
3183
|
|
|
*/ |
3184
|
|
View Code Duplication |
public static function STEYX($yValues,$xValues) { |
3185
|
|
|
if (!self::_checkTrendArrays($yValues,$xValues)) { |
3186
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3187
|
|
|
} |
3188
|
|
|
$yValueCount = count($yValues); |
3189
|
|
|
$xValueCount = count($xValues); |
3190
|
|
|
|
3191
|
|
|
if (($yValueCount == 0) || ($yValueCount != $xValueCount)) { |
3192
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
3193
|
|
|
} elseif ($yValueCount == 1) { |
3194
|
|
|
return PHPExcel_Calculation_Functions::DIV0(); |
3195
|
|
|
} |
3196
|
|
|
|
3197
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); |
3198
|
|
|
return $bestFitLinear->getStdevOfResiduals(); |
3199
|
|
|
} // function STEYX() |
3200
|
|
|
|
3201
|
|
|
|
3202
|
|
|
/** |
3203
|
|
|
* TDIST |
3204
|
|
|
* |
3205
|
|
|
* Returns the probability of Student's T distribution. |
3206
|
|
|
* |
3207
|
|
|
* @param float $value Value for the function |
3208
|
|
|
* @param float $degrees degrees of freedom |
3209
|
|
|
* @param float $tails number of tails (1 or 2) |
3210
|
|
|
* @return float |
3211
|
|
|
*/ |
3212
|
|
|
public static function TDIST($value, $degrees, $tails) { |
3213
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
3214
|
|
|
$degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); |
3215
|
|
|
$tails = floor(PHPExcel_Calculation_Functions::flattenSingleValue($tails)); |
3216
|
|
|
|
3217
|
|
|
if ((is_numeric($value)) && (is_numeric($degrees)) && (is_numeric($tails))) { |
3218
|
|
|
if (($value < 0) || ($degrees < 1) || ($tails < 1) || ($tails > 2)) { |
3219
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
3220
|
|
|
} |
3221
|
|
|
// tdist, which finds the probability that corresponds to a given value |
3222
|
|
|
// of t with k degrees of freedom. This algorithm is translated from a |
3223
|
|
|
// pascal function on p81 of "Statistical Computing in Pascal" by D |
3224
|
|
|
// Cooke, A H Craven & G M Clark (1985: Edward Arnold (Pubs.) Ltd: |
3225
|
|
|
// London). The above Pascal algorithm is itself a translation of the |
3226
|
|
|
// fortran algoritm "AS 3" by B E Cooper of the Atlas Computer |
3227
|
|
|
// Laboratory as reported in (among other places) "Applied Statistics |
3228
|
|
|
// Algorithms", editied by P Griffiths and I D Hill (1985; Ellis |
3229
|
|
|
// Horwood Ltd.; W. Sussex, England). |
3230
|
|
|
$tterm = $degrees; |
3231
|
|
|
$ttheta = atan2($value,sqrt($tterm)); |
3232
|
|
|
$tc = cos($ttheta); |
3233
|
|
|
$ts = sin($ttheta); |
3234
|
|
|
$tsum = 0; |
3235
|
|
|
|
3236
|
|
|
if (($degrees % 2) == 1) { |
3237
|
|
|
$ti = 3; |
3238
|
|
|
$tterm = $tc; |
3239
|
|
|
} else { |
3240
|
|
|
$ti = 2; |
3241
|
|
|
$tterm = 1; |
3242
|
|
|
} |
3243
|
|
|
|
3244
|
|
|
$tsum = $tterm; |
3245
|
|
|
while ($ti < $degrees) { |
3246
|
|
|
$tterm *= $tc * $tc * ($ti - 1) / $ti; |
3247
|
|
|
$tsum += $tterm; |
3248
|
|
|
$ti += 2; |
3249
|
|
|
} |
3250
|
|
|
$tsum *= $ts; |
3251
|
|
|
if (($degrees % 2) == 1) { $tsum = M_2DIVPI * ($tsum + $ttheta); } |
3252
|
|
|
$tValue = 0.5 * (1 + $tsum); |
3253
|
|
|
if ($tails == 1) { |
3254
|
|
|
return 1 - abs($tValue); |
3255
|
|
|
} else { |
3256
|
|
|
return 1 - abs((1 - $tValue) - $tValue); |
3257
|
|
|
} |
3258
|
|
|
} |
3259
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3260
|
|
|
} // function TDIST() |
3261
|
|
|
|
3262
|
|
|
|
3263
|
|
|
/** |
3264
|
|
|
* TINV |
3265
|
|
|
* |
3266
|
|
|
* Returns the one-tailed probability of the chi-squared distribution. |
3267
|
|
|
* |
3268
|
|
|
* @param float $probability Probability for the function |
3269
|
|
|
* @param float $degrees degrees of freedom |
3270
|
|
|
* @return float |
3271
|
|
|
*/ |
3272
|
|
View Code Duplication |
public static function TINV($probability, $degrees) { |
3273
|
|
|
$probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); |
3274
|
|
|
$degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); |
3275
|
|
|
|
3276
|
|
|
if ((is_numeric($probability)) && (is_numeric($degrees))) { |
3277
|
|
|
$xLo = 100; |
3278
|
|
|
$xHi = 0; |
3279
|
|
|
|
3280
|
|
|
$x = $xNew = 1; |
3281
|
|
|
$dx = 1; |
3282
|
|
|
$i = 0; |
3283
|
|
|
|
3284
|
|
|
while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) { |
3285
|
|
|
// Apply Newton-Raphson step |
3286
|
|
|
$result = self::TDIST($x, $degrees, 2); |
3287
|
|
|
$error = $result - $probability; |
3288
|
|
|
if ($error == 0.0) { |
3289
|
|
|
$dx = 0; |
3290
|
|
|
} elseif ($error < 0.0) { |
3291
|
|
|
$xLo = $x; |
3292
|
|
|
} else { |
3293
|
|
|
$xHi = $x; |
3294
|
|
|
} |
3295
|
|
|
// Avoid division by zero |
3296
|
|
|
if ($result != 0.0) { |
3297
|
|
|
$dx = $error / $result; |
3298
|
|
|
$xNew = $x - $dx; |
3299
|
|
|
} |
3300
|
|
|
// If the NR fails to converge (which for example may be the |
3301
|
|
|
// case if the initial guess is too rough) we apply a bisection |
3302
|
|
|
// step to determine a more narrow interval around the root. |
3303
|
|
|
if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) { |
3304
|
|
|
$xNew = ($xLo + $xHi) / 2; |
3305
|
|
|
$dx = $xNew - $x; |
3306
|
|
|
} |
3307
|
|
|
$x = $xNew; |
3308
|
|
|
} |
3309
|
|
|
if ($i == MAX_ITERATIONS) { |
3310
|
|
|
return PHPExcel_Calculation_Functions::NA(); |
3311
|
|
|
} |
3312
|
|
|
return round($x,12); |
3313
|
|
|
} |
3314
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3315
|
|
|
} // function TINV() |
3316
|
|
|
|
3317
|
|
|
|
3318
|
|
|
/** |
3319
|
|
|
* TREND |
3320
|
|
|
* |
3321
|
|
|
* Returns values along a linear trend |
3322
|
|
|
* |
3323
|
|
|
* @param array of mixed Data Series Y |
3324
|
|
|
* @param array of mixed Data Series X |
3325
|
|
|
* @param array of mixed Values of X for which we want to find Y |
3326
|
|
|
* @param boolean A logical value specifying whether to force the intersect to equal 0. |
3327
|
|
|
* @return array of float |
3328
|
|
|
*/ |
3329
|
|
View Code Duplication |
public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True) { |
3330
|
|
|
$yValues = PHPExcel_Calculation_Functions::flattenArray($yValues); |
3331
|
|
|
$xValues = PHPExcel_Calculation_Functions::flattenArray($xValues); |
3332
|
|
|
$newValues = PHPExcel_Calculation_Functions::flattenArray($newValues); |
3333
|
|
|
$const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); |
3334
|
|
|
|
3335
|
|
|
$bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const); |
3336
|
|
|
if (empty($newValues)) { |
3337
|
|
|
$newValues = $bestFitLinear->getXValues(); |
3338
|
|
|
} |
3339
|
|
|
|
3340
|
|
|
$returnArray = array(); |
3341
|
|
|
foreach($newValues as $xValue) { |
3342
|
|
|
$returnArray[0][] = $bestFitLinear->getValueOfYForX($xValue); |
3343
|
|
|
} |
3344
|
|
|
|
3345
|
|
|
return $returnArray; |
3346
|
|
|
} // function TREND() |
3347
|
|
|
|
3348
|
|
|
|
3349
|
|
|
/** |
3350
|
|
|
* TRIMMEAN |
3351
|
|
|
* |
3352
|
|
|
* Returns the mean of the interior of a data set. TRIMMEAN calculates the mean |
3353
|
|
|
* taken by excluding a percentage of data points from the top and bottom tails |
3354
|
|
|
* of a data set. |
3355
|
|
|
* |
3356
|
|
|
* Excel Function: |
3357
|
|
|
* TRIMEAN(value1[,value2[, ...]],$discard) |
3358
|
|
|
* |
3359
|
|
|
* @access public |
3360
|
|
|
* @category Statistical Functions |
3361
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3362
|
|
|
* @param float $discard Percentage to discard |
|
|
|
|
3363
|
|
|
* @return float |
3364
|
|
|
*/ |
3365
|
|
|
public static function TRIMMEAN() { |
3366
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
3367
|
|
|
|
3368
|
|
|
// Calculate |
3369
|
|
|
$percent = array_pop($aArgs); |
3370
|
|
|
|
3371
|
|
|
if ((is_numeric($percent)) && (!is_string($percent))) { |
3372
|
|
|
if (($percent < 0) || ($percent > 1)) { |
3373
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
3374
|
|
|
} |
3375
|
|
|
$mArgs = array(); |
3376
|
|
|
foreach ($aArgs as $arg) { |
3377
|
|
|
// Is it a numeric value? |
3378
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
3379
|
|
|
$mArgs[] = $arg; |
3380
|
|
|
} |
3381
|
|
|
} |
3382
|
|
|
$discard = floor(self::COUNT($mArgs) * $percent / 2); |
3383
|
|
|
sort($mArgs); |
3384
|
|
|
for ($i=0; $i < $discard; ++$i) { |
3385
|
|
|
array_pop($mArgs); |
3386
|
|
|
array_shift($mArgs); |
3387
|
|
|
} |
3388
|
|
|
return self::AVERAGE($mArgs); |
3389
|
|
|
} |
3390
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3391
|
|
|
} // function TRIMMEAN() |
3392
|
|
|
|
3393
|
|
|
|
3394
|
|
|
/** |
3395
|
|
|
* VARFunc |
3396
|
|
|
* |
3397
|
|
|
* Estimates variance based on a sample. |
3398
|
|
|
* |
3399
|
|
|
* Excel Function: |
3400
|
|
|
* VAR(value1[,value2[, ...]]) |
3401
|
|
|
* |
3402
|
|
|
* @access public |
3403
|
|
|
* @category Statistical Functions |
3404
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3405
|
|
|
* @return float |
3406
|
|
|
*/ |
3407
|
|
View Code Duplication |
public static function VARFunc() { |
3408
|
|
|
// Return value |
3409
|
|
|
$returnValue = PHPExcel_Calculation_Functions::DIV0(); |
3410
|
|
|
|
3411
|
|
|
$summerA = $summerB = 0; |
3412
|
|
|
|
3413
|
|
|
// Loop through arguments |
3414
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
3415
|
|
|
$aCount = 0; |
3416
|
|
|
foreach ($aArgs as $arg) { |
3417
|
|
|
if (is_bool($arg)) { $arg = (integer) $arg; } |
3418
|
|
|
// Is it a numeric value? |
3419
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
3420
|
|
|
$summerA += ($arg * $arg); |
3421
|
|
|
$summerB += $arg; |
3422
|
|
|
++$aCount; |
3423
|
|
|
} |
3424
|
|
|
} |
3425
|
|
|
|
3426
|
|
|
// Return |
3427
|
|
|
if ($aCount > 1) { |
3428
|
|
|
$summerA *= $aCount; |
3429
|
|
|
$summerB *= $summerB; |
3430
|
|
|
$returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1)); |
3431
|
|
|
} |
3432
|
|
|
return $returnValue; |
3433
|
|
|
} // function VARFunc() |
3434
|
|
|
|
3435
|
|
|
|
3436
|
|
|
/** |
3437
|
|
|
* VARA |
3438
|
|
|
* |
3439
|
|
|
* Estimates variance based on a sample, including numbers, text, and logical values |
3440
|
|
|
* |
3441
|
|
|
* Excel Function: |
3442
|
|
|
* VARA(value1[,value2[, ...]]) |
3443
|
|
|
* |
3444
|
|
|
* @access public |
3445
|
|
|
* @category Statistical Functions |
3446
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3447
|
|
|
* @return float |
3448
|
|
|
*/ |
3449
|
|
View Code Duplication |
public static function VARA() { |
3450
|
|
|
// Return value |
3451
|
|
|
$returnValue = PHPExcel_Calculation_Functions::DIV0(); |
3452
|
|
|
|
3453
|
|
|
$summerA = $summerB = 0; |
3454
|
|
|
|
3455
|
|
|
// Loop through arguments |
3456
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
3457
|
|
|
$aCount = 0; |
3458
|
|
|
foreach ($aArgs as $k => $arg) { |
3459
|
|
|
if ((is_string($arg)) && |
3460
|
|
|
(PHPExcel_Calculation_Functions::isValue($k))) { |
3461
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3462
|
|
|
} elseif ((is_string($arg)) && |
|
|
|
|
3463
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
3464
|
|
|
} else { |
3465
|
|
|
// Is it a numeric value? |
3466
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) { |
3467
|
|
|
if (is_bool($arg)) { |
3468
|
|
|
$arg = (integer) $arg; |
3469
|
|
|
} elseif (is_string($arg)) { |
3470
|
|
|
$arg = 0; |
3471
|
|
|
} |
3472
|
|
|
$summerA += ($arg * $arg); |
3473
|
|
|
$summerB += $arg; |
3474
|
|
|
++$aCount; |
3475
|
|
|
} |
3476
|
|
|
} |
3477
|
|
|
} |
3478
|
|
|
|
3479
|
|
|
// Return |
3480
|
|
|
if ($aCount > 1) { |
3481
|
|
|
$summerA *= $aCount; |
3482
|
|
|
$summerB *= $summerB; |
3483
|
|
|
$returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1)); |
3484
|
|
|
} |
3485
|
|
|
return $returnValue; |
3486
|
|
|
} // function VARA() |
3487
|
|
|
|
3488
|
|
|
|
3489
|
|
|
/** |
3490
|
|
|
* VARP |
3491
|
|
|
* |
3492
|
|
|
* Calculates variance based on the entire population |
3493
|
|
|
* |
3494
|
|
|
* Excel Function: |
3495
|
|
|
* VARP(value1[,value2[, ...]]) |
3496
|
|
|
* |
3497
|
|
|
* @access public |
3498
|
|
|
* @category Statistical Functions |
3499
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3500
|
|
|
* @return float |
3501
|
|
|
*/ |
3502
|
|
View Code Duplication |
public static function VARP() { |
3503
|
|
|
// Return value |
3504
|
|
|
$returnValue = PHPExcel_Calculation_Functions::DIV0(); |
3505
|
|
|
|
3506
|
|
|
$summerA = $summerB = 0; |
3507
|
|
|
|
3508
|
|
|
// Loop through arguments |
3509
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); |
3510
|
|
|
$aCount = 0; |
3511
|
|
|
foreach ($aArgs as $arg) { |
3512
|
|
|
if (is_bool($arg)) { $arg = (integer) $arg; } |
3513
|
|
|
// Is it a numeric value? |
3514
|
|
|
if ((is_numeric($arg)) && (!is_string($arg))) { |
3515
|
|
|
$summerA += ($arg * $arg); |
3516
|
|
|
$summerB += $arg; |
3517
|
|
|
++$aCount; |
3518
|
|
|
} |
3519
|
|
|
} |
3520
|
|
|
|
3521
|
|
|
// Return |
3522
|
|
|
if ($aCount > 0) { |
3523
|
|
|
$summerA *= $aCount; |
3524
|
|
|
$summerB *= $summerB; |
3525
|
|
|
$returnValue = ($summerA - $summerB) / ($aCount * $aCount); |
3526
|
|
|
} |
3527
|
|
|
return $returnValue; |
3528
|
|
|
} // function VARP() |
3529
|
|
|
|
3530
|
|
|
|
3531
|
|
|
/** |
3532
|
|
|
* VARPA |
3533
|
|
|
* |
3534
|
|
|
* Calculates variance based on the entire population, including numbers, text, and logical values |
3535
|
|
|
* |
3536
|
|
|
* Excel Function: |
3537
|
|
|
* VARPA(value1[,value2[, ...]]) |
3538
|
|
|
* |
3539
|
|
|
* @access public |
3540
|
|
|
* @category Statistical Functions |
3541
|
|
|
* @param mixed $arg,... Data values |
|
|
|
|
3542
|
|
|
* @return float |
3543
|
|
|
*/ |
3544
|
|
View Code Duplication |
public static function VARPA() { |
3545
|
|
|
// Return value |
3546
|
|
|
$returnValue = PHPExcel_Calculation_Functions::DIV0(); |
3547
|
|
|
|
3548
|
|
|
$summerA = $summerB = 0; |
3549
|
|
|
|
3550
|
|
|
// Loop through arguments |
3551
|
|
|
$aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); |
3552
|
|
|
$aCount = 0; |
3553
|
|
|
foreach ($aArgs as $k => $arg) { |
3554
|
|
|
if ((is_string($arg)) && |
3555
|
|
|
(PHPExcel_Calculation_Functions::isValue($k))) { |
3556
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3557
|
|
|
} elseif ((is_string($arg)) && |
|
|
|
|
3558
|
|
|
(!PHPExcel_Calculation_Functions::isMatrixValue($k))) { |
3559
|
|
|
} else { |
3560
|
|
|
// Is it a numeric value? |
3561
|
|
|
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) { |
3562
|
|
|
if (is_bool($arg)) { |
3563
|
|
|
$arg = (integer) $arg; |
3564
|
|
|
} elseif (is_string($arg)) { |
3565
|
|
|
$arg = 0; |
3566
|
|
|
} |
3567
|
|
|
$summerA += ($arg * $arg); |
3568
|
|
|
$summerB += $arg; |
3569
|
|
|
++$aCount; |
3570
|
|
|
} |
3571
|
|
|
} |
3572
|
|
|
} |
3573
|
|
|
|
3574
|
|
|
// Return |
3575
|
|
|
if ($aCount > 0) { |
3576
|
|
|
$summerA *= $aCount; |
3577
|
|
|
$summerB *= $summerB; |
3578
|
|
|
$returnValue = ($summerA - $summerB) / ($aCount * $aCount); |
3579
|
|
|
} |
3580
|
|
|
return $returnValue; |
3581
|
|
|
} // function VARPA() |
3582
|
|
|
|
3583
|
|
|
|
3584
|
|
|
/** |
3585
|
|
|
* WEIBULL |
3586
|
|
|
* |
3587
|
|
|
* Returns the Weibull distribution. Use this distribution in reliability |
3588
|
|
|
* analysis, such as calculating a device's mean time to failure. |
3589
|
|
|
* |
3590
|
|
|
* @param float $value |
3591
|
|
|
* @param float $alpha Alpha Parameter |
3592
|
|
|
* @param float $beta Beta Parameter |
3593
|
|
|
* @param boolean $cumulative |
3594
|
|
|
* @return float |
3595
|
|
|
* |
3596
|
|
|
*/ |
3597
|
|
|
public static function WEIBULL($value, $alpha, $beta, $cumulative) { |
3598
|
|
|
$value = PHPExcel_Calculation_Functions::flattenSingleValue($value); |
3599
|
|
|
$alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); |
3600
|
|
|
$beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); |
3601
|
|
|
|
3602
|
|
|
if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) { |
3603
|
|
|
if (($value < 0) || ($alpha <= 0) || ($beta <= 0)) { |
3604
|
|
|
return PHPExcel_Calculation_Functions::NaN(); |
3605
|
|
|
} |
3606
|
|
|
if ((is_numeric($cumulative)) || (is_bool($cumulative))) { |
3607
|
|
|
if ($cumulative) { |
3608
|
|
|
return 1 - exp(0 - pow($value / $beta,$alpha)); |
3609
|
|
|
} else { |
3610
|
|
|
return ($alpha / pow($beta,$alpha)) * pow($value,$alpha - 1) * exp(0 - pow($value / $beta,$alpha)); |
3611
|
|
|
} |
3612
|
|
|
} |
3613
|
|
|
} |
3614
|
|
|
return PHPExcel_Calculation_Functions::VALUE(); |
3615
|
|
|
} // function WEIBULL() |
3616
|
|
|
|
3617
|
|
|
|
3618
|
|
|
/** |
3619
|
|
|
* ZTEST |
3620
|
|
|
* |
3621
|
|
|
* Returns the Weibull distribution. Use this distribution in reliability |
3622
|
|
|
* analysis, such as calculating a device's mean time to failure. |
3623
|
|
|
* |
3624
|
|
|
* @param float $value |
|
|
|
|
3625
|
|
|
* @param float $alpha Alpha Parameter |
|
|
|
|
3626
|
|
|
* @param float $beta Beta Parameter |
|
|
|
|
3627
|
|
|
* @param boolean $cumulative |
|
|
|
|
3628
|
|
|
* @return float |
3629
|
|
|
* |
3630
|
|
|
*/ |
3631
|
|
|
public static function ZTEST($dataSet, $m0, $sigma=null) { |
3632
|
|
|
$dataSet = PHPExcel_Calculation_Functions::flattenArrayIndexed($dataSet); |
3633
|
|
|
$m0 = PHPExcel_Calculation_Functions::flattenSingleValue($m0); |
3634
|
|
|
$sigma = PHPExcel_Calculation_Functions::flattenSingleValue($sigma); |
3635
|
|
|
|
3636
|
|
|
if (is_null($sigma)) { |
3637
|
|
|
$sigma = self::STDEV($dataSet); |
3638
|
|
|
} |
3639
|
|
|
$n = count($dataSet); |
3640
|
|
|
|
3641
|
|
|
return 1 - self::NORMSDIST((self::AVERAGE($dataSet) - $m0)/($sigma/SQRT($n))); |
3642
|
|
|
} // function ZTEST() |
3643
|
|
|
|
3644
|
|
|
} // class PHPExcel_Calculation_Statistical |
3645
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.