aboutsummaryrefslogtreecommitdiffstats
path: root/R/backtest_tranches.R
blob: f9614f04c14156f60b115053bf74396e9e46d96a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
args <- commandArgs(trailingOnly=TRUE)

if(.Platform$OS.type == "unix"){
  root.dir <- "/home/share/CorpCDOs"
}else{
  root.dir <- "//WDSENTINEL/share/CorpCDOs"
}

source(file.path(root.dir, "code", "R", "cds_utils.R"))
source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
source(file.path(root.dir, "code", "R", "tranche_functions.R"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
load.index("hy21")

## calibrate HY21
## calibrate the single names curves
bps <- 1e-4
files <-  dir(file.path(root.dir, "Scenarios", "Calibration"), pattern="hy21_singlenames_*")
dates <- as.Date(substr(files, 18, 27))
basis <- data.frame(row.names=dates, indexref=numeric(length(dates)), basis=numeric(length(dates)))
for(i in 1:length(files)){
    f <- files[i]
    singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration", f))
    nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
    calibration.date <- as.Date(substr(f, 18, 27))
    if(calibration.date == as.Date("2013-12-20")){
        next
    }
    MarkitData <- getMarkitIRData(calibration.date)
    futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
                                       sprintf("futures-%s.csv", calibration.date)), header=F)
    tradedate <- adjust(dates=as.Date(MarkitData$effectiveasof))
    setEvaluationDate(tradedate)
    setCalendarContext("TARGET")
    L1m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/12)
    L2m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/6)
    L3m <-  buildMarkitYC(MarkitData, futurequotes[,2])
    L6m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/2)

    cdsdates <- as.Date(character(0))
    for(tenor in paste0(1:5, "y")){
        cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
    }

    hy21portfolio <- c()
    cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
                         1, tradedate, IMMDate(tradedate, "prev"))
    for(j in 1:nrow(nondefaulted)){
        SC <- new("creditcurve",
                  recovery=nondefaulted$recovery[j]/100,
                  startdate=tradedate,
                  issuer=as.character(nondefaulted$ticker[j]))
        quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[j,4:8]) /100,
                             running=rep(nondefaulted$running[j] * bps, 5))
        SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[j]/100, tradedate, cs)
        hy21portfolio <- c(hy21portfolio, SC)
    }
    market.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
                                      paste0("hy21_tranches_", calibration.date, ".csv")))
    ## get the index ref
    hy21$indexref <- market.data$bidRefPrice[1]/100
    temp <- tweakcurves(hy21portfolio, hy21, tradedate)
    hy21portfolio.tweaked <- temp$portfolio
    basis[as.character(tradedate), "basis"] <- temp$epsilon
    basis[as.character(tradedate), "indexref"] <- market.data$bidRefPrice[1]
    recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
    cs <- couponSchedule(IMMDate(tradedate), hy21$maturity, "Q", "FIXED", 0.05, 0, tradedate)
    SurvProb <- SPmatrix(hy21portfolio.tweaked, length(cs$dates))
    colnames(SurvProb) <- as.character(cs$dates)
    write.csv(SurvProb, row.names=FALSE,
              file=file.path(root.dir, "Data", "Backtest", paste0("survprob_", calibration.date, ".csv")))
    write(recov, ncolumns=1,
          file=file.path(root.dir, "Data", "Backtest", paste0("recov_", calibration.date, ".csv")))
    cat(as.character(calibration.date), "\n")
}