You are here

public static function PHPExcel_Calculation_Statistical::GAMMAINV 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::GAMMAINV()

* GAMMAINV * * Returns the inverse of the beta distribution. * *

Parameters

float $probability Probability at which you want to evaluate the distribution: * @param float $alpha Parameter to the distribution * @param float $beta Parameter to the distribution * @return float *

File

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

Class

PHPExcel_Calculation_Statistical
PHPExcel_Calculation_Statistical

Code

public static function GAMMAINV($probability, $alpha, $beta) {
  $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability);
  $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha);
  $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta);
  if (is_numeric($probability) && is_numeric($alpha) && is_numeric($beta)) {
    if ($alpha <= 0 || $beta <= 0 || $probability < 0 || $probability > 1) {
      return PHPExcel_Calculation_Functions::NaN();
    }
    $xLo = 0;
    $xHi = $alpha * $beta * 5;
    $x = $xNew = 1;
    $error = $pdf = 0;
    $dx = 1024;
    $i = 0;
    while (abs($dx) > PRECISION && $i++ < MAX_ITERATIONS) {

      // Apply Newton-Raphson step
      $error = self::GAMMADIST($x, $alpha, $beta, True) - $probability;
      if ($error < 0.0) {
        $xLo = $x;
      }
      else {
        $xHi = $x;
      }
      $pdf = self::GAMMADIST($x, $alpha, $beta, False);

      // Avoid division by zero
      if ($pdf != 0.0) {
        $dx = $error / $pdf;
        $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 || $pdf == 0.0) {
        $xNew = ($xLo + $xHi) / 2;
        $dx = $xNew - $x;
      }
      $x = $xNew;
    }
    if ($i == MAX_ITERATIONS) {
      return PHPExcel_Calculation_Functions::NA();
    }
    return $x;
  }
  return PHPExcel_Calculation_Functions::VALUE();
}