Skip to content

Commit

Permalink
Change xgrid to time x
Browse files Browse the repository at this point in the history
  • Loading branch information
FinYang committed Feb 12, 2024
1 parent 3bda64b commit b2758f7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions R/ycevo.R
Expand Up @@ -56,8 +56,8 @@
#'
#'
#' @param data Data frame; bond data to estimate discount curve from. See \code{?USbonds} for an example bond data structure.
#' @param xgrid Numeric vector of values between 0 and 1.
#' Time grids over the entire time horizon (percentile) of the data at which the discount curve is evaluated.
#' @param x Time grids at which the discount curve is evaluated.
#' Should be specified using the same class of object as the quotation date (\code{qdate}) column in \code{data}.
#' @param tau Numeric vector that
#' represents time-to-maturities in years where discount function and yield curve will be found
#' for each time point \code{xgrid}.
Expand Down Expand Up @@ -101,8 +101,8 @@
#' @importFrom lubridate days
#' @export
ycevo <- function(data,
xgrid,
hx = 1/length(xgrid),
x,
hx = 1/length(x),
cols = NULL,
tau = NULL,
ht = NULL,
Expand All @@ -111,13 +111,15 @@ ycevo <- function(data,
...){
stopifnot(is.data.frame(data))

if(anyDuplicated(xgrid)){
stop("Duplicated xgrid found.")
if(anyDuplicated(x)){
stop("Duplicated time grid x found.")
}
if(anyDuplicated(tau)){
stop("Duplicated tau found.")
}
stopifnot(!anyNA(xgrid))


stopifnot(!anyNA(x))
stopifnot(!anyNA(tau))

# The minimum required columns
Expand Down Expand Up @@ -145,6 +147,8 @@ ycevo <- function(data,
stop(paste0(names(dots)[temp], collapse = ", "), " column(s) not found in the data")
}

xgrid <- ecdf(data$qdate)(x)

# Handle interest rate
interest <- NULL
rgrid <- NULL
Expand Down Expand Up @@ -172,7 +176,7 @@ ycevo <- function(data,
if(length(hx) == 1) {
hx <- rep(hx, length(xgrid))
} else if(length(hx) != length(xgrid)) {
stop("Length of hx does not equal to length of xgrid.")
stop("Length of hx does not equal to length of x.")
}
# tau
if(is.null(tau)) {
Expand Down Expand Up @@ -237,7 +241,6 @@ ycevo <- function(data,
},
future.seed = TRUE
)
xgrid_time <- unname(quantile(getElement(data, "qdate"), xgrid, type = 1))

res <- output %>%
bind_rows() %>%
Expand All @@ -246,7 +249,8 @@ ycevo <- function(data,
tidyr::nest() %>%
ungroup() %>%
rename_with(function(x) rep(names(dots) %||% character(0), length(x)), any_of("rgrid")) %>%
mutate(!!sym(qdate_label) := xgrid_time, .before = 1)
mutate(!!sym(qdate_label) := x, .before = 1) %>%
select(-xgrid)

attr(res, "cols") <- cols
new_ycevo(res)
Expand Down

0 comments on commit b2758f7

Please sign in to comment.