library(cowplot)
## Warning: package 'cowplot' was built under R version 4.0.5
library(ggbiplot)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.5
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 4.0.5
## Loading required package: scales
## Warning: package 'scales' was built under R version 4.0.5
## Loading required package: grid
#function to estimate the bond value, where the r is semi annual rate of interest and so is the coupon paid, the Time is taken twice
bondval<-function(c,t, r, par){
bv=c/r+(par-c/r)*(1+r)^(-2*t)
return(bv)
}
par=100 #par value
C=5 #semi annual coupon rate
t=30 #total to maturity
price=110 #price traded
r=seq(0.02,0.05,length=300) #half yearly rates
value=bondval(C,t,r,par)
yield2mat=spline(value,r,xout=price)
#Chapter 3: Problem #1Solve the YTM graphically
plot(r,value,xlab='Yield to Maturity',ylab='Price of Bond')
abline(h=110,col='red')
abline(v=yield2mat,col='green')

#Problem 2: what does uniroot(function(r) r^2 - 0.5, c(0.7, 0.8)) do?
soln<-uniroot(function(r) r^2 - 0.5, c(0.7, 0.8))
y2m=2*soln$root
sprintf('The YTM annual is %f',y2m)
## [1] "The YTM annual is 1.414214"
#it finds out the square root of 0.5
#Problem 3: Use uniroot() to find the yield to maturity of the 30-year par
#$1,000 bond with coupon payments of $40 that is selling at $1,200.
#Solution
#Running on original data
soln<-uniroot(function(r) price-C/r+(par-C/r)*(1+r)^-(2*t), c(0.02, 0.05))
y2m=2*soln$root
sprintf('The YTM annual is %f',y2m)
## [1] "The YTM annual is 0.091408"
#defining the values as per the question
price=1200
C=40
par=1000
t=30
soln<-uniroot(function(r) price-C/r+(par-C/r)*(1+r)^-(2*t), c(0.02, 0.05))
soln$root
## [1] 0.03400478
#Problem 4: Find the yield to maturity of a par $10,000 bond selling at $9,800 with semiannual coupon payments equal to $280 and maturing in 8 years.
price=9800
C=280
par=10000
t=8
soln<-uniroot(function(r) price-C/r+(par-C/r)*(1+r)^-(2*t), c(0.02, 0.05))
y2m<-2*soln$root
sprintf('The yield is %f',y2m)
## [1] "The yield is 0.056695"
#Problem #5: Use uniroot() to find the yield to maturity of the 20-year par $1,000 bond with semiannual coupon payments of $35 that is selling at $1,050.
price=1050
C=35
par=1000
t=20
soln<-uniroot(function(r) price-C/r+(par-C/r)*(1+r)^-(2*t), c(0.02, 0.05))
y2m<-2*soln$root
sprintf('The yield to maturity is %f',y2m)
## [1] "The yield to maturity is 0.067340"
#Problem #6: The yield to maturity is 0.035 on a par $1,000 bond selling at $950.10 and maturing in 5 years. What is the coupon payment?
price=950.1
r=0.035
par=1000
t=5
annual_soln<-uniroot(function(C) price-C/r+(par-C/r)*(1+r)^-(t), c(0,100))
annual_soln$root
## [1] 34.05183
semiannual_soln<-uniroot(function(C) price-C/r+(par-C/r)*(1+r)^-(2*t),c(0,100))
semiannual_soln$root
## [1] 33.97801
#Zero coupon yields
library(YieldCurve)
## Warning: package 'YieldCurve' was built under R version 4.0.5
## Loading required package: xts
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.5
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
data("FedYieldCurve")
head(FedYieldCurve)
## Warning: index class is Date, which does not support timezones.
## Expected 'UTC' timezone, but tzone is ''
## R_3M R_6M R_1Y R_2Y R_3Y R_5Y R_7Y R_10Y
## 1981-12-31 12.92 13.90 14.32 14.57 14.64 14.65 14.67 14.59
## 1982-01-31 14.28 14.81 14.73 14.82 14.73 14.54 14.46 14.43
## 1982-02-28 13.31 13.83 13.95 14.19 14.13 13.98 13.93 13.86
## 1982-03-31 13.34 13.87 13.98 14.20 14.18 14.00 13.94 13.87
## 1982-04-30 12.71 13.13 13.34 13.78 13.77 13.75 13.74 13.62
## 1982-05-31 13.08 13.76 14.07 14.47 14.48 14.43 14.47 14.30
dates<-c("2000-01-01","2010-01-01")
rates<-FedYieldCurve["2000"]
autoplot(rates)

autoplot(rates, facets=FALSE)

#autoplot(rates$R_3M,rates$R_6M,rates$R_1Y,rates$R_2Y,rates$R_3Y,facets = FALSE)
#last(rates)
#plot_grid(rates$R_3M,rates$R_6M,rates$R_1Y,rates$R_2Y,rates$R_3Y)
#Problem #7: Describe how the yield curve changes between December 1, 1985 and March 1, 1986. Describe the behavior of both the short and long ends of
#the yield curves.
start.date=as.POSIXct("1985-12-01")
end.date=as.POSIXct("1986-03-01")
rates<-FedYieldCurve[paste(start.date,end.date,sep="::")]
TSG<-autoplot(rates,facet=FALSE)
TSG+ggtitle("Yields from 01/12/1985 to 01/03/1986")+xlab("Dates")+ylab('Yields')+ggthemes::theme_solarized()

#Problem #8: Plot the yield curves from December 1, 1986 to March 1, 1987 and describe how the yield curve changes during this period.
start.date=as.POSIXct("1986-12-01")
end.date=as.POSIXct("1987-03-01")
rates<-FedYieldCurve[paste(start.date,end.date,sep="::")]
TSG<-autoplot(rates,facet=FALSE)
TSG+ggtitle("Yields from 01/12/1986 to 01/03/1987")+xlab("Dates")+ylab('Yields')+ggthemes::theme_solarized()

#Problem#9: Plot the forward rates on the same dates used before, 1985-12-01, 1986-01-01, 1986-02-01, and 1986-03-01. Describe how the forward rates changed from month to month.
#after chapter 21