hsloot / cvalr

Credit derivative valuation in R
Other
1 stars 2 forks source link

[FEAT] Implement `expected_pcds_loss` for the H2-Extendible parameter classes #28

Closed hsloot closed 3 years ago

hsloot commented 3 years ago

The expected_pcds_loss method can be implemented for all H2-Extendible parameter classes:

#' @describeIn <insert-class-name>-class
#'   returns the expected portfolio CDS loss for a specific time-point.
#' @aliases expected_pcds_loss,<insert-class-name>-method
#'
#' @inheritParams probability_distribution
#' @param method Calculation method (either `"default"` or the name of the
#'   class whose implementation should be used).
#'
#' @inheritSection ExtMO2FParam-class Expected portfolio CDS loss
#'
#' @examples
#' parm <- <insert-class-name>(c(3, 3, 4, 5), 8e-2, rho = c(3e-1, 6e-1))
#' expected_pcds_loss(parm, times = 0.25, recovery_rate = 0.4)
#' expected_pcds_loss(parm, times = seq(0, 1, by = 0.25), recovery_rate = 0.4)
#' expected_pcds_loss(parm, times = seq(0, 1, by = 0.25), recovery_rate = 0.4,
#'   method = "CalibrationParam")
#' expected_pcds_loss(parm, times = seq(0, 1, by = 0.25), recovery_rate = 0.4,
#'   method = "CalibrationParam",
#'   pd_args = list(method = "CalibrationParam", seed = 1623,
#'     sim_args = list(n_sim = 1e2L)))
#'
#' @importFrom stats pexp
#' @importFrom checkmate qassert
#'
#' @export
setMethod("expected_pcds_loss", "<insert-class-name>",
  function(object, times, recovery_rate, ...,
      method = c("default", "<insert-class-name>", "CalibrationParam")) {
    method <- match.arg(method)
    if (isTRUE("default" == method || "<insert-class-name>" == method)) {
      qassert(times, "N+[0,)")
      qassert(recovery_rate, "N1[0,1]")
      out <- (1 - recovery_rate) * pexp(times, rate = getLambda(object))
    } else {
      out <- callNextMethod(object, times, recovery_rate, ..., method = method)
    }

    out
  })