Retirement Simulation

A recent law school graduate would like your assistance in determining how much to save for retirement. She is planning to invest $3,000 in a tax-sheltered retirement fund at the end of each year. The rate of return each year can be modeled as a normally distributed random variable with a mean of 12 percent and a standard deviation of 2 percent.

a. If she is 30 years old now, how much money should she expect to have in her retirement fund at age 60?

b. What is the probability she will have more than 1 million in her retirement fund when she reaches age 60?

c. How much should she invest each year if she wants the mean value of her portfolio to be at least $1 million at age 60?

d. How much should she invest each year if she wants there to be a 90 percent chance of having at least $1 million in her retirement fund at age 60?

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
runSimulation <- function(trials, years, invest, meanRate, sdRate){
  Sample <- list()
  for(i in 1:trials){
    fund = 0
    for(t in 1:years-1){
      rate = rnorm(1, meanRate, sdRate)
      fund = fund + rate*fund + invest
      # print(paste("current year:", t, "funds:", fund ))
    }
    Sample <- append(Sample, fund)
  }
  # writeLines(paste("EV of the retirment fund:", signif(mean(as.numeric(Sample)),8)))
  # writeLines(paste("SD of the retirment fund:", signif(sd(as.numeric(Sample)),7)))
  return(Sample)
}

A.) If she is 30 years old now, how much money should she expect to have in her retirement fund at age 60?

Results <- as.data.frame(as.numeric(runSimulation(1000, 30, 3000, .12, .02))) %>% 
  rename(results = 'as.numeric(runSimulation(1000, 30, 3000, 0.12, 0.02))')

writeLines(paste("EV of the retirment fund:", signif(mean(Results$results),8)))
## EV of the retirment fund: 723261.91
writeLines(paste("SD of the retirment fund:", signif(sd(Results$results),7)))
## SD of the retirment fund: 54321.72
bw <- 2*IQR(Results$results)/length(Results$results)^(1/3)

ggplot(Results,
       aes(x = results))+
  geom_histogram(binwidth = bw,
                 fill = 'green',
                 color = 'black',
                 alpha = .5)+
  labs(title = "Distribution of Retirment fund",
       x = "Fund Amount",
       y = "Frequency")+
  hrbrthemes::theme_ft_rc()

B.) What is the probability she will have more than 1 million in her retirement fund when she reaches age 60?

calcProbablity <- function(Results, value){
  count = 0
  for(fund in Results){
    if(fund > value){
      count = count + 1
    }
  }
  probability = count/length(Results)
  return(probability)
} 

prob = calcProbablity(Results$results, 1000000)
paste("The probablity is:", prob,"percent")
## [1] "The probablity is: 0 percent"

C.) How much should she invest each year if she wants the mean value of her portfolio to be at least $1 million at age 60?

evFund = 0 
invest = 3000
evList = c()
investList = c()
while (evFund < 1000000) {
  Results <- as.numeric(runSimulation(1000, 30, invest, .12, .02))
  evFund = round(mean(Results), digits = 2)
  evList = append(evList, evFund)
  investList = append(investList, invest)
  writeLines(paste("for investment of:", dollar(invest), "Ev fund is:", dollar(evFund)))
  invest = invest + 50
}
## for investment of: $3,000 Ev fund is: $720,770
## for investment of: $3,050 Ev fund is: $738,489
## for investment of: $3,100 Ev fund is: $749,699
## for investment of: $3,150 Ev fund is: $760,017
## for investment of: $3,200 Ev fund is: $772,735
## for investment of: $3,250 Ev fund is: $784,666
## for investment of: $3,300 Ev fund is: $793,023
## for investment of: $3,350 Ev fund is: $808,908
## for investment of: $3,400 Ev fund is: $817,773
## for investment of: $3,450 Ev fund is: $830,816
## for investment of: $3,500 Ev fund is: $849,085
## for investment of: $3,550 Ev fund is: $859,112
## for investment of: $3,600 Ev fund is: $868,927
## for investment of: $3,650 Ev fund is: $880,864
## for investment of: $3,700 Ev fund is: $893,293
## for investment of: $3,750 Ev fund is: $906,168
## for investment of: $3,800 Ev fund is: $920,561
## for investment of: $3,850 Ev fund is: $931,335
## for investment of: $3,900 Ev fund is: $942,926
## for investment of: $3,950 Ev fund is: $950,025
## for investment of: $4,000 Ev fund is: $962,920
## for investment of: $4,050 Ev fund is: $973,819
## for investment of: $4,100 Ev fund is: $991,027
## for investment of: $4,150 Ev fund is: $999,185
## for investment of: $4,200 Ev fund is: $1,011,929
ev.df <- data.frame(investList, evList)

ev.df %>% 
  ggplot(aes(investList, evList))+
  geom_line(size = 1, color = "green", alpha = .5)+
  labs(title = "Investment vs expected return")+
  hrbrthemes::theme_ft_rc()

D.) How much should she invest each year if she wants there to be a 99 percent chance of having at least $1 million in her retirement fund at age 60?

probMillion = 0 
invest = 3000
probList = c()
investList = c()
while (probMillion < .99) {
  Results <- as.numeric(runSimulation(1000, 30, invest, .12, .02))
  probMillion = calcProbablity(Results, 1000000)
  probList = append(probList, probMillion)
  investList = append(investList, invest)
  # writeLines(paste("for investment of:", dollar(invest), "Probability of getting a million is:", dollar(probMillion)))
  invest = invest + 50
}

writeLines(paste("for investment of:", dollar(invest), "Probability of getting a million is:", probMillion))
## for investment of: $5,000 Probability of getting a million is: 0.993
ev.df <- data.frame(investList, probList)

ev.df %>% 
  ggplot(aes(investList, probList))+
  geom_line(size = 1, color = "green", alpha = .5)+
  labs(title = "Investment vs probability of return", subtitle = "at 99% probability")+
  hrbrthemes::theme_ft_rc()