if(.Platform$OS.type == "unix"){ root.dir <- "/home/share/CorpCDOs" }else{ root.dir <- "//WDSENTINEL/share/CorpCDOs" } library(lossdistrib) n.int <- 250 gh <- GHquad(n.int) Ngrid <- 201 creditIndex <- function(name, tenor="5yr", Z=gh$Z, w=gh$w, N=Ngrid) { ## constructor for the index object ## FIXME: figure out what to do with the recovery name <- toupper(name) r <- list(name=name, tenor=tenor, type = substr(name, 1, 2), series=as.integer(substr(name, 3, nchar(name))), Z=Z, w=w, N=N, recovery=0.4) return( structure(r, class="creditIndex") ) } `$<-.creditIndex` <- function(x, name, value){ unclass(x) x[[name]] <- value class(x) <- "creditIndex" return(x) } c.creditIndex <- function(..., recursive = FALSE){ structure(c(unlist(lapply(list(...), unclass), recursive=FALSE)), class="creditIndex") } print.creditIndex <- function(index){ cat(index$name, index$tenor, as.character(index$tradedate), "\n\n") if("maturity" %in% names(index)){ cat("maturity:", as.character(index$maturity), "\n") cat("factor:", index$factor, "\n") cat("losstodate:", index$loss, "\n\n") } if("quotes" %in% names(index)){ cat("Index price ref:", index$quotes$refprice, "\n") cat("Index price spread:", index$quotes$refspread, "\n") cat("Index basis:", index$basis, "\n\n") } ##mapping to some prettier names colnames.toprint <- c("upfront", "running", "mkt.delta", "delta", "gamma", "theta", "corr01") available.colnames <- colnames.toprint[colnames.toprint %in% names(index$tranches)] df <- index$tranches[available.colnames] ##FIXME: need to check if it's bottom-up or top-down if(!is.null(index$rho)){ df <- cbind(df, data.frame(rho=index$rho[-1])) } print(df, digits=4) } load.index <- function(name, tenor, date){ ## load a creditIndex which was previously saved indexfile <- file.path(root.dir, "Tranche_data", "Objects", paste0(paste(name, tenor, date, sep="_"),".RData")) if(file.exists(indexfile)){ load(indexfile) }else{ loginfo(paste(name, tenor, "file missing")) return(NULL) } return(index) } csvheaders <- function(index){ if(class(index)!="creditIndex"){ stop("argument needs to be of class creditIndex") } tranche.names <- row.names(index$tranches) headers <- c("date", "indexpriceref", "indexspreadref", "indexprice", "indexBasis", "indexEL", "indexduration", "indexTheta", paste(tranche.names, "Upfront"), paste(tranche.names, "Dealer Delta"), paste(tranche.names, "Model Delta"), paste(tranche.names, "Forward Deltas"), paste(tranche.names, "Gamma"), paste(tranche.names, "Theta"), paste(index$K.orig[-1]*100, "Corr"), paste(tranche.names, "Corr01"), paste(tranche.names, "Dur"), paste(tranche.names, "EL")) return(paste(headers, collapse=",")) } tocsv <- function(index){ ##write a one line csv representation of the index object if(class(index)!="creditIndex"){ stop("argument needs to be of class creditIndex") } row <- c(as.character(index$tradedate), index$quotes$refprice, index$quotes$refspread, index$quotes$price, index$basis, index$EL, index$duration, index$theta, unlist(index$tranches[c("upfront", "mkt.delta", "delta", "fw.delta","gamma", "theta")]), index$rho[-1], unlist(index$tranches[c("corr01", "duration", "EL")])) return(paste(row, collapse=",")) }