Function irr

Source
pub fn irr(
    cash_flows: &[Decimal],
    guess: Option<Decimal>,
    tolerance: Option<Decimal>,
) -> Result<Decimal, (Decimal, Decimal)>
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 Decimal 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

§Returns

  • Result of the IRR calculation
  • If the calculation fails, it returns 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;
use rust_decimal_macros::*;

let cash_flows = vec![dec!(-100), dec!(50), dec!(40), dec!(30), dec!(20)];
irr(&cash_flows, 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 Newton-Raphson method to find the root of the NPV formula, maxing out at 20 iterations.