[Uwgarp-commits] r218 - in pkg/GARPFRM: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Nov 15 21:10:31 CET 2014
Author: jaiganeshp
Date: 2014-11-15 21:10:31 +0100 (Sat, 15 Nov 2014)
New Revision: 218
Added:
pkg/GARPFRM/man/Modified.bondDuration.Rd
Modified:
pkg/GARPFRM/R/discountFactorArbitrage.R
pkg/GARPFRM/R/riskMetricsAndHedges.R
Log:
Updated BondFullPrice function + added Modified.bondDuration function
Modified: pkg/GARPFRM/R/discountFactorArbitrage.R
===================================================================
--- pkg/GARPFRM/R/discountFactorArbitrage.R 2014-11-15 19:20:31 UTC (rev 217)
+++ pkg/GARPFRM/R/discountFactorArbitrage.R 2014-11-15 20:10:31 UTC (rev 218)
@@ -1,281 +1,285 @@
-# Ch 6 Prices, Discount Factors, and Arbitrage (Law of one Price)
-# The Cash Flows from Fixed-Rate Government Coupon Bonds, Discount Faors, Law of One Price
-# Arbitrage opportunity: trade that generates profits without any chance of losing money.
-# If there is a deviation from the law of one price, there exists an arbitrage opportunity.
-# In order to estimate the discount factor for a particular term gives the value today,
-# or the present alue of one unit of currency to be received at the end of that term.(Pg.129)
-
-#' Constructor for bond specification
-#'
-#' Created a bond object \code{bond.spec} with data for bond specification.
-#'
-#' @param time vector of sequence of coupon payments in years
-#' @param face face value of bond
-#' @param m compounding frequency
-#' @param couponRate rate the coupon pays
-#' @return a \code{bond} object with the bond data used for pricing
-#' @examples
-#' time = seq(from=0.5, to=2, by=0.5)
-#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
-#' @author Thomas Fillebeen
-#' @export
-bondSpec = function(time=seq(from=0.5,to=2,by=0.5), face=100, m=2, couponRate=0.01){
- if(!all(diff(time) == (1/m))) stop("misspecification of sequence of time and compounding frequency")
- bond = list()
- bond$m = m
- bond$couponRate = couponRate
- bond$face = face
- bond$time = time
- class(bond) = c("bond.spec", "bond")
- return(bond)
-}
-
-#' To determine if user is specifying bond parameters correctly
-#'
-#' @param object a capm object created by \code{\link{bond.spec}}
-#' @author TF
-#' @export
-is.bond = function(object){
- inherits(object, "bond.spec")
-}
-
-#' Estimate price of bond
-#'
-#' This function calculates the price of a fixed rate coupon bond given the
-#' discount curve and bond data. First it converts the discountCurve into CF
-#' @param bond a \code{bondSpec} object
-#' @param discountCurve vector of discount rates
-#' @return price of the bond
-#' @examples
-#' time = seq(from=0.5, to=2, by=0.5)
-#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
-#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
-#' price = bondPrice(bond,DF)
-#' @author Thomas Fillebeen
-#' @export
-bondPrice = function(bond, discountCurve){
- if(!is.bond(bond)) stop("bond must be an object of class 'bond'")
- # Number of periods in discount curve
- nDC <- length(discountCurve)
- m <- bond$m
- couponRate <- bond$couponRate
- face <- bond$face
- time <- bond$time
-
- couponAmount <- face * couponRate / m
- cashflows <- rep(couponAmount, nDC)
- cashflows[nDC] <- couponAmount + face
- price <- sum(cashflows * discountCurve)
- return(price)
-}
-
-#' Estimate discountFactor
-#'
-#' This function calculates the discountFactor (DF) given price
-#' and cashFlows.
-#' @param price of a bond
-#' @param cashFlow of a bond
-#' @return discount factors
-#' @examples
-#' cashFlow = rbind(c(100+(1+1/4)/2,0,0),c((4 +7/8)/2,100+(4+7/8)/2,0),c((4+1/2)/2,(4+1/2)/2,100+(4+1/2)/2))
-#' # Created Price of the bond
-#' price = matrix(c(100.550, 104.513, 105.856), ncol=1)
-#' DF = discountFactor(price, cashFlow)
-#' @author Thomas Fillebeen
-#' @export
-discountFactor = function(price, cashFlow){
- DF = solve(cashFlow) %*% price
- return(DF)
-}
-
-#' bondFullPrice
-#'
-#' Estimate price of bond w/ acrrued interest
-#' The present value of a bond's cash flows should be equated or
-#' compared with its full price, with the amount a pruchaser actually
-#' pays to purchase those cash flows. The flat price is p, accrued
-#' interest is AI, the present value of the cash flows by PV, and the
-#' full price by P:
-#' P=p+AI=PV
-#'
-#' This function calculates the price of a fixed rate coupon bond given coupon rate, yield,
-#' compoundPd, cashFlowPd, face value, previous coupon date, next coupon date.
-#' @param bond is a bondSpec object created by \code{\link{bondSpec}}
-#' @param yield is the yield on the bond
-#' @param cashFlowPd cash flow period
-#' @param t0 previous coupon date
-#' @param t1 next coupon period
-#' @param currentDate current date
-#' @examples
-#' t0 = as.Date("2013-08-15")
-#' t1 = as.Date("2014-02-15")
-#' tn = as.Date("2013-10-04")
-#' currentDate = tn
-#' bond = bondSpec(face=100, m=2, couponRate = 0.0475)
-#' y1 = 0.00961
-#' bondFullPrice(bond, y1, 8, t0, t1, tn)$clean
-#' bondFullPrice(bond, y1, 8, t0, t1, tn)$dirty
-#' bondFullPrice(bond, y1, 8, t0, t1, tn)$accruedInterest
-#' @return price of the bond: clean, dirty and accrued interest
-#' @author Thomas Fillebeen
-#' @export
-bondFullPrice = function(bond, yield, cashFlowPd, t0, t1, currentDate){
- compoundPd = bond$m
- face = bond$face
- couponRate = bond$couponRate
- # Apply a general dayCount (weekend included)
- d1 = as.numeric(t1-currentDate)
- d2 = as.numeric(t1-t0)
- # Initialize
- tmp = 0
- for(k in 1:(cashFlowPd-1)){
- tmp = tmp + ((couponRate / compoundPd * face) / ((1 + yield/compoundPd)^k))
- }
- # Calculate dirty price based on partial periods formula
- dirtyP = (1 / ((1 + yield / compoundPd)^(d1/d2))) * (couponRate / compoundPd * face + tmp + face / ((1 + yield/compoundPd)^(cashFlowPd-1)))
- # Calculate accruedInterest
- aiDays = as.numeric(currentDate-t0)
- couponDays = as.numeric(t1-t0)
- ai = couponRate / compoundPd * face * aiDays / couponDays
- cleanP = dirtyP - ai
- return(list(dirty=dirtyP, clean=cleanP, accruedInterest=ai))
-}
-
-#' Estimate continuously conpounding rate to be used in term structure
-#'
-#' This function calculates the continuously compounding rate given an initial dataset
-#' with specific format, date of reference coumpounding frequency, and face value
-#' @param dat is a dataset with cusip, issueDate, MaturityDate, Name, Coupon, Bid/Ask
-#' @param intialDate is the date when the estimation should be conducted: date of reference
-#' @param m compounding frequency
-#' @param face face value
-#' @return continuously compounding rates
-#' @author Thomas Fillebeen
-#' @export
-compoundingRate = function(dat, initialDate=as.Date("1995-05-15"), m, face=100){
- # Convert the dates to a date class
- dat[, "IssueDate"] = as.Date(dat[, "IssueDate"], format="%m/%d/%Y")
- dat[, "MaturityDate"] = as.Date(dat[, "MaturityDate"], format="%m/%d/%Y")
- # Convert the coupon column to a numeric
- # Vector of prices
- price = (dat[, "Bid"] + dat[, "Ask"]) / 2
-
- # Generate cash flow dates for each bond
- bondData = list()
- for(i in 1:nrow(dat)){
- maturityDate = dat[i, "MaturityDate"]
- # Intialize a new list for every price, coupon, coupon date.
- bondData[[i]] = list()
- # Store price and the number of the coupon
- bondData[[i]]$price = price[i]
- bondData[[i]]$coupon = dat[i, "Coupon"]
- # Remove initialDate
- tmpSeq <- seq(from=initialDate, to=maturityDate, by="3 months")
- bondData[[i]]$couponDates = tmpSeq[-1]
- tmpDates = bondData[[i]]$couponDates
- tmpCoupons = vector("numeric", length(tmpDates))
- for(j in 1:length(tmpDates)){
- tmpCoupons[j] = bondData[[i]]$coupon / m * face
- if(j == length(tmpDates)){
- tmpCoupons[j] = face + bondData[[i]]$coupon / m * face
- }
- }
- bondData[[i]]$cashFlow = tmpCoupons
- }
- # Create a matrix of cash flows
- CF = matrix(0, length(price), length(price))
- # Populate the CF matrix
- for(i in 1:nrow(CF)){
- tmp = bondData[[i]]$cashFlow
- index = 1:length(tmp)
- CF[i, index] = tmp
- }
- # Utilize the discountFactor function
- DF = discountFactor(price,CF)
-
- dates = bondData[[nrow(dat)]]$couponDates
- years = vector("numeric", length(dates))
- for(i in 1:length(years)){
- years[i] = (as.numeric(strftime(dates[i], "%Y")) + as.numeric(strftime(dates[i], "%m"))/12) - (as.numeric(strftime(initialDate, "%Y")) + as.numeric(strftime(initialDate, "%m"))/12)
- }
- # Calculate continuously compounded rates from discount factors
- ccRate = vector("numeric", length(years))
- for(i in 1:length(ccRate)){
- ccRate[i] = - log(DF[i]) / years[i]
- }
- rate = list()
- rate$years = years
- rate$ccRate = ccRate
- return(rate)
-}
-
-#' Estimate spot and forward rates
-#'
-#' This function calculates the forward or forward rates given an discount factors
-#' and time increments
-#' @param time increments of time when discount factors are estimated
-#' @param DF discount factor for during time increments
-#' @examples
-#' DF = c(0.996489, 0.991306, 0.984484, 0.975616, 0.964519)
-#' time = seq(from=0.5, to=2.5, by=0.5)
-#' rates = spotForwardRates(time,DF)
-#' rates
-#' @author Thomas Fillebeen
-#' @export
-spotForwardRates = function(time, DF){
- if(length(time) != length(DF)) stop("both time and DF parameter need to be of the same length")
-spotRates = matrix(0,length(time),1)
-for(i in 1:(length(time))){
- spotRates[i] = (2-2*DF[i]^(1/(2*time[i]))) / DF[i]^(1/(2*time[i]))
-}
-
-forwardRates = matrix(0,length(time),1)
-forwardRates[1] = spotRates[1]
-for(j in 1:(length(time)-1)){
- forwardRates[j+1] = (DF[j]/DF[j+1] - 1) *2
-}
-rates = cbind(spotRates, forwardRates)
-colnames(rates)= cbind("Spot","Forward")
-return(rates)
-}
-
-### Modelling a Zero-Coupon Bond (ZCB)
-#' There are three main types of yield curve shapes: normal, inverted and flat (or humped)
-#' Estimate Vasicek zero-coupon bond to be used in term structure
-#'
-#' This function calculates the Vasicek Price given an initial data calibration
-#' The function is a subfunction for yieldCurveVasicek
-#' @param r initial short rate
-#' @param k speed of reversion parameter
-#' @param theta long-term reversion yield
-#' @param sigma randomness parameter. Modelled after Brownan Motion
-#' @return t length of time modelled for
-#' @author Thomas Fillebeen
-#' @export
-vasicekPrice = function(r, k, theta, sigma, maturity){
- mean = (1/k)*(1 - exp(-maturity*k))
- variance = (theta - sigma^2/(2*k^2))*(maturity - mean) + (sigma^2)/(4*k)*mean^2
- price = exp(-variance - mean*r)
- return(price)
- }
-
-#' Estimate Vasicek zero-coupon yield
-#'
-#' This function calculates the Vasicek yield given an initial data calibration
-#' @param r initial short rate
-#' @param k speed of reversion parameter
-#' @param theta long-term reversion yield
-#' @param sigma randomness parameter. Modelled after Brownan Motion
-#' @return t length of time modelled for
-#' @author Thomas Fillebeen
-#' @export
-yieldCurveVasicek = function(r, k, theta, sigma, maturity){
- n = length(r)
- yield = matrix(0, maturity, n)
- for(i in 1:n){
- for(t in 1:maturity){
- yield[t,i] = -log(vasicekPrice(r[i], k, theta, sigma, t))/t
- }
- }
- return(yield)
+# Ch 6 Prices, Discount Factors, and Arbitrage (Law of one Price)
+# The Cash Flows from Fixed-Rate Government Coupon Bonds, Discount Faors, Law of One Price
+# Arbitrage opportunity: trade that generates profits without any chance of losing money.
+# If there is a deviation from the law of one price, there exists an arbitrage opportunity.
+# In order to estimate the discount factor for a particular term gives the value today,
+# or the present alue of one unit of currency to be received at the end of that term.(Pg.129)
+
+#' Constructor for bond specification
+#'
+#' Created a bond object \code{bond.spec} with data for bond specification.
+#'
+#' @param time vector of sequence of coupon payments in years
+#' @param face face value of bond
+#' @param m compounding frequency
+#' @param couponRate rate the coupon pays
+#' @return a \code{bond} object with the bond data used for pricing
+#' @examples
+#' time = seq(from=0.5, to=2, by=0.5)
+#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+#' @author Thomas Fillebeen
+#' @export
+bondSpec = function(time=seq(from=0.5,to=2,by=0.5), face=100, m=2, couponRate=0.01){
+ if(!all(diff(time) == (1/m))) stop("misspecification of sequence of time and compounding frequency")
+ bond = list()
+ bond$m = m
+ bond$couponRate = couponRate
+ bond$face = face
+ bond$time = time
+ class(bond) = c("bond.spec", "bond")
+ return(bond)
+}
+
+#' To determine if user is specifying bond parameters correctly
+#'
+#' @param object a capm object created by \code{\link{bond.spec}}
+#' @author TF
+#' @export
+is.bond = function(object){
+ inherits(object, "bond.spec")
+}
+
+#' Estimate price of bond
+#'
+#' This function calculates the price of a fixed rate coupon bond given the
+#' discount curve and bond data. First it converts the discountCurve into CF
+#' @param bond a \code{bondSpec} object
+#' @param discountCurve vector of discount rates
+#' @return price of the bond
+#' @examples
+#' time = seq(from=0.5, to=2, by=0.5)
+#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
+#' price = bondPrice(bond,DF)
+#' @author Thomas Fillebeen
+#' @export
+bondPrice = function(bond, discountCurve){
+ if(!is.bond(bond)) stop("bond must be an object of class 'bond'")
+ # Number of periods in discount curve
+ nDC <- length(discountCurve)
+ m <- bond$m
+ couponRate <- bond$couponRate
+ face <- bond$face
+ time <- bond$time
+
+ couponAmount <- face * couponRate / m
+ cashflows <- rep(couponAmount, nDC)
+ cashflows[nDC] <- couponAmount + face
+ price <- sum(cashflows * discountCurve)
+ return(price)
+}
+
+#' Estimate discountFactor
+#'
+#' This function calculates the discountFactor (DF) given price
+#' and cashFlows.
+#' @param price of a bond
+#' @param cashFlow of a bond
+#' @return discount factors
+#' @examples
+#' cashFlow = rbind(c(100+(1+1/4)/2,0,0),c((4 +7/8)/2,100+(4+7/8)/2,0),c((4+1/2)/2,(4+1/2)/2,100+(4+1/2)/2))
+#' # Created Price of the bond
+#' price = matrix(c(100.550, 104.513, 105.856), ncol=1)
+#' DF = discountFactor(price, cashFlow)
+#' @author Thomas Fillebeen
+#' @export
+discountFactor = function(price, cashFlow){
+ DF = solve(cashFlow) %*% price
+ return(DF)
+}
+
+#' bondFullPrice
+#'
+#' Estimate price of bond w/ acrrued interest
+#' The present value of a bond's cash flows should be equated or
+#' compared with its full price, with the amount a pruchaser actually
+#' pays to purchase those cash flows. The flat price is p, accrued
+#' interest is AI, the present value of the cash flows by PV, and the
+#' full price by P:
+#' P=p+AI=PV
+#'
+#' This function calculates the price of a fixed rate coupon bond given coupon rate, yield,
+#' compoundPd, cashFlowPd, face value, previous coupon date, next coupon date.
+#' @param bond is a bondSpec object created by \code{\link{bondSpec}}
+#' @param yield is the yield on the bond
+#' @param cashFlowPd cash flow period
+#' @param t0 previous coupon date
+#' @param t1 next coupon period
+#' @param currentDate current date
+#' @examples
+#' t0 = as.Date("2013-08-15")
+#' t1 = as.Date("2014-02-15")
+#' tn = as.Date("2013-10-04")
+#' currentDate = tn
+#' bond = bondSpec(face=100, m=2, couponRate = 0.0475)
+#' y1 = 0.00961
+#' bondFullPrice(bond, y1, 8, t0, t1, tn)$clean
+#' bondFullPrice(bond, y1, 8, t0, t1, tn)$dirty
+#' bondFullPrice(bond, y1, 8, t0, t1, tn)$accruedInterest
+#' @return price of the bond: clean, dirty and accrued interest
+#' @author Thomas Fillebeen
+#' @export
+bondFullPrice = function(bond, yield, cashFlowPd, t0, t1, currentDate){
+ compoundPd = bond$m
+ face = bond$face
+ couponRate = bond$couponRate
+ # Apply a general dayCount (weekend included)
+ d1 = as.numeric(t1-currentDate)
+ d2 = as.numeric(t1-t0)
+ # Initialize
+ tmp = 0
+
+ #Will go through the loop only if the number of cashFlow periods are at least 2
+ if (cashFlowPd > 1){
+ for(k in 1:(cashFlowPd-1)){
+ tmp = tmp + ((couponRate / compoundPd * face) / ((1 + yield/compoundPd)^k))
+ }
+ }
+ # Calculate dirty price based on partial periods formula
+ dirtyP = (1 / ((1 + yield / compoundPd)^(d1/d2))) * (couponRate / compoundPd * face + tmp + face / ((1 + yield/compoundPd)^(cashFlowPd-1)))
+ # Calculate accruedInterest
+ aiDays = as.numeric(currentDate-t0)
+ couponDays = as.numeric(t1-t0)
+ ai = couponRate / compoundPd * face * aiDays / couponDays
+ cleanP = dirtyP - ai
+ return(list(dirty=dirtyP, clean=cleanP, accruedInterest=ai))
+}
+
+#' Estimate continuously conpounding rate to be used in term structure
+#'
+#' This function calculates the continuously compounding rate given an initial dataset
+#' with specific format, date of reference coumpounding frequency, and face value
+#' @param dat is a dataset with cusip, issueDate, MaturityDate, Name, Coupon, Bid/Ask
+#' @param intialDate is the date when the estimation should be conducted: date of reference
+#' @param m compounding frequency
+#' @param face face value
+#' @return continuously compounding rates
+#' @author Thomas Fillebeen
+#' @export
+compoundingRate = function(dat, initialDate=as.Date("1995-05-15"), m, face=100){
+ # Convert the dates to a date class
+ dat[, "IssueDate"] = as.Date(dat[, "IssueDate"], format="%m/%d/%Y")
+ dat[, "MaturityDate"] = as.Date(dat[, "MaturityDate"], format="%m/%d/%Y")
+ # Convert the coupon column to a numeric
+ # Vector of prices
+ price = (dat[, "Bid"] + dat[, "Ask"]) / 2
+
+ # Generate cash flow dates for each bond
+ bondData = list()
+ for(i in 1:nrow(dat)){
+ maturityDate = dat[i, "MaturityDate"]
+ # Intialize a new list for every price, coupon, coupon date.
+ bondData[[i]] = list()
+ # Store price and the number of the coupon
+ bondData[[i]]$price = price[i]
+ bondData[[i]]$coupon = dat[i, "Coupon"]
+ # Remove initialDate
+ tmpSeq <- seq(from=initialDate, to=maturityDate, by="3 months")
+ bondData[[i]]$couponDates = tmpSeq[-1]
+ tmpDates = bondData[[i]]$couponDates
+ tmpCoupons = vector("numeric", length(tmpDates))
+ for(j in 1:length(tmpDates)){
+ tmpCoupons[j] = bondData[[i]]$coupon / m * face
+ if(j == length(tmpDates)){
+ tmpCoupons[j] = face + bondData[[i]]$coupon / m * face
+ }
+ }
+ bondData[[i]]$cashFlow = tmpCoupons
+ }
+ # Create a matrix of cash flows
+ CF = matrix(0, length(price), length(price))
+ # Populate the CF matrix
+ for(i in 1:nrow(CF)){
+ tmp = bondData[[i]]$cashFlow
+ index = 1:length(tmp)
+ CF[i, index] = tmp
+ }
+ # Utilize the discountFactor function
+ DF = discountFactor(price,CF)
+
+ dates = bondData[[nrow(dat)]]$couponDates
+ years = vector("numeric", length(dates))
+ for(i in 1:length(years)){
+ years[i] = (as.numeric(strftime(dates[i], "%Y")) + as.numeric(strftime(dates[i], "%m"))/12) - (as.numeric(strftime(initialDate, "%Y")) + as.numeric(strftime(initialDate, "%m"))/12)
+ }
+ # Calculate continuously compounded rates from discount factors
+ ccRate = vector("numeric", length(years))
+ for(i in 1:length(ccRate)){
+ ccRate[i] = - log(DF[i]) / years[i]
+ }
+ rate = list()
+ rate$years = years
+ rate$ccRate = ccRate
+ return(rate)
+}
+
+#' Estimate spot and forward rates
+#'
+#' This function calculates the forward or forward rates given an discount factors
+#' and time increments
+#' @param time increments of time when discount factors are estimated
+#' @param DF discount factor for during time increments
+#' @examples
+#' DF = c(0.996489, 0.991306, 0.984484, 0.975616, 0.964519)
+#' time = seq(from=0.5, to=2.5, by=0.5)
+#' rates = spotForwardRates(time,DF)
+#' rates
+#' @author Thomas Fillebeen
+#' @export
+spotForwardRates = function(time, DF){
+ if(length(time) != length(DF)) stop("both time and DF parameter need to be of the same length")
+spotRates = matrix(0,length(time),1)
+for(i in 1:(length(time))){
+ spotRates[i] = (2-2*DF[i]^(1/(2*time[i]))) / DF[i]^(1/(2*time[i]))
+}
+
+forwardRates = matrix(0,length(time),1)
+forwardRates[1] = spotRates[1]
+for(j in 1:(length(time)-1)){
+ forwardRates[j+1] = (DF[j]/DF[j+1] - 1) *2
+}
+rates = cbind(spotRates, forwardRates)
+colnames(rates)= cbind("Spot","Forward")
+return(rates)
+}
+
+### Modelling a Zero-Coupon Bond (ZCB)
+#' There are three main types of yield curve shapes: normal, inverted and flat (or humped)
+#' Estimate Vasicek zero-coupon bond to be used in term structure
+#'
+#' This function calculates the Vasicek Price given an initial data calibration
+#' The function is a subfunction for yieldCurveVasicek
+#' @param r initial short rate
+#' @param k speed of reversion parameter
+#' @param theta long-term reversion yield
+#' @param sigma randomness parameter. Modelled after Brownan Motion
+#' @return t length of time modelled for
+#' @author Thomas Fillebeen
+#' @export
+vasicekPrice = function(r, k, theta, sigma, maturity){
+ mean = (1/k)*(1 - exp(-maturity*k))
+ variance = (theta - sigma^2/(2*k^2))*(maturity - mean) + (sigma^2)/(4*k)*mean^2
+ price = exp(-variance - mean*r)
+ return(price)
+ }
+
+#' Estimate Vasicek zero-coupon yield
+#'
+#' This function calculates the Vasicek yield given an initial data calibration
+#' @param r initial short rate
+#' @param k speed of reversion parameter
+#' @param theta long-term reversion yield
+#' @param sigma randomness parameter. Modelled after Brownan Motion
+#' @return t length of time modelled for
+#' @author Thomas Fillebeen
+#' @export
+yieldCurveVasicek = function(r, k, theta, sigma, maturity){
+ n = length(r)
+ yield = matrix(0, maturity, n)
+ for(i in 1:n){
+ for(t in 1:maturity){
+ yield[t,i] = -log(vasicekPrice(r[i], k, theta, sigma, t))/t
+ }
+ }
+ return(yield)
}
\ No newline at end of file
Modified: pkg/GARPFRM/R/riskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-11-15 19:20:31 UTC (rev 217)
+++ pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-11-15 20:10:31 UTC (rev 218)
@@ -1,242 +1,268 @@
-########## Hedge Section-Convexity and Duration##########
-#' Calculate the modified duration of a bond
-#'
-#' The function estimates modified duration of a fixed rate coupon bond
-#' given the discount curve and bond data. The modified duration is calculated
-#' using the continuously compounded yield
-#'
-#' @param bond a \code{bond} object in discountFactorArbitrage
-#' @param discountCurve vector of discount rates
-#' @param percentChangeYield optional elasticity measure
-#' @return duration of the bond
-#' @examples
-#' time = seq(from=0.5, to=2, by=0.5)
-#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
-#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
-#' mDuration = bondDuration(bond,DF)
-#' @author Thomas Fillebeen
-#' @export
-bondDuration <- function(bond, discountCurve, percentChangeYield = 0){
- # Get data from the bond and discount curve
- nDC = length(discountCurve)
- m = bond$m
- couponRate = bond$couponRate
- face = bond$face
- time = bond$time
- # Calculate the ytm
- ytm = bondYTM(bond=bond, discountCurve=discountCurve) + percentChangeYield
- # Convert to continuously compounded rate
- y_c = m * log(1 + ytm / m)
- # Get the cashflows of coupon amounts and face value
- couponAmount = face * couponRate / m
- cashflows = rep(couponAmount, nDC)
- cashflows[nDC] = couponAmount + face
- # Calculate the price based on the continuously compounded rate
- price = sum(cashflows * exp(-y_c * time))
- # Calculate the duration
- duration = sum(-time * cashflows * exp(-y_c * time)) / -price
- return(duration)
-}
-
-#' Calculate the convexity of a fixed rate coupon bond
-#'
-#' This function estimates the convexity of a fixed rate coupon bond
-#' given the discount curve and bond data.
-#'
-#' @param bond a \code{bond} object in discountFactorArbitrage
-#' @param discountCurve vector of discount rates
-#' @return convexity of the bond
-#' @examples
-#' time = seq(from=0.5, to=2, by=0.5)
-#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
-#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
-#' convexity = bondConvexity(bond,DF)
-#' @author Thomas Fillebeen
-#' @export
-bondConvexity <- function(bond, discountCurve){
- # Get data from the bond and discount curve
- nDC = length(discountCurve)
- m = bond$m
- couponRate = bond$couponRate
- face = bond$face
- time = bond$time
- # Get the cashflows of coupon amounts and face value
- couponAmount = face * couponRate / m
- cashflows = rep(couponAmount, nDC)
- cashflows[nDC] = couponAmount + face
- # The price is the sum of the discounted cashflows
- price = sum(discountCurve * cashflows)
- weights = (discountCurve * cashflows) / price
- convexity = sum(weights * time^2)
- return(convexity)
-}
-
-#' Calculate the yield to maturity of a bond
-#'
-#' This function calculates the yield to maturity of a fixed rate coupon bond
-#' given the discount curve and bond data.
-#'
-#' @param bond a \code{bond} object
-#' @param discountCurve vector of discount rates
-#' @return yield to maturity of the bond
-#' @examples
-#' time = seq(from=0.5, to=2, by=0.5)
-#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
-#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
-#' bondYTM(bond,DF)
-#' @author Thomas Fillebeen
-#' @export
-bondYTM <- function(bond, discountCurve){
- # First step is to calculate the price based on the discount curve
- price <- bondPrice(bond=bond, discountCurve=discountCurve)
-
- # Get the data from the bond object
- m <- bond$m
- couponRate <- bond$couponRate
- face <- bond$face
- time <- bond$time
-
- # Use optimize to solve for the yield to maturity
- tmp <- optimize(ytmSolve, interval=c(-1,1), couponRate=couponRate, m=m, nPayments=length(time), face=face, targetPrice=price, tol=.Machine$double.eps)
- ytm <- tmp$minimum
- return(ytm)
-}
-
-#' Solve for the yield to maturity of a bond
-#'
-#' This function solves for the yield to maturity of a fixed rate coupon bond
-#' given the discount curve and bond data.
-#'
-#' @param ytm yield to maturity
-#' @param couponRate coupon rate
-#' @param m compounding frequency
-#' @param nPayments is the number of payments
-#' @param face is the face value
-#' @param targetPrice is the price of the bond
-#' @return Absolute value of difference between the price and the present value
-#' @author Thomas Fillebeen
-#' @export
-ytmSolve <- function(ytm, couponRate, m, nPayments, face, targetPrice){
- C <- face * couponRate / m
- tmpPrice <- 0
- for(i in 1:nPayments){
- tmpPrice <- tmpPrice + C / ((1 + (ytm / m))^i)
- }
- tmpPrice <- tmpPrice + face / (1 + ytm / m)^nPayments
- return(abs(tmpPrice - targetPrice))
-}
-
-
-############ Hedge section-Empirical###############
-
-#' Estimate the delta hedge of for a bond
-#'
-#' This function estimates the delta for hedging a particular bond
-#' given bond data
-#'
-#' @param regressand a \code{bond} object in discountFactorArbitrage
-#' @param regressor the right hand side
-#' @return delta of the hedge
-#' @examples
-#' # Load Data for historcal analysis tools
-#' data(crsp.short)
-#' data = largecap.ts[,2:6]
-#' head(data)
-#' # Empirical application: Linear hedge estimation
-#' # OLS Level-on-Level regression
-#' deltas = linearHedge(data[,1],data[,2:5])
-#' # Insert the normalized hedged contract versus hedgeable contract value
-#' deltas = c(1,deltas)
-#' # In sample illustration: random, mean reverting spreads
-# ' hedgedInstruments = data%*%deltas
-#' @author Thomas Fillebeen
-#' @export
-linearHedge <- function(regressand, regressor){
- deltas = matrix(0,nrow=1,ncol= ncol(regressor))
- reg = lm(regressand ~ regressor)
- deltas = -coef(reg)[seq(2,ncol(data),1)]
- return(deltas)
-}
-
-#' Estimate PCA loadings and create a PCA object
-#'
-#' This function estimates the delta for hedging a particular bond
-#' given bond data
-#'
-#' @param data time series data
-#' @param nfactors number of components to extract
-#' @param rotate "none", "varimax", "quatimax", "promax", "oblimin", "simplimax", and "cluster" are possible rotations/transformations of the solution.
-#' @return pca object loadings
-#' @author Thomas Fillebeen
-#' @export
-PCA <- function(data, nfactors, rotate = "none"){
- stopifnot("package:psych" %in% search() || require("psych", quietly = TRUE))
-
- pca = principal(data, nfactors, rotate="none")
- class(pca) <- c("PCA","psych", "principal")
- return(pca)
-}
-
-#' Retrieve PCA loadings
-#'
-#' @param object is a pca object created by \code{\link{PCA}}
-#' @author Thomas Fillebeen
-#' @export
-getLoadings <- function(object){
-loadings = object$loadings
- return(loadings)
-}
-
-#' Retrieve PCA weights
-#'
-#' @param object is a pca object created by \code{\link{PCA}}
-#' @author Thomas Fillebeen
-#' @export
-getWeights <- function(object){
- weights = object$weight
- return(weights)
-}
-
-#' Plotting method for PCA
-#'
-#' Plot a fitted PCA object
-#'
-#' @param x a PCA object created by \code{\link{PCA}}
-#' @param y not used
-#' @param number specify the nunber of loadings
-#' @param \dots passthrough parameters to \code{\link{plot}}.
-#' @param main a main title for the plot
-#' @param separate if TRUE plot of same, and if FALSE plot separately
-#' @author Thomas Fillebeen
-#' @method plot PCA
-#' @S3method plot PCA
-plot.PCA <- function(x, y, ..., main="Beta from PCA regression",separate=TRUE){
- if(ncol(x$loading)> 3) warning("Only first 3 loadings will be graphically displayed")
- # Plot the first three factors
- if (ncol(x$loading) >= 3){
- if(!separate){
- plot(x$loading[,1], type="l", main = main,
- xlab="Maturity/Items", ylab="Loadings")
- lines(x$loading[,2], col="blue",lty=2)
- lines(x$loading[,3], col="red",lty=2)
- legend("topleft",legend=c("PCA1","PCA2","PCA3"),bty="n",lty=c(1,2,2),col=c("black","blue","red"), cex=0.8)
- }else{
- plot.zoo(pca$loading[,1:3], type="l", main = main,
- xlab="Maturity/Items")
- }
- }else if(ncol(x$loading) == 2){
- if(!separate){
- plot(x$loading[,1], type="l", main = main,
- xlab="Maturity/Items", ylab="Loadings")
- lines(x$loading[,2], col="blue",lty=2)
- legend("topleft",legend=c("PCA1","PCA2"),bty="n",lty=c(1,2),col=c("black","blue"), cex=0.8)
- }else{
- plot.zoo(pca$loading[,1:2], type="l", main = main,
- xlab="Maturity/Items")
- }
- }else{
- plot(x$loading[,1], type="l", main = main,
- xlab="Maturity/Items", ylab="Loadings")
- legend("topleft",legend=c("PCA1"),bty="n",lty=c(1),col=c("black"), cex=0.8)
- }
+########## Hedge Section-Convexity and Duration##########
+#' Calculate the macaulay duration of a bond
+#'
+#' The function estimates maculay duration of a fixed rate coupon bond
+#' given the discount curve and bond data. The macaulay duration is calculated
+#' using the continuously compounded yield
+#'
+#' @param bond a \code{bond} object in discountFactorArbitrage
+#' @param discountCurve vector of discount rates
+#' @param percentChangeYield optional elasticity measure
+#' @return duration of the bond
+#' @examples
+#' time = seq(from=0.5, to=2, by=0.5)
+#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
+#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+#' mDuration = bondDuration(bond,DF)
+#' @author Thomas Fillebeen
+#' @export
+bondDuration <- function(bond, discountCurve, percentChangeYield = 0){
+ # Get data from the bond and discount curve
+ nDC = length(discountCurve)
+ m = bond$m
+ couponRate = bond$couponRate
+ face = bond$face
+ time = bond$time
+ # Calculate the ytm
+ ytm = bondYTM(bond=bond, discountCurve=discountCurve) + percentChangeYield
+ # Convert to continuously compounded rate
+ y_c = m * log(1 + ytm / m)
+ # Get the cashflows of coupon amounts and face value
+ couponAmount = face * couponRate / m
+ cashflows = rep(couponAmount, nDC)
+ cashflows[nDC] = couponAmount + face
+ # Calculate the price based on the continuously compounded rate
+ price = sum(cashflows * exp(-y_c * time))
+ # Calculate the duration
+ duration = sum(-time * cashflows * exp(-y_c * time)) / -price
+ return(duration)
+}
+
+#' Calculate the modified duration of a bond
+#'
+#' The function estimates modified duration of a fixed rate coupon bond
+#' given the discount curve and bond data. The modified duration is calculated
+#' using the continuously compounded yield
+#'
+#' @param bond a \code{bond} object in discountFactorArbitrage
+#' @param discountCurve vector of discount rates
+#' @param percentChangeYield optional elasticity measure
+#' @return modified duration of the bond
+#' @examples
+#' time = seq(from=0.5, to=2, by=0.5)
+#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
+#' bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+#' mDuration = Modified.bondDuration(bond,DF)
+#' @author Jaiganesh Prabhakaran
+#' @export
+Modified.bondDuration <- function(bond, discountCurve, percentChangeYield = 0){
+ #Get the Duration using bondDuration Function
+ duration = bondDuration(bond, discountCurve, percentChangeYield)
+ #Calculating yield to maturity using bondYTM Function
+ ytm = bondYTM(bond,df)
+ mduration = duration/(1+ytm/bond$m)
+ return(mduration)
+}
+
+#' Calculate the convexity of a fixed rate coupon bond
+#'
+#' This function estimates the convexity of a fixed rate coupon bond
+#' given the discount curve and bond data.
+#'
+#' @param bond a \code{bond} object in discountFactorArbitrage
+#' @param discountCurve vector of discount rates
+#' @return convexity of the bond
+#' @examples
+#' time = seq(from=0.5, to=2, by=0.5)
+#' DF = rbind(0.968,0.9407242,0.9031545,0.8739803)
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/uwgarp -r 218
More information about the Uwgarp-commits
mailing list