Spot Rate Curve Interpolation

The fixedincome package implements many interpolation methods for spot rate curve. All interpolation methods inherits the S4 class Interpolation. Once you instantiate an Interpolation object, it has to be set to a SpotRateCurve object and this is done throught the curve’s interpolation<- method.

There is a list with the interpolation methods implemented and their constructors:

Here it follows an example on how to create and set an interpolation to a spot rate curve.

Let’s start by creating a curve using data obtained with rb3 package.

Firstly, the packages have to be loaded.

library(rb3)
library(bizdays)
library(dplyr)
library(fixedincome)

Use the bizdays::getdate function to get the last business day, according to Brazil/ANBIMA calendar.

refdate <- getdate("last bizday", Sys.Date() - 1, "Brazil/ANBIMA")

In order to build a term structure formed only by futures maturities, the yield curve data and futures data have to be mixed and this is done with the rb3::yc_superset function. Once the superset is returned, the rows related to futures maturities can be filtered. The first term, usually 1 business day term, is also used to anchor the curve’s short part.

yc_ <- yc_get(refdate)
fut_ <- futures_get(refdate)
yc_ss <- yc_superset(yc_, fut_)
yc <- bind_rows(
  yc_ss |> slice(1),
  yc_ss |> filter(!is.na(symbol))
)

yc
#> # A tibble: 39 x 7
#>    refdate    cur_days biz_days forward_date r_252 r_360 symbol
#>    <date>        <int>    <dbl> <date>       <dbl> <dbl> <chr> 
#>  1 2022-07-13        1        1 2022-07-14   0.132 0     <NA>  
#>  2 2022-07-13       19       13 2022-08-01   0.132 0.128 DI1Q22
#>  3 2022-07-13       50       36 2022-09-01   0.134 0.138 DI1U22
#>  4 2022-07-13       82       57 2022-10-03   0.135 0.134 DI1V22
#>  5 2022-07-13      111       77 2022-11-01   0.137 0.135 DI1X22
#>  6 2022-07-13      141       97 2022-12-01   0.138 0.135 DI1Z22
#>  7 2022-07-13      173      119 2023-01-02   0.139 0.136 DI1F23
#>  8 2022-07-13      203      141 2023-02-01   0.139 0.138 DI1G23
#>  9 2022-07-13      231      159 2023-03-01   0.140 0.137 DI1H23
#> 10 2022-07-13      264      182 2023-04-03   0.140 0.138 DI1J23
#> # ... with 29 more rows

With the curve data prepared, the spotratecurve is created.

sp_curve <- spotratecurve(
  yc$r_252, yc$biz_days,
  "discrete", "business/252", "Brazil/ANBIMA",
  refdate = refdate
)

sp_curve
#>          SpotRateCurve
#> 1 day           0.1315
#> 13 days         0.1315
#> 36 days         0.1342
#> 57 days         0.1354
#> 77 days         0.1367
#> 97 days         0.1377
#> 119 days        0.1388
#> 141 days        0.1395
#> 159 days        0.1398
#> 182 days        0.1403
#> # ... with 29 more rows
#> discrete business/252 Brazil/ANBIMA 
#> Reference date: 2022-07-13

From the output above it is possible to observe that this curve does not have an interpolation method defined.

Let’s, for example, define a flat forward interpolation for this curve. The FlatForward object is created and set to the curve with the interpolation<- method.

interpolation(sp_curve) <- interp_flatforward()

sp_curve
#>          SpotRateCurve
#> 1 day           0.1315
#> 13 days         0.1315
#> 36 days         0.1342
#> 57 days         0.1354
#> 77 days         0.1367
#> 97 days         0.1377
#> 119 days        0.1388
#> 141 days        0.1395
#> 159 days        0.1398
#> 182 days        0.1403
#> # ... with 29 more rows
#> discrete business/252 Brazil/ANBIMA 
#> Reference date: 2022-07-13 
#> Interpolation: flatforward

Now the output shows the curve with the interpolation defined.

Interpolate with [[

The spot rate curve method [[ is used to interpolte the curve. The term is passed as a Term object or numeric and a spot rate curve is returned with all interpolated values.

sp_curve[[c(21, 42, 63)]]
#>         SpotRateCurve
#> 21 days        0.1331
#> 42 days        0.1347
#> 63 days        0.1359
#> discrete business/252 Brazil/ANBIMA 
#> Reference date: 2022-07-13

The term 21 doesn’t exist in the spot rate curve, so it is interpolated according to the interpolation method defined. Since the flat forward interpolation just connect the dots, the terms 42 and 63 have the same values of the spot rate curve.

Other interpolation methods can be set with the interpolation method overriding any method set previously.

interpolation(sp_curve) <- interp_naturalspline()
sp_curve[[c(21, 42, 63)]]
#>         SpotRateCurve
#> 21 days        0.1322
#> 42 days        0.1347
#> 63 days        0.1358
#> discrete business/252 Brazil/ANBIMA 
#> Reference date: 2022-07-13

Unset interpolation

Set interpolation to NULL to unset the interpolation.

interpolation(sp_curve) <- NULL
sp_curve[[c(21, 42, 63)]]
#>         SpotRateCurve
#> 21 days            NA
#> 42 days            NA
#> 63 days            NA
#> discrete business/252 Brazil/ANBIMA 
#> Reference date: 2022-07-13

Note that for those terms in the [[ method that don’t have a related term in the spot rate curve, NA is returned.

Plot with interpolation

The fixedincome::plot method for the spot rate curve has an argument use_interpolation that shows the interpolation together with the curve points. This argument defaults to FALSE.

interpolation(sp_curve) <- interp_flatforward()
plot(sp_curve, use_interpolation = TRUE)

Forward rates with and without interpolation set

Once the interpolation is set, the plot method uses it to calculate daily forward rates. Otherwise, it uses the forward rates between the curve terms. Set the show_forward argument to TRUE to show the forward rates.

interpolation(sp_curve) <- NULL
plot(sp_curve, show_forward = TRUE, legend_location = "bottomright")

The forward rates are drawn with step lines.

If the use_interpolation argument is TRUE then the daily forward rates are calculated with the defined interpolation.

interpolation(sp_curve) <- interp_flatforward()
plot(sp_curve, use_interpolation = TRUE, show_forward = TRUE, legend_location = "bottomright")

It is possible to note that the flat forward daily rates are fairly close to curve terms forward rates.

As the interpolation changes its effects can be viewed in the forward rates dynamic.

interpolation(sp_curve) <- interp_naturalspline()
plot(sp_curve, use_interpolation = TRUE, show_forward = TRUE, legend_location = "bottomright")