1

I have not a real problem but I don't quite get the difference in my code due to different writing schemes.

The situation is as follows:

I wrote a for loop that should insert certain values in my function. Before I did this I defined the parameters to insert. The code looks like this:

yield <- rep(1, nrow(timeseries1))
pc <- c(1:1265)
tc <- c(1:1265)
for(i in 1:1265){
  pc[i] <- timeseries1$`CoCo-Preis`[i]
  tc[i] <- timeseries1$t[i]
  yield[i] <- yield.to.call(ytc,p=pc[i],t=tc[i],c=5,N=100)
}
yield <- data.table(yield)

I simply want to tell the loop to insert the values from i=1 to i=1265 but what he does is to define i as 1264L. When i run another code AFTER I did the above he assigns i to 1265. The code is:

for(i in c(1,1265)){
  pc[i] <- timeseries1$`CoCo-Preis`[i]
  tc[i] <- timeseries1$t[i]
  yield[i] <- yield.to.call(ytc,p=pc[i],t=tc[i],c=5,N=100)
}
yield <- data.table(yield)

(Note the new interval as a vector) Now he especially offers me the right solution for the last value. But when I only run this second code it does not yield the right solutions at all apart from only the first value. So my workaround is to run both codes after each other but I want to know why I simply cannot run the second code only or why the first code stops before the last value.

I hope that anyone can help.

EDIT:

yield.to.call <- function(ytc,p,c,t,f=4,N=100){
c <- c/f #new to adjust the coupon
if(t<92){t <- ((92-t)/(92-0))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+c/((1+(ytc/f)))^(t+14)+c/((1+(ytc/f)))^(t+15)+c/((1+(ytc/f)))^(t+16)+c/((1+(ytc/f)))^(t+17)+c/((1+(ytc/f)))^(t+18)+(c+N)/((1+(ytc/f)))^(t+19)-p}}
else if(t<184){t <- ((184-t)/(184-92))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+c/((1+(ytc/f)))^(t+14)+c/((1+(ytc/f)))^(t+15)+c/((1+(ytc/f)))^(t+16)+c/((1+(ytc/f)))^(t+17)+(c+N)/((1+(ytc/f)))^(t+18)-p}}
else if(t<275){t <- ((275-t)/(275-184))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+c/((1+(ytc/f)))^(t+14)+c/((1+(ytc/f)))^(t+15)+c/((1+(ytc/f)))^(t+16)+(c+N)/((1+(ytc/f)))^(t+17)-p}}
else if(t<365){t <- ((365-t)/(365-275))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+c/((1+(ytc/f)))^(t+14)+c/((1+(ytc/f)))^(t+15)+(c+N)/((1+(ytc/f)))^(t+16)-p}}
else if(t<457){t <- ((457-t)/(457-365))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+c/((1+(ytc/f)))^(t+14)+(c+N)/((1+(ytc/f)))^(t+15)-p}}
else if(t<548){t <- ((548-t)/(548-457))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+c/((1+(ytc/f)))^(t+13)+(c+N)/((1+(ytc/f)))^(t+14)-p}}
else if(t<639){t <- ((639-t)/(639-548))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+c/((1+(ytc/f)))^(t+12)+(c+N)/((1+(ytc/f)))^(t+13)-p}}
else if(t<730){t <- ((730-t)/(730-639))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+c/((1+(ytc/f)))^(t+11)+(c+N)/((1+(ytc/f)))^(t+12)-p}}
else if(t<821){t <- ((821-t)/(821-730))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+c/((1+(ytc/f)))^(t+10)+(c+N)/((1+(ytc/f)))^(t+11)-p}}
else if(t<915){t <- ((915-t)/(915-821))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+c/((1+(ytc/f)))^(t+9)+(c+N)/((1+(ytc/f)))^(t+10)-p}}
else if(t<1006){t <- ((1006-t)/(1006-915))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+c/((1+(ytc/f)))^(t+8)+(c+N)/((1+(ytc/f)))^(t+9)-p}}
else if(t<1097){t <- ((1097-t)/(1097-1006))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+c/((1+(ytc/f)))^(t+7)+(c+N)/((1+(ytc/f)))^(t+8)-p}} 
else if(t<1188){t <- ((1188-t)/(1188-1097))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+c/((1+(ytc/f)))^(t+6)+(c+N)/((1+(ytc/f)))^(t+7)-p}}
else if(t<1280){t <- ((1280-t)/(1280-1188))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+c/((1+(ytc/f)))^(t+5)+(c+N)/((1+(ytc/f)))^(t+6)-p}}
else if(t<1371){t <- ((1371-t)/(1371-1280)) 
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+c/((1+(ytc/f)))^(t+4)+(c+N)/((1+(ytc/f)))^(t+5)-p}}
else if(t<1461){t <- ((1461-t)/(1461-1371))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+c/((1+(ytc/f)))^(t+3)+(c+N)/((1+(ytc/f)))^(t+4)-p}}
else if(t<1553){t <- ((1553-t)/(1553-1461)) 
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+c/((1+(ytc/f)))^(t+2)+(c+N)/((1+(ytc/f)))^(t+3)-p}}
else if(t<1645){t <- ((1645-t)/(1645-1553))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+c/((1+(ytc/f)))^(t+1)+(c+N)/((1+(ytc/f)))^(t+2)-p}}
else if(t<1736){t <- ((1736-t)/(1736-1645))
q <- function(ytc,p,c,t,f=4,N=100){c/((1+(ytc/f)))^(t)+(N+c)/((1+(ytc/f)))^(t+1)-p}}  
else if(t<1826){t<-((1826-t)/(1826-1736))
q <- function(ytc,p,c,t,f=4,N=100){(N+c)/(1+(ytc/f))^(t)-p}} 
else q <- function(ytc,p,c,t=0,f=4,N=100){((N+c)/p)-((ytc/f))-1} 

q2<- function(ytc){q(ytc,p,c,t,N=100)}
yield.t.c <- uniroot(q2,c(-1,10),tol = .Machine$double.eps^0.5)$root 
return(yield.t.c)
}

Sample data:

structure(list(Datum = structure(c(17960, 17961, 17962, 17963, 
17966, 17967), class = "Date"), `CoCo-Preis` = c(98.19, 98.14, 
98.21, 97.97, 98.01, 98.03), `CDS-Spread` = c(55.67, 55.23, 55.64, 
56.27, 55.05, 53.33), Aktienpreis = c(4.244, 4.3145, 4.163, 4.0785, 
4.1375, 4.142), Zins = c(-0.287467, -0.31443, -0.360349, -0.351772, 
-0.363637, -0.36096), t = c(1819L, 1820L, 1821L, 1822L, 1825L, 
1826L)), row.names = 1260:1265, class = "data.frame")
Leon
  • 37
  • 1
  • 8
  • Do you get any error messages when it stops at 1264? – Sven Jun 19 '19 at 10:18
  • No. It seems to print the same value as for i=1264 into the table. The value should be like 0.133 but is 1 which is equal to the value of the 1294 i. – Leon Jun 19 '19 at 10:19
  • But this is then adjusted by the second code. Don't know why but it works. I am only wondering what causes this in the first code. – Leon Jun 19 '19 at 10:20
  • `c(1, 1265)` has two elements; `1:1265` has 1265 elements. Is that the issue? – user2554330 Jun 19 '19 at 12:22
  • @Leon, and at the end of the first for loop, your value for `i` is `1264L`? That's very strange... Do you have some sample code we can reproduce this with? – Sven Jun 19 '19 at 13:28
  • @user2554330 when I insert the vector in the loop he is going to insert all values from 1 to 1265 so I don't understand why it only has too elements then? I thought it is simply another way to express the interval – Leon Jun 19 '19 at 13:48
  • @sven what do you mean by sample code? Do you mean data? – Leon Jun 19 '19 at 13:48
  • @user2554330 Now I see what you mean! In the second code he is only adding the value for i=1265 then. okay I understand... But why is he then not going up to that i=1265 in the first equation? – Leon Jun 19 '19 at 13:52
  • @Leon, sorry yes, I meant sample data. – Sven Jun 19 '19 at 13:55
  • @sven How can I provide my data? – Leon Jun 19 '19 at 13:56
  • Post the output of `dput(data)` or if that's too big, `dput(head(data))` for each of the data pieces we would need. In this case I suppose we only need it for `timeseries1`. – Sven Jun 19 '19 at 13:57
  • @Sven Is there a way to post the last values only? Because that are the interesting ones for the function. – Leon Jun 19 '19 at 14:01
  • Sure, use tail instead of head. – Sven Jun 19 '19 at 14:01
  • added the function and will add the data in a second – Leon Jun 19 '19 at 14:06
  • Okay and it seems as if the data type was not the problem. I don't get it why the first code of the loop does not add my last value of i=1265 correctly. – Leon Jun 19 '19 at 14:12
  • The problem is with your `uniroot` function. it seems when there isn't a root at that interval, your loop stops. You can try to allow the function to extend the interval with `yield.t.c <- uniroot(q2,c(-1,10),tol = .Machine$double.eps^0.5, extendInt = "yes")$root `. It works for me then. – Sven Jun 19 '19 at 15:00
  • That was it!! Thank you! – Leon Jun 19 '19 at 15:10
  • Hooo... that's heck of a function. You may be interested in opening a new question asking for help writing a nicer version of the function---one that is readable and has much lower probability of a unnoticed typo causing a bug. – Gregor Thomas Jun 19 '19 at 15:13

1 Answers1

0

When reproducing, it seems the loop stops at the 2nd but last iteration of the loop, hence your i being stuck at 1264L or in the sample data at the 5th iteration. From here it was clear that the problem is with the intervals of the uniroot function. As per the suggestion in that link, it worked for me when adding the extendInt = "yes" to the line.

yield.t.c <- uniroot(q2,c(-1,10),tol = .Machine$double.eps^0.5, extendInt = "yes")$root 

Hope it helps!

Sven
  • 1,203
  • 1
  • 5
  • 14