NEW Python for Beginners Course Starts July 27! View Course

What is a Monte Carlo Simulation? (Part 2)

By Zachariah Miller • January 30, 2018

This is part 2 of a series by Metis Sr. Data Scientist Zach Miller dedicated to investigating how Monte Carlo can be a great tool. Part 1 introduces the concept of Monte Carlo, and in part 3 (coming soon), he'll try to outsmart a casino using Monte Carlo techniques.


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?

To make this easy to follow along with, I've uploaded some Python notebooks where the entirety of the code is available to view and there are notes throughout to help you see exactly what's going on. So click on over to those, for a walk-through of the problem, the code, and a solution. After seeing how we can setup simple problems, we'll move on to trying to defeat video poker, a much more complicated problem, in part 3. After that, we'll investigate how physicists can use MC to figure out how particles will behave in part 4, by building our own particle simulator (also coming soon).

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.

In the next installment, we'll take everything we've learned from these problems and work on applying them to a more complicated problem. In particular, we'll focus on trying to beat the casino in video poker.


Read Part 1 of Zach's series on Monte Carlo here. Want to hear Zach discuss data science live? He's hosting an online event February 15th on Recommendation Engines and How They Work. To receive an invitation, apply to our data science bootcamp by February 12th.

Similar Posts

data science
Introducing Our New Bootcamp Prep Course: Python for Beginners

By Metis • June 24, 2020

We know the importance of professional development, especially when roles and careers are continuously evolving. Our team is always looking for ways to deliver new courses that students can use to advance in data science. We’re excited to announce our latest bootcamp prep course: Python for Beginners! Learn more here.

data science
3 Ways to Learn Live Online with Metis

By Jane Durand • March 27, 2020

At a challenging time like now, when remote learning is the only option, our live online capabilities have enabled us to continue serving our students and clients without skipping a beat. Here are 3 ways you can learn live online with us.

data science
Now Through June 15th, Enroll in Bootcamp Prep Courses at Reduced Price

By Metis • April 17, 2020

Now through June 15th, when you enroll in any of our upcoming Bootcamp Prep Courses (no matter the start date), you'll pay a reduced price of $499. During this difficult time, we hope this reduced tuition opens up more opportunities for those interested in building data science skills in a live online format.