fmbasics makes it easy to work with interest rates and discount factors. You can convert interest rates to discount factors and vice-versa.

Interest rates

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

Discount factors

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