diff options
| -rw-r--r-- | R/calibrate_tranches.R | 36 | ||||
| -rw-r--r-- | python/db.py | 5 | ||||
| -rw-r--r-- | python/insert_index.py | 64 | ||||
| -rw-r--r-- | python/yieldcurve.py | 8 |
4 files changed, 72 insertions, 41 deletions
diff --git a/R/calibrate_tranches.R b/R/calibrate_tranches.R index 52802685..d1320883 100644 --- a/R/calibrate_tranches.R +++ b/R/calibrate_tranches.R @@ -1,22 +1,25 @@ library("parallel")
-root.dir <- "//WDSENTINEL/share/CorpCDOs/"
+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", "index_definitions.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"))
-cl <- makeCluster(6)
-
-MarkitData <- getMarkitIRData()
+tradedate <- Sys.Date()
+MarkitData <- getMarkitIRData(tradedate)
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
L2m <- buildMarkitYC(MarkitData, dt = 1/6)
L3m <- buildMarkitYC(MarkitData)
L6m <- buildMarkitYC(MarkitData, dt = 1/2)
setEvaluationDate(as.Date(MarkitData$effectiveasof))
-## calibrate HY19
+## calibrate HY21
## calibrate the single names curves
singlenames.data <- read.table(file="clipboard", sep="\t", header=T)
nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy19$defaulted,]
@@ -24,10 +27,12 @@ bps <- 1e-4 cdsdates <- as.Date(character(0))
for(tenor in paste0(1:5, "y")){
- cdsdates <- c(cdsdates, cdsMaturity(tenor))
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
}
-hy19portfolio <- c()
+hy21portfolio <- c()
+cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
+ 1, tradedate, IMMDate(tradedate, "prev"))
for(i in 1:nrow(nondefaulted)){
SC <- new("creditcurve",
recovery=nondefaulted$recovery[i]/100,
@@ -36,10 +41,10 @@ for(i in 1:nrow(nondefaulted)){ quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
running=rep(nondefaulted$running[i]*bps, 5))
SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100)
- hy19portfolio <- c(hy19portfolio, SC)
+ hy21portfolio <- c(hy21portfolio, SC)
}
-issuerweights <- rep(1/length(hy19portfolio), length(hy19portfolio))
+issuerweights <- rep(1/length(hy21portfolio), length(hh21portfolio))
hy19$indexref <- 1.0275
hy19portfolio.tweaked <- tweakcurves(hy19portfolio, hy19)
@@ -254,3 +259,14 @@ Rfun <- splinefun(c(0, cumsum(MFdist$R[,i])),c(0, seq(0, 1, length=Ngrid)), "mon r[i, j] <- Rfun(d1[j])
}
}
+
+issuerweights <- rep(1/100, 100)
+rho <- 0.45
+recov <- read.csv("~/code/python/recov.csv", header=F)
+recov <- as.numeric(recov$V1)
+survprob <- as.matrix(read.csv("~/code/python/survprob.csv", header=F))
+gq <- gauss.quad.prob(500, "normal")
+Z <- gq$nodes
+w <- gq$weights
+defaultprob <- 1-survprob
+test <- BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, 101)
diff --git a/python/db.py b/python/db.py index 5a4e10c8..38a152c3 100644 --- a/python/db.py +++ b/python/db.py @@ -7,6 +7,11 @@ conn = psycopg2.connect(database="ET", host="debian", cursor_factory=DictCursor) +connmlpdb = psycopg2.connect(database="mlpdb", + user="mlpdb_user", + password="Serenitas1", + host="debian", + cursor_factory=DictCursor) def with_connection(f): def with_connection_(*args, **kwargs): # or use a pool, or a factory function... diff --git a/python/insert_index.py b/python/insert_index.py index 1223b0f0..ce130fcd 100644 --- a/python/insert_index.py +++ b/python/insert_index.py @@ -6,11 +6,11 @@ metadata = MetaData(bind = engine) index_desc = Table('index_desc', metadata, autoload=True)
index_ins = index_desc.insert()
-offset=182970
-# index = [[offset, 'EU', 19, datetime.date(2016, 06, 20), 'Y3', 100, 0],
-# [offset, 'EU', 19, datetime.date(2018, 06, 20), 'Y5', 100, 0],
-# [offset, 'EU', 19, datetime.date(2020, 06, 20), 'Y7', 100, 0],
-# [offset, 'EU', 19, datetime.date(2023, 06, 20), 'Y10', 100, 0]]
+offset=183000
+index = [[offset, 'EU', 21, datetime.date(2017, 6, 20), 'Y3', 100, 0],
+ [offset, 'EU', 21, datetime.date(2019, 6, 20), 'Y5', 100, 0],
+ [offset, 'EU', 21, datetime.date(2021, 6, 20), 'Y7', 100, 0],
+ [offset, 'EU', 21, datetime.date(2024, 6, 20), 'Y10', 100, 0]]
# index = [[offset, 'HY', 21, datetime.date(2016, 12, 20), 'Y3', 100, 0],
# [offset, 'HY', 21, datetime.date(2018, 12, 20), 'Y5', 100, 0],
@@ -38,31 +38,41 @@ for line in index: tranche_desc = Table('tranche_desc', metadata, autoload = True)
tranche_ins = tranche_desc.insert()
-igbasketid = 182990
-igattach = [0, 3, 7, 15, 100]
-igtenors = ['Y3', 'Y5', 'Y7', 'Y10']
-for tenor in igtenors:
- for i, (attach, detach) in enumerate(zip(igattach[:-1], igattach[1:])):
- with engine.begin() as conn:
- conn.execute(tranche_ins,
- trancheid = igbasketid+i,
- basketid = igbasketid,
- tenor = tenor,
- attach=attach,
- detach = detach)
+# igbasketid = 182990
+# igattach = [0, 3, 7, 15, 100]
+# igtenors = ['Y3', 'Y5', 'Y7', 'Y10']
+# for tenor in igtenors:
+# for i, (attach, detach) in enumerate(zip(igattach[:-1], igattach[1:])):
+# with engine.begin() as conn:
+# conn.execute(tranche_ins,
+# trancheid = igbasketid+i,
+# basketid = igbasketid,
+# tenor = tenor,
+# attach=attach,
+# detach = detach)
+
+# hybasketid = 182970
+# hyattach = [0, 15, 25, 35, 100]
+# hytenors = ['Y3', 'Y5', 'Y7']
+# for tenor in hytenors:
+# for i, (attach, detach) in enumerate(zip(hyattach[:-1], hyattach[1:])):
+# with engine.begin() as conn:
+# conn.execute(tranche_ins,
+# trancheid = hybasketid+i,
+# basketid = hybasketid,
+# tenor = tenor,
+# attach = attach,
+# detach = detach)
-hybasketid = 182970
-hyattach = [0, 15, 25, 35, 100]
-hytenors = ['Y3', 'Y5', 'Y7']
-for tenor in hytenors:
- for i, (attach, detach) in enumerate(zip(hyattach[:-1], hyattach[1:])):
+eubasketid = 183000
+euattach = [0, 3, 6, 12, 100]
+eutenors = ['Y3', 'Y5']
+for tenor in eutenors:
+ for i, (attach, detach) in enumerate(zip(euattach[:-1], euattach[1:])):
with engine.begin() as conn:
conn.execute(tranche_ins,
- trancheid = hybasketid+i,
- basketid = hybasketid,
+ trancheid =eubasketid+i,
+ basketid = eubasketid,
tenor = tenor,
attach = attach,
detach = detach)
-
-# euattach = [0, 3, 6, 9, 12, 22, 100]
-# eutenors = ['Y3', 'Y5', 'Y7']
diff --git a/python/yieldcurve.py b/python/yieldcurve.py index 50588aa6..23f411a8 100644 --- a/python/yieldcurve.py +++ b/python/yieldcurve.py @@ -50,15 +50,15 @@ def YC(date = datetime.date.today(), MarkitData=None, futures = None): futures = get_futures_data(date.date()) m = libor_market('USD(NY)') quotes = [('ED',i+1, v) for i, v in enumerate(futures)] - # if next_imm_date(date, 9) == pydate_to_qldate(date) + Period('2Yr'): - # quotes.pop(8) + if next_imm_date(date, 9) == m.settle_date + Period('2Yr'): + quotes.pop(7) quotes += [('SWAP', k, v) for k, v in MarkitData['swaps'].items()] m.set_quotes(date, quotes) - ts = m.bootstrap_term_structure(interpolator='linear') + ts = m.bootstrap_term_structure() return ts if __name__=="__main__": - date = datetime.date(2013, 12, 23) + date = datetime.datetime(2014, 4, 29) ts = YC(date) cal = Calendar.from_name('USA') p1 = Period('1Mo') |
