Reference no: EM132255280
Programming Project -
Instructions - The goal of this project is to produce an app that generate quotes for life annuities. This project is intended to be completed by each student individually.
Life Annuities - A life annuity is a stream of period payments that begin at a set time and continue for the rest of the beneficiary's life. For our purposes, we will assume that the payments are made annually. (In practice, payments are made monthly.) A life annuity is a model for a defined benefit pension plan with level benefits.
Since the actual number of payments is uncertain, the actual cost of a life annuity is a random variable. We can simulate the number of payments using the sample( ) function in a way that is similar to what you did in Lab 5 with unfair dice. The process works like this.
For a person of age a, the possible number of payments ranges from 0 to 119 - a (the maximum age in the table is 119). The probabilities assigned to these outcomes are the probability that the person dies at ages a through 119, assuming they are alive when they purchase the annuity. We can get these probabilities (that depend on gender) from a life table. The Social Security 2015 table is provided for you in LifeTable.csv. Next we look at how to use the information in the table.
The fields called "Male_lives" and "Female_lives" are the estimated number of 100,000 individuals born at the same time who are alive at the given age (say, on their birthday). You can use the diff function to determine how many of the original cohort are expected to die each year. This will be proportional to the probability that a member of the cohort dies in each year. To create a vector with the number of males who are alive each year beginning with age a, use lives <- LifeTable$Male_lives[(a+1):120] or the female version. Keep in mind that the table begins with age 0, so the index for age a is a+1.
To actually code this idea, you need to take two things into account:
- The diff function will give you a vector with lives{k+1] - lives[k]. Since lives is decreasing with age [once a person dies, she stays dead], these values will all be negative. So, you actually want -diff(lives).
- The diff function produces a vector that has length(lives) - 1. This will create a problem if you use the result as the probability parameter in sample( ). The solution is to add lives[120] to the vector: c(-diff(lives), lives[120]). In effect, you are saying that if anyone is alive at age 119, they will die in the next year. [You have the power to make this happen in a simulated world ... ]
If you want to convert this vector to probabilities, divide it by the number of lives at age a (= LifeTable$Male_lives[a+1], etc.).
Now, let's do something
Part 1 -
1. Write an r function, sim.lifeAnnuity, that generates n samples of the number of payments in a life annuity purchased by an individual at age a and gender g using the 2015 Social Security table. Use gender in c("Female", "Male"). The arguments are n, a and g. The function returns a vector of length n containing integers ranging from 0 to 119 - a. Test it on a few ages to see if it produces reasonable answers.
2. Use n = 100,000 and a = your age (and gender) to create some simulated output. Then make a histogram of the results, using the parameter probability = TRUE. Overlay the density plot using lines(density(x), col = "your favorite color" [put in the name of your output variable for x and an actual color name.] Does this curve look like any distribution you have seen before?
3. The insurer's cost will be the present value of the payments. In the context of the simulation, we know exactly how many payments are made. So, we can use a fixed annuity formula to evaluate the present value of each simulated annuity. R has a function in the FinCal package called pv.annuity(r, n, pmt, type). This is a vectorized function. If you make the n parameter a vector (of integer values), the result will be a vector of annuity values with n payments. Write a function called sim.pv.lifeAnnuity to simulate present values of a life annuity using this approach. Set pmt = -1 in pv.annuity. The arguments for your function are n, a, and g (as before) and interest rate r.
4. Run sim.pv.lifeAnnuity on the same input you used for problem 2 with r = .05. Create a histogram and density plot for the present values of your simulation. Does the shape of this density make sense in relation to the graph you produced in #2? Explain the change. It might facilitate the comparison if you put the graphs on the same page using par(mfrow=c(2, 1)) and align the x axes using the same xlim range for both. Use par(mfrow=c(1, 1)) to restore the default plot settings.
5. Compute the mean and median of the distribution of present values. Which one is higher? Using the function abline(v = c(mean(x),median(x)), col = c("green", "blue")), add vertical lines to your plot to visualize these statistics. Insurance rates are often based on the expected value of losses. Does that create any issues here? What would you recommend that the company charge for a life annuity?
6. In our course on life contingencies, you will learn a different way to calculate the expected value of a life annuity. The formula that applies there is: t=a+1∑119pt(1+r)-(t-a), where pt = probability that the subject is alive at time t. [The actuarial notation used is a little different because pt should also reflect the starting age, a. A symbol like apt might be better.] Write a function that calculates the actuarial expected value for a life annuity issued at age a, gender g, and interest rate r. Use the same Life Table data. [Hint: the formula can be written as the sum of p*v, where p is a vector of probabilities and v is of the form (1+r)^-(1:n). Here you want the probability that the person is alive at various times, so the probability vector (for males) will be proportional to LifeTable$Male_lives[(a+2):120], etc.]
7. Plot another vertical line on the plot you made in #5 the shows the actuarial present value. Compare the mean of simulated present values with the actuarial present value at several ages. What did you find?
8. To complete this project, you will write an app in R that prompts the user for some information and issues a quote for a life annuity. This code should be in a function called Quote.annuity that has no arguments. Here are some suggestions for writing this function:
- Use variable_name <- readline("message") to prompt the user for input. This function reads input from the keyboard as type = character and stores it in variable_name. You may need to convert it to some other type.
- You can use a while loop to keep offering to provide a quote until the user declines.
- The first question (inside the loop) might be "Do you want an annuity quote? (Y/N)". If the response is a variable called Continue, set cond <- Continue %in% c("Y","y") as the condition in the while loop. If cond is false, print "Bye" and exit the loop with break().
- Ask for a name.
- Ask for date of birth in form mm/dd/yyyy.
- Convert date of birth to a Date variable using as.Date (see text for more details).
- Ask for gender (as m/f) and convert to the form needed for your annuity functions.
- Ask for annual payment desired and convert to numeric.
- Compute age from given date of birth. (Sys.date() will give you today's date.) Caution: the Date class measures the number of days from the starting point. So the difference in date values will be in days. You will need to convert that difference to years in order to get age. I suggest rounding age to the closest integer.
- Compute expected value of the annuity (= payment*actuarial.pv).
- Print the expected value (rounded to even dollars) with a message that includes the name. You can use the cat( ) function for this. Make sure to add "\n" to the end of the message to make sure it moves the cursor to the next line after printing this message.
Submit your file on Canvas to Project, part 1.
Part 2 -
The life table was provided in an Excel format. On a separate worksheet, create an input block to capture:
Name
|
|
Date of Birth
|
|
Gender
|
|
Annual payment
|
|
Compute the actuarial present value of the life annuity using the above information. You may want to set up a table that constructs the same vectors you produced in your R code. The VLOOKUP function may be helpful for extracting the values you need from the life table. Verify that your Excel code produces the same answer as your R code.
Submit this file on Canvas (Project, part 2).
Attachment:- Assignment Files.rar