NumberF.Financial (NumberF v0.3.0)

Copy Markdown View Source

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

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

amortization_schedule(principal, annual_rate, term_months)

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

annuity_future_value(payment, rate, periods)

Future value of a stream of equal payments.

Examples

iex> NumberF.Financial.annuity_future_value(100, 0.05, 10)
1257.79

annuity_payment(present_value, rate, periods)

Payment required to amortise a present value over n periods.

Examples

iex> NumberF.Financial.annuity_payment(10_000, 0.05, 10)
1295.05

annuity_present_value(payment, rate, periods)

Present value of a stream of equal payments.

Examples

iex> NumberF.Financial.annuity_present_value(100, 0.05, 10)
772.17

break_even_point(fixed_costs, price_per_unit, variable_cost_per_unit)

Units that must be sold to cover fixed costs.

Examples

iex> NumberF.Financial.break_even_point(10_000, 25, 15)
1000.0

cagr(beginning_value, ending_value, years)

Compound annual growth rate, as a percentage.

Examples

iex> NumberF.Financial.cagr(1000, 2000, 5)
14.87

calculate_emi(principal, rate, term_months)

Calculates EMI (Equated Monthly Installment).

compound_interest(principal, rate, time, frequency)

Calculates compound interest.

convert_currency(amount, from_rate, to_rate)

Converts currency based on exchange rates.

depreciation_declining_balance(cost, salvage, life_years, factor \\ 2)

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}

depreciation_straight_line(cost, salvage, life_years)

Straight-line depreciation per year.

Examples

iex> NumberF.Financial.depreciation_straight_line(10_000, 1_000, 5)
1800.0

effective_annual_rate(nominal_rate, compounds_per_year)

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(present_value, rate, periods)

Future value of a present sum compounded at a periodic rate.

Examples

iex> NumberF.Financial.future_value(1000, 0.05, 10)
1628.89

irr(cash_flows, options \\ [])

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

loan_payment(principal, annual_rate, term_months)

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

npv(rate, cash_flows)

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

payback_period(initial_investment, cash_flows)

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(future_value, rate, periods)

Present value of a future sum discounted at a periodic rate.

Examples

iex> NumberF.Financial.present_value(1000, 0.05, 10)
613.91

roi(gain, cost)

Return on investment, as a percentage.

Examples

iex> NumberF.Financial.roi(1500, 1000)
50.0

simple_interest(principal, rate, time)

Calculates simple interest.