 # Probability in a stock price

Hi

I made a simulated stock price using:

``````for (X in 1:n){
Y<-rnorm(n,mu,sigma)
X<-100*exp(cumsum(Y))
}
``````

Where n = 1000, mu = 0.0002, sigma = 0.01

I need som help to find:

``````E(X_1000)
P(X_1000 > 200)
``````

And the probability that the stock price will go below 75 during the 1000 days by using the minimum function.

So far i've tried to find P(X_1000 > 200) by:

``````pnorm(200, mean=0.0002, sd=0.01, lower.tail=FALSE)
``````

But i'm just getting:

``````> pnorm(200, mean=0.0002, sd=0.01, lower.tail=FALSE)
 0
``````

Thank you for helping.

you have done 1 simulation of a random walk with 1000 steps in the walk.
To empirically calculate the two metrics you want, you will have to simulate many such walks, and analyse the results.

The first thing I note is that your for loop to create a random walk, does not need a for loop and can be simpler:

``````  X<-100*exp(cumsum(rnorm(1:n,mu,sigma)))
``````

I don't like to write for loops, and will normally use functional programming approach when possible. the tidyverse includes the useful sub-library `purrr` to make this easy. it has various `map*` functions that let your repeat an operation many times.
In the below I reuse n to be not just steps per walk, but also walks to simulate. I also only care to keep the last value (x_1000) I can then make a vector of all the x_1000 values , I call this `x_1000s`

``````
library(tidyverse)

x_1000s <- map_dbl(1:n,
~{
X<- 100*exp(cumsum(rnorm(1:n,mu,sigma)))
X_1000 <- X
})

(expected_from_average_of_bootstrap_sampling <- mean(x_1000s))

# p(x_1000) > 200
#from n what is the split seen in the bootstrap sampling ?
table(x_1000s>200)

#so as a single number
(prob_x_1000_gt_200 <-scales::percent(sum(x_1000s>200) / length(x_1000s)))
``````

you can alter the above to test whether there are values below 75 in each walk, and return the true/false in a vector, and then see the proportion of those that did and those that didn't. I'll leave that as an exercise for you so you have a chance to learn by doing.

`X` is distributed lognormal in your model. You could use that for an analytic result. Or you could generate many versions of your data and then take the mean of `x` to estimate the expected value.
(Ninjaed by @nirgrahamuk on the second part.)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.