PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/extensions/phpexcel/Classes/PHPExcel/Calculation/Statistical.php

https://bitbucket.org/rakot/boosted_yii
PHP | 3644 lines | 2023 code | 386 blank | 1235 comment | 647 complexity | a7ee7b8be7f2cbac77dd50bc35a8a35d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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.8, 2012-10-12
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/trendClass.php';
  36. /** LOG_GAMMA_X_MAX_VALUE */
  37. define('LOG_GAMMA_X_MAX_VALUE', 2.55e305);
  38. /** XMININ */
  39. define('XMININ', 2.23e-308);
  40. /** EPS */
  41. define('EPS', 2.22e-16);
  42. /** SQRT2PI */
  43. define('SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
  44. /**
  45. * PHPExcel_Calculation_Statistical
  46. *
  47. * @category PHPExcel
  48. * @package PHPExcel_Calculation
  49. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  50. */
  51. class PHPExcel_Calculation_Statistical {
  52. private static function _checkTrendArrays(&$array1,&$array2) {
  53. if (!is_array($array1)) { $array1 = array($array1); }
  54. if (!is_array($array2)) { $array2 = array($array2); }
  55. $array1 = PHPExcel_Calculation_Functions::flattenArray($array1);
  56. $array2 = PHPExcel_Calculation_Functions::flattenArray($array2);
  57. foreach($array1 as $key => $value) {
  58. if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
  59. unset($array1[$key]);
  60. unset($array2[$key]);
  61. }
  62. }
  63. foreach($array2 as $key => $value) {
  64. if ((is_bool($value)) || (is_string($value)) || (is_null($value))) {
  65. unset($array1[$key]);
  66. unset($array2[$key]);
  67. }
  68. }
  69. $array1 = array_merge($array1);
  70. $array2 = array_merge($array2);
  71. return True;
  72. } // function _checkTrendArrays()
  73. /**
  74. * Beta function.
  75. *
  76. * @author Jaco van Kooten
  77. *
  78. * @param p require p>0
  79. * @param q require q>0
  80. * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  81. */
  82. private static function _beta($p, $q) {
  83. if ($p <= 0.0 || $q <= 0.0 || ($p + $q) > LOG_GAMMA_X_MAX_VALUE) {
  84. return 0.0;
  85. } else {
  86. return exp(self::_logBeta($p, $q));
  87. }
  88. } // function _beta()
  89. /**
  90. * Incomplete beta function
  91. *
  92. * @author Jaco van Kooten
  93. * @author Paul Meagher
  94. *
  95. * The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992).
  96. * @param x require 0<=x<=1
  97. * @param p require p>0
  98. * @param q require q>0
  99. * @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
  100. */
  101. private static function _incompleteBeta($x, $p, $q) {
  102. if ($x <= 0.0) {
  103. return 0.0;
  104. } elseif ($x >= 1.0) {
  105. return 1.0;
  106. } elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE)) {
  107. return 0.0;
  108. }
  109. $beta_gam = exp((0 - self::_logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x));
  110. if ($x < ($p + 1.0) / ($p + $q + 2.0)) {
  111. return $beta_gam * self::_betaFraction($x, $p, $q) / $p;
  112. } else {
  113. return 1.0 - ($beta_gam * self::_betaFraction(1 - $x, $q, $p) / $q);
  114. }
  115. } // function _incompleteBeta()
  116. // Function cache for _logBeta function
  117. private static $_logBetaCache_p = 0.0;
  118. private static $_logBetaCache_q = 0.0;
  119. private static $_logBetaCache_result = 0.0;
  120. /**
  121. * The natural logarithm of the beta function.
  122. *
  123. * @param p require p>0
  124. * @param q require q>0
  125. * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
  126. * @author Jaco van Kooten
  127. */
  128. private static function _logBeta($p, $q) {
  129. if ($p != self::$_logBetaCache_p || $q != self::$_logBetaCache_q) {
  130. self::$_logBetaCache_p = $p;
  131. self::$_logBetaCache_q = $q;
  132. if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE)) {
  133. self::$_logBetaCache_result = 0.0;
  134. } else {
  135. self::$_logBetaCache_result = self::_logGamma($p) + self::_logGamma($q) - self::_logGamma($p + $q);
  136. }
  137. }
  138. return self::$_logBetaCache_result;
  139. } // function _logBeta()
  140. /**
  141. * Evaluates of continued fraction part of incomplete beta function.
  142. * Based on an idea from Numerical Recipes (W.H. Press et al, 1992).
  143. * @author Jaco van Kooten
  144. */
  145. private static function _betaFraction($x, $p, $q) {
  146. $c = 1.0;
  147. $sum_pq = $p + $q;
  148. $p_plus = $p + 1.0;
  149. $p_minus = $p - 1.0;
  150. $h = 1.0 - $sum_pq * $x / $p_plus;
  151. if (abs($h) < XMININ) {
  152. $h = XMININ;
  153. }
  154. $h = 1.0 / $h;
  155. $frac = $h;
  156. $m = 1;
  157. $delta = 0.0;
  158. while ($m <= MAX_ITERATIONS && abs($delta-1.0) > PRECISION ) {
  159. $m2 = 2 * $m;
  160. // even index for d
  161. $d = $m * ($q - $m) * $x / ( ($p_minus + $m2) * ($p + $m2));
  162. $h = 1.0 + $d * $h;
  163. if (abs($h) < XMININ) {
  164. $h = XMININ;
  165. }
  166. $h = 1.0 / $h;
  167. $c = 1.0 + $d / $c;
  168. if (abs($c) < XMININ) {
  169. $c = XMININ;
  170. }
  171. $frac *= $h * $c;
  172. // odd index for d
  173. $d = -($p + $m) * ($sum_pq + $m) * $x / (($p + $m2) * ($p_plus + $m2));
  174. $h = 1.0 + $d * $h;
  175. if (abs($h) < XMININ) {
  176. $h = XMININ;
  177. }
  178. $h = 1.0 / $h;
  179. $c = 1.0 + $d / $c;
  180. if (abs($c) < XMININ) {
  181. $c = XMININ;
  182. }
  183. $delta = $h * $c;
  184. $frac *= $delta;
  185. ++$m;
  186. }
  187. return $frac;
  188. } // function _betaFraction()
  189. /**
  190. * logGamma function
  191. *
  192. * @version 1.1
  193. * @author Jaco van Kooten
  194. *
  195. * Original author was Jaco van Kooten. Ported to PHP by Paul Meagher.
  196. *
  197. * The natural logarithm of the gamma function. <br />
  198. * Based on public domain NETLIB (Fortran) code by W. J. Cody and L. Stoltz <br />
  199. * Applied Mathematics Division <br />
  200. * Argonne National Laboratory <br />
  201. * Argonne, IL 60439 <br />
  202. * <p>
  203. * References:
  204. * <ol>
  205. * <li>W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for the Natural
  206. * Logarithm of the Gamma Function,' Math. Comp. 21, 1967, pp. 198-203.</li>
  207. * <li>K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May, 1969.</li>
  208. * <li>Hart, Et. Al., Computer Approximations, Wiley and sons, New York, 1968.</li>
  209. * </ol>
  210. * </p>
  211. * <p>
  212. * From the original documentation:
  213. * </p>
  214. * <p>
  215. * This routine calculates the LOG(GAMMA) function for a positive real argument X.
  216. * Computation is based on an algorithm outlined in references 1 and 2.
  217. * The program uses rational functions that theoretically approximate LOG(GAMMA)
  218. * to at least 18 significant decimal digits. The approximation for X > 12 is from
  219. * reference 3, while approximations for X < 12.0 are similar to those in reference
  220. * 1, but are unpublished. The accuracy achieved depends on the arithmetic system,
  221. * the compiler, the intrinsic functions, and proper selection of the
  222. * machine-dependent constants.
  223. * </p>
  224. * <p>
  225. * Error returns: <br />
  226. * The program returns the value XINF for X .LE. 0.0 or when overflow would occur.
  227. * The computation is believed to be free of underflow and overflow.
  228. * </p>
  229. * @return MAX_VALUE for x < 0.0 or when overflow would occur, i.e. x > 2.55E305
  230. */
  231. // Function cache for logGamma
  232. private static $_logGammaCache_result = 0.0;
  233. private static $_logGammaCache_x = 0.0;
  234. private static function _logGamma($x) {
  235. // Log Gamma related constants
  236. static $lg_d1 = -0.5772156649015328605195174;
  237. static $lg_d2 = 0.4227843350984671393993777;
  238. static $lg_d4 = 1.791759469228055000094023;
  239. static $lg_p1 = array( 4.945235359296727046734888,
  240. 201.8112620856775083915565,
  241. 2290.838373831346393026739,
  242. 11319.67205903380828685045,
  243. 28557.24635671635335736389,
  244. 38484.96228443793359990269,
  245. 26377.48787624195437963534,
  246. 7225.813979700288197698961 );
  247. static $lg_p2 = array( 4.974607845568932035012064,
  248. 542.4138599891070494101986,
  249. 15506.93864978364947665077,
  250. 184793.2904445632425417223,
  251. 1088204.76946882876749847,
  252. 3338152.967987029735917223,
  253. 5106661.678927352456275255,
  254. 3074109.054850539556250927 );
  255. static $lg_p4 = array( 14745.02166059939948905062,
  256. 2426813.369486704502836312,
  257. 121475557.4045093227939592,
  258. 2663432449.630976949898078,
  259. 29403789566.34553899906876,
  260. 170266573776.5398868392998,
  261. 492612579337.743088758812,
  262. 560625185622.3951465078242 );
  263. static $lg_q1 = array( 67.48212550303777196073036,
  264. 1113.332393857199323513008,
  265. 7738.757056935398733233834,
  266. 27639.87074403340708898585,
  267. 54993.10206226157329794414,
  268. 61611.22180066002127833352,
  269. 36351.27591501940507276287,
  270. 8785.536302431013170870835 );
  271. static $lg_q2 = array( 183.0328399370592604055942,
  272. 7765.049321445005871323047,
  273. 133190.3827966074194402448,
  274. 1136705.821321969608938755,
  275. 5267964.117437946917577538,
  276. 13467014.54311101692290052,
  277. 17827365.30353274213975932,
  278. 9533095.591844353613395747 );
  279. static $lg_q4 = array( 2690.530175870899333379843,
  280. 639388.5654300092398984238,
  281. 41355999.30241388052042842,
  282. 1120872109.61614794137657,
  283. 14886137286.78813811542398,
  284. 101680358627.2438228077304,
  285. 341747634550.7377132798597,
  286. 446315818741.9713286462081 );
  287. static $lg_c = array( -0.001910444077728,
  288. 8.4171387781295e-4,
  289. -5.952379913043012e-4,
  290. 7.93650793500350248e-4,
  291. -0.002777777777777681622553,
  292. 0.08333333333333333331554247,
  293. 0.0057083835261 );
  294. // Rough estimate of the fourth root of logGamma_xBig
  295. static $lg_frtbig = 2.25e76;
  296. static $pnt68 = 0.6796875;
  297. if ($x == self::$_logGammaCache_x) {
  298. return self::$_logGammaCache_result;
  299. }
  300. $y = $x;
  301. if ($y > 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE) {
  302. if ($y <= EPS) {
  303. $res = -log(y);
  304. } elseif ($y <= 1.5) {
  305. // ---------------------
  306. // EPS .LT. X .LE. 1.5
  307. // ---------------------
  308. if ($y < $pnt68) {
  309. $corr = -log($y);
  310. $xm1 = $y;
  311. } else {
  312. $corr = 0.0;
  313. $xm1 = $y - 1.0;
  314. }
  315. if ($y <= 0.5 || $y >= $pnt68) {
  316. $xden = 1.0;
  317. $xnum = 0.0;
  318. for ($i = 0; $i < 8; ++$i) {
  319. $xnum = $xnum * $xm1 + $lg_p1[$i];
  320. $xden = $xden * $xm1 + $lg_q1[$i];
  321. }
  322. $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden));
  323. } else {
  324. $xm2 = $y - 1.0;
  325. $xden = 1.0;
  326. $xnum = 0.0;
  327. for ($i = 0; $i < 8; ++$i) {
  328. $xnum = $xnum * $xm2 + $lg_p2[$i];
  329. $xden = $xden * $xm2 + $lg_q2[$i];
  330. }
  331. $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
  332. }
  333. } elseif ($y <= 4.0) {
  334. // ---------------------
  335. // 1.5 .LT. X .LE. 4.0
  336. // ---------------------
  337. $xm2 = $y - 2.0;
  338. $xden = 1.0;
  339. $xnum = 0.0;
  340. for ($i = 0; $i < 8; ++$i) {
  341. $xnum = $xnum * $xm2 + $lg_p2[$i];
  342. $xden = $xden * $xm2 + $lg_q2[$i];
  343. }
  344. $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
  345. } elseif ($y <= 12.0) {
  346. // ----------------------
  347. // 4.0 .LT. X .LE. 12.0
  348. // ----------------------
  349. $xm4 = $y - 4.0;
  350. $xden = -1.0;
  351. $xnum = 0.0;
  352. for ($i = 0; $i < 8; ++$i) {
  353. $xnum = $xnum * $xm4 + $lg_p4[$i];
  354. $xden = $xden * $xm4 + $lg_q4[$i];
  355. }
  356. $res = $lg_d4 + $xm4 * ($xnum / $xden);
  357. } else {
  358. // ---------------------------------
  359. // Evaluate for argument .GE. 12.0
  360. // ---------------------------------
  361. $res = 0.0;
  362. if ($y <= $lg_frtbig) {
  363. $res = $lg_c[6];
  364. $ysq = $y * $y;
  365. for ($i = 0; $i < 6; ++$i)
  366. $res = $res / $ysq + $lg_c[$i];
  367. }
  368. $res /= $y;
  369. $corr = log($y);
  370. $res = $res + log(SQRT2PI) - 0.5 * $corr;
  371. $res += $y * ($corr - 1.0);
  372. }
  373. } else {
  374. // --------------------------
  375. // Return for bad arguments
  376. // --------------------------
  377. $res = MAX_VALUE;
  378. }
  379. // ------------------------------
  380. // Final adjustments and return
  381. // ------------------------------
  382. self::$_logGammaCache_x = $x;
  383. self::$_logGammaCache_result = $res;
  384. return $res;
  385. } // function _logGamma()
  386. //
  387. // Private implementation of the incomplete Gamma function
  388. //
  389. private static function _incompleteGamma($a,$x) {
  390. static $max = 32;
  391. $summer = 0;
  392. for ($n=0; $n<=$max; ++$n) {
  393. $divisor = $a;
  394. for ($i=1; $i<=$n; ++$i) {
  395. $divisor *= ($a + $i);
  396. }
  397. $summer += (pow($x,$n) / $divisor);
  398. }
  399. return pow($x,$a) * exp(0-$x) * $summer;
  400. } // function _incompleteGamma()
  401. //
  402. // Private implementation of the Gamma function
  403. //
  404. private static function _gamma($data) {
  405. if ($data == 0.0) return 0;
  406. static $p0 = 1.000000000190015;
  407. static $p = array ( 1 => 76.18009172947146,
  408. 2 => -86.50532032941677,
  409. 3 => 24.01409824083091,
  410. 4 => -1.231739572450155,
  411. 5 => 1.208650973866179e-3,
  412. 6 => -5.395239384953e-6
  413. );
  414. $y = $x = $data;
  415. $tmp = $x + 5.5;
  416. $tmp -= ($x + 0.5) * log($tmp);
  417. $summer = $p0;
  418. for ($j=1;$j<=6;++$j) {
  419. $summer += ($p[$j] / ++$y);
  420. }
  421. return exp(0 - $tmp + log(SQRT2PI * $summer / $x));
  422. } // function _gamma()
  423. /***************************************************************************
  424. * inverse_ncdf.php
  425. * -------------------
  426. * begin : Friday, January 16, 2004
  427. * copyright : (C) 2004 Michael Nickerson
  428. * email : nickersonm@yahoo.com
  429. *
  430. ***************************************************************************/
  431. private static function _inverse_ncdf($p) {
  432. // Inverse ncdf approximation by Peter J. Acklam, implementation adapted to
  433. // PHP by Michael Nickerson, using Dr. Thomas Ziegler's C implementation as
  434. // a guide. http://home.online.no/~pjacklam/notes/invnorm/index.html
  435. // I have not checked the accuracy of this implementation. Be aware that PHP
  436. // will truncate the coeficcients to 14 digits.
  437. // You have permission to use and distribute this function freely for
  438. // whatever purpose you want, but please show common courtesy and give credit
  439. // where credit is due.
  440. // Input paramater is $p - probability - where 0 < p < 1.
  441. // Coefficients in rational approximations
  442. static $a = array( 1 => -3.969683028665376e+01,
  443. 2 => 2.209460984245205e+02,
  444. 3 => -2.759285104469687e+02,
  445. 4 => 1.383577518672690e+02,
  446. 5 => -3.066479806614716e+01,
  447. 6 => 2.506628277459239e+00
  448. );
  449. static $b = array( 1 => -5.447609879822406e+01,
  450. 2 => 1.615858368580409e+02,
  451. 3 => -1.556989798598866e+02,
  452. 4 => 6.680131188771972e+01,
  453. 5 => -1.328068155288572e+01
  454. );
  455. static $c = array( 1 => -7.784894002430293e-03,
  456. 2 => -3.223964580411365e-01,
  457. 3 => -2.400758277161838e+00,
  458. 4 => -2.549732539343734e+00,
  459. 5 => 4.374664141464968e+00,
  460. 6 => 2.938163982698783e+00
  461. );
  462. static $d = array( 1 => 7.784695709041462e-03,
  463. 2 => 3.224671290700398e-01,
  464. 3 => 2.445134137142996e+00,
  465. 4 => 3.754408661907416e+00
  466. );
  467. // Define lower and upper region break-points.
  468. $p_low = 0.02425; //Use lower region approx. below this
  469. $p_high = 1 - $p_low; //Use upper region approx. above this
  470. if (0 < $p && $p < $p_low) {
  471. // Rational approximation for lower region.
  472. $q = sqrt(-2 * log($p));
  473. return ((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) /
  474. (((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1);
  475. } elseif ($p_low <= $p && $p <= $p_high) {
  476. // Rational approximation for central region.
  477. $q = $p - 0.5;
  478. $r = $q * $q;
  479. return ((((($a[1] * $r + $a[2]) * $r + $a[3]) * $r + $a[4]) * $r + $a[5]) * $r + $a[6]) * $q /
  480. ((((($b[1] * $r + $b[2]) * $r + $b[3]) * $r + $b[4]) * $r + $b[5]) * $r + 1);
  481. } elseif ($p_high < $p && $p < 1) {
  482. // Rational approximation for upper region.
  483. $q = sqrt(-2 * log(1 - $p));
  484. return -((((($c[1] * $q + $c[2]) * $q + $c[3]) * $q + $c[4]) * $q + $c[5]) * $q + $c[6]) /
  485. (((($d[1] * $q + $d[2]) * $q + $d[3]) * $q + $d[4]) * $q + 1);
  486. }
  487. // If 0 < p < 1, return a null value
  488. return PHPExcel_Calculation_Functions::NULL();
  489. } // function _inverse_ncdf()
  490. private static function _inverse_ncdf2($prob) {
  491. // Approximation of inverse standard normal CDF developed by
  492. // B. Moro, "The Full Monte," Risk 8(2), Feb 1995, 57-58.
  493. $a1 = 2.50662823884;
  494. $a2 = -18.61500062529;
  495. $a3 = 41.39119773534;
  496. $a4 = -25.44106049637;
  497. $b1 = -8.4735109309;
  498. $b2 = 23.08336743743;
  499. $b3 = -21.06224101826;
  500. $b4 = 3.13082909833;
  501. $c1 = 0.337475482272615;
  502. $c2 = 0.976169019091719;
  503. $c3 = 0.160797971491821;
  504. $c4 = 2.76438810333863E-02;
  505. $c5 = 3.8405729373609E-03;
  506. $c6 = 3.951896511919E-04;
  507. $c7 = 3.21767881768E-05;
  508. $c8 = 2.888167364E-07;
  509. $c9 = 3.960315187E-07;
  510. $y = $prob - 0.5;
  511. if (abs($y) < 0.42) {
  512. $z = ($y * $y);
  513. $z = $y * ((($a4 * $z + $a3) * $z + $a2) * $z + $a1) / (((($b4 * $z + $b3) * $z + $b2) * $z + $b1) * $z + 1);
  514. } else {
  515. if ($y > 0) {
  516. $z = log(-log(1 - $prob));
  517. } else {
  518. $z = log(-log($prob));
  519. }
  520. $z = $c1 + $z * ($c2 + $z * ($c3 + $z * ($c4 + $z * ($c5 + $z * ($c6 + $z * ($c7 + $z * ($c8 + $z * $c9)))))));
  521. if ($y < 0) {
  522. $z = -$z;
  523. }
  524. }
  525. return $z;
  526. } // function _inverse_ncdf2()
  527. private static function _inverse_ncdf3($p) {
  528. // ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3.
  529. // Produces the normal deviate Z corresponding to a given lower
  530. // tail area of P; Z is accurate to about 1 part in 10**16.
  531. //
  532. // This is a PHP version of the original FORTRAN code that can
  533. // be found at http://lib.stat.cmu.edu/apstat/
  534. $split1 = 0.425;
  535. $split2 = 5;
  536. $const1 = 0.180625;
  537. $const2 = 1.6;
  538. // coefficients for p close to 0.5
  539. $a0 = 3.3871328727963666080;
  540. $a1 = 1.3314166789178437745E+2;
  541. $a2 = 1.9715909503065514427E+3;
  542. $a3 = 1.3731693765509461125E+4;
  543. $a4 = 4.5921953931549871457E+4;
  544. $a5 = 6.7265770927008700853E+4;
  545. $a6 = 3.3430575583588128105E+4;
  546. $a7 = 2.5090809287301226727E+3;
  547. $b1 = 4.2313330701600911252E+1;
  548. $b2 = 6.8718700749205790830E+2;
  549. $b3 = 5.3941960214247511077E+3;
  550. $b4 = 2.1213794301586595867E+4;
  551. $b5 = 3.9307895800092710610E+4;
  552. $b6 = 2.8729085735721942674E+4;
  553. $b7 = 5.2264952788528545610E+3;
  554. // coefficients for p not close to 0, 0.5 or 1.
  555. $c0 = 1.42343711074968357734;
  556. $c1 = 4.63033784615654529590;
  557. $c2 = 5.76949722146069140550;
  558. $c3 = 3.64784832476320460504;
  559. $c4 = 1.27045825245236838258;
  560. $c5 = 2.41780725177450611770E-1;
  561. $c6 = 2.27238449892691845833E-2;
  562. $c7 = 7.74545014278341407640E-4;
  563. $d1 = 2.05319162663775882187;
  564. $d2 = 1.67638483018380384940;
  565. $d3 = 6.89767334985100004550E-1;
  566. $d4 = 1.48103976427480074590E-1;
  567. $d5 = 1.51986665636164571966E-2;
  568. $d6 = 5.47593808499534494600E-4;
  569. $d7 = 1.05075007164441684324E-9;
  570. // coefficients for p near 0 or 1.
  571. $e0 = 6.65790464350110377720;
  572. $e1 = 5.46378491116411436990;
  573. $e2 = 1.78482653991729133580;
  574. $e3 = 2.96560571828504891230E-1;
  575. $e4 = 2.65321895265761230930E-2;
  576. $e5 = 1.24266094738807843860E-3;
  577. $e6 = 2.71155556874348757815E-5;
  578. $e7 = 2.01033439929228813265E-7;
  579. $f1 = 5.99832206555887937690E-1;
  580. $f2 = 1.36929880922735805310E-1;
  581. $f3 = 1.48753612908506148525E-2;
  582. $f4 = 7.86869131145613259100E-4;
  583. $f5 = 1.84631831751005468180E-5;
  584. $f6 = 1.42151175831644588870E-7;
  585. $f7 = 2.04426310338993978564E-15;
  586. $q = $p - 0.5;
  587. // computation for p close to 0.5
  588. if (abs($q) <= split1) {
  589. $R = $const1 - $q * $q;
  590. $z = $q * ((((((($a7 * $R + $a6) * $R + $a5) * $R + $a4) * $R + $a3) * $R + $a2) * $R + $a1) * $R + $a0) /
  591. ((((((($b7 * $R + $b6) * $R + $b5) * $R + $b4) * $R + $b3) * $R + $b2) * $R + $b1) * $R + 1);
  592. } else {
  593. if ($q < 0) {
  594. $R = $p;
  595. } else {
  596. $R = 1 - $p;
  597. }
  598. $R = pow(-log($R),2);
  599. // computation for p not close to 0, 0.5 or 1.
  600. If ($R <= $split2) {
  601. $R = $R - $const2;
  602. $z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) /
  603. ((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1);
  604. } else {
  605. // computation for p near 0 or 1.
  606. $R = $R - $split2;
  607. $z = ((((((($e7 * $R + $e6) * $R + $e5) * $R + $e4) * $R + $e3) * $R + $e2) * $R + $e1) * $R + $e0) /
  608. ((((((($f7 * $R + $f6) * $R + $f5) * $R + $f4) * $R + $f3) * $R + $f2) * $R + $f1) * $R + 1);
  609. }
  610. if ($q < 0) {
  611. $z = -$z;
  612. }
  613. }
  614. return $z;
  615. } // function _inverse_ncdf3()
  616. /**
  617. * AVEDEV
  618. *
  619. * Returns the average of the absolute deviations of data points from their mean.
  620. * AVEDEV is a measure of the variability in a data set.
  621. *
  622. * Excel Function:
  623. * AVEDEV(value1[,value2[, ...]])
  624. *
  625. * @access public
  626. * @category Statistical Functions
  627. * @param mixed $arg,... Data values
  628. * @return float
  629. */
  630. public static function AVEDEV() {
  631. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  632. // Return value
  633. $returnValue = null;
  634. $aMean = self::AVERAGE($aArgs);
  635. if ($aMean != PHPExcel_Calculation_Functions::DIV0()) {
  636. $aCount = 0;
  637. foreach ($aArgs as $k => $arg) {
  638. if ((is_bool($arg)) &&
  639. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  640. $arg = (integer) $arg;
  641. }
  642. // Is it a numeric value?
  643. if ((is_numeric($arg)) && (!is_string($arg))) {
  644. if (is_null($returnValue)) {
  645. $returnValue = abs($arg - $aMean);
  646. } else {
  647. $returnValue += abs($arg - $aMean);
  648. }
  649. ++$aCount;
  650. }
  651. }
  652. // Return
  653. if ($aCount == 0) {
  654. return PHPExcel_Calculation_Functions::DIV0();
  655. }
  656. return $returnValue / $aCount;
  657. }
  658. return PHPExcel_Calculation_Functions::NaN();
  659. } // function AVEDEV()
  660. /**
  661. * AVERAGE
  662. *
  663. * Returns the average (arithmetic mean) of the arguments
  664. *
  665. * Excel Function:
  666. * AVERAGE(value1[,value2[, ...]])
  667. *
  668. * @access public
  669. * @category Statistical Functions
  670. * @param mixed $arg,... Data values
  671. * @return float
  672. */
  673. public static function AVERAGE() {
  674. $returnValue = $aCount = 0;
  675. // Loop through arguments
  676. foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg) {
  677. if ((is_bool($arg)) &&
  678. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  679. $arg = (integer) $arg;
  680. }
  681. // Is it a numeric value?
  682. if ((is_numeric($arg)) && (!is_string($arg))) {
  683. if (is_null($returnValue)) {
  684. $returnValue = $arg;
  685. } else {
  686. $returnValue += $arg;
  687. }
  688. ++$aCount;
  689. }
  690. }
  691. // Return
  692. if ($aCount > 0) {
  693. return $returnValue / $aCount;
  694. } else {
  695. return PHPExcel_Calculation_Functions::DIV0();
  696. }
  697. } // function AVERAGE()
  698. /**
  699. * AVERAGEA
  700. *
  701. * Returns the average of its arguments, including numbers, text, and logical values
  702. *
  703. * Excel Function:
  704. * AVERAGEA(value1[,value2[, ...]])
  705. *
  706. * @access public
  707. * @category Statistical Functions
  708. * @param mixed $arg,... Data values
  709. * @return float
  710. */
  711. public static function AVERAGEA() {
  712. // Return value
  713. $returnValue = null;
  714. $aCount = 0;
  715. // Loop through arguments
  716. foreach (PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()) as $k => $arg) {
  717. if ((is_bool($arg)) &&
  718. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  719. } else {
  720. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  721. if (is_bool($arg)) {
  722. $arg = (integer) $arg;
  723. } elseif (is_string($arg)) {
  724. $arg = 0;
  725. }
  726. if (is_null($returnValue)) {
  727. $returnValue = $arg;
  728. } else {
  729. $returnValue += $arg;
  730. }
  731. ++$aCount;
  732. }
  733. }
  734. }
  735. // Return
  736. if ($aCount > 0) {
  737. return $returnValue / $aCount;
  738. } else {
  739. return PHPExcel_Calculation_Functions::DIV0();
  740. }
  741. } // function AVERAGEA()
  742. /**
  743. * AVERAGEIF
  744. *
  745. * Returns the average value from a range of cells that contain numbers within the list of arguments
  746. *
  747. * Excel Function:
  748. * AVERAGEIF(value1[,value2[, ...]],condition)
  749. *
  750. * @access public
  751. * @category Mathematical and Trigonometric Functions
  752. * @param mixed $arg,... Data values
  753. * @param string $condition The criteria that defines which cells will be checked.
  754. * @return float
  755. */
  756. public static function AVERAGEIF($aArgs,$condition,$averageArgs = array()) {
  757. // Return value
  758. $returnValue = 0;
  759. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  760. $averageArgs = PHPExcel_Calculation_Functions::flattenArray($averageArgs);
  761. if (empty($averageArgs)) {
  762. $averageArgs = $aArgs;
  763. }
  764. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  765. // Loop through arguments
  766. $aCount = 0;
  767. foreach ($aArgs as $key => $arg) {
  768. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  769. $testCondition = '='.$arg.$condition;
  770. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  771. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  772. $returnValue += $arg;
  773. ++$aCount;
  774. }
  775. }
  776. }
  777. // Return
  778. if ($aCount > 0) {
  779. return $returnValue / $aCount;
  780. } else {
  781. return PHPExcel_Calculation_Functions::DIV0();
  782. }
  783. } // function AVERAGEIF()
  784. /**
  785. * BETADIST
  786. *
  787. * Returns the beta distribution.
  788. *
  789. * @param float $value Value at which you want to evaluate the distribution
  790. * @param float $alpha Parameter to the distribution
  791. * @param float $beta Parameter to the distribution
  792. * @param boolean $cumulative
  793. * @return float
  794. *
  795. */
  796. public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) {
  797. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  798. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  799. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  800. $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  801. $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  802. if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  803. if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
  804. return PHPExcel_Calculation_Functions::NaN();
  805. }
  806. if ($rMin > $rMax) {
  807. $tmp = $rMin;
  808. $rMin = $rMax;
  809. $rMax = $tmp;
  810. }
  811. $value -= $rMin;
  812. $value /= ($rMax - $rMin);
  813. return self::_incompleteBeta($value,$alpha,$beta);
  814. }
  815. return PHPExcel_Calculation_Functions::VALUE();
  816. } // function BETADIST()
  817. /**
  818. * BETAINV
  819. *
  820. * Returns the inverse of the beta distribution.
  821. *
  822. * @param float $probability Probability at which you want to evaluate the distribution
  823. * @param float $alpha Parameter to the distribution
  824. * @param float $beta Parameter to the distribution
  825. * @param boolean $cumulative
  826. * @return float
  827. *
  828. */
  829. public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) {
  830. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  831. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  832. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  833. $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  834. $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  835. if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  836. if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
  837. return PHPExcel_Calculation_Functions::NaN();
  838. }
  839. if ($rMin > $rMax) {
  840. $tmp = $rMin;
  841. $rMin = $rMax;
  842. $rMax = $tmp;
  843. }
  844. $a = 0;
  845. $b = 2;
  846. $i = 0;
  847. while ((($b - $a) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  848. $guess = ($a + $b) / 2;
  849. $result = self::BETADIST($guess, $alpha, $beta);
  850. if (($result == $probability) || ($result == 0)) {
  851. $b = $a;
  852. } elseif ($result > $probability) {
  853. $b = $guess;
  854. } else {
  855. $a = $guess;
  856. }
  857. }
  858. if ($i == MAX_ITERATIONS) {
  859. return PHPExcel_Calculation_Functions::NA();
  860. }
  861. return round($rMin + $guess * ($rMax - $rMin),12);
  862. }
  863. return PHPExcel_Calculation_Functions::VALUE();
  864. } // function BETAINV()
  865. /**
  866. * BINOMDIST
  867. *
  868. * Returns the individual term binomial distribution probability. Use BINOMDIST in problems with
  869. * a fixed number of tests or trials, when the outcomes of any trial are only success or failure,
  870. * when trials are independent, and when the probability of success is constant throughout the
  871. * experiment. For example, BINOMDIST can calculate the probability that two of the next three
  872. * babies born are male.
  873. *
  874. * @param float $value Number of successes in trials
  875. * @param float $trials Number of trials
  876. * @param float $probability Probability of success on each trial
  877. * @param boolean $cumulative
  878. * @return float
  879. *
  880. * @todo Cumulative distribution function
  881. *
  882. */
  883. public static function BINOMDIST($value, $trials, $probability, $cumulative) {
  884. $value = floor(PHPExcel_Calculation_Functions::flattenSingleValue($value));
  885. $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  886. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  887. if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) {
  888. if (($value < 0) || ($value > $trials)) {
  889. return PHPExcel_Calculation_Functions::NaN();
  890. }
  891. if (($probability < 0) || ($probability > 1)) {
  892. return PHPExcel_Calculation_Functions::NaN();
  893. }
  894. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  895. if ($cumulative) {
  896. $summer = 0;
  897. for ($i = 0; $i <= $value; ++$i) {
  898. $summer += PHPExcel_Calculation_MathTrig::COMBIN($trials,$i) * pow($probability,$i) * pow(1 - $probability,$trials - $i);
  899. }
  900. return $summer;
  901. } else {
  902. return PHPExcel_Calculation_MathTrig::COMBIN($trials,$value) * pow($probability,$value) * pow(1 - $probability,$trials - $value) ;
  903. }
  904. }
  905. }
  906. return PHPExcel_Calculation_Functions::VALUE();
  907. } // function BINOMDIST()
  908. /**
  909. * CHIDIST
  910. *
  911. * Returns the one-tailed probability of the chi-squared distribution.
  912. *
  913. * @param float $value Value for the function
  914. * @param float $degrees degrees of freedom
  915. * @return float
  916. */
  917. public static function CHIDIST($value, $degrees) {
  918. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  919. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  920. if ((is_numeric($value)) && (is_numeric($degrees))) {
  921. if ($degrees < 1) {
  922. return PHPExcel_Calculation_Functions::NaN();
  923. }
  924. if ($value < 0) {
  925. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  926. return 1;
  927. }
  928. return PHPExcel_Calculation_Functions::NaN();
  929. }
  930. return 1 - (self::_incompleteGamma($degrees/2,$value/2) / self::_gamma($degrees/2));
  931. }
  932. return PHPExcel_Calculation_Functions::VALUE();
  933. } // function CHIDIST()
  934. /**
  935. * CHIINV
  936. *
  937. * Returns the one-tailed probability of the chi-squared distribution.
  938. *
  939. * @param float $probability Probability for the function
  940. * @param float $degrees degrees of freedom
  941. * @return float
  942. */
  943. public static function CHIINV($probability, $degrees) {
  944. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  945. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  946. if ((is_numeric($probability)) && (is_numeric($degrees))) {
  947. $xLo = 100;
  948. $xHi = 0;
  949. $x = $xNew = 1;
  950. $dx = 1;
  951. $i = 0;
  952. while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  953. // Apply Newton-Raphson step
  954. $result = self::CHIDIST($x, $degrees);
  955. $error = $result - $probability;
  956. if ($error == 0.0) {
  957. $dx = 0;
  958. } elseif ($error < 0.0) {
  959. $xLo = $x;
  960. } else {
  961. $xHi = $x;
  962. }
  963. // Avoid division by zero
  964. if ($result != 0.0) {
  965. $dx = $error / $result;
  966. $xNew = $x - $dx;
  967. }
  968. // If the NR fails to converge (which for example may be the
  969. // case if the initial guess is too rough) we apply a bisection
  970. // step to determine a more narrow interval around the root.
  971. if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) {
  972. $xNew = ($xLo + $xHi) / 2;
  973. $dx = $xNew - $x;
  974. }
  975. $x = $xNew;
  976. }
  977. if ($i == MAX_ITERATIONS) {
  978. return PHPExcel_Calculation_Functions::NA();
  979. }
  980. return round($x,12);
  981. }
  982. return PHPExcel_Calculation_Functions::VALUE();
  983. } // function CHIINV()
  984. /**
  985. * CONFIDENCE
  986. *
  987. * Returns the confidence interval for a population mean
  988. *
  989. * @param float $alpha
  990. * @param float $stdDev Standard Deviation
  991. * @param float $size
  992. * @return float
  993. *
  994. */
  995. public static function CONFIDENCE($alpha,$stdDev,$size) {
  996. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  997. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  998. $size = floor(PHPExcel_Calculation_Functions::flattenSingleValue($size));
  999. if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) {
  1000. if (($alpha <= 0) || ($alpha >= 1)) {
  1001. return PHPExcel_Calculation_Functions::NaN();
  1002. }
  1003. if (($stdDev <= 0) || ($size < 1)) {
  1004. return PHPExcel_Calculation_Functions::NaN();
  1005. }
  1006. return self::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size);
  1007. }
  1008. return PHPExcel_Calculation_Functions::VALUE();
  1009. } // function CONFIDENCE()
  1010. /**
  1011. * CORREL
  1012. *
  1013. * Returns covariance, the average of the products of deviations for each data point pair.
  1014. *
  1015. * @param array of mixed Data Series Y
  1016. * @param array of mixed Data Series X
  1017. * @return float
  1018. */
  1019. public static function CORREL($yValues,$xValues=null) {
  1020. if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) {
  1021. return PHPExcel_Calculation_Functions::VALUE();
  1022. }
  1023. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1024. return PHPExcel_Calculation_Functions::VALUE();
  1025. }
  1026. $yValueCount = count($yValues);
  1027. $xValueCount = count($xValues);
  1028. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1029. return PHPExcel_Calculation_Functions::NA();
  1030. } elseif ($yValueCount == 1) {
  1031. return PHPExcel_Calculation_Functions::DIV0();
  1032. }
  1033. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1034. return $bestFitLinear->getCorrelation();
  1035. } // function CORREL()
  1036. /**
  1037. * COUNT
  1038. *
  1039. * Counts the number of cells that contain numbers within the list of arguments
  1040. *
  1041. * Excel Function:
  1042. * COUNT(value1[,value2[, ...]])
  1043. *
  1044. * @access public
  1045. * @category Statistical Functions
  1046. * @param mixed $arg,... Data values
  1047. * @return int
  1048. */
  1049. public static function COUNT() {
  1050. // Return value
  1051. $returnValue = 0;
  1052. // Loop through arguments
  1053. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  1054. foreach ($aArgs as $k => $arg) {
  1055. if ((is_bool($arg)) &&
  1056. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1057. $arg = (integer) $arg;
  1058. }
  1059. // Is it a numeric value?
  1060. if ((is_numeric($arg)) && (!is_string($arg))) {
  1061. ++$returnValue;
  1062. }
  1063. }
  1064. // Return
  1065. return $returnValue;
  1066. } // function COUNT()
  1067. /**
  1068. * COUNTA
  1069. *
  1070. * Counts the number of cells that are not empty within the list of arguments
  1071. *
  1072. * Excel Function:
  1073. * COUNTA(value1[,value2[, ...]])
  1074. *
  1075. * @access public
  1076. * @category Statistical Functions
  1077. * @param mixed $arg,... Data values
  1078. * @return int
  1079. */
  1080. public static function COUNTA() {
  1081. // Return value
  1082. $returnValue = 0;
  1083. // Loop through arguments
  1084. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1085. foreach ($aArgs as $arg) {
  1086. // Is it a numeric, boolean or string value?
  1087. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  1088. ++$returnValue;
  1089. }
  1090. }
  1091. // Return
  1092. return $returnValue;
  1093. } // function COUNTA()
  1094. /**
  1095. * COUNTBLANK
  1096. *
  1097. * Counts the number of empty cells within the list of arguments
  1098. *
  1099. * Excel Function:
  1100. * COUNTBLANK(value1[,value2[, ...]])
  1101. *
  1102. * @access public
  1103. * @category Statistical Functions
  1104. * @param mixed $arg,... Data values
  1105. * @return int
  1106. */
  1107. public static function COUNTBLANK() {
  1108. // Return value
  1109. $returnValue = 0;
  1110. // Loop through arguments
  1111. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1112. foreach ($aArgs as $arg) {
  1113. // Is it a blank cell?
  1114. if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) {
  1115. ++$returnValue;
  1116. }
  1117. }
  1118. // Return
  1119. return $returnValue;
  1120. } // function COUNTBLANK()
  1121. /**
  1122. * COUNTIF
  1123. *
  1124. * Counts the number of cells that contain numbers within the list of arguments
  1125. *
  1126. * Excel Function:
  1127. * COUNTIF(value1[,value2[, ...]],condition)
  1128. *
  1129. * @access public
  1130. * @category Statistical Functions
  1131. * @param mixed $arg,... Data values
  1132. * @param string $condition The criteria that defines which cells will be counted.
  1133. * @return int
  1134. */
  1135. public static function COUNTIF($aArgs,$condition) {
  1136. // Return value
  1137. $returnValue = 0;
  1138. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1139. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  1140. // Loop through arguments
  1141. foreach ($aArgs as $arg) {
  1142. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1143. $testCondition = '='.$arg.$condition;
  1144. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1145. // Is it a value within our criteria
  1146. ++$returnValue;
  1147. }
  1148. }
  1149. // Return
  1150. return $returnValue;
  1151. } // function COUNTIF()
  1152. /**
  1153. * COVAR
  1154. *
  1155. * Returns covariance, the average of the products of deviations for each data point pair.
  1156. *
  1157. * @param array of mixed Data Series Y
  1158. * @param array of mixed Data Series X
  1159. * @return float
  1160. */
  1161. public static function COVAR($yValues,$xValues) {
  1162. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1163. return PHPExcel_Calculation_Functions::VALUE();
  1164. }
  1165. $yValueCount = count($yValues);
  1166. $xValueCount = count($xValues);
  1167. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1168. return PHPExcel_Calculation_Functions::NA();
  1169. } elseif ($yValueCount == 1) {
  1170. return PHPExcel_Calculation_Functions::DIV0();
  1171. }
  1172. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1173. return $bestFitLinear->getCovariance();
  1174. } // function COVAR()
  1175. /**
  1176. * CRITBINOM
  1177. *
  1178. * Returns the smallest value for which the cumulative binomial distribution is greater
  1179. * than or equal to a criterion value
  1180. *
  1181. * See http://support.microsoft.com/kb/828117/ for details of the algorithm used
  1182. *
  1183. * @param float $trials number of Bernoulli trials
  1184. * @param float $probability probability of a success on each trial
  1185. * @param float $alpha criterion value
  1186. * @return int
  1187. *
  1188. * @todo Warning. This implementation differs from the algorithm detailed on the MS
  1189. * web site in that $CumPGuessMinus1 = $CumPGuess - 1 rather than $CumPGuess - $PGuess
  1190. * This eliminates a potential endless loop error, but may have an adverse affect on the
  1191. * accuracy of the function (although all my tests have so far returned correct results).
  1192. *
  1193. */
  1194. public static function CRITBINOM($trials, $probability, $alpha) {
  1195. $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  1196. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1197. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1198. if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) {
  1199. if ($trials < 0) {
  1200. return PHPExcel_Calculation_Functions::NaN();
  1201. }
  1202. if (($probability < 0) || ($probability > 1)) {
  1203. return PHPExcel_Calculation_Functions::NaN();
  1204. }
  1205. if (($alpha < 0) || ($alpha > 1)) {
  1206. return PHPExcel_Calculation_Functions::NaN();
  1207. }
  1208. if ($alpha <= 0.5) {
  1209. $t = sqrt(log(1 / ($alpha * $alpha)));
  1210. $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));
  1211. } else {
  1212. $t = sqrt(log(1 / pow(1 - $alpha,2)));
  1213. $trialsApprox = $t - (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t);
  1214. }
  1215. $Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability)));
  1216. if ($Guess < 0) {
  1217. $Guess = 0;
  1218. } elseif ($Guess > $trials) {
  1219. $Guess = $trials;
  1220. }
  1221. $TotalUnscaledProbability = $UnscaledPGuess = $UnscaledCumPGuess = 0.0;
  1222. $EssentiallyZero = 10e-12;
  1223. $m = floor($trials * $probability);
  1224. ++$TotalUnscaledProbability;
  1225. if ($m == $Guess) { ++$UnscaledPGuess; }
  1226. if ($m <= $Guess) { ++$UnscaledCumPGuess; }
  1227. $PreviousValue = 1;
  1228. $Done = False;
  1229. $k = $m + 1;
  1230. while ((!$Done) && ($k <= $trials)) {
  1231. $CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability));
  1232. $TotalUnscaledProbability += $CurrentValue;
  1233. if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
  1234. if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
  1235. if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
  1236. $PreviousValue = $CurrentValue;
  1237. ++$k;
  1238. }
  1239. $PreviousValue = 1;
  1240. $Done = False;
  1241. $k = $m - 1;
  1242. while ((!$Done) && ($k >= 0)) {
  1243. $CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability);
  1244. $TotalUnscaledProbability += $CurrentValue;
  1245. if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
  1246. if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
  1247. if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
  1248. $PreviousValue = $CurrentValue;
  1249. --$k;
  1250. }
  1251. $PGuess = $UnscaledPGuess / $TotalUnscaledProbability;
  1252. $CumPGuess = $UnscaledCumPGuess / $TotalUnscaledProbability;
  1253. // $CumPGuessMinus1 = $CumPGuess - $PGuess;
  1254. $CumPGuessMinus1 = $CumPGuess - 1;
  1255. while (True) {
  1256. if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) {
  1257. return $Guess;
  1258. } elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) {
  1259. $PGuessPlus1 = $PGuess * ($trials - $Guess) * $probability / $Guess / (1 - $probability);
  1260. $CumPGuessMinus1 = $CumPGuess;
  1261. $CumPGuess = $CumPGuess + $PGuessPlus1;
  1262. $PGuess = $PGuessPlus1;
  1263. ++$Guess;
  1264. } elseif (($CumPGuessMinus1 >= $alpha) && ($CumPGuess >= $alpha)) {
  1265. $PGuessMinus1 = $PGuess * $Guess * (1 - $probability) / ($trials - $Guess + 1) / $probability;
  1266. $CumPGuess = $CumPGuessMinus1;
  1267. $CumPGuessMinus1 = $CumPGuessMinus1 - $PGuess;
  1268. $PGuess = $PGuessMinus1;
  1269. --$Guess;
  1270. }
  1271. }
  1272. }
  1273. return PHPExcel_Calculation_Functions::VALUE();
  1274. } // function CRITBINOM()
  1275. /**
  1276. * DEVSQ
  1277. *
  1278. * Returns the sum of squares of deviations of data points from their sample mean.
  1279. *
  1280. * Excel Function:
  1281. * DEVSQ(value1[,value2[, ...]])
  1282. *
  1283. * @access public
  1284. * @category Statistical Functions
  1285. * @param mixed $arg,... Data values
  1286. * @return float
  1287. */
  1288. public static function DEVSQ() {
  1289. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  1290. // Return value
  1291. $returnValue = null;
  1292. $aMean = self::AVERAGE($aArgs);
  1293. if ($aMean != PHPExcel_Calculation_Functions::DIV0()) {
  1294. $aCount = -1;
  1295. foreach ($aArgs as $k => $arg) {
  1296. // Is it a numeric value?
  1297. if ((is_bool($arg)) &&
  1298. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1299. $arg = (integer) $arg;
  1300. }
  1301. if ((is_numeric($arg)) && (!is_string($arg))) {
  1302. if (is_null($returnValue)) {
  1303. $returnValue = pow(($arg - $aMean),2);
  1304. } else {
  1305. $returnValue += pow(($arg - $aMean),2);
  1306. }
  1307. ++$aCount;
  1308. }
  1309. }
  1310. // Return
  1311. if (is_null($returnValue)) {
  1312. return PHPExcel_Calculation_Functions::NaN();
  1313. } else {
  1314. return $returnValue;
  1315. }
  1316. }
  1317. return self::NA();
  1318. } // function DEVSQ()
  1319. /**
  1320. * EXPONDIST
  1321. *
  1322. * Returns the exponential distribution. Use EXPONDIST to model the time between events,
  1323. * such as how long an automated bank teller takes to deliver cash. For example, you can
  1324. * use EXPONDIST to determine the probability that the process takes at most 1 minute.
  1325. *
  1326. * @param float $value Value of the function
  1327. * @param float $lambda The parameter value
  1328. * @param boolean $cumulative
  1329. * @return float
  1330. */
  1331. public static function EXPONDIST($value, $lambda, $cumulative) {
  1332. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1333. $lambda = PHPExcel_Calculation_Functions::flattenSingleValue($lambda);
  1334. $cumulative = PHPExcel_Calculation_Functions::flattenSingleValue($cumulative);
  1335. if ((is_numeric($value)) && (is_numeric($lambda))) {
  1336. if (($value < 0) || ($lambda < 0)) {
  1337. return PHPExcel_Calculation_Functions::NaN();
  1338. }
  1339. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1340. if ($cumulative) {
  1341. return 1 - exp(0-$value*$lambda);
  1342. } else {
  1343. return $lambda * exp(0-$value*$lambda);
  1344. }
  1345. }
  1346. }
  1347. return PHPExcel_Calculation_Functions::VALUE();
  1348. } // function EXPONDIST()
  1349. /**
  1350. * FISHER
  1351. *
  1352. * Returns the Fisher transformation at x. This transformation produces a function that
  1353. * is normally distributed rather than skewed. Use this function to perform hypothesis
  1354. * testing on the correlation coefficient.
  1355. *
  1356. * @param float $value
  1357. * @return float
  1358. */
  1359. public static function FISHER($value) {
  1360. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1361. if (is_numeric($value)) {
  1362. if (($value <= -1) || ($value >= 1)) {
  1363. return PHPExcel_Calculation_Functions::NaN();
  1364. }
  1365. return 0.5 * log((1+$value)/(1-$value));
  1366. }
  1367. return PHPExcel_Calculation_Functions::VALUE();
  1368. } // function FISHER()
  1369. /**
  1370. * FISHERINV
  1371. *
  1372. * Returns the inverse of the Fisher transformation. Use this transformation when
  1373. * analyzing correlations between ranges or arrays of data. If y = FISHER(x), then
  1374. * FISHERINV(y) = x.
  1375. *
  1376. * @param float $value
  1377. * @return float
  1378. */
  1379. public static function FISHERINV($value) {
  1380. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1381. if (is_numeric($value)) {
  1382. return (exp(2 * $value) - 1) / (exp(2 * $value) + 1);
  1383. }
  1384. return PHPExcel_Calculation_Functions::VALUE();
  1385. } // function FISHERINV()
  1386. /**
  1387. * FORECAST
  1388. *
  1389. * Calculates, or predicts, a future value by using existing values. The predicted value is a y-value for a given x-value.
  1390. *
  1391. * @param float Value of X for which we want to find Y
  1392. * @param array of mixed Data Series Y
  1393. * @param array of mixed Data Series X
  1394. * @return float
  1395. */
  1396. public static function FORECAST($xValue,$yValues,$xValues) {
  1397. $xValue = PHPExcel_Calculation_Functions::flattenSingleValue($xValue);
  1398. if (!is_numeric($xValue)) {
  1399. return PHPExcel_Calculation_Functions::VALUE();
  1400. }
  1401. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1402. return PHPExcel_Calculation_Functions::VALUE();
  1403. }
  1404. $yValueCount = count($yValues);
  1405. $xValueCount = count($xValues);
  1406. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1407. return PHPExcel_Calculation_Functions::NA();
  1408. } elseif ($yValueCount == 1) {
  1409. return PHPExcel_Calculation_Functions::DIV0();
  1410. }
  1411. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1412. return $bestFitLinear->getValueOfYForX($xValue);
  1413. } // function FORECAST()
  1414. /**
  1415. * GAMMADIST
  1416. *
  1417. * Returns the gamma distribution.
  1418. *
  1419. * @param float $value Value at which you want to evaluate the distribution
  1420. * @param float $a Parameter to the distribution
  1421. * @param float $b Parameter to the distribution
  1422. * @param boolean $cumulative
  1423. * @return float
  1424. *
  1425. */
  1426. public static function GAMMADIST($value,$a,$b,$cumulative) {
  1427. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1428. $a = PHPExcel_Calculation_Functions::flattenSingleValue($a);
  1429. $b = PHPExcel_Calculation_Functions::flattenSingleValue($b);
  1430. if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) {
  1431. if (($value < 0) || ($a <= 0) || ($b <= 0)) {
  1432. return PHPExcel_Calculation_Functions::NaN();
  1433. }
  1434. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1435. if ($cumulative) {
  1436. return self::_incompleteGamma($a,$value / $b) / self::_gamma($a);
  1437. } else {
  1438. return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b));
  1439. }
  1440. }
  1441. }
  1442. return PHPExcel_Calculation_Functions::VALUE();
  1443. } // function GAMMADIST()
  1444. /**
  1445. * GAMMAINV
  1446. *
  1447. * Returns the inverse of the beta distribution.
  1448. *
  1449. * @param float $probability Probability at which you want to evaluate the distribution
  1450. * @param float $alpha Parameter to the distribution
  1451. * @param float $beta Parameter to the distribution
  1452. * @return float
  1453. *
  1454. */
  1455. public static function GAMMAINV($probability,$alpha,$beta) {
  1456. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1457. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1458. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  1459. if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) {
  1460. if (($alpha <= 0) || ($beta <= 0) || ($probability < 0) || ($probability > 1)) {
  1461. return PHPExcel_Calculation_Functions::NaN();
  1462. }
  1463. $xLo = 0;

Large files files are truncated, but you can click here to view the full file