pub fn xirr<T: FloatLike>(
flow_table: &[(T, i32)],
guess: Option<T>,
tolerance: Option<T>,
max_iter: Option<u16>,
) -> Result<T, FinPrimError<T>>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)wheredaterepresents 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 fastertolerance(optional) - The tolerance/maximum error bound for the IRR calculation, defaults to 1e-5 i.e. 0.00001max_iter(optional) - The maximum number of iterations to perform, defaults to 20.
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 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::xirr;
let flow_table = vec![
(-100.0, 0),
(50.0, 359),
(40.0, 400),
(30.0, 1000),
(20.0, 2000),
];
xirr(&flow_table, None, None, None);This function uses the Halley method to find the root of the NPV formula, maxing out at 20 iterations.