fmbasics makes it easy to work with interest rates and discount factors. You can convert interest rates to discount factors and vice-versa.
Let’s make an interest rate object:
library("fmdates")
library("fmbasics")
# Quarterly compounding, with Actual/365 day basis
(rate <- InterestRate(value = 0.04, compounding = 4, day_basis = 'act/365'))
#> <InterestRate> 4.00000%, QUARTERLY, ACT/365
You can convert this rate to another rate with a different compounding/day basis:
as_InterestRate(rate, compounding = 2)
#> <InterestRate> 4.02000%, SEMI-ANNUAL, ACT/365
as_InterestRate(rate, day_basis = 'act/360')
#> <InterestRate> 3.944936%, QUARTERLY, ACT/360
as_InterestRate(rate, compounding = Inf, day_basis = '30/360us')
#> <InterestRate> 3.980132%, CONTINUOUS, 30/360US
You can perform arithmetic on interest rates. Rates are implicitly converted to equivalent day basis and compounding before the operation is performed on the rates’ values.
rate1 <- InterestRate(0.04, 2, 'act/365')
rate2 <- InterestRate(0.01, Inf, 'act/360')
rate1 + rate2
#> <InterestRate> 5.016463%, SEMI-ANNUAL, ACT/365
You can also convert interest rates into discount factors:
library("lubridate")
df <- as_DiscountFactor(rate, ymd(20140101), ymd(20140401))
The InterestRate
class is vectorised.
rates <- InterestRate(seq(0.04, 0.05, 1e-4), 2, 'act/365')
rates[23:26]
#> <InterestRate> 4.22000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 4.23000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 4.24000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 4.25000%, SEMI-ANNUAL, ACT/365
rates[23:26] <- InterestRate(0.05, 2, 'act/365')
rates[23:26]
#> <InterestRate> 5.00000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 5.00000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 5.00000%, SEMI-ANNUAL, ACT/365
#> <InterestRate> 5.00000%, SEMI-ANNUAL, ACT/365
Let’s make a discount factor object:
(df <- DiscountFactor(0.9, ymd(20140101), ymd(20150101)))
#> <DiscountFactor> 0.9, 2014-01-01--2015-01-01
You can convert discount factors to interest rates.
as_InterestRate(df, compounding = 2, day_basis = 'act/365')
#> <InterestRate> 10.81851%, SEMI-ANNUAL, ACT/365
The DiscountFactor
class is vectorised.
dfs <- DiscountFactor(seq(1, 0.9, -1e-3), ymd(20140101), ymd(20150101) + days(0:100))
dfs[23:26]
#> <DiscountFactor> 0.978, 2014-01-01--2015-01-23
#> <DiscountFactor> 0.977, 2014-01-01--2015-01-24
#> <DiscountFactor> 0.976, 2014-01-01--2015-01-25
#> <DiscountFactor> 0.975, 2014-01-01--2015-01-26
You can make a zero coupon interest rate curve:
zc_df <- fmdata_example("zerocurve.csv")
values <- zc_df$dfs
starts <- as.Date(as.character(zc_df[["start"]]), "%Y%m%d")
ends <- as.Date(as.character(zc_df[["end"]]), "%Y%m%d")
dfs <- DiscountFactor(values, starts, ends)
zc <- ZeroCurve(dfs, starts[1], LogDFInterpolation())
plot(zc$pillar_times, zc$pillar_zeros, xlab = 'Years', ylab = 'Zero')
And interpolate on this:
interpolate(zc, 1:20)
#> [1] 0.01886025 0.01875951 0.01957137 0.02077059 0.02226385 0.02370676
#> [7] 0.02500574 0.02602505 0.02696769 0.02783101 0.02862813 0.02929467
#> [13] 0.02990451 0.03042819 0.03088204 0.03131674 0.03170140 0.03204332
#> [19] 0.03234925 0.03262458