Skip to content

Instantly share code, notes, and snippets.

@blog1729
Created April 19, 2015 08:27
Show Gist options
  • Select an option

  • Save blog1729/0359f629b98070618c63 to your computer and use it in GitHub Desktop.

Select an option

Save blog1729/0359f629b98070618c63 to your computer and use it in GitHub Desktop.
Geometric Brownian motion
sigma = 0.3
mu = 0.06
N = 1000
flag = 0
lambda = c(0.01, 0.05, 0.1, 0.5, 1)
xs = c(5)
Xs = matrix(5, nrow = 5, ncol = N)
zs = rnorm(N)
for (i in 2:N)
{
xs[i] = xs[i-1] + (mu - 0.5*sigma*sigma)/N + sigma*sqrt(1/N)*zs[i]
}
for (j in 1:5)
{
for (i in 2:N)
{
n = rpois(1, lambda[j]*1/N)
M = 0
if(n != 0)
{
flag <<- flag+1
ys = 0.1*rnorm(n, mean = 0, sd = 1)+1
M = 0
for (k in 1:n)
M = M + log(ys[k])
}
Xs[j, i] = Xs[j,i-1] + (mu-0.5*sigma*sigma)/N + sigma*sqrt(1/N)*zs[i] + M
}
}
yM = max(max(xs), max(Xs))
ym = min(min(xs), min(Xs))
plot(xs, cex = 0.05, type="l", ylim=c(ym, yM))
lines(1:N, Xs[1,1:N], col="red")
lines(1:N, Xs[2,1:N], col="blue")
lines(1:N, Xs[3,1:N], col="green")
lines(1:N, Xs[4,1:N], col="violet")
lines(1:N, Xs[5,1:N], col="orange")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment