Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating log-normal random walks

Tags:

r

wolframalpha can generate log-normal random walks based on historical parameters for 6 months, 1 year, 2 years ahead

for example the GSPC index: http://www.wolframalpha.com/input/?i=GSPC

I was wondering how I could do this in R and I would be greatful for some guidance.

library(quantmod)
getSymbols("^GSPC", from ="2000-01-01")
like image 529
adam.888 Avatar asked Nov 24 '25 17:11

adam.888


1 Answers

How can I improve this to allow the
volatility to change through time according to a simple Markov chain?

library(ggplot2)
library(quantmod)
getSymbols("^GSPC", from ="2000-01-01")

oldata <-GSPC[,6]
oldata <-na.omit(oldata)

lastprice <-tail(olddata,1)
oldsteps <- tail(diff(log(oldata)),-1)
head(oldsteps)
n_days =100
percent <- exp(cumsum(rnorm(n_days,mean(oldsteps), apply(oldsteps, 2, sd))))
path2 <- exp(cumsum(rnorm(n_days,mean(oldsteps), apply(oldsteps, 2, sd))))
path3 <- exp(cumsum(rnorm(n_days,mean(oldsteps), apply(oldsteps, 2, sd))))

paths <- data.frame(T=c(1:100),path1,path2,path3 )

plot1 <- ggplot(data=paths, aes(x=T,y=percent )) + geom_line()
plot1 <- plot1+ geom_line(aes(x=T,y=path2))+  geom_line(aes(x=T,y=path3))
plot1 <- plot1+ ggtitle("pathways")
plot1
like image 102
adam.888 Avatar answered Nov 27 '25 09:11

adam.888



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!