[Uwgarp-commits] r201 - in pkg/GARPFRM: R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 1 16:04:40 CEST 2014
Author: tfillebeen
Date: 2014-07-01 16:04:39 +0200 (Tue, 01 Jul 2014)
New Revision: 201
Added:
pkg/GARPFRM/demo/RiskMetricsAndHedges.R
Modified:
pkg/GARPFRM/R/discountFactorArbitrage.R
pkg/GARPFRM/R/riskMetricsAndHedges.R
pkg/GARPFRM/man/PCA.Rd
pkg/GARPFRM/man/bondFullPrice.Rd
pkg/GARPFRM/man/bondPrice.Rd
pkg/GARPFRM/man/discountFactor.Rd
pkg/GARPFRM/man/getLoadings.Rd
pkg/GARPFRM/man/getWeights.Rd
pkg/GARPFRM/man/linearHedge.Rd
pkg/GARPFRM/man/plot.PCA.Rd
Log:
examples, references and demo for hedge
Modified: pkg/GARPFRM/R/discountFactorArbitrage.R
===================================================================
--- pkg/GARPFRM/R/discountFactorArbitrage.R 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/R/discountFactorArbitrage.R 2014-07-01 14:04:39 UTC (rev 201)
@@ -43,7 +43,7 @@
#'
#' 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{discountFactorArbitrage} object
+#' @param bond a \code{bondSpec} object
#' @param discountCurve vector of discount rates
#' @return price of the bond
#' @examples
@@ -72,11 +72,14 @@
#'
#' This function calculates the discountFactor (DF) given price
#' and cashFlows.
-#' @param bond a \code{discountFactorArbitrage} object
-#' @param price of a bond
-#' @return cashFlow of a bond
+#' @param price of a bond
+#' @param cashFlow of a bond
+#' @return discount factors
#' @examples
-#' solve(cashFlow) %*% price
+#' 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){
@@ -92,11 +95,12 @@
#' 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
+#' @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
Modified: pkg/GARPFRM/R/riskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/R/riskMetricsAndHedges.R 2014-07-01 14:04:39 UTC (rev 201)
@@ -134,6 +134,18 @@
#' @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){
@@ -151,6 +163,15 @@
#' @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.
+#'@examples
+#' data(crsp.short)
+#' data = largecap.ts[,2:6]
+#' pca = PCA(data, nfactors = 3, rotate="none")
+#' summary(pca)
+#' # Retrieve Loadings and if loading is insignificant then omit
+#' getLoadings(pca)
+#' # Retrieve Weights
+#' getWeights(pca)
#' @return pca object loadings
#' @author Thomas Fillebeen
#' @export
@@ -162,7 +183,7 @@
#' Retrieve PCA loadings
#'
-#' @param object is a pca object
+#' @param object is a pca object created by \code{\link{PCA}}
#' @author Thomas Fillebeen
#' @export
getLoadings <- function(object){
@@ -172,7 +193,7 @@
#' Retrieve PCA weights
#'
-#' @param object is a pca object
+#' @param object is a pca object created by \code{\link{PCA}}
#' @author Thomas Fillebeen
#' @export
getWeights <- function(object){
@@ -184,7 +205,7 @@
#'
#' Plot a fitted PCA object
#'
-#' @param x a PCA object created.
+#' @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}}.
Added: pkg/GARPFRM/demo/RiskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/demo/RiskMetricsAndHedges.R (rev 0)
+++ pkg/GARPFRM/demo/RiskMetricsAndHedges.R 2014-07-01 14:04:39 UTC (rev 201)
@@ -0,0 +1,85 @@
+library(GARPFRM)
+library(psych)
+library(GPArotation)
+
+# Applying duration as a hedge
+# #1: Initialize the Discount Factors (DF)
+DF_1 = rbind(0.968,0.9407242,0.9031545,0.8739803)
+# Choose a 2 year bond with semiannual payments to match number of bond prices and CFs
+time = seq(from=0.5, to=2, by=0.5)
+# First define a bond object to be used throughout the analysis, where m is the compound frequency
+bond_1 = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+# Duration measures the effect of a small parallel shift in the yield curve
+mDuration_1 = bondDuration(bond_1,DF_1)
+# # 2: Initialize the Discount Factors (DF)
+DF_2 = rbind(0.95434,0.9434,0.917232,0.89,0.85678,0.8396,0.81242,0.7921,0.7693,0.7473,0.7298,0.7050)
+# Choose a 2 year bond with semiannual payments to match number of bond prices and CFs
+time = seq(from=0.5, to=6, by=0.5)
+# First define a bond object to be used throughout the analysis, where m is the compound frequency
+bond_2 = bondSpec(time, face=100, m=2, couponRate = 0.0475)
+# Duration measures the effect of a small parallel shift in the yield curve
+mDuration_2 = bondDuration(bond_2,DF_2)
+# Hedging Ratio: for every bond_2 used there needs to be hedgeRatio amount of bond_1s
+# If you hold the portfolio bond_2, you want to sell hedgeRatio units of bonds
+hedgeRatio = - mDuration_2/mDuration_1
+
+
+# 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
+plot(hedgedInstruments, type="l", main = "Hedged Price Difference: Level", xlab="Time",ylab="Difference")
+
+# OLS Change-on-Change regression
+deltas = linearHedge(diff(data[,1]),diff(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
+plot(hedgedInstruments, type="l", main = "Hedged Price Difference: Change", xlab="Time",ylab="Difference")
+
+
+# Have a single, empirical description of the behavior of the term structure that can be applied across all
+# assets. Principal Compnents (PCs) provide such an emperical description
+# Retain components that combined account for x% of the cumulative variance
+pca = PCA(data, nfactors = 3, rotate="none")
+summary(pca)
+
+# Retrieve Loadings and if loading is insignificant then omit
+getLoadings(pca)
+
+# Retrieve Weights
+getWeights(pca)
+
+## Structural Equation Modelling
+# Determining the appropriate number of factors
+# A graphic representation of the 3 oblique factors
+fa.diagram(pca)
+
+# Alternative to determining the number of factors to compare the solution
+# to random data with the same properties as the real data set.
+fa.parallel(data)
+
+# Plot up to the first three factors
+plot(pca)
+pca = PCA(data, nfactors = 2, rotate="none")
+plot(pca)
+
+# Creating factor scores: Linear composite of the weighted observed variables
+ # Determine weights
+ # Multiply variable for each observation by these weights
+ # Sum the products
+pca.r = principal(data, nfactors=2, rotate="varimax", scores=T)
+scores = pca.r$scores
+plot(pca$scores[,1],pca$scores[,2], xlab="PCA1", ylab="PCA2", main = "Scores: Observable Pattern")
\ No newline at end of file
Modified: pkg/GARPFRM/man/PCA.Rd
===================================================================
--- pkg/GARPFRM/man/PCA.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/PCA.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -19,6 +19,16 @@
This function estimates the delta for hedging a particular bond
given bond data
}
+\examples{
+data(crsp.short)
+data = largecap.ts[,2:6]
+pca = PCA(data, nfactors = 3, rotate="none")
+summary(pca)
+# Retrieve Loadings and if loading is insignificant then omit
+getLoadings(pca)
+# Retrieve Weights
+getWeights(pca)
+}
\author{
Thomas Fillebeen
}
Modified: pkg/GARPFRM/man/bondFullPrice.Rd
===================================================================
--- pkg/GARPFRM/man/bondFullPrice.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/bondFullPrice.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,7 +6,7 @@
bondFullPrice(bond, yield, cashFlowPd, t0, t1, currentDate)
}
\arguments{
-\item{bond}{is a bondSpec object}
+\item{bond}{is a bondSpec object created by \code{\link{bondSpec}}}
\item{yield}{is the yield on the bond}
@@ -28,9 +28,10 @@
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
}
\details{
+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.
}
Modified: pkg/GARPFRM/man/bondPrice.Rd
===================================================================
--- pkg/GARPFRM/man/bondPrice.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/bondPrice.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,7 +6,7 @@
bondPrice(bond, discountCurve)
}
\arguments{
-\item{bond}{a \code{discountFactorArbitrage} object}
+\item{bond}{a \code{bondSpec} object}
\item{discountCurve}{vector of discount rates}
}
Modified: pkg/GARPFRM/man/discountFactor.Rd
===================================================================
--- pkg/GARPFRM/man/discountFactor.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/discountFactor.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,19 +6,22 @@
discountFactor(price, cashFlow)
}
\arguments{
-\item{bond}{a \code{discountFactorArbitrage} object}
+\item{price}{of a bond}
-\item{price}{of a bond}
+\item{cashFlow}{of a bond}
}
\value{
-cashFlow of a bond
+discount factors
}
\description{
This function calculates the discountFactor (DF) given price
and cashFlows.
}
\examples{
-solve(cashFlow) \%*\% price
+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
Modified: pkg/GARPFRM/man/getLoadings.Rd
===================================================================
--- pkg/GARPFRM/man/getLoadings.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/getLoadings.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,7 +6,7 @@
getLoadings(object)
}
\arguments{
-\item{object}{is a pca object}
+\item{object}{is a pca object created by \code{\link{PCA}}}
}
\description{
Retrieve PCA loadings
Modified: pkg/GARPFRM/man/getWeights.Rd
===================================================================
--- pkg/GARPFRM/man/getWeights.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/getWeights.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,7 +6,7 @@
getWeights(object)
}
\arguments{
-\item{object}{is a pca object}
+\item{object}{is a pca object created by \code{\link{PCA}}}
}
\description{
Retrieve PCA weights
Modified: pkg/GARPFRM/man/linearHedge.Rd
===================================================================
--- pkg/GARPFRM/man/linearHedge.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/linearHedge.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -17,6 +17,18 @@
This function estimates the delta for hedging a particular bond
given bond data
}
+\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
+}
\author{
Thomas Fillebeen
}
Modified: pkg/GARPFRM/man/plot.PCA.Rd
===================================================================
--- pkg/GARPFRM/man/plot.PCA.Rd 2014-06-30 00:27:39 UTC (rev 200)
+++ pkg/GARPFRM/man/plot.PCA.Rd 2014-07-01 14:04:39 UTC (rev 201)
@@ -6,7 +6,7 @@
\method{plot}{PCA}(x, y, ..., main = "Beta from PCA regression")
}
\arguments{
-\item{x}{a PCA object created.}
+\item{x}{a PCA object created by \code{\link{PCA}}}
\item{y}{not used}
More information about the Uwgarp-commits
mailing list