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.
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
-
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")