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, once, from the exact value: simple, future_value, present_value, payment and effective_annual_rate give the same result as rounding the exact fraction, so they agree with Python’s decimal at 200 digits. A rate is used as given, whatever its places, so a rate written to 16 places such as 0.0041666666666667 (5% a year over 12 months) works although 1 + rate does not fit a Decimal.

The exact value of (1 + rate)^periods can have thousands of digits, so it is first bounded from below and above with about 35, then 70, significant digits. When both bounds round to the same result, that is the result; only when the exact value is too close to a half-unit for that, as for a tie, is it computed in full. An exact tie only happens when that power has fewer than about 250 digits and an exponent of at most about 230, so the full value is computed up to 700 digits and an exponent of 700, and no further: a value within about 10^-60 of a half-unit, not on it, whose power would need more is ArithmeticFailed(PrecisionExceeded). No such input is known; a payment just above a tie, such as principal × rate = 4833.95625 over 1152 periods at 4 places, is settled without the power. So the work of a call is bounded whatever its inputs: at most two products per period at each of about 35 and 70 digits, and an exact value of at most 700 digits.

The result of simple, future_value, present_value, payment and effective_annual_rate always has exactly digits places. When it does not fit a Decimal at digits places, because its coefficient would pass 2^53 − 1, as for a payment of 1,199.10 at 20 places, the function returns ArithmeticFailed(PrecisionExceeded) rather than fewer places. A digits outside 0 to 28 returns DigitsOutOfRange before any work is done.

schedule pays the rounded payment each period and rounds each period’s interest, the exact balance times the rate, to digits. The balance and the last row keep the places of the principal when it has more than digits, and a row whose sum would need fewer places than its operands is ArithmeticFailed(PrecisionExceeded) too.

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