stopifnot((root.dir <- Sys.getenv("SERENITAS_BASE_DIR")) != "") 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) r$recovery <- if(r$type=="HY") 0.3 else 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=recursive)), 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=",")) } as.data.frame.creditIndex <- function(index) { data.frame(tranche_id = index$tranches$id, date = index$tradedate, index = index$type, series = index$series, tenor = index$tenor, index_price = index$quotes$price, index_basis = index$basis, index_EL = index$EL, index_duration = index$duration, index_theta = index$theta, attach = as.integer(index$K.orig[-length(index$K.orig)]*100), detach = as.integer(index$K.orig[-1]*100), corr_at_detach = index$rho[-1], delta = index$tranche$delta, forward_delta = index$tranche$fw.delta, gamma = index$tranches$gamma, theta = index$tranches$theta, corr01 = index$tranches$corr01, duration = index$tranches$duration, EL = index$tranches$EL) }