R/market_interest_rate.R

Defines functions InterestRateTermStructure

setClass(
    Class          = "InterestRateTermStructure",
    representation = representation(
        dayCount = "character",
        period   = "character",
        spotLag  = "integer",
        tenor    = "character",
        value    = "numeric"
    ),
    validity      = function(object) {
        valueLength <- length(object@value)
        if (length(object@dayCount) != valueLength ||
            length(object@period) != valueLength ||
            length(object@spotLag) != valueLength ||
            length(object@tenor) != valueLength) {
            "all properties must have the same length"
        } else {
            TRUE
        }
    } 
)

InterestRateTermStructure <- function(dayCount, period, spotLag, tenor, value,
                                      valueType = "value") {
    new(Class    = "InterestRateTermStructure",
        dayCount = dayCount, 
        period   = period, 
        spotLag  = spotLag, 
        tenor    = tenor, 
        value    = .AdjustValue(value, valueType))
}

setMethod(
    f          = "GenerateCashflow",
    signature  = signature(object     = "InterestRateTermStructure",
                           convention = "DayConvention"),
    definition = function(object, convention) {
        IsBusinessDay       <- SetIsBusinessDay(convention)
        ConvertTenorToDate  <- SetTenorConverter(convention)
        PricingYearFraction <- SetyearFraction(DayCount())
        valueDate           <- ValueDate()
        
        mapply(FUN      = function(dayCount, period, spotLag, tenor, value) {
                   YearFraction <- SetYearFraction(dayCount)
                   startDate    <- valueDate
                   
                   while (spotLag > 0) {
                       startDate <- startDate %m+% lubridate::days()
                       
                       if (IsBusinessDay(startDate)) {
                           spotLag <- spotLag - 1
                       }
                   }
                   
                   dateSeq <- DateSequence(convention = convention,
                                           from       = startDate, 
                                           to         = tenor,
                                           by         = `if`(period == "", tenor, period))
                   
                   compoundPeriod <- YearFraction(startDate = dateSeq[-length(dateSeq)],
                                                  endDate   = dateSeq[-1])
                   
                   cashflowValue                        <- c(-1, compoundPeriod * value)
                   cashflowValue[length(cashflowValue)] <- cashflowValue[length(cashflowValue)] + 1

                   Cashflow(maturity = PricingYearFraction(startDate = ValueDate,
                                                           endDate   = dateSeq),
                            value    = cashflowValue)
                   
               },
               dayCount = object@dayCount, 
               period   = object@period, 
               spotLag  = object@spotLag, 
               tenor    = object@tenor, 
               value    = object@value)
    } 
)


setMethod(
    f          = "show",
    signature  = "InterestRateTermStructure",
    definition = function(object) {
        print(
            data.frame(tenor    = object@tenor,
                       value    = object@value,
                       dayCount = object@dayCount,
                       spotLag  = object@dayCount,
                       period   = object@period)
        )
    } 
)
gfunk0704/StochasticVolatility documentation built on Feb. 8, 2020, 10:04 a.m.