411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Pension Simulator
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Pension Simulator Mortgages and Loans Risk Minimization in Finance
To calculate the future capital of a supplementary pension assuming a fixed rate of return r,
the classic compound annuity formula is used:
where
(P) = periodic payment
(r) = average return
(n) = number of periods
In reality, various scientific publications assume that rates of return r fluctuate around an average value.
For example:
| year | return |
| ---- | ----- |
| 1 | 7% |
| 2 | -3% |
| 3 | 11% |
| 4 | 2% |
thumb|left|Normal distributions with mean and standard deviation or volatility
This results in an approximately bell-shaped or normal distribution, which is caused by the fact that financial returns are influenced by many factors:
economics
politics
interest rates
news
investor behavior
The combination of these factors often leads to a distribution similar to the normal, but this is an approximation; in fact, many analysts prefer to approximate rates with the Student t-distribution.
Monte Carlo simulation can be used to obtain a distribution of final pension assets by randomly varying the annual rate of return according to a normal distribution with only two parameters: mean and standard deviation or volatility , and iterating the process, for example, 10,000 times.
Each of the 10,000 simulations produces a different future and a different final net worth.
Once this distribution is obtained, percentiles are calculated, allowing us to understand how much net worth could be had in the various scenarios.
For example, suppose we run 10,000 Monte Carlo simulations of net worth at 30 years. We obtain 10,000 final net worth values.
| Percentile | Net Worth |
| ---------- | ----------- |
| 10% | 350.000 € |
| 50% | 700.000 € |
| 90% | 1.300.000 € |
Interpretation:
10% → pessimistic scenario (10% of the scenarios have a value lower than €350,000)
50% → typical scenario (the median - 50% of the scenarios have a net worth lower or higher than €700,000)
90% → very favorable scenario (only 10% of the scenarios have values higher than €1,300,000)
Implementation in Julia
First, I install the Julia libraries I need:
using Pkg
Pkg.add("Distributions")
Pkg.add("Statistics")
To calculate the net worth obtainable after 30 years with a fixed rate of return of 5%, I use the compound annuity formula in Julia, obtaining €249,677:
function pac(payment, r, n)
payment * ((1+r)^n - 1) / r
end
pac(300, 0.05/12, 12*30)
249677.59060843976
To reach a target pension capital of €500,000 after 30 years with an initial capital of €20,000, an annual payment of €5,000, and a variable rate of return r approximately according to the normal with a mean of 5% and a standard deviation or volatility of 15%, 10,000 Monte Carlo simulations are implemented in Julia and the 10%, 50%, and 90% quantiles are calculated, as well as the probability of reaching the target. target:
using Distributions, Statistics
# parameters
initial_capital = 20000
deposit = 5000
years = 30
target = 500000
mean = 0.05
volatility = 0.15
simulations = 10,000
# return distribution
dist = Normal(mean, volatility)
function simulate()
capital = initial_capital
for i in 1:years
return = rand(dist)
capital = capital*(1 + return) + deposit
end
return capital
end
# run simulations
results = [simula() for i in 1:simulations]
# final capital percentiles
p10 = quantile(results, 0.10)
p50 = quantile(results, 0.50)
p90 = quantile(results, 0.90)
# probability of reaching target
prob_target = mean(results .>= target)
println("Probability of reaching target: ", prob_target)
println("10th percentile: ", p10)
println("Median: ", p50)
println("90th percentile: ", p90)
Probability of reaching target: 0.2676
10th percentile: 176089.39588073976
Median: 352329.90761352365
90th percentile: 731757.8309400893
So:
26.76% probability of reaching the €500,000 target
in In the worst-case scenario, the figure is approximately €176,000.
In the median scenario, it's €352,000.
If we want to create a "complete pension simulator" with 40 years of work in which assets are accumulated with a variable rate of return r approximately according to the normal, with a mean of 5% and a standard deviation or volatility of 15%, and then 25 years of pension in which the withdrawal is equal to the difference between expenditure and the pension, minus inflation of 2%, we obtainand that the probability of not exhausting the capital is 78.29%, and then in the median scenario, it can reach €799,000:
using Distributions, Statistics
initial_capital = 20000
annual_contribution = 6000
work_years = 40
pension_years = 25
average_return = 0.05
volatility = 0.15
inflation = 0.02
annual_expense = 35000
pension = 20000
simulations = 10,000
dist = Normal(average_return, volatility)
function simula_vita()
capital = initial_capital
# accumulation phase
for i in 1:work_years
r = rand(dist)
capital = capital*(1+r) + annual_contribution
end
# pension phase
expenditure = annual_expenditure
for i in 1: pension_years
r = rand(dist)
# pension income
withdrawal = max(expenditure - pension, 0)
capital = capital*(1+r) - withdrawal
# expenditure inflation
expenditure *= (1 + inflation)
if capital <= 0
return 0
end
end
return capital
end
results = [simula_vita() for i in 1: simulations]
# final wealth percentiles
p10 = quantile(results, 0.10)
p50 = quantile(results, 0.50)
p90 = quantile(results, 0.90)
prob_success = mean(results .> 0)
println("10th percentile: ", p10)
println("Median: ", p50)
println("90th percentile: ", p90)
println("Probability of not exhausting the capital: ", prob_success)
10th percentile: 0.0
Median: 799229.9586610713
90th percentile: 4.859875070862252e6
Probability of not exhausting the capital: 0.7829
FIRE Method
The FIRE (Financial Independence, Retire Early) method is based on a simple principle: accumulate enough assets to generate passive income to cover living expenses, making work optional. Its fundamental pillars are the 4% rule (you can withdraw 4% of your assets each year without exhausting them) and its inverse (1/0.04 = 25), the 25x rule (you need 25 times your annual expenses).
Implementation in Julia
Assuming €30,000 in annual expenses, applying the 25x rule, the FIRE goal becomes €30,000 x 25 = €750,000, which must be reached to cover living expenses, making work optional. It takes us a long time to reach the FIRE goal and the accumulated wealth history :
# FIRE goal with 25x rule
annual_expenses = 30,000.0
fire_goal = annual_expenses * 25 # €750,000
# Savings rate (primary leverage)
net_income = 4,000.0 # monthly
monthly_expenses = 2,500.0
monthly_savings = net_income - monthly_expenses
savings_rate = monthly_savings / net_income # 37.5%
# Year-by-year simulation
function fire_simulation(
initial_assets::Float64,
annual_savings::Float64,
return::Float64,
goal::Float64
)
assets = initial_assets
history = [assets]
year = 0
while assets < goal && year < 60
assets = assets * (1 + return) + annual_savings
push!(history, assets)
year += 1
end
return year, history
end
years, history = simulation_fire(50,000.0, monthly_savings * 12, 0.07, fire_goal)
println("Years to FIRE: $years")
Years to FIRE: 18
for a in 1: years
println("Year $a: €$(round(history[a], digits=2))")
end
Year 1: €50,000.0
Year 2: €71,500.0
Year 3: €94,505.0
Year 4: €119,120.35
Year 5: €145,458.77
Year 6: €173,640.89
Year 7: €203,795.75
Year 8: €236,061.45
Year 9: €270,585.76
Year 10: €307,526.76
Year 11: €347,053.63
Year 12: €389,347.39
Year 13: €434,601.7
Year 14: €483,023.82
Year 15: €534,835.49
Year 16: €590,273.97
Year 17: €649,593.15
Year 18: €713,064.67
With the 4% rule, you can withdraw the 4% of assets each year without depleting them:
# Decumulation phase:
function decumulation(assets, annual_expenses, return, years)
p = assets
for a in 1: years
p = p * (1 + return) - annual_expenses
println("Year $a: €$(round(p, digits=0))")
p < 0 && (println("Assets depleted!"); break)
end
end
decumulation(fire_goal, annual_expenses, 0.04, 40)
Year 1: €750,000.0
Year 2: €750,000.0
Year 3: €750,000.0
Year 4: €750,000.0
Year 5: €750,000.0
Year 6: €750,000.0
Year 7: €750,000.0
Year 8: €750,000.0
Year 9: €750,000.0
Year 10: €750,000.0
Year 11: €750,000.0
Year 12: €750,000.0
Year 13: €750,000.0
Year 14: €750,000.0
Year 15: €750,000.0
Year 16: €750,000.0
Year 17: €750,000.0
Year 18: €750,000.0
Year 19: €750,000.0
Year 20: €750,000.0
Year 21: €750,000.0
Year 22: €750,000.0
Year 23: €750,000.0
Year 24: €750,000.0
Year 25: €750,000.0
Year 26: €750,000.0
Year 27: €750,000.0
Year 28: €750,000.0
Year 29: €750,000.0
Year 30: €750,000.0
Year 31: €750,000.0
Year 32: €750,000.0
Year 33: €750,000.0
Year 34: €750,000.0
Year 35: €750,000.0
Year 36: €750,000.0
Year 37: €750,000.0
Year 38: €750,000.0
Year 39: €750,000.0
Year 40: €750,000.0
By withdrawing 3% annually, the assets decrease; with 5% annually, the assets grow beyond the fire target:
decumulation(fire target, annual expenses, 0.03, 40)
decumulation(fire target, annual expenses, 0.05, 40)