Function rust_finprim::tvm::npv

source ·
pub fn npv(rate: Decimal, cash_flows: &[Decimal]) -> Decimal
Expand description

NPV - Net Present Value

The net present value (NPV) is the difference between the present value of cash inflows and the present value of cash outflows over a period of time. NPV is used in capital budgeting to analyze the profitability of an investment or project. The NPV is calculated by discounting all cash flows to the present value using a specified discount rate. If the NPV is positive, the investment is considered profitable. If the NPV is negative, the investment is considered unprofitable. Similar to the Excel NPV function, with the main difference is that this implementation assumes the first cash flow is at time value 0 (initial investment).

§Arguments

  • rate - The discount rate per period
  • cash_flows - A slice of Decimal values representing the cash flows of the investment, note that the first cash flow is assumed to be at time value 0 (initial investment)

§Returns

  • The net present value (NPV)

§Example

  • 5% discount rate
  • Cash flows of $-100, $50, $40, $30, $20
use rust_decimal_macros::*;
use rust_finprim::tvm::npv;

let rate = dec!(0.05);
let cash_flows = vec![dec!(-100), dec!(50), dec!(40), dec!(30), dec!(20)];
npv(rate, &cash_flows);

§Formula

The NPV is calculated by discounting all cash flows to the present value using a specified discount rate. The formula is: $$NPV = \sum_{t=0}^{n} \frac{CF_t}{(1+r)^t}$$ Where:

  • \(CF_t\) = cash flow at time \(t\)
  • \(r\) = discount rate