Skip to content

Instantly share code, notes, and snippets.

@wilsonfreitas
Created May 29, 2022 10:04
Show Gist options
  • Save wilsonfreitas/804d9ffa82f6a80b3b32fc211710aa37 to your computer and use it in GitHub Desktop.
Save wilsonfreitas/804d9ffa82f6a80b3b32fc211710aa37 to your computer and use it in GitHub Desktop.
ForwardRate example - fixedincome package

forwardrate method, for a SpotRateCurve and with arguments t1 and t2, computes the forward rate between two future terms that exist in the term structure.

library(rb3)
library(fixedincome)

df_yc <- yc_get("2022-05-20")

crv <- spotratecurve(
  df_yc$r_252, df_yc$biz_days, "discrete", "business/252", "Brazil/ANBIMA",
  refdate = df_yc$refdate[1]
)

crv

# 10 days and 20 days exist in the term structure
forwardrate(crv, term(10, "days"), term(20, "days"))
#>         ForwardRate
#> 10 days   0.1289007
#> discrete business/252 Brazil/ANBIMA

This is equivalent to:

t1 <- term(10, "days")
t2 <- term(20, "days")
fact1 <- compound(crv[[t1]])
fact2 <- compound(crv[[t2]])
fwd_term <- t2 - t1
tf <- toyears(crv@daycount, fwd_term)
rates(crv@compounding, tf, fact2 / fact1)
#> [1] 0.1289007

So, to use forwardrate you must pass, as t1 and t2, terms that exist in the term structure, otherwise it returns NA. I took note for an extension that uses interpolation, once defined, so any term could be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment