如何使用 R 中的 'termstrc' 包估算静态收益率曲线?

How to estimate static yield curve with 'termstrc' package in R?

我正在尝试使用 R 中的 termstrc 包来估计巴西的 static 收益率曲线。我正在使用函数 estim_nss.couponbonds 并将 0%息票利率和 0 美元的现金流,除了最后一个是 1000 美元(到期面值)——据我所知这是执行此操作的函数,因为 estim_nss.zeroyields 只计算动态曲线。问题是我收到以下错误消息:

"Error in (pos_cf[i] + 1):pos_cf[i + 1] : NA/NaN argument In addition: Warning message: In max(n_of_cf) : no non-missing arguments to max; returning -Inf "

我尝试使用 trace(estim_nss.couponbons, edit=T) 来追踪问题,但我找不到计算 pos_cf[i]+1 的位置。根据名称,我认为它可能来自 postpro_bond 函数并使用了 trace(postpro_bond, edit=T),但我无法再次找到计算。我相信 "cf" 来自现金流量,因此现金流量的计算可能存在某种问题。我用 create_cashflows_matrix 来测试这个理论,但效果很好,所以我不确定问题出在现金流量上。

密码是:

#Creating the 'couponbond' class
ISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019', 'ltn_2021','ltn_2023')) #Bond's identification

MATURITYDATE <- as.Date(c(42736, 43101, 43466,  44197,  44927), origin='1899-12-30') #Dates are in system's format

ISSUEDATE <- as.Date(c(41288,41666,42395, 42073, 42395), origin='1899-12-30') #Dates are in system's format

COUPONRATE <- rep(0,5) #Coupon rates are 0 because these are zero-coupon bonds

PRICE <- c(969.32,  867.77, 782.48, 628.43, 501.95) #Prices seen 'TODAY'

ACCRUED <- rep(0.1,5) #There is no accrued interest in the brazilian bond's market

#Creating the cashflows sublist
CFISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019', 'ltn_2021', 'ltn_2023')) #Bond's identification

CF <- c(1000,1000,1000,1000,1000)# The face-values

DATE <- as.Date(c(42736,    43101,  43466,  44197,  44927), origin='1899-12-30') #Dates are in system's format

CASHFLOWS <- list(CFISIN,CF,DATE)
names(CASHFLOWS) <- c("ISIN","CF","DATE")

TODAY <- as.Date(42646, origin='1899-12-30')

brasil <- list(ISIN,MATURITYDATE,ISSUEDATE,
               COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)

names(brasil) <- c("ISIN","MATURITYDATE","ISSUEDATE","COUPONRATE",
                   "PRICE","ACCRUED","CASHFLOWS","TODAY")

mybonds <- list(brasil)

class(mybonds) <- "couponbonds"

#Estimating the zero-yield curve
ns_res <-estim_nss.couponbonds(mybonds, 'brasil' ,method = "ns")

#Testing the hypothesis that the error comes from the cashflow matrix
cf_p <- create_cashflows_matrix(mybonds[[1]], include_price = T)
m_p <- create_maturities_matrix(mybonds[[1]], include_price = T)
b <- bond_yields(cf_p,m_p)

请注意,我知道这个 question 报告了同样的问题。但是,它是针对动态曲线的。除此之外,没有有用的答案。

您的代码有两个问题。 (1) 没有命名第一个列表(这是错误的直接原因。但是如果修改它,则会发生另一个错误)。 (2) cashflows子表中,至少一级ISIN需要多于1条数据。

  # ... 
CFISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019', 
                         'ltn_2021', 'ltn_2023',  'ltn_2023'))   # added a 6th element
CF <- c(1000,1000,1000,1000,1000,  1000)                         # added a 6th
DATE <- as.Date(c(42736,43101,43466,44197,44927,  44928), origin='1899-12-30') # added a 6th

CASHFLOWS <- list(CFISIN,CF,DATE)
names(CASHFLOWS) <- c("ISIN","CF","DATE")
TODAY <- as.Date(42646, origin='1899-12-30')
brasil <- list(ISIN,MATURITYDATE,ISSUEDATE,
               COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)
names(brasil) <- c("ISIN","MATURITYDATE","ISSUEDATE","COUPONRATE",
                   "PRICE","ACCRUED","CASHFLOWS","TODAY")

mybonds <- list(brasil = brasil)                      # named the list

class(mybonds) <- "couponbonds"
ns_res <-estim_nss.couponbonds(mybonds, 'brasil', method = "ns")
注意:错误来自这些行
bonddata <- bonddata[group]    # prepro_bond()'s 1st line (the direct reason).

# cf <- lapply(bonddata, create_cashflows_matrix)   # the additional error
create_cashflows_matrix(mybonds[[1]], include_price = F)  # don't run