by Daniel Hanson

**Introduction**

Last time, we used the discretization of a Brownian Motion process with a Monte Carlo method to simulate the returns of a single security, with the (rather strong) assumption of a fixed drift term and fixed volatility. We will return to this topic in a future article, as it relates to basic option pricing methods, which we will then expand upon.

For more advanced derivatives pricing methods, however, as well as an important topic in its own right, we will talk about implementing a term structure of interest rates using R. This will be broken up into two parts: 1) working with dates and interpolation (the subject of today’s article), and 2) calculating forward interest rates and discount factors (the topic of our next article), using the results presented below.

**Working with Dates in R**

The standard date objects in base R, to be honest, are not the most user-friendly when it comes to basic date calculations such as adding days, months, or years. For example, just to add, say, five years to a given date, we would need to do the following:

startDate <- as.Date('2014-05-27')

pDate <- as.POSIXlt(startDate)

endDate <- as.Date(paste(pDate$year + 1900 + 5, "-", pDate$mon + 1, "-", pDate$mday, sep = ""))

So, you’re probably asking yourself, “wouldn’t it be great if we could just add the years like this?”:

endDate <- startDate + years(5) # ?

Well, the good news is that we can, by using the lubridate package. In addition, instantiating a date is also easier, simply by indicating the date format (eg ymd(.) for year-mo-day) as the function. Below are some examples:

require(lubridate)

startDate <- ymd(20140527)

startDate # Result is: "2014-05-27 UTC"

anotherDate <- dmy(26102013)

anotherDate # Result is: "2013-10-26 UTC"

startDate + years(5) # Result is: "2019-05-27 UTC"

anotherDate - years(40) # Result is: "1973-10-26 UTC"

startDate + days(2) # Result is: "2014-05-29 UTC"

anotherDate - months(5) # Result is: "2013-05-26 UTC"

Remark: Note that “UTC” is appended to the end of each date, which indicates the Coordinated Universal Time time zone (the default). While it is not an issue in these examples, it will be important to specify a particular time zone when we set up our interpolated yield curve, as we shall see shortly.

**Interpolation with Dates in R**

When interpolating values in a time series in R, we revisit with our old friend, the xts package, which provides both linear and cubic spline interpolation. We will demonstrate this with a somewhat realistic example.