diff options
| -rw-r--r-- | R/calibration.R | 12 | ||||
| -rw-r--r-- | R/cds_functions_generic.R | 17 | ||||
| -rw-r--r-- | R/tranche_functions.R | 2 |
3 files changed, 17 insertions, 14 deletions
diff --git a/R/calibration.R b/R/calibration.R index 1a8242e8..bf79c8e4 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -50,12 +50,12 @@ set.singlenamesdata <- function(index, tradedate){ } index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates)) } - if(index$name == "EU9"){ - index$issuerweights <- rep(0.08, 123) - index$issuerweights[quotes$ticker=="GDFS"] <- 0.08*2 - index$issuerweights[quotes$ticker=="GASSM"] <- 0.08*2 - } index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio)) + ## group common curves + index$issuerweights <- aggregate(index$issuerweights, + by = list(unlist(lapply(index$portfolio, function(x)x@issuer))), + sum)[,2] + index$portfolio <- unique(index$portfolio) index$recov <- sapply(index$portfolio, attr, "recovery") return( index ) } @@ -87,7 +87,7 @@ set.tranchedata <- function(index, tradedate){ tweak <- tweakcurves(index, tradedate) index$portfolio <- NULL index <- c(index, tweak) - index$defaultprob <- 1 - SPmatrix(index$portfolio, length(index$cs$dates)) + index$defaultprob <- 1 - SPmatrix(index$portfolio, index$cs$dates) negprob <- which(index$defaultprob<0, arr.ind=T) if(nrow(negprob) > 0){ stop(paste(index$portfolio[[negprob[1,1]]]@issuer, "has negative probability, check single names data")) diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index 706072ee..64176812 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -768,14 +768,13 @@ FEP <- function(index, exerciseDate) { return( fep )
}
-SP <- function(sc, startdate=Sys.Date()){
+SP <- function(sc, dates, dT){
## computes the survival probability associated with the survival curve
- T <- c(0, yearFrac(startdate, sc@dates))
- dT <- diff(T)
- return( cumprod(exp(-sc@hazardrates * dT)) )
+ h <- approx(sc@dates, sc@hazardrates, dates, "constant", rule=2)$y
+ return( cumprod(exp(-h * dT)) )
}
-SPmatrix <- function(portfolio, n.dates){
+SPmatrix <- function(portfolio, dates){
## computes matrix of survival probability
## inputs:
## portfolio: portfolio of survival curves
@@ -784,10 +783,14 @@ SPmatrix <- function(portfolio, n.dates){ ## matrix of survival probabilities of dimensions dim1 x dim2
## with dim1 number of issuers and dim2 number of dates in the
## coupon schedule of index
- SP <- matrix(0, length(portfolio), n.dates)
+ SP <- matrix(0, length(portfolio), length(dates))
for(i in 1:length(portfolio)){
- SP[i,] <- SP(portfolio[[i]]@curve, portfolio[[i]]@startdate)[1:n.dates]
+ T <- c(0, yearFrac(portfolio[[i]]@startdate, dates))
+ dT <- diff(T)
+ SP[i,] <- SP(portfolio[[i]]@curve, dates, dT)
}
+ colnames(SP) <- as.character(dates)
+ rownames(SP) <- unlist(lapply(portfolio, function(x)x@issuer))
return( SP )
}
diff --git a/R/tranche_functions.R b/R/tranche_functions.R index 4cc9ba73..99709832 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -266,7 +266,7 @@ BCtranche.delta <- function(index, complement=FALSE){ }else{ newindex <- index newindex$portfolio <- tweakportfolio(newindex$portfolio, x) - newindex$defaultprob <- 1 - SPmatrix(newindex$portfolio, length(index$cs$dates)) + newindex$defaultprob <- 1 - SPmatrix(newindex$portfolio, index$cs$dates) return(newindex) } }) |
