Financial calculations for cash-flow analysis, time value of money, depreciation, and risk.
The functions are organised into domain modules:
Finance.CashFlow— net present value and internal rate of return (npv,xnpv,irr,xirr,mirr).Finance.TVM— time value of money (pv,fv,pmt,ipmt,ppmt,nper,rate) andamortization_schedule.Finance.Rates— rate conversions (effective_annual_rate,nominal_rate,continuous_to_periodic).Finance.Bonds— fixed income (price,ytm,duration,modified_duration,convexity).Finance.Depreciation—sln,syd,ddb,db.Finance.Returns— performance and risk metrics (volatility).Finance.Solver— the root-finding strategy behind the rate functions, swappable via the:solveroption orconfig :finance, solver: MySolver.
Deprecated flat API
Every function is also available directly on Finance (e.g. Finance.xirr/1),
but those delegators are deprecated and will be removed in 2.0 — call the
domain module instead, for example:
Finance.CashFlow.xirr([{~D[2019-01-01], -1000}, {~D[2020-01-01], 1100}])
#=> {:ok, 0.1}Options
The rate-finding and value functions take an optional keyword list. Options are
validated with nimble_options: an unknown key or bad value raises, while
problems with the data come back as {:error, reason}.
:guess(float/0) - initial rate for the Newton-Raphson solver The default value is0.1.:tolerance(float/0) - convergence threshold on the net present value The default value is1.0e-9.:max_iterations(pos_integer/0) - cap on solver iterations before giving up The default value is100.:precision(non_neg_integer/0) - decimal places the result is rounded to The default value is6.:solver(atom/0) - module implementing theFinance.Solverbehaviour (defaults toFinance.Solver.Newton)
Summary
Types
A cash flow on a given date. Money coming in is positive, money going out is negative.
A reason returned as {:error, reason} when the data can't yield a result.
A keyword option accepted by the rate-finding and value functions.
An annual rate of return expressed as a fraction, so 0.1 means 10%.
Functions
See Finance.TVM.fv/5.
See Finance.TVM.fv!/5.
See Finance.TVM.nper/5.
See Finance.TVM.nper!/5.
See Finance.TVM.pmt/5.
See Finance.TVM.pmt!/5.
See Finance.TVM.pv/5.
See Finance.TVM.pv!/5.
Types
A cash-flow amount: any number, or a Decimal when that optional dependency is installed.
A cash flow on a given date. Money coming in is positive, money going out is negative.
A date, given either as a Date struct or as an Erlang-style {year, month, day} tuple.
@type error() ::
:mismatched_lengths
| :insufficient_data
| :single_signed_flow
| :invalid_date
| :did_not_converge
| :undefined
A reason returned as {:error, reason} when the data can't yield a result.
@type option() :: {:guess, number()} | {:tolerance, number()} | {:max_iterations, pos_integer()} | {:precision, non_neg_integer()} | {:solver, module()}
A keyword option accepted by the rate-finding and value functions.
@type rate() :: float()
An annual rate of return expressed as a fraction, so 0.1 means 10%.
Functions
See Finance.TVM.fv/5.
See Finance.TVM.fv!/5.
See Finance.TVM.nper/5.
See Finance.TVM.nper!/5.
See Finance.TVM.pmt/5.
See Finance.TVM.pmt!/5.
See Finance.TVM.pv/5.
See Finance.TVM.pv!/5.
See Finance.TVM.rate/6.
See Finance.TVM.rate!/6.