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, 2019 in Data Analytics by Sophie may
• 10,620 points
1,580 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, 2019 by Tyrion anex
• 8,700 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, 2019 in Data Analytics by anonymous
• 33,030 points
15,068 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,840 points
811 views
+3 votes
2 answers

Error: could not find function - R Programming

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

answered Aug 23, 2019 in Data Analytics by anonymous
• 33,030 points
33,637 views
+1 vote
1 answer

R programming Web Scraping

Try something like this: library(rvest) library(rvest) library(tidyverse) urls <- read_html("http://dk.farnell.com/c/office-computer-networking-products/prl/results/") pag <- ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,840 points
962 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, 2019 in Data Analytics by Sophie may
• 10,620 points
1,210 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,578 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,840 points
698 views
0 votes
1 answer

R programming: Using Caret package to implement Random Forest

You can check out the official docs ...READ MORE

answered Apr 30, 2019 in Data Analytics by Tyrion anex
• 8,700 points
686 views
+1 vote
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,700 points
629 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP