diff options
Diffstat (limited to 'R/cds_functions_generic.R')
| -rw-r--r-- | R/cds_functions_generic.R | 60 |
1 files changed, 36 insertions, 24 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index ea44abce..15b8a404 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -575,14 +575,19 @@ bondhazardrate.shaped <- function(collateral, shape, R=0.4, alpha=0.25, beta=15, return( shapedtodpc(cs, sc, startdate) )
}
-tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE, forward.tweak=1){
+tweakportfolio <- function(portfolio, epsilon, start.tweak=1L, end.tweak=NA,
+ multiplicative=TRUE){
## 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[forward.tweak:length(x@curve@hazardrates)] <-
- x@curve@hazardrates[forward.tweak:length(x@curve@hazardrates)] * (1+epsilon)
+ if(is.na(end.tweak)) {
+ range <- start.tweak:length(x@curve@hazardrates)
+ } else {
+ range <- start.tweak:end.tweak
+ }
+ x@curve@hazardrates[range] <- x@curve@hazardrates[range] * (1+epsilon)
x
})
}else{
@@ -596,33 +601,35 @@ tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE, forward.twea }
indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE,
- maturity=index$maturity, forward.tweak=1, check=FALSE) {
+ maturity=index$maturity, start.tweak=1L, end.tweak=NA) {
## 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, forward.tweak=forward.tweak)
- }else{
+ portfolio <- tweakportfolio(index$portfolio, epsilon, start.tweak, end.tweak)
+ }else {
portfolio <- index$portfolio
}
startdate <- tradedate + 1
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))) {
- logerror(paste("couldn't compute single name protection leg for",
- index$portfolio[[which(is.na(pl.list))]]@issuer))
- return(NA)
+ cl.list <- vapply(portfolio, function(x) {
+ cl <- couponleg(cs, x@curve, startdate)
+ if(is.na(cl)) {
+ logerror(paste("couldn't compute single name coupon leg for", x@issuer))
+ return( NA )
}
- 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)
+ return( cl )
+ }, numeric(1))
+ pl.list <- vapply(portfolio, function(x) {
+ pl <- defaultleg(cs, x@curve, x@recovery, startdate)
+ if(is.na(pl)) {
+ logerror(paste("couldn't compute single name protection leg for", x@issuer))
+ return( NA )
}
- }
+ return( pl )
+ }, numeric(1))
spread <- index$quotes$spread[index$quotes$maturity == index$maturity]
r <- list(cl = spread * crossprod(cl.list, index$issuerweights),
pl = crossprod(pl.list, index$issuerweights),
@@ -704,17 +711,22 @@ tweakcurves <- function(index, tradedate=Sys.Date()){ epsilon <- rep(0, nrow(index$quotes))
for(i in 1:nrow(index$quotes)){
if(i == 1){
- forward.tweak <- 1
+ start.tweak <- 1
+ range <- which(index$cs$unadj.dates<=index$quotes$maturity[i])
+ end.tweak <- range[length(range)]
}else{
- forward.tweak <- which(index$cs$unadj.dates>index$quotes$maturity[i-1])[1]
+ range <- which((index$cs$unadj.dates > index$quotes$maturity[i-1]) &
+ (index$cs$unadj.dates <= index$quotes$maturity[i]))
+ start.tweak <- range[1]
+ end.tweak <- range[length(range)]
}
- f <- function(epsilon, index, tradedate, forward.tweak, i){
+ f <- function(epsilon, index, tradedate, start.tweak, end.tweak, i){
abs(indexpv(index, epsilon, tradedate, maturity=index$quotes$maturity[i],
- forward.tweak=forward.tweak)$bp - index$quotes$price[i])
+ TRUE, start.tweak, end.tweak)$bp - index$quotes$price[i])
}
epsilon[i] <- optimize(f, c(-0.25, 0.30),
- index, tradedate, forward.tweak, i, tol=1e-6)$minimum
- index$portfolio <- tweakportfolio(index$portfolio, epsilon[i], forward.tweak=forward.tweak)
+ index, tradedate, start.tweak, end.tweak, i, tol=1e-6)$minimum
+ index$portfolio <- tweakportfolio(index$portfolio, epsilon[i], start.tweak, end.tweak)
loginfo(paste("tweak =", epsilon[i]))
}
return( list(portfolio=index$portfolio, basis=epsilon) )
|
