diff options
Diffstat (limited to 'R/cds_functions_generic.R')
| -rw-r--r-- | R/cds_functions_generic.R | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index 2f4fbf7a..f5afd0f5 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -524,13 +524,14 @@ bondhazardrate.shaped <- function(collateral, shape, R=0.4, alpha=0.25, beta=15, return( shapedtodpc(cs, sc, startdate) )
}
-tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE){
+tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE, forward.tweak=1){
## tweak a portfolio of creditcurves
## if multiplicative is TRUE apply a multiplicative tweak
## otherwise apply an additive one
if(multiplicative){
r <- lapply(portfolio, function(x) {
- x@curve@hazardrates <- x@curve@hazardrates * (1+epsilon)
+ x@curve@hazardrates[forward.tweak:length(x@curve@hazardrates)] <-
+ x@curve@hazardrates[forward.tweak:length(x@curve@hazardrates)] * (1+epsilon)
x
})
}else{
@@ -543,17 +544,24 @@ tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE){ return( r )
}
-indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE){
+indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE,
+ maturity=index$maturity, forward.tweak=1){
## computes the intrinsic price of a portfolio of cds
+ ## If maturity is provided, only computes the pv up to that point
+ ## (Say we compute the 3 year pv based on 5 year curves
+ ## forward.tweak only makes sense if epsilon is non zero
+ ## and will teak the curves starting from forward.index
if(epsilon!=0){
- portfolio <- tweakportfolio(index$portfolio, epsilon)
+ portfolio <- tweakportfolio(index$portfolio, epsilon, forward.tweak=forward.tweak)
}else{
portfolio <- index$portfolio
}
startdate <- tradedate + 1
- cl.list <- unlist(lapply(portfolio, function(x){couponleg(index$cs, x@curve, startdate)}))
- pl.list <- unlist(lapply(portfolio, function(x){defaultleg(index$cs, x@curve, x@recovery, startdate)}))
- accrued <- cdsAccrued(tradedate, index$spreadref)
+ cs <- index$cs[index$cs$dates<=maturity,]
+ cl.list <- unlist(lapply(portfolio, function(x){couponleg(cs, x@curve, startdate)}))
+ pl.list <- unlist(lapply(portfolio, function(x){defaultleg(cs, x@curve, x@recovery, startdate)}))
+ spread <- index$quotes$spread[index$quotes$maturity==maturity]
+ accrued <- cdsAccrued(tradedate, spread)
r <- list(cl = mean(cl.list) - accrued, pl = mean(pl.list), bp = 1+mean(cl.list-pl.list))
if(clean){
r$bp <- r$bp-accrued
@@ -561,13 +569,13 @@ indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE){ return(r)
}
-indexduration <- function(portfolio, index){
+indexduration <- function(index){
## compute the duration of a portfolio of survival curves
- durations <- unlist(lapply(portfolio, function(x){cdsduration(x@curve, index$maturity)}))
+ durations <- unlist(lapply(index$portfolio, function(x){cdsduration(x@curve, index$maturity)}))
return( mean(durations) )
}
-indexspread <- function(portfolio, index){
+indexspread <- function(index){
## computes the spread of a portfolio of survival curves
## S <- 0
## d <- rep(0, length(portfolio))
@@ -575,10 +583,19 @@ indexspread <- function(portfolio, index){ ## d[i] <- cdsduration(portfolio[[i]]@curve, index$maturity)
## S <- S + d[i] * cdsspread(portfolio[[i]]@curve, index$maturity, portfolio[[i]]@recovery)
## }
- S <- index$coupon-(indexpv(portfolio, index)$bp-1)/indexduration(portfolio, index)
+ S <- index$spreadref-(indexpv(index)$bp-1)/indexduration(index)
return(S)
}
+indextheta <- function(index, tradedate=Sys.Date()){
+ current.pv <- indexpv(index, tradedate=tradedate)$bp
+ newmaturity <- index$cs$dates[nrow(orig.cs)-4]
+ forward.pv <- indexpv(index, tradedate=tradedate, maturity=newmaturity)$bp
+ theta <- forward.pv-current.pv+index$spreadref
+ return( theta )
+}
+
+
portfoliospread <- function(portfolio, maturity){
## computes the spread of a portfolio defined by notionals and survivalcurves
## for a given maturity.
@@ -612,11 +629,19 @@ portfolioduration <- function(portfolio, maturity){ tweakcurves <- function(index, tradedate=Sys.Date()){
## computes the tweaking factor
- epsilon <- 0
- f <- function(epsilon, ...){
- abs(indexpv(index, epsilon, tradedate)$bp - index$priceref)
+ epsilon <- rep(0, nrow(index$quotes))
+ for(i in 1:nrow(index$quotes)){
+ if(i==1){
+ forward.tweak <- 1
+ }else{
+ forward.tweak <- which.min(index$cs$dates>index$quotes$maturity[i-1])
+ }
+ f <- function(epsilon, ...){
+ abs(indexpv(index, epsilon, tradedate, maturity=index$quotes$maturity[i],
+ forward.tweak=forward.tweak)$bp - index$quotes$price[i])
+ }
+ epsilon[i] <- optimize(f, c(-0.15, 0.15), index, tol=1e-6)$minimum
}
- epsilon <- optimize(f, c(-0.15, 0.15), index, tol=1e-6)$minimum
cat("tweak = ", epsilon, "\n")
return( list(portfolio=tweakportfolio(index$portfolio, epsilon), epsilon=epsilon) )
}
|
