Anita
Anita

Reputation: 789

R: order of calculation

VN <- function(n,s,y){
   if (n < N){
      V <- 1/(1+r)*(ptil*VN(n+1,u*s,a*u*s+y)+qtil*VN(n+1,s*d, a*d*s+y))
      return(V)
   }
   if (n == N){
      return(max(c(0,y-K)))
   }  
}
VN(0,S0, a*S0)

How does R calculate this? Does R first calculate VN(1,us,aus+y), VN(2,us,aus+y), VN(3,us,aus+y),... until VN(N,us,aus+y) and then start with VN(2,ds,ad*s+y),...? Or what is the correct order for R to calculate this? Take for example N = 3.

Upvotes: 0

Views: 304

Answers (1)

Oliver Keyes
Oliver Keyes

Reputation: 3304

The general rule is that R evaluates inside-to-out (lowest nested expression to highest nested expression), and left-to-right after that. If you're worried about things being evaluated in the wrong order, braces are the answer, since they separate the equation you're using into distinct expressions.

Upvotes: 1

Related Questions