Donsker’s invariance principle is a classical extension of the central limit theorem. It says that the path of a discrete random walk converges, if properly rescaled, in distribution to a standard Brownian motion. More details can be found in your favorite text book or on wikipedia. Here just comes an animation.

```
library(dplyr)
library(gganimate)
library(transformr)
set.seed(42)
n <- 10000 # maximal steps of random walk
n_seq <- seq(from=10, 100, by = 10) # starting phase
n_seq <- c(n_seq, seq(from=100, n, by = 100))
number_iteration <-length(n_seq)
X <- sample(c(-1,1), n, replace=TRUE, prob=c(0.5, 0.5))
S <- cumsum(X) # random walk S_n
W <- numeric(number_iteration*n)
id <- numeric(number_iteration*n)
time <- numeric(number_iteration*n)
for (i in 1:number_iteration){
for (j in (1:n)){
index <- floor(j/n * n_seq[i]) + 1
W[j+(i-1)*n] <- (S[index] + ((j/n * n_seq[i]) -(index-1)) *( S[index+1]-S[index]))/ sqrt(n_seq[i])
}
id[(1+n*(i-1)):(n*i)] <- rep(n_seq[i],n)
time[(1+n*(i-1)):(n*i)] <- (1:n)/n
}
rw <- data.frame(x = time,
y = W,
id =id)
p <- ggplot(rw, aes(x=x, y=y)) +
geom_line()+
ylab(expression(W^{(n)}~ (x)))+
transition_states(
id,
transition_length = 2,
state_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')+
labs(title = "Donsker's invariance principle. n = {closest_state}")
animate(p, nframes=number_iteration*2)
```