You are here

public static function PHPExcel_Calculation_Statistical::CHIINV in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Statistical.php \PHPExcel_Calculation_Statistical::CHIINV()

* CHIINV * * Returns the one-tailed probability of the chi-squared distribution. * *

Parameters

float $probability Probability for the function: * @param float $degrees degrees of freedom * @return float

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Statistical.php, line 1046

Class

PHPExcel_Calculation_Statistical
PHPExcel_Calculation_Statistical

Code

public static function CHIINV($probability, $degrees) {
  $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees));
  if (is_numeric($probability) && is_numeric($degrees)) {
    $xLo = 100;
    $xHi = 0;
    $x = $xNew = 1;
    $dx = 1;
    $i = 0;
    while (abs($dx) > PRECISION && $i++ < MAX_ITERATIONS) {

      // Apply Newton-Raphson step
      $result = self::CHIDIST($x, $degrees);
      $error = $result - $probability;
      if ($error == 0.0) {
        $dx = 0;
      }
      elseif ($error < 0.0) {
        $xLo = $x;
      }
      else {
        $xHi = $x;
      }

      // Avoid division by zero
      if ($result != 0.0) {
        $dx = $error / $result;
        $xNew = $x - $dx;
      }

      // If the NR fails to converge (which for example may be the
      // case if the initial guess is too rough) we apply a bisection
      // step to determine a more narrow interval around the root.
      if ($xNew < $xLo || $xNew > $xHi || $result == 0.0) {
        $xNew = ($xLo + $xHi) / 2;
        $dx = $xNew - $x;
      }
      $x = $xNew;
    }
    if ($i == MAX_ITERATIONS) {
      return PHPExcel_Calculation_Functions::NA();
    }
    return round($x, 12);
  }
  return PHPExcel_Calculation_Functions::VALUE();
}