# Spot Rate Curve Indexing

In order to make it easy and fun the use of fixedincome spot rate curve, a few operators have been implemented.

The fixedincome package is loaded and one curve crv is created.

library(fixedincome)

crv <- spotratecurve(
c(0.1315, 0.1319, 0.1338, 0.1348, 0.1372, 0.1381),
c(1, 21, 42, 63, 126, 252),
refdate = Sys.Date()
)

The spot rate curve crv is used in the examples.

## Positional Indexing

The operator [ indexes the elements by their positions and returns one SpotRateCurve object.

The code below returns one SpotRateCurve object with the first, third and fifth elements.

crv[c(1, 3, 5)]
#>          SpotRateCurve
#> 1 day           0.1315
#> 42 days         0.1338
#> 126 days        0.1372
#> Reference date: 2022-08-15

This curve has the terms 1, 42, and 126 days.

### Boolean indexing

The positional indexing also works with logical vectors. For example, let’s get the part of the curve that is greater than 13.5%

crv[crv > 0.135]
#>          SpotRateCurve
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15

or the elements which terms are even.

crv[(crv@terms %% 2) == 0]
#>          SpotRateCurve
#> 42 days         0.1338
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15

## Term Indexing

The operator [[ indexes the elements according to the terms of the term structured and returns one SpotRateCurve object.

The code below returns one SpotRateCurve object with the terms 1, 21, and 42.

crv[[c(1, 21, 42)]]
#>         SpotRateCurve
#> 1 day          0.1315
#> 21 days        0.1319
#> 42 days        0.1338
#> Reference date: 2022-08-15

All these terms are present in the SpotRateCurve. If any given term doesn’t correspond to the SpotRateCurve terms, NA is returned for the unmatched elements.

crv[[c(1, 13, 42)]]
#>         SpotRateCurve
#> 1 day          0.1315
#> 13 days            NA
#> 42 days        0.1338
#> Reference date: 2022-08-15

To solve that an interpolation has to be set and in these cases the interpolation fulfills these gaps.

interpolation(crv) <- interp_linear()
crv[[c(1, 13, 42)]]
#>         SpotRateCurve
#> 1 day          0.1315
#> 13 days        0.1317
#> 42 days        0.1338
#> Reference date: 2022-08-15

Let’s get back to the original curve removing the interpolation.

interpolation(crv) <- NULL

## Positional Replacement

The spot rate elements in an SpotRateCurve object can be replaced positionally with the operator [.

For example, the second element can be set accordingly.

crv[2] <- 0.14
crv[c(1, 2, 3)]
#>         SpotRateCurve
#> 1 day          0.1315
#> 21 days        0.1400
#> 42 days        0.1338
#> Reference date: 2022-08-15

Negative indexes remove elements

crv[-2]
#>          SpotRateCurve
#> 1 day           0.1315
#> 42 days         0.1338
#> 63 days         0.1348
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15

But it doesn’t change the object in place, instead returns a new object without the removed elements.

crv
#>          SpotRateCurve
#> 1 day           0.1315
#> 21 days         0.1400
#> 42 days         0.1338
#> 63 days         0.1348
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15

The second element (term 21) hasn’t been removed.

## Replacement by Term

The spot rate elements in an SpotRateCurve object can be replaced by term with the operator [[.

For example, let’s replace the element related to the term 21.

crv[[21]] <- 0.1320
crv[[c(1, 21, 42)]]
#>         SpotRateCurve
#> 1 day          0.1315
#> 21 days        0.1320
#> 42 days        0.1338
#> Reference date: 2022-08-15

Negative terms also remove elements.

crv[[-21]]
#>          SpotRateCurve
#> 1 day           0.1315
#> 42 days         0.1338
#> 63 days         0.1348
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15

Nonetheless, if the interpolation is defined, negative terms raises an error.

interpolation(crv) <- interp_linear()
crv[[-21]]
#> Error in validObject(.Object): invalid class "SpotRateCurve" object: FALSE

## Special methods

The methods first and last are handy to slice parts of the SpotRateCurve.

first and last return spot rate curve according to the term given in the second argument.

For example, the following call returns the elements that lies inside the first 50 days.

fixedincome::first(crv, "50 days")
#>         SpotRateCurve
#> 1 day          0.1315
#> 21 days        0.1320
#> 42 days        0.1338
#> Reference date: 2022-08-15

The fixedincome:: prefix is necessary to avoid conflicts with dplyr’s methods.

We can do similar with the last method, for example, let’s get the elements in the last 6 months of the spot rate curve.

fixedincome::last(crv, "6 months")
#>          SpotRateCurve
#> 126 days        0.1372
#> 252 days        0.1381
#> Reference date: 2022-08-15
There is one last spotratecurve method that isn’t properly an indexing method, in the usual way, but certainly it is useful. This is the fixedincome::closest method, for the spotratecurve, it returns the element of the curve that is close to the given term. For example, to get the curve element that is the closest to 1 year, we do
fixedincome::closest(crv, "1 year")
#> Reference date: 2022-08-15