#!/usr/bin/Rscript require(methods) library(logging) args <- commandArgs(trailingOnly=TRUE) basicConfig() removeHandler('basic.stdout') addHandler(writeToFile, file=file.path(Sys.getenv("LOG_DIR"), "calibrate_tranches_MF.log")) ##options(warn=2) code.dir <- Sys.getenv("CODE_DIR") data.dir <- file.path(Sys.getenv("DATA_DIR"), "..", "Scenarios", "Calibration") source(file.path(code.dir, "R", "yieldcurve.R")) source(file.path(code.dir, "R", "optimization.R")) source(file.path(code.dir, "R", "calibration.R"), chdir=TRUE) source(file.path(code.dir, "R", "serenitasdb.R")) source(file.path(code.dir, "R", "creditIndex.R")) source(file.path(code.dir, "R", "tranche_functions.R")) ##figure out the tradedate if(length(args) >= 1){ tradedate <- as.Date(args[1]) }else{ tradedate <- addBusDay(Sys.Date(), -1) } exportYC(tradedate) ## calibrate HY25 ## calibrate the single names curves index <- creditIndex("hy27", "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")