Nothing
durcoupon <-
function(){
#discrate<-0.10
maxcouprate<-0.20
#discrate <- 10/ # lets keep this fixed
faceval<-1000 # lets keep this fixed
#faceval<-as.numeric(panel$facevalue)
# maturity <- panel$maturity
bonddur<- function(panel,couprate,faceval){
#couprate<-as.numeric(panel$couprate)/100
maturity<-as.numeric(panel$maturity)
discrate = as.numeric(panel$discrate)/100
freq<-1 # change thru radio button
times<-seq(from=1,by=1,length.out=maturity*freq)
pvfactors=1/(1+discrate/freq)^(freq*times)
# effrate = discrate/(100*freq)
# effperiods = freq*maturity
# pv_coupons<-(coupon/effrate)*(1-(1+effrate)^(-effperiods)) # PV of coupons
# pv_face<-faceval*(1+effrate)^(-effperiods) # PV of face value
# price<-pv_coupons+pv_face # bond price is the sum of both
# price <- round(price,2)
coupon<-couprate*faceval/100
cashflows <- rep(coupon,maturity*freq)
cashflows[length(cashflows)] = cashflows[length(cashflows)]+faceval
price<-sum(cashflows*pvfactors)
dur = sum(cashflows*pvfactors*times)/price
dur<-round(dur,2)
return(dur)
}
my.draw <- function(panel)
{
with(panel, {
bonddurs<-rep(0,20)
couprates<-seq(1,20,1)
for (i in 1:20){
bonddurs[i]=bonddur(panel,couprates[i],faceval)
}
plot(couprates, bonddurs, type='l',lwd=2, xlab="Coupon Rate", ylab="Duration",
frame = TRUE)
title(paste("Duration and Coupon Rate"))
#cat(pv)
})
panel
}
my.redraw <- function(panel)
{
rp.tkrreplot(panel, tkrp)
panel
}
my.panel <- rp.control("Duration and Coupon Rate")
#rp.doublebutton(my.panel,variable=couprate,step=0.25,title="Coupon (% p.a.)",initval=10,range=c(1,15),showvalue=TRUE,action=my.redraw)
rp.doublebutton(my.panel,variable=discrate,step=0.5,title="Discount Rate (% p.a.)",initval=10,range=c(1,15),showvalue=TRUE,action=my.redraw)
rp.doublebutton(my.panel,variable=maturity,step=1,title="Maturity (Yrs)",initval=10,range=c(1,25),showvalue=TRUE,action=my.redraw)
rp.tkrplot(panel=my.panel, name=tkrp, plotfun=my.draw, hscale=2,vscale=1.8)
#rp.do(my.panel, my.draw)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.