aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/cds_functions_generic.R23
1 files changed, 11 insertions, 12 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 8b05d666..1cd89c76 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -736,19 +736,17 @@ forwardportfolioprice <- function(portfolio, startdate, maturity, coupontype, ma
forwardcs <- couponSchedule(nextpaydate=startdate+45, maturity,
frequency="Q", coupontype, margin,
currentcoupon, startdate=startdate)
- r <- c()
- notionalvec <- c()
- for(i in seq_along(portfolio$SC)){
- if(length(portfolio$SC[[i]]@curve@hazardrates)==0){
- next
- }else{
- notionalvec <- c(notionalvec, portfolio$notional[i])
- }
- cl <- couponleg(forwardcs, portfolio$SC[[i]]@curve, startdate)
- pl <- contingentleg(forwardcs, portfolio$SC[[i]]@curve, portfolio$SC[[i]]@recovery, startdate)
- r <- c(r, pl+cl)
+ #select curves that have non zero lengthhazardrates
+ idx <- unlist(lapply(portfolio$SC, function(x)length(x@curve@hazardrates)>0))
+ price <- function(x, ...){
+ cl <- couponleg(forwardcs, x@curve, startdate)
+ pl <- contingentleg(forwardcs, x@curve, x@recovery, startdate)
+ return( pl + cl)
}
- return(crossprod(notionalvec, r)/sum(notionalvec))
+
+ return(crossprod(portfolio$notional[idx],
+ vapply(portfolio$SC[idx], price, numeric(1)))/
+ sum(portfolio$notional[idx]))
}
@@ -764,3 +762,4 @@ creditcurve.maturity <- function(creditcurve){
stop("not of class creditcurve")
}
}
+