What is a Monte Carlo Simulation? (Part 2)

How do we work with Monte Carlo in Python?

A great tool for doing Monte Carlo simulations in Python is the numpy library. Today we’ll focus on using its random number generators, as well as some traditional Python, to set up two sample problems. These problems will lay out the best way for us think about building our simulations in the future. Since I plan to spend the next blog talking in detail about how we can use MC to solve much more complicated problems, let’s start with two simple ones:

  1. If I know that 70% of the time I eat chicken after I eat beef, what percentage of my overall meals are beef?
  2. If there really was a drunk guy randomly walking around a bar, how often would he make it to the bathroom?

What is my average dinner?

The Average Dinner Notebook will introduce you to the idea of a transition matrix, how we can use weighted sampling and the idea of using a large amount of samples to be sure we’re getting a consistent answer.

Will our drunk friend make it to the bathroom?

The Random Walk Notebook will get into deeper territory of using a detailed set of rules to lay out the conditions for success and failure. It will teach you how to break down a big chain of motions into single calculable actions, and how to keep track of winning and losing in a Monte Carlo simulation so that you can find statistically interesting results.

So what did we learn?

We’ve gained the ability to use numpy’s random number generator to extract statistically significant results! That’s a huge first step. We’ve also learned how to frame Monte Carlo problems such that we can use a transition matrix if the problem calls for it. Notice that in the random walk the random number generator didn’t just choose some state that corresponded to win-or-not. It was instead a chain of steps that we simulated to see whether we win or not. On top of that, we also were able to convert our random numbers into whatever form we needed, casting them into angles that informed our chain of motions. That’s another big part of why Monte Carlo is such a flexible and powerful technique: you don’t have to just pick states, but can instead pick individual motions that lead to different possible outcomes.

Accelerating data science careers through full-time bootcamps, part-time professional development courses, corporate training & more. NYC | SF | CHI | SEA | DC.

Share your ideas with millions of readers.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store