Open Sourcing our Monte Carlo Tool
TL;DR
We’ve developed a light Monte Carlo tool for internal use and we’re open sourcing it. You can see the code and installation instructions here -https://github.com/AtherEnergy/MonteCarloTool.
You can file bug reports on Github directly or as comments on https://medium.com/@thecomeonman/open-sourcing-our-monte-carlo-tool-2a48dac7c89b.
Why we built the Monte Carlo tool
Most free MC tools we could find were Excel based so we decided to build an R-Shiny application to run our own simulations. The advantages include -
- Interface: easy to use, doesn’t look like Excel.
- Improvement in speed and performance.
- More options in terms of methods of analysing results, distributions, complex functions.
- Ability to host it on a server so that everyone has access to the same tool without needing a powerful local machine or making copies.
- The option to collaborate with others outside the organisation.
How do we use Monte Carlo simulations at Ather
Manufacturing is not an error free process and sometimes these errors can combine in a way that makes it impossible to put the components together to get to the final assembly. We need to figure out the proportion of instances when this can happen. This gives us an idea of the wastage we can expect and also helps us set error margins with our vendors. This exercise is called a stack up test.
It would require a lot of of effort and money to actually produce so many pieces of each component and then try and assemble various combinations of them to calculate these numbers empirically. That is why we use Monte Carlo simulations instead.
Bonus section: Monte Carlo simulations explained with an example
( Just so I could make the pun at the end )
Imagine you have a rich friend. He owns a large green space which he’d opened up to the public. However, of late, he’s taken an interest in ornithology and is considering building a rehabilitation centre for injured and sick birds. For this, he will need to close the area to public and wall it up to protect the birds from predators and other environmental risks.
Before he begins constructing the walls, he needs to submit the height of the walls to the local authority. They will charge him some fee which increases with the height, and they will charge him a penalty if at any point the wall exceeds the height he has submitted. Unfortunately for him, brick manufacturing is not a high accuracy process. He knows that there is a good chance that a 10 brick high wall would exceed the figure of ( 10 * the typical height of a brick ). He should pick a safer number so as to not incur the fine, but not so safe a number that he ends up paying an unnecessarily high fees.
He calls his brick vendor and asks him the exact heights of the bricks he’s doing to send. They don’t have that number since the bricks haven’t been manufactured yet, but they do have historical data. He doesn’t know how to use that data but asks them to send it anyway hoping that his statistician friend, which is you, can help him out.
You have recently found this nifty tool, released by Ather, to run quick Monte Carlo simulations ( https://github.com/AtherEnergy/MonteCarloTool ) and offer to help him even though you secretly want the park to remain open.
“Monte Carlo is probably the most popular simulation method,” you start off just as he is turning around to leave.
”The user specifies a bunch of input variables, the sort of values they can take, and the formulae to calculate the output variables from these input values. The simulation picks up these details and randomly generates many valid sets of input values. Each of these sets is fed to the formulae to arrive at the respective output values. Since we generated many input sets, we are able to generate as many sets of output values too.”
“Interesting,” he says, and nods a little too eagerly for the situation.
“Using the distributions of the output variables, we can then check what the average or the median value of the output would be, what the ninety ninth percentile would be, what proportion of the times would the output be within a certain range of values, etc.”
“Uh huh.”
“Let’s simplify your problem a little,
- Y = X1 + X2 + X3… X10, where
- X looks like it follows a normal distribution with mean 1 typical-brick-height (TBH) and standard deviation 0.01 TBH ( as observed from the data from your brick vendor ). We don’t need to figure all that out though since the authors at Ather have allowed us the option to use the raw data itself.
Where, obviously, X, the input variable, is the height of each brick, and Y, the output variable, is the height of your wall.”
“Obviously, obviously,” he adds, looking at his watch
You turn to your computer, “So from the data, we randomly pick 10 bricks and add up their heights to see what the total height would be. And then we do this many times over.”
He spins around and beckons you to look at his monitor which shows this -
“If you take a 1000 random combinations of 10 bricks from this data, this is the probability of getting any of these heights. The larger the number of combinations you ask the simulation to consider, the more accurate your output variables will be represented. Here’s what the heights of 100000 sets of 10 random bricks are distributed like.”
“What is that?” he asks, sounding curious.
Triumphant, you patiently explain, “So the X axis is roughly what the precise height of your wall could be, with the probability of it being that height on Y axis.” You run a couple of calculations more and add, “99% of the combinations are less than 10.07372 TBH. At 10.05208 TBH, it falls to 95%. One of those numbers is what you should probably submit to the authorities.”
He thanks you, and reveals that he thought it would take much longer than that. You do your embarrassed smile-shrug but since you loved the park yourself and can’t help but ask why he’s doing it and if there is no other way. He sighs.
“Between you and me, I don’t like birds that much. I was just getting sick of all the people and all the noise from the outside. It wasn’t peaceful anymore. One day, I overheard my lawyer talking to someone else and that gave me the idea.”
“Oh. What idea?”
“That I could build a bird hospital type thing and people would understand.”
“I don’t get it. Why a bird hospital? What did he say?”
“He said something to the tune of, ’You could brick the lawn if your premises were, knowingly or unknowingly, being used to host ill eagle activities.’”
Originally published at https://blog.atherenergy.com on September 24, 2017.