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, from the
exact value of x × (1 + rate), so a rate written to 16 places, such
as 0.0041666666666667 for 5% a year over 12 months, is used as it is
even though 1 + rate does not fit a Decimal.
payment and effective_annual_rate keep every intermediate value
at the most places, up to 28, that fit a Decimal, about 16
significant digits, and round once to digits at the end;
effective_annual_rate divides the nominal rate that way too.
schedule pays that payment and rounds each period’s interest, the
exact balance times the rate, to digits. The unrounded payment is
within about periods × 1e-15 of the exact one, relative to the
larger of the payment and principal × rate, and Python’s decimal
measured at most 5.3e-14. Only a payment whose exact value is closer
than that to a half-unit at digits places can round to the other
neighbour. For monthly rates of 1% to 20% a year over 360 and 1200
months, the payment and every row of the schedule are the same as
Python’s decimal gives.
future_value and present_value work at digits + 2 places, at
least 7. present_value divides by 1 + rate, which has to fit a
Decimal. When it does not, as for 0.0041666666666667, the rate in
it is rounded half-even to the most places at which it does, which is
15 for a rate between -1 and 1, so the rate moves by at most 5e-16.
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 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
-
NegativePrincipalThe principal was negative.
-
RateBelowMinusOneThe rate was below -1, a loss of more than 100%.
-
PeriodsOutOfRangeThe number of periods was negative or above 1200 (100 years of months).
paymentalso rejects 0. -
CompoundsOutOfRangeThe number of compounding periods per year was zero, negative or above 1200.
-
DigitsOutOfRangedigitswas negative or above 28, the most places aDecimalhas. -
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")