aboutsummaryrefslogtreecommitdiffstats
path: root/build_SC.R
diff options
context:
space:
mode:
Diffstat (limited to 'build_SC.R')
-rw-r--r--build_SC.R161
1 files changed, 47 insertions, 114 deletions
diff --git a/build_SC.R b/build_SC.R
index f7e2ba75..f7f7eea9 100644
--- a/build_SC.R
+++ b/build_SC.R
@@ -145,7 +145,7 @@ maturity <- function(creditcurve){
}
}
-stackcurve <- function(SC, line.item, startdate, global.params){
+stackcurve <- function(SC, line.item, global.params, startdate){
newdates <- seq(startdate, line.item$maturity, by="3 months")
if(line.item$assettype=="Loan"){
hvec <- global.params$shape(yearFrac(today(), newdates[-1])) * global.params$defaultloanhazardrate
@@ -160,17 +160,20 @@ stackcurve <- function(SC, line.item, startdate, global.params){
return(SC)
}
-buildSC <- function(line.item, global.params, startdate=today()){
- ## cat(i, "\n")
- line.item <- collatdata[i,]
- ## cat(line.item$issuername, "\n")
- if( is.na(line.item$maturity) ){
- stop("empty maturity")
- }
- ##most likely equity, doesn't impact the risk anyway
- if(line.item$currentbalance < 1){
- next
+buildSC.matured <- function(SC, line.item, reinvdate, dealmaturity, global.params, startdate){
+ if(startdate<=reinvdate){ #reinvest
+ line.item$maturity <- min(dealdata$maturity, startdate + global.params$rollingmaturity)
+ SC <- stackcurve(SC, line.item, global.params,)
+ }else{ #no reinvestment
+ SC@dates <- startdate
+ SC@hazardrates <- 0
+ SC@prepayrates <- 0
}
+ return( SC )
+}
+
+buildSC <- function(line.item, reinvdate, dealmaturity, global.params, startdate){
+ ## cat(line.item$issuername, "\n")
if(!is.na(line.item$iscdo) && line.item$iscdo && is.na(line.item$price)){
##we have prices for some cdos e.g. 210795PS3
orig.moody <- cdorating(line.item$cusip)
@@ -178,55 +181,46 @@ buildSC <- function(line.item, global.params, startdate=today()){
orig.moody <- "NR"
}
line.item$price <- as.numeric(global.params$cdoprices[orig.moody])
- }
+ }
##build survival curve
SC <- new("creditcurve", recovery=recovery(line.item), startdate=startdate,
issuer=line.item$issuername)
SC@curve <- new("defaultprepaycurve", dates=as.Date(character(0)))
## defaulted asset
- if(!is.na(line.item$defaultedflag) && line.item$defaultedflag){
+ if(!is.na(line.item$defaultedflag) && line.item$defaultedflag){
+ if(!is.na(line.item$price)){
line.item$currentbalance <- line.item$currentbalance * line.item$price/100
- SC@startdate <- startdate + global.params$defaultedlag
- line.item$maturity <- min(dealdata$maturity, SC@startdate + global.params$rollingmaturity)
- ## automatic reinvest
- SC<- stackcurve(SC, line.item, SC@startdate, global.params)
- }else if( is.na(line.item$price) ){ #missing price
- if(line.item$maturity <= startdate){
- if(startdate<=dealdata$"Reinv End Date"){ #reinvest
- line.item$maturity <- min(dealdata$maturity, startdate + global.params$rollingmaturity)
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }else{ #no reinvestment
- SC@dates <- startdate
- SC@hazardrates <- 0
- SC@prepayrates <- 0
- }
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }else{
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }
}else{
- ## normal case
- if(line.item$maturity > startdate){
- if(line.item$assettype=="Bond"){ #no prepay rate
- alpha <- 0
- }else{
- alpha <- global.params$alpha
- }
- try <- bondhazardrate.shaped(line.item, global.params$shape,
- recovery(line.item), alpha)
- if(!is.null(try)){
- SC@curve <- try
- }
- }
+ line.item$currentbalance <- line.item$currentbalance * recovery(line.item)
+ }
+ SC@startdate <- startdate + global.params$defaultedlag
+ line.item$maturity <- min(dealdata$maturity, SC@startdate + global.params$rollingmaturity)
+ ## automatic reinvest
+ SC<- stackcurve(SC, line.item, global.params, SC@startdate)
+ }else if(line.item$maturity<=startdate){#matured asset
+ SC <- buildSC.matured(SC, line.item, reinvdate, dealmaturity, global.params, startdate)
+ }else if(is.na(line.item$price)){ #missing price
+ SC <- stackcurve(SC, line.item, global.params, SC@startdate)
+ }else{ #normal case
+ if(line.item$assettype=="Bond"){ #no prepay rate
+ alpha <- 0
+ }else{
+ alpha <- global.params$alpha
}
- ## if(length(maturity(SC))==0){
- ## browser()
- ## }
- if(maturity(SC) <= dealdata$"Reinv End Date"){ #we reinvest
+ try <- bondhazardrate.shaped(line.item, global.params$shape,
+ recovery(line.item), alpha)
+ if(is.null(try)){
+ SC <- stackcurve(SC, line.item, global.params, SC@startdate)
+ }else{
+ SC <- try
+ }
+ }
+ if(maturity(SC) <= reinvdate){ #we reinvest
newstartdate <- line.item$maturity
line.item$maturity <- min(dealdata$maturity, newstartdate + global.params$rollingmaturity)
- SC <- stackcurve(SC, line.item, newstartdate, global.params)
+ SC <- stackcurve(SC, line.item, global.params, newstartdate)
}
+ return( list(SC=SC, notional=line.item$currentbalance) )
}
buildSC.portfolio <- function(dealname, global.params, startdate=today()) {
@@ -238,77 +232,16 @@ buildSC.portfolio <- function(dealname, global.params, startdate=today()) {
for(i in 1:nrow(collatdata)){
## cat(i, "\n")
line.item <- collatdata[i,]
- ## cat(line.item$issuername, "\n")
if( is.na(line.item$maturity) ){
stop("empty maturity")
}
- #most likely equity, doesn't impact the risk anyway
+ ##most likely equity, doesn't impact the risk anyway
if(line.item$currentbalance < 1){
next
}
- if(!is.na(line.item$iscdo) && line.item$iscdo && is.na(line.item$price)){
- #we have prices for some cdos e.g. 210795PS3
- orig.moody <- cdorating(line.item$cusip)
- if(length(orig.moody)==0){
- orig.moody <- "NR"
- }
- line.item$price <- as.numeric(global.params$cdoprices[orig.moody])
- }
- #build survival curve
- SC <- new("creditcurve", recovery=recovery(line.item), startdate=startdate,
- issuer=line.item$issuername)
- SC@curve <- new("defaultprepaycurve", dates=as.Date(character(0)))
- ##expired asset
- ## if(i ==185){
- ## browser()
- ## }
- ## defaulted asset
- if(!is.na(line.item$defaultedflag) && line.item$defaultedflag){
- line.item$currentbalance <- line.item$currentbalance * line.item$price/100
- SC@startdate <- startdate + global.params$defaultedlag
- line.item$maturity <- min(dealdata$maturity, SC@startdate + global.params$rollingmaturity)
- ## automatic reinvest
- SC<- stackcurve(SC, line.item, SC@startdate, global.params)
- }else if( is.na(line.item$price) ){ #missing price
- if(line.item$maturity <= startdate){
- if(startdate<=dealdata$"Reinv End Date"){ #reinvest
- line.item$maturity <- min(dealdata$maturity, startdate + global.params$rollingmaturity)
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }else{ #no reinvestment
- SC@dates <- startdate
- SC@hazardrates <- 0
- SC@prepayrates <- 0
- }
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }else{
- SC <- stackcurve(SC, line.item, SC@startdate, global.params)
- }
- }else{
- ## normal case
- if(line.item$maturity > startdate){
- if(line.item$assettype=="Bond"){ #no prepay rate
- alpha <- 0
- }else{
- alpha <- global.params$alpha
- }
- try <- bondhazardrate.shaped(line.item, global.params$shape,
- recovery(line.item), alpha)
- if(!is.null(try)){
- SC@curve <- try
- }
- }
- }
- ## if(length(maturity(SC))==0){
- ## browser()
- ## }
- if(maturity(SC) <= dealdata$"Reinv End Date"){ #we reinvest
- newstartdate <- line.item$maturity
- line.item$maturity <- min(dealdata$maturity, newstartdate + global.params$rollingmaturity)
- SC <- stackcurve(SC, line.item, newstartdate, global.params)
- }
-
- notionalvec <- c(notionalvec, line.item$currentbalance)
- SCvec <- c(SCvec, SC)
+ temp <- buildSC(line.item, dealdata$"Reinv End Date", dealdata$maturity, global.params, startdate)
+ notionalvec <- c(notionalvec, temp$notional)
+ SCvec <- c(SCvec, temp$SC)
betavec <- c(betavec, if(!is.na(line.item$iscdo) && line.item$iscdo) 1 else
global.params$defaultcorr)
}