finanza/interest

Time value of money: simple interest, future and present value, loan payments, effective annual rates and amortization schedules.

Rates are per period as decimals, so 0.5% a month is 0.005. Every function rounds its result half-even to digits places.

Growth over many periods is computed one period at a time. Results agree with numpy-financial and spreadsheet functions to the cent for monthly rates over 30 years. future_value and present_value work at digits + 2 places; payment, effective_annual_rate and schedule work at 7, so a digits above 7 pads with zeros there.

A Decimal has at most 28 places, so a digits above 28 returns DigitsOutOfRange before any work is done, and future_value and present_value work at no more than 28 places.

The growth per period is 1 + rate, which has to fit a Decimal too. A rate written to 16 places, such as 0.0041666666666667 for 5% a year over 12 months, makes 1 + rate a 17-digit coefficient. The rate in it is then rounded half-even to the most places at which decimal.add computes the sum. A rate between -1 and 1 with 15 places or fewer is used as it is, and one with more is rounded to 15, so it moves by at most 5e-16. future_value, present_value, payment and effective_annual_rate compound with that base; each period’s interest in schedule and the numerator of payment use the rate as given.

The number of periods, and of compounding periods per year, is at most 1200 (100 years of months). The functions loop once per period in constant stack, on JavaScript too.

Types

Why an interest calculation was rejected.

pub type InterestError {
  NegativePrincipal
  RateBelowMinusOne
  PeriodsOutOfRange
  CompoundsOutOfRange
  DigitsOutOfRange
  ArithmeticFailed(decimal.ArithmeticError)
}

Constructors

  • NegativePrincipal

    The principal was negative.

  • RateBelowMinusOne

    The rate was below -1, a loss of more than 100%.

  • PeriodsOutOfRange

    The number of periods was negative or above 1200 (100 years of months). payment also rejects 0.

  • CompoundsOutOfRange

    The number of compounding periods per year was zero, negative or above 1200.

  • DigitsOutOfRange

    digits was negative or above 28, the most places a Decimal has.

  • ArithmeticFailed(decimal.ArithmeticError)

    The decimal arithmetic underneath failed.

One row of an amortization schedule.

pub type Period {
  Period(
    index: Int,
    payment: decimal.Decimal,
    interest: decimal.Decimal,
    principal_paid: decimal.Decimal,
    balance: decimal.Decimal,
  )
}

Constructors

  • Period(
      index: Int,
      payment: decimal.Decimal,
      interest: decimal.Decimal,
      principal_paid: decimal.Decimal,
      balance: decimal.Decimal,
    )

    Arguments

    index

    The period number, starting at 1.

    payment

    The total paid in this period.

    interest

    The part of the payment that is interest.

    principal_paid

    The part of the payment that reduces the balance.

    balance

    The balance left after this period.

Values

pub fn effective_annual_rate(
  nominal_rate nominal_rate: decimal.Decimal,
  compounds_per_year compounds_per_year: Int,
  digits digits: Int,
) -> Result(decimal.Decimal, InterestError)

Returns the effective annual rate of a nominal rate compounded compounds_per_year times a year: (1 + nominal_rate / compounds_per_year)^compounds_per_year - 1.

pub fn future_value(
  present present: decimal.Decimal,
  rate_per_period rate: decimal.Decimal,
  periods periods: Int,
  digits digits: Int,
) -> Result(decimal.Decimal, InterestError)

Returns present × (1 + rate_per_period)^periods.

For a nominal annual rate compounded n times a year, pass the annual rate divided by n and the number of years times n.

Examples

let assert Ok(present) = decimal.parse("1000")
let assert Ok(rate) = decimal.parse("0.05")
interest.future_value(present:, rate_per_period: rate, periods: 10, digits: 2)
|> result.map(decimal.to_string)
// -> Ok("1628.89")
pub fn payment(
  principal principal: decimal.Decimal,
  rate_per_period rate: decimal.Decimal,
  periods periods: Int,
  digits digits: Int,
) -> Result(decimal.Decimal, InterestError)

Returns the payment per period that repays principal over periods periods: principal × rate / (1 - (1 + rate)^-periods), or principal / periods when the rate is zero.

Examples

let assert Ok(principal) = decimal.parse("200000")
let assert Ok(rate) = decimal.parse("0.005")
interest.payment(principal:, rate_per_period: rate, periods: 360, digits: 2)
|> result.map(decimal.to_string)
// -> Ok("1199.10")
pub fn present_value(
  future future: decimal.Decimal,
  rate_per_period rate: decimal.Decimal,
  periods periods: Int,
  digits digits: Int,
) -> Result(decimal.Decimal, InterestError)

Returns future ÷ (1 + rate_per_period)^periods, the inverse of future_value.

pub fn schedule(
  principal principal: decimal.Decimal,
  rate_per_period rate: decimal.Decimal,
  periods periods: Int,
  digits digits: Int,
) -> Result(List(Period), InterestError)

Returns the amortization schedule of a loan repaid with payment each period, or an empty list for 0 periods. Each period’s interest is the balance times the rate, rounded half-even to digits places. The last period pays off what is left, so the final balance is exactly zero. Returns PeriodsOutOfRange for negative periods or more than 1200.

Examples

let assert Ok(principal) = decimal.parse("1000")
let assert Ok(rate) = decimal.parse("0.01")
let assert Ok([first, ..]) =
  interest.schedule(principal:, rate_per_period: rate, periods: 12, digits: 2)
#(decimal.to_string(first.interest), decimal.to_string(first.principal_paid))
// -> #("10.00", "78.85")
pub fn simple(
  principal principal: decimal.Decimal,
  rate rate: decimal.Decimal,
  periods periods: Int,
  digits digits: Int,
) -> Result(decimal.Decimal, InterestError)

Returns the simple interest principal × rate × periods.

Examples

let assert Ok(principal) = decimal.parse("1000")
let assert Ok(rate) = decimal.parse("0.05")
interest.simple(principal:, rate:, periods: 3, digits: 2)
|> result.map(decimal.to_string)
// -> Ok("150.00")
✨ Search Document