R Programming: Implement Newton Raphson Algorithm

0 votes

I'm a student and I want to solve a textbook exercise which is implementing Newton-Raphson Algorithm in R Programming.

This is the exercise:

#Inputs:
  s0 <- 2.36
  E <- 2.36 
  r <- 0.01 
  t <- 1 
  c <- 0.1875

#Initial values
  sigma <-0.10 
  sig <- rep(0,10)
  sig[1] <- sigma

#Newton-Raphson method:
for(i in 2:100){
  d1 <- (log(s0/E)+(r+sigma^2/2)*t)/(sigma*sqrt(t)) 
  d2 <- d1-sigma*sqrt(t) 
  f <- s0*pnorm(d1)-E*exp(r*t)*pnorm(d2)-c 

#Derivative of d1 w.r.t. sigma:
      d11 <- (sigma^2*t*sqrt(t)-(log(s0/E)+(r+sigma^2/2)*t)*sqrt(t))/(sigma^2*t) 

#Derivative of d2 w.r.t. sigma: 
      d22 <- d11-sqrt(t) 

#Derivative of f(sigma):
      f1 <- s0*dnorm(d1)*d11-E*exp(-r*t)*dnorm(d2)*d22 

#Update sigma: 
    sigma <- sigma - f/f1 
    sig[i] <- sigma
    if(abs(sig[i]-sig[i-1]) < 0.00000001){sig <- sig[1:i]; break}}sig

The result for sig is [1] 0.1000000 0.2140636 0.2117527 0.2117864 0.2117859 0.2117859

But the answer in my textbook is: [1] 0.1000000 0.1877024 0.1876218 0.1876218

Can some one tell me where I've gone wrong?

Jan 7 in Data Analytics by Sophie may
• 9,870 points
134 views

1 answer to this question.

0 votes

The problem is that the expressions for f and f1 aren't matching. The term exp(r*t) in the expression for f should read exp(-r*t) if the expression for f1 is correct. Define f like this:

f <- s0*pnorm(d1)-E*exp(-r*t)*pnorm(d2)-c

When you change this, you'll get an output:

[1] 0.1000000 0.1877024 0.1876218 0.1876218

answered Jan 7 by Tyrion anex
• 8,310 points

Related Questions In Data Analytics

0 votes
2 answers

What are the rules to define a variable name in R programming language?

The same rules almost follow for all ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 32,260 points
119 views
0 votes
1 answer

Why should I adopt R programming

R Programming is the best mechanism for ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
35 views
0 votes
2 answers

Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

answered Aug 22 in Data Analytics by anonymous
• 32,260 points
5,754 views
0 votes
1 answer

R programming Web Scraping

Try something like this: library(rvest) library(rvest) library(tidy ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
118 views
0 votes
1 answer

R programming: Flip coin simulation

Make use of the ggplot2 package. Start by installing and ...READ MORE

answered Jan 21 in Data Analytics by Sophie may
• 9,870 points
76 views
+1 vote
2 answers
0 votes
1 answer

R programming logic

Use gsub to match the substring that we want ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,040 points
41 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

R programming error

Alright, you can either use gsub to match the ...READ MORE

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,310 points
36 views