Function rust_finprim::rate::xirr

source ·
pub fn xirr(
    flow_table: &[(Decimal, i32)],
    guess: Option<Decimal>,
    tolerance: Option<Decimal>
) -> Result<Decimal, (Decimal, Decimal)>
Expand description

XIRR - Internal Rate of Return for Irregular Cash Flows

The XIRR function calculates the internal rate of return for a schedule of cash flows that is not necessarily periodic.

§Arguments

  • flow_table - A slice of tuples representing the cash flows and dates for each period (cash_flow, date) where date represents the number of days from an arbitrary epoch. The first cash flow is assumed to be the initial investment date, the order of subsequent cash flows does not matter.
  • 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

Most time libraries will provide a method for the number of days from an epoch. For example, in the chrono library you can use the num_days_from_ce method to get the number of days from the Common Era (CE) epoch, simply convert your date types to an integer representing the number of days from any epoch. Alternatively, you can calculate the time delta in days from an arbitrary epoch, such as the initial investment date.

Cash flows are discounted assuming a 365-day year.

§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::xirr;
use rust_decimal_macros::*;

let flow_table = vec![
   (dec!(-100), 0),
   (dec!(50), 359),
   (dec!(40), 400),
   (dec!(30), 1000),
   (dec!(20), 2000),
];
xirr(&flow_table, None, None);