aboutsummaryrefslogtreecommitdiffstats
path: root/R/cds_functions_generic.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/cds_functions_generic.R')
-rw-r--r--R/cds_functions_generic.R39
1 files changed, 20 insertions, 19 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 6ccf7c37..a6b83a07 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -602,7 +602,7 @@ indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE,
## (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){
+ if(epsilon != 0) {
portfolio <- tweakportfolio(index$portfolio, epsilon, forward.tweak=forward.tweak)
}else{
portfolio <- index$portfolio
@@ -611,38 +611,39 @@ indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE,
cs <- index$cs[index$cs$unadj.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)))
- if(check){
- if(any(is.na(cl.list))){
+ if(check) {
+ if(any(is.na(cl.list))) {
logerror(paste("couldn't compute single name protection leg for",
index$portfolio[[which(is.na(pl.list))]]@issuer))
return(NA)
}
- if(any(is.na(pl.list))){
+ if(any(is.na(pl.list))) {
logerror(paste("couldn't compute single name protection leg for",
index$portfolio[[which(is.na(pl.list))]]@issuer))
return(NA)
}
}
spread <- index$quotes$spread[index$quotes$maturity == index$maturity]
- r <- list(cl = spread * mean(cl.list), pl = mean(pl.list),
- bp = 1+mean(spread*cl.list-pl.list))
- if(clean){
+ r <- list(cl = spread * crossprod(cl.list, index$issuerweights),
+ pl = crossprod(pl.list, index$issuerweights),
+ bp = 1 + crossprod(index$issuerweights, spread * cl.list - pl.list))
+ if(clean) {
accrued <- cdsAccrued(tradedate, spread)
- r$bp <- r$bp-accrued
- r$cl <- r$cl-accrued
+ r$bp <- r$bp - accrued
+ r$cl <- r$cl - accrued
}
return(r)
}
-indexduration <- function(index, tradedate, maturity=index$maturity){
+indexduration <- function(index, tradedate=Sys.Date(), maturity=index$maturity) {
## compute the duration of a portfolio of survival curves
- cs <- index$cs[index$cs$dates-maturity< 3,]
- startdate <- tradedate+1
- cl.list <- unlist(lapply(index$portfolio, function(x) couponleg(cs, x@curve, startdate) ))
- return( mean(cl.list) )
+ cs <- index$cs[index$cs$unadj.dates <= maturity,]
+ startdate <- tradedate + 1
+ cl.list <- unlist(lapply(index$portfolio, function(x) couponleg(cs, x@curve, startdate)))
+ return( crossprod(index$issuerweights, cl.list) )
}
-indexspread <- function(index, tradedate=Sys.Date()){
+indexspread <- function(index, tradedate=Sys.Date()) {
## computes the spread of a portfolio of survival curves
## S <- 0
## d <- rep(0, length(portfolio))
@@ -651,23 +652,23 @@ indexspread <- function(index, tradedate=Sys.Date()){
## S <- S + d[i] * cdsspread(portfolio[[i]]@curve, index$maturity, portfolio[[i]]@recovery)
## }
temp <- indexpv(index, tradedate=tradedate)
- S <- index$quotes$spread*(1-(temp$bp-1)/temp$cl)
+ S <- index$quotes$spread * (1 - (temp$bp -1 ) / temp$cl)
return(S)
}
-indextheta <- function(index, tradedate=Sys.Date()) {
+indextheta <- function(index, tradedate=Sys.Date(), maturity=index$maturity) {
if(nrow(index$cs) <= 4) {
return( NA )
}
current.pv <- indexpv(index, tradedate=tradedate)$bp
- newmaturity <- index$maturity - lubridate::years(1)
+ newmaturity <- maturity - lubridate::years(1)
rolled.pv <- indexpv(index, tradedate=tradedate, maturity=newmaturity)$bp
theta <- rolled.pv - current.pv + index$quotes$spread[index$quotes$maturity == index$maturity]
return( theta )
}
-portfoliospread <- function(portfolio, maturity, tradedate=Sys.Date()){
+portfoliospread <- function(portfolio, maturity, tradedate=Sys.Date()) {
## computes the spread of a portfolio defined by notionals and survivalcurves
## for a given maturity.
## if maturity is missing, we use the intrinsic maturity for each curve