-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathYeildCurve.R
More file actions
85 lines (69 loc) · 1.78 KB
/
YeildCurve.R
File metadata and controls
85 lines (69 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
library(readxl)
df2 <- read.csv(file.choose(),header=T)
'The InputFile.csv data file'
'Function to calculate bond price'
BondPrice=function(Fv,Cpn_Rate,N,w,rates,f){
pv=0
for(i in 1:N){
pv=pv+Fv*Cpn_Rate/(f*100*((1+rates[i]/(100*f))^(w+i-1)))
}
pv=pv+Fv/((1+rates[N]/(100*f))^(N+w-1))
return(pv)
}
'For YTM use Newton Raphson'
'For f(y)'
bp_fun=function(Fv,Cpn_Rate,N,w,y,f){
pv=0
for(i in 1:N){
pv=pv+Fv*Cpn_Rate/(f*100*((1+y/(100*f))^(w+i-1)))
}
pv=pv+Fv/((1+y/(100*f))^(N+w-1))
return(pv)
}
'For f_(y)'
derivf=function(Fv,Cpn_Rate,N,w,rates,f){
pv=0
for(i in 1:N){
pv=pv+Fv*((w+i-1)/f)*Cpn_Rate/(f*100*((1+y/(100*f))^(w+i)))
}
pv=pv+Fv*((N+w-1)/f)/((1+y/(100*f))^(N+w))
return(pv)
}
no_of_cpns = as.numeric(unlist(df2[1:7,8]))
cpn_rates = as.numeric(unlist(df2[1:7,3]))
NDNCs = as.numeric(unlist(df2[1:7,10]))
Maturity = as.numeric(unlist(df2[1:7,9]))
Maturity = Maturity/125
'Have to scale down Maturity to fit in the curve for comparision'
YTMS=c()
for(i in 1:length(no_of_cpns)){
N <- no_of_cpns[i]
Fv <- 100
Cpn_Rate <- cpn_rates[i]
NDNC <- NDNCs[i]
f <- 2
w <- NDNC/(365/f)
irs <- as.numeric(unlist(df2[,12]))
'Interest Rate Calculation'
rates=c()
for(j in 1:(length(irs)-1)){
rates[j] <-irs[j] + (irs[j+1]-irs[j])*w
}
dp=BondPrice(Fv,Cpn_Rate,N,w,rates,f)
'newton raphson method'
'initial guess'
y <- 10
error=(dp-bp_fun(Fv,Cpn_Rate,N,w,y,f))/derivf(Fv,Cpn_Rate,N,w,rates,f)
while(error > 1/1000000){
y=y-error
error=(dp-bp_fun(Fv,Cpn_Rate,N,w,y,f))/derivf(Fv,Cpn_Rate,N,w,rates,f)
}
YTMS[i] <- y
}
rates=df2[,12]
Time_to_maturity=df2[,11]
'ZCYC curve'
plot(Time_to_maturity,rates,col="blue", type="l")
'YTMs against the maturity date'
lines(Maturity,YTMS,col="green", type="l")
'Compared'