Function rust_finprim::tvm::xnpv
source · pub fn xnpv(rate: Decimal, flow_table: &[(Decimal, i32)]) -> Decimal
Expand description
XNPV - Net Present Value for irregular cash flows
The XNPV function calculates the net present value of a series of cash flows that are not necessarily periodic.
§Arguments
rate
- The discount rateflow_table
- A slice of tuples representing the cash flows and dates for each period(cash_flow, date)
wheredate
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.
Most time libraries will provide a method yielding 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
- The net present value (NPV)
§Example
- 5% discount rate
- Cash flows of $-100, $50, $40, $30, $20
- Dates of 0, 365, 420, 1360, 1460
use rust_decimal_macros::*;
use rust_finprim::tvm::xnpv;
let rate = dec!(0.05);
let flows_table = vec![
(dec!(-100), 0),
(dec!(50), 365),
(dec!(40), 420),
(dec!(30), 1360),
(dec!(20), 1460),
];
xnpv(rate, &flows_table);