diff options
Diffstat (limited to 'R/calibration.R')
| -rw-r--r-- | R/calibration.R | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/R/calibration.R b/R/calibration.R index ca9f9b9e..1b802d99 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -87,26 +87,34 @@ set.tranchedata <- function(index, tradedate){ return( index ) } -build.skew <- function(index){ +build.skew <- function(index, type="bottomup"){ require(lossdistrib) - aux <- function(rho, index, K, quote, spread){ - temp <- BCtranche.legs(index, K, rho) + aux <- function(rho, index, K, quote, spread, complement){ + temp <- BCtranche.legs(index, K, rho, complement) return(abs(temp$pl+temp$cl*spread + quote)) } - rhovec <- c(0) - for(j in 1:(length(index$K)-2)){ - ##use the current tranche coupon - ## we compute the 0-index$K[j+1] equivalent quote using the coupon of the jth quote - if(j==1){ - q <- index$tranche.quotes[j]*index$K[j+1] - }else{ - temp <- BCtranche.legs(index, index$K[j], rhovec[j]) - q <- index$tranche.quotes[j] * (index$K[j+1]-index$K[j])- - temp$pl - temp$cl*index$tranche.running[j] + rhovec <- rep(NA, length(index$K)) + dK <- diff(index$K) + if(type=="bottomup"){ + for(j in 1:(length(dK)-1)){ + ##use the current tranche coupon + ## we compute the 0-index$K[j+1] equivalent quote using the coupon of the jth quote + tranchepv <- BCtranche.legs(index, index$K[j], rhovec[j]) + q <- index$tranche.quotes[j] * dK[j] - + tranchepv$pl - tranchepv$cl*index$tranche.running[j] + rho <- optimize(aux, interval=c(0,1), index=index, K=index$K[j+1], quote=q, + spread=index$tranche.running[j], complement=FALSE)$minimum + rhovec[j+1] <- rho + } + }else if(type=="topdown"){ + for(j in length(dK):2){ + tranchepv <- BCtranche.legs(index, index$K[j+1], rhovec[j+1], complement=TRUE) + q <- index$tranche.quotes[j] * dK[j] - + tranchepv$pl - tranchepv$cl * index$tranche.running[j] + rho <- optimize(aux, interval=c(0,1), index=index, K=index$K[j], quote=q, + spread=index$tranche.running[j], complement=TRUE)$minimum + rhovec[j] <- rho } - rho <- optimize(aux, interval=c(0,1), index=index, K=index$K[j+1], quote=q, - spread=index$tranche.running[j])$minimum - rhovec <- c(rhovec, rho) } - return( c(rhovec, NA) ) + return( rhovec ) } |
