1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
use crate::{ONE, ZERO};
use rust_decimal::prelude::*;
use rust_decimal_macros::*;
/// PV - Present Value
///
/// A general present value calculation, similar to the Excel `PV` function. Commonly used
/// for bond pricing and annuity calculations.
/// The `due` parameter expresses whether the annuity type is an ordinary annuity (false and the default) or an annuity due (true),
/// Excel provides this parameter as `type` with 0 for ordinary annuity and 1 for annuity due.
///
/// The present value (PV) is the current value of a future sum of money or cash flow given a
/// specified rate of return.
///
/// # Arguments
/// * `rate` - The interest rate per period
/// * `nper` - The number of compounding periods
/// * `pmt` - The payment amount per period (negative for cash outflows)
/// * `fv` (optional) - The future value
/// * `due` (optional) - The timing of the payment (false = end of period, true = beginning of period), default is false
/// (ordinary annuity)
///
/// At least one of `pmt` or `fv` should be non-zero.
///
/// # Returns
/// The present value (PV)
///
/// # Example
/// 10 Year bond with 3% YTM, $1000 future value, and 5% coupon rate (paid annually)
/// * 5% interest rate
/// * 10 compounding periods
/// * $50 payment per period (5% of $1000)
/// ```
/// use rust_finprim::tvm::pv;
/// use rust_decimal_macros::*;
///
/// let rate = dec!(0.05); let nper = dec!(10); let pmt = dec!(-50); let fv = dec!(1000);
/// pv(rate, nper, pmt, Some(fv), None);
/// ```
pub fn pv(rate: Decimal, nper: Decimal, pmt: Decimal, fv: Option<Decimal>, due: Option<bool>) -> Decimal {
let fv: Decimal = fv.unwrap_or(ZERO);
let due = due.unwrap_or(false);
let mut pv: Decimal;
if rate == ZERO {
// If the rate is zero, the nth_power should be 1 (since (1 + 0)^n = 1)
// The present value calculation when rate is zero is simplified
pv = fv + (pmt * nper);
} else {
let nth_power = (ONE + rate).powd(-nper);
let fv = fv * nth_power;
if due {
pv = pmt * (ONE - nth_power) / rate * (ONE + rate) + fv;
} else {
pv = pmt * (ONE - nth_power) / rate + fv;
}
}
// Present value negative since it represents a cash outflow
pv.set_sign_negative(true);
pv
}
/// 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
pub fn npv(rate: Decimal, cash_flows: &[Decimal]) -> Decimal {
cash_flows
.iter()
.enumerate()
.map(|(t, cf)| *cf / (ONE + rate).powi(t as i64))
.sum()
}
/// NPV Differing Rates - Net Present Value with differing discount rates
///
/// 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.
/// This function allows for differing discount rates for each cash flow.
///
/// # Arguments
/// * `flow_table` - A slice of tuples representing the cash flows and discount rates for each period `(cash_flow, discount_rate)`,
/// note that the first cash flow is assumed to be at time value 0 (initial investment)
///
/// # Returns
/// * The net present value (NPV)
///
/// # Example
/// * Cash flows of $-100, $50, $40, $30, $20
/// * Discount rates of 5%, 6%, 7%, 8%, 9%
/// ```
/// use rust_decimal_macros::*;
/// use rust_finprim::tvm::npv_differing_rates;
///
/// let flow_table = vec![
/// (dec!(-100), dec!(0.05)),
/// (dec!(50), dec!(0.06)),
/// (dec!(40), dec!(0.07)),
/// (dec!(30), dec!(0.08)),
/// (dec!(20), dec!(0.09)),
/// ];
/// npv_differing_rates(&flow_table);
/// ```
///
/// # Formula
/// The NPV is calculated by discounting all cash flows to the present value using a specified discount rate.
/// $$NPV = \sum_{t=0}^{n} \frac{CF_t}{(1+r_t)^t}$$
/// Where:
/// * \\(CF_t\\) = cash flow at time \\(t\\)
/// * \\(r_t\\) = discount rate at time \\(t\\)
pub fn npv_differing_rates(flow_table: &[(Decimal, Decimal)]) -> Decimal {
flow_table
.iter()
.enumerate()
.map(|(t, (cf, rate))| *cf / (ONE + *rate).powi(t as i64))
.sum()
}
/// 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 rate
/// * `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.
///
/// 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);
pub fn xnpv(rate: Decimal, flow_table: &[(Decimal, i32)]) -> Decimal {
// First date should be 0 (initial investment) and the rest should be difference from the initial date
let init_date = flow_table.first().unwrap().1;
let mut flows_table = flow_table.to_vec();
for (_, date) in flows_table.iter_mut() {
*date -= init_date;
}
flows_table
.iter()
.map(|(cf, date)| {
let years = Decimal::from_i32(*date).unwrap() / dec!(365);
*cf / (ONE + rate).powd(years)
})
.sum()
}
#[cfg(test)]
mod tests {
#[cfg(not(feature = "std"))]
extern crate std;
use super::*;
#[cfg(not(feature = "std"))]
use std::prelude::v1::*;
#[cfg(not(feature = "std"))]
use std::{assert, vec};
#[test]
fn test_xnpv() {
let rate = dec!(0.05);
let flows_table = vec![
(dec!(-100), 0),
(dec!(50), 365),
(dec!(40), 730),
(dec!(30), 1095),
(dec!(20), 1460),
];
let result = xnpv(rate, &flows_table);
let expected = dec!(26.26940);
assert!(
(result - expected).abs() < dec!(1e-5),
"Failed on case: {}. Expected: {}, Result: {}",
"5% discount rate, cash flows of -100, 50, 40, 30, 20",
expected,
result
);
}
#[test]
fn test_pv() {
struct TestCase {
rate: Decimal,
nper: Decimal,
pmt: Decimal,
fv: Option<Decimal>,
due: Option<bool>,
expected: Decimal,
description: &'static str,
}
impl TestCase {
fn new(
rate: f64,
nper: f64,
pmt: f64,
fv: Option<f64>,
due: Option<bool>,
expected: f64,
description: &'static str,
) -> TestCase {
TestCase {
rate: Decimal::from_f64(rate).unwrap(),
nper: Decimal::from_f64(nper).unwrap(),
pmt: Decimal::from_f64(pmt).unwrap(),
fv: fv.map(Decimal::from_f64).unwrap_or(None),
due,
expected: Decimal::from_f64(expected).unwrap(),
description,
}
}
}
let cases = [
TestCase::new(
0.05,
10.0,
100.0,
None,
None,
-772.17349,
"Standard case with 5% rate, 10 periods, and $100 pmt",
),
TestCase::new(
0.05,
10.0,
100.0,
None,
Some(true),
-810.78217,
"Payment at the beg of period should result in higher present value",
),
TestCase::new(0.0, 10.0, -100.0, None, None, -1000.0, "Zero interest rate no growth"),
TestCase::new(
0.05,
10.0,
100.0,
Some(1000.0),
None,
-1386.08675,
"Bond with 5% rate, 10 periods, 10% coupon, and $1000 future value",
),
TestCase::new(
0.05,
10.0,
0.0,
Some(2000.0),
None,
-1227.82651,
"No cash flows, just a future pay out",
),
];
for case in &cases {
let calculated_pv = pv(case.rate, case.nper, case.pmt, case.fv, case.due);
assert!(
(calculated_pv - case.expected).abs() < dec!(1e-5),
"Failed on case: {}. Expected {}, got {}",
case.description,
case.expected,
calculated_pv
);
}
}
#[test]
fn test_npv() {
struct TestCase {
rate: Decimal,
cash_flows: Vec<Decimal>,
expected: Decimal,
description: &'static str,
}
impl TestCase {
fn new(rate: f64, cash_flows: Vec<f64>, expected: f64, description: &'static str) -> TestCase {
TestCase {
rate: Decimal::from_f64(rate).unwrap(),
cash_flows: cash_flows.iter().map(|&cf| Decimal::from_f64(cf).unwrap()).collect(),
expected: Decimal::from_f64(expected).unwrap(),
description,
}
}
}
let cases = [
TestCase::new(
0.05,
vec![-100.0, 50.0, 40.0, 30.0, 20.0],
26.26940,
"Standard case with 5% rate and cash flows of -100, 50, 40, 30, 20",
),
TestCase::new(
0.05,
vec![100.0, 50.0, 40.0, 30.0, 20.0],
226.26940,
"All positive cash flows",
),
TestCase::new(
0.05,
vec![-100.0, 50.0, 40.0, 30.0, 20.0, 1000.0],
809.79557,
"Additional future cash flow should increase NPV",
),
];
for case in &cases {
let calculated_npv = npv(case.rate, &case.cash_flows);
assert!(
(calculated_npv - case.expected).abs() < dec!(1e-5),
"Failed on case: {}. Expected {}, got {}",
case.description,
case.expected,
calculated_npv
);
}
}
#[test]
fn test_npv_differing_rates() {
struct TestCase {
flow_table: Vec<(Decimal, Decimal)>,
expected: Decimal,
description: &'static str,
}
impl TestCase {
fn new(rates: Vec<f64>, cash_flows: Vec<f64>, expected: f64, description: &'static str) -> TestCase {
let rates: Vec<Decimal> = rates.iter().map(|&r| Decimal::from_f64(r).unwrap()).collect();
let cash_flows: Vec<Decimal> = cash_flows.iter().map(|&cf| Decimal::from_f64(cf).unwrap()).collect();
let flow_table = cash_flows.iter().zip(rates.iter()).map(|(&cf, &r)| (cf, r)).collect();
TestCase {
flow_table,
expected: Decimal::from_f64(expected).unwrap(),
description,
}
}
}
let cases = [
TestCase::new(
vec![0.05, 0.06, 0.07, 0.08, 0.09],
vec![-100.0, 50.0, 40.0, 30.0, 20.0],
20.09083,
"Increasing rate and cash flows of -100, 50, 40, 30, 20",
),
TestCase::new(
vec![0.05, 0.06, 0.07, 0.08, 0.09],
vec![100.0, 50.0, 40.0, 30.0, 20.0],
220.09083,
"All positive cash flows",
),
TestCase::new(
vec![0.05, 0.06, 0.07, 0.08, 0.09, 0.1],
vec![-100.0, 50.0, 40.0, 30.0, 20.0, 1000.0],
641.01215,
"Additional future cash flow should increase NPV",
),
];
for case in &cases {
let calculated_npv = npv_differing_rates(&case.flow_table);
assert!(
(calculated_npv - case.expected).abs() < dec!(1e-5),
"Failed on case: {}. Expected {}, got {}",
case.description,
case.expected,
calculated_npv
);
}
}
}