[Uwgarp-commits] r179 - in pkg/GARPFRM: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 11 03:24:20 CEST 2014


Author: tfillebeen
Date: 2014-06-11 03:24:18 +0200 (Wed, 11 Jun 2014)
New Revision: 179

Modified:
   pkg/GARPFRM/R/riskMetricsAndHedges.R
   pkg/GARPFRM/sandbox/principleComponent.R
Log:
linear hedge

Modified: pkg/GARPFRM/R/riskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/R/riskMetricsAndHedges.R	2014-06-10 18:34:49 UTC (rev 178)
+++ pkg/GARPFRM/R/riskMetricsAndHedges.R	2014-06-11 01:24:18 UTC (rev 179)
@@ -1,5 +1,4 @@
-# Convexity and Duration
-
+########## Convexity and Duration##########
 #' Calculate the modified duration of a bond
 #' 
 #' The function estimates modified duration of a fixed rate coupon bond 
@@ -109,22 +108,22 @@
 }
 
 
-############Hedge section
+############ Hedge section###############
 
 #' Estimate the delta hedge of for a bond
 #' 
 #' This function estimates the delta for hedging a particular bond 
 #' given bond data
 #' 
-#' @param bond a \code{bond} object in discountFactorArbitrage
+#' @param regressand a \code{bond} object in discountFactorArbitrage
+#' @param regressor the right hand side
 #' @return delta of the hedge
 #' @export
-linearHedge <- function(bond){
-  ### Write body####
-  
-  
-  
-  return(delta)
+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 creat PCA object
@@ -139,7 +138,7 @@
 #' @export
 PCA <- function(data, nfactors, rotate = "none"){
   pca = principal(data, nfactors, rotate="none")
-  class(pca) <- c("psych", "principal","PCA")
+  class(pca) <- c("PCA","psych", "principal")
   return(pca)
 }
 

Modified: pkg/GARPFRM/sandbox/principleComponent.R
===================================================================
--- pkg/GARPFRM/sandbox/principleComponent.R	2014-06-10 18:34:49 UTC (rev 178)
+++ pkg/GARPFRM/sandbox/principleComponent.R	2014-06-11 01:24:18 UTC (rev 179)
@@ -6,6 +6,15 @@
 data = largecap.ts[,2:6]
 head(data)
 
+# Linear hedge estimation
+deltas = linearHedge(data[,1],data[,2:5])
+# Insert the hedgeable contract value
+deltas = c(1,deltas)
+
+# In sample illustration: random noise
+hedgedInstruments = data%*%deltas
+plot(hedgedInstruments, type="l", main = "Hedged Price Difference", xlab="Time",ylab="Difference")
+
 # Retain components that combined account for x% of the cumulative variance
 pca = PCA(data, nfactors = 3, rotate="none")
 summary(pca)
@@ -37,4 +46,4 @@
   # 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
+scores = pca.r$scores
\ No newline at end of file



More information about the Uwgarp-commits mailing list