title: A simple simulation example in R from a textbook
body: <p>I found this piece of code from a the textbook "Statistics and Data analysis for financial engineering," but I am confused about certain line in this code:</p>
  
  <p>This code tried to answer the question of What is the probability that the value of the stock will be below $950,000 at the close of at least one of the next 45 trading days? They provide the mean and SD too.</p>
  
  <p>Code: </p>
  
  <pre><code>niter = 1e5 # number of iterations
  below = rep(0,niter) # set up storage
  set.seed(2009)
  for (i in 1:niter)
  {
      r = rnorm(45,mean=.05/253,
      sd=.23/sqrt(253)) # generate random numbers
      logPrice = log(1e6) + cumsum(r)
      minlogP = min(logPrice) # minimum price over next 45 days
      below[i] = as.numeric(minlogP &lt; log(950000))
  }
  
  mean(below)
  </code></pre>
  
  <p>A few questions:</p>
  
  <ol>
  <li>I dont understand about <code>logPrice = log(1e6) + cumsum(r)</code>, why we use <code>log(1e6)</code> and why we have <code>cumsum(r)</code>?</li>
  <li>What is the purpose of this: <code>below[i] = as.numeric(minlogP &lt; log(950000))</code></li>
  <li>why do we use <code>log(950000)</code>? why do we need to <code>log</code>?</li>
  </ol>