Created
July 4, 2012 22:48
-
-
Save sethrylan/3049928 to your computer and use it in GitHub Desktop.
ST101_02.06 Homework
This file contains 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
print.matrix <- function(m){ | |
write.table(format(m, justify="right"), | |
row.names=T, col.names=T, quote=F) | |
} | |
p_loaded = 0.1 | |
p_hfair = 0.5 | |
p_hloaded = 0.9 | |
cells <- c(4, 0,10, 0, 20, 0, 0, 5, 2, 3) | |
rnames <- c("# heads", "# tails") | |
# creates a list of in form ("#1", "#2", "#n"...) to n=number of trials | |
cnames <- c(paste("#", 1:(length(cells)/2), sep = "")) | |
trials <- matrix(cells, nrow=2, ncol=5, byrow=FALSE, dimnames=list(rnames, cnames)) | |
#print(trials) | |
# P(fair|flips) = (P(flips|fair) * P(fair))/ P(flips) | |
for (trial in 1:ncol(trials)) { | |
# joint probablities for fair and loaded coins given observed results | |
jp_fair = trials[1, trial] * p_hfair + trials[2, trial] * (1-p_hfair) | |
jp_loaded = trials[1, trial] * p_hloaded + trials[2, trial] * (1-p_hloaded) | |
if (jp_fair < jp_loaded) { | |
print(paste("trial ", trial, " is < 0.5 probable for a fair coin!", sep="")) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment