You are here

public static function PHPExcel_Calculation_Financial::IRR in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php \PHPExcel_Calculation_Financial::IRR()

* IRR * * Returns the internal rate of return for a series of cash flows represented by the numbers in values. * These cash flows do not have to be even, as they would be for an annuity. However, the cash flows must occur * at regular intervals, such as monthly or annually. The internal rate of return is the interest rate received * for an investment consisting of payments (negative values) and income (positive values) that occur at regular * periods. * * Excel Function: * IRR(values[,guess]) * *

Parameters

float[] $values An array or a reference to cells that contain numbers for which you want: * to calculate the internal rate of return. * Values must contain at least one positive value and one negative value to * calculate the internal rate of return. * @param float $guess A number that you guess is close to the result of IRR * @return float

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php, line 1336

Class

PHPExcel_Calculation_Financial
PHPExcel_Calculation_Financial

Code

public static function IRR($values, $guess = 0.1) {
  if (!is_array($values)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  $values = PHPExcel_Calculation_Functions::flattenArray($values);
  $guess = PHPExcel_Calculation_Functions::flattenSingleValue($guess);

  // create an initial range, with a root somewhere between 0 and guess
  $x1 = 0.0;
  $x2 = $guess;
  $f1 = self::NPV($x1, $values);
  $f2 = self::NPV($x2, $values);
  for ($i = 0; $i < FINANCIAL_MAX_ITERATIONS; ++$i) {
    if ($f1 * $f2 < 0.0) {
      break;
    }
    if (abs($f1) < abs($f2)) {
      $f1 = self::NPV($x1 += 1.6 * ($x1 - $x2), $values);
    }
    else {
      $f2 = self::NPV($x2 += 1.6 * ($x2 - $x1), $values);
    }
  }
  if ($f1 * $f2 > 0.0) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  $f = self::NPV($x1, $values);
  if ($f < 0.0) {
    $rtb = $x1;
    $dx = $x2 - $x1;
  }
  else {
    $rtb = $x2;
    $dx = $x1 - $x2;
  }
  for ($i = 0; $i < FINANCIAL_MAX_ITERATIONS; ++$i) {
    $dx *= 0.5;
    $x_mid = $rtb + $dx;
    $f_mid = self::NPV($x_mid, $values);
    if ($f_mid <= 0.0) {
      $rtb = $x_mid;
    }
    if (abs($f_mid) < FINANCIAL_PRECISION || abs($dx) < FINANCIAL_PRECISION) {
      return $x_mid;
    }
  }
  return PHPExcel_Calculation_Functions::VALUE();
}