In this blog, I’ll demonstrate (with complete code, visualization, and explanation) how to make a decision with several unknowns using Monte Carlo simulation and analysis in Python. We’ll tackle a hypothetical scenario of whether or not we should mine for Gold.
We want to know if it’s worth mining for gold in the following situation:
We have some known variables, and several unknowns. For example, if we choose to mine, we know the cost of gold extraction is $900,000. We think the amount of gold may be about 1,000 ounce (which is set as Mean) with a standard deviation of 300 oz. But it’s a guess, we don’t know for sure since we haven’t mined yet. We also don’t know what the price of gold will be next year (as our mining will need to occur early next year) but we’ll use our best guess and state that the Mean may be $1,000 per oz with a standard deviation of $500. The question is: Should we go for it (mine) or not?
The main factor in making the decision is profitability. If we make a reasonable profit, we’ll mine, otherwise, we won’t bother. Having no deterministic way to foretell the future price of gold (i.e. once we mine the gold, how much can we sell it for next year) or the amount of gold that’s in that location, we cannot determine the profitability. However, we need to make a go, no-go decision.
This is a great scenario in which we can utilize Monte Carlo simulation. If you want a quick overview of Monte Carlo simulation method, you want to check out my other post on playoff chances (also linked below) where I give a high-level overview. In this post, I’ll just focus on tackling this specific scenario and less on theories. In essence, we can use Monte Carlo method to generate (simulate) many different random values for amount of gold, and the future price of gold while applying some reasonability (standard deviations, ranges for example) and calculate the profit each time and do it a large number of time. Once we have calculate different profits in different scenarios, we’ll calculate the overall chances (probability) of making a profit (or not), and even find the average amount of profit (or loss) from that simulation. That’s basically the crux of Monte Carlo. The profit function for this scenario is simple: (ounces of gold mined * price of gold per ounce) - cost of extraction.
Before I share the code, let’s see what I got from the simulation. The probability of making a profit came out to be ~52% after 10,000 simulations. Typically, higher the number of simulations, the more accurate the prediction will be. For this scenario, 10,000 is good enough. The Mean profit came out to be ~$90,996. Additionally, I generated a histogram to visually understand the results, which looks like this:
What is it telling us? The red line separates the loss and prof into two sections. The left portion of the line are the scenarios of loss, and the right of the line contains the scenarios of profit. The x-axis is the profit ($) in millions. The y-axis is showing the frequency of each bar occurring…where each bar’s height is the frequency and its corresponding x-axis value is a specific profit range (or loss if it’s to the left of the red line). We see the red line is at about 0 profit meaning break-even. We also see the highest bars (most frequently occuring profit or loss ranges) occur slightly toward the left of zero, and to the right of zero. That means there is a higher chance of breaking even, or a slight loss than a profit. However, we also see there are more bars to the right of the red line, meaning, there are more scenarios that can yield some profit! And for those instances of profits, the average profit was $90,996. That’s what the green vertical line is showing…Mean profit. The probability of profit is computed and displayed in the shell output window. Now that we can interpret the outputs, let’s take a look at the code.
The Python Code:
There’s nothing too tricky here, but I’ll point out a couple of things. The np.random.normal()
function in NumPy generates random samples from a normal (Gaussian) distribution and we specified the Mean and Standard Deviation values as mentioned in the scenario. You’ll notice that our best guesses for amount of gold to be found, and future price of gold are set as Mean values for the sampling. We generate those sample values 10,000 times each (num_simulations
) and from that list of values, we calculate a series of profits (using numpy arrays) for all values in amount_of_gold_samples
and price_of_gold_samples
using the profit function mentioned above. Next, we calculate the probability by counting the number times a profit event occurred and dividing it by the number of simulations. A profit event occurs when sum of profits is greater than 0 in the numpy array of random samples created by np.random.normal()
function. The remaining code deals with plotting the histogram as shown and explained in the diagram above.
So, what’s the decision?
This is not a slam dunk situation because the probability of making a profit is slightly better than a coin toss! But given the slight probability edge and the mean profit, the decision to mine or not hinges on our risk appetite and financial strategy. If we’re prepared for the potential slight losses highlighted by the histogram and are aiming for long-term gains, it might be worth pursuing. However, if minimizing risk is paramount, we might opt for a more certain investment. Ultimately, the decision should align with our overall strategic goals and risk management approach.
What this example illustrated is the usefulness of Monte Carlo that can be used in real-world scenarios in situations where full clarity is not possible ahead of time or deterministic data is not available or possible. It also explains how to interpret the results from the simulation and the actual implementation (code). I hope this was helpful and interesting. Keep exploring!
Related: