Fortran Finance
Monte Carlo Simulation
Author: | Mitch Richling |
Updated: | 2025-01-21 13:41:20 |
Generated: | 2025-01-21 13:41:21 |
Copyright © 2025 Mitch Richling. All rights reserved.
Table of Contents
These examples both use simple, resampling monte carlo on historical US financial data. Both examples include both Fortran code to run the simulations, and R code to visualize them. One of the examples illustrates endpoint distribution forecasting while the other illustrates trajectory forecasting.
1. inflation.f90
& inflation.R
This program runs 200000 (trials) inflation simulations on $100 (initial_value). Each simulation is over 20 (years) years of inflation using the last 30 (mc_history_years) years of historical US inflation data. This program prints the value after 20 years for each simulation to STDOUT. If placed in a file, this data may be consumed by inflation.R to produce a nice histogram showing the probability of the value after 100 years.
This is sometimes called "trajectory" Monte Carlo because we record the entire trajectory, the steps, of each simulation. See inflation.f90 for an example of "end point" Monte Carlo – where we only store the final step of each simulation.
2. stocks.f90
& stocks.R
This program runs 2000 (trials) stocks simulations on $100 (initial_value). Each simulation is over 20 (years) years of stocks using the last 30 (mc_history_years) years of historical US stocks data. This program prints the resulting value of each simulation to STDOUT. If placed in a file, this data may be consumed by stocks.R to produce a nice histogram showing the probability of the value after 100 years.
This technique is frequently called "monte carlo" in finance circles. The heart of the idea is pretty simple. Simulate the scenario using resampled values from historical data, and then analyze the result.
3. blend_risk.f90
& blend_risk.R
Scenario:
We start with 4M in the bank. Over the next 50 years we wish to withdrawal 100K at the end of each year – and grow that value over time with inflation. We invest the money in a blend of S&P 500 and US 10 year treasury bonds.
Approach:
For each possible integer percentage mix of S&P & bonds (i.e. percentages of S&P that range from 0% up to 100%) we run 100000
(trials
) simulations. The simulations use historical data. The technique is called "coupled resampling" where we pick a
random year, and use the measured values for that year for all three variables (S&P 500 return, 10 year US Treasury bond
return, and US inflation). This technique attempts to capture the inherent correlation between the variables; however, when
used with low resolution data it can create bias in the results. Note we can switch to uncoupled via the variable
coupled_mc
.
This next image shows the difference in results between coupled resampling (correlated) and uncoupled resampling (uncorrelated) monte carlo for this particular scenario.