Introducing the ‘monaco’ Monte-Carlo Python Library

At the heart of all serious forecasting, whether that be of elections, the spread of pandemics, weather, or the path of a lander on its way to Mars, is a statistical tool known as the Monte-Carlo method. The Monte-Carlo method, named for the rolling of the dice at the famous Monte Carlo casino located in the city-state of Monaco, allows you to quantify uncertainty by introducing randomness to otherwise deterministic processes, and seeing what the range of results is. This is especially valuable for stochastic or nonlinear systems, which includes pretty much everything in the real world above a very basic level of complexity.

In my first job out of college, I was responsible for ensuring rockets would reach orbit and drop off satellites and interplanetary probes exactly when and where we wanted them. Monte-Carlo methods were the gold standard for ensuring that our rocket was robust enough to do the job no matter what the conditions were on launch day. I found it an incredibly valuable approach to characterizing robustness and sensitivities of systems in general, and when I left that job I wanted to find an open-source tool that would let me do this in the future. Alas, I couldn’t find anything. So when the Coronavirus lockdowns hit and I had a bunch of free time with nothing to do, I decided to write my own Monte-Carlo framework from scratch. The idea was to make it fully general, better architected than the software tools I had previously used, statistically rigorous, and of course easy to use.

The result is monaco, a python library for setting up, running, and analyzing Monte-Carlo simulations. Users can define random input variables drawn using chosen sampling methods from any of SciPy’s continuous or discrete distributions (including custom distributions), preprocess and structure that data as needed to feed to their main simulation, run that simulation in parallel anywhere from 1 to millions of times, and postprocess the simulation outputs to obtain meaningful, statistically significant conclusions. Plotting and statistical functions specific to use cases that might be encountered are provided, and repeatability of results is ensured through careful management of random seeds.

Check it out on GitHub, or install it right now!

pip install monaco

The project isn’t finished yet. The last two big features I want to add in are the ability to run the simulations on parallel on a remote server (such as AWS or the like) and some more rigorous approached to sensitivity analysis. Plus the documentation needs brushing up, though hopefully the examples are thorough enough to provide a template.

But it’s working right now! And it can be used for anything that can be modeled by a computer – right now I have examples showing this tool as analyzing the flight of a baseball, the spread of a pandemic, the 2020 US Presidential election, and long-term financial projections for retirement. I’ll let you peruse the images from those example above to whet your appetite.

Leave a Reply