-1

This is my code below and all I get is a list of the same values and it doesn't seem like R is taking a randomly generated value, running through the equation and then giving me the results.

montecarlo = function(r,v,t,x,k,n) {
   y = rnorm(n)
   stockprice = x*exp((-v*sqrt(t)*y)+((r-(.5*v^2))*t))
   MCOP = exp(-r*t)*(stockprice-k)
   return(MCOP)
}

The output for this code is just a single value 100 times if I set n = 100

Jonathan Carroll
  • 3,897
  • 14
  • 34
Dmitriy
  • 37
  • 7

1 Answers1

1

Let's add some print() statements (you could also do this by using debug(montecarlo) to step through the function interactively):

montecarlo = function(r,v,t,x,k,n) {
   y = rnorm(n)
   p = (-v*sqrt(t)*y)+((r-(.5*v^2))*t)
   print(range(p))
   stockprice = x*exp(p)
   print(range(stockprice))   
   MCOP = exp(-r*t)*(stockprice-k)
   return(MCOP)
}

Now try the function:

set.seed(101)
m <- montecarlo(.45,65,1,30,29,100)
[1] -2232.440 -1961.294  ## exponent values
[1] 0 0                  ## stockprice values

So the exponents are of such large (negative) magnitude that the exponential function underflows to zero. Since the random values enter the expression only through stockprice, they are zeroed out.

It looks like the main culprit here is v, which you have set to 65, so v^2 is a large number ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thank you Ben I've figured it out and you're right, some of those values need to be smaller – Dmitriy Feb 01 '17 at 02:37
  • 2
    While the sentiment is appreciated, StackOverflow deprecates [using comments to say "thank you"](http://meta.stackoverflow.com/questions/258004/should-thank-you-comments-be-flagged?lq=1); if this answer was useful you can upvote it (if you have sufficient reputation), and in any case if it answers your question satisfactorily you are encouraged to click the check-mark to accept it. – Ben Bolker Feb 01 '17 at 02:38