Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Created March 28, 2014 08:13
Show Gist options
  • Save benmarwick/9827728 to your computer and use it in GitHub Desktop.
Save benmarwick/9827728 to your computer and use it in GitHub Desktop.
# http://stackoverflow.com/questions/19208502/levy-walk-simulation-in-r
alpha=2.2
n=10000
x=rep(0,n)
y=rep(0,n)
# Note that you need 0 < fmin < fmax.
fmin <- 0.01
fmax <- 1000
theta <- runif(n-1)*2*pi
f <- runif(n-1, fmax^(-alpha), fmin^(-alpha))^(-1/alpha)
x <- c(0, cumsum(f*cos(theta)))
y <- c(0, cumsum(f*sin(theta)))
ggplot(data.frame(x=x, y=y), aes(x, y)) +
geom_point() +
geom_path() +
theme_minimal()
ggplot(data.frame(f=f), aes(f)) +
geom_histogram() +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment