Tags

,

UPDATE: Now with lambda.r

Here’s a short example of calculating zero rates and discount factors from cash rates using lambda.r. Of note is how type conversions are handled and how the core function implementation focuses strictly on the calculation while overloaded versions focus on the type conversions.

discount_factor(deposit.rate, spot.date, maturity.date, day.count) %::% numeric : character : character : character : numeric
discount_factor(deposit.rate, spot.date, maturity.date, day.count) %as% {
  s <- as.Date(spot.date)
  m <- as.Date(maturity.date)
  discount_factor(deposit.rate, s, m, day.count)
}

discount_factor(deposit.rate, spot.date, maturity.date, day.count) %::% numeric : Date : Date : character : numeric
discount_factor(deposit.rate, spot.date, maturity.date, day.count) %as%
{
 days <- as.numeric(maturity.date - spot.date, units="days")
 discount_factor(deposit.rate, days, day.count)
}

discount_factor(deposit.rate, days, "Actual360") %as%
{
 1 / (1 + deposit.rate * days / 360)
}


zero_rate(discount.factor, spot.date, maturity.date) %::% a : character : character : numeric
zero_rate(discount.factor, spot.date, maturity.date) %as%
{
 s <- as.Date(spot.date)
 m <- as.Date(maturity.date)
 zero_rate(discount.factor, s,m)
}

zero_rate(discount.factor, spot.date, maturity.date) %::% a : Date : Date : numeric
zero_rate(discount.factor, spot.date, maturity.date) %as%
{
 days <- as.numeric(maturity.date - spot.date, units="days")
 zero_rate(discount.factor, days)
}

zero_rate(discount.factor, days) %::% a : numeric : numeric
zero_rate(discount.factor, days) %as%
{
 1 / df ^ (1/(days/365)) - 1
}

To get the zero rates, simply execute the following code.

df <- discount_factor(0.0123375, '2009-02-05', '2009-05-05', "Actual360")
zero_rate(df, '2009-02-05', '2009-05-05')
Advertisements