Hi all,
I'm pretty new to R, and need some help in debugging this code. I got three errors that i couldn't find solutions online.
- Error: unexpected symbol in "while (sqrt((SA-IT1)^2 + (V0-IT2)^2) > 0.1*(1+sqrt(IT1^2 + IT2^2)) and"
- Error in VA/VE : non-numeric argument to binary operator.
- Error: unexpected '}' in "}" .
Any help is much appreciated! Many thanks for your time
The code:
library(foreign)
#Solving nonlinear system of two equations following newton's | broyden's starting values.
VE = 47440291
SE = 0.02396919
#SO = 4740291
#sigmaS = 0.02396919
D= 33404048
r=2.32
T-t = 1
#SO = 4740291
SA = 0.02396919
a = c(VE,SE)
fnewton <- function(x) {
y <- numeric(2)
d1 = (log(x[1]/D) + (r + x[2]^2/2)*T)/x[2]/sqrt(T)
d2 = d1 - x[2]*sqrt(T)
y[1] <- VE - (x[1]*pnorm(d1) - exp(-r*T)*D*pnorm(d2))
y[2] <- SE * VE - pnorm(d1)*x[2]*x[1]
y
}
#nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Broyden") \$x
nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Broyden")
nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Newton")
#Now, the iteration
V0 = 4740291
sigmaV = 0.02396919
#counter = 1
D1 <- function(V0, D, r, sigmaV, T)
{(log(V0/D) + (r + sigmaV^2/2)*T)/sigmaV/sqrt(T)}
D2 <- function(d1,sigmaV,T) {d1-sigmaV*sqrt(T)}
#SA = 0.02396919
f1 <- function(Va)
{Va*pnorm(D1(Va,D,r,SA,1)) - exp(-r)*D*pnorm(D2(D1(Va,D,r,SA,1), SE,1))-VE}
f2 <- function(Sa) {VA/VE*pnorm(D1(VA,D,r,Sa,1))*Sa -SE}
IT1 <- VE;
IT2 <- SE;
counter <- 1
while (sqrt((SA-IT1)^2 + (V0-IT2)^2) > 0.1*(1+sqrt(IT1^2 + IT2^2)) and (counter<1000)) {
SA <- IT2
IT1 <- uniroot(f1,c(0,VE*100))
VA <- IT1
IT2 <- uniroot(f2,c(0,SE*100))
counter <- counter + 1
}
``