# Monte Carlo Simulation: Random Sampling, Trading and Python

Updated by Chainika Thakar (Originally written by Zach Oakes)

Years ago, I had made it to the ﬁnal round in an interview for a Senior Delta One/Quantitative Futures position at an HFT ﬁrm (unnamed for privacy). Things were going well, I had answered two out of three of those ridiculous questions that are only applicable in Sub Saharan Africa or Finance interviews (Like how to get 5 gallons from a 6 and 4-gallon jug); I was feeling good.

They asked me about my optimisation process — a layup compared to most — and I went through my process and ended with Monte Carlo Simulation, where their Head of Quant asked me How I run Monte Carlo Simulations, and what parameters I use.

The easy answer is “I run it in Multicharts”.

I clicked Monte Carlo — but I decided to try to explain my Python code. I got so wrapped up in it, by the end of it I had lost my place and forgotten what Monte Carlo is really doing at its core. What should have been a home run became a sloppy drawn out mess of an answer while missing the key points.

I essentially explained the world's most confusing backtest/parameter optimisation and blanked on what was unique by the time I got there in my explanation. I want to make a point to emphasise that there’s a lot more to Monte Carlo’s than colourful line plots.

Luckily I did later realise what I was grasping for — and I used my favourite analogy; If a backtest is a Ladder, Monte Carlo is randomly rearranging the rungs on that ladder, and determining the likelihood of possible outcomes. THAT is an answer — if only it were my ﬁrst answer. Needless to say, I wasn’t offered this job, but it taught me an important lesson — knowing what your models and code are doing is just as important as being able to write them.

After reading this article, I will ensure you don’t fall victim to coding yourself into a corner with a model like Monte Carlo. First of all, let me clarify that there are a few different types of Monte Carlo optimisations — they are not all created equally. First of all, there is entirely random Monte Carlo’s, Random within a Normal Distribution Monte Carlo’s, and simple Random Trade Order.

Random can be further sectioned into with or without replacement, but I will leave it at these three types —which should make more sense to you as we continue. I will primarily focus on entirely (pseudo) Random Monte Carlo’s, as I ﬁnd them to be the most useful / least prone to error (for more info on limitations of Normal Distributions, I encourage you to read the Incerto series by Nassim Nicholas Taleb).

Many of you have either heard of or extensively used Monte Carlo methods of optimisation or simulation — it can be an invaluable tool in measuring the unpredictable. They are not only useful in optimisation problems but great for forecasting things like Max DD, or complex scenarios like the probability of your savings being sufﬁcient for retirement expenses. I primarily use them for two key parts of development; Portfolio Optimisation/Selection, and System/ Portfolio Stress Testing.

All the concepts covered in this blog are taken from this Quantra learning track on Options Volatility Trading. You can take a Free Preview of the course.

Let us dive further into the blog that covers:

## What is Monte Carlo Simulation?

Monte Carlo Simulation is a computational method used to model and analyse complex systems or processes with uncertainty and randomness. It's named after the Monte Carlo Casino in Monaco, known for games of chance because it relies on random sampling and probability.

Monte Carlo (MC) simulations are models used to model the probability of complex events by compiling thousands - millions of various outcomes with a predetermined ‘random’ (changing) variable. Essentially you run 10k iterations with random values for a speciﬁc variable, in hopes of ﬁnding an optimum value or determining a range of possible outcomes — i.e. using randomness to solve a complex problem.

Here's a simplified explanation of how Monte Carlo Simulation works:

### Step 1 - Problem Modelling

You start with a real-world problem or system that involves uncertainty and variability. This could be anything from financial investments to engineering designs, project scheduling, or risk assessment.

### Step 2 - Random Sampling

Instead of trying to solve the problem analytically (which may be challenging or impossible due to its complexity), you use random sampling to generate a large number of possible scenarios or inputs based on known probability distributions. These distributions represent the range of uncertainty in the system.

### Step 3 - Simulation

For each set of random inputs, you apply the rules, equations, or algorithms that govern the system. This allows you to calculate a corresponding output or result. This process is repeated many times, typically thousands or even millions, to create a broad range of simulated outcomes.

### Step 4 - Statistical Analysis

With the dataset of simulated results, you can perform statistical analysis to gain insights into the behaviour of the system. This includes understanding the distribution of possible outcomes, calculating probabilities, and identifying potential risks or opportunities.

Monte Carlo Simulation is particularly valuable in situations where deterministic modelling is impractical because of the complexity of the problem or the presence of randomness.

Some common applications include:

• Finance: Assessing investment risks, estimating portfolio returns, and pricing financial derivatives.
• Engineering: Evaluating the reliability and performance of complex systems, such as structural analysis or electronic circuit design.
• Project Management: Predicting project completion times and budget overruns.
• Science: Modelling physical phenomena, like simulating particle interactions in particle physics experiments.
• Risk Analysis: Assessing risks in various industries, from insurance to environmental impact assessments.

In essence, Monte Carlo Simulation helps decision-makers make more informed choices by considering a multitude of possible outcomes and their associated probabilities in the face of uncertainty.

## Example of Monte Carlo Simulation

A simple example is modelling the Maximum Sharpe Ratio of a Portfolio, based on ‘random’ security weights — so you have a Portfolio comprised of AAPL, AMZN, AMD, & ADBE and you want to determine the ideal weighting of these securities to maximise Sharpe ratio.

The other more common scenario is using Monte Carlo Simulations to determine the probability of outcomes — for example, % Risk of Ruin with a portfolio, given its return characteristics (Mean, Std), and initial balance. This is where Monte Carlo Simulations have applications in virtually every ﬁeld from Finance and Engineering to Logistics or Social Sciences.

Many common metrics such as VaR and CVaR (Conditional Value at Risk) are derived at their core from Monte Carlo Simulations and have proven to be a valuable tool in a Quant’s toolkit.

The most important thing to take away from this is that Monte Carlo Sims are endlessly ﬂexible —if there’s ever a problem that you need to solve that you cannot ﬁgure out, chances are Monte Carlo Simulations can be used to get you pretty close to correct.

## Importance of Monte Carlo Simulation in trading

Monte Carlo Simulation holds significant importance in the field of trading for several compelling reasons:

• Risk Assessment and Management: Trading inherently involves risk, and understanding the potential risks associated with various strategies and portfolios is crucial. Monte Carlo Simulation allows traders to model numerous market scenarios, providing a comprehensive view of possible outcomes and associated risks. This aids in crafting risk management strategies, setting stop-loss levels, and making informed decisions.
• Complex Portfolio Optimisation: Modern trading often involves diverse portfolios with multiple assets, each having its own risk-return profile. Monte Carlo Simulation helps traders optimise portfolio allocation by considering various combinations of asset weights. This optimisation can lead to the creation of portfolios that offer better risk-adjusted returns.
• Stress Testing: Markets can be highly unpredictable, and traders need to ensure their strategies can withstand adverse conditions. Monte Carlo Simulation enables stress testing by simulating extreme market events, helping traders identify vulnerabilities in their trading plans and make necessary adjustments.
• Quantifying Uncertainty: Financial markets are influenced by a multitude of factors, making future price movements uncertain. Monte Carlo Simulation provides a quantitative approach to assess this uncertainty, offering probabilistic forecasts rather than deterministic ones. Traders can gauge the likelihood of achieving specific returns or encountering losses under different market conditions.
• Strategy Development and Testing: Traders can use Monte Carlo Simulation to develop and test new trading strategies. By simulating the strategies under various market scenarios, they can evaluate performance, refine tactics, and gain confidence in their approach before risking real capital.
• Asset Valuation: Monte Carlo Simulation is valuable for estimating the fair value of financial instruments, especially options and derivatives. It considers various factors, such as volatility and interest rates, which impact asset prices. This aids traders in pricing and trading options effectively.
• Scenario Analysis: Trading decisions often involve considering multiple factors simultaneously, such as interest rates, economic indicators, and geopolitical events. Monte Carlo Simulation allows traders to incorporate all these variables into their analysis and understand the potential outcomes under different scenarios.
• Data-Driven Decision-Making: In an era of big data, traders have access to vast amounts of information. Monte Carlo Simulation can process this data to generate actionable insights. It can help traders identify patterns, correlations, and potential trading opportunities, enhancing data-driven decision-making.
• Education and Training: For aspiring traders, Monte Carlo Simulation serves as a valuable educational tool. It allows them to gain hands-on experience in risk assessment, strategy development, and decision-making in a controlled, simulated environment before venturing into real trading.

In essence, Monte Carlo Simulation empowers traders with a powerful tool to make more informed, data-driven decisions, manage risk effectively, optimise portfolios, and navigate the dynamic and uncertain world of financial markets with greater confidence.

## Explanation of random sampling and its role in the simulation

Random sampling plays a pivotal role in Monte Carlo Simulation by mimicking the inherent randomness and uncertainty present in real-world scenarios.

Here's an explanation of random sampling and its critical role in the simulation process:

### What is Random Sampling?

Random sampling is a statistical technique that involves selecting a subset of data or values from a larger dataset in such a way that each element in the population has an equal chance of being included. It essentially mimics the concept of drawing random samples or observations from a real-world distribution. The randomness in sampling helps capture the variability and uncertainty present in complex systems.

### Role in Monte Carlo Simulation

In Monte Carlo Simulation, random sampling is used to model uncertainty and variability within the parameters and variables of a given problem. Here's how it operates within the simulation:

1. Generating Random Inputs
• Parameter Variability: In many trading scenarios, parameters like asset returns, volatilities, interest rates, or economic indicators are uncertain and subject to change. Random sampling allows us to generate multiple sets of these parameters, each representing a different possible state of the market.
• Random Scenarios: Monte Carlo Simulations often involve running thousands or even millions of iterations. In each iteration, random values are drawn for these parameters, creating a diverse set of scenarios. These random scenarios simulate the possible future states of the market.

2. Running Simulations

• Simulating Market Movements: With the random inputs in place, the simulation calculates the outcome or result of interest (e.g., portfolio returns, risk metrics) for each scenario. By doing this repeatedly with different random inputs, the simulation creates a distribution of possible outcomes.
• Monte Carlo Iterations: The number of iterations determines the granularity of the simulation. More iterations provide a more accurate representation of the potential outcomes but require more computational resources.

3. Analysing Results

• Statistical Distribution: The collection of results from the simulations forms a statistical distribution. This distribution reflects the range of possible outcomes and their associated probabilities.
• Risk Assessment: Traders can use this distribution to assess risk. They can calculate measures such as value at risk (VaR), conditional value at risk (CVaR), or drawdowns to understand the downside risk associated with their strategies.

### Significance of Random sampling

Random sampling is significant in Monte Carlo Simulation because it allows traders and analysts to:

• Account for uncertainty: It captures the inherent randomness and unpredictability of financial markets, making simulations more realistic and robust.
• Explore a wide range of scenarios: By generating random inputs, Monte Carlo Simulations consider a multitude of market conditions, enabling a comprehensive analysis of risk and return.
• Make informed decisions: Traders can use the results of the simulation to make data-driven decisions and develop strategies that are resilient in the face of market uncertainty.

## Steps with an example of portfolio risk assessment in Monte Carlo Simulation

Now we will discuss the steps for applying the Monte Carlo Simulation.

But, we will delve into a practical example of how Monte Carlo Simulation can be applied to assess the risk of a portfolio in the world of trading.

The steps with our example go as follows:

### Step 1 - Define the Problem

Suppose you are managing a diverse portfolio of stocks and bonds. You want to estimate the potential risk associated with this portfolio over the next year, considering various market scenarios.

### Step 2 - Data Collection

Gather historical daily returns of the securities in your portfolio. Ensure that the data includes a sufficient time frame to capture different market conditions. It's often best to use log-returns for more accurate modelling.

### Step 3 - Monte Carlo Simulations

Now, we initiate the Monte Carlo Simulations. Here's what we do:

• Initialise arrays to store the performance metrics (returns, volatility, Sharpe ratio) for each simulation run.
• Set up a loop for the simulations, specifying the number of runs. You can start with a reasonable number, like 1,000, and scale up as needed.
• The key to Monte Carlo magic lies in the weights. In each run, we randomly assign weights to each asset in the portfolio. This randomness ensures that each run is unique and represents a different asset allocation.
• Calculate the portfolio's return, volatility, and Sharpe ratio for each run and save them in their respective arrays.

### Step 4 - Analysing Results

Once the simulations are complete, we have a wealth of data. Run an argmax() function on the Sharpe ratio array (or the metric you're optimising). This will give you the set of weights that generated the highest Sharpe ratio. In our example, let's say it's Run 477.

### Step 5 - Decision-Making

Now, you have valuable insight. The asset allocation from Run 477, which gave you the best risk-adjusted return, can be considered your ideal portfolio mix. You can explore the optimal values for all weights by referencing the corresponding run number.

Bonus Tip:

We've also included a handy helper function that saves these optimal weights and the corresponding tickers into a DataFrame and pickles it for reference. This can be extremely useful for tracking and implementing the optimal portfolio mix in practice.

In summary, Monte Carlo Simulation empowers you to explore countless scenarios, helping you make informed decisions when managing a portfolio. By randomly varying asset weights and assessing their impact on risk and return, you can arrive at an optimal allocation strategy tailored to your specific investment goals.

## Monte Carlo Simulation with Python

We could set the spread ratio as a random variable and run it as a Monte Carlo Simulation. In just 5 minutes and 100k iterations, we had a simple 15-line solution to a problem that initially took maybe 350 lines of Python when we attempted to use a minimisation function. This is the adjustable wrench in your toolbox.

Let's dive in, and we're going to over-comment this code so it couldn't be clearer what's doing what.

### Step 2: Save simulation results and calculate portfolio returns as well as volatility

Next step is to calculate the volatility and returns of portfolio, crucial for assessing investment performance and risk management.

### Step 3: Calculate annualised sharpe ratio

Now, let us create a data frame from the simulation results and calculate the maximum point of sharpe ratio in the portfolio in order to find out the optimal portfolio.

### Step 4: Plot the efficient frontier to find out the optimal portfolio point

If you’re a visual person, you can plot it with a quick pyplot.

'''Plot the Markowitz efficient frontier'''

Output:

Remember, you can make these return and volatility columns maximise anything you’d like — Correlation, Beta, anything. You can also randomise anything you’d like to optimise for within reason — we just need to ensure the logic works and that it’s incorporated properly.

(Hint: We've taken entire strategies and simply put a Monte Carlo simulations loop at the VERY end when calculating return, and added weights or thresholds in to be randomised and multiplied by the returns to optimise for them — it could even theoretically be randomised before the entries, just put a loop in there and randomise your entry characteristics.)

Our hope is to open you up to a world of possibilities for Monte Carlo simulations to solve equations you never thought possible.

Now, you can calculate the Sharpe ratio and find the optimal portfolio weights from the previously computed efficient frontier.

Output:

Optimal Portfolio Weights: [0.30122608 0.33272641 0.36604751]
Optimal Portfolio Return: 0.15194464307369632
Optimal Portfolio Volatility: 0.2723708728880492
Max Sharpe Ratio: 0.4477154321997508
Max Annualized Sharpe Ratio: 1.5509317518052546


Our next example involves a more common Monte Carlo simulation method, where we use Portfolio characteristics to predict expected returns, variance, and worst-case scenarios. We'll use the same data in this example and visualise the results. Rest assured, this one is much simpler.

In this case, all that's required is to obtain the mean daily (log) return and the daily standard deviation of our system or portfolio. Once these values are plugged in, we have all the necessary information. We just need to specify the number of iterations in the range() function and ensure that the plot is within the loop, with .show() outside of it.

Output:

This chart, while visually appealing, lacks practical utility. Our preferred approach is to transform it into a distribution and extract multiple metrics from the entire set of portfolio runs. It's important to note that we achieve this by utilising the daily mean and standard deviation to simulate thousands of one-year (T value) performance trajectories.

We employed a normal distribution for randomness in this case to create a cleaner histogram. However, feel free to experiment with different random distributions or entirely random values/samples as you see fit.

Explore various models and observe how they exhibit variability.

Output:

Mean return %: 667.2478242196812
Median return %: 496.59625512045795
Min return %: 25.18580868253688
Max return %: 5509.973952484023
Standard Deviation %: 601.4898183345118


We enjoy calculating various percentiles and monitoring minimum values, along with several common metrics. Histograms offer a significantly clearer representation of data. In absolute terms, the data can sometimes appear large, so we prefer to normalise it by dividing it by the initial account value to express it as percentage values.

This process is generally straightforward, except for the list comprehension, which calculates each run's result as a percentage of the initial account value.

Output:
Mean: 6672478.242196812
Mean Ret: 667.2478242196813
Median: 4965962.551204579
Median Ret: 496.5962551204579
Min: 251858.0868253688
Min Ret: 25.18580868253688
Max: 55099739.52484023
Max Ret: 5509.973952484023
Stdev 6017907.889904912
sharpe: 2.2322260023454827
5% Quantile 1194209.5277546835
5% Quantile % 119.42095277546836
95% Quantile 17924258.001847178
95% Quantile % 1792.425800184718


So there we have it — Monte Carlo Simulations are one of the most flexible models we have at our disposal. Becoming comfortable with the inner workings of these models can make all the difference in optimising complex problems.

We hope you've also learned not to answer a Monte Carlo interview question with a complex response that misses the point. Instead, dig into the basic moving parts, as that's where the magic truly lies in these models. Mastering Monte Carlo simulations will provide you with the tools to solve otherwise insurmountable equations and challenging problems — or, of course, create visually engaging line plots.

## Tips for Effective Monte Carlo Simulations

Let us now discuss some of the tips for effective results from Monte Carlo implementation below.

• Understand Your Problem: Begin by thoroughly understanding the problem you want to solve or the scenario you want to analyse. Clearly define your objectives, variables, and assumptions.
• Use a Sufficient Number of Iterations: The accuracy of Monte Carlo Simulations improves with a larger number of iterations. Aim for a balance between computational resources and precision; start with a reasonable number and scale up as needed.
• Generate High-Quality Random Numbers: Use a reliable random number generator to ensure that the random inputs are truly random and representative of the underlying distribution.
• Validate Your Model: Compare the results of your Monte Carlo Simulation with historical data or other known benchmarks to validate the accuracy of your model.
• Sensitivity Analysis: Conduct sensitivity analysis to understand how changes in input parameters affect the simulation outcomes. Identify which variables have the most significant impact on your results.
• Variance Reduction Techniques: Explore variance reduction techniques like control variates, importance sampling, and antithetic variates to improve the efficiency of your simulations and reduce computational costs.
• Parallelisation: For complex simulations, consider parallelisation techniques to distribute the computational load across multiple processors or machines, reducing simulation time.
• Documentation: Maintain thorough documentation of your simulation setup, including assumptions, data sources, and code. This ensures transparency and reproducibility.
• Visualise Results: Utilise data visualisation techniques to present the simulation results clearly. Visualisations can help in understanding the distribution of outcomes and identifying trends.
• Stay Informed: Keep up with the latest developments in Monte Carlo Simulation techniques and software tools. Continuous learning can enhance the quality of your simulations.

## Real-World Implementations and Success Stories

Here are a few real-world implementations and success stories of Monte Carlo Simulation in the context of trading:

1. Risk Assessment in Portfolio Management

Example: A portfolio manager at an investment firm wants to assess the potential risk associated with a diversified portfolio of stocks and bonds. They use Monte Carlo Simulation to model different economic scenarios, including market crashes and economic downturns. By running simulations, they estimate the range of possible portfolio returns and identify strategies to minimise losses during adverse market conditions.

2. Option Pricing and Hedging

Example: A derivatives trader is tasked with pricing exotic options with complex pay-off structures. They employ Monte Carlo Simulation to estimate the fair value of these options, taking into account various factors like volatility, interest rates, and underlying asset behaviour. This simulation helps the trader make pricing decisions and implement effective hedging strategies.

Example: A quantitative analyst (quant) is developing an algorithmic trading strategy. Monte Carlo Simulation is used to backtest a strategy across historical market data. By simulating thousands of trades under different market conditions, the quant can optimise the strategy's parameters and risk management rules to maximise returns while minimising drawdowns.

4. Risk Management for Derivative Portfolios

Example: A financial institution holds a portfolio of complex derivatives, including futures, options, and swaps. Monte Carlo Simulation is employed to assess the potential risk exposures associated with these derivatives under various market scenarios. This helps the institution set appropriate risk limits and allocate capital efficiently.

5. Stress Testing and Regulatory Compliance

Example: A bank is required to undergo stress testing as part of regulatory compliance. Monte Carlo Simulation is used to evaluate the bank's resilience to extreme economic shocks, such as a severe recession or financial crisis. The results of the simulation are reported to regulatory authorities to demonstrate the bank's ability to withstand adverse conditions.

Example: A volatility trader uses Monte Carlo Simulation to model future volatility levels in the options market. This simulation helps the trader make informed decisions about when to buy or sell options based on expected changes in market volatility.

7. Monte Carlo Simulations in Risk-Adjusted Returns

Example: An individual trader is looking to optimise their investment portfolio. They utilise Monte Carlo Simulations to analyse how different asset allocations affect risk-adjusted returns over time. By running simulations with various weightings of stocks and bonds, they can identify the portfolio mix that maximises returns while minimising risk.

Hence, Monte Carlo Simulation is an essential tool in the trading domain. It enables the traders, portfolio managers, and financial institutions to make data-driven decisions, manage risk effectively, and optimise trading strategies in a dynamic and uncertain market environment.

## Pros of Monte Carlo Simulation

• Flexibility: Monte Carlo Simulation can model a wide range of complex scenarios and systems, making it applicable across various industries, including finance, engineering, and science.
• Uncertainty Quantification: It provides a robust framework for quantifying and managing uncertainty by generating probabilistic outcomes, allowing for better risk assessment.
• Complex System Modeling: Monte Carlo Simulation can handle intricate systems with multiple variables and dependencies, making it suitable for modelling real-world situations accurately.
• Sensitivity Analysis: It enables sensitivity analysis to identify which variables have the most significant impact on outcomes, helping in better decision-making.
• Risk Assessment: It allows for comprehensive risk assessment, enabling organisations to prepare for and mitigate potential adverse events effectively.
• Data-Driven Insights: Monte Carlo Simulations provide data-driven insights, aiding in making informed decisions and optimising strategies.

## Cons of Monte Carlo Simulation

• Resource-Intensive: Performing a large number of Monte Carlo iterations can be computationally intensive and time-consuming, especially for complex models.
• Garbage-In, Garbage-Out (GIGO): The quality of the simulation's results heavily depends on the quality of input data and assumptions. Incorrect or biassed input can lead to inaccurate outcomes.
• Complexity: Developing and implementing Monte Carlo Simulations can be challenging, requiring expertise in statistics, probability, and simulation techniques.
• Assumption Dependency: The accuracy of Monte Carlo Simulations relies on the validity of underlying assumptions. Deviations from these assumptions can lead to unreliable results.
• Interpreting Results: Analysing and interpreting the vast amount of data generated by Monte Carlo Simulations can be complex, requiring careful statistical analysis.

### Conclusion

Monte Carlo Simulation is a powerful and versatile technique with several advantages, including its flexibility, ability to model complex systems, and robust uncertainty quantification. It is invaluable for risk assessment, sensitivity analysis, and data-driven decision-making across various domains.

However, it comes with challenges, such as computational demands, the reliance on input data quality, and the complexity of implementation. Overall, when applied effectively and with careful consideration of its limitations, Monte Carlo Simulation proves to be a valuable tool for addressing real-world problems and uncertainties.

If you wish to explore the Monte Carlo simulation in detail, you can check our Options Volatility Trading course. With this course, you can learn to apply Monte Carlo simulation to estimate the profit and loss (P/L) distribution of straddle and strangle options positions and much more.

Note: The original post has been revamped on 20th November 2023 for accuracy, and recentness.

Disclaimer: All data and information provided in this article are for informational purposes only. QuantInsti® makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information in this article and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.