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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
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 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
load(file.path(root.dir, "Tranche_data", "Objects",
paste0(paste(name, tenor, date, sep="_"),".RData")))
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=","))
}
|