PageRenderTime 73ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/heart/protected/vendor/phpexcel/Classes/PHPExcel/Calculation/Statistical.php

https://github.com/wawancell/yiiheart
PHP | 3651 lines | 2023 code | 386 blank | 1242 comment | 647 complexity | e85ad470880e4acf2c2c76e288216b26 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception

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 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  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 - 2014 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. * @param mixed[] $averageArgs Data values
  755. * @return float
  756. */
  757. public static function AVERAGEIF($aArgs,$condition,$averageArgs = array()) {
  758. // Return value
  759. $returnValue = 0;
  760. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  761. $averageArgs = PHPExcel_Calculation_Functions::flattenArray($averageArgs);
  762. if (empty($averageArgs)) {
  763. $averageArgs = $aArgs;
  764. }
  765. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  766. // Loop through arguments
  767. $aCount = 0;
  768. foreach ($aArgs as $key => $arg) {
  769. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  770. $testCondition = '='.$arg.$condition;
  771. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  772. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  773. $returnValue += $arg;
  774. ++$aCount;
  775. }
  776. }
  777. }
  778. // Return
  779. if ($aCount > 0) {
  780. return $returnValue / $aCount;
  781. } else {
  782. return PHPExcel_Calculation_Functions::DIV0();
  783. }
  784. } // function AVERAGEIF()
  785. /**
  786. * BETADIST
  787. *
  788. * Returns the beta distribution.
  789. *
  790. * @param float $value Value at which you want to evaluate the distribution
  791. * @param float $alpha Parameter to the distribution
  792. * @param float $beta Parameter to the distribution
  793. * @param boolean $cumulative
  794. * @return float
  795. *
  796. */
  797. public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) {
  798. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  799. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  800. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  801. $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  802. $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  803. if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  804. if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
  805. return PHPExcel_Calculation_Functions::NaN();
  806. }
  807. if ($rMin > $rMax) {
  808. $tmp = $rMin;
  809. $rMin = $rMax;
  810. $rMax = $tmp;
  811. }
  812. $value -= $rMin;
  813. $value /= ($rMax - $rMin);
  814. return self::_incompleteBeta($value,$alpha,$beta);
  815. }
  816. return PHPExcel_Calculation_Functions::VALUE();
  817. } // function BETADIST()
  818. /**
  819. * BETAINV
  820. *
  821. * Returns the inverse of the beta distribution.
  822. *
  823. * @param float $probability Probability at which you want to evaluate the distribution
  824. * @param float $alpha Parameter to the distribution
  825. * @param float $beta Parameter to the distribution
  826. * @param float $rMin Minimum value
  827. * @param float $rMax Maximum value
  828. * @param boolean $cumulative
  829. * @return float
  830. *
  831. */
  832. public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) {
  833. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  834. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  835. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  836. $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin);
  837. $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax);
  838. if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
  839. if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
  840. return PHPExcel_Calculation_Functions::NaN();
  841. }
  842. if ($rMin > $rMax) {
  843. $tmp = $rMin;
  844. $rMin = $rMax;
  845. $rMax = $tmp;
  846. }
  847. $a = 0;
  848. $b = 2;
  849. $i = 0;
  850. while ((($b - $a) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  851. $guess = ($a + $b) / 2;
  852. $result = self::BETADIST($guess, $alpha, $beta);
  853. if (($result == $probability) || ($result == 0)) {
  854. $b = $a;
  855. } elseif ($result > $probability) {
  856. $b = $guess;
  857. } else {
  858. $a = $guess;
  859. }
  860. }
  861. if ($i == MAX_ITERATIONS) {
  862. return PHPExcel_Calculation_Functions::NA();
  863. }
  864. return round($rMin + $guess * ($rMax - $rMin),12);
  865. }
  866. return PHPExcel_Calculation_Functions::VALUE();
  867. } // function BETAINV()
  868. /**
  869. * BINOMDIST
  870. *
  871. * Returns the individual term binomial distribution probability. Use BINOMDIST in problems with
  872. * a fixed number of tests or trials, when the outcomes of any trial are only success or failure,
  873. * when trials are independent, and when the probability of success is constant throughout the
  874. * experiment. For example, BINOMDIST can calculate the probability that two of the next three
  875. * babies born are male.
  876. *
  877. * @param float $value Number of successes in trials
  878. * @param float $trials Number of trials
  879. * @param float $probability Probability of success on each trial
  880. * @param boolean $cumulative
  881. * @return float
  882. *
  883. * @todo Cumulative distribution function
  884. *
  885. */
  886. public static function BINOMDIST($value, $trials, $probability, $cumulative) {
  887. $value = floor(PHPExcel_Calculation_Functions::flattenSingleValue($value));
  888. $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  889. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  890. if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) {
  891. if (($value < 0) || ($value > $trials)) {
  892. return PHPExcel_Calculation_Functions::NaN();
  893. }
  894. if (($probability < 0) || ($probability > 1)) {
  895. return PHPExcel_Calculation_Functions::NaN();
  896. }
  897. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  898. if ($cumulative) {
  899. $summer = 0;
  900. for ($i = 0; $i <= $value; ++$i) {
  901. $summer += PHPExcel_Calculation_MathTrig::COMBIN($trials,$i) * pow($probability,$i) * pow(1 - $probability,$trials - $i);
  902. }
  903. return $summer;
  904. } else {
  905. return PHPExcel_Calculation_MathTrig::COMBIN($trials,$value) * pow($probability,$value) * pow(1 - $probability,$trials - $value) ;
  906. }
  907. }
  908. }
  909. return PHPExcel_Calculation_Functions::VALUE();
  910. } // function BINOMDIST()
  911. /**
  912. * CHIDIST
  913. *
  914. * Returns the one-tailed probability of the chi-squared distribution.
  915. *
  916. * @param float $value Value for the function
  917. * @param float $degrees degrees of freedom
  918. * @return float
  919. */
  920. public static function CHIDIST($value, $degrees) {
  921. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  922. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  923. if ((is_numeric($value)) && (is_numeric($degrees))) {
  924. if ($degrees < 1) {
  925. return PHPExcel_Calculation_Functions::NaN();
  926. }
  927. if ($value < 0) {
  928. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  929. return 1;
  930. }
  931. return PHPExcel_Calculation_Functions::NaN();
  932. }
  933. return 1 - (self::_incompleteGamma($degrees/2,$value/2) / self::_gamma($degrees/2));
  934. }
  935. return PHPExcel_Calculation_Functions::VALUE();
  936. } // function CHIDIST()
  937. /**
  938. * CHIINV
  939. *
  940. * Returns the one-tailed probability of the chi-squared distribution.
  941. *
  942. * @param float $probability Probability for the function
  943. * @param float $degrees degrees of freedom
  944. * @return float
  945. */
  946. public static function CHIINV($probability, $degrees) {
  947. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  948. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  949. if ((is_numeric($probability)) && (is_numeric($degrees))) {
  950. $xLo = 100;
  951. $xHi = 0;
  952. $x = $xNew = 1;
  953. $dx = 1;
  954. $i = 0;
  955. while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  956. // Apply Newton-Raphson step
  957. $result = self::CHIDIST($x, $degrees);
  958. $error = $result - $probability;
  959. if ($error == 0.0) {
  960. $dx = 0;
  961. } elseif ($error < 0.0) {
  962. $xLo = $x;
  963. } else {
  964. $xHi = $x;
  965. }
  966. // Avoid division by zero
  967. if ($result != 0.0) {
  968. $dx = $error / $result;
  969. $xNew = $x - $dx;
  970. }
  971. // If the NR fails to converge (which for example may be the
  972. // case if the initial guess is too rough) we apply a bisection
  973. // step to determine a more narrow interval around the root.
  974. if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) {
  975. $xNew = ($xLo + $xHi) / 2;
  976. $dx = $xNew - $x;
  977. }
  978. $x = $xNew;
  979. }
  980. if ($i == MAX_ITERATIONS) {
  981. return PHPExcel_Calculation_Functions::NA();
  982. }
  983. return round($x,12);
  984. }
  985. return PHPExcel_Calculation_Functions::VALUE();
  986. } // function CHIINV()
  987. /**
  988. * CONFIDENCE
  989. *
  990. * Returns the confidence interval for a population mean
  991. *
  992. * @param float $alpha
  993. * @param float $stdDev Standard Deviation
  994. * @param float $size
  995. * @return float
  996. *
  997. */
  998. public static function CONFIDENCE($alpha,$stdDev,$size) {
  999. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1000. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  1001. $size = floor(PHPExcel_Calculation_Functions::flattenSingleValue($size));
  1002. if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) {
  1003. if (($alpha <= 0) || ($alpha >= 1)) {
  1004. return PHPExcel_Calculation_Functions::NaN();
  1005. }
  1006. if (($stdDev <= 0) || ($size < 1)) {
  1007. return PHPExcel_Calculation_Functions::NaN();
  1008. }
  1009. return self::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size);
  1010. }
  1011. return PHPExcel_Calculation_Functions::VALUE();
  1012. } // function CONFIDENCE()
  1013. /**
  1014. * CORREL
  1015. *
  1016. * Returns covariance, the average of the products of deviations for each data point pair.
  1017. *
  1018. * @param array of mixed Data Series Y
  1019. * @param array of mixed Data Series X
  1020. * @return float
  1021. */
  1022. public static function CORREL($yValues,$xValues=null) {
  1023. if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) {
  1024. return PHPExcel_Calculation_Functions::VALUE();
  1025. }
  1026. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1027. return PHPExcel_Calculation_Functions::VALUE();
  1028. }
  1029. $yValueCount = count($yValues);
  1030. $xValueCount = count($xValues);
  1031. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1032. return PHPExcel_Calculation_Functions::NA();
  1033. } elseif ($yValueCount == 1) {
  1034. return PHPExcel_Calculation_Functions::DIV0();
  1035. }
  1036. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1037. return $bestFitLinear->getCorrelation();
  1038. } // function CORREL()
  1039. /**
  1040. * COUNT
  1041. *
  1042. * Counts the number of cells that contain numbers within the list of arguments
  1043. *
  1044. * Excel Function:
  1045. * COUNT(value1[,value2[, ...]])
  1046. *
  1047. * @access public
  1048. * @category Statistical Functions
  1049. * @param mixed $arg,... Data values
  1050. * @return int
  1051. */
  1052. public static function COUNT() {
  1053. // Return value
  1054. $returnValue = 0;
  1055. // Loop through arguments
  1056. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  1057. foreach ($aArgs as $k => $arg) {
  1058. if ((is_bool($arg)) &&
  1059. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1060. $arg = (integer) $arg;
  1061. }
  1062. // Is it a numeric value?
  1063. if ((is_numeric($arg)) && (!is_string($arg))) {
  1064. ++$returnValue;
  1065. }
  1066. }
  1067. // Return
  1068. return $returnValue;
  1069. } // function COUNT()
  1070. /**
  1071. * COUNTA
  1072. *
  1073. * Counts the number of cells that are not empty within the list of arguments
  1074. *
  1075. * Excel Function:
  1076. * COUNTA(value1[,value2[, ...]])
  1077. *
  1078. * @access public
  1079. * @category Statistical Functions
  1080. * @param mixed $arg,... Data values
  1081. * @return int
  1082. */
  1083. public static function COUNTA() {
  1084. // Return value
  1085. $returnValue = 0;
  1086. // Loop through arguments
  1087. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1088. foreach ($aArgs as $arg) {
  1089. // Is it a numeric, boolean or string value?
  1090. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  1091. ++$returnValue;
  1092. }
  1093. }
  1094. // Return
  1095. return $returnValue;
  1096. } // function COUNTA()
  1097. /**
  1098. * COUNTBLANK
  1099. *
  1100. * Counts the number of empty cells within the list of arguments
  1101. *
  1102. * Excel Function:
  1103. * COUNTBLANK(value1[,value2[, ...]])
  1104. *
  1105. * @access public
  1106. * @category Statistical Functions
  1107. * @param mixed $arg,... Data values
  1108. * @return int
  1109. */
  1110. public static function COUNTBLANK() {
  1111. // Return value
  1112. $returnValue = 0;
  1113. // Loop through arguments
  1114. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1115. foreach ($aArgs as $arg) {
  1116. // Is it a blank cell?
  1117. if ((is_null($arg)) || ((is_string($arg)) && ($arg == ''))) {
  1118. ++$returnValue;
  1119. }
  1120. }
  1121. // Return
  1122. return $returnValue;
  1123. } // function COUNTBLANK()
  1124. /**
  1125. * COUNTIF
  1126. *
  1127. * Counts the number of cells that contain numbers within the list of arguments
  1128. *
  1129. * Excel Function:
  1130. * COUNTIF(value1[,value2[, ...]],condition)
  1131. *
  1132. * @access public
  1133. * @category Statistical Functions
  1134. * @param mixed $arg,... Data values
  1135. * @param string $condition The criteria that defines which cells will be counted.
  1136. * @return int
  1137. */
  1138. public static function COUNTIF($aArgs,$condition) {
  1139. // Return value
  1140. $returnValue = 0;
  1141. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1142. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  1143. // Loop through arguments
  1144. foreach ($aArgs as $arg) {
  1145. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1146. $testCondition = '='.$arg.$condition;
  1147. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1148. // Is it a value within our criteria
  1149. ++$returnValue;
  1150. }
  1151. }
  1152. // Return
  1153. return $returnValue;
  1154. } // function COUNTIF()
  1155. /**
  1156. * COVAR
  1157. *
  1158. * Returns covariance, the average of the products of deviations for each data point pair.
  1159. *
  1160. * @param array of mixed Data Series Y
  1161. * @param array of mixed Data Series X
  1162. * @return float
  1163. */
  1164. public static function COVAR($yValues,$xValues) {
  1165. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1166. return PHPExcel_Calculation_Functions::VALUE();
  1167. }
  1168. $yValueCount = count($yValues);
  1169. $xValueCount = count($xValues);
  1170. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1171. return PHPExcel_Calculation_Functions::NA();
  1172. } elseif ($yValueCount == 1) {
  1173. return PHPExcel_Calculation_Functions::DIV0();
  1174. }
  1175. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1176. return $bestFitLinear->getCovariance();
  1177. } // function COVAR()
  1178. /**
  1179. * CRITBINOM
  1180. *
  1181. * Returns the smallest value for which the cumulative binomial distribution is greater
  1182. * than or equal to a criterion value
  1183. *
  1184. * See http://support.microsoft.com/kb/828117/ for details of the algorithm used
  1185. *
  1186. * @param float $trials number of Bernoulli trials
  1187. * @param float $probability probability of a success on each trial
  1188. * @param float $alpha criterion value
  1189. * @return int
  1190. *
  1191. * @todo Warning. This implementation differs from the algorithm detailed on the MS
  1192. * web site in that $CumPGuessMinus1 = $CumPGuess - 1 rather than $CumPGuess - $PGuess
  1193. * This eliminates a potential endless loop error, but may have an adverse affect on the
  1194. * accuracy of the function (although all my tests have so far returned correct results).
  1195. *
  1196. */
  1197. public static function CRITBINOM($trials, $probability, $alpha) {
  1198. $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials));
  1199. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1200. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1201. if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) {
  1202. if ($trials < 0) {
  1203. return PHPExcel_Calculation_Functions::NaN();
  1204. }
  1205. if (($probability < 0) || ($probability > 1)) {
  1206. return PHPExcel_Calculation_Functions::NaN();
  1207. }
  1208. if (($alpha < 0) || ($alpha > 1)) {
  1209. return PHPExcel_Calculation_Functions::NaN();
  1210. }
  1211. if ($alpha <= 0.5) {
  1212. $t = sqrt(log(1 / ($alpha * $alpha)));
  1213. $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));
  1214. } else {
  1215. $t = sqrt(log(1 / pow(1 - $alpha,2)));
  1216. $trialsApprox = $t - (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t);
  1217. }
  1218. $Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability)));
  1219. if ($Guess < 0) {
  1220. $Guess = 0;
  1221. } elseif ($Guess > $trials) {
  1222. $Guess = $trials;
  1223. }
  1224. $TotalUnscaledProbability = $UnscaledPGuess = $UnscaledCumPGuess = 0.0;
  1225. $EssentiallyZero = 10e-12;
  1226. $m = floor($trials * $probability);
  1227. ++$TotalUnscaledProbability;
  1228. if ($m == $Guess) { ++$UnscaledPGuess; }
  1229. if ($m <= $Guess) { ++$UnscaledCumPGuess; }
  1230. $PreviousValue = 1;
  1231. $Done = False;
  1232. $k = $m + 1;
  1233. while ((!$Done) && ($k <= $trials)) {
  1234. $CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability));
  1235. $TotalUnscaledProbability += $CurrentValue;
  1236. if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
  1237. if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
  1238. if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
  1239. $PreviousValue = $CurrentValue;
  1240. ++$k;
  1241. }
  1242. $PreviousValue = 1;
  1243. $Done = False;
  1244. $k = $m - 1;
  1245. while ((!$Done) && ($k >= 0)) {
  1246. $CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability);
  1247. $TotalUnscaledProbability += $CurrentValue;
  1248. if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
  1249. if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
  1250. if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
  1251. $PreviousValue = $CurrentValue;
  1252. --$k;
  1253. }
  1254. $PGuess = $UnscaledPGuess / $TotalUnscaledProbability;
  1255. $CumPGuess = $UnscaledCumPGuess / $TotalUnscaledProbability;
  1256. // $CumPGuessMinus1 = $CumPGuess - $PGuess;
  1257. $CumPGuessMinus1 = $CumPGuess - 1;
  1258. while (True) {
  1259. if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) {
  1260. return $Guess;
  1261. } elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) {
  1262. $PGuessPlus1 = $PGuess * ($trials - $Guess) * $probability / $Guess / (1 - $probability);
  1263. $CumPGuessMinus1 = $CumPGuess;
  1264. $CumPGuess = $CumPGuess + $PGuessPlus1;
  1265. $PGuess = $PGuessPlus1;
  1266. ++$Guess;
  1267. } elseif (($CumPGuessMinus1 >= $alpha) && ($CumPGuess >= $alpha)) {
  1268. $PGuessMinus1 = $PGuess * $Guess * (1 - $probability) / ($trials - $Guess + 1) / $probability;
  1269. $CumPGuess = $CumPGuessMinus1;
  1270. $CumPGuessMinus1 = $CumPGuessMinus1 - $PGuess;
  1271. $PGuess = $PGuessMinus1;
  1272. --$Guess;
  1273. }
  1274. }
  1275. }
  1276. return PHPExcel_Calculation_Functions::VALUE();
  1277. } // function CRITBINOM()
  1278. /**
  1279. * DEVSQ
  1280. *
  1281. * Returns the sum of squares of deviations of data points from their sample mean.
  1282. *
  1283. * Excel Function:
  1284. * DEVSQ(value1[,value2[, ...]])
  1285. *
  1286. * @access public
  1287. * @category Statistical Functions
  1288. * @param mixed $arg,... Data values
  1289. * @return float
  1290. */
  1291. public static function DEVSQ() {
  1292. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  1293. // Return value
  1294. $returnValue = null;
  1295. $aMean = self::AVERAGE($aArgs);
  1296. if ($aMean != PHPExcel_Calculation_Functions::DIV0()) {
  1297. $aCount = -1;
  1298. foreach ($aArgs as $k => $arg) {
  1299. // Is it a numeric value?
  1300. if ((is_bool($arg)) &&
  1301. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  1302. $arg = (integer) $arg;
  1303. }
  1304. if ((is_numeric($arg)) && (!is_string($arg))) {
  1305. if (is_null($returnValue)) {
  1306. $returnValue = pow(($arg - $aMean),2);
  1307. } else {
  1308. $returnValue += pow(($arg - $aMean),2);
  1309. }
  1310. ++$aCount;
  1311. }
  1312. }
  1313. // Return
  1314. if (is_null($returnValue)) {
  1315. return PHPExcel_Calculation_Functions::NaN();
  1316. } else {
  1317. return $returnValue;
  1318. }
  1319. }
  1320. return self::NA();
  1321. } // function DEVSQ()
  1322. /**
  1323. * EXPONDIST
  1324. *
  1325. * Returns the exponential distribution. Use EXPONDIST to model the time between events,
  1326. * such as how long an automated bank teller takes to deliver cash. For example, you can
  1327. * use EXPONDIST to determine the probability that the process takes at most 1 minute.
  1328. *
  1329. * @param float $value Value of the function
  1330. * @param float $lambda The parameter value
  1331. * @param boolean $cumulative
  1332. * @return float
  1333. */
  1334. public static function EXPONDIST($value, $lambda, $cumulative) {
  1335. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1336. $lambda = PHPExcel_Calculation_Functions::flattenSingleValue($lambda);
  1337. $cumulative = PHPExcel_Calculation_Functions::flattenSingleValue($cumulative);
  1338. if ((is_numeric($value)) && (is_numeric($lambda))) {
  1339. if (($value < 0) || ($lambda < 0)) {
  1340. return PHPExcel_Calculation_Functions::NaN();
  1341. }
  1342. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1343. if ($cumulative) {
  1344. return 1 - exp(0-$value*$lambda);
  1345. } else {
  1346. return $lambda * exp(0-$value*$lambda);
  1347. }
  1348. }
  1349. }
  1350. return PHPExcel_Calculation_Functions::VALUE();
  1351. } // function EXPONDIST()
  1352. /**
  1353. * FISHER
  1354. *
  1355. * Returns the Fisher transformation at x. This transformation produces a function that
  1356. * is normally distributed rather than skewed. Use this function to perform hypothesis
  1357. * testing on the correlation coefficient.
  1358. *
  1359. * @param float $value
  1360. * @return float
  1361. */
  1362. public static function FISHER($value) {
  1363. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1364. if (is_numeric($value)) {
  1365. if (($value <= -1) || ($value >= 1)) {
  1366. return PHPExcel_Calculation_Functions::NaN();
  1367. }
  1368. return 0.5 * log((1+$value)/(1-$value));
  1369. }
  1370. return PHPExcel_Calculation_Functions::VALUE();
  1371. } // function FISHER()
  1372. /**
  1373. * FISHERINV
  1374. *
  1375. * Returns the inverse of the Fisher transformation. Use this transformation when
  1376. * analyzing correlations between ranges or arrays of data. If y = FISHER(x), then
  1377. * FISHERINV(y) = x.
  1378. *
  1379. * @param float $value
  1380. * @return float
  1381. */
  1382. public static function FISHERINV($value) {
  1383. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1384. if (is_numeric($value)) {
  1385. return (exp(2 * $value) - 1) / (exp(2 * $value) + 1);
  1386. }
  1387. return PHPExcel_Calculation_Functions::VALUE();
  1388. } // function FISHERINV()
  1389. /**
  1390. * FORECAST
  1391. *
  1392. * Calculates, or predicts, a future value by using existing values. The predicted value is a y-value for a given x-value.
  1393. *
  1394. * @param float Value of X for which we want to find Y
  1395. * @param array of mixed Data Series Y
  1396. * @param array of mixed Data Series X
  1397. * @return float
  1398. */
  1399. public static function FORECAST($xValue,$yValues,$xValues) {
  1400. $xValue = PHPExcel_Calculation_Functions::flattenSingleValue($xValue);
  1401. if (!is_numeric($xValue)) {
  1402. return PHPExcel_Calculation_Functions::VALUE();
  1403. }
  1404. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1405. return PHPExcel_Calculation_Functions::VALUE();
  1406. }
  1407. $yValueCount = count($yValues);
  1408. $xValueCount = count($xValues);
  1409. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1410. return PHPExcel_Calculation_Functions::NA();
  1411. } elseif ($yValueCount == 1) {
  1412. return PHPExcel_Calculation_Functions::DIV0();
  1413. }
  1414. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1415. return $bestFitLinear->getValueOfYForX($xValue);
  1416. } // function FORECAST()
  1417. /**
  1418. * GAMMADIST
  1419. *
  1420. * Returns the gamma distribution.
  1421. *
  1422. * @param float $value Value at which you want to evaluate the distribution
  1423. * @param float $a Parameter to the distribution
  1424. * @param float $b Parameter to the distribution
  1425. * @param boolean $cumulative
  1426. * @return float
  1427. *
  1428. */
  1429. public static function GAMMADIST($value,$a,$b,$cumulative) {
  1430. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1431. $a = PHPExcel_Calculation_Functions::flattenSingleValue($a);
  1432. $b = PHPExcel_Calculation_Functions::flattenSingleValue($b);
  1433. if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) {
  1434. if (($value < 0) || ($a <= 0) || ($b <= 0)) {
  1435. return PHPExcel_Calculation_Functions::NaN();
  1436. }
  1437. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  1438. if ($cumulative) {
  1439. return self::_incompleteGamma($a,$value / $b) / self::_gamma($a);
  1440. } else {
  1441. return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b));
  1442. }
  1443. }
  1444. }
  1445. return PHPExcel_Calculation_Functions::VALUE();
  1446. } // function GAMMADIST()
  1447. /**
  1448. * GAMMAINV
  1449. *
  1450. * Returns the inverse of the beta distribution.
  1451. *
  1452. * @param float $probability Probability at which you want to evaluate the distribution
  1453. * @param float $alpha Parameter to the distribution
  1454. * @param float $beta Parameter to the distribution
  1455. * @return float
  1456. *
  1457. */
  1458. public static function GAMMAINV($probability,$alpha,$beta) {
  1459. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1460. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  1461. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  1462. if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) {
  1463. if (($alpha <= 0) || ($bet

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