diff --git a/lab/lab_week_04.Rmd b/lab/lab_week_04.Rmd new file mode 100644 index 0000000..db0785e --- /dev/null +++ b/lab/lab_week_04.Rmd @@ -0,0 +1,38 @@ +--- +title: "EEEB UN3005/GR5005 \nLab - Week 04 - 17 and 19 February 2020" +author: "USE YOUR NAME HERE" +output: pdf_document +fontsize: 12pt +--- + +```{r setup, include = FALSE} + +knitr::opts_chunk$set(echo = TRUE) +``` + + +# Bayesian Basics + + +## Exercise 1: Applying Bayes' Theorem using Grid Approximation + +Imagine if the series of observations in the globe tossing example from the *Statistical Rethinking* text and class were: W L W W, where "W" corresponds to water and "L" corresponds to land. + +With this set of observations, use grid approximation (with 11 grid points) to construct the posterior for the parameter *p* (the proportion of water on the globe). Assume a flat prior for *p*. + +Plot the posterior distribution. + +```{r} + +``` + + +## Exercise 2: Thinking Deeper with Bayes' Theorem + +Suppose in the globe tossing scenario there are actually two globes, one for Earth and one for Mars. The Earth globe is 30% land. The Mars globe is 100% land. Further suppose that one of these globes—-you don’t know which—-was tossed in the air and produced a “land” observation. Assume that each globe was equally likely to be tossed. Show that the posterior probability that the globe was Earth, conditional on seeing “land” (Pr(Earth|land)), is 0.23. + +Note, this problem might seem like it has a lot of information to consider, but it is actually a direct application of Bayes' Theorem. If you're having problems getting started, write out Bayes' Theorem. Also, R is not strictly necessary for this problem. You could do the math by hand, so R is really just a glorified calculator here. + +```{r} + +``` diff --git a/lecture/lecture_week_04.R b/lecture/lecture_week_04.R new file mode 100644 index 0000000..afb411c --- /dev/null +++ b/lecture/lecture_week_04.R @@ -0,0 +1,70 @@ + + +# Demonstrate grid approximation of the posterior for a globe tossing problem +# with 6 water observations out of 9 globe tosses + +#============================================================================== + + +# 1) Define the grid to be used to compute the posterior + +# In this case, let's say we choose 20 grid points +# Since the parameter of interest (proportion of water on the globe) is +# bounded by 0 and 1, our grid should have those limits as well +p_grid <- seq(from = 0, to = 1, length.out = 20) + +p_grid + +#============================================================================== + + +# 2) Compute/define the value of the prior at each parameter value on the grid + +# In this case, simply choose a flat prior +prior <- rep(1, 20) + +# You could visualize this prior +plot(p_grid, prior, pch = 19, type = "b") + +#============================================================================== + + +# 3) Compute the likelihood at each parameter value on the grid + +# This requires the use of a likelihood function applied to the observed data, +# evaluated at each potential parameter value on the grid +likelihood <- dbinom(6, size = 9, prob = p_grid) + +# You could also visualize the likelihood +plot(p_grid, likelihood, pch = 19, type = "b") + +#============================================================================== + + +# 4) Compute the unstandardized posterior at each parameter value on the grid + +# The unstandardized posterior is simply the product of the likelihood and +# prior +unstd.posterior <- likelihood * prior + +# Again, could visualize +plot(p_grid, unstd.posterior, pch = 19, type = "b") + +# Note that this unstandardized posterior is not a proper probability +# distribution since it does not add to 1 +sum(unstd.posterior) + +#============================================================================== + + +# 4) Standardize the posterior + +posterior <- unstd.posterior/sum(unstd.posterior) + +# This standardized posterior is a now proper probability distribution +sum(posterior) + +# Visualize the posterior +plot(p_grid, posterior, pch = 19, type = "b", + xlab = "proportion of water on globe", + ylab = "posterior probability") diff --git a/lecture/lecture_week_04.pptx b/lecture/lecture_week_04.pptx new file mode 100644 index 0000000..7d64fa3 Binary files /dev/null and b/lecture/lecture_week_04.pptx differ