In this weeks lab you will use loops and conditional statements to simulate the outcomes of different retirement saving strategies.

Background

Historically, retirement plans often promised “defined benefits”, i.e. they would guarantee you a certain amount of income after you retired. However, these types of pension plans have become less popular because they are expensive (and sometimes unsustainable) as a greater fraction of the world’s population becomes older.

In the USA today, many individuals are responsible for saving for their own retirements. These workers have to decide how much of their paycheck to divert into a retirement account instead of their checking account. For example, if you earned $2000 dollars in your paycheck, you might decide to send $200 (i.e. 10% of your earnings) to your retirement account. The remaining $1800 (minus taxes) would then be deposited in your bank account.

(Note: in this lab, we are going to make many simplifications about both the US retirement system and financial markets - the actual system is a bit more complicated and so you should do your own research before making any investments.)

Exercises

  1. We are going to create a simple simulation that will track how much we can save for retirement between graduating college and reaching retirement age.

    To do this:

    • Create a variable that represents our savings. Initially this variable should contain the number 0.

    • Create a variable to represent our income. This should hold the number 58000 (i.e. $58,000, which is the median income of a college graduate in the USA).

    • Write a for loop, that starts at age 22 (when you leave college) and runs until age 65 (the full retirement age). Each iteration of the for-loop should represent one year. Within the for-loop, calculate how much money you have saved by the end of this year and then assign this number to the savings variable. You can calculate this new value as:

      \(\text{savings} = \text{previous savings} + \text{additional money saved this year} + \text{interest on previous savings}\)

      Assume that you will save 10% of your salary each year, and that you earn 6% interest on existing retirement savings.

    Here is a code template that you might find helpful to implement the previous three bullet points:

    savings <- ...
    income <- ...
    interest_rate <- ...
    savings_rate <- ...
    
    for(year in ...){
      savings <- savings + ...
    }

    Print out the final saved amount below the code chunk using code (i.e. print out the last value in the savings vector). (If your code is correct, the final number stored in the savings variable should be about $1,158,597!)

    Explain why this number is so high even though we only actually paid $2.552^{5} into our savings account ($5800 per year for 44 years).

    Commit your work after this exercise.

  2. There are different strategies for how to spend retirement savings after you retire. These strategies aim to provide a balance between being able to withdraw enough money to be able to live comfortably in retirement, while also not spending so much that you run out of retirement savings!

    One strategy is to live on the interest generated by your retirement savings (i.e. leaving the underlying savings untouched). A common rule-of-thumb is to assume that you will be able to spend 4% of interest each year without depleting the underlying savings.

    Using code, calculate how much you would have to live on each year in retirement if you can safely withdraw 4% of the final value of your savings account. (Remember that it is a good idea to refer to a calculated value using the variable where it is stored, instead of rewriting the value out.)

    Is this annual retirement income greater or less than our estimated annual salary before retirement?

    Commit your work after this exercise.

  3. Many employers will incentivize you to save for retirement. They do this by offering an “employer match” - if you save some money for retirement, they will also add some additional money to your retirement account as well!

    Employer matches vary between employers - usually they will contribute a fraction of the amount that you save, up to some limit (e.g. your employer might contribute half the amount you do, up to a maximum of $5000). For simplicity, we will assume that your employer contributes 5% of your salary.

    Copy the code from Exercise 1, and modify them to include an employer contribution of an additional 5% of your salary each year. (I.e. you will need to add this additional contribution to your savings each year).

    (Your final savings value should be about $1,737,895.)

    Commit your work after this exercise.

  4. We have assumed that our income stays fixed for our whole career. Hopefully that is not the case!

    Let’s make the simple assumption that we will get a $1000 pay rise every year.

    Copy and paste your code from Exercise 3 and within the for-loop, update the income variable after you calculate the new savings. For example, your code for the for-loop might look something like this:

    for(year in ...){
      savings <- ...
      income <- ...
    }

    (Your final savings value should be about $2,127,290.)

    Also copy your code from Exercise 2 to calculate what 4% interest on this final savings value is (i.e. how much you could safely withdraw per year without reducing the amount in the savings account).

    Commit your work after this exercise.

  5. It can feel hard to save for retirement early in your career when you have a lower salary. It might feel like it would be easier later on when you have a higher salary and thus (hopefully) more disposable income.

    Let’s compare what would happen if we saved 15% of our income in the last ten years of our career (from ages 56-65, with no saving prior to that), versus what would happen if we saved 5 percent of our income in the first 10 years of our career (i.e. only saving between ages 22-31, and then nothing after that).

    Write two more simulations to run these two scenarios by copying and modifying your code from Exercise 4 (you will find this easiest if you put each one in its own code chunk). You will probably need to use a conditional if-else statement inside the for-loop to run different code dependending on what year you are in, e.g. something similar to this:

    for(year in ...) {
      if(year >= ...) {
        savings <- ...
      } else {
        savings <- ...
      }
      income <- ...
    }

    Tips:

    • Note that in both cases your employer will only contribute an additional 5% of your salary to your retirement account in years when you also contribute.
    • In both cases we are interested in the total savings that you have by the time that you are 65 years old.
    • Make sure that you are adding interest correctly in both simulations (in the early saving scenario you will earn interest after age 31; however, in the late saving scenario you need some savings before you can start earning interest).

    Write a comparison of the results of these two simulations. Also, what does this imply for our two retirement saving strategies (i.e. does it seem to be more effective to start saving for retirement early, or can you easily catch up by saving a larger amount later)?

    Commit your work after this exercise.

How to submit

To submit your lab assignment, follow the two steps below. Your lab will be graded for credit after you’ve completed both steps!

Credits

This lab is released under a Creative Commons Attribution-ShareAlike 4.0 International License. Lab instructions were written by Dominic White.