Skip to content

Instantly share code, notes, and snippets.

@gjkerns
Created February 15, 2014 23:39
Show Gist options
  • Save gjkerns/9026890 to your computer and use it in GitHub Desktop.
Save gjkerns/9026890 to your computer and use it in GitHub Desktop.
Weak Law of Large Numbers
####################################################################
# WLLN
samp.size <- 5
mu <- 10
sigma <- 3
histN <- 1000
xbar <- rnorm( histN, mean = mu, sd = sigma/sqrt(samp.size) )
hist( xbar,
xlim = mu + sigma*c(-3,3),
xlab = "",
ylab = "",
main = "Weak Law of Large Numbers",
sub = "Sample size is n")
abline( v = mu, lty = 3)
WLLN <- function( samp.size = 3,
mu = 0,
sigma = 1,
histN = 1000){
require(TeachingDemos)
xbar <- rnorm( histN, mean = mu, sd = sigma/sqrt(samp.size) )
hist(xbar, freq = FALSE, xlim = c(-3,3))
lines(density(xbar))
abline( v = mu, lty = 3)
mtext(expression(mu), cex=2, at = mu, col="blue")
hist.refresh <- function(...){
samp.size <- slider(no = 1)
mu <- slider(no = 2)
sigma <- slider(no = 3)
histN <- slider(no = 4)
xr <- mu + sigma*c(-3,3)
xbar <- rnorm( histN, mean = mu, sd = sigma/sqrt(samp.size) )
hist( xbar,
freq = FALSE,
xlim = c(-3,3),
main = "Weak Law of Large Numbers",
xlab = expression(paste("Sampling Distribution of ", bar(italic(X))))
)
lines(density(xbar))
abline( v = mu, lty = 3, lwd= 2, col = "blue")
mtext(expression(mu), cex=2, at = mu, col="blue")
}
slider( hist.refresh,
c("Sample size n", "Population mean", "Population standard deviation", "Simulations for hist()"),
c(1, -3, 0.1, 10),
c(1000, 3, 2, 5000),
c(1, 0.1, 0.1, 1),
c(1, 0, 1, 1000),
title = "Weak LLN Demo")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment