PageRenderTime 77ms CodeModel.GetById 28ms 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
  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;
  1464. $xHi = $alpha * $beta * 5;
  1465. $x = $xNew = 1;
  1466. $error = $pdf = 0;
  1467. $dx = 1024;
  1468. $i = 0;
  1469. while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  1470. // Apply Newton-Raphson step
  1471. $error = self::GAMMADIST($x, $alpha, $beta, True) - $probability;
  1472. if ($error < 0.0) {
  1473. $xLo = $x;
  1474. } else {
  1475. $xHi = $x;
  1476. }
  1477. $pdf = self::GAMMADIST($x, $alpha, $beta, False);
  1478. // Avoid division by zero
  1479. if ($pdf != 0.0) {
  1480. $dx = $error / $pdf;
  1481. $xNew = $x - $dx;
  1482. }
  1483. // If the NR fails to converge (which for example may be the
  1484. // case if the initial guess is too rough) we apply a bisection
  1485. // step to determine a more narrow interval around the root.
  1486. if (($xNew < $xLo) || ($xNew > $xHi) || ($pdf == 0.0)) {
  1487. $xNew = ($xLo + $xHi) / 2;
  1488. $dx = $xNew - $x;
  1489. }
  1490. $x = $xNew;
  1491. }
  1492. if ($i == MAX_ITERATIONS) {
  1493. return PHPExcel_Calculation_Functions::NA();
  1494. }
  1495. return $x;
  1496. }
  1497. return PHPExcel_Calculation_Functions::VALUE();
  1498. } // function GAMMAINV()
  1499. /**
  1500. * GAMMALN
  1501. *
  1502. * Returns the natural logarithm of the gamma function.
  1503. *
  1504. * @param float $value
  1505. * @return float
  1506. */
  1507. public static function GAMMALN($value) {
  1508. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1509. if (is_numeric($value)) {
  1510. if ($value <= 0) {
  1511. return PHPExcel_Calculation_Functions::NaN();
  1512. }
  1513. return log(self::_gamma($value));
  1514. }
  1515. return PHPExcel_Calculation_Functions::VALUE();
  1516. } // function GAMMALN()
  1517. /**
  1518. * GEOMEAN
  1519. *
  1520. * Returns the geometric mean of an array or range of positive data. For example, you
  1521. * can use GEOMEAN to calculate average growth rate given compound interest with
  1522. * variable rates.
  1523. *
  1524. * Excel Function:
  1525. * GEOMEAN(value1[,value2[, ...]])
  1526. *
  1527. * @access public
  1528. * @category Statistical Functions
  1529. * @param mixed $arg,... Data values
  1530. * @return float
  1531. */
  1532. public static function GEOMEAN() {
  1533. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1534. $aMean = PHPExcel_Calculation_MathTrig::PRODUCT($aArgs);
  1535. if (is_numeric($aMean) && ($aMean > 0)) {
  1536. $aCount = self::COUNT($aArgs) ;
  1537. if (self::MIN($aArgs) > 0) {
  1538. return pow($aMean, (1 / $aCount));
  1539. }
  1540. }
  1541. return PHPExcel_Calculation_Functions::NaN();
  1542. } // GEOMEAN()
  1543. /**
  1544. * GROWTH
  1545. *
  1546. * Returns values along a predicted emponential trend
  1547. *
  1548. * @param array of mixed Data Series Y
  1549. * @param array of mixed Data Series X
  1550. * @param array of mixed Values of X for which we want to find Y
  1551. * @param boolean A logical value specifying whether to force the intersect to equal 0.
  1552. * @return array of float
  1553. */
  1554. public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True) {
  1555. $yValues = PHPExcel_Calculation_Functions::flattenArray($yValues);
  1556. $xValues = PHPExcel_Calculation_Functions::flattenArray($xValues);
  1557. $newValues = PHPExcel_Calculation_Functions::flattenArray($newValues);
  1558. $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  1559. $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const);
  1560. if (empty($newValues)) {
  1561. $newValues = $bestFitExponential->getXValues();
  1562. }
  1563. $returnArray = array();
  1564. foreach($newValues as $xValue) {
  1565. $returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue);
  1566. }
  1567. return $returnArray;
  1568. } // function GROWTH()
  1569. /**
  1570. * HARMEAN
  1571. *
  1572. * Returns the harmonic mean of a data set. The harmonic mean is the reciprocal of the
  1573. * arithmetic mean of reciprocals.
  1574. *
  1575. * Excel Function:
  1576. * HARMEAN(value1[,value2[, ...]])
  1577. *
  1578. * @access public
  1579. * @category Statistical Functions
  1580. * @param mixed $arg,... Data values
  1581. * @return float
  1582. */
  1583. public static function HARMEAN() {
  1584. // Return value
  1585. $returnValue = PHPExcel_Calculation_Functions::NA();
  1586. // Loop through arguments
  1587. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1588. if (self::MIN($aArgs) < 0) {
  1589. return PHPExcel_Calculation_Functions::NaN();
  1590. }
  1591. $aCount = 0;
  1592. foreach ($aArgs as $arg) {
  1593. // Is it a numeric value?
  1594. if ((is_numeric($arg)) && (!is_string($arg))) {
  1595. if ($arg <= 0) {
  1596. return PHPExcel_Calculation_Functions::NaN();
  1597. }
  1598. if (is_null($returnValue)) {
  1599. $returnValue = (1 / $arg);
  1600. } else {
  1601. $returnValue += (1 / $arg);
  1602. }
  1603. ++$aCount;
  1604. }
  1605. }
  1606. // Return
  1607. if ($aCount > 0) {
  1608. return 1 / ($returnValue / $aCount);
  1609. } else {
  1610. return $returnValue;
  1611. }
  1612. } // function HARMEAN()
  1613. /**
  1614. * HYPGEOMDIST
  1615. *
  1616. * Returns the hypergeometric distribution. HYPGEOMDIST returns the probability of a given number of
  1617. * sample successes, given the sample size, population successes, and population size.
  1618. *
  1619. * @param float $sampleSuccesses Number of successes in the sample
  1620. * @param float $sampleNumber Size of the sample
  1621. * @param float $populationSuccesses Number of successes in the population
  1622. * @param float $populationNumber Population size
  1623. * @return float
  1624. *
  1625. */
  1626. public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) {
  1627. $sampleSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleSuccesses));
  1628. $sampleNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleNumber));
  1629. $populationSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationSuccesses));
  1630. $populationNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationNumber));
  1631. if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) {
  1632. if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
  1633. return PHPExcel_Calculation_Functions::NaN();
  1634. }
  1635. if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) {
  1636. return PHPExcel_Calculation_Functions::NaN();
  1637. }
  1638. if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) {
  1639. return PHPExcel_Calculation_Functions::NaN();
  1640. }
  1641. return PHPExcel_Calculation_MathTrig::COMBIN($populationSuccesses,$sampleSuccesses) *
  1642. PHPExcel_Calculation_MathTrig::COMBIN($populationNumber - $populationSuccesses,$sampleNumber - $sampleSuccesses) /
  1643. PHPExcel_Calculation_MathTrig::COMBIN($populationNumber,$sampleNumber);
  1644. }
  1645. return PHPExcel_Calculation_Functions::VALUE();
  1646. } // function HYPGEOMDIST()
  1647. /**
  1648. * INTERCEPT
  1649. *
  1650. * Calculates the point at which a line will intersect the y-axis by using existing x-values and y-values.
  1651. *
  1652. * @param array of mixed Data Series Y
  1653. * @param array of mixed Data Series X
  1654. * @return float
  1655. */
  1656. public static function INTERCEPT($yValues,$xValues) {
  1657. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1658. return PHPExcel_Calculation_Functions::VALUE();
  1659. }
  1660. $yValueCount = count($yValues);
  1661. $xValueCount = count($xValues);
  1662. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1663. return PHPExcel_Calculation_Functions::NA();
  1664. } elseif ($yValueCount == 1) {
  1665. return PHPExcel_Calculation_Functions::DIV0();
  1666. }
  1667. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  1668. return $bestFitLinear->getIntersect();
  1669. } // function INTERCEPT()
  1670. /**
  1671. * KURT
  1672. *
  1673. * Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness
  1674. * or flatness of a distribution compared with the normal distribution. Positive
  1675. * kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a
  1676. * relatively flat distribution.
  1677. *
  1678. * @param array Data Series
  1679. * @return float
  1680. */
  1681. public static function KURT() {
  1682. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  1683. $mean = self::AVERAGE($aArgs);
  1684. $stdDev = self::STDEV($aArgs);
  1685. if ($stdDev > 0) {
  1686. $count = $summer = 0;
  1687. // Loop through arguments
  1688. foreach ($aArgs as $k => $arg) {
  1689. if ((is_bool($arg)) &&
  1690. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  1691. } else {
  1692. // Is it a numeric value?
  1693. if ((is_numeric($arg)) && (!is_string($arg))) {
  1694. $summer += pow((($arg - $mean) / $stdDev),4) ;
  1695. ++$count;
  1696. }
  1697. }
  1698. }
  1699. // Return
  1700. if ($count > 3) {
  1701. return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1,2) / (($count-2) * ($count-3)));
  1702. }
  1703. }
  1704. return PHPExcel_Calculation_Functions::DIV0();
  1705. } // function KURT()
  1706. /**
  1707. * LARGE
  1708. *
  1709. * Returns the nth largest value in a data set. You can use this function to
  1710. * select a value based on its relative standing.
  1711. *
  1712. * Excel Function:
  1713. * LARGE(value1[,value2[, ...]],entry)
  1714. *
  1715. * @access public
  1716. * @category Statistical Functions
  1717. * @param mixed $arg,... Data values
  1718. * @param int $entry Position (ordered from the largest) in the array or range of data to return
  1719. * @return float
  1720. *
  1721. */
  1722. public static function LARGE() {
  1723. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1724. // Calculate
  1725. $entry = floor(array_pop($aArgs));
  1726. if ((is_numeric($entry)) && (!is_string($entry))) {
  1727. $mArgs = array();
  1728. foreach ($aArgs as $arg) {
  1729. // Is it a numeric value?
  1730. if ((is_numeric($arg)) && (!is_string($arg))) {
  1731. $mArgs[] = $arg;
  1732. }
  1733. }
  1734. $count = self::COUNT($mArgs);
  1735. $entry = floor(--$entry);
  1736. if (($entry < 0) || ($entry >= $count) || ($count == 0)) {
  1737. return PHPExcel_Calculation_Functions::NaN();
  1738. }
  1739. rsort($mArgs);
  1740. return $mArgs[$entry];
  1741. }
  1742. return PHPExcel_Calculation_Functions::VALUE();
  1743. } // function LARGE()
  1744. /**
  1745. * LINEST
  1746. *
  1747. * Calculates the statistics for a line by using the "least squares" method to calculate a straight line that best fits your data,
  1748. * and then returns an array that describes the line.
  1749. *
  1750. * @param array of mixed Data Series Y
  1751. * @param array of mixed Data Series X
  1752. * @param boolean A logical value specifying whether to force the intersect to equal 0.
  1753. * @param boolean A logical value specifying whether to return additional regression statistics.
  1754. * @return array
  1755. */
  1756. public static function LINEST($yValues,$xValues=null,$const=True,$stats=False) {
  1757. $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  1758. $stats = (is_null($stats)) ? False : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats);
  1759. if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues)));
  1760. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1761. return PHPExcel_Calculation_Functions::VALUE();
  1762. }
  1763. $yValueCount = count($yValues);
  1764. $xValueCount = count($xValues);
  1765. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1766. return PHPExcel_Calculation_Functions::NA();
  1767. } elseif ($yValueCount == 1) {
  1768. return 0;
  1769. }
  1770. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const);
  1771. if ($stats) {
  1772. return array( array( $bestFitLinear->getSlope(),
  1773. $bestFitLinear->getSlopeSE(),
  1774. $bestFitLinear->getGoodnessOfFit(),
  1775. $bestFitLinear->getF(),
  1776. $bestFitLinear->getSSRegression(),
  1777. ),
  1778. array( $bestFitLinear->getIntersect(),
  1779. $bestFitLinear->getIntersectSE(),
  1780. $bestFitLinear->getStdevOfResiduals(),
  1781. $bestFitLinear->getDFResiduals(),
  1782. $bestFitLinear->getSSResiduals()
  1783. )
  1784. );
  1785. } else {
  1786. return array( $bestFitLinear->getSlope(),
  1787. $bestFitLinear->getIntersect()
  1788. );
  1789. }
  1790. } // function LINEST()
  1791. /**
  1792. * LOGEST
  1793. *
  1794. * Calculates an exponential curve that best fits the X and Y data series,
  1795. * and then returns an array that describes the line.
  1796. *
  1797. * @param array of mixed Data Series Y
  1798. * @param array of mixed Data Series X
  1799. * @param boolean A logical value specifying whether to force the intersect to equal 0.
  1800. * @param boolean A logical value specifying whether to return additional regression statistics.
  1801. * @return array
  1802. */
  1803. public static function LOGEST($yValues,$xValues=null,$const=True,$stats=False) {
  1804. $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  1805. $stats = (is_null($stats)) ? False : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats);
  1806. if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues)));
  1807. if (!self::_checkTrendArrays($yValues,$xValues)) {
  1808. return PHPExcel_Calculation_Functions::VALUE();
  1809. }
  1810. $yValueCount = count($yValues);
  1811. $xValueCount = count($xValues);
  1812. foreach($yValues as $value) {
  1813. if ($value <= 0.0) {
  1814. return PHPExcel_Calculation_Functions::NaN();
  1815. }
  1816. }
  1817. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  1818. return PHPExcel_Calculation_Functions::NA();
  1819. } elseif ($yValueCount == 1) {
  1820. return 1;
  1821. }
  1822. $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const);
  1823. if ($stats) {
  1824. return array( array( $bestFitExponential->getSlope(),
  1825. $bestFitExponential->getSlopeSE(),
  1826. $bestFitExponential->getGoodnessOfFit(),
  1827. $bestFitExponential->getF(),
  1828. $bestFitExponential->getSSRegression(),
  1829. ),
  1830. array( $bestFitExponential->getIntersect(),
  1831. $bestFitExponential->getIntersectSE(),
  1832. $bestFitExponential->getStdevOfResiduals(),
  1833. $bestFitExponential->getDFResiduals(),
  1834. $bestFitExponential->getSSResiduals()
  1835. )
  1836. );
  1837. } else {
  1838. return array( $bestFitExponential->getSlope(),
  1839. $bestFitExponential->getIntersect()
  1840. );
  1841. }
  1842. } // function LOGEST()
  1843. /**
  1844. * LOGINV
  1845. *
  1846. * Returns the inverse of the normal cumulative distribution
  1847. *
  1848. * @param float $value
  1849. * @return float
  1850. *
  1851. * @todo Try implementing P J Acklam's refinement algorithm for greater
  1852. * accuracy if I can get my head round the mathematics
  1853. * (as described at) http://home.online.no/~pjacklam/notes/invnorm/
  1854. */
  1855. public static function LOGINV($probability, $mean, $stdDev) {
  1856. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  1857. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  1858. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  1859. if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  1860. if (($probability < 0) || ($probability > 1) || ($stdDev <= 0)) {
  1861. return PHPExcel_Calculation_Functions::NaN();
  1862. }
  1863. return exp($mean + $stdDev * self::NORMSINV($probability));
  1864. }
  1865. return PHPExcel_Calculation_Functions::VALUE();
  1866. } // function LOGINV()
  1867. /**
  1868. * LOGNORMDIST
  1869. *
  1870. * Returns the cumulative lognormal distribution of x, where ln(x) is normally distributed
  1871. * with parameters mean and standard_dev.
  1872. *
  1873. * @param float $value
  1874. * @return float
  1875. */
  1876. public static function LOGNORMDIST($value, $mean, $stdDev) {
  1877. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  1878. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  1879. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  1880. if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  1881. if (($value <= 0) || ($stdDev <= 0)) {
  1882. return PHPExcel_Calculation_Functions::NaN();
  1883. }
  1884. return self::NORMSDIST((log($value) - $mean) / $stdDev);
  1885. }
  1886. return PHPExcel_Calculation_Functions::VALUE();
  1887. } // function LOGNORMDIST()
  1888. /**
  1889. * MAX
  1890. *
  1891. * MAX returns the value of the element of the values passed that has the highest value,
  1892. * with negative numbers considered smaller than positive numbers.
  1893. *
  1894. * Excel Function:
  1895. * MAX(value1[,value2[, ...]])
  1896. *
  1897. * @access public
  1898. * @category Statistical Functions
  1899. * @param mixed $arg,... Data values
  1900. * @return float
  1901. */
  1902. public static function MAX() {
  1903. // Return value
  1904. $returnValue = null;
  1905. // Loop through arguments
  1906. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1907. foreach ($aArgs as $arg) {
  1908. // Is it a numeric value?
  1909. if ((is_numeric($arg)) && (!is_string($arg))) {
  1910. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  1911. $returnValue = $arg;
  1912. }
  1913. }
  1914. }
  1915. // Return
  1916. if(is_null($returnValue)) {
  1917. return 0;
  1918. }
  1919. return $returnValue;
  1920. } // function MAX()
  1921. /**
  1922. * MAXA
  1923. *
  1924. * Returns the greatest value in a list of arguments, including numbers, text, and logical values
  1925. *
  1926. * Excel Function:
  1927. * MAXA(value1[,value2[, ...]])
  1928. *
  1929. * @access public
  1930. * @category Statistical Functions
  1931. * @param mixed $arg,... Data values
  1932. * @return float
  1933. */
  1934. public static function MAXA() {
  1935. // Return value
  1936. $returnValue = null;
  1937. // Loop through arguments
  1938. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1939. foreach ($aArgs as $arg) {
  1940. // Is it a numeric value?
  1941. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  1942. if (is_bool($arg)) {
  1943. $arg = (integer) $arg;
  1944. } elseif (is_string($arg)) {
  1945. $arg = 0;
  1946. }
  1947. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  1948. $returnValue = $arg;
  1949. }
  1950. }
  1951. }
  1952. // Return
  1953. if(is_null($returnValue)) {
  1954. return 0;
  1955. }
  1956. return $returnValue;
  1957. } // function MAXA()
  1958. /**
  1959. * MAXIF
  1960. *
  1961. * Counts the maximum value within a range of cells that contain numbers within the list of arguments
  1962. *
  1963. * Excel Function:
  1964. * MAXIF(value1[,value2[, ...]],condition)
  1965. *
  1966. * @access public
  1967. * @category Mathematical and Trigonometric Functions
  1968. * @param mixed $arg,... Data values
  1969. * @param string $condition The criteria that defines which cells will be checked.
  1970. * @return float
  1971. */
  1972. public static function MAXIF($aArgs,$condition,$sumArgs = array()) {
  1973. // Return value
  1974. $returnValue = null;
  1975. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  1976. $sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  1977. if (empty($sumArgs)) {
  1978. $sumArgs = $aArgs;
  1979. }
  1980. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  1981. // Loop through arguments
  1982. foreach ($aArgs as $key => $arg) {
  1983. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  1984. $testCondition = '='.$arg.$condition;
  1985. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1986. if ((is_null($returnValue)) || ($arg > $returnValue)) {
  1987. $returnValue = $arg;
  1988. }
  1989. }
  1990. }
  1991. // Return
  1992. return $returnValue;
  1993. } // function MAXIF()
  1994. /**
  1995. * MEDIAN
  1996. *
  1997. * Returns the median of the given numbers. The median is the number in the middle of a set of numbers.
  1998. *
  1999. * Excel Function:
  2000. * MEDIAN(value1[,value2[, ...]])
  2001. *
  2002. * @access public
  2003. * @category Statistical Functions
  2004. * @param mixed $arg,... Data values
  2005. * @return float
  2006. */
  2007. public static function MEDIAN() {
  2008. // Return value
  2009. $returnValue = PHPExcel_Calculation_Functions::NaN();
  2010. $mArgs = array();
  2011. // Loop through arguments
  2012. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2013. foreach ($aArgs as $arg) {
  2014. // Is it a numeric value?
  2015. if ((is_numeric($arg)) && (!is_string($arg))) {
  2016. $mArgs[] = $arg;
  2017. }
  2018. }
  2019. $mValueCount = count($mArgs);
  2020. if ($mValueCount > 0) {
  2021. sort($mArgs,SORT_NUMERIC);
  2022. $mValueCount = $mValueCount / 2;
  2023. if ($mValueCount == floor($mValueCount)) {
  2024. $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2;
  2025. } else {
  2026. $mValueCount == floor($mValueCount);
  2027. $returnValue = $mArgs[$mValueCount];
  2028. }
  2029. }
  2030. // Return
  2031. return $returnValue;
  2032. } // function MEDIAN()
  2033. /**
  2034. * MIN
  2035. *
  2036. * MIN returns the value of the element of the values passed that has the smallest value,
  2037. * with negative numbers considered smaller than positive numbers.
  2038. *
  2039. * Excel Function:
  2040. * MIN(value1[,value2[, ...]])
  2041. *
  2042. * @access public
  2043. * @category Statistical Functions
  2044. * @param mixed $arg,... Data values
  2045. * @return float
  2046. */
  2047. public static function MIN() {
  2048. // Return value
  2049. $returnValue = null;
  2050. // Loop through arguments
  2051. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2052. foreach ($aArgs as $arg) {
  2053. // Is it a numeric value?
  2054. if ((is_numeric($arg)) && (!is_string($arg))) {
  2055. if ((is_null($returnValue)) || ($arg < $returnValue)) {
  2056. $returnValue = $arg;
  2057. }
  2058. }
  2059. }
  2060. // Return
  2061. if(is_null($returnValue)) {
  2062. return 0;
  2063. }
  2064. return $returnValue;
  2065. } // function MIN()
  2066. /**
  2067. * MINA
  2068. *
  2069. * Returns the smallest value in a list of arguments, including numbers, text, and logical values
  2070. *
  2071. * Excel Function:
  2072. * MINA(value1[,value2[, ...]])
  2073. *
  2074. * @access public
  2075. * @category Statistical Functions
  2076. * @param mixed $arg,... Data values
  2077. * @return float
  2078. */
  2079. public static function MINA() {
  2080. // Return value
  2081. $returnValue = null;
  2082. // Loop through arguments
  2083. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2084. foreach ($aArgs as $arg) {
  2085. // Is it a numeric value?
  2086. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
  2087. if (is_bool($arg)) {
  2088. $arg = (integer) $arg;
  2089. } elseif (is_string($arg)) {
  2090. $arg = 0;
  2091. }
  2092. if ((is_null($returnValue)) || ($arg < $returnValue)) {
  2093. $returnValue = $arg;
  2094. }
  2095. }
  2096. }
  2097. // Return
  2098. if(is_null($returnValue)) {
  2099. return 0;
  2100. }
  2101. return $returnValue;
  2102. } // function MINA()
  2103. /**
  2104. * MINIF
  2105. *
  2106. * Returns the minimum value within a range of cells that contain numbers within the list of arguments
  2107. *
  2108. * Excel Function:
  2109. * MINIF(value1[,value2[, ...]],condition)
  2110. *
  2111. * @access public
  2112. * @category Mathematical and Trigonometric Functions
  2113. * @param mixed $arg,... Data values
  2114. * @param string $condition The criteria that defines which cells will be checked.
  2115. * @return float
  2116. */
  2117. public static function MINIF($aArgs,$condition,$sumArgs = array()) {
  2118. // Return value
  2119. $returnValue = null;
  2120. $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  2121. $sumArgs = PHPExcel_Calculation_Functions::flattenArray($sumArgs);
  2122. if (empty($sumArgs)) {
  2123. $sumArgs = $aArgs;
  2124. }
  2125. $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);
  2126. // Loop through arguments
  2127. foreach ($aArgs as $key => $arg) {
  2128. if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); }
  2129. $testCondition = '='.$arg.$condition;
  2130. if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  2131. if ((is_null($returnValue)) || ($arg < $returnValue)) {
  2132. $returnValue = $arg;
  2133. }
  2134. }
  2135. }
  2136. // Return
  2137. return $returnValue;
  2138. } // function MINIF()
  2139. //
  2140. // Special variant of array_count_values that isn't limited to strings and integers,
  2141. // but can work with floating point numbers as values
  2142. //
  2143. private static function _modeCalc($data) {
  2144. $frequencyArray = array();
  2145. foreach($data as $datum) {
  2146. $found = False;
  2147. foreach($frequencyArray as $key => $value) {
  2148. if ((string) $value['value'] == (string) $datum) {
  2149. ++$frequencyArray[$key]['frequency'];
  2150. $found = True;
  2151. break;
  2152. }
  2153. }
  2154. if (!$found) {
  2155. $frequencyArray[] = array('value' => $datum,
  2156. 'frequency' => 1 );
  2157. }
  2158. }
  2159. foreach($frequencyArray as $key => $value) {
  2160. $frequencyList[$key] = $value['frequency'];
  2161. $valueList[$key] = $value['value'];
  2162. }
  2163. array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray);
  2164. if ($frequencyArray[0]['frequency'] == 1) {
  2165. return PHPExcel_Calculation_Functions::NA();
  2166. }
  2167. return $frequencyArray[0]['value'];
  2168. } // function _modeCalc()
  2169. /**
  2170. * MODE
  2171. *
  2172. * Returns the most frequently occurring, or repetitive, value in an array or range of data
  2173. *
  2174. * Excel Function:
  2175. * MODE(value1[,value2[, ...]])
  2176. *
  2177. * @access public
  2178. * @category Statistical Functions
  2179. * @param mixed $arg,... Data values
  2180. * @return float
  2181. */
  2182. public static function MODE() {
  2183. // Return value
  2184. $returnValue = PHPExcel_Calculation_Functions::NA();
  2185. // Loop through arguments
  2186. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2187. $mArgs = array();
  2188. foreach ($aArgs as $arg) {
  2189. // Is it a numeric value?
  2190. if ((is_numeric($arg)) && (!is_string($arg))) {
  2191. $mArgs[] = $arg;
  2192. }
  2193. }
  2194. if (!empty($mArgs)) {
  2195. return self::_modeCalc($mArgs);
  2196. }
  2197. // Return
  2198. return $returnValue;
  2199. } // function MODE()
  2200. /**
  2201. * NEGBINOMDIST
  2202. *
  2203. * Returns the negative binomial distribution. NEGBINOMDIST returns the probability that
  2204. * there will be number_f failures before the number_s-th success, when the constant
  2205. * probability of a success is probability_s. This function is similar to the binomial
  2206. * distribution, except that the number of successes is fixed, and the number of trials is
  2207. * variable. Like the binomial, trials are assumed to be independent.
  2208. *
  2209. * @param float $failures Number of Failures
  2210. * @param float $successes Threshold number of Successes
  2211. * @param float $probability Probability of success on each trial
  2212. * @return float
  2213. *
  2214. */
  2215. public static function NEGBINOMDIST($failures, $successes, $probability) {
  2216. $failures = floor(PHPExcel_Calculation_Functions::flattenSingleValue($failures));
  2217. $successes = floor(PHPExcel_Calculation_Functions::flattenSingleValue($successes));
  2218. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2219. if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) {
  2220. if (($failures < 0) || ($successes < 1)) {
  2221. return PHPExcel_Calculation_Functions::NaN();
  2222. }
  2223. if (($probability < 0) || ($probability > 1)) {
  2224. return PHPExcel_Calculation_Functions::NaN();
  2225. }
  2226. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  2227. if (($failures + $successes - 1) <= 0) {
  2228. return PHPExcel_Calculation_Functions::NaN();
  2229. }
  2230. }
  2231. return (PHPExcel_Calculation_MathTrig::COMBIN($failures + $successes - 1,$successes - 1)) * (pow($probability,$successes)) * (pow(1 - $probability,$failures)) ;
  2232. }
  2233. return PHPExcel_Calculation_Functions::VALUE();
  2234. } // function NEGBINOMDIST()
  2235. /**
  2236. * NORMDIST
  2237. *
  2238. * Returns the normal distribution for the specified mean and standard deviation. This
  2239. * function has a very wide range of applications in statistics, including hypothesis
  2240. * testing.
  2241. *
  2242. * @param float $value
  2243. * @param float $mean Mean Value
  2244. * @param float $stdDev Standard Deviation
  2245. * @param boolean $cumulative
  2246. * @return float
  2247. *
  2248. */
  2249. public static function NORMDIST($value, $mean, $stdDev, $cumulative) {
  2250. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2251. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2252. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2253. if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2254. if ($stdDev < 0) {
  2255. return PHPExcel_Calculation_Functions::NaN();
  2256. }
  2257. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  2258. if ($cumulative) {
  2259. return 0.5 * (1 + PHPExcel_Calculation_Engineering::_erfVal(($value - $mean) / ($stdDev * sqrt(2))));
  2260. } else {
  2261. return (1 / (SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean,2) / (2 * ($stdDev * $stdDev))));
  2262. }
  2263. }
  2264. }
  2265. return PHPExcel_Calculation_Functions::VALUE();
  2266. } // function NORMDIST()
  2267. /**
  2268. * NORMINV
  2269. *
  2270. * Returns the inverse of the normal cumulative distribution for the specified mean and standard deviation.
  2271. *
  2272. * @param float $value
  2273. * @param float $mean Mean Value
  2274. * @param float $stdDev Standard Deviation
  2275. * @return float
  2276. *
  2277. */
  2278. public static function NORMINV($probability,$mean,$stdDev) {
  2279. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2280. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2281. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2282. if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2283. if (($probability < 0) || ($probability > 1)) {
  2284. return PHPExcel_Calculation_Functions::NaN();
  2285. }
  2286. if ($stdDev < 0) {
  2287. return PHPExcel_Calculation_Functions::NaN();
  2288. }
  2289. return (self::_inverse_ncdf($probability) * $stdDev) + $mean;
  2290. }
  2291. return PHPExcel_Calculation_Functions::VALUE();
  2292. } // function NORMINV()
  2293. /**
  2294. * NORMSDIST
  2295. *
  2296. * Returns the standard normal cumulative distribution function. The distribution has
  2297. * a mean of 0 (zero) and a standard deviation of one. Use this function in place of a
  2298. * table of standard normal curve areas.
  2299. *
  2300. * @param float $value
  2301. * @return float
  2302. */
  2303. public static function NORMSDIST($value) {
  2304. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2305. return self::NORMDIST($value, 0, 1, True);
  2306. } // function NORMSDIST()
  2307. /**
  2308. * NORMSINV
  2309. *
  2310. * Returns the inverse of the standard normal cumulative distribution
  2311. *
  2312. * @param float $value
  2313. * @return float
  2314. */
  2315. public static function NORMSINV($value) {
  2316. return self::NORMINV($value, 0, 1);
  2317. } // function NORMSINV()
  2318. /**
  2319. * PERCENTILE
  2320. *
  2321. * Returns the nth percentile of values in a range..
  2322. *
  2323. * Excel Function:
  2324. * PERCENTILE(value1[,value2[, ...]],entry)
  2325. *
  2326. * @access public
  2327. * @category Statistical Functions
  2328. * @param mixed $arg,... Data values
  2329. * @param float $entry Percentile value in the range 0..1, inclusive.
  2330. * @return float
  2331. */
  2332. public static function PERCENTILE() {
  2333. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2334. // Calculate
  2335. $entry = array_pop($aArgs);
  2336. if ((is_numeric($entry)) && (!is_string($entry))) {
  2337. if (($entry < 0) || ($entry > 1)) {
  2338. return PHPExcel_Calculation_Functions::NaN();
  2339. }
  2340. $mArgs = array();
  2341. foreach ($aArgs as $arg) {
  2342. // Is it a numeric value?
  2343. if ((is_numeric($arg)) && (!is_string($arg))) {
  2344. $mArgs[] = $arg;
  2345. }
  2346. }
  2347. $mValueCount = count($mArgs);
  2348. if ($mValueCount > 0) {
  2349. sort($mArgs);
  2350. $count = self::COUNT($mArgs);
  2351. $index = $entry * ($count-1);
  2352. $iBase = floor($index);
  2353. if ($index == $iBase) {
  2354. return $mArgs[$index];
  2355. } else {
  2356. $iNext = $iBase + 1;
  2357. $iProportion = $index - $iBase;
  2358. return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ;
  2359. }
  2360. }
  2361. }
  2362. return PHPExcel_Calculation_Functions::VALUE();
  2363. } // function PERCENTILE()
  2364. /**
  2365. * PERCENTRANK
  2366. *
  2367. * Returns the rank of a value in a data set as a percentage of the data set.
  2368. *
  2369. * @param array of number An array of, or a reference to, a list of numbers.
  2370. * @param number The number whose rank you want to find.
  2371. * @param number The number of significant digits for the returned percentage value.
  2372. * @return float
  2373. */
  2374. public static function PERCENTRANK($valueSet,$value,$significance=3) {
  2375. $valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet);
  2376. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2377. $significance = (is_null($significance)) ? 3 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($significance);
  2378. foreach($valueSet as $key => $valueEntry) {
  2379. if (!is_numeric($valueEntry)) {
  2380. unset($valueSet[$key]);
  2381. }
  2382. }
  2383. sort($valueSet,SORT_NUMERIC);
  2384. $valueCount = count($valueSet);
  2385. if ($valueCount == 0) {
  2386. return PHPExcel_Calculation_Functions::NaN();
  2387. }
  2388. $valueAdjustor = $valueCount - 1;
  2389. if (($value < $valueSet[0]) || ($value > $valueSet[$valueAdjustor])) {
  2390. return PHPExcel_Calculation_Functions::NA();
  2391. }
  2392. $pos = array_search($value,$valueSet);
  2393. if ($pos === False) {
  2394. $pos = 0;
  2395. $testValue = $valueSet[0];
  2396. while ($testValue < $value) {
  2397. $testValue = $valueSet[++$pos];
  2398. }
  2399. --$pos;
  2400. $pos += (($value - $valueSet[$pos]) / ($testValue - $valueSet[$pos]));
  2401. }
  2402. return round($pos / $valueAdjustor,$significance);
  2403. } // function PERCENTRANK()
  2404. /**
  2405. * PERMUT
  2406. *
  2407. * Returns the number of permutations for a given number of objects that can be
  2408. * selected from number objects. A permutation is any set or subset of objects or
  2409. * events where internal order is significant. Permutations are different from
  2410. * combinations, for which the internal order is not significant. Use this function
  2411. * for lottery-style probability calculations.
  2412. *
  2413. * @param int $numObjs Number of different objects
  2414. * @param int $numInSet Number of objects in each permutation
  2415. * @return int Number of permutations
  2416. */
  2417. public static function PERMUT($numObjs,$numInSet) {
  2418. $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs);
  2419. $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet);
  2420. if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
  2421. $numInSet = floor($numInSet);
  2422. if ($numObjs < $numInSet) {
  2423. return PHPExcel_Calculation_Functions::NaN();
  2424. }
  2425. return round(PHPExcel_Calculation_MathTrig::FACT($numObjs) / PHPExcel_Calculation_MathTrig::FACT($numObjs - $numInSet));
  2426. }
  2427. return PHPExcel_Calculation_Functions::VALUE();
  2428. } // function PERMUT()
  2429. /**
  2430. * POISSON
  2431. *
  2432. * Returns the Poisson distribution. A common application of the Poisson distribution
  2433. * is predicting the number of events over a specific time, such as the number of
  2434. * cars arriving at a toll plaza in 1 minute.
  2435. *
  2436. * @param float $value
  2437. * @param float $mean Mean Value
  2438. * @param boolean $cumulative
  2439. * @return float
  2440. *
  2441. */
  2442. public static function POISSON($value, $mean, $cumulative) {
  2443. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2444. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2445. if ((is_numeric($value)) && (is_numeric($mean))) {
  2446. if (($value <= 0) || ($mean <= 0)) {
  2447. return PHPExcel_Calculation_Functions::NaN();
  2448. }
  2449. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  2450. if ($cumulative) {
  2451. $summer = 0;
  2452. for ($i = 0; $i <= floor($value); ++$i) {
  2453. $summer += pow($mean,$i) / PHPExcel_Calculation_MathTrig::FACT($i);
  2454. }
  2455. return exp(0-$mean) * $summer;
  2456. } else {
  2457. return (exp(0-$mean) * pow($mean,$value)) / PHPExcel_Calculation_MathTrig::FACT($value);
  2458. }
  2459. }
  2460. }
  2461. return PHPExcel_Calculation_Functions::VALUE();
  2462. } // function POISSON()
  2463. /**
  2464. * QUARTILE
  2465. *
  2466. * Returns the quartile of a data set.
  2467. *
  2468. * Excel Function:
  2469. * QUARTILE(value1[,value2[, ...]],entry)
  2470. *
  2471. * @access public
  2472. * @category Statistical Functions
  2473. * @param mixed $arg,... Data values
  2474. * @param int $entry Quartile value in the range 1..3, inclusive.
  2475. * @return float
  2476. */
  2477. public static function QUARTILE() {
  2478. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2479. // Calculate
  2480. $entry = floor(array_pop($aArgs));
  2481. if ((is_numeric($entry)) && (!is_string($entry))) {
  2482. $entry /= 4;
  2483. if (($entry < 0) || ($entry > 1)) {
  2484. return PHPExcel_Calculation_Functions::NaN();
  2485. }
  2486. return self::PERCENTILE($aArgs,$entry);
  2487. }
  2488. return PHPExcel_Calculation_Functions::VALUE();
  2489. } // function QUARTILE()
  2490. /**
  2491. * RANK
  2492. *
  2493. * Returns the rank of a number in a list of numbers.
  2494. *
  2495. * @param number The number whose rank you want to find.
  2496. * @param array of number An array of, or a reference to, a list of numbers.
  2497. * @param mixed Order to sort the values in the value set
  2498. * @return float
  2499. */
  2500. public static function RANK($value,$valueSet,$order=0) {
  2501. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2502. $valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet);
  2503. $order = (is_null($order)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($order);
  2504. foreach($valueSet as $key => $valueEntry) {
  2505. if (!is_numeric($valueEntry)) {
  2506. unset($valueSet[$key]);
  2507. }
  2508. }
  2509. if ($order == 0) {
  2510. rsort($valueSet,SORT_NUMERIC);
  2511. } else {
  2512. sort($valueSet,SORT_NUMERIC);
  2513. }
  2514. $pos = array_search($value,$valueSet);
  2515. if ($pos === False) {
  2516. return PHPExcel_Calculation_Functions::NA();
  2517. }
  2518. return ++$pos;
  2519. } // function RANK()
  2520. /**
  2521. * RSQ
  2522. *
  2523. * Returns the square of the Pearson product moment correlation coefficient through data points in known_y's and known_x's.
  2524. *
  2525. * @param array of mixed Data Series Y
  2526. * @param array of mixed Data Series X
  2527. * @return float
  2528. */
  2529. public static function RSQ($yValues,$xValues) {
  2530. if (!self::_checkTrendArrays($yValues,$xValues)) {
  2531. return PHPExcel_Calculation_Functions::VALUE();
  2532. }
  2533. $yValueCount = count($yValues);
  2534. $xValueCount = count($xValues);
  2535. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  2536. return PHPExcel_Calculation_Functions::NA();
  2537. } elseif ($yValueCount == 1) {
  2538. return PHPExcel_Calculation_Functions::DIV0();
  2539. }
  2540. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  2541. return $bestFitLinear->getGoodnessOfFit();
  2542. } // function RSQ()
  2543. /**
  2544. * SKEW
  2545. *
  2546. * Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry
  2547. * of a distribution around its mean. Positive skewness indicates a distribution with an
  2548. * asymmetric tail extending toward more positive values. Negative skewness indicates a
  2549. * distribution with an asymmetric tail extending toward more negative values.
  2550. *
  2551. * @param array Data Series
  2552. * @return float
  2553. */
  2554. public static function SKEW() {
  2555. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  2556. $mean = self::AVERAGE($aArgs);
  2557. $stdDev = self::STDEV($aArgs);
  2558. $count = $summer = 0;
  2559. // Loop through arguments
  2560. foreach ($aArgs as $k => $arg) {
  2561. if ((is_bool($arg)) &&
  2562. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  2563. } else {
  2564. // Is it a numeric value?
  2565. if ((is_numeric($arg)) && (!is_string($arg))) {
  2566. $summer += pow((($arg - $mean) / $stdDev),3) ;
  2567. ++$count;
  2568. }
  2569. }
  2570. }
  2571. // Return
  2572. if ($count > 2) {
  2573. return $summer * ($count / (($count-1) * ($count-2)));
  2574. }
  2575. return PHPExcel_Calculation_Functions::DIV0();
  2576. } // function SKEW()
  2577. /**
  2578. * SLOPE
  2579. *
  2580. * Returns the slope of the linear regression line through data points in known_y's and known_x's.
  2581. *
  2582. * @param array of mixed Data Series Y
  2583. * @param array of mixed Data Series X
  2584. * @return float
  2585. */
  2586. public static function SLOPE($yValues,$xValues) {
  2587. if (!self::_checkTrendArrays($yValues,$xValues)) {
  2588. return PHPExcel_Calculation_Functions::VALUE();
  2589. }
  2590. $yValueCount = count($yValues);
  2591. $xValueCount = count($xValues);
  2592. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  2593. return PHPExcel_Calculation_Functions::NA();
  2594. } elseif ($yValueCount == 1) {
  2595. return PHPExcel_Calculation_Functions::DIV0();
  2596. }
  2597. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  2598. return $bestFitLinear->getSlope();
  2599. } // function SLOPE()
  2600. /**
  2601. * SMALL
  2602. *
  2603. * Returns the nth smallest value in a data set. You can use this function to
  2604. * select a value based on its relative standing.
  2605. *
  2606. * Excel Function:
  2607. * SMALL(value1[,value2[, ...]],entry)
  2608. *
  2609. * @access public
  2610. * @category Statistical Functions
  2611. * @param mixed $arg,... Data values
  2612. * @param int $entry Position (ordered from the smallest) in the array or range of data to return
  2613. * @return float
  2614. */
  2615. public static function SMALL() {
  2616. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  2617. // Calculate
  2618. $entry = array_pop($aArgs);
  2619. if ((is_numeric($entry)) && (!is_string($entry))) {
  2620. $mArgs = array();
  2621. foreach ($aArgs as $arg) {
  2622. // Is it a numeric value?
  2623. if ((is_numeric($arg)) && (!is_string($arg))) {
  2624. $mArgs[] = $arg;
  2625. }
  2626. }
  2627. $count = self::COUNT($mArgs);
  2628. $entry = floor(--$entry);
  2629. if (($entry < 0) || ($entry >= $count) || ($count == 0)) {
  2630. return PHPExcel_Calculation_Functions::NaN();
  2631. }
  2632. sort($mArgs);
  2633. return $mArgs[$entry];
  2634. }
  2635. return PHPExcel_Calculation_Functions::VALUE();
  2636. } // function SMALL()
  2637. /**
  2638. * STANDARDIZE
  2639. *
  2640. * Returns a normalized value from a distribution characterized by mean and standard_dev.
  2641. *
  2642. * @param float $value Value to normalize
  2643. * @param float $mean Mean Value
  2644. * @param float $stdDev Standard Deviation
  2645. * @return float Standardized value
  2646. */
  2647. public static function STANDARDIZE($value,$mean,$stdDev) {
  2648. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2649. $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean);
  2650. $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev);
  2651. if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
  2652. if ($stdDev <= 0) {
  2653. return PHPExcel_Calculation_Functions::NaN();
  2654. }
  2655. return ($value - $mean) / $stdDev ;
  2656. }
  2657. return PHPExcel_Calculation_Functions::VALUE();
  2658. } // function STANDARDIZE()
  2659. /**
  2660. * STDEV
  2661. *
  2662. * Estimates standard deviation based on a sample. The standard deviation is a measure of how
  2663. * widely values are dispersed from the average value (the mean).
  2664. *
  2665. * Excel Function:
  2666. * STDEV(value1[,value2[, ...]])
  2667. *
  2668. * @access public
  2669. * @category Statistical Functions
  2670. * @param mixed $arg,... Data values
  2671. * @return float
  2672. */
  2673. public static function STDEV() {
  2674. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  2675. // Return value
  2676. $returnValue = null;
  2677. $aMean = self::AVERAGE($aArgs);
  2678. if (!is_null($aMean)) {
  2679. $aCount = -1;
  2680. foreach ($aArgs as $k => $arg) {
  2681. if ((is_bool($arg)) &&
  2682. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  2683. $arg = (integer) $arg;
  2684. }
  2685. // Is it a numeric value?
  2686. if ((is_numeric($arg)) && (!is_string($arg))) {
  2687. if (is_null($returnValue)) {
  2688. $returnValue = pow(($arg - $aMean),2);
  2689. } else {
  2690. $returnValue += pow(($arg - $aMean),2);
  2691. }
  2692. ++$aCount;
  2693. }
  2694. }
  2695. // Return
  2696. if (($aCount > 0) && ($returnValue >= 0)) {
  2697. return sqrt($returnValue / $aCount);
  2698. }
  2699. }
  2700. return PHPExcel_Calculation_Functions::DIV0();
  2701. } // function STDEV()
  2702. /**
  2703. * STDEVA
  2704. *
  2705. * Estimates standard deviation based on a sample, including numbers, text, and logical values
  2706. *
  2707. * Excel Function:
  2708. * STDEVA(value1[,value2[, ...]])
  2709. *
  2710. * @access public
  2711. * @category Statistical Functions
  2712. * @param mixed $arg,... Data values
  2713. * @return float
  2714. */
  2715. public static function STDEVA() {
  2716. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  2717. // Return value
  2718. $returnValue = null;
  2719. $aMean = self::AVERAGEA($aArgs);
  2720. if (!is_null($aMean)) {
  2721. $aCount = -1;
  2722. foreach ($aArgs as $k => $arg) {
  2723. if ((is_bool($arg)) &&
  2724. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  2725. } else {
  2726. // Is it a numeric value?
  2727. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  2728. if (is_bool($arg)) {
  2729. $arg = (integer) $arg;
  2730. } elseif (is_string($arg)) {
  2731. $arg = 0;
  2732. }
  2733. if (is_null($returnValue)) {
  2734. $returnValue = pow(($arg - $aMean),2);
  2735. } else {
  2736. $returnValue += pow(($arg - $aMean),2);
  2737. }
  2738. ++$aCount;
  2739. }
  2740. }
  2741. }
  2742. // Return
  2743. if (($aCount > 0) && ($returnValue >= 0)) {
  2744. return sqrt($returnValue / $aCount);
  2745. }
  2746. }
  2747. return PHPExcel_Calculation_Functions::DIV0();
  2748. } // function STDEVA()
  2749. /**
  2750. * STDEVP
  2751. *
  2752. * Calculates standard deviation based on the entire population
  2753. *
  2754. * Excel Function:
  2755. * STDEVP(value1[,value2[, ...]])
  2756. *
  2757. * @access public
  2758. * @category Statistical Functions
  2759. * @param mixed $arg,... Data values
  2760. * @return float
  2761. */
  2762. public static function STDEVP() {
  2763. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  2764. // Return value
  2765. $returnValue = null;
  2766. $aMean = self::AVERAGE($aArgs);
  2767. if (!is_null($aMean)) {
  2768. $aCount = 0;
  2769. foreach ($aArgs as $k => $arg) {
  2770. if ((is_bool($arg)) &&
  2771. ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) {
  2772. $arg = (integer) $arg;
  2773. }
  2774. // Is it a numeric value?
  2775. if ((is_numeric($arg)) && (!is_string($arg))) {
  2776. if (is_null($returnValue)) {
  2777. $returnValue = pow(($arg - $aMean),2);
  2778. } else {
  2779. $returnValue += pow(($arg - $aMean),2);
  2780. }
  2781. ++$aCount;
  2782. }
  2783. }
  2784. // Return
  2785. if (($aCount > 0) && ($returnValue >= 0)) {
  2786. return sqrt($returnValue / $aCount);
  2787. }
  2788. }
  2789. return PHPExcel_Calculation_Functions::DIV0();
  2790. } // function STDEVP()
  2791. /**
  2792. * STDEVPA
  2793. *
  2794. * Calculates standard deviation based on the entire population, including numbers, text, and logical values
  2795. *
  2796. * Excel Function:
  2797. * STDEVPA(value1[,value2[, ...]])
  2798. *
  2799. * @access public
  2800. * @category Statistical Functions
  2801. * @param mixed $arg,... Data values
  2802. * @return float
  2803. */
  2804. public static function STDEVPA() {
  2805. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  2806. // Return value
  2807. $returnValue = null;
  2808. $aMean = self::AVERAGEA($aArgs);
  2809. if (!is_null($aMean)) {
  2810. $aCount = 0;
  2811. foreach ($aArgs as $k => $arg) {
  2812. if ((is_bool($arg)) &&
  2813. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  2814. } else {
  2815. // Is it a numeric value?
  2816. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  2817. if (is_bool($arg)) {
  2818. $arg = (integer) $arg;
  2819. } elseif (is_string($arg)) {
  2820. $arg = 0;
  2821. }
  2822. if (is_null($returnValue)) {
  2823. $returnValue = pow(($arg - $aMean),2);
  2824. } else {
  2825. $returnValue += pow(($arg - $aMean),2);
  2826. }
  2827. ++$aCount;
  2828. }
  2829. }
  2830. }
  2831. // Return
  2832. if (($aCount > 0) && ($returnValue >= 0)) {
  2833. return sqrt($returnValue / $aCount);
  2834. }
  2835. }
  2836. return PHPExcel_Calculation_Functions::DIV0();
  2837. } // function STDEVPA()
  2838. /**
  2839. * STEYX
  2840. *
  2841. * Returns the standard error of the predicted y-value for each x in the regression.
  2842. *
  2843. * @param array of mixed Data Series Y
  2844. * @param array of mixed Data Series X
  2845. * @return float
  2846. */
  2847. public static function STEYX($yValues,$xValues) {
  2848. if (!self::_checkTrendArrays($yValues,$xValues)) {
  2849. return PHPExcel_Calculation_Functions::VALUE();
  2850. }
  2851. $yValueCount = count($yValues);
  2852. $xValueCount = count($xValues);
  2853. if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
  2854. return PHPExcel_Calculation_Functions::NA();
  2855. } elseif ($yValueCount == 1) {
  2856. return PHPExcel_Calculation_Functions::DIV0();
  2857. }
  2858. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues);
  2859. return $bestFitLinear->getStdevOfResiduals();
  2860. } // function STEYX()
  2861. /**
  2862. * TDIST
  2863. *
  2864. * Returns the probability of Student's T distribution.
  2865. *
  2866. * @param float $value Value for the function
  2867. * @param float $degrees degrees of freedom
  2868. * @param float $tails number of tails (1 or 2)
  2869. * @return float
  2870. */
  2871. public static function TDIST($value, $degrees, $tails) {
  2872. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2873. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  2874. $tails = floor(PHPExcel_Calculation_Functions::flattenSingleValue($tails));
  2875. if ((is_numeric($value)) && (is_numeric($degrees)) && (is_numeric($tails))) {
  2876. if (($value < 0) || ($degrees < 1) || ($tails < 1) || ($tails > 2)) {
  2877. return PHPExcel_Calculation_Functions::NaN();
  2878. }
  2879. // tdist, which finds the probability that corresponds to a given value
  2880. // of t with k degrees of freedom. This algorithm is translated from a
  2881. // pascal function on p81 of "Statistical Computing in Pascal" by D
  2882. // Cooke, A H Craven & G M Clark (1985: Edward Arnold (Pubs.) Ltd:
  2883. // London). The above Pascal algorithm is itself a translation of the
  2884. // fortran algoritm "AS 3" by B E Cooper of the Atlas Computer
  2885. // Laboratory as reported in (among other places) "Applied Statistics
  2886. // Algorithms", editied by P Griffiths and I D Hill (1985; Ellis
  2887. // Horwood Ltd.; W. Sussex, England).
  2888. $tterm = $degrees;
  2889. $ttheta = atan2($value,sqrt($tterm));
  2890. $tc = cos($ttheta);
  2891. $ts = sin($ttheta);
  2892. $tsum = 0;
  2893. if (($degrees % 2) == 1) {
  2894. $ti = 3;
  2895. $tterm = $tc;
  2896. } else {
  2897. $ti = 2;
  2898. $tterm = 1;
  2899. }
  2900. $tsum = $tterm;
  2901. while ($ti < $degrees) {
  2902. $tterm *= $tc * $tc * ($ti - 1) / $ti;
  2903. $tsum += $tterm;
  2904. $ti += 2;
  2905. }
  2906. $tsum *= $ts;
  2907. if (($degrees % 2) == 1) { $tsum = M_2DIVPI * ($tsum + $ttheta); }
  2908. $tValue = 0.5 * (1 + $tsum);
  2909. if ($tails == 1) {
  2910. return 1 - abs($tValue);
  2911. } else {
  2912. return 1 - abs((1 - $tValue) - $tValue);
  2913. }
  2914. }
  2915. return PHPExcel_Calculation_Functions::VALUE();
  2916. } // function TDIST()
  2917. /**
  2918. * TINV
  2919. *
  2920. * Returns the one-tailed probability of the chi-squared distribution.
  2921. *
  2922. * @param float $probability Probability for the function
  2923. * @param float $degrees degrees of freedom
  2924. * @return float
  2925. */
  2926. public static function TINV($probability, $degrees) {
  2927. $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  2928. $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  2929. if ((is_numeric($probability)) && (is_numeric($degrees))) {
  2930. $xLo = 100;
  2931. $xHi = 0;
  2932. $x = $xNew = 1;
  2933. $dx = 1;
  2934. $i = 0;
  2935. while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) {
  2936. // Apply Newton-Raphson step
  2937. $result = self::TDIST($x, $degrees, 2);
  2938. $error = $result - $probability;
  2939. if ($error == 0.0) {
  2940. $dx = 0;
  2941. } elseif ($error < 0.0) {
  2942. $xLo = $x;
  2943. } else {
  2944. $xHi = $x;
  2945. }
  2946. // Avoid division by zero
  2947. if ($result != 0.0) {
  2948. $dx = $error / $result;
  2949. $xNew = $x - $dx;
  2950. }
  2951. // If the NR fails to converge (which for example may be the
  2952. // case if the initial guess is too rough) we apply a bisection
  2953. // step to determine a more narrow interval around the root.
  2954. if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) {
  2955. $xNew = ($xLo + $xHi) / 2;
  2956. $dx = $xNew - $x;
  2957. }
  2958. $x = $xNew;
  2959. }
  2960. if ($i == MAX_ITERATIONS) {
  2961. return PHPExcel_Calculation_Functions::NA();
  2962. }
  2963. return round($x,12);
  2964. }
  2965. return PHPExcel_Calculation_Functions::VALUE();
  2966. } // function TINV()
  2967. /**
  2968. * TREND
  2969. *
  2970. * Returns values along a linear trend
  2971. *
  2972. * @param array of mixed Data Series Y
  2973. * @param array of mixed Data Series X
  2974. * @param array of mixed Values of X for which we want to find Y
  2975. * @param boolean A logical value specifying whether to force the intersect to equal 0.
  2976. * @return array of float
  2977. */
  2978. public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True) {
  2979. $yValues = PHPExcel_Calculation_Functions::flattenArray($yValues);
  2980. $xValues = PHPExcel_Calculation_Functions::flattenArray($xValues);
  2981. $newValues = PHPExcel_Calculation_Functions::flattenArray($newValues);
  2982. $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const);
  2983. $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const);
  2984. if (empty($newValues)) {
  2985. $newValues = $bestFitLinear->getXValues();
  2986. }
  2987. $returnArray = array();
  2988. foreach($newValues as $xValue) {
  2989. $returnArray[0][] = $bestFitLinear->getValueOfYForX($xValue);
  2990. }
  2991. return $returnArray;
  2992. } // function TREND()
  2993. /**
  2994. * TRIMMEAN
  2995. *
  2996. * Returns the mean of the interior of a data set. TRIMMEAN calculates the mean
  2997. * taken by excluding a percentage of data points from the top and bottom tails
  2998. * of a data set.
  2999. *
  3000. * Excel Function:
  3001. * TRIMEAN(value1[,value2[, ...]],$discard)
  3002. *
  3003. * @access public
  3004. * @category Statistical Functions
  3005. * @param mixed $arg,... Data values
  3006. * @param float $discard Percentage to discard
  3007. * @return float
  3008. */
  3009. public static function TRIMMEAN() {
  3010. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  3011. // Calculate
  3012. $percent = array_pop($aArgs);
  3013. if ((is_numeric($percent)) && (!is_string($percent))) {
  3014. if (($percent < 0) || ($percent > 1)) {
  3015. return PHPExcel_Calculation_Functions::NaN();
  3016. }
  3017. $mArgs = array();
  3018. foreach ($aArgs as $arg) {
  3019. // Is it a numeric value?
  3020. if ((is_numeric($arg)) && (!is_string($arg))) {
  3021. $mArgs[] = $arg;
  3022. }
  3023. }
  3024. $discard = floor(self::COUNT($mArgs) * $percent / 2);
  3025. sort($mArgs);
  3026. for ($i=0; $i < $discard; ++$i) {
  3027. array_pop($mArgs);
  3028. array_shift($mArgs);
  3029. }
  3030. return self::AVERAGE($mArgs);
  3031. }
  3032. return PHPExcel_Calculation_Functions::VALUE();
  3033. } // function TRIMMEAN()
  3034. /**
  3035. * VARFunc
  3036. *
  3037. * Estimates variance based on a sample.
  3038. *
  3039. * Excel Function:
  3040. * VAR(value1[,value2[, ...]])
  3041. *
  3042. * @access public
  3043. * @category Statistical Functions
  3044. * @param mixed $arg,... Data values
  3045. * @return float
  3046. */
  3047. public static function VARFunc() {
  3048. // Return value
  3049. $returnValue = PHPExcel_Calculation_Functions::DIV0();
  3050. $summerA = $summerB = 0;
  3051. // Loop through arguments
  3052. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  3053. $aCount = 0;
  3054. foreach ($aArgs as $arg) {
  3055. if (is_bool($arg)) { $arg = (integer) $arg; }
  3056. // Is it a numeric value?
  3057. if ((is_numeric($arg)) && (!is_string($arg))) {
  3058. $summerA += ($arg * $arg);
  3059. $summerB += $arg;
  3060. ++$aCount;
  3061. }
  3062. }
  3063. // Return
  3064. if ($aCount > 1) {
  3065. $summerA *= $aCount;
  3066. $summerB *= $summerB;
  3067. $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
  3068. }
  3069. return $returnValue;
  3070. } // function VARFunc()
  3071. /**
  3072. * VARA
  3073. *
  3074. * Estimates variance based on a sample, including numbers, text, and logical values
  3075. *
  3076. * Excel Function:
  3077. * VARA(value1[,value2[, ...]])
  3078. *
  3079. * @access public
  3080. * @category Statistical Functions
  3081. * @param mixed $arg,... Data values
  3082. * @return float
  3083. */
  3084. public static function VARA() {
  3085. // Return value
  3086. $returnValue = PHPExcel_Calculation_Functions::DIV0();
  3087. $summerA = $summerB = 0;
  3088. // Loop through arguments
  3089. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  3090. $aCount = 0;
  3091. foreach ($aArgs as $k => $arg) {
  3092. if ((is_string($arg)) &&
  3093. (PHPExcel_Calculation_Functions::isValue($k))) {
  3094. return PHPExcel_Calculation_Functions::VALUE();
  3095. } elseif ((is_string($arg)) &&
  3096. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3097. } else {
  3098. // Is it a numeric value?
  3099. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  3100. if (is_bool($arg)) {
  3101. $arg = (integer) $arg;
  3102. } elseif (is_string($arg)) {
  3103. $arg = 0;
  3104. }
  3105. $summerA += ($arg * $arg);
  3106. $summerB += $arg;
  3107. ++$aCount;
  3108. }
  3109. }
  3110. }
  3111. // Return
  3112. if ($aCount > 1) {
  3113. $summerA *= $aCount;
  3114. $summerB *= $summerB;
  3115. $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
  3116. }
  3117. return $returnValue;
  3118. } // function VARA()
  3119. /**
  3120. * VARP
  3121. *
  3122. * Calculates variance based on the entire population
  3123. *
  3124. * Excel Function:
  3125. * VARP(value1[,value2[, ...]])
  3126. *
  3127. * @access public
  3128. * @category Statistical Functions
  3129. * @param mixed $arg,... Data values
  3130. * @return float
  3131. */
  3132. public static function VARP() {
  3133. // Return value
  3134. $returnValue = PHPExcel_Calculation_Functions::DIV0();
  3135. $summerA = $summerB = 0;
  3136. // Loop through arguments
  3137. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  3138. $aCount = 0;
  3139. foreach ($aArgs as $arg) {
  3140. if (is_bool($arg)) { $arg = (integer) $arg; }
  3141. // Is it a numeric value?
  3142. if ((is_numeric($arg)) && (!is_string($arg))) {
  3143. $summerA += ($arg * $arg);
  3144. $summerB += $arg;
  3145. ++$aCount;
  3146. }
  3147. }
  3148. // Return
  3149. if ($aCount > 0) {
  3150. $summerA *= $aCount;
  3151. $summerB *= $summerB;
  3152. $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
  3153. }
  3154. return $returnValue;
  3155. } // function VARP()
  3156. /**
  3157. * VARPA
  3158. *
  3159. * Calculates variance based on the entire population, including numbers, text, and logical values
  3160. *
  3161. * Excel Function:
  3162. * VARPA(value1[,value2[, ...]])
  3163. *
  3164. * @access public
  3165. * @category Statistical Functions
  3166. * @param mixed $arg,... Data values
  3167. * @return float
  3168. */
  3169. public static function VARPA() {
  3170. // Return value
  3171. $returnValue = PHPExcel_Calculation_Functions::DIV0();
  3172. $summerA = $summerB = 0;
  3173. // Loop through arguments
  3174. $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args());
  3175. $aCount = 0;
  3176. foreach ($aArgs as $k => $arg) {
  3177. if ((is_string($arg)) &&
  3178. (PHPExcel_Calculation_Functions::isValue($k))) {
  3179. return PHPExcel_Calculation_Functions::VALUE();
  3180. } elseif ((is_string($arg)) &&
  3181. (!PHPExcel_Calculation_Functions::isMatrixValue($k))) {
  3182. } else {
  3183. // Is it a numeric value?
  3184. if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg != '')))) {
  3185. if (is_bool($arg)) {
  3186. $arg = (integer) $arg;
  3187. } elseif (is_string($arg)) {
  3188. $arg = 0;
  3189. }
  3190. $summerA += ($arg * $arg);
  3191. $summerB += $arg;
  3192. ++$aCount;
  3193. }
  3194. }
  3195. }
  3196. // Return
  3197. if ($aCount > 0) {
  3198. $summerA *= $aCount;
  3199. $summerB *= $summerB;
  3200. $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
  3201. }
  3202. return $returnValue;
  3203. } // function VARPA()
  3204. /**
  3205. * WEIBULL
  3206. *
  3207. * Returns the Weibull distribution. Use this distribution in reliability
  3208. * analysis, such as calculating a device's mean time to failure.
  3209. *
  3210. * @param float $value
  3211. * @param float $alpha Alpha Parameter
  3212. * @param float $beta Beta Parameter
  3213. * @param boolean $cumulative
  3214. * @return float
  3215. *
  3216. */
  3217. public static function WEIBULL($value, $alpha, $beta, $cumulative) {
  3218. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  3219. $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  3220. $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  3221. if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) {
  3222. if (($value < 0) || ($alpha <= 0) || ($beta <= 0)) {
  3223. return PHPExcel_Calculation_Functions::NaN();
  3224. }
  3225. if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
  3226. if ($cumulative) {
  3227. return 1 - exp(0 - pow($value / $beta,$alpha));
  3228. } else {
  3229. return ($alpha / pow($beta,$alpha)) * pow($value,$alpha - 1) * exp(0 - pow($value / $beta,$alpha));
  3230. }
  3231. }
  3232. }
  3233. return PHPExcel_Calculation_Functions::VALUE();
  3234. } // function WEIBULL()
  3235. /**
  3236. * ZTEST
  3237. *
  3238. * Returns the Weibull distribution. Use this distribution in reliability
  3239. * analysis, such as calculating a device's mean time to failure.
  3240. *
  3241. * @param float $value
  3242. * @param float $alpha Alpha Parameter
  3243. * @param float $beta Beta Parameter
  3244. * @param boolean $cumulative
  3245. * @return float
  3246. *
  3247. */
  3248. public static function ZTEST($dataSet, $m0, $sigma=null) {
  3249. $dataSet = PHPExcel_Calculation_Functions::flattenArrayIndexed($dataSet);
  3250. $m0 = PHPExcel_Calculation_Functions::flattenSingleValue($m0);
  3251. $sigma = PHPExcel_Calculation_Functions::flattenSingleValue($sigma);
  3252. if (is_null($sigma)) {
  3253. $sigma = self::STDEV($dataSet);
  3254. }
  3255. $n = count($dataSet);
  3256. return 1 - self::NORMSDIST((self::AVERAGE($dataSet) - $m0)/($sigma/SQRT($n)));
  3257. } // function ZTEST()
  3258. } // class PHPExcel_Calculation_Statistical