[Uwgarp-commits] r175 - in pkg/GARPFRM: . R sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 10 00:34:14 CEST 2014
Author: tfillebeen
Date: 2014-06-10 00:34:13 +0200 (Tue, 10 Jun 2014)
New Revision: 175
Modified:
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/discountFactorArbitrage.R
pkg/GARPFRM/R/riskMetricsAndHedges.R
pkg/GARPFRM/sandbox/principleComponent.R
pkg/GARPFRM/sandbox/test_discountFactorArbitrage.R
Log:
bond object used
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-06-09 21:12:10 UTC (rev 174)
+++ pkg/GARPFRM/NAMESPACE 2014-06-09 22:34:13 UTC (rev 175)
@@ -28,6 +28,8 @@
export(EWMA)
export(backtestVaR)
export(backtestVaR.GARCH)
+export(bondConvexity)
+export(bondDuration)
export(bondFullPrice)
export(bondPrice)
export(bondSpec)
Modified: pkg/GARPFRM/R/discountFactorArbitrage.R
===================================================================
--- pkg/GARPFRM/R/discountFactorArbitrage.R 2014-06-09 21:12:10 UTC (rev 174)
+++ pkg/GARPFRM/R/discountFactorArbitrage.R 2014-06-09 22:34:13 UTC (rev 175)
@@ -16,7 +16,7 @@
#' @return a \code{bond} object with the bond data used for pricing
#' @author TF
#' @export
-bondSpec = function(time, face=100, m=2, couponRate=0.01){
+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
@@ -79,18 +79,19 @@
#'
#' 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 couponRate is the coupon rate
+#' @param bond is a bondSpec object
#' @param yield is the yield on the bond
-#' @param compoundPd coumpounding period
#' @param cashFlowPd cash flow period
-#' @param face face value
#' @param t0 previous coupon date
#' @param t1 next coupon period
#' @param currentDate current date
#' @return price of the bond: clean, dirty and accrued interest
#' @author TF
#' @export
-bondFullPrice = function(couponRate, yield, compoundPd, cashFlowPd, face=100, t0, t1, currentDate){
+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)
@@ -120,7 +121,7 @@
#' @return continuously compounding rates
#' @author TF
#' @export
-compoundingRate = function(dat, initialDate=as.Date("2000-05-15"), m, face=100){
+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")
Modified: pkg/GARPFRM/R/riskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-06-09 21:12:10 UTC (rev 174)
+++ pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-06-09 22:34:13 UTC (rev 175)
@@ -1,4 +1,74 @@
# Convexity and Duration
-# linear hedge
+#' Calculate the modified duration of a bond
+#'
+#' This function calculates the 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
+#' @param discountCurve vector of discount rates
+#' @return duration of the bond
+#' @export
+bondDuration <- 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
+
+ # Calculate the ytm
+ ytm <- bondYTM(bond=bond, discountCurve=discountCurve)
+
+ # 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 calculates the convexity 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 convexity of the bond
+#' @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
+
+ # weights <- ((discountCurve * cashflows) / price) * time^2
+ convexity <- sum(weights * time^2)
+ return(convexity)
+}
+
+#### linear hedge####
+
Modified: pkg/GARPFRM/sandbox/principleComponent.R
===================================================================
--- pkg/GARPFRM/sandbox/principleComponent.R 2014-06-09 21:12:10 UTC (rev 174)
+++ pkg/GARPFRM/sandbox/principleComponent.R 2014-06-09 22:34:13 UTC (rev 175)
@@ -2,6 +2,7 @@
library(psych)
library(GPArotation)
+data(crsp.short)
data = largecap.ts[,2:6]
head(data)
Modified: pkg/GARPFRM/sandbox/test_discountFactorArbitrage.R
===================================================================
--- pkg/GARPFRM/sandbox/test_discountFactorArbitrage.R 2014-06-09 21:12:10 UTC (rev 174)
+++ pkg/GARPFRM/sandbox/test_discountFactorArbitrage.R 2014-06-09 22:34:13 UTC (rev 175)
@@ -5,37 +5,39 @@
# The Cash Flows from Fixed-Rate Government Coupon Bonds
# Discount Factors and the Law of One Price
-
# Initialize: The Cash Flows from Fixed-Rate: treasury bonds ticking in quarters
cashFlow = rbind(c(100, 0, 0, 0), c(2 + 7/8, 102 + 7/8, 0, 0), c(3 + 3/4, 3 + 3/4, 103 + 3/4, 0), c(3 + 3/4, 3 + 3/4, 3 + 3/4, 103 + 3/4))
# Initialize: Price of the bond
price <- matrix(c(96.8, 99.56, 100.86, 101.22), ncol=1)
+
# Estimate the Discount Factors (DF)
DF = discountFactor(price , cashFlow)
-# Define a bond object to be used throughout the analysis
-# Estimate bondPrice############
-# example, a 2 year bond with semiannual payments
+
+# Estimate bondPrice
+# Choose a 2 year bond with semiannual payments
time = seq(from=0.5, to=2, by=0.5)
+# First define a bond object to be used throughout the analysis
+bond = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+price = bondPrice(bond,DF)
-
-
# Appliation: Idiosyncratic Pricing of US Treasury Notes and Bonds
t0 = as.Date("2013-08-15")
t1 = as.Date("2014-02-15")
tn = as.Date("2013-10-04")
currentDate = tn
-# Apply a yield on 4.75% bond
+# Apply a coupon rate of 4.75% bond and create a bond object
+bond = bondSpec(face=100, m=2, couponRate = 0.0475)
y1 = 0.00961
-bondFullPrice(0.0475, y1, 2, 8, face=100, t0, t1, tn)$clean
-bondFullPrice(0.0475, y1, 2, 8, face=100, t0, t1, tn)$dirty
-bondFullPrice(0.0475, y1, 2, 8, face=100, t0, t1, tn)$accruedInterest
+bondFullPrice(bond, y1, 8, t0, t1, tn)$clean
+bondFullPrice(bond, y1, 8, t0, t1, tn)$dirty
+bondFullPrice(bond, y1, 8, t0, t1, tn)$accruedInterest
# Estimating the term structure: compounded rates from discount factors
# Ulitzing data in the following format: Cusip, IssueDate, MaturityDate, Name, Coupon, Bid/Ask
head(dat)
-ccRate = compoundingRate(dat, initialDate=as.Date("2000-05-15"), m=4, face=100)
+ccRate = compoundingRate(dat, initialDate=as.Date("1995-05-15"), m=4, face=100)
years = ccRate$years
rate = ccRate$ccRate
More information about the Uwgarp-commits
mailing list