bump <- function(x, loc=0, scale=1){
xi = (x - loc) / scale
ifelse(abs(xi) < 1, exp(-1/(1-xi^2)), 0.)
}
daily_probability_of_escape <- function(day, weight){
# Expects a day and a weight and computes the daily probability of escape
bump(day, 30, 10) * (1e-2 + bump(weight, 200, 150)+bump(weight, 700, 150))
}
chickenwise_probability_of_escape <- function(weights){
# Expects a vector of daily weights from day 1 to N and computes the probability of
# escape at the end of the time series
prob_of_failure = 1
for(day in 1:length(weights)){
prob_of_failure = prob_of_failure * (1-daily_probability_of_escape(day, weights[day]))
}
return(1 - prob_of_failure)
}
Assignment 9
Decision analysis
1 General information
The maximum amount of points from this assignment is 3.
We have prepared a quarto template specific to this assignment (html, qmd, pdf) to help you get started.
We recommend you use jupyter.cs.aalto.fi or the docker container.
Reading instructions:
- The reading instructions for BDA3 Chapter 9 (decision analysis).
Grading instructions:
The grading will be done in peergrade. All grading questions and evaluations for this assignment are contained within this document in the collapsible Rubric blocks.
Installing and using CmdStanR
:
See the Stan demos on how to use Stan in R (or Python). Aalto JupyterHub has working R and CmdStanR/RStan environment and is probably the easiest way to use Stan. * To use CmdStanR in Aalto JupyterHub:
library(cmdstanr)
set_cmdstan_path('/coursedata/cmdstan')
The Aalto Ubuntu desktops also have the necessary libraries installed.]{.aalto}
To install Stan on your laptop, run ‘install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))’ in R. If you encounter problems, see additional answers in FAQ. If you don’t succeed in short amount of time, it is probably easier to use Aalto JupyterHub.
If you use Aalto JupyterHub
, all necessary packages have been pre-installed. In your laptop, install package cmdstanr
. Installation instructions on Linux, Mac and Windows can be found at https://mc-stan.org/cmdstanr/. Additional useful packages are loo
, bayesplot
and posterior
(but you don’t need these in this assignment). For Python users, PyStan
, CmdStanPy
, and ArviZ
packages are useful.
Stan manual can be found at https://mc-stan.org/users/documentation/. From this website, you can also find a lot of other useful material about Stan.
If you edit files ending .stan
in RStudio, you can click “Check” in the editor toolbar to make syntax check. This can significantly speed-up writing a working Stan model.
For posterior statistics of interest, only report digits that are not completely random based on the Monte Carlo standard error (MCSE).
Example: If you estimate \(E(\mu) \approx 1.234\) with MCSE(\(E(\mu)\)) = 0.01, then the true expectation is likely to be between \(1.204\) and \(1.264\), it makes sense to report \(E(\mu) \approx 1.2\).
See Lecture video 4.1, the chapter notes, and a case study for more information.
- The recommended tool in this course is R (with the IDE RStudio).
- Instead of installing R and RStudio on you own computer, see how to use R and RStudio remotely.
- If you want to install R and RStudio locally, download R and RStudio.
- There are tons of tutorials, videos and introductions to R and RStudio online. You can find some initial hints from RStudio Education pages.
- When working with R, we recommend writing the report using
quarto
and the provided template. The template includes the formatting instructions and how to include code and figures. - Instead of
quarto
, you can use other software to make the PDF report, but the the same instructions for formatting should be used. - Report all results in a single, anonymous *.pdf -file and submit it in peergrade.io.
- The course has its own R package
aaltobda
with data and functionality to simplify coding. The package is pre-installed in JupyterHub. To install the package on your own system, run the following code (upgrade="never" skips question about updating other packages):
install.packages("aaltobda", repos = c("https://avehtari.github.io/BDA_course_Aalto/", getOption("repos")))
- Many of the exercises can be checked automatically using the R package
markmyassignment
(pre-installed in JupyterHub). Information on how to install and use the package can be found in themarkmyassignment
documentation. There is no need to includemarkmyassignment
results in the report. - Recommended additional self study exercises for each chapter in BDA3 are listed in the course web page. These will help to gain deeper understanding of the topic.
- Common questions and answers regarding installation and technical problems can be found in Frequently Asked Questions (FAQ).
- Deadlines for all assignments can be found on the course web page and in Peergrade. You can set email alerts for the deadlines in Peergrade settings.
- You are allowed to discuss assignments with your friends, but it is not allowed to copy solutions directly from other students or from internet.
- You can copy, e.g., plotting code from the course demos, but really try to solve the actual assignment problems with your own code and explanations.
- Do not share your answers publicly.
- Do not copy answers from the internet or from previous years. We compare the answers to the answers from previous years and to the answers from other students this year.
- Use of AI is allowed on the course, but the most of the work needs to by the student, and you need to report whether you used AI and in which way you used them (See points 5 and 6 in Aalto guidelines for use of AI in teaching).
- All suspected plagiarism will be reported and investigated. See more about the Aalto University Code of Academic Integrity and Handling Violations Thereof.
- Do not submit empty PDFs, almost empty PDFs, copy of the questions, nonsense generated by yourself or AI, as these are just harming the other students as they can’t do peergrading for the empty or nonsense submissions. Violations of this rule will be reported and investigated in the same way was plagiarism.
- If you have any suggestions or improvements to the course material, please post in the course chat feedback channel, create an issue, or submit a pull request to the public repository!
This exercise is an example of a decision analysis (DA). In a broad context, this means optimizing over different decisions that lead to different outcomes that all have different utilities. In a Bayesian context, this means using posterior distributions to make decisions.
2 Escaping from the chicken coop
You are an adult chicken living in an organic chicken commune, where life is great, if a bit boring. You have settled in comfortably, but you want something more for your offspring. Your traveling corvid friends tell you of places where chickens eat corn all day, get ferried around in mobile chicken coops to see the world or get mental stimulation by being trained humanely to perform tricks. The chicken elders have gained access to the computers of your human caretakers and have found the results of a complicated statistical analysis of the growth curves of your relatives. Because you are a chicken, you don’t care about convergence diagnostics or priors.
Your task is to maximizes the chance of escape for your offspring.
To sample a “new chicken” from the posterior, use posterior_predict with options newdata=..., allow_new_levels=TRUE, sample_new_levels="gaussian"
, where you pass a dataframe as newdata
which has a “new” chicken ID Chick
and appropriate values for Time
and Diet
.
Your chicken elders have been meticulously collecting data on what kind of characteristics have allowed previous chickens to escape. They have found out that both the age and the weight influence the (daily) probability of escape for a chicken:
- If the chicken is too young, it is not yet mature enough to venture out into the world.
- If the chicken is too old, it will not try to escape anymore.
- If the chicken is small and has just the right size, it can try to squeze through a tiny crack in the fence.
- If the chicken is big enough, it is strong enough to try to fly over the fence.
- No matter the size, there is always a small residual probability that the chicken can escape.
Every day, chickens will try to escape if they are of the right age. Their daily escape probability \(e(\text{day}, \text{weight})\) is implemented in the daily_probability_of_escape(day, weight)
function. The probability that a chicken with daily weights \(w = (w_1,\dots,w_N)\) has not escaped after \(i+1\) days can be computed as follows: \[
f_{i+1} = f_i \, (1 - e(i, w_i))
\] The chickenwise_probability_of_escape(weights)
computes the probability that a chicken has escaped after length(weights)
days.
days = 1:40
weights = 1:900
heatmap_matrix = outer(days,weights,daily_probability_of_escape)
image(days, weights, heatmap_matrix, xlab="day", ylab="weight", main="Daily probability of escape")
You can reuse the predictions you created in the previous subtask. Work with the draws to compute the chickenwise probability of escape first, and then take the expectation!