Skip to content

Commit

Permalink
Adding week 04 lecture and lab
Browse files Browse the repository at this point in the history
  • Loading branch information
eveskew committed Feb 16, 2020
1 parent 8deece1 commit 2664d09
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lab/lab_week_04.Rmd
Original file line number Diff line number Diff line change
@@ -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}
```
70 changes: 70 additions & 0 deletions lecture/lecture_week_04.R
Original file line number Diff line number Diff line change
@@ -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")
Binary file added lecture/lecture_week_04.pptx
Binary file not shown.

0 comments on commit 2664d09

Please sign in to comment.