# Illustration: CDF of a point (relevant to proof)
par(mar=c(0, 0, 0, 0))
plot(0, 0, xlim=c(-1, 1), ylim=c(-1, 1), bty='n', xaxt='n', yaxt='n', xlab='', ylab='')
polygon(c(0, 1, 1, 0), c(0, 0, 1, 1), col='gray', border=NA)
points(0, 0, pch=19, cex=2)
segments(-1, 0, 0, 0, lty=2, lwd=2)
segments(0, 0, 1, 0, lty=1, lwd=2)
segments(0, -1, 0, 0, lty=2, lwd=2)
segments(0, 0, 0, 1, lty=1, lwd=2)
text(0.1, -0.1, 'a', cex=2)
text(-0.5, -0.5, '0', cex=2)
text(-0.5, +0.5, '0', cex=2)
text(+0.5, -0.5, '0', cex=2)
text(+0.5, +0.5, '1', cex=2)

# LIL: Moderate n
n <- 100000
set.seed(1)
r <- 50
gap <- n/1000
nn <- seq(gap, n, gap)
Y <- matrix(rnorm(n*r/gap, sd=sqrt(gap)), n/gap, r)
X <- apply(Y, 2, cumsum) / sqrt(nn * log(log(nn)))
options(scipen=9999)
matplot(nn, X, lty=1, col=scales::alpha('gray50', 0.5), bty='n', type='l', las=1, xlab='n', ylim=c(-2,2))
abline(h=sqrt(2), lty=2, col='slateblue', lwd=2)
abline(h=-sqrt(2), lty=2, col='slateblue', lwd=2)

# LIL: Huge n
# Simulate data
set.seed(2)
r <- 50
gap <- 10^seq(1, 100, len=1000)
nn <- cumsum(gap)
Y <- matrix(rnorm(r*length(gap), sd=sqrt(gap)), length(gap), r)
Z <- apply(Y, 2, cumsum) / sqrt(nn * log(log(nn)))

# Plot
plot(log10(nn), Z[,1], lty=1, col='black', bty='n', type='l', las=1, xlab='n', ylab=expression(X[n]), ylim=c(-1.5, 1.5), xaxt='n')
at <- seq(0, 100, 20)
lab <- parse(text = paste0("10^", at))
axis(1, at=at, labels=lab)
abline(h=sqrt(2), lty=2, col='slateblue', lwd=2)
abline(h=-sqrt(2), lty=2, col='slateblue', lwd=2)
lines(log10(nn), qnorm(.975)/sqrt(log(log(nn))), lty=2, col="#FF4E37FF", lwd=2)
lines(log10(nn), -qnorm(.975)/sqrt(log(log(nn))), lty=2, col="#FF4E37FF", lwd=2)

# Plot a bunch of them (uncomment if you want to see what this looks like)
# matplot(log10(nn), Z, lty=1, col=scales::alpha('black', 0.1), bty='n', type='l', las=1, xlab='n', ylim=c(-1.5, 1.5), xaxt='n')
# axis(1, at=at, labels=lab)
# abline(h=sqrt(2), lty=2, col='slateblue', lwd=2)
# abline(h=-sqrt(2), lty=2, col='slateblue', lwd=2)
# lines(log10(nn), qnorm(.975)/sqrt(log(log(nn))), lty=2, col="#FF4E37FF", lwd=2)
# lines(log10(nn), -qnorm(.975)/sqrt(log(log(nn))), lty=2, col="#FF4E37FF", lwd=2)

