Assignment 4

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 4. 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/assignment4.yml", sep="")
set_assignment(assignment_path)
Assignment set:
assignment4: Bayesian Data Analysis: Assignment 4
The assignment contain the following (4) tasks:
- log_importance_weights
- normalized_importance_weights
- S_eff
- posterior_mean

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

2 Bioassay model

2.1 (a)

2.2 (b)

Loading the library and the data.

# Useful functions: quantile()
# and mcse_quantile() (from aaltobda)

data("bioassay_posterior")
# The 4000 draws are now stored in the variable `bioassay_posterior`.
# The below displays the first rows of the data:
head(bioassay_posterior)
        alpha      beta
1 -0.02050577 10.032841
2  1.21738518  4.504546
3  3.04829407 16.239424
4  1.32272770  4.924268
5  1.36274817 12.880561
6  1.08593225  5.943731

3 Importance sampling

3.1 (c)

# Useful functions: bioassaylp (from aaltobda)
alpha_test = c(1.896, -3.6,  0.374, 0.964, -3.123, -1.581)
beta_test = c(24.76, 20.04, 6.15, 18.65, 8.16, 17.4)


log_importance_weights <- function(alpha, beta) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above.
    c(-8.95, -23.47, -6.02, -8.13, -16.61, -14.57)
}

3.2 (d)

normalized_importance_weights <- function(alpha, beta) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above.
    c(0.045, 0.000, 0.852, 0.103, 0.000, 0.000)
}

3.3 (e)

Write your answers and code here!

3.4 (f)

S_eff <- function(alpha, beta) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above.
    1.354
}

3.5 (g)

3.6 (h)

posterior_mean <- function(alpha, beta) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above.
    c(0.503, 8.275)
}
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 | log_importance_weights()                                        
✖ | 2       3 | log_importance_weights()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-1-subtask-1-tests.R:23:3'): log_importance_weights()
log_importance_weights(alpha, beta) not equivalent to c(...).
Lengths differ: 6 is not 10
Error: Incorrect result for alpha = c(4.5, 1.8, -2.4, 0.7, 0.3, -3.1, 0.4, 1.7, -0.5, -0.1) and beta = c(25.2, 7, -5.6, 1.1, 14.7, -6.5, 14.2, 25.9, 13.4, 19.8).

Failure ('test-task-1-subtask-1-tests.R:31:3'): log_importance_weights()
log_importance_weights(alpha, beta) not equivalent to c(...).
Lengths differ: 6 is not 10
Error: Incorrect result for alpha = c(1.4, 1.5, -2.3, -2.3, -0.5, 1.9, 1.6, -1.8, -2.7, -1.9) and beta = c(11.4, 8.2, 10.8, 7.4, 18.2, 27.8, 5.1, 1.9, -15.7, 25.1).
────────────────────────────────────────────────────────────────────────────────

⠏ |         0 | task-2-subtask-1-tests                                          
⠏ |         0 | normalized_importance_weights()                                 
✖ | 2       3 | normalized_importance_weights()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-2-subtask-1-tests.R:23:3'): normalized_importance_weights()
normalized_importance_weights(alpha, beta) not equivalent to c(0.03, 0.44, 0, 0.01, 0.18, 0, 0.25, 0.04, 0.04, 0.01).
Lengths differ: 6 is not 10
Error: Incorrect result for alpha = c(4.5, 1.8, -2.4, 0.7, 0.3, -3.1, 0.4, 1.7, -0.5, -0.1) and beta = c(25.2, 7, -5.6, 1.1, 14.7, -6.5, 14.2, 25.9, 13.4, 19.8).

Failure ('test-task-2-subtask-1-tests.R:31:3'): normalized_importance_weights()
normalized_importance_weights(alpha, beta) not equivalent to c(0.47, 0.42, 0, 0, 0, 0.01, 0.09, 0, 0, 0).
Lengths differ: 6 is not 10
Error: Incorrect result for alpha = c(1.4, 1.5, -2.3, -2.3, -0.5, 1.9, 1.6, -1.8, -2.7, -1.9) and beta = c(11.4, 8.2, 10.8, 7.4, 18.2, 27.8, 5.1, 1.9, -15.7, 25.1).
────────────────────────────────────────────────────────────────────────────────

⠏ |         0 | task-3-subtask-1-tests                                          
⠏ |         0 | S_eff()                                                         
✖ | 2       3 | S_eff()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-3-subtask-1-tests.R:23:3'): S_eff()
S_eff(alpha, beta) not equivalent to 3.404.
1/1 mismatches
[1] 1.35 - 3.4 == -2.05
Error: Incorrect result for alpha = c(4.5, 1.8, -2.4, 0.7, 0.3, -3.1, 0.4, 1.7, -0.5, -0.1) and beta = c(25.2, 7, -5.6, 1.1, 14.7, -6.5, 14.2, 25.9, 13.4, 19.8).

Failure ('test-task-3-subtask-1-tests.R:31:3'): S_eff()
S_eff(alpha, beta) not equivalent to 2.448.
1/1 mismatches
[1] 1.35 - 2.45 == -1.09
Error: Incorrect result for alpha = c(1.4, 1.5, -2.3, -2.3, -0.5, 1.9, 1.6, -1.8, -2.7, -1.9) and beta = c(11.4, 8.2, 10.8, 7.4, 18.2, 27.8, 5.1, 1.9, -15.7, 25.1).
────────────────────────────────────────────────────────────────────────────────

⠏ |         0 | task-4-subtask-1-tests                                          
⠏ |         0 | posterior_mean()                                                
✖ | 2       3 | posterior_mean()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-4-subtask-1-tests.R:23:3'): posterior_mean()
posterior_mean(alpha, beta) not equivalent to c(1.111039, 11.854793).
2/2 mismatches (average diff: 2.09)
[1] 0.503 -  1.11 == -0.608
[2] 8.275 - 11.85 == -3.580
Error: Incorrect result for alpha = c(4.5, 1.8, -2.4, 0.7, 0.3, -3.1, 0.4, 1.7, -0.5, -0.1) and beta = c(25.2, 7, -5.6, 1.1, 14.7, -6.5, 14.2, 25.9, 13.4, 19.8).

Failure ('test-task-4-subtask-1-tests.R:30:3'): posterior_mean()
posterior_mean(alpha, beta) not equivalent to c(1.458649, 9.67896).
2/2 mismatches (average diff: 1.18)
[1] 0.503 - 1.46 == -0.956
[2] 8.275 - 9.68 == -1.404
Error: Incorrect result for alpha = c(1.4, 1.5, -2.3, -2.3, -0.5, 1.9, 1.6, -1.8, -2.7, -1.9) and beta = c(11.4, 8.2, 10.8, 7.4, 18.2, 27.8, 5.1, 1.9, -15.7, 25.1).
────────────────────────────────────────────────────────────────────────────────

══ Results ═════════════════════════════════════════════════════════════════════
Duration: 0.3 s

[ FAIL 8 | WARN 0 | SKIP 0 | PASS 12 ]