Skip to content

Instantly share code, notes, and snippets.

@Kornel
Last active September 14, 2016 09:29
Show Gist options
  • Save Kornel/191382985d2409d6fb95232a1a4a4e2c to your computer and use it in GitHub Desktop.
Save Kornel/191382985d2409d6fb95232a1a4a4e2c to your computer and use it in GitHub Desktop.
Plotting "the most dangerous equation in the world": http://press.princeton.edu/chapters/s8863.pdf. Monte Carlo test how the standard deviation changes with respect to the sample size.
split.into.intervals <- function(what, interval.count = NULL, interval.size = NULL) {
if (sum(sapply(list(interval.count, interval.size), is.null)) != 1)
stop("exactly one of 'interval.count', 'interval.size', must be NULL")
if (!is.null(interval.count))
n <- length(what) / interval.count
else
n <- interval.size
split(what, ceiling(seq_along(what) / n))
}
N <- 10000
xs <- seq(2, N / 2, by = 2)
X <- rnorm(N, mean = 0, sd = 10)
sds <- sapply(xs, function(n) {
sd(sapply(split.into.intervals(X, interval.size = n), mean))
})
take <- 200
plot(xs[1:take], sds[1:take], type = 'l', col = 'blue', xlab = 'sample size', ylab = 'standard deviation')
lines(xs[1:take], (sd(X) / sqrt(xs))[1:take], col = 'darkgoldenrod2', lwd = 2)
@Kornel
Copy link
Author

Kornel commented Sep 14, 2016

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment