30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
39 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/trend/trendClass.php';
43 define(
'LOG_GAMMA_X_MAX_VALUE', 2.55e305);
46 define(
'XMININ', 2.23e-308);
49 define(
'EPS', 2.22e-16);
52 define(
'SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
66 if (!is_array($array1)) { $array1 = array($array1); }
67 if (!is_array($array2)) { $array2 = array($array2); }
71 foreach($array1 as $key => $value) {
72 if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
77 foreach($array2 as $key => $value) {
78 if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
83 $array1 = array_merge($array1);
84 $array2 = array_merge($array2);
99 private static function _beta($p, $q) {
103 return exp(self::_logBeta($p, $q));
123 } elseif (
$x >= 1.0) {
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)) {
150 if ($p != self::$_logBetaCache_p || $q != self::$_logBetaCache_q) {
151 self::$_logBetaCache_p = $p;
152 self::$_logBetaCache_q = $q;
154 self::$_logBetaCache_result = 0.0;
173 $h = 1.0 - $sum_pq *
$x / $p_plus;
184 $d = $m * ($q - $m) *
$x / ( ($p_minus + $m2) * ($p + $m2));
196 $d = -($p + $m) * ($sum_pq + $m) *
$x / (($p + $m2) * ($p_plus + $m2));
263 static $lg_d1 = -0.5772156649015328605195174;
264 static $lg_d2 = 0.4227843350984671393993777;
265 static $lg_d4 = 1.791759469228055000094023;
267 static $lg_p1 = array( 4.945235359296727046734888,
268 201.8112620856775083915565,
269 2290.838373831346393026739,
270 11319.67205903380828685045,
271 28557.24635671635335736389,
272 38484.96228443793359990269,
273 26377.48787624195437963534,
274 7225.813979700288197698961 );
275 static $lg_p2 = array( 4.974607845568932035012064,
276 542.4138599891070494101986,
277 15506.93864978364947665077,
278 184793.2904445632425417223,
279 1088204.76946882876749847,
280 3338152.967987029735917223,
281 5106661.678927352456275255,
282 3074109.054850539556250927 );
283 static $lg_p4 = array( 14745.02166059939948905062,
284 2426813.369486704502836312,
285 121475557.4045093227939592,
286 2663432449.630976949898078,
287 29403789566.34553899906876,
288 170266573776.5398868392998,
289 492612579337.743088758812,
290 560625185622.3951465078242 );
292 static $lg_q1 = array( 67.48212550303777196073036,
293 1113.332393857199323513008,
294 7738.757056935398733233834,
295 27639.87074403340708898585,
296 54993.10206226157329794414,
297 61611.22180066002127833352,
298 36351.27591501940507276287,
299 8785.536302431013170870835 );
300 static $lg_q2 = array( 183.0328399370592604055942,
301 7765.049321445005871323047,
302 133190.3827966074194402448,
303 1136705.821321969608938755,
304 5267964.117437946917577538,
305 13467014.54311101692290052,
306 17827365.30353274213975932,
307 9533095.591844353613395747 );
308 static $lg_q4 = array( 2690.530175870899333379843,
309 639388.5654300092398984238,
310 41355999.30241388052042842,
311 1120872109.61614794137657,
312 14886137286.78813811542398,
313 101680358627.2438228077304,
314 341747634550.7377132798597,
315 446315818741.9713286462081 );
317 static $lg_c = array( -0.001910444077728,
319 -5.952379913043012e-4,
320 7.93650793500350248e-4,
321 -0.002777777777777681622553,
322 0.08333333333333333331554247,
326 static $lg_frtbig = 2.25e76;
327 static $pnt68 = 0.6796875;
330 if (
$x == self::$_logGammaCache_x) {
337 } elseif (
$y <= 1.5) {
348 if ($y <= 0.5 || $y >= $pnt68) {
351 for ($i = 0; $i < 8; ++$i) {
352 $xnum = $xnum * $xm1 + $lg_p1[$i];
353 $xden = $xden * $xm1 + $lg_q1[$i];
355 $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden));
360 for ($i = 0; $i < 8; ++$i) {
361 $xnum = $xnum * $xm2 + $lg_p2[$i];
362 $xden = $xden * $xm2 + $lg_q2[$i];
364 $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
366 } elseif (
$y <= 4.0) {
373 for ($i = 0; $i < 8; ++$i) {
374 $xnum = $xnum * $xm2 + $lg_p2[$i];
375 $xden = $xden * $xm2 + $lg_q2[$i];
377 $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
378 } elseif (
$y <= 12.0) {
385 for ($i = 0; $i < 8; ++$i) {
386 $xnum = $xnum * $xm4 + $lg_p4[$i];
387 $xden = $xden * $xm4 + $lg_q4[$i];
389 $res = $lg_d4 + $xm4 * ($xnum / $xden);
395 if (
$y <= $lg_frtbig) {
398 for ($i = 0; $i < 6; ++$i)
404 $res +=
$y * ($corr - 1.0);
415 self::$_logGammaCache_x =
$x;
416 self::$_logGammaCache_result =
$res;
427 for ($n=0; $n<=$max; ++$n) {
429 for ($i=1; $i<=$n; ++$i) {
430 $divisor *= ($a + $i);
432 $summer += (pow(
$x,$n) / $divisor);
434 return pow(
$x,$a) * exp(0-
$x) * $summer;
442 if (
$data == 0.0)
return 0;
444 static $p0 = 1.000000000190015;
445 static $p = array ( 1 => 76.18009172947146,
446 2 => -86.50532032941677,
447 3 => 24.01409824083091,
448 4 => -1.231739572450155,
449 5 => 1.208650973866179e-3,
450 6 => -5.395239384953e-6
455 $tmp -= (
$x + 0.5) * log($tmp);
458 for ($j=1;$j<=6;++$j) {
459 $summer += ($p[$j] / ++
$y);
461 return exp(0 - $tmp + log(
SQRT2PI * $summer /
$x));
487 static $a = array( 1 => -3.969683028665376e+01,
488 2 => 2.209460984245205e+02,
489 3 => -2.759285104469687e+02,
490 4 => 1.383577518672690e+02,
491 5 => -3.066479806614716e+01,
492 6 => 2.506628277459239e+00
495 static $b = array( 1 => -5.447609879822406e+01,
496 2 => 1.615858368580409e+02,
497 3 => -1.556989798598866e+02,
498 4 => 6.680131188771972e+01,
499 5 => -1.328068155288572e+01
502 static $c = array( 1 => -7.784894002430293e-03,
503 2 => -3.223964580411365e-01,
504 3 => -2.400758277161838e+00,
505 4 => -2.549732539343734e+00,
506 5 => 4.374664141464968e+00,
507 6 => 2.938163982698783e+00
510 static $d = array( 1 => 7.784695709041462e-03,
511 2 => 3.224671290700398e-01,
512 3 => 2.445134137142996e+00,
513 4 => 3.754408661907416e+00
518 $p_high = 1 - $p_low;
520 if (0 < $p && $p < $p_low) {
522 $q = sqrt(-2 * log($p));
523 return ((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) /
524 ((((
$d[1] * $q +
$d[2]) * $q +
$d[3]) * $q +
$d[4]) * $q + 1);
525 } elseif ($p_low <= $p && $p <= $p_high) {
529 return ((((($a[1] * $r + $a[2]) * $r + $a[3]) * $r + $a[4]) * $r + $a[5]) * $r + $a[6]) * $q /
530 ((((($b[1] * $r + $b[2]) * $r + $b[3]) * $r + $b[4]) * $r + $b[5]) * $r + 1);
531 } elseif ($p_high < $p && $p < 1) {
533 $q = sqrt(-2 * log(1 - $p));
534 return -((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) /
535 ((((
$d[1] * $q +
$d[2]) * $q +
$d[3]) * $q +
$d[4]) * $q + 1);
547 $a2 = -18.61500062529;
548 $a3 = 41.39119773534;
549 $a4 = -25.44106049637;
552 $b2 = 23.08336743743;
553 $b3 = -21.06224101826;
556 $c1 = 0.337475482272615;
557 $c2 = 0.976169019091719;
558 $c3 = 0.160797971491821;
559 $c4 = 2.76438810333863E-02;
560 $c5 = 3.8405729373609E-03;
561 $c6 = 3.951896511919E-04;
562 $c7 = 3.21767881768E-05;
563 $c8 = 2.888167364E-07;
564 $c9 = 3.960315187E-07;
567 if (abs(
$y) < 0.42) {
569 $z =
$y * ((($a4 * $z + $a3) * $z + $a2) * $z + $a1) / (((($b4 * $z + $b3) * $z + $b2) * $z + $b1) * $z + 1);
572 $z = log(-log(1 - $prob));
574 $z = log(-log($prob));
576 $z = $c1 + $z * ($c2 + $z * ($c3 + $z * ($c4 + $z * ($c5 + $z * ($c6 + $z * ($c7 + $z * ($c8 + $z * $c9)))))));
598 $a0 = 3.3871328727963666080;
599 $a1 = 1.3314166789178437745E+2;
600 $a2 = 1.9715909503065514427E+3;
601 $a3 = 1.3731693765509461125E+4;
602 $a4 = 4.5921953931549871457E+4;
603 $a5 = 6.7265770927008700853E+4;
604 $a6 = 3.3430575583588128105E+4;
605 $a7 = 2.5090809287301226727E+3;
607 $b1 = 4.2313330701600911252E+1;
608 $b2 = 6.8718700749205790830E+2;
609 $b3 = 5.3941960214247511077E+3;
610 $b4 = 2.1213794301586595867E+4;
611 $b5 = 3.9307895800092710610E+4;
612 $b6 = 2.8729085735721942674E+4;
613 $b7 = 5.2264952788528545610E+3;
616 $c0 = 1.42343711074968357734;
617 $c1 = 4.63033784615654529590;
618 $c2 = 5.76949722146069140550;
619 $c3 = 3.64784832476320460504;
620 $c4 = 1.27045825245236838258;
621 $c5 = 2.41780725177450611770E-1;
622 $c6 = 2.27238449892691845833E-2;
623 $c7 = 7.74545014278341407640E-4;
625 $d1 = 2.05319162663775882187;
626 $d2 = 1.67638483018380384940;
627 $d3 = 6.89767334985100004550E-1;
628 $d4 = 1.48103976427480074590E-1;
629 $d5 = 1.51986665636164571966E-2;
630 $d6 = 5.47593808499534494600E-4;
631 $d7 = 1.05075007164441684324E-9;
634 $e0 = 6.65790464350110377720;
635 $e1 = 5.46378491116411436990;
636 $e2 = 1.78482653991729133580;
637 $e3 = 2.96560571828504891230E-1;
638 $e4 = 2.65321895265761230930E-2;
639 $e5 = 1.24266094738807843860E-3;
640 $e6 = 2.71155556874348757815E-5;
641 $e7 = 2.01033439929228813265E-7;
643 $f1 = 5.99832206555887937690E-1;
644 $f2 = 1.36929880922735805310E-1;
645 $f3 = 1.48753612908506148525E-2;
646 $f4 = 7.86869131145613259100E-4;
647 $f5 = 1.84631831751005468180E-5;
648 $f6 = 1.42151175831644588870E-7;
649 $f7 = 2.04426310338993978564E-15;
654 if (abs($q) <= split1) {
655 $R = $const1 - $q * $q;
656 $z = $q * ((((((($a7 * $R + $a6) * $R + $a5) * $R + $a4) * $R + $a3) * $R + $a2) * $R + $a1) * $R + $a0) /
657 ((((((($b7 * $R + $b6) * $R + $b5) * $R + $b4) * $R + $b3) * $R + $b2) * $R + $b1) * $R + 1);
664 $R = pow(-log($R),2);
669 $z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) /
670 ((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1);
674 $z = ((((((($e7 * $R + $e6) * $R + $e5) * $R + $e4) * $R + $e3) * $R + $e2) * $R + $e1) * $R + $e0) /
675 ((((((($f7 * $R + $f6) * $R + $f5) * $R + $f4) * $R + $f3) * $R + $f2) * $R + $f1) * $R + 1);
708 foreach ($aArgs as $k =>
$arg) {
709 if ((is_bool(
$arg)) &&
714 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
715 if (is_null($returnValue)) {
716 $returnValue = abs(
$arg - $aMean);
718 $returnValue += abs(
$arg - $aMean);
728 return $returnValue / $aCount;
748 $returnValue = $aCount = 0;
752 if ((is_bool(
$arg)) &&
757 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
758 if (is_null($returnValue)) {
761 $returnValue +=
$arg;
769 return $returnValue / $aCount;
796 if ((is_bool(
$arg)) &&
799 if ((is_numeric(
$arg)) || (is_bool(
$arg)) || ((is_string(
$arg) && (
$arg !=
'')))) {
802 } elseif (is_string(
$arg)) {
805 if (is_null($returnValue)) {
808 $returnValue +=
$arg;
817 return $returnValue / $aCount;
838 public static function AVERAGEIF($aArgs,$condition,$averageArgs = array()) {
844 if (count($averageArgs) == 0) {
845 $averageArgs = $aArgs;
850 foreach ($aArgs as $key =>
$arg) {
852 $testCondition =
'='.$arg.$condition;
854 if ((is_null($returnValue)) || (
$arg > $returnValue)) {
855 $returnValue +=
$arg;
863 return $returnValue / $aCount;
882 public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) {
889 if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
890 if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
899 $value /= ($rMax - $rMin);
918 public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) {
925 if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
926 if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
939 $guess = ($a + $b) / 2;
941 if (($result == $probability) || ($result == 0)) {
943 } elseif ($result > $probability) {
952 return round($rMin + $guess * ($rMax - $rMin),12);
976 public static function BINOMDIST($value, $trials, $probability, $cumulative) {
981 if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) {
982 if (($value < 0) || ($value > $trials)) {
985 if (($probability < 0) || ($probability > 1)) {
988 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
991 for ($i = 0; $i <= $value; ++$i) {
1013 public static function CHIDIST($value, $degrees) {
1017 if ((is_numeric($value)) && (is_numeric($degrees))) {
1042 public static function CHIINV($probability, $degrees) {
1046 if ((is_numeric($probability)) && (is_numeric($degrees))) {
1058 $error = $result - $probability;
1061 } elseif (
$error < 0.0) {
1067 if ($result != 0.0) {
1074 if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) {
1075 $xNew = ($xLo + $xHi) / 2;
1083 return round(
$x,12);
1105 if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) {
1106 if (($alpha <= 0) || ($alpha >= 1)) {
1109 if (($stdDev <= 0) || ($size < 1)) {
1127 public static function CORREL($yValues,$xValues=null) {
1128 if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) {
1131 if (!self::_checkTrendArrays($yValues,$xValues)) {
1134 $yValueCount = count($yValues);
1135 $xValueCount = count($xValues);
1137 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
1139 } elseif ($yValueCount == 1) {
1144 return $bestFitLinear->getCorrelation();
1167 foreach ($aArgs as $k =>
$arg) {
1168 if ((is_bool(
$arg)) &&
1173 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
1179 return $returnValue;
1202 foreach ($aArgs as
$arg) {
1204 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
1210 return $returnValue;
1233 foreach ($aArgs as
$arg) {
1235 if ((is_null($arg)) || ((is_string($arg)) && ($arg ==
''))) {
1241 return $returnValue;
1259 public static function COUNTIF($aArgs,$condition) {
1266 foreach ($aArgs as
$arg) {
1268 $testCondition =
'='.$arg.$condition;
1276 return $returnValue;
1289 public static function COVAR($yValues,$xValues) {
1290 if (!self::_checkTrendArrays($yValues,$xValues)) {
1293 $yValueCount = count($yValues);
1294 $xValueCount = count($xValues);
1296 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
1298 } elseif ($yValueCount == 1) {
1303 return $bestFitLinear->getCovariance();
1326 public static function CRITBINOM($trials, $probability, $alpha) {
1331 if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) {
1335 if (($probability < 0) || ($probability > 1)) {
1338 if (($alpha < 0) || ($alpha > 1)) {
1341 if ($alpha <= 0.5) {
1342 $t = sqrt(log(1 / ($alpha * $alpha)));
1343 $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 $t = sqrt(log(1 / pow(1 - $alpha,2)));
1346 $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 $Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability)));
1351 } elseif ($Guess > $trials) {
1355 $TotalUnscaledProbability = $UnscaledPGuess = $UnscaledCumPGuess = 0.0;
1356 $EssentiallyZero = 10e-12;
1358 $m = floor($trials * $probability);
1359 ++$TotalUnscaledProbability;
1360 if ($m == $Guess) { ++$UnscaledPGuess; }
1361 if ($m <= $Guess) { ++$UnscaledCumPGuess; }
1366 while ((!$Done) && ($k <= $trials)) {
1367 $CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability));
1368 $TotalUnscaledProbability += $CurrentValue;
1369 if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
1370 if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
1371 if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
1372 $PreviousValue = $CurrentValue;
1379 while ((!$Done) && ($k >= 0)) {
1380 $CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability);
1381 $TotalUnscaledProbability += $CurrentValue;
1382 if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
1383 if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
1384 if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
1385 $PreviousValue = $CurrentValue;
1389 $PGuess = $UnscaledPGuess / $TotalUnscaledProbability;
1390 $CumPGuess = $UnscaledCumPGuess / $TotalUnscaledProbability;
1393 $CumPGuessMinus1 = $CumPGuess - 1;
1396 if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) {
1398 } elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) {
1399 $PGuessPlus1 = $PGuess * ($trials - $Guess) * $probability / $Guess / (1 - $probability);
1400 $CumPGuessMinus1 = $CumPGuess;
1401 $CumPGuess = $CumPGuess + $PGuessPlus1;
1402 $PGuess = $PGuessPlus1;
1404 } elseif (($CumPGuessMinus1 >= $alpha) && ($CumPGuess >= $alpha)) {
1405 $PGuessMinus1 = $PGuess * $Guess * (1 - $probability) / ($trials - $Guess + 1) / $probability;
1406 $CumPGuess = $CumPGuessMinus1;
1407 $CumPGuessMinus1 = $CumPGuessMinus1 - $PGuess;
1408 $PGuess = $PGuessMinus1;
1434 $returnValue = null;
1439 foreach ($aArgs as $k =>
$arg) {
1441 if ((is_bool(
$arg)) &&
1445 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
1446 if (is_null($returnValue)) {
1447 $returnValue = pow((
$arg - $aMean),2);
1449 $returnValue += pow((
$arg - $aMean),2);
1456 if (is_null($returnValue)) {
1459 return $returnValue;
1478 public static function EXPONDIST($value, $lambda, $cumulative) {
1483 if ((is_numeric($value)) && (is_numeric($lambda))) {
1484 if (($value < 0) || ($lambda < 0)) {
1487 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
1489 return 1 - exp(0-$value*$lambda);
1491 return $lambda * exp(0-$value*$lambda);
1512 if (is_numeric($value)) {
1513 if (($value <= -1) || ($value >= 1)) {
1516 return 0.5 * log((1+$value)/(1-$value));
1535 if (is_numeric($value)) {
1536 return (exp(2 * $value) - 1) / (exp(2 * $value) + 1);
1552 public static function FORECAST($xValue,$yValues,$xValues) {
1554 if (!is_numeric($xValue)) {
1558 if (!self::_checkTrendArrays($yValues,$xValues)) {
1561 $yValueCount = count($yValues);
1562 $xValueCount = count($xValues);
1564 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
1566 } elseif ($yValueCount == 1) {
1571 return $bestFitLinear->getValueOfYForX($xValue);
1587 public static function GAMMADIST($value,$a,$b,$cumulative) {
1592 if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) {
1593 if (($value < 0) || ($a <= 0) || ($b <= 0)) {
1596 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
1600 return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b));
1619 public static function GAMMAINV($probability,$alpha,$beta) {
1624 if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) {
1625 if (($alpha <= 0) || ($beta <= 0) || ($probability < 0) || ($probability > 1)) {
1630 $xHi = $alpha * $beta * 5;
1654 if (($xNew < $xLo) || ($xNew > $xHi) || ($pdf == 0.0)) {
1655 $xNew = ($xLo + $xHi) / 2;
1680 if (is_numeric($value)) {
1684 return log(self::_gamma($value));
1709 if (is_numeric($aMean) && ($aMean > 0)) {
1711 if (self::MIN($aArgs) > 0) {
1712 return pow($aMean, (1 / $aCount));
1730 public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True) {
1737 if (count($newValues) == 0) {
1738 $newValues = $bestFitExponential->getXValues();
1741 $returnArray = array();
1742 foreach($newValues as $xValue) {
1743 $returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue);
1746 return $returnArray;
1770 if (self::MIN($aArgs) < 0) {
1774 foreach ($aArgs as
$arg) {
1776 if ((is_numeric($arg)) && (!is_string($arg))) {
1780 if (is_null($returnValue)) {
1781 $returnValue = (1 /
$arg);
1783 $returnValue += (1 /
$arg);
1791 return 1 / ($returnValue / $aCount);
1793 return $returnValue;
1811 public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) {
1817 if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) {
1818 if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
1821 if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) {
1824 if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) {
1845 if (!self::_checkTrendArrays($yValues,$xValues)) {
1848 $yValueCount = count($yValues);
1849 $xValueCount = count($xValues);
1851 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
1853 } elseif ($yValueCount == 1) {
1858 return $bestFitLinear->getIntersect();
1879 $count = $summer = 0;
1881 foreach ($aArgs as $k =>
$arg) {
1882 if ((is_bool(
$arg)) &&
1886 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
1887 $summer += pow(((
$arg - $mean) / $stdDev),4) ;
1895 return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1,2) / (($count-2) * ($count-3)));
1922 $entry = floor(array_pop($aArgs));
1924 if ((is_numeric($entry)) && (!is_string($entry))) {
1926 foreach ($aArgs as
$arg) {
1928 if ((is_numeric($arg)) && (!is_string($arg))) {
1933 $entry = floor(--$entry);
1934 if (($entry < 0) || ($entry >= $count) || ($count == 0)) {
1938 return $mArgs[$entry];
1956 public static function LINEST($yValues,$xValues=null,$const=True,$stats=False) {
1961 if (!self::_checkTrendArrays($yValues,$xValues)) {
1964 $yValueCount = count($yValues);
1965 $xValueCount = count($xValues);
1968 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
1970 } elseif ($yValueCount == 1) {
1976 return array( array( $bestFitLinear->getSlope(),
1977 $bestFitLinear->getSlopeSE(),
1978 $bestFitLinear->getGoodnessOfFit(),
1979 $bestFitLinear->getF(),
1980 $bestFitLinear->getSSRegression(),
1982 array( $bestFitLinear->getIntersect(),
1983 $bestFitLinear->getIntersectSE(),
1984 $bestFitLinear->getStdevOfResiduals(),
1985 $bestFitLinear->getDFResiduals(),
1986 $bestFitLinear->getSSResiduals()
1990 return array( $bestFitLinear->getSlope(),
1991 $bestFitLinear->getIntersect()
2009 public static function LOGEST($yValues,$xValues=null,$const=True,$stats=False) {
2014 if (!self::_checkTrendArrays($yValues,$xValues)) {
2017 $yValueCount = count($yValues);
2018 $xValueCount = count($xValues);
2020 foreach($yValues as $value) {
2021 if ($value <= 0.0) {
2027 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2029 } elseif ($yValueCount == 1) {
2035 return array( array( $bestFitExponential->getSlope(),
2036 $bestFitExponential->getSlopeSE(),
2037 $bestFitExponential->getGoodnessOfFit(),
2038 $bestFitExponential->getF(),
2039 $bestFitExponential->getSSRegression(),
2041 array( $bestFitExponential->getIntersect(),
2042 $bestFitExponential->getIntersectSE(),
2043 $bestFitExponential->getStdevOfResiduals(),
2044 $bestFitExponential->getDFResiduals(),
2045 $bestFitExponential->getSSResiduals()
2049 return array( $bestFitExponential->getSlope(),
2050 $bestFitExponential->getIntersect()
2068 public static function LOGINV($probability, $mean, $stdDev) {
2073 if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2074 if (($probability < 0) || ($probability > 1) || ($stdDev <= 0)) {
2077 return exp($mean + $stdDev * self::NORMSINV($probability));
2097 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2098 if (($value <= 0) || ($stdDev <= 0)) {
2121 public static function MAX() {
2123 $returnValue = null;
2127 foreach ($aArgs as
$arg) {
2129 if ((is_numeric($arg)) && (!is_string($arg))) {
2130 if ((is_null($returnValue)) || ($arg > $returnValue)) {
2131 $returnValue =
$arg;
2137 if(is_null($returnValue)) {
2140 return $returnValue;
2159 $returnValue = null;
2163 foreach ($aArgs as
$arg) {
2165 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
2166 if (is_bool($arg)) {
2167 $arg = (integer) $arg;
2168 } elseif (is_string($arg)) {
2171 if ((is_null($returnValue)) || ($arg > $returnValue)) {
2172 $returnValue =
$arg;
2178 if(is_null($returnValue)) {
2181 return $returnValue;
2199 public static function MAXIF($aArgs,$condition,$sumArgs = array()) {
2201 $returnValue = null;
2205 if (count($sumArgs) == 0) {
2210 foreach ($aArgs as $key =>
$arg) {
2212 $testCondition =
'='.$arg.$condition;
2214 if ((is_null($returnValue)) || (
$arg > $returnValue)) {
2215 $returnValue =
$arg;
2221 return $returnValue;
2245 foreach ($aArgs as
$arg) {
2247 if ((is_numeric($arg)) && (!is_string($arg))) {
2252 $mValueCount = count($mArgs);
2253 if ($mValueCount > 0) {
2254 sort($mArgs,SORT_NUMERIC);
2255 $mValueCount = $mValueCount / 2;
2256 if ($mValueCount == floor($mValueCount)) {
2257 $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2;
2259 $mValueCount == floor($mValueCount);
2260 $returnValue = $mArgs[$mValueCount];
2265 return $returnValue;
2283 public static function MIN() {
2285 $returnValue = null;
2289 foreach ($aArgs as
$arg) {
2291 if ((is_numeric($arg)) && (!is_string($arg))) {
2292 if ((is_null($returnValue)) || ($arg < $returnValue)) {
2293 $returnValue =
$arg;
2299 if(is_null($returnValue)) {
2302 return $returnValue;
2321 $returnValue = null;
2325 foreach ($aArgs as
$arg) {
2327 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
2328 if (is_bool($arg)) {
2329 $arg = (integer) $arg;
2330 } elseif (is_string($arg)) {
2333 if ((is_null($returnValue)) || ($arg < $returnValue)) {
2334 $returnValue =
$arg;
2340 if(is_null($returnValue)) {
2343 return $returnValue;
2361 public static function MINIF($aArgs,$condition,$sumArgs = array()) {
2363 $returnValue = null;
2367 if (count($sumArgs) == 0) {
2372 foreach ($aArgs as $key =>
$arg) {
2374 $testCondition =
'='.$arg.$condition;
2376 if ((is_null($returnValue)) || (
$arg < $returnValue)) {
2377 $returnValue =
$arg;
2383 return $returnValue;
2392 $frequencyArray = array();
2393 foreach(
$data as $datum) {
2395 foreach($frequencyArray as $key => $value) {
2396 if ((
string) $value[
'value'] == (
string) $datum) {
2397 ++$frequencyArray[$key][
'frequency'];
2403 $frequencyArray[] = array(
'value' => $datum,
2408 foreach($frequencyArray as $key => $value) {
2409 $frequencyList[$key] = $value[
'frequency'];
2410 $valueList[$key] = $value[
'value'];
2412 array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray);
2414 if ($frequencyArray[0][
'frequency'] == 1) {
2417 return $frequencyArray[0][
'value'];
2442 foreach ($aArgs as
$arg) {
2444 if ((is_numeric($arg)) && (!is_string($arg))) {
2449 if (count($mArgs) > 0) {
2454 return $returnValue;
2478 if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) {
2479 if (($failures < 0) || ($successes < 1)) {
2482 if (($probability < 0) || ($probability > 1)) {
2486 if (($failures + $successes - 1) <= 0) {
2510 public static function NORMDIST($value, $mean, $stdDev, $cumulative) {
2515 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2519 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
2523 return (1 / (
SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean,2) / (2 * ($stdDev * $stdDev))));
2542 public static function NORMINV($probability,$mean,$stdDev) {
2547 if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2548 if (($probability < 0) || ($probability > 1)) {
2554 return (self::_inverse_ncdf($probability) * $stdDev) + $mean;
2608 $entry = array_pop($aArgs);
2610 if ((is_numeric($entry)) && (!is_string($entry))) {
2611 if (($entry < 0) || ($entry > 1)) {
2615 foreach ($aArgs as
$arg) {
2617 if ((is_numeric($arg)) && (!is_string($arg))) {
2621 $mValueCount = count($mArgs);
2622 if ($mValueCount > 0) {
2625 $index = $entry * ($count-1);
2626 $iBase = floor($index);
2627 if ($index == $iBase) {
2628 return $mArgs[$index];
2630 $iNext = $iBase + 1;
2631 $iProportion = $index - $iBase;
2632 return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ;
2650 public static function PERCENTRANK($valueSet,$value,$significance=3) {
2655 foreach($valueSet as $key => $valueEntry) {
2656 if (!is_numeric($valueEntry)) {
2657 unset($valueSet[$key]);
2660 sort($valueSet,SORT_NUMERIC);
2661 $valueCount = count($valueSet);
2662 if ($valueCount == 0) {
2666 $valueAdjustor = $valueCount - 1;
2667 if (($value < $valueSet[0]) || ($value > $valueSet[$valueAdjustor])) {
2671 $pos = array_search($value,$valueSet);
2672 if (
$pos === False) {
2674 $testValue = $valueSet[0];
2675 while ($testValue < $value) {
2676 $testValue = $valueSet[++
$pos];
2679 $pos += (($value - $valueSet[
$pos]) / ($testValue - $valueSet[
$pos]));
2682 return round(
$pos / $valueAdjustor,$significance);
2699 public static function PERMUT($numObjs,$numInSet) {
2703 if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
2704 $numInSet = floor($numInSet);
2705 if ($numObjs < $numInSet) {
2727 public static function POISSON($value, $mean, $cumulative) {
2731 if ((is_numeric($value)) && (is_numeric($mean))) {
2732 if (($value <= 0) || ($mean <= 0)) {
2735 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
2738 for ($i = 0; $i <= floor($value); ++$i) {
2741 return exp(0-$mean) * $summer;
2769 $entry = floor(array_pop($aArgs));
2771 if ((is_numeric($entry)) && (!is_string($entry))) {
2773 if (($entry < 0) || ($entry > 1)) {
2792 public static function RANK($value,$valueSet,$order=0) {
2797 foreach($valueSet as $key => $valueEntry) {
2798 if (!is_numeric($valueEntry)) {
2799 unset($valueSet[$key]);
2804 rsort($valueSet,SORT_NUMERIC);
2806 sort($valueSet,SORT_NUMERIC);
2808 $pos = array_search($value,$valueSet);
2809 if (
$pos === False) {
2826 public static function RSQ($yValues,$xValues) {
2827 if (!self::_checkTrendArrays($yValues,$xValues)) {
2830 $yValueCount = count($yValues);
2831 $xValueCount = count($xValues);
2833 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2835 } elseif ($yValueCount == 1) {
2840 return $bestFitLinear->getGoodnessOfFit();
2860 $count = $summer = 0;
2862 foreach ($aArgs as $k =>
$arg) {
2863 if ((is_bool(
$arg)) &&
2867 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
2868 $summer += pow(((
$arg - $mean) / $stdDev),3) ;
2876 return $summer * ($count / (($count-1) * ($count-2)));
2891 public static function SLOPE($yValues,$xValues) {
2892 if (!self::_checkTrendArrays($yValues,$xValues)) {
2895 $yValueCount = count($yValues);
2896 $xValueCount = count($xValues);
2898 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2900 } elseif ($yValueCount == 1) {
2905 return $bestFitLinear->getSlope();
2928 $entry = array_pop($aArgs);
2930 if ((is_numeric($entry)) && (!is_string($entry))) {
2932 foreach ($aArgs as
$arg) {
2934 if ((is_numeric($arg)) && (!is_string($arg))) {
2939 $entry = floor(--$entry);
2940 if (($entry < 0) || ($entry >= $count) || ($count == 0)) {
2944 return $mArgs[$entry];
2965 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2969 return ($value - $mean) / $stdDev ;
2993 $returnValue = null;
2996 if (!is_null($aMean)) {
2998 foreach ($aArgs as $k =>
$arg) {
2999 if ((is_bool(
$arg)) &&
3004 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
3005 if (is_null($returnValue)) {
3006 $returnValue = pow((
$arg - $aMean),2);
3008 $returnValue += pow((
$arg - $aMean),2);
3015 if (($aCount > 0) && ($returnValue >= 0)) {
3016 return sqrt($returnValue / $aCount);
3040 $returnValue = null;
3043 if (!is_null($aMean)) {
3045 foreach ($aArgs as $k =>
$arg) {
3046 if ((is_bool(
$arg)) &&
3050 if ((is_numeric(
$arg)) || (is_bool(
$arg)) || ((is_string(
$arg) & (
$arg !=
'')))) {
3051 if (is_bool(
$arg)) {
3053 } elseif (is_string(
$arg)) {
3056 if (is_null($returnValue)) {
3057 $returnValue = pow((
$arg - $aMean),2);
3059 $returnValue += pow((
$arg - $aMean),2);
3067 if (($aCount > 0) && ($returnValue >= 0)) {
3068 return sqrt($returnValue / $aCount);
3092 $returnValue = null;
3095 if (!is_null($aMean)) {
3097 foreach ($aArgs as $k =>
$arg) {
3098 if ((is_bool(
$arg)) &&
3103 if ((is_numeric(
$arg)) && (!is_string(
$arg))) {
3104 if (is_null($returnValue)) {
3105 $returnValue = pow((
$arg - $aMean),2);
3107 $returnValue += pow((
$arg - $aMean),2);
3114 if (($aCount > 0) && ($returnValue >= 0)) {
3115 return sqrt($returnValue / $aCount);
3139 $returnValue = null;
3142 if (!is_null($aMean)) {
3144 foreach ($aArgs as $k =>
$arg) {
3145 if ((is_bool(
$arg)) &&
3149 if ((is_numeric(
$arg)) || (is_bool(
$arg)) || ((is_string(
$arg) & (
$arg !=
'')))) {
3150 if (is_bool(
$arg)) {
3152 } elseif (is_string(
$arg)) {
3155 if (is_null($returnValue)) {
3156 $returnValue = pow((
$arg - $aMean),2);
3158 $returnValue += pow((
$arg - $aMean),2);
3166 if (($aCount > 0) && ($returnValue >= 0)) {
3167 return sqrt($returnValue / $aCount);
3183 public static function STEYX($yValues,$xValues) {
3184 if (!self::_checkTrendArrays($yValues,$xValues)) {
3187 $yValueCount = count($yValues);
3188 $xValueCount = count($xValues);
3190 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
3192 } elseif ($yValueCount == 1) {
3197 return $bestFitLinear->getStdevOfResiduals();
3211 public static function TDIST($value, $degrees, $tails) {
3216 if ((is_numeric($value)) && (is_numeric($degrees)) && (is_numeric($tails))) {
3217 if (($value < 0) || ($degrees < 1) || ($tails < 1) || ($tails > 2)) {
3230 $ttheta = atan2($value,sqrt($tterm));
3235 if (($degrees % 2) == 1) {
3244 while ($ti < $degrees) {
3245 $tterm *= $tc * $tc * ($ti - 1) / $ti;
3250 if (($degrees % 2) == 1) { $tsum =
M_2DIVPI * ($tsum + $ttheta); }
3251 $tValue = 0.5 * (1 + $tsum);
3253 return 1 - abs($tValue);
3255 return 1 - abs((1 - $tValue) - $tValue);
3271 public static function TINV($probability, $degrees) {
3275 if ((is_numeric($probability)) && (is_numeric($degrees))) {
3286 $error = $result - $probability;
3289 } elseif (
$error < 0.0) {
3295 if ($result != 0.0) {
3302 if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) {
3303 $xNew = ($xLo + $xHi) / 2;
3311 return round(
$x,12);
3328 public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True) {
3335 if (count($newValues) == 0) {
3336 $newValues = $bestFitLinear->getXValues();
3339 $returnArray = array();
3340 foreach($newValues as $xValue) {
3341 $returnArray[0][] = $bestFitLinear->getValueOfYForX($xValue);
3344 return $returnArray;
3368 $percent = array_pop($aArgs);
3370 if ((is_numeric($percent)) && (!is_string($percent))) {
3371 if (($percent < 0) || ($percent > 1)) {
3375 foreach ($aArgs as
$arg) {
3377 if ((is_numeric($arg)) && (!is_string($arg))) {
3381 $discard = floor(self::COUNT($mArgs) * $percent / 2);
3383 for ($i=0; $i < $discard; ++$i) {
3385 array_shift($mArgs);
3410 $summerA = $summerB = 0;
3415 foreach ($aArgs as
$arg) {
3416 if (is_bool($arg)) { $arg = (integer) $arg; }
3418 if ((is_numeric($arg)) && (!is_string($arg))) {
3419 $summerA += ($arg *
$arg);
3427 $summerA *= $aCount;
3428 $summerB *= $summerB;
3429 $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
3431 return $returnValue;
3452 $summerA = $summerB = 0;
3457 foreach ($aArgs as $k =>
$arg) {
3458 if ((is_string(
$arg)) &&
3461 } elseif ((is_string(
$arg)) &&
3465 if ((is_numeric(
$arg)) || (is_bool(
$arg)) || ((is_string(
$arg) & (
$arg !=
'')))) {
3466 if (is_bool(
$arg)) {
3468 } elseif (is_string(
$arg)) {
3480 $summerA *= $aCount;
3481 $summerB *= $summerB;
3482 $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
3484 return $returnValue;
3505 $summerA = $summerB = 0;
3510 foreach ($aArgs as
$arg) {
3511 if (is_bool($arg)) { $arg = (integer) $arg; }
3513 if ((is_numeric($arg)) && (!is_string($arg))) {
3514 $summerA += ($arg *
$arg);
3522 $summerA *= $aCount;
3523 $summerB *= $summerB;
3524 $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
3526 return $returnValue;
3547 $summerA = $summerB = 0;
3552 foreach ($aArgs as $k =>
$arg) {
3553 if ((is_string(
$arg)) &&
3556 } elseif ((is_string(
$arg)) &&
3560 if ((is_numeric(
$arg)) || (is_bool(
$arg)) || ((is_string(
$arg) & (
$arg !=
'')))) {
3561 if (is_bool(
$arg)) {
3563 } elseif (is_string(
$arg)) {
3575 $summerA *= $aCount;
3576 $summerB *= $summerB;
3577 $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
3579 return $returnValue;
3596 public static function WEIBULL($value, $alpha, $beta, $cumulative) {
3601 if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) {
3602 if (($value < 0) || ($alpha <= 0) || ($beta <= 0)) {
3605 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
3607 return 1 - exp(0 - pow($value / $beta,$alpha));
3609 return ($alpha / pow($beta,$alpha)) * pow($value,$alpha - 1) * exp(0 - pow($value / $beta,$alpha));
3630 public static function ZTEST($dataSet, $m0, $sigma=null) {
3635 if (is_null($sigma)) {
3638 $n = count($dataSet);
3640 return 1 -
self::NORMSDIST((self::AVERAGE($dataSet) - $m0)/($sigma/SQRT($n)));