Chrome, JavaScript

Monte Carlo Simulation On Compute Engine/App Script

App script on Ido's project placeThis week in Google Developer Live Israel we show you how to work with Google App script in order to run Monte Carlo simulations and get the results in a nice informative way inside google spreadsheets. We took it one step farther, and explained how to run these script on your own instance machine inside Compute Engine with nodeJS. This is powerful option because with the ability to ‘try’ quickly on App Script and than take the code to ‘production’ (=more efficient way) on Compute engine give you more productivity.

So what we are doing? First let’s touch on the definition. This is a problem solving technique used to approximate the probability of certain outcomes by running multiple trial runs, called simulations, using random input variables. The cool aspect is that we are steering the scenarios by using randomness. The randomness helps us ‘try’ each future scenario in a way that includes our probability per variable. This gives us the ability to fine tune our trial runs to answer a question with several variables. In simple words, in our case, it will help us find the best way to spend the time of our sales person.

 Monte Carlo simulations Steps

1. Define a model with a domain of possible inputs.
2. Generate inputs values randomly, but sampling from a probability distribution over the domain.
3. Perform a deterministic computation on the inputs.
4. Aggregate the results.

Our Case

What should our sales friend do in the next 50 days? She can only do 25 big deals that take 2 days per deal or run on 50 small deals that take one day per deal? What is the best blend of big and small deals for her skill sets? We make few assumptions (of course) like:

  • The probability for closing a big deal – Let’s say 30% chance.
  • The probability for closing a small deal – Let’s say 50% chance.
  • What is the value of each deal. For example: $25K for big deal and $10K for small one.
  • The amounts of deals that she can do in X days. For example: a big deal will take two days and a small deal only one day. So if we have 10 days we can do a maximum of five big deals or a maximum of 10 small deals.

All these parameters will drive the results, so you wish to base them as close as possible to reality. The next step, after we define our possible inputs for the problem is to generate inputs randomly and measure the results. In our case, we printed the results back on google sheet and use the charts in order to have a better look on them.

The main usage for App Script in this case is to have a quick ‘look and feel’ for our data. Once, we are happy with this initial phase and we are ready to test 10K (or 10B) scenarios it’s time for us to move our code to Compute Engine. Here we will have Google’s scale under our hands with the ability to simulate our ‘problems’ on lots of machines without any worries about ‘administrative tasks’ like creating images of our virtual machines and deploying them on 10K machines.

Screen Shot 2013-07-11 at 2.43.33 PMHere is the code that will run this simulation on Node.js inside Compute Engine.


3 thoughts on “Monte Carlo Simulation On Compute Engine/App Script

  1. Michael Anguiano says:

    Ido: Great presentation on Monte Carlo simulations. You have a very cool Google App Script demo at the very beginning of the video. Can you provide that Google App Script code? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s