[][src]Function finance_solution::present_value_solution

pub fn present_value_solution<T>(
    rate: f64,
    periods: u32,
    future_value: T,
    continuous_compounding: bool
) -> TvmSolution where
    T: Into<f64> + Copy

Calculates the current value of a future amount using a fixed rate and returns a struct with the inputs and the calculated value. This is used for keeping track of a collection of financial scenarios so that they can be examined later.

See the present_value module page for the formulas.

Related functions:

Arguments

  • rate - The rate at which the investment grows or shrinks per period, expressed as a floating point number. For instance 0.05 would mean 5% growth. Often appears as r or i in formulas.
  • periods - The number of periods such as quarters or years. Often appears as n or t.
  • future_value - The final value of the investment.
  • continuous_compounding - True for continuous compounding, false for simple compounding.

Panics

The call will fail if rate is less than -1.0 as this would mean the investment is losing more than its full value every period. It will fail also if the future value is zero as in this case there's no way to determine the present value.

Examples

Calculate a present value and examine the period-by-period values.

use finance_solution::*;

// The rate is 8.45% per year.
let rate = 0.0845;

// The investment will grow for six years.
let periods = 6;

// The final value is $50,000.
let future_value = 50_000;

let continuous_compounding = false;

// Calculate the present value and create a struct with the input values and
// the formula used.
let solution = present_value_solution(rate, periods, future_value, continuous_compounding);
dbg!(&solution);

let present_value = solution.present_value();
assert_rounded_4(present_value, -30_732.1303);

// Examine the formulas.
let formula = solution.formula();
dbg!(&formula);
assert_eq!(formula, "-30732.1303 = -50000.0000 / (1.084500 ^ 6)");
let symbolic_formula = solution.symbolic_formula();
dbg!(&symbolic_formula);
assert_eq!("pv = -fv / (1 + r)^n", symbolic_formula);

// Calculate the amount at the end of each period.
let series = solution.series();
dbg!(&series);

Build a collection of present value calculations where the future value and periodic rate are fixed but the number of periods varies, then filter the results.

// The rate is 0.9% per month.
let rate = 0.009;

// The final value is $100,000.
let future_value = 100_000;

let continuous_compounding = false;

// We'll keep a collection of the calculated present values along with their inputs.
let mut scenarios = vec![];

// Calculate the present value for terms ranging from 1 to 36 months.
for periods in 1..=36 {
    // Calculate the future value for this number of months and add the details to the
    // collection.
    scenarios.push(present_value_solution(rate, periods, future_value, continuous_compounding));
}
dbg!(&scenarios);
assert_eq!(36, scenarios.len());

// Keep only the scenarios where the present value (which is negative) is greater than or
// than or equal to -$80,000.
scenarios.retain(|x| x.present_value() >= -80_000.00);
dbg!(&scenarios);
assert_eq!(12, scenarios.len());

// Find the range of months for the remaining scenarios.
let min_months = scenarios.iter().map(|x| x.periods()).min().unwrap();
let max_months = scenarios.iter().map(|x| x.periods()).max().unwrap();
dbg!(min_months, max_months);
assert_eq!(25, min_months);
assert_eq!(36, max_months);

// Check the formulas for the first of the remaining scenarios.
let formula = scenarios[0].formula();
dbg!(&formula);
assert_eq!("-79932.0303 = -100000.0000 / (1.009000 ^ 25)", formula);
let symbolic_formula = scenarios[0].symbolic_formula();
dbg!(&symbolic_formula);
assert_eq!("pv = -fv / (1 + r)^n", symbolic_formula);

Error case: The investment loses 111% per year. There's no way to work out what this means so the call to present_value() will panic.

let rate = -1.11;
let periods = 12;
let present_value = 100_000.85;
let continuous_compounding = false;
let present_value = present_value_solution(rate, periods, present_value, continuous_compounding);