---
title: "Assignment 2"
author: anonymous # <-- hand in anonymously
format:
  html:
    toc: true
    code-tools: true
    code-line-numbers: true
    number-sections: true
    mainfont: Georgia, serif
  pdf:
    geometry:
    - left=1cm,top=1cm,bottom=1cm,right=7cm
    number-sections: true
    code-annotations: none
    include-in-header:
      text: |
        % You can add TeX macros here for PDF,
        % see https://quarto.org/docs/output-formats/pdf-basics.html#latex-includes
        \newcommand{\BetaDist}{\mathrm{Beta}}
editor: source
---


::: {.hidden}
$$
% You can add TeX macros here for HTML, see https://quarto.org/docs/authoring/markdown-basics.html#equations
\renewcommand{\BetaDist}{\mathrm{Beta}}
$$
:::

# General information

:::: {.content-hidden when-format="pdf"}
::: {.callout-warning collapse=false}

## 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](assignment2.html). You can download the [qmd-file](https://avehtari.github.io/BDA_course_Aalto/assignments/template2.qmd) 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:

```{r}
library(markmyassignment)
assignment_path = paste("https://github.com/avehtari/BDA_course_Aalto/",
"blob/master/assignments/tests/assignment2.yml", sep="")
set_assignment(assignment_path)
```

The following installs the `aaltobda` package:
```{r}
#| cache: true
# Caching should be fine here
install.packages("aaltobda", repos = c("https://avehtari.github.io/BDA_course_Aalto/", getOption("repos")))
```

:::
::::


# Inference for binomial proportion

Loading the library and the data.
```{r}
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.
```{r}
algae_test <- c(0, 1, 1, 0, 0, 0)
```



## (a)


Write the likelihood, the prior and the posterior here!
```{r}
# 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](https://en.wikipedia.org/wiki/String_interpolation) using [R inline code execution in quarto](https://quarto.org/docs/computations/execution-options.html#knitr) as such:

$\alpha_\text{prior}$ is **`r prior_alpha`** and $\beta_\text{prior}$ is **`r prior_beta`**.
Or string interpolation within math: $\BetaDist(`r prior_alpha`,`r prior_beta`)$

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



## (b)


Keep the below name and format for the functions to work with `markmyassignment`:
```{r}
# 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)
}
```



## (c)


Keep the below name and format for the function to work with `markmyassignment`:
```{r}
# 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
}
```



## (d)


Write your answer here!



## (e)


Plot the PDFs here. Explain shortly what you do.
```{r}
# Useful function: dbeta()
```



:::: {.content-hidden when-format="pdf"}
::: {.callout-warning collapse=false}

## 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:

```{r}
mark_my_assignment()
```

:::
::::
