Testing Different Approaches to a Monte Carlo Simulation
By: Roberto Porto Lorena
Tags: Data Visualization, Jupyter Notebook, Excel, Analytics, Vba

How Excel, VBA, and Jupyter Notebooks stacked up to the task

“I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail.” Some historians dispute the authorship of the quote, though its meaning has always been clear. As it relates to my line of work, I know I need more than just a figurative hammer (i.e., Excel) to do meaningful data analysis — I need a full toolbox. Recently, a friend of mine was trying to decide between two options, so I decided to help her by testing the capabilities of Excel, VBA, and Jupyter Notebook to find the best choice. My goal was to 1) be a supportive friend and 2) better understand the differences between Excel, VBA, and Jupyter Notebooks. The article will elaborate on my friend’s problem, Monte Carlo simulation, and the different tools listed.

#1-My friend’s problem

My friend’s car has been a reliable companion for thousands of miles. However, after 165,000 miles, the repair bills have been going up in cost and increasing in frequency. My friend has been saving diligently over the years to afford a new car once the time comes, and she wants to make sure she makes the best financial decision. Should she spend the X dollars she saved buying the car in cash, or should she take a five-year loan and invest her savings?

To answer the question, I set up the problem with five scenarios for market performance (terrible, bad, ok, good, amazing) and implemented constraints around how much she would be able to afford per month in car payments. Option 1 (paying cash for the car) equaled her savings and option 2 (taking a loan and investing her savings) equaled the output of a simulation of the different market scenarios over five years.

#2-Monte Carlo Simulation

From the MIT article listed below, the Monte Carlo simulation is “a statistical technique used to model probabilistic (or “stochastic”) systems and establish the odds for a variety of outcomes.” What it basically means is that when different outcomes are possible, you run a number of simulations to understand the true likelihood of each outcome. As an example, I could flip a coin six times and it could land Heads every time. If I run a Monte Carlo simulation of six flips thousands of times, I would see that the most likely scenario would be of a 50/50 split between Heads and Tails. The article and video below explore the topic in different depths if you’re interested in more information.

#3-The setup

a. Excel

My first attempt at modeling the problem relied on Excel and no VBA coding. I leveraged the Data Table tool, in addition to cell colors and an instructions box to help my friend complete the data input. The model auto-calculated 5,000 scenarios and displayed the data in the histogram on the bottom right and in the cells in the center with statistical values. Overall, the process was the most time-consuming of all as I had to constantly adjust the spreadhseet display (e.g, column sizes, graph positions).

b. VBA

Since I had already put a good amount of effort working with Excel to get the first model done, I decided to use VBA for my second attempt. Even though I relied primarily on the Developer tab, I still leveraged a blank spreadsheet to aggregate the output of each simulation and display the data (as shown in the screenshot below). It took 160+ lines of code to get the result of 5,000 simulations displayed in the histogram in the bottom right and statistical summaries about the distribution in the center.

c. Jupyter Notebook

The transition to Jupyter Notebook from VBA was straightforward since I had fully switched to a coding mindset at this point. Jupyter offered a modern user experience and efficiency in terms of lines of code (about 30% of what VBA required) and computing power. I was able to run simulations into the hundreds of thousands when VBA struggled to go over 10,000 runs. The output below shows the statistics on top and the histogram in the bottom.

#4-Tool performance

You need to understand the problem and your end customer well to choose the right tool for the job. Some projects may require scalability or be one-off, while some of your end customers may be tech-savvy or need hand-holding going through models. For my friend’s problem, the Jupyter Notebook was the best tool given her technical background and desire to customize the assumptions as we went through the model. I summarized Excel, VBA, and Jupyter Notebook below and included a link to an article covering framework, visualization, and statistical methods.

#5-Conclusion

In the end, all models pointed in the same direction: based on my friend’s assumptions and inputs, taking out a loan and investing her savings should be a better option approximately 85% of the time (I emphasize that this is not financial advice). Most importantly, the models helped me deliver on the primary goal of being a supportive friend and better understanding the difference between Excel, VBA, and Jupyter Notebooks.