Title: | Time Value of Money Functions |
---|---|
Description: | Functions for managing cashflows and interest rate curves. |
Authors: | Juan Manuel Truppia |
Maintainer: | Juan Manuel Truppia <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.2 |
Built: | 2025-02-20 04:00:21 UTC |
Source: | https://bitbucket.org/juancentro/tvm |
Returns a particular rate or rates from a curve
## S3 method for class 'rate_curve' r[rate_type = "zero_eff", x = NULL]
## S3 method for class 'rate_curve' r[rate_type = "zero_eff", x = NULL]
r |
The rate_curve object |
rate_type |
The rate type |
x |
The points in time to return |
If x
is NULL
, then returns a rate function of rate_type
type.
Else, it returns the rates of rate_type
type and corresponding to time x
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") r["zero_eff"] r["swap",c(1.5, 2)]
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") r["zero_eff"] r["swap",c(1.5, 2)]
Adjusts the discount factors by a spread
adjust_disc(fd, spread)
adjust_disc(fd, spread)
fd |
vector of discount factors used to discount cashflows in |
spread |
effective spread |
adjust_disc(fd = c(0.99, 0.98), spread = 0.01)
adjust_disc(fd = c(0.99, 0.98), spread = 0.01)
Returns the cashflow for the loan, excluding the initial inflow for the loan taker
cashflow(l)
cashflow(l)
l |
The loan |
l <- loan(rate = 0.05, maturity = 10, amt = 100, type = "bullet") cashflow(l)
l <- loan(rate = 0.05, maturity = 10, amt = 100, type = "bullet") cashflow(l)
This is the IRR of the loan's cashflow, after adding all the extra costs
cft(amt, maturity, rate, up_fee = 0, per_fee = 0)
cft(amt, maturity, rate, up_fee = 0, per_fee = 0)
amt |
The amount of the loan |
maturity |
The maturity of the loan |
rate |
The loan rate, in effective rate |
up_fee |
The fee that the loan taker pays upfront |
per_fee |
The fee that the loan payer pays every period |
It is assumed that the loan has monthly payments The CFT is returned as an effective rate of periodicity equal to that of the maturity and the rate The interest is calculated over amt + fee
cft(amt = 100, maturity = 10, rate = 0.05, up_fee = 1, per_fee = 0.1)
cft(amt = 100, maturity = 10, rate = 0.05, up_fee = 1, per_fee = 0.1)
Value of a discounted cashflow
disc_cf(fd, cf)
disc_cf(fd, cf)
fd |
The discount factor vector |
cf |
The cashflow |
disc_cf(fd = c(1, 0.99, 0.98, 0.97), cf = c(1, -0.3, -0.4, -0.6))
disc_cf(fd = c(1, 0.99, 0.98, 0.97), cf = c(1, -0.3, -0.4, -0.6))
Calculates the present value of a cashflow
disc_value(r, cf, d = 1:length(cf))
disc_value(r, cf, d = 1:length(cf))
r |
A rate curve |
cf |
The vector of values corresponding to the cashflow |
d |
The periods on which the cashflow occurs. If missing, it is assumed that cf[i] occurs on period i |
The present value of the cashflow
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") disc_value(r, cf = c(-1, 1.10), d = c(0,1)) disc_value(r, cf = c(-1, 1.15*1.15), d = c(0,2))
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") disc_value(r, cf = c(-1, 1.10), d = c(0,1)) disc_value(r, cf = c(-1, 1.15*1.15), d = c(0,2))
Thru a root finding process, this function finds the rate that corresponds to a given set of discount factors, as for the loan to have the same present value discounted with the discount factors or with that constant rate
find_rate(m, d, loan_type, interval = c(1e-06, 2), tol = 1e-08)
find_rate(m, d, loan_type, interval = c(1e-06, 2), tol = 1e-08)
m |
The maturity of the loan |
d |
The discount factor vector |
loan_type |
One of the loan types |
interval |
The interval for the root finding process |
tol |
The tolerance for the root finding process |
find_rate(m = 3, d = c(0.99, 0.98, 0.97), loan_type = "bullet")
find_rate(m = 3, d = c(0.99, 0.98, 0.97), loan_type = "bullet")
Internal Rate of Return of a periodic cashflow (IRR)
irr(cf, ts = seq(from = 0, by = 1, along.with = cf), interval = c(-1, 10), ...)
irr(cf, ts = seq(from = 0, by = 1, along.with = cf), interval = c(-1, 10), ...)
cf |
The cashflow |
ts |
The times on which the cashflow occurs. It is assumed that |
interval |
A length 2 vector that indicates the root finding algorithm where to search for the irr |
... |
Other arguments to be passed on to uniroot |
irr(cf = c(-1, 0.5, 0.9), ts = c(0, 1, 3))
irr(cf = c(-1, 0.5, 0.9), ts = c(0, 1, 3))
Creates an instance of a loan class
loan(rate, maturity, amt, type, grace_int = 0, grace_amort = grace_int)
loan(rate, maturity, amt, type, grace_int = 0, grace_amort = grace_int)
rate |
The periodic effective rate of the loan |
maturity |
The maturity of the loan, measured in the same units as the periodicity of the rate |
amt |
The amount loaned |
type |
The type of loan. Available types are |
grace_int |
The number of periods that the loan doesn't pay interest and capitalizes it. Leave in 0 for zero loans |
grace_amort |
The number of periods that the loan doesn't amortize |
loan(rate = 0.05, maturity = 10, amt = 100, type = "bullet")
loan(rate = 0.05, maturity = 10, amt = 100, type = "bullet")
Net Present Value of a periodic cashflow (NPV)
npv(i, cf, ts = seq(from = 0, by = 1, along.with = cf))
npv(i, cf, ts = seq(from = 0, by = 1, along.with = cf))
i |
The rate used to discount the cashflow. It must be effective and with a periodicity that matches that of the cashflow |
cf |
The cashflow |
ts |
The times on which the cashflow occurs. It is assumed that |
The net present value at
npv(i = 0.01, cf = c(-1, 0.5, 0.9), ts = c(0, 1, 3))
npv(i = 0.01, cf = c(-1, 0.5, 0.9), ts = c(0, 1, 3))
Plots a rate curve
## S3 method for class 'rate_curve' plot(x, rate_type = NULL, y_labs_perc = TRUE, y_labs_acc = NULL, ...)
## S3 method for class 'rate_curve' plot(x, rate_type = NULL, y_labs_perc = TRUE, y_labs_acc = NULL, ...)
x |
The rate curve |
rate_type |
The rate types to plot, in c("french", "fut", "german", "zero_eff", "zero_nom", "swap", "zero_cont") |
y_labs_perc |
If TRUE, the y axe is labeled with percentages |
y_labs_acc |
If y_labs_perc is TRUE, the accuracy for the percentages (i.e., 1 for xx%, 0.1 for xx.x%, 0.01 for xx.xx%, etc) |
... |
Other arguments (unused) |
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") plot(r) ## Not run: plot(r, rate_type = "german") plot(r, rate_type = c("french", "german")) ## End(Not run)
r <- rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") plot(r) ## Not run: plot(r, rate_type = "german") plot(r, rate_type = c("french", "german")) ## End(Not run)
The value of the payment of a loan with constant payments (french type amortization)
pmt(amt, maturity, rate)
pmt(amt, maturity, rate)
amt |
The amount of the loan |
maturity |
The maturity of the loan |
rate |
The rate of the loan |
The periodicity of the maturity and the rate must match, and this will be the periodicity of the payments
pmt(amt = 100, maturity = 10, rate = 0.05)
pmt(amt = 100, maturity = 10, rate = 0.05)
The rate of a loan with constant payments (french type amortization)
rate(amt, maturity, pmt, extrema = c(1e-04, 1e+09), tol = 1e-04)
rate(amt, maturity, pmt, extrema = c(1e-04, 1e+09), tol = 1e-04)
amt |
The amount of the loan |
maturity |
The maturity of the loan |
pmt |
The payments of the loan |
extrema |
Vector of length 2 that has the minimum and maximum value to search for the rate |
tol |
The tolerance to use in the root finding algorithm |
The periodicity of the maturity and the payment must match, and this will be the periodicity of the rate (which is returned as an effective rate)
rate(amt = 100, maturity = 10, pmt = 15)
rate(amt = 100, maturity = 10, pmt = 15)
Creates a rate curve instance
rate_curve( rates = NULL, rate_type = "zero_eff", pers = 1:length(rates), rate_scale = 1, fun_d = NULL, fun_r = NULL, knots = seq.int(from = 1, to = max(pers), by = 1), functor = function(x, y) splinefun(x = x, y = y, method = "monoH.FC") )
rate_curve( rates = NULL, rate_type = "zero_eff", pers = 1:length(rates), rate_scale = 1, fun_d = NULL, fun_r = NULL, knots = seq.int(from = 1, to = max(pers), by = 1), functor = function(x, y) splinefun(x = x, y = y, method = "monoH.FC") )
rates |
A rate vector |
rate_type |
The rate type. Must be on of c("fut", "zero_nom", "zero_eff", "swap", "zero_cont) |
pers |
The periods the rates correspond to |
rate_scale |
In how many periods is the rate expressed. For example, when measuring periods in days, and using annual rates, you should use 365. When measuring periods in months, and using annual rates, you should use 12. If no scaling, use 1. |
fun_d |
A discount factor function. fun_d(x) returns the discount factor for time x, vectorized on x |
fun_r |
A rate function. fun_r(x) returns the EPR for time x, vectorized on x |
knots |
The nodes used to bootstrap the rates. This is a mandatory argument if a rate function or discount function is provided |
functor |
A function with parameters x and y, that returns a function used to interpolate |
Currently a rate curve can only be built from one of the following sources
A discount factor function
A rate function and a rate type from the following types: "fut", "zero_nom", "zero_eff", "swap" or "zero_cont
A rate vector, a pers vector and a rate type as before
rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") rate_curve(fun_r = function(x) rep_len(0.1, length(x)), rate_type = "swap", knots = 1:12) rate_curve(fun_d = function(x) 1 / (1 + x), knots = 1:12)
rate_curve(rates = c(0.1, 0.2, 0.3), rate_type = "zero_eff") rate_curve(fun_r = function(x) rep_len(0.1, length(x)), rate_type = "swap", knots = 1:12) rate_curve(fun_d = function(x) 1 / (1 + x), knots = 1:12)
The amount that has to be repayed at each moment in a loan, at the end of the period
rem(cf, amt, r)
rem(cf, amt, r)
cf |
The cashflow of the loan, not including the initial inflow for the loan taker |
amt |
The original amount of the loan |
r |
The periodic rate of the loan |
rem(cf = rep_len(0.4, 4), amt = 1, r = 0.2)
rem(cf = rep_len(0.4, 4), amt = 1, r = 0.2)
Internal Rate of Return of an irregular cashflow (IRR)
xirr(cf, d, tau = NULL, comp_freq = 1, interval = c(-0.99999, 10), ...)
xirr(cf, d, tau = NULL, comp_freq = 1, interval = c(-0.99999, 10), ...)
cf |
The cashflow |
d |
The dates when each cashflow occurs. Same length as the cashflow. Only used if tau is NULL. Assumes act/365 fractions |
tau |
The year fractions when each cashflow occurs. Same length as the cashflow |
comp_freq |
The compounding frequency used. Most relevant cases are 1 for yearly, 2 twice a year, 4 quarterly, 12 monthly, 0 no compounding, Inf continuous |
interval |
A length 2 vector that indicates the root finding algorithm where to search for the irr |
... |
Other arguments to be passed on to uniroot |
xirr(cf = c(-1, 1.5), d = Sys.Date() + c(0, 365))
xirr(cf = c(-1, 1.5), d = Sys.Date() + c(0, 365))
Net Present Value of an irregular cashflow (NPV)
xnpv(i, cf, d, tau = NULL, comp_freq = 1)
xnpv(i, cf, d, tau = NULL, comp_freq = 1)
i |
The rate used to discount the cashflow |
cf |
The cashflow |
d |
The dates when each cashflow occurs. Same length as the cashflow. Only used if tau is NULL. Assumes act/365 fractions |
tau |
The year fractions when each cashflow occurs. Same length as the cashflow |
comp_freq |
The compounding frequency used. Most relevant cases are 1 for yearly, 2 twice a year, 4 quarterly, 12 monthly, 0 no compounding, Inf continuous |
xnpv(i = 0.01, cf = c(-1, 0.5, 0.9), d = as.Date(c("2015-01-01", "2015-02-15", "2015-04-10")))
xnpv(i = 0.01, cf = c(-1, 0.5, 0.9), d = as.Date(c("2015-01-01", "2015-02-15", "2015-04-10")))