Skip to content

Commit

Permalink
resloved errors/warnings/notes
Browse files Browse the repository at this point in the history
  • Loading branch information
farzadwp committed Nov 27, 2019
1 parent 5db97f6 commit eef3ffd
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 111 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export(ZeroCurve)
export(ZeroHazardRate)
export(as_DiscountFactor)
export(as_InterestRate)
export(as_SurvivalProbabilities)
export(as_ZeroHazardRate)
export(build_zero_curve)
export(interpolate)
Expand Down
4 changes: 2 additions & 2 deletions R/cedit-ops.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ c.ZeroHazardRate <- function (..., recursive = FALSE) {

#' @export
`[.ZeroHazardRate` <- function (x, i, j, ..., drop = TRUE) {
new_ZeroHazardRate(x$value[i], x$compounding[i], x$day_basis[i], spec = x$specs[i])
new_ZeroHazardRate(x$value[i], x$compounding[i], x$day_basis[i], specs = x$specs[i])
}


Expand Down Expand Up @@ -265,7 +265,7 @@ c.SurvivalProbabilities <- function (..., recursive = FALSE) {

#' @export
`[.SurvivalProbabilities` <- function (x, i, j, ..., drop = TRUE) {
new_SurvivalProbabilities(x$value[i], x$start_date[i], x$end_date[i], spec = x$specs[i])
new_SurvivalProbabilities(x$value[i], x$start_date[i], x$end_date[i], specs = x$specs[i])
}

#' @export
Expand Down
40 changes: 18 additions & 22 deletions R/credit-class.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Build a `CDSSpec`
#'
#' This class will enable you to specify CDS curves. It is used by
#' [SurvivalProbabilitiesCurve()] and [ZeroHazardCurve()].
#' [SurvivalProbabilities()] and [ZeroHazardRate()].
#'
#' @param rank Seniority of the reference debt. Must be one of the following
#' options: "SNR" for Senior, "SubTier3" for Subordinate Tier 3,
Expand Down Expand Up @@ -188,7 +188,7 @@ validate_CDSCurve <- function(x) {
#' typically be bootstrapped from a [CDSCurve()].
#'
#' @inheritParams CDSCurve
#' @param survival_probabilities a vector of survival probabilities corresponding to each
#' @param values a vector of survival probabilities corresponding to each
#' time step in `tenors`.
#' @param d1 a `Date` vector containing the as of date
#' @param d2 a `Date` vector containing the date to which the survival probability
Expand Down Expand Up @@ -233,19 +233,22 @@ validate_SurvivalProbabilities <- function(x) {
#' Builds a `ZeroHazardRate`
#'
#' This will allow you to create a harzard rate curve. This will typically be
#' bootstrapped or implied from a [CDSCurve()] or [SurvivalProbabilitiesCurve()].
#' bootstrapped or implied from a [CDSCurve()] or [SurvivalProbabilities()].
#'
#' @inheritParams CDSCurve
#' @param hazard_rates a vector of hazard rates corresponding to each time step
#' in `tenors`
#' @param value a numeric vector containing zero hazard rate values (as decimals).
#' @param values a numeric vector containing zero hazard rate values (as decimals).
#' @param compounding a numeric vector representing the [compounding] frequency.
#' @param day_basis a character vector representing the day basis associated
#' with the interest rate and hazard rate(see [fmdates::year_frac()])
#' @param specs CDS curve specifications that inherits from [CDSSpec()]
#' @return returns an object of type `hazard_rates`
#' @examples
#'
#' curve_specs <- CDSMarkitSpec(
#' rating = "AAA",
#' region = "Japan",
#' sector = "Utilities"
#' )
#' ZeroHazardRate(values = c(0.04, 0.05), compounding = c(2, 4),
#' day_basis = 'act/365', specs = curve_specs )
#'
Expand Down Expand Up @@ -306,8 +309,6 @@ validate_ZeroHazardRate <- function(x) {
#' @param specs CDS curve specifications that inherits from [CDSSpec()]
#'
#' @return a `CreditCurve` object
#'
#' @examples
#' curve_specs <- CDSMarkitSpec(rating = "AAA", region = "Japan", sector = "Utilities")
#' zero_curve <- build_zero_curve()
#' ref_date <- zero_curve$reference_date
Expand All @@ -318,6 +319,9 @@ validate_ZeroHazardRate <- function(x) {
#' sp <- as_SurvivalProbabilities(x = cds_curve, zero_curve = zero_curve)
#' CreditCurve(survival_probabilities = sp, reference_date =ref_date,
#' interpolation = CubicInterpolation(), specs = curve_specs)
#'
#'
#'
#' @export
#' @seealso [Interpolation]
CreditCurve <- function(survival_probabilities, reference_date, interpolation,
Expand Down Expand Up @@ -576,6 +580,11 @@ format.ZeroHazardRate <- function(x, ...) {
print.ZeroHazardRate <- function(x, ...) {cat(format(x), "\n"); invisible(x)}


#' Inherits from CreditCurve
#'
#' Checks whether object inherits from `CreditCurve` class
#' @param x an R Object
#' @return `TRUE` if `x` inherits from the `CreditCurve` class; otherwise `FALSE`
#' @export
is.CreditCurve <- function(x) {
inherits(x, "CreditCurve")
Expand Down Expand Up @@ -603,19 +612,6 @@ print.CreditCurve <- function(x, ...) {
#' @return a `tibble` with two columns named `Years` and `Zero Hazard Rates`.
#' @seealso [tibble::tibble()]
#' @importFrom tibble as_tibble
#' @examples
#' curve_specs <- CDSMarkitSpec(rating = "AAA", region = "Japan", sector = "Utilities")
#' zero_curve <- build_zero_curve()
#' ref_date <- zero_curve$reference_date
#' specs <- CDSMarkitSpec(rating = "AAA", region = "Japan", sector = "Utilities")
#' cds_curve <- CDSCurve(reference_date = ref_date,
#' tenors = c(1, 3, 5, 7), spreads = c(0.0050, 0.0070, 0.0090, 0.0110), lgd = .6,
#' premium_frequency = 4, specs = curve_specs)
#' sp <- as_SurvivalProbabilities(x = cds_curve, zero_curve = zero_curve)
#' cc <- CreditCurve(survival_probabilities = sp,
#' reference_date =ref_date, interpolation = LinearInterpolation(),
#' specs = curve_specs)
#' as_tibble(cc)
#' @export
as_tibble.CreditCurve <- function(x, ...) {
tibble::tibble(
Expand Down Expand Up @@ -648,7 +644,7 @@ interpolate.CreditCurve <- function(x, at, ...) {
x$interpolator(at)
}

#' @rdname interpolate_zeros *edit interpolate zero
#' @rdname interpolate_zeros
#' @export
interpolate_zeros.CreditCurve <- function(x, at, compounding = NULL, day_basis = NULL, ...) {

Expand Down
59 changes: 31 additions & 28 deletions R/credit-methods.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#' Coerce to InterestRate
#'
#' You can coerce objects to the `SurvivalProbabilities` class using this method.
#'
#' @param x object to coerce
#' @param ... other parameters passed to methods
#' @return an `SurvivalProbabilities` object
#' @export
as_SurvivalProbabilities <- function(x, ...) UseMethod("as_SurvivalProbabilities")

#' Bootstraps Survival Probabilitie from a CDS curve
#' Using \href{https://www.rdocumentation.org/packages/credule/versions/0.1.3}{credule package.}
#' The output of bootstrapping is a vector of cumulative survival probabilities.
#'
#' @param cds_curve An object of type `CDSCurve`
#' @param x An object of type `CDSCurve`
#' @param zero_curve An object of type `ZeroCurve`
#' @param num_timesteps_pa It represents the number of timesteps used to perform the numerical integral
#' required while computing the default leg value. It is shown that a monthly discretisation
Expand All @@ -13,24 +21,14 @@ as_SurvivalProbabilities <- function(x, ...) UseMethod("as_SurvivalProbabilities
#' @param accrued_premium If set to TRUE, the accrued premium will be taken into account in the calculation of the premium leg value.
#'
#' @return An object of type `SurvivalProbabilitiesCurve`
#' @examples
#'
#'
#'zero_curve <- build_zero_curve()
#'specs <- CDSMarkitSpec(rating = "AAA", region = "Japan", sector = "Utilities")
#'cds_curve <- CDSCurve(
#' reference_date = zero_curve$reference_date,
#' tenors = c(1, 3, 5, 7), spreads = c(0.0050, 0.0070, 0.0090, 0.0110), lgd = .6,
#' premium_frequency = 4, specs = curve_specs)
#'as_SurvivalProbabilities(x = cds_curve, zero_curve = zero_curve)
#'
#'

as_SurvivalProbabilities.CDSCurve <- function(x,
zero_curve,
num_timesteps_pa = 12,
accrued_premium = TRUE) {
if (cds_curve$reference_date != zero_curve$reference_date) {
stop("The reference dates for CDS Curve and the Zero Curve are different", call. = FALSE)
zero_curve,
num_timesteps_pa = 12,
accrued_premium = TRUE) {
if (x$reference_date != zero_curve$reference_date) {
stop("The reference dates for CDS Curve and the Zero Curve are different",
call. = FALSE)
}
if (!is.ZeroCurve(zero_curve)) {
stop("zero_curve must be an object of type ZeroCurve", call. = FALSE)
Expand All @@ -39,30 +37,35 @@ as_SurvivalProbabilities.CDSCurve <- function(x,
sp_output <- credule::bootstrapCDS(
yieldcurveTenor = zero_curve$pillar_times,
yieldcurveRate = zero_curve$pillar_zeros,
cdsTenors = cds_curve$tenors,
cdsSpreads = cds_curve$spread,
recoveryRate = 1 - cds_curve$lgd,
numberPremiumPerYear = cds_curve$premium_frequency,
cdsTenors = x$tenors,
cdsSpreads = x$spread,
recoveryRate = 1 - x$lgd,
numberPremiumPerYear = x$premium_frequency,
numberDefaultIntervalPerYear = num_timesteps_pa,
accruedPremium = accrued_premium
)


SurvivalProbabilities(
d1 = cds_curve$reference_date,
d2 = cds_curve$reference_date + 365 * cds_curve$tenors,
d1 = x$reference_date,
d2 = x$reference_date + 365 * x$tenors,
values = sp_output$survprob,
specs = cds_curve$specs
specs = x$specs
)
}


#' @inheritParams SurvivalProbabilities
#' @rdname as_SurvivalProbabilities
#' @examples
#' curve_specs <- CDSMarkitSpec(
#' rating = "AAA",
#' region = "Japan",
#' sector = "Utilities"
#' )
#' HR <- ZeroHazardRate(values = c(0.04, 0.05), compounding = c(2, 4),
#' day_basis = 'act/365', specs = specs)
#' as_SurvivalProbabilities(HR, ymd(20160202), ymd(20160302))
#' day_basis = 'act/365', specs = curve_specs)
#' as_SurvivalProbabilities(HR, lubridate::ymd(20160202), lubridate::ymd(20160302))
#' @export
as_SurvivalProbabilities.ZeroHazardRate <- function(x, d1, d2, ...) {
assertthat::assert_that(
Expand Down Expand Up @@ -100,7 +103,7 @@ as_SurvivalProbabilities.ZeroHazardRate <- function(x, d1, d2, ...) {
#' @return an `ZeroHazardRate` object
#' @examples
#' library("lubridate")
#' as_InterestRate(SurvivalProbabilities(0.95, ymd(20130101), ymd(20140101), CDSSpec("Empty")),
#' as_ZeroHazardRate(SurvivalProbabilities(0.95, ymd(20130101), ymd(20140101), CDSSpec("Empty")),
#' compounding = 2, day_basis = "act/365")
#' @export
as_ZeroHazardRate <- function(x, ...) UseMethod("as_ZeroHazardRate")
Expand Down
1 change: 1 addition & 0 deletions R/generic-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ interpolate <- function(x, ...) UseMethod("interpolate")
#' @param day_basis a valid [day basis][fmdates::is_valid_day_basis()] string.
#' Defaults to `NULL` which uses the curve's native day basis.
#' @param ... further arguments passed to specific methods
#'
#' @return an [InterestRate][InterestRate()] object of interpolated zero rates
#' with the `compounnding` and `day_basis` requested.
#' @export
Expand Down
2 changes: 1 addition & 1 deletion man/CDSSpec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions man/CreditCurve.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/SurvivalProbabilities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions man/ZeroHazardRate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 2 additions & 15 deletions man/as_SurvivalProbabilities.CDSCurve.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eef3ffd

Please sign in to comment.