Skip to content

Instantly share code, notes, and snippets.

@gjkerns
Created February 15, 2014 23:38
Show Gist options
  • Save gjkerns/9026878 to your computer and use it in GitHub Desktop.
Save gjkerns/9026878 to your computer and use it in GitHub Desktop.
Strong Law of Large Numbers
SLLN <- function( iter = 10,
p = 0.5){
require(TeachingDemos)
x <- rbinom( iter, size=1, prob = p)
plot( cumsum(x)/1:length(x),
type="l",
#lwd = 2,
ylab = "Relative Frequency",
xlab = "Cumulative Trials",
ylim = c(0,1))
abline( h = p, lty = 3)
plot.refresh <- function(...){
iter <- slider(no = 1)
p <- slider(no = 2)
xr <- c(1,iter)
x <- rbinom( iter, size=1, prob = p)
plot( cumsum(x)/1:length(x),
type="l",
lwd = -.002*iter+3,
ylab = "Relative Frequency",
xlab = "Cumulative Trials",
main = "Strong Law of Large Numbers",
ylim = c(0,1))
abline( h = p, lty = 3, lwd = -.002*iter+3)
}
slider( plot.refresh,
c("Sample size n", "Probability of event"),
c(10, 0),
c(1000000, 1),
c(1, 0.01),
c(10, 0.5),
title = "Strong LLN Demo")
}
cummax <- function(x){
z = rep(0,length(x))
for(i in 1:length(x)){
z[i] = max(x[1:i])
}
return(z)
}
rmin <- function(x){
z = rep(0,length(x))
for(i in 1:length(x)){
z[i] = max(x[1:i])
}
return(z)
}
limsup(rw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment