aboutsummaryrefslogtreecommitdiffstats
path: root/R/creditIndex.R
blob: 9748d6e468f9d95352b7f5ff184215e3f45e6d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
    r <- list(name=name, tenor=tenor, 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 ref:", index$quotes$price*100, "\n")
        cat("Index basis:", index$basis, "\n\n")
    }
    row <- paste(index$K.orig[-length(index$K.orig)]*100, index$K.orig[-1]*100, sep="-")

    ##mapping to some prettier names
    colnames.toprint <- c("tranche.upf", "tranche.running", "thetas", "deltas",
                          "forward.deltas", "gammas", "rho", "corr01")
    short.names <- c("upfront", "running", "theta", "delta", "fw.delta","gamma", "rho", "corr01")
    names(short.names) <- colnames.toprint
    colnames.available <- names(index)[names(index) %in% colnames.toprint]
    ##FIXME: need to check if it's bottom-up or top-down
    index$rho <- index$rho[-1]
    df <- data.frame(index[colnames.available], row.names=row)
    names(df) <- short.names[colnames.available]
    print(df, digits=4)
}

load.index <- function(name, tenor, date){
    ## load a creditIndex which was previously saved
    load(file.path(root.dir, "Tranche_data", "Objects",
                   paste0(paste(name, tenor, date, sep="_"),".RData")))
    return(index)
}