Question 1

setwd("/cloud/project")
strips <- read_excel("Homework 2 Data.xlsx",sheet = "STRIPS")
#Write spot rate function
F <- 100 #Assuming Face Value =100
strips$discount.function <- strips$Price/F
get.spot.rate <- function(D,T,compound.times){
spot.rate <- compound.times*((1/D)^(1/(compound.times*T))-1)
return(spot.rate)
}
strips$spot.rate <- get.spot.rate(strips$discount.function,strips$Maturity,4)
#Write convert to forward rate function
conv.2.forward.rate <- function(x)
{
  y <- lag(x)/x
  r <- 4*(y-1)
  return(r)
}

strips$forward.rate <- conv.2.forward.rate(strips$discount.function)
spot.forward.rate <- merge(strips$spot.rate,strips$forward.rate)

#plot forward
#plot spot rate
df <- strips %>% 
  select(Maturity,spot.rate,forward.rate)
df
df.plot <- melt(data = df, id.vars = "Maturity")
df.plot
ggplot(data=df.plot,aes(x=Maturity,y=value,color = variable))+
  geom_line(size=1)+
  scale_colour_manual(values=c("blue", "red"))+
  theme_classic()
## Warning: Removed 1 row(s) containing missing values (geom_path).

Question 2

out1 <- lm(log(discount.function) ~ Maturity + I(Maturity^2) +I(Maturity^3) +I(Maturity^4)+I(Maturity^5)-1,data = strips)
a <- out1$coefficients[1]
b <- out1$coefficients[2]
c <- out1$coefficients[3]
d <- out1$coefficients[4]
e <- out1$coefficients[5]
estimates <- matrix(c(a,b,c,d,e),nrow = 5)
rownames(estimates) <- c("a","b","c","d","e")
colnames(estimates) <- "Estimates"
knitr::kable(estimates,caption = "**Estimates of discount function ploynomial $D(T) = exp(aT + bT^2 +cT^3 +dT^4 +eT^5)$**")
Estimates of discount function ploynomial \(D(T) = exp(aT + bT^2 +cT^3 +dT^4 +eT^5)\)
Estimates
a -0.0326280
b -0.0010747
c -0.0000198
d 0.0000028
e 0.0000000

Based on the output here, The discount function regression polynomial can be expressed as :\(D(T) = \exp {(0.03155 T -0.0013T^2)}\)

Question 3

strips$fitted.discount.function <-exp(a*strips$Maturity+b*I(strips$Maturity^2)+c*I(strips$Maturity^3)+d*I(strips$Maturity^4)+e*I(strips$Maturity^5))
strips$fitted.spot.rate <- get.spot.rate(strips$fitted.discount.function,strips$Maturity,2)
plot(x=strips$Maturity,y=strips$fitted.spot.rate,type="l",col=4,xlab = "Maturity",ylab = "Fitted Spot Rate")

Question 4

#write par rate function
get.par.curve <- function(D,T,compond.times){
  n <- compond.times*T
  denom <- for (i in n) {
    sum(D[1:i/compond.times])
  }
  r <- 2*((100-100*D)/sum(D[1:i/compond.times]))
  return(r)
  return(denom)
}

strips$par.rate <- get.par.curve(strips$fitted.discount.function,strips$Maturity,2)
plot(x=strips$Maturity, y= strips$par.rate, type="l",col=4)

Question 5

strips$fitted.forward.rate<-conv.2.forward.rate(strips$fitted.discount.function)
plot(x=strips$Maturity, y=strips$fitted.forward.rate, type = "l",col=4, xlab = "Maturity",ylab="Forward Rate")

#Question 6

note <- read_excel("Homework 2 Data.xlsx", sheet = 2)
# Regress on Yield Curve
out2 <- lm(Yield~ Maturity + I(Maturity^2) +I(Maturity^3) +I(Maturity^4)+I(Maturity^5),data=note)
a <- out2$coefficients[1]
b <- out2$coefficients[2]
c <- out2$coefficients[3]
d <- out2$coefficients[4]
e <- out2$coefficients[5]
f <- out2$coefficients[6]
estimates <- matrix(c(a,b,c,d,e,f),nrow = 6)
rownames(estimates) <- c("a","b","c","d","e","f")
colnames(estimates) <- "Estimates"
knitr::kable(estimates,caption = "**Estimates of par curve ploynomial $Y(T) = exp(a + bT +cT^2 +dT^3 +eT^4+fT^5)$**")
Estimates of par curve ploynomial \(Y(T) = exp(a + bT +cT^2 +dT^3 +eT^4+fT^5)\)
Estimates
a 2.5943575
b 0.5126526
c -0.0794252
d 0.0065459
e -0.0002517
f 0.0000036
note$fitted.par.yield <-exp(a+b*I(note$Maturity)+c*I(note$Maturity^2)+d*I(note$Maturity^3)+e*I(note$Maturity^4)+f*I(note$Maturity^5))
note.par.curve <- ggplot()+
  geom_line(data = note,aes(x = Maturity,y=fitted.par.yield),color=3)+
  theme_bw()
note.par.curve