aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_MF.R
blob: c87d20b12171efb5ca58585162957eca2a914cae (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
#!/usr/bin/Rscript
require(methods)
library(logging)

args <- commandArgs(trailingOnly=TRUE)

basicConfig()
removeHandler('basic.stdout')
addHandler(writeToFile, file=file.path(Sys.getenv("SERENITAS_LOG_DIR"), "calibrate_tranches_MF.log"))
##options(warn=2)
data.dir <- file.path(Sys.getenv("SERENITAS_BASE_DIR"), "Scenarios", "Calibration")
source("yieldcurve.R")
source("optimization.R")
source("calibration.R")
source("serenitasdb.R")
source("creditIndex.R")
source("tranche_functions.R")

##figure out the tradedate
tradedate <- if(length(args) >= 1) as.Date(args[1]) else addBusDay(Sys.Date(), -1)

exportYC(tradedate)
## calibrate HY39
## calibrate the single names curves
index <- creditIndex("hy39", "5yr")
index <- set.index.desc(index, tradedate)
index <- set.singlenamesdata(index, tradedate)
## load tranche data
index <- set.tranchedata(index, tradedate)
##calibrate by modifying the factor distribution
index$w.mod <- build.MFdist(index)
dist <- MFlossdist(index)
write.table(data.frame(Z=index$Z, w=index$w.mod),
            file=file.path(data.dir,
            paste0("calibration-", tradedate, ".csv")),
            col.names=T, row.names=F, sep=",")

save(index, dist, file = file.path(data.dir,
                                   paste0("marketdata-", tradedate, ".RData")),
     compress="xz")