Functions for financial calculations such as interest, EMI, and currency conversion.
Summary
Functions
Full amortization schedule for a loan, one entry per period.
Future value of a stream of equal payments.
Payment required to amortise a present value over n periods.
Present value of a stream of equal payments.
Units that must be sold to cover fixed costs.
Compound annual growth rate, as a percentage.
Calculates EMI (Equated Monthly Installment).
Calculates compound interest.
Converts currency based on exchange rates.
Declining-balance depreciation schedule, one entry per year.
Straight-line depreciation per year.
Effective annual rate for a nominal rate compounded n times per year.
Future value of a present sum compounded at a periodic rate.
Internal rate of return: the discount rate at which npv/2 is zero.
Level payment for a loan. A zero-rate-safe alias for calculate_emi/3.
Net present value of a series of cash flows.
Years until cumulative cash flows repay an initial investment.
Present value of a future sum discounted at a periodic rate.
Return on investment, as a percentage.
Calculates simple interest.
Functions
Full amortization schedule for a loan, one entry per period.
Each entry gives the payment, how much of it is principal, how much is interest, and the balance remaining afterwards.
Precision
The final period absorbs any accumulated rounding so the balance closes at exactly zero, which is what a lender's schedule does.
Examples
iex> schedule = NumberF.Financial.amortization_schedule(100_000, 0.10, 12)
iex> length(schedule)
12
iex> List.last(schedule).balance
0.0
Future value of a stream of equal payments.
Examples
iex> NumberF.Financial.annuity_future_value(100, 0.05, 10)
1257.79
Payment required to amortise a present value over n periods.
Examples
iex> NumberF.Financial.annuity_payment(10_000, 0.05, 10)
1295.05
Present value of a stream of equal payments.
Examples
iex> NumberF.Financial.annuity_present_value(100, 0.05, 10)
772.17
Units that must be sold to cover fixed costs.
Examples
iex> NumberF.Financial.break_even_point(10_000, 25, 15)
1000.0
Compound annual growth rate, as a percentage.
Examples
iex> NumberF.Financial.cagr(1000, 2000, 5)
14.87
Calculates EMI (Equated Monthly Installment).
Calculates compound interest.
Converts currency based on exchange rates.
Declining-balance depreciation schedule, one entry per year.
factor defaults to 2, giving the double-declining-balance method. Depreciation
stops at the salvage value.
Examples
iex> schedule = NumberF.Financial.depreciation_declining_balance(10_000, 1_000, 5)
iex> hd(schedule)
%{year: 1, depreciation: 4000.0, book_value: 6000.0}
Straight-line depreciation per year.
Examples
iex> NumberF.Financial.depreciation_straight_line(10_000, 1_000, 5)
1800.0
Effective annual rate for a nominal rate compounded n times per year.
Examples
iex> NumberF.Financial.effective_annual_rate(0.12, 12)
0.1268
Future value of a present sum compounded at a periodic rate.
Examples
iex> NumberF.Financial.future_value(1000, 0.05, 10)
1628.89
Internal rate of return: the discount rate at which npv/2 is zero.
Returns {:ok, rate} or {:error, :no_convergence}.
Edge cases
Solved numerically by bisection, so it needs the cash flows to change sign at
least once — an all-positive or all-negative series has no IRR and returns
{:error, :no_sign_change}. A series that changes sign more than once can have
several mathematically valid answers; this returns the one in -0.9999..10.
Examples
iex> {:ok, rate} = NumberF.Financial.irr([-1000, 300, 400, 500, 600])
iex> Float.round(rate, 4)
0.2489
Level payment for a loan. A zero-rate-safe alias for calculate_emi/3.
Examples
iex> NumberF.Financial.loan_payment(100_000, 0.0, 12)
8333.33
Net present value of a series of cash flows.
The first element is treated as occurring at t=0 and is not discounted, which is the convention a negative initial investment implies.
Examples
iex> NumberF.Financial.npv(0.1, [-1000, 300, 400, 500, 600])
388.77
Years until cumulative cash flows repay an initial investment.
Interpolates within the year the investment is recovered. Returns
{:error, :never_recovered} when the flows never add up to the investment.
Examples
iex> NumberF.Financial.payback_period(1000, [300, 400, 500])
{:ok, 2.6}
Present value of a future sum discounted at a periodic rate.
Examples
iex> NumberF.Financial.present_value(1000, 0.05, 10)
613.91
Return on investment, as a percentage.
Examples
iex> NumberF.Financial.roi(1500, 1000)
50.0
Calculates simple interest.