Assignment 5

Author

anonymous

1 General information

Setup

This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission. Make sure that this does not get displayed in the PDF!

This is the template for assignment 5. You can download the qmd-file or copy the code from this rendered document after clicking on </> Code in the top right corner.

Please replace the instructions in this template by your own text, explaining what you are doing in each exercise.

The following will set-up markmyassignment to check your functions at the end of the notebook:

if(!require(markmyassignment)){
    install.packages("markmyassignment")
    library(markmyassignment)
}
Loading required package: markmyassignment
assignment_path = paste("https://github.com/avehtari/BDA_course_Aalto/",
"blob/master/assignments/tests/assignment5.yml", sep="")
set_assignment(assignment_path)
Assignment set:
assignment5: Bayesian Data Analysis: Assignment 5
The assignment contain the following task:
- density_ratio

The following installs and loads the aaltobda package:

if(!require(aaltobda)){
    install.packages("aaltobda", repos = c("https://avehtari.github.io/BDA_course_Aalto/", getOption("repos")))
    library(aaltobda)
}
Loading required package: aaltobda

The following installs and loads the latex2exp package, which allows us to use LaTeX in plots:

if(!require(latex2exp)){
    install.packages("latex2exp")
    library(latex2exp)
}
Loading required package: latex2exp

The following installs and loads the posterior package which imports the rhat_basic() function:

if(!require(posterior)){
    install.packages("posterior")
    library(posterior)
}
Loading required package: posterior
This is posterior version 1.4.0

Attaching package: 'posterior'
The following object is masked from 'package:aaltobda':

    mcse_quantile
The following objects are masked from 'package:stats':

    mad, sd, var
The following objects are masked from 'package:base':

    %in%, match

The following installs and loads the ggplot2 package and the bayesplot package

if(!require(ggplot2)){
    install.packages("ggplot2")
    library(ggplot2)
}
Loading required package: ggplot2
if(!require(bayesplot)){
    install.packages("bayesplot")
    library(bayesplot)
}
Loading required package: bayesplot
This is bayesplot version 1.10.0
- Online documentation and vignettes at mc-stan.org/bayesplot
- bayesplot theme set to bayesplot::theme_default()
   * Does _not_ affect other ggplot2 plots
   * See ?bayesplot_theme_set for details on theme setting

Attaching package: 'bayesplot'
The following object is masked from 'package:posterior':

    rhat

2 Generalized linear model: Bioassay model with Metropolis algorithm

2.1 (a)

Write your answers/code here!

# Useful functions: runif, rnorm
# bioassaylp, dmvnorm (from aaltobda)

data("bioassay")
# Start by implementing a function called `density_ratio` to
# compute the density ratio function, $r$ in Eq. (11.1) in BDA3:
density_ratio <- function(alpha_propose, alpha_previous, beta_propose, beta_previous, x, y, n){
    # Do computation here, and return as below.
    # Below are the correct return values for two different calls of this function:

    # alpha_propose = 1.89, alpha_previous = 0.374,
    # beta_propose = 24.76, beta_previous = 20.04,
    # x = bioassay$x, y = bioassay$y, n = bioassay$n
    1.305179

    # alpha_propose = 0.374, alpha_previous = 1.89,
    # beta_propose = 20.04, beta_previous = 24.76,
    # x = bioassay$x, y = bioassay$y, n = bioassay$n
    0.7661784

}
# Then implement a function called `metropolis_bioassay()` which
# implements the Metropolis algorithm using the `density_ratio()`:
metropolis_bioassay <- function(alpha_initial, beta_initial, alpha_sigma, beta_sigma, no_draws, x, y, n){
    # Do computation here, and return as below.
    # Below are "wrong" values (unlikely to actually occur)
    # in the "correct" format (such that they work with the plotting functions further down).
    data.frame(
        alpha=c(alpha_initial, alpha_initial+alpha_sigma, alpha_initial-alpha_sigma),
        beta=c(beta_initial, beta_initial+beta_sigma, beta_initial-beta_sigma)
    )

}
df = metropolis_bioassay(0, 0, 1, 1, 1000, bioassay$x, bioassay$y, bioassay$n)

2.2 (b)

Write your answers/code here!

Have a look at bayesplot trace plot examples and tune your plot if wanted/needed. Don’t forget to include a title/caption/description.

The below example plot only includes a single chain, but your report should include a plot with multiple chains overlayed!

# Useful functions: mcmc_trace (from bayesplot)
mcmc_trace(df, pars=c("alpha", "beta"))

2.3 (c)

Write your answers/code here!

# Useful functions: rhat_basic (from posterior)

2.4 (c)

Write your answers/code here!

Have a look at bayesplot scatter plot examples and tune your plot if wanted/needed. Don’t forget to include a title/caption/description.

# Useful functions: mcmc_scatter (from bayesplot)
mcmc_scatter(df, pars=c("alpha", "beta"))

markmyassignment

This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission. Make sure that this does not get displayed in the PDF!

The following will check the functions for which markmyassignment has been set up:

mark_my_assignment()
✔ | F W S  OK | Context

⠏ |         0 | task-1-subtask-1-tests                                          
⠏ |         0 | density_ratio()                                                 
✖ | 3       3 | density_ratio()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-1-subtask-1-tests.R:14:3'): density_ratio()
density_ratio(...) not equivalent to 1.305179.
1/1 mismatches
[1] 0.766 - 1.31 == -0.539
Error: Incorrect result for alpha_propose = 1.89, alpha_previous = 0.374, beta_propose = 24.76 and beta_previous = 20.04.

Failure ('test-task-1-subtask-1-tests.R:26:3'): density_ratio()
density_ratio(...) not equivalent to 1.
1/1 mismatches
[1] 0.766 - 1 == -0.234
Error: Incorrect result for alpha_propose = 1, alpha_previous = 1, beta_propose = 20 and beta_previous = 20.

Failure ('test-task-1-subtask-1-tests.R:32:3'): density_ratio()
density_ratio(...) not equivalent to 0.09510128.
1/1 mismatches
[1] 0.766 - 0.0951 == 0.671
Error: Incorrect result for alpha_propose = 0, alpha_previous = 1, beta_propose = 20 and beta_previous = 20.
────────────────────────────────────────────────────────────────────────────────

══ Results ═════════════════════════════════════════════════════════════════════
Duration: 0.1 s

[ FAIL 3 | WARN 0 | SKIP 0 | PASS 3 ]