Types
Abstact Types
The abstract types below can be extended to add new functionality.
RetirementPlanners.AbstractGBM — TypeAbstractGBM <: ContinuousUnivariateDistributionAbstract type for simulating investment growth using Geometric Brownian Motion.
RetirementPlanners.AbstractLogger — TypeAbstractLoggerAn abstract type for recording the state of the model during simulation.
RetirementPlanners.AbstractModel — TypeAbstractModelAn abstract model type for simulating retirement investments.
RetirementPlanners.AbstractState — TypeAbstractStateAn abstract type for tracking the state of the model during simulation.
RetirementPlanners.AbstractTransaction — TypeAbstractTransaction{T, D}An abstract type for scheduling transactions.
Concrete Types
RetirementPlanners.GBM — TypeGBM{T <: Real} <: AbstractGBMA distribution object for Geometric Brownian Motion (GBM), which is used to model growth of stocks.
Fields
μ::T: growth rateσ::T: volitility in growth rateμᵣ::T: growth rate during recessionσᵣ::T: volitility in growth rate during recessionx0::T: initial value of stockx::T: current value
RetirementPlanners.VarGBM — TypeVarGBM{T <: Real} <: AbstractGBMA distribution object for variable Geometric Brownian Motion (vGBM), which is used to model growth of stocks. Unlike GBM, vGBM selects growth rate (μ) and volitility (σ) parameters from a normal distribution on each simulation run to capture uncertainy in these parameters.
Fields
μ::T: growth rate sampled from normal distributionσ::T: volitility in growth rate during, sampled from truncated normal distributionμᵣ::T: growth rate during a recession sampled from normal distributionσᵣ::T: volitility in growth rate during a recession, sampled from truncated normal distributionαμ::T: mean of growth rate distributionασ::T: mean of volitility of growth rate distributionημ::T: standard deviation of growth rate distributionησ::T: standard deviation of volitility of growth rate distributionαμ::T: mean of growth rate distribution during a recessionασ::T: mean of volitility of growth rate distribution during a recessionημ::T: standard deviation of growth rate distribution during a recessionησ::T: standard deviation of volitility of growth rate distribution during a recessionx0::T: initial value of stockx::T: current value
Constructor
VarGBM(; αμ, ασ, ημ, ησ, x0=1.0, x=x0)RetirementPlanners.MvGBM — TypeMvGBM{T <: Real} <: AbstractGBMA distribution object for Multivariate Geometric Brownian Motion (MvGBM), which is used to model growth of multiple stocks and bonds.
Fields
μ::Vector{T}: growth ratesσ::Vector{T}: volitility in growth ratesx0::Vector{T}: initial value of stocksx::Vector{T}: value of stocksratios::Vector{T}: allocation proportion of stocks/bondsρ::Array{T, 2}: correlation matrix between stocks/bondsΣ::Array{T, 2}: covariance matrix between stocks/bonds
Constructor
MvGBM(; μ, σ, ρ, ratios)RetirementPlanners.Model — TypeModel{S, R, T <: Real} <: AbstractModelThe default retirement simulation Model.
Fields
Δt::T: the time step of the simulation in yearsduration::T: the duration of the simulation in yearsstart_age::T: age at the beginning of the simulationstart_amount::T: initial investment amountlog_times::R: a collection of ordered times to log datastate::S: the current state of the systemwithdraw!: a function called on each time step to withdraw from investmentsinvest!: a function called on each time step to invest money into investmentsupdate_income!: a function called on each time step to update income sourcesupdate_inflation!: a function called on each time step to compute inflationupdate_market!: a function called on each time step to compute interest on investmentsupdate_investments!: a function called on each time step to compute net worthlog!: a function called on each time step to log data
Constructor
Model(;
Δt,
duration,
start_age,
start_amount,
log_times,
state = State(),
withdraw! = withdraw!,
invest! = invest!,
update_income! = update_income!,
update_inflation! = dynamic_inflation,
update_market! = dynamic_market,
update_investments! = update_investments!,
log! = default_log!,
config...
)RetirementPlanners.State — TypeState{T<:Real} <: AbstractStateRepresents the state of the model, which is updated on each iteration.
Fields
interest_rate::T: interest rate of investment during the current time periodinflation_rate::T: the inflation rate during the current time periodincome_amount::T: income during the current time period from various sources, e.g., social security, pension, etc.invest_amount::T: the amount invested during the current time periodwithdraw_amount::T: the amount deducted from investments during the current time periodnet_worth::T: total value of the investment during the current time periodlog_idx::Int: the index of the current time step to be logged
RetirementPlanners.Logger — TypeLogger{T <: Real} <: AbstractLoggerAn object for storing variables of the simulation.
Fields
net_worth::Array{T, 2}: total value of investmentsinterest::Array{T, 2}: growth rate of investmentinflation::Array{T, 2}: inflation ratetotal_income::Array{T, 2}: income from investment withdraws, social security etc.
In each array above, rows are time steps and columns are repetitions of the simulation.
RetirementPlanners.Transaction — TypeTransaction{T, D} <: AbstractTransaction{T, D}Specifies the time range and amount of a transaction.
Fields
start_age = 0.0: the age at which a series of transactions beginend_age = Inf: the age at which a series of transactions endamount = 0: the amount of each transaction
Constructor
Transaction(; start_age = 0.0, end_age = Inf, amount = 0)Methods
General Methods
RetirementPlanners.get_times — Functionget_times(model::AbstractModel)Returns the time steps used in the simulation.
Arguments
model::AbstractModel: an abstract Model object
RetirementPlanners.grid_search — Functiongrid_search(
model_type::Type{<:AbstractModel},
Logger::Type{<:AbstractLogger},
n_reps,
all_args;
threaded::Bool = false,
show_progress::Bool = false,
yoked_values = ()
)Performs a grid search over vectorized inputs specified in the configuration setup. As an example, consider the following configuration setup:
config = (
Δt = 1 / 12,
start_age = 30.,
duration = 55.0,
start_amount = 10_000.0,
# withdraw parameters
kw_withdraw = (
distribution = [
Normal(3000, 1000),
Normal(4000, 1000),
],
start_age = 65,
),
# invest parameters
kw_invest = (
distribution = [
Normal(1000, 100),
Normal(1500, 100),
]
end_age = 65,
),
# interest parameters
kw_market = (
gbm = GBM(; μ = .07, σ = .05),
),
# inflation parameters
kw_inflation = (
gbm = GBM(; μ = .035, σ = .005),
)
)In the example above, four simulations will be performed: one for each combination of withdraw distribution and investment distribution.
Arguments
model_type::Type{<:AbstractModel}: an abstract model type for performing Monte Carlo simulations of investment scenariosLogger::Type{<:AbstractLogger}: a type for collecting variables of the simulation. The constructor signature isLogger(; n_reps, n_steps)n_reps: the number of times the investiment simulation is repeated for each input combination.all_args: a NamedTuple of configuration settings
Keywords
threaded::Bool = false: runs simulations on separate threads if trueshow_progress::Bool = false: shows progress bar if trueyoked_values = (): fix specified inputs to have the same values, such as:[Pair((:kw_withdraw, :start_age), (:kw_invest, :end_age))]
Output
Returns a vector of tuples where each tuple corresponds to the result of a single simulation condition. Each tuple consists of input values and output results. The function to_dataframe can be used to transform output into a long-form DataFrame.
Notes
This function was inspired by parmscan in Agents.jl.
RetirementPlanners.is_event_time — Functionis_event_time(model::AbstractModel, t, rate)Indicate whether it is time for a periodic event to occur.
Arguments
model::AbstractModel: an abstract Model objectt: current time (or age) in yearsrate: the interval between repeating events measured in years
Base.rand — Functionrand(dist::AbstractGBM, n_steps, n_reps; Δt)Simulate a random trajectory of a Geometric Brownian motion process.
Arguments
dist::AbstractGBM: a distribution object for Geometric Brownian Motionn_steps: the number of discrete time steps in the simulationn_reps: the number of times the simulation is repeated
Keywords
Δt: the time step for Geometric Brownian Motion
RetirementPlanners.simulate! — Functionsimulate!(model::AbstractModel, logger::AbstractLogger, n_reps)Simulate the a retirement scenario a specified number of times.
Arguments
model::AbstractModel: an abstract Model objectlogger::AbstractLogger: an object for storing variables of the simulationn_reps: the number of times to repeat the Monte Carlo simulation
RetirementPlanners.transact — Functiontransact(
model::AbstractModel,
income::Transaction{T, D};
t
) where {T, D <: Distribution}Sample a amount from a specified distribution and execute a transaction.
Arguments
::AbstractModel: unused model objectinvestment::Transaction{T, D}: a transaction object specifing an investment rule
Keywords
t: the current time
transact(
model::AbstractModel,
income::Transaction{T, D};
t
) where {T, D <: NominalAmount}Execute a transaction using the nominal value of the transaction amount.
Arguments
::AbstractModel: unused model objectinvestment::Transaction{T, D}: a transaction object specifing an investment rule
Keywords
t: the current time
transact(
::AbstractModel,
investment::Transaction{T, D};
t
) where {T, D <: AdaptiveInvestment}Execute an adaptive investment transaction in which the real invested amount increased until reaching a peak earning potential.
Arguments
::AbstractModel: unused model objectinvestment::Transaction{T, D}: a transaction object specifing an investment rule
Keywords
t: the current time
RetirementPlanners.update! — Functionupdate!(model::AbstractModel, logger::AbstractLogger, step, rep, t)Performs an update on each time step by calling the following functions defined in model:
update_inflation!: compute inflationupdate_market!: compute interestinvest!: invest moneywithdraw!: withdraw moneyupdate_income!: update sources of income, such as social security, pension etc.update_investments!: compute net worth for the time steplog!: log desired variables
Each function except log! has the signature my_func(model, t; kwargs...). The function log! has the signature log!(model, logger, step, rep; kwargs...).
Arguments
model::AbstractModel: an abstract Model objectrep::Int: repetition count of simulationtime_step::Int: time step count of simulationt: time in years
Update Methods
Update Income
RetirementPlanners.update_income! — Functionupdate_income!(
model::AbstractModel,
t;
income_sources = Transaction(0.0, -1.0, 0.0)
)Recieve income from specified sources on each time step, as indicated by income_sources.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
income_sources = Transaction(0.0, -1.0, 0.0): a transaction or vector of transactions indicating the amount and time period of income recieved per time step
Update Inflation
RetirementPlanners.fixed_inflation — Functionfixed_inflation(
model::AbstractModel,
t;
inflation_rate = .03
)Returns a fixed inflation rate of a specified value.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
inflation_rate = .03: a constant rate of inflation per year
RetirementPlanners.variable_inflation — Functionvariable_inflation(
model::AbstractModel,
t;
distribution = Normal(.03, .01)
)Returns an interest rate sampled from a specified distribution.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
distribution = Normal(.03, .01): the distribution of inflation per year
RetirementPlanners.dynamic_inflation — Functiondynamic_inflation(
model::AbstractModel,
t;
gbm = GBM(; μ=.03, σ=.01, x0=1),
kwargs...
)Models inflation in the stock market as a geometric brownian motion process.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keyword
gbm = GBM(; μ=.03, σ=.01, x0=1): a geometric brownian motion object with parametersμreflecting mean growth rate, andσreflecting volitility in growth rate. The parameterx0sets an arbitrary scale. The function also supportsVarGBM.kwargs...: optional keyword arguments passed toincrement!
Update Interest
RetirementPlanners.fixed_market — Functionfixed_market(model::AbstractModel, t; interest_rate = .07)Returns a fixed interesting rate using a specified value.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
interest_rate = .07: a constant rate of investment growth per year
RetirementPlanners.variable_market — Functionvariable_market(
model::AbstractModel,
t;
distribution = Normal(.07, .05)
)Returns interest rate sampled from a specified distribution.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
distribution = Normal(.07, .05): the distribution of interest per year
RetirementPlanners.dynamic_market — Functiondynamic_market(
model::AbstractModel,
t;
rebalance_rate = Inf,
gbm = GBM(; μ = 0.07, σ = 0.05, x0 = 1),
kwargs...
)Models the stock market as a geometric brownian motion process.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keyword
gbm = GBM(; μ=.07, σ=.05, x0=1): a geometric Brownian motion object with parametersμreflecting mean growth rate, andσreflecting volitility in growth rate. The parameterx0sets an arbitrary scale. Other variations of geometric Brownian motion can be used, includingVarGBMandMvGBMrebalance_rate = Inf: the time elapsed in years between rebalacing the portfolio. Not applicable toGBMkwargs...: optional keyword arguments passed toincrement!
Update Investments
RetirementPlanners.invest! — Functioninvest!(
model::AbstractModel,
t;
investments = Transaction(0.0, -1.0, 0.0),
real_growth = 0.0,
peak_age = 45
)Contribute a specified amount into investments on each time step.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
investments = Transaction(0.0, -1.0, 0.0): a transaction or vector of transactions indicating the time frame and amount to invest
Log
RetirementPlanners.default_log! — Functiondefault_log!(model::AbstractModel, logger, rep, t; _...)Logs the following information on each time step of each simulation repetition:
net worthinterest rateinflation ratetotal_income
Arguments
model::AbstractModel: an abstract Model objectlogger: a logger objectrep: indexes the repetition of the simulationt: current time in years
Keywords
_...: optional keyword arguments which are not used
Update Investments
RetirementPlanners.update_investments! — Functionupdate_investments!(model::AbstractModel, t)Computes net worth for the current time step as follows:
- withdraw money
- contribute money
- multiply by real growth rate.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Update Withdraw
RetirementPlanners.withdraw! — Functionwithdraw!(
model::AbstractModel,
t;
withdraws = Transaction(0.0, -1.0, 0.0)
)Schedules withdraws from investments as specified in withdraws.
Arguments
model::AbstractModel: an abstract Model objectt: current time of simulation in years
Keywords
withdraws = Transaction(0.0, -1.0, 0.0): a transaction or vector of transactions indicating the amount and time period in which money is withdrawn from investments per time step