Calculating the Gompertz model parameters for joint survival
calc_gompertz_joint_parameters( p1 = list(age = NULL, mode = NULL, dispersion = NULL), p2 = list(age = NULL, mode = NULL, dispersion = NULL), max_age = 120 )
calc_gompertz_joint_parameters( p1 = list(age = NULL, mode = NULL, dispersion = NULL), p2 = list(age = NULL, mode = NULL, dispersion = NULL), max_age = 120 )
p1 |
A list with |
p2 |
A list with |
max_age |
A numeric. The maximum age for the Gompertz model. |
A list containing:
data |
A data frame with survival rates for 'p1', 'p2', 'joint' survival, and the fitted Gompertz model |
mode |
The mode of the joint Gompertz distribution |
dispersion |
The dispersion parameter of the joint Gompertz distribution |
calc_gompertz_joint_parameters( p1 = list( age = 65, mode = 88, dispersion = 10.65 ), p2 = list( age = 60, mode = 91, dispersion = 8.88 ), max_age = 110 )
calc_gompertz_joint_parameters( p1 = list( age = 65, mode = 88, dispersion = 10.65 ), p2 = list( age = 60, mode = 91, dispersion = 8.88 ), max_age = 110 )
Calculating Gompertz model parameters
calc_gompertz_parameters( mortality_rates, current_age, estimate_max_age = FALSE )
calc_gompertz_parameters( mortality_rates, current_age, estimate_max_age = FALSE )
mortality_rates |
A data frame
with columns |
current_age |
A numeric. Current age. |
estimate_max_age |
A logical. Should the maximum age be estimated? |
A list containing:
data |
The input mortality rates data frame with additional columns like 'survival_rate' and 'probability_of_death' |
mode |
The mode of the Gompertz distribution |
dispersion |
The dispersion parameter of the Gompertz distribution |
current_age |
The current age parameter |
max_age |
The maximum age parameter |
Blanchet, David M., and Paul D. Kaplan. 2013. "Alpha, Beta, and Now... Gamma." Journal of Retirement 1 (2): 29-45. doi:10.3905/jor.2013.1.2.029.
mortality_rates <- dplyr::filter( life_tables, country == "USA" & sex == "male" & year == 2022 ) calc_gompertz_parameters( mortality_rates = mortality_rates, current_age = 65 )
mortality_rates <- dplyr::filter( life_tables, country == "USA" & sex == "male" & year == 2022 ) calc_gompertz_parameters( mortality_rates = mortality_rates, current_age = 65 )
Calculating Gompertz survival probability
calc_gompertz_survival_probability( current_age, target_age, mode, dispersion, max_age = NULL )
calc_gompertz_survival_probability( current_age, target_age, mode, dispersion, max_age = NULL )
current_age |
Current age |
target_age |
Target age |
mode |
Mode of the Gompertz distribution |
dispersion |
Dispersion of the Gompertz distribution |
max_age |
Maximum age. Defaults to |
A numeric. The probability of survival from 'current_age' to 'target_age' based on the Gompertz distribution with the given parameters.
calc_gompertz_survival_probability( current_age = 65, target_age = 85, mode = 80, dispersion = 10 )
calc_gompertz_survival_probability( current_age = 65, target_age = 85, mode = 80, dispersion = 10 )
Calculates the optimal allocation to the risky asset using the Merton Share formula.
calc_optimal_risky_asset_allocation( risky_asset_return_mean, risky_asset_return_sd, safe_asset_return, risk_aversion )
calc_optimal_risky_asset_allocation( risky_asset_return_mean, risky_asset_return_sd, safe_asset_return, risk_aversion )
risky_asset_return_mean |
A numeric. The expected (average) yearly return of the risky asset. |
risky_asset_return_sd |
A numeric. The standard deviation of the yearly returns of the risky asset. |
safe_asset_return |
A numeric. The expected yearly return of the safe asset. |
risk_aversion |
A numeric. The risk aversion coefficient. |
Can be used to calculate the optimal allocation to the risky asset for vectors of inputs.
A numeric.
The optimal allocation to the risky asset.
In case of NaN()
(because of division by zero)
the optimal allocation to the risky asset is set to 0.
Haghani V., White J. (2023) "The Missing Billionaires: A Guide to Better Financial Decisions." ISBN:978-1-119-74791-8.
calc_optimal_risky_asset_allocation( risky_asset_return_mean = 0.05, risky_asset_return_sd = 0.15, safe_asset_return = 0.02, risk_aversion = 2 ) calc_optimal_risky_asset_allocation( risky_asset_return_mean = c(0.05, 0.06), risky_asset_return_sd = c(0.15, 0.16), safe_asset_return = 0.02, risk_aversion = 2 )
calc_optimal_risky_asset_allocation( risky_asset_return_mean = 0.05, risky_asset_return_sd = 0.15, safe_asset_return = 0.02, risk_aversion = 2 ) calc_optimal_risky_asset_allocation( risky_asset_return_mean = c(0.05, 0.06), risky_asset_return_sd = c(0.15, 0.16), safe_asset_return = 0.02, risk_aversion = 2 )
Calculates changes in purchasing power over time, taking into account the real interest rate.
calc_purchasing_power(x, years, real_interest_rate)
calc_purchasing_power(x, years, real_interest_rate)
x |
A numeric. The initial amount of money. |
years |
A numeric. The number of years. |
real_interest_rate |
A numeric. The yearly real interest rate. |
The real interest rate is the interest rate after inflation. If negative (e.g. equal to the average yearly inflation rate) it can show diminishing purchasing power over time. If positive, it can show increasing purchasing power over time, and effect of compounding interest on the purchasing power.
A numeric. The purchasing power.
calc_purchasing_power(x = 10, years = 30, real_interest_rate = -0.02) calc_purchasing_power(x = 10, years = 30, real_interest_rate = 0.02)
calc_purchasing_power(x = 10, years = 30, real_interest_rate = -0.02) calc_purchasing_power(x = 10, years = 30, real_interest_rate = 0.02)
Calculating retirement ruin probability
calc_retirement_ruin( portfolio_return_mean, portfolio_return_sd, age, gompertz_mode, gompertz_dispersion, portfolio_value, monthly_spendings, yearly_spendings = 12 * monthly_spendings, spending_rate = yearly_spendings/portfolio_value )
calc_retirement_ruin( portfolio_return_mean, portfolio_return_sd, age, gompertz_mode, gompertz_dispersion, portfolio_value, monthly_spendings, yearly_spendings = 12 * monthly_spendings, spending_rate = yearly_spendings/portfolio_value )
portfolio_return_mean |
A numeric. Mean of portfolio returns. |
portfolio_return_sd |
A numeric. Standard deviation of portfolio returns. |
age |
A numeric. Current age. |
gompertz_mode |
A numeric. Gompertz mode. |
gompertz_dispersion |
A numeric. Gompertz dispersion. |
portfolio_value |
A numeric. Initial portfolio value. |
monthly_spendings |
A numeric. Monthly spendings. |
yearly_spendings |
A numeric. Yearly spendings. |
spending_rate |
A numeric. Spending rate (initial withdrawal rate). |
A numeric. The probability of retirement ruin (between 0 and 1), representing the likelihood of running out of money during retirement.
Milevsky, M.A. (2020). Retirement Income Recipes in R: From Ruin Probabilities to Intelligent Drawdowns. Use R! Series. doi:10.1007/978-3-030-51434-1.
calc_retirement_ruin( age = 65, gompertz_mode = 88, gompertz_dispersion = 10, portfolio_value = 1000000, monthly_spendings = 3000, portfolio_return_mean = 0.02, portfolio_return_sd = 0.15 )
calc_retirement_ruin( age = 65, gompertz_mode = 88, gompertz_dispersion = 10, portfolio_value = 1000000, monthly_spendings = 3000, portfolio_return_mean = 0.02, portfolio_return_sd = 0.15 )
Calculates the risk adjusted return for portfolio of given allocation to the risky asset.
calc_risk_adjusted_return( safe_asset_return, risky_asset_return_mean, risky_asset_allocation, risky_asset_return_sd = NULL, risk_aversion = NULL )
calc_risk_adjusted_return( safe_asset_return, risky_asset_return_mean, risky_asset_allocation, risky_asset_return_sd = NULL, risk_aversion = NULL )
safe_asset_return |
A numeric. The expected yearly return of the safe asset. |
risky_asset_return_mean |
A numeric. The expected (average) yearly return of the risky asset. |
risky_asset_allocation |
A numeric.
The allocation to the risky asset. Could be a vector.
If it is the optimal allocation then parameters
|
risky_asset_return_sd |
A numeric. The standard deviation of the yearly returns of the risky asset. |
risk_aversion |
A numeric. The risk aversion coefficient. |
A numeric. The risk adjusted return.
Haghani V., White J. (2023) "The Missing Billionaires: A Guide to Better Financial Decisions." ISBN:978-1-119-74791-8.
calc_risk_adjusted_return( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_return_sd = 0.15, risky_asset_allocation = 0.5, risk_aversion = 2 ) calc_risk_adjusted_return( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_allocation = c(0.25, 0.5, 0.75), risky_asset_return_sd = 0.15, risk_aversion = 2 )
calc_risk_adjusted_return( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_return_sd = 0.15, risky_asset_allocation = 0.5, risk_aversion = 2 ) calc_risk_adjusted_return( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_allocation = c(0.25, 0.5, 0.75), risky_asset_return_sd = 0.15, risk_aversion = 2 )
A data frame based on: HMD. Human Mortality Database. Max Planck Institute for Demographic Research (Germany), University of California, Berkeley (USA), and French Institute for Demographic Studies (France). Available at www.mortality.org.
life_tables
life_tables
life_tables
A data frame with 6 columns:
Country name
Sex: "male", "female", "both"
Year
Age
Mortality rate
Life expectancy
Plotting the results of Gompertz model calibration
plot_gompertz_calibration(params, mode, dispersion, max_age)
plot_gompertz_calibration(params, mode, dispersion, max_age)
params |
A list returned by |
mode |
A numeric. The mode of the Gompertz model. |
dispersion |
A numeric. The dispersion of the Gompertz model. |
max_age |
A numeric. The maximum age of the Gompertz model. |
A ggplot2::ggplot()
object showing the comparison between
actual survival rates from life tables and the fitted Gompertz model.
mortality_rates <- dplyr::filter( life_tables, country == "USA" & sex == "female" & year == 2022 ) params <- calc_gompertz_parameters( mortality_rates = mortality_rates, current_age = 65 ) plot_gompertz_calibration(params = params)
mortality_rates <- dplyr::filter( life_tables, country == "USA" & sex == "female" & year == 2022 ) params <- calc_gompertz_parameters( mortality_rates = mortality_rates, current_age = 65 ) plot_gompertz_calibration(params = params)
Plotting the results of Gompertz model calibration for joint survival
plot_joint_survival(params, include_gompertz = FALSE)
plot_joint_survival(params, include_gompertz = FALSE)
params |
A list returned by |
include_gompertz |
A logical. Should the Gompertz survival curve be included in the plot? |
A ggplot2::ggplot()
object showing the survival probabilities
for two individuals and their joint survival probability.
params <- calc_gompertz_joint_parameters( p1 = list( age = 65, mode = 88, dispersion = 10.65 ), p2 = list( age = 60, mode = 91, dispersion = 8.88 ), max_age = 110 ) plot_joint_survival(params = params, include_gompertz = TRUE)
params <- calc_gompertz_joint_parameters( p1 = list( age = 65, mode = 88, dispersion = 10.65 ), p2 = list( age = 60, mode = 91, dispersion = 8.88 ), max_age = 110 ) plot_joint_survival(params = params, include_gompertz = TRUE)
Plots the effect of real interest rates (positive or negative) on the purchasing power of savings over the span of 50 years (default).
plot_purchasing_power( x, real_interest_rate, years = 50, legend_title = "Real interest rate", seed = NA )
plot_purchasing_power( x, real_interest_rate, years = 50, legend_title = "Real interest rate", seed = NA )
x |
A numeric. The initial amount of money. |
real_interest_rate |
A numeric. The yearly real interest rate. |
years |
A numeric. The number of years. |
legend_title |
A character. |
seed |
A numeric. Seed passed to |
A ggplot2::ggplot()
object.
plot_purchasing_power( x = 10, real_interest_rate = seq(-0.02, 0.04, by = 0.02) )
plot_purchasing_power( x = 10, real_interest_rate = seq(-0.02, 0.04, by = 0.02) )
Plotting retirement ruin
plot_retirement_ruin( portfolio_return_mean, portfolio_return_sd, age, gompertz_mode, gompertz_dispersion, portfolio_value, monthly_spendings = NULL )
plot_retirement_ruin( portfolio_return_mean, portfolio_return_sd, age, gompertz_mode, gompertz_dispersion, portfolio_value, monthly_spendings = NULL )
portfolio_return_mean |
A numeric. Mean of portfolio returns. |
portfolio_return_sd |
A numeric. Standard deviation of portfolio returns. |
age |
A numeric. Current age. |
gompertz_mode |
A numeric. Gompertz mode. |
gompertz_dispersion |
A numeric. Gompertz dispersion. |
portfolio_value |
A numeric. Initial portfolio value. |
monthly_spendings |
A numeric. Monthly spendings. |
A ggplot2::ggplot()
object showing the probability of
retirement ruin for different monthly spending levels.
If a specific 'monthly_spendings' value is provided,
it will be highlighted on the plot with annotations.
plot_retirement_ruin( portfolio_return_mean = 0.034, portfolio_return_sd = 0.15, age = 65, gompertz_mode = 88, gompertz_dispersion = 10, portfolio_value = 1000000, monthly_spendings = 3000 )
plot_retirement_ruin( portfolio_return_mean = 0.034, portfolio_return_sd = 0.15, age = 65, gompertz_mode = 88, gompertz_dispersion = 10, portfolio_value = 1000000, monthly_spendings = 3000 )
Plots the risk adjusted returns for portfolios of various allocations to the risky asset.
plot_risk_adjusted_returns( safe_asset_return, risky_asset_return_mean, risky_asset_return_sd, risk_aversion = 2, current_risky_asset_allocation = NULL )
plot_risk_adjusted_returns( safe_asset_return, risky_asset_return_mean, risky_asset_return_sd, risk_aversion = 2, current_risky_asset_allocation = NULL )
safe_asset_return |
A numeric. The expected yearly return of the safe asset. |
risky_asset_return_mean |
A numeric. The expected (average) yearly return of the risky asset. |
risky_asset_return_sd |
A numeric. The standard deviation of the yearly returns of the risky asset. |
risk_aversion |
A numeric. The risk aversion coefficient. |
current_risky_asset_allocation |
A numeric. The current allocation to the risky asset. For comparison with the optimal allocation. |
A ggplot2::ggplot()
object.
Haghani V., White J. (2023) "The Missing Billionaires: A Guide to Better Financial Decisions." ISBN:978-1-119-74791-8.
plot_risk_adjusted_returns( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_return_sd = 0.15, risk_aversion = 2, current_risky_asset_allocation = 0.8 )
plot_risk_adjusted_returns( safe_asset_return = 0.02, risky_asset_return_mean = 0.04, risky_asset_return_sd = 0.15, risk_aversion = 2, current_risky_asset_allocation = 0.8 )
Wrapper functions for printing nicely formatted values.
print_currency( x, suffix = "", big.mark = ",", accuracy = NULL, prefix = NULL, ... ) print_percent(x, accuracy = 0.1, ...)
print_currency( x, suffix = "", big.mark = ",", accuracy = NULL, prefix = NULL, ... ) print_percent(x, accuracy = 0.1, ...)
x |
A numeric vector |
big.mark |
Character used between every 3 digits to separate thousands. |
accuracy |
A number to round to. Use (e.g.) Applied to rescaled data. |
prefix , suffix
|
Symbols to display before and after value. |
... |
Other arguments passed on to |
A character. Formatted value.
A character. Formatted value.
print_currency(2345678, suffix = " PLN") print_percent(0.52366)
print_currency(2345678, suffix = " PLN") print_percent(0.52366)
Reading HMD life tables
read_hmd_life_tables( path = getwd(), files = c("mltper_1x1.txt", "fltper_1x1.txt", "bltper_1x1.txt") )
read_hmd_life_tables( path = getwd(), files = c("mltper_1x1.txt", "fltper_1x1.txt", "bltper_1x1.txt") )
path |
A character. Path to the folder with life tables. |
files |
A character. Names of files with life tables. |
A data frame containing mortality data with columns:
sex |
Character - sex ('male', 'female', or 'both') |
year |
Integer - the year of the data |
age |
Integer - age |
mortality_rate |
Numeric - mortality rate |
life_expectancy |
Numeric - life expectancy |
HMD. Human Mortality Database. Max Planck Institute for Demographic Research (Germany), University of California, Berkeley (USA), and French Institute for Demographic Studies (France). Available at www.mortality.org
## Not run: # Download 'txt' files # ("mltper_1x1.txt", "fltper_1x1.txt", "bltper_1x1.txt") # for a given country to the working directory # from https://www.mortality.org after registration. read_hmd_life_tables(path = getwd()) ## End(Not run)
## Not run: # Download 'txt' files # ("mltper_1x1.txt", "fltper_1x1.txt", "bltper_1x1.txt") # for a given country to the working directory # from https://www.mortality.org after registration. read_hmd_life_tables(path = getwd()) ## End(Not run)
Run a package app
run_app( which = c("risk-adjusted-returns", "purchasing-power", "retirement-ruin"), res = 120, shinylive = FALSE )
run_app( which = c("risk-adjusted-returns", "purchasing-power", "retirement-ruin"), res = 120, shinylive = FALSE )
which |
A character. The name of the app to run. Currently available:
|
res |
A numeric. The initial resolution of the plots. |
shinylive |
A logical. Whether to use |
A shiny::shinyApp()
object if shinylive
is TRUE
.
Runs the app if shinylive
is FALSE
with shiny::runApp()
.
run_app("risk-adjusted-returns") run_app("purchasing-power") run_app("retirement-ruin")
run_app("risk-adjusted-returns") run_app("purchasing-power") run_app("retirement-ruin")