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.

Types

Why an interest calculation was rejected.

pub type InterestError {
  NegativePrincipal
  RateBelowMinusOne
  PeriodsOutOfRange
  CompoundsOutOfRange
  NegativeDigits
  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.

  • NegativeDigits

    digits was negative.

  • 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.

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