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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
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)
}
|