Assignment 2

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 2. 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:

library(markmyassignment)
assignment_path = paste("https://github.com/avehtari/BDA_course_Aalto/",
"blob/master/assignments/tests/assignment2.yml", sep="")
set_assignment(assignment_path)
Assignment set:
assignment2: Bayesian Data Analysis: Assignment 2
The assignment contain the following (3) tasks:
- beta_point_est
- beta_interval
- beta_low

The following installs the aaltobda package:

# Caching should be fine here
install.packages("aaltobda", repos = c("https://avehtari.github.io/BDA_course_Aalto/", getOption("repos")))
Installing package into '/usr/local/lib/R/site-library'
(as 'lib' is unspecified)

2 Inference for binomial proportion

Loading the library and the data.

library(aaltobda)
data("algae")
# The data are now stored in the variable `algae`.
# These are the values for the prior required in the assignment
prior_alpha = 2
prior_beta = 10

The below data is only for the tests, you need to change to the full data algae when reporting your results.

algae_test <- c(0, 1, 1, 0, 0, 0)

2.1 (a)

Write the likelihood, the prior and the posterior here!

# These are not the actual values for the posterior!
# You will have to compute those from the data!
posterior_alpha = 2
posterior_beta = 10

You can do string interpolation using R inline code execution in quarto as such:

\(\alpha_\text{prior}\) is 2 and \(\beta_\text{prior}\) is 10. Or string interpolation within math: \(\BetaDist(2,10)\)

This template defines a \BetaDist TeX command which renders $\BetaDist(1,2)$ as \(\BetaDist(1,2)\).

2.2 (b)

Keep the below name and format for the functions to work with markmyassignment:

# Useful function: qbeta()

beta_point_est <- function(prior_alpha, prior_beta, data) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above,
    # combined with the prior provided above.
    0.2222222
}
beta_interval <- function(prior_alpha, prior_beta, data, prob=0.9) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above,
    # combined with the prior provided above.
    c(0.0846451, 0.3956414)
}

2.3 (c)

Keep the below name and format for the function to work with markmyassignment:

# Useful function: pbeta()

beta_low <- function(prior_alpha, prior_beta, data, pi_0=0.2) {
    # Do computation here, and return as below.
    # This is the correct return value for the test data provided above,
    # combined with the correct prior.
    0.4511238
}

2.4 (d)

Write your answer here!

2.5 (e)

Plot the PDFs here. Explain shortly what you do.

# Useful function: dbeta()
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 | beta_point_est()                                                
⠼ | 3       2 | beta_point_est()                                                
✖ | 3       2 | beta_point_est() [0.1s]
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-1-subtask-1-tests.R:16:3'): beta_point_est()
beta_point_est(prior_alpha = alpha, prior_beta = beta, data = test) not equivalent to c(0.5).
1/1 mismatches
[1] 0.222 - 0.5 == -0.278
Error: Incorrect result for prior_alpha = 2, prior_beta = 4 and data = c(0, 1, 1, 0, 1, 1)

Failure ('test-task-1-subtask-1-tests.R:23:3'): beta_point_est()
beta_point_est(prior_alpha = alpha, prior_beta = beta, data = test) not equivalent to c(0.5).
1/1 mismatches
[1] 0.222 - 0.5 == -0.278
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1)

Failure ('test-task-1-subtask-1-tests.R:30:3'): beta_point_est()
beta_point_est(prior_alpha = alpha, prior_beta = beta, data = test) not equivalent to c(0.5555556).
1/1 mismatches
[1] 0.222 - 0.556 == -0.333
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1, 1, 1)
────────────────────────────────────────────────────────────────────────────────

⠏ |         0 | task-2-subtask-1-tests                                          
⠏ |         0 | beta_interval()                                                 
✖ | 3       2 | beta_interval()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-2-subtask-1-tests.R:16:3'): beta_interval()
beta_interval(...) not equivalent to c(0.2712499, 0.7287501).
2/2 mismatches (average diff: 0.26)
[1] 0.0846 - 0.271 == -0.187
[2] 0.3956 - 0.729 == -0.333
Error: Incorrect result for prior_alpha = 2, prior_beta = 4 and data = c(0, 1, 1, 0, 1, 1) and 90% interval.

Failure ('test-task-2-subtask-1-tests.R:23:3'): beta_interval()
beta_interval(...) not equivalent to c(0.2999865, 0.7000135).
2/2 mismatches (average diff: 0.26)
[1] 0.0846 - 0.3 == -0.215
[2] 0.3956 - 0.7 == -0.304
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1) and 90% interval.

Failure ('test-task-2-subtask-1-tests.R:30:3'): beta_interval()
beta_interval(...) not equivalent to c(0.3640088, 0.7398856).
2/2 mismatches (average diff: 0.312)
[1] 0.0846 - 0.364 == -0.279
[2] 0.3956 - 0.740 == -0.344
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1, 1, 1) and 90% interval.
────────────────────────────────────────────────────────────────────────────────

⠏ |         0 | task-3-subtask-1-tests                                          
⠏ |         0 | beta_low()                                                      
✖ | 3       2 | beta_low()
────────────────────────────────────────────────────────────────────────────────
Failure ('test-task-3-subtask-1-tests.R:16:3'): beta_low()
beta_low(...) not equivalent to c(0.5).
1/1 mismatches
[1] 0.451 - 0.5 == -0.0489
Error: Incorrect result for prior_alpha = 2, prior_beta = 4 and data = c(0, 1, 1, 0, 1, 1)

Failure ('test-task-3-subtask-1-tests.R:23:3'): beta_low()
beta_low(...) not equivalent to c(0.5).
1/1 mismatches
[1] 0.451 - 0.5 == -0.0489
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1)

Failure ('test-task-3-subtask-1-tests.R:30:3'): beta_low()
beta_low(...) not equivalent to 0.0004932497.
1/1 mismatches
[1] 0.451 - 0.000493 == 0.451
Error: Incorrect result for prior_alpha = 3, prior_beta = 5 and data = c(0, 1, 1, 0, 1, 1, 0, 1, 1, 1)
────────────────────────────────────────────────────────────────────────────────

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

[ FAIL 9 | WARN 0 | SKIP 0 | PASS 6 ]