Skip to content

Commit

Permalink
Adding week 05 lecture and lab
Browse files Browse the repository at this point in the history
  • Loading branch information
eveskew committed Feb 24, 2020
1 parent 5a5bb89 commit 15528a3
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 20 deletions.
71 changes: 71 additions & 0 deletions lab/lab_week_05.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "EEEB UN3005/GR5005 \nLab - Week 05 - 24 and 26 February 2020"
author: "USE YOUR NAME HERE"
output: pdf_document
fontsize: 12pt
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rethinking)
```


# Statistical Distributions and Summary Statistics


## Exercise 1: Grid Approximation, Our Old Friend

Imagine that the globe tossing example from the *Statistical Rethinking* text and class resulted in 8 water observations out of 15 globe tosses.

With this set of data, use grid approximation (with 101 grid points) to construct the posterior for *p* (the probability of water). Assume a flat prior.

Plot the posterior distribution.

```{r}
```


## Exercise 2: Sampling From a Grid-Approximate Posterior

Now generate 10,000 samples from the posterior distribution of *p*. Call these samples `post.samples`. Visualize `post.samples` using the `dens()` function.

For your own understanding, re-run your sampling and plotting code multiple times to observe the effects of sampling variation.

```{r}
```


## Exercise 3: Summarizing Samples

Return the mean, median, and mode (using `chainmode()`) of `post.samples`. Then calculate the 80%, 90%, and 99% highest posterior density intervals of `post.samples`.

```{r}
```


## Exercise 4: Model Predictions

Using `post.samples`, generate 10,000 simulated model predictions (you can call these `preds`) for a binomial trial of size 15. Visualize the model predictions using the `simplehist()` function.

Based on these posterior predictions, what is the probability of observing 8 waters in 15 globe tosses?

```{r}
```


## Exercise 5: More Model Predictions

Using the *same* posterior samples (i.e., `post.samples`), generate 10,000 posterior predictions for a binomial trial of size 9.

Using these new predictions, calculate the posterior probability of observing 8 waters in 9 tosses.

```{r}
```
47 changes: 27 additions & 20 deletions lecture/lecture_week_04.R
Original file line number Diff line number Diff line change
@@ -1,67 +1,74 @@


# Demonstrate grid approximation of the posterior for a globe tossing problem
# with 6 water observations out of 9 globe tosses
# 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
# 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
# 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
# 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
# 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
# 4) Compute the unstandardized posterior at each
# parameter value on the grid

# The unstandardized posterior is simply the product of the likelihood and
# prior
# 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
# 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
# 5) Standardize the posterior

posterior <- unstd.posterior/sum(unstd.posterior)

# This standardized posterior is a now proper probability distribution
# This standardized posterior is a now proper probability
# distribution
sum(posterior)

# Visualize the posterior
Expand Down
Loading

0 comments on commit 15528a3

Please sign in to comment.