Introduction

The dataset employed was gotten from the federal reserve for 1month, 2month, 3month, 6month, 1 year, 2year, 3year, 5year, 7year, 10year, 20year and 30year respectively for the month of January, 2022.

library(dplyr) 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(YieldCurve)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
library(readxl)
library(magrittr)
library(xml2)
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)

Yield Curve Plots

The first 2 plots are a representation of date and maturity against the bond rates respectively.

yield_url <- "http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData"
yield_data <- read_xml(yield_url) %>%
  xml_find_all("//*[name()='m:properties']") %>%
  lapply(xml_children) %>%
  lapply(function(x) {
    lapply(x, function(y) {
      y %>%
        xml_text() %>%
        {
          if (xml_name(y) == "Id") {
            as.integer(.)
          } else if (xml_name(y) == "NEW_DATE") {
            as.Date(.)
          } else {
            as.numeric(.)
          }
        }
    }) %>%
      set_names(xml_name(x)) %>%
      as.data.frame(stringsAsFactors = FALSE)
  }) %>% 
  bind_rows() %>%
  select(-BC_30YEARDISPLAY) %>%
  arrange(NEW_DATE)
y <- yield_data %$%
  NEW_DATE

x <- c("1 Month", "3 Month", "6 Month", "1 Year", "2 Year", "3 Year", "5 Year", 
       "7 Year", "10 Year", "20 Year", "30 Year")

z <- yield_data %>%
  select(BC_1MONTH:BC_30YEAR) %>%
  as.matrix()
tail(yield_data)
dim(yield_data)
## [1] 8030   14

Below is a chart representation of the yield rate against maturity: a) 1-Month

plot(yield_data$NEW_DATE, yield_data$BC_1MONTH)

b) 2-Month

plot(yield_data$NEW_DATE, yield_data$BC_2MONTH)

  1. 3-Month
plot(yield_data$NEW_DATE, yield_data$BC_3MONTH)

d) 6-Month

plot(yield_data$NEW_DATE, yield_data$BC_6MONTH)

e) 1-Year

plot(yield_data$NEW_DATE, yield_data$BC_1YEAR)

f) 2-Year

plot(yield_data$NEW_DATE, yield_data$BC_2YEAR)

g) 3-Year

plot(yield_data$NEW_DATE, yield_data$BC_3YEAR)

h) 5-Year

plot(yield_data$NEW_DATE, yield_data$BC_5YEAR)

i) 7-Year

plot(yield_data$NEW_DATE, yield_data$BC_7YEAR)

j) 10-Year

plot(yield_data$NEW_DATE, yield_data$BC_10YEAR)

k) 20-Year

plot(yield_data$NEW_DATE, yield_data$BC_20YEAR)

l) 30-Year

plot(yield_data$NEW_DATE, yield_data$BC_30YEAR)

Below is the 3D representation of the current time (t), maturity time (T) and yield rate (r);

plot <- plot_ly(yield_data,
                x = x,
                y = y,
                z = z,
                type = "surface",
                name = "yield",
                colorscale = list(
                  list(
                    0,
                    "rgb(231, 245, 255)"
                  ),
                  list (
                    1,
                    "rgb(31, 119, 180)"
                  )
                ),
                showscale = FALSE,
                lighting = list(
                  fresnel = 0.01,
                  specular = 0.01,
                  ambient = 0.95,
                  diffuse = 0.99,
                  roughness = 0.01
                ),
                zmax = 10.0, 
                zmin = 0)

plot <- layout(
  plot,
  title = "US Treasury Yield Curve",
  autosize = FALSE,
  scene = list(
    xaxis = list(
      zeroline = FALSE,
      showgrid = TRUE,
      type = "category",
      title = ""
    ),
    yaxis = list(
      zeroline = FALSE,
      showgrid = TRUE,
      type = "date",
      title = ""
    ),
    type = "surface",
    zaxis = list(
      zeroline = FALSE,
      showgrid = TRUE,
      ticksuffix = "%",
      title = "Yield"
    ),
    aspectmode = "manual",
    camera = list(
      eye = list(
        y = -0.03,
        x = -2.7,
        z = 1.3
      ),
      up = list(
        y = 0,
        x = 0,
        z = 1
      ),
      center = list(
        y = 0,
        x = 0,
        z = 0
      )
    ),
    aspectratio = list(
      y = 4,
      x = 1,
      z = 2
    )
  ),
  height = 350,
  width = 700,
  showlegend = FALSE,
  margin = list(
    r = 20,
    b = 60,
    l = 20,
    t = 60
  )
)

plot

Conclusion

From the charts, it’s evident that the bond yields are more deterministic for longer maturity bonds.