Homework 8

Due: 2020-10-13, 11:59pm

For this homework you should submit a ZIP archive called firstnameLastnameHW7.zip. When unzipped there should be a single directory/folder called firstnameLastnameHW7 and all files should be within that directory. For this homework that directory should contain:

  • A single document with the answers to all the following items in HTML or IPYNB format. Make sure you include plain English blocks in between the code and its output, to interpret what R is giving you. There should also be comments in the R code blocks prefixed with #.
  • Code file used to generate the HTML file in RMD format (not needed if using IPYNB).

In this homework we will practice writing functions, loops, and some simulations.

1. Absolute deviation (50%)

Read in the Agren data.

  • Create a function similar to the rss function in the class notes that calculates the sum of the absolute values of the deviations of a vector x from a number a. Divide by the number of values to get a mean absolute deviation.
  • Create a vector of at least 100 values that span the range of the fitness values in Italy in 2011.
  • Use a loop to go through each value in the vector spanning the range, and calculate the mean absolute deviation from each of the 100 numbers.
  • What number minimizes the mean absolute deviation? Compare with the mean and the median.

2. Herd immunity and vaccination (50%)

Consider the SIR model discussed in the lecture.

  • Write a function to calculate the expected proportion of susceptible, infected and recovered individuals after a year (365). You may use the evolve function from the lecture. The function should take the proportion of susceptible, infected, and recovered individuals as arguments, as well as the infection and recovery rates.
  • Fix the starting values of $S=999/1000$ and $I=1/1000$. Let gamma=1/14. Vary beta from 0.01 to 0.99, and calculate the proportion of susceptible individuals at the end of a year in each of those scenarios. Plot the proportion of susceptible individuals against beta. (Hint: You may want to use an “apply” function.) This figure will tell you at what proportion of individuals will be uninfected and susceptible when infections die down (when “herd immunity” is acheived).
  • Fix beta=1/2, gamma=1/14 and I=1/1000. Now vary S from $999/1000$ to $1/1000$. Note that $S+I+R=1$. This is what happens if there is some amount of immunity in the population (eg. from vaccination). As in the previous question, let the population evolve for a year, and plot the number of susceptible individuals at the end of one year as a function of the proportion of susceptible individuals in the beginning. This will give us an idea of the impact of vaccination (taking individuals out of the susceptible pool) on the total number of susceptible individuals in the end.

3. Gambling with slot machines (optional)

Imagine you are in a casino playing a slot machine that takes one dollar each time. You either lose the dollar (with probability $p$), or win two dollars (with probability $1{-}p$). Since you are in a casino, $p\geq 0.5$.

  • Write a function that simulates the outcome of one play at the slot machine, i.e. either returns 0 or 2 with probability p and 1-p respectively.
  • Suppose you start with $10, and decide to play until you either win $20, or lose all your money. Assume p=0.5. Write a while loop to simulate this process. In the end you will have either $20 or $0. How many times did you play?
  • Make a function out of the previous item that simulates a visit to the slot machine with $10. The function should return how much you have in the end ($20 or $0). It’s argument should be p which is a measure of how rigged the casino is.
  • Run the previous function 100 times (assume p=0.5), and find out how often you will come a winner (with $20) and how many times you will come as a loser (with $0).
  • Repeat the previous item with p=0.6.

4. Animation (optional)

Build on the drawing functions in the class notes and previous homeworks to make a simple animation.

  • Write a function to draw a car using rectangles and circles.
  • Write a loop (or equivalent) to draw a sequence of drawings moving a car in the horizontal (X) direction.
  • Make an animation where the car moves from one side of the frame to the other, with other objects (trees, house, person) in the background.

5. Acknowledgements

Cite resources or individuals helping you.