```
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.

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.**

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.