Last active
December 24, 2015 12:19
-
-
Save ascv/6796978 to your computer and use it in GitHub Desktop.
lego man
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
legoSim <- function() { | |
i = 1 | |
legos <- c() | |
while(TRUE) { | |
x <- sample(1:16, 1) | |
if (x %in% legos == FALSE) { | |
legos <- c(legos, x) | |
if (length(legos) == 16) { break } | |
} | |
else { i <- i + 1 } | |
} | |
return(i) | |
} | |
makeCDF <- function(lst) { | |
out = c(lst[1]) | |
z = 2 | |
s = out[1] | |
while(z < length(lst)) { | |
out[z] <- lst[z] + s | |
s <- out[z] | |
z <- z + 1 | |
} | |
return(out) | |
} | |
x <- replicate(5000, legoSim()) | |
h <- hist(x, prob=TRUE, breaks=seq(1, 199, by=1)) | |
cdf <- makeCDF(h$density) | |
plot(cdf, | |
type='l', | |
main='lego man empirical CDF', | |
ylab='P(X <= k)', | |
xlab='k = number of flips', | |
xaxt='n', | |
col='blue' | |
) | |
lbls <- seq(1, length(cdf), by=1) | |
axis(1, labels=seq(1,199,by=5), at=seq(1,199,by=5)) | |
segments(1, .4916, 34, .4916, col='red', lty=2) | |
segments(34, .4916, 34, 0, col='red', lty=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment