1

I'm forecasting a stock price 'St1' starting at 'Stt = 10' at time t. Dates are not important. I want to find the next 100 prices using the following formula:

St1 <- Stt*exp((r-0.5*sigma^2)*T/n+sigma*E*sqrt(T/n))

So I'm starting with 10 and then need the next 100 values and put it in a data.frame or whatever is easy. How do I do this? I'm stuck trying this...

Stt = 10
r = 0.15
sigma = 0.2
T = 1
n = 100
E = 0.15

St1 = Stt
for (i in 1:100)
{
    St1[i] <- Stt*exp((r-0.5*sigma^2)*T/n+sigma*E*sqrt(T/n))
}
1
  • Please add a reproducible example Commented Mar 18, 2016 at 9:55

1 Answer 1

1

One way is this

r = 0.15
sigma = 0.2
T = 1
n = 100
E = 0.15

Stt<- rep(NA,n) # preallocate
Stt[1] <- 10
for (i in 2:100)
{
    Stt[i] <- Stt[i-1]*exp((r-0.5*sigma^2)*T/n+sigma*E*sqrt(T/n))
}

Without for loop, and in order to use rnorm for E, this code could work:

# for fixed E of length one
Stt <- cumprod(c(10,rep(exp((r-0.5*sigma^2)*T/n+sigma*E*sqrt(T/n)), n-1)))

# for random vector E 
E <- rnorm(n-1, mean=0.15, sd=0.01)
Stt <- cumprod(c(10, exp((r-0.5*sigma^2)*T/n+sigma*E*sqrt(T/n))))
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm, tnx a lot!
What if I wanted to make E a normal random variabel? rnorm(100) - gives me error message " number of items to replace is not a multiple of replacement length"
In the formula, replace E by E[i] since E is a vector. By the way, I would recommend you to vectorize the whole formula.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.