Function irr

Source
pub fn irr<T: FloatLike>(
    cash_flows: &[T],
    guess: Option<T>,
    tolerance: Option<T>,
    max_iter: Option<u16>,
) -> Result<T, FinPrimError<T>>
Expand description

IRR - Internal Rate of Return

The internal rate of return (IRR) is a metric used in capital budgeting to estimate the profitability of potential investments. The IRR is the interest rate (discount rate) that makes the net present value (NPV) of all cash flows from a particular project equal to zero. IRR calculations rely on the same formula as NPV does, but in this case, the NPV is set to zero and the discount rate is the unknown variable. Similar behavior and usage to the IRR function in Excel.

§Arguments

  • cash_flows - A vector of values representing the cash flows of the investment
  • guess (optional) - A guess for the IRR, defaults to 0.1. Providing a guess can help the function converge faster
  • tolerance (optional) - The tolerance/maximum error bound for the IRR calculation, defaults to 1e-5 i.e. 0.00001
  • max_iter (optional) - The maximum number of iterations to perform, defaults to 20.

§Returns

  • Result of the IRR calculation
  • If the calculation fails, it returns a tuple of the error type with a tuple of the last estimated rate and the NPV at that rate
  • If the NPV is close to zero, you may consider lowering the tolerance or providing a guess at the last estimated rate. Otherwise, there may be no IRR.

§Example

  • Cash flows of $-100, $50, $40, $30, $20
use rust_finprim::rate::irr;

let cash_flows = vec![-100.0, 50.0, 40.0, 30.0, 20.0];
irr(&cash_flows, None, None, None);

§Formula

The IRR is calculated by finding the discount rate that makes the net present value (NPV) of all cash flows equal to zero. The formula is: $$NPV = \sum_{t=0}^{n} \frac{CF_t}{(1+IRR)^t} = 0$$

Where:

  • \(CF_t\) = cash flow at time \(t\)
  • \(IRR\) = internal rate of return

This function uses the Halley method to find the root of the NPV formula, maxing out at 20 iterations.