因此,我试图根据从Datastream检索到的数据来计算每日成熟度。该数据包括EMU国债,价格,优惠券和到期日。在R中,构建矩阵,每天所有价格,优惠券和时间安排都与其日期相对应。成熟后,所有三个变量都为0(零)。
使用bondvalue函数:
bondvalue = function(c,T,r,par)
{
bv = c/r + (par - c/r) * (1+r)^(-2*T) #from (3.3)
bv
}
我基本上使用:
Yields = c(Yields,
uniroot(function(r) bondvalue(Coupon*100, Prices, r, 100) - Prices,
c(-1, 1))$root)
找到到期日的收益率。 但是,如前所述,我想计算到期日收益率。因此我使用for循环:
daily_ytm <- matrix(NA,ncol = ncol(Prices), nrow = nrow(Prices))
for (i in 1:nrow(Prices)) {
for (j in 1:ncol(Prices)) {
if (Prices[i,j] == 0){
daily_ytm[i,j]=0
}
else if (Prices > 0){
daily_ytm[i,j]=c(daily_ytm,
uniroot(function(r) bondvalue(Coupon*100, Prices, r, 100) - Prices,
c(-1, 1))$root)
}
}
}
我希望在新矩阵中得到0(零),只要价格/优惠券/成熟度为零(或最初为NA),否则它应该使用uniroot()找到ytm。
但是,我收到错误:
Error in pmax.int(pmin(x, .Machine$double.xmax), -.Machine$double.xmax) :
invalid input type
In addition: Warning messages:
1: In if (Prices > 0) { :
the condition has length > 1 and only the first element will be used
2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
the condition has length > 1 and only the first element will be used
3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
the condition has length > 1 and only the first element will be used
4: In cbind(is.na(mmm), is.na(each)) :
number of rows of result is not a multiple of vector length (arg 2)
我在这里做错了什么?
> head(Prices)
PTOTEAOE0013 PTOTEMOE0001 DE0004109103 PTOTEOOE0009
1999-12-31 105.88 100.13 120.033 108.4
2000-01-03 105.75 100.13 120.033 108.4
2000-01-04 105.85 100.13 120.033 108.4
2000-01-05 105.92 100.13 120.033 108.4
2000-01-06 105.92 100.13 120.033 108.4
2000-01-07 106.20 100.13 120.033 108.4
> head(Coupon[,1:4])
PTOTEUOE0001 PTOTEAOE0013 PTOTEMOE0001 DE0004109103
1999-12-31 0.0875 0.048125 0.10625 0.07125
2000-01-03 0.0875 0.048125 0.10625 0.07125
2000-01-04 0.0875 0.048125 0.10625 0.07125
2000-01-05 0.0875 0.048125 0.10625 0.07125
2000-01-06 0.0875 0.048125 0.10625 0.07125
2000-01-07 0.0875 0.048125 0.10625 0.07125
> head(ttm_y[,1:4])
PTOTEUOE0001 PTOTEAOE0013 PTOTEMOE0001 DE0004109103
1 1.16667 3.25 3.41667 3.50000
2 1.16667 3.25 3.41667 3.41667
3 1.16667 3.25 3.41667 3.41667
4 1.16667 3.25 3.41667 3.41667
5 1.16667 3.25 3.41667 3.41667
6 1.16667 3.25 3.41667 3.41667
干杯