Last active
May 1, 2017 13:32
-
-
Save jsta/ccb2543df9241b3afd4b1f1c88d8597a to your computer and use it in GitHub Desktop.
Use naive bayes to calculate distribution mean + credible interval
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
# http://math.utoledo.edu/~mleite/math-EES-seminar/week1Baysean.pdf | |
data(iris) | |
# Estimation #### | |
y <- iris$Sepal.Length | |
y_bar <- mean(y) | |
y_sd <- sd(y) | |
n <- length(y) | |
# Confidence Interval #### | |
ci_mu <- y_bar + y_sd/sqrt(n) * qt(c(0.025, 0.975), n - 1) | |
ci_sigma <- y_sd * sqrt((n - 1)) / qchisq(c(0.025, 0.975), n - 1) | |
# Naive Bayes #### | |
n.sims <- 5000 | |
chi2 <- rchisq(n.sims, df = n - 1) | |
sigma <- y_sd * sqrt((n - 1) / chi2) # likelihood * prior ? | |
mu <- rnorm(n.sims, y_bar, sigma/sqrt(n)) | |
y_tilde <- rnorm(n.sims, mu, sigma) | |
# Plotting #### | |
par(mfrow = c(1, 2)) | |
hist(y) | |
abline(v = c(y_bar, ci_mu), col = "red") | |
hist(y_tilde) | |
abline(v = c(y_bar, mu[order(mu)][length(mu) * c(.025, 0.975 )]), col = "red") # mean +- 95% credible interval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment