Minimum-Variance Portfolio and Monte Carlo Simulation on Selected Stocks’ Returns
Fall 2016 MATH 5800-030 — Group 6 Final ProjectBy Zhenqian Li and Pei Wang
09 December 2016
Background and Objectives
We have learned many useful programming skills for financial analyzing with Matlab, Python and R in this course. We want to apply the knowledge to complete a comprehensive project using Python and Quantopian with these following objectives:- Retrieve selected stocks' data from Yahoo Finance for a certain period.
- Establish a minimum-variance portfolio based on the stocks' returns.
- Run back-testing for our portfolio on Quantopian and compare it with the stock market.
- Develop Monte Carlo Simulation on selected stocks’ returns and compare it with their real performances.
Methodology
Minimum Variance Portfolio
Portfolio variance is a measurement of how the aggregate actual returns of a set of securities making up a portfolio fluctuate over time. So the minimum variance portfolio is a portfolio of individually risky assets that, when taken together, result in the lowest possible risk level for the rate of expected return. The name of the term comes from how it is mathematically expressed in Markowitz Portfolio Theory, in which volatility is used as a replacement for risk, and in which less variance in volatility correlates to less risk in an investment.
Generally, in minimum variance portfolio, we aim to minimize:
$$w^{T}Cw$$
Here, C is a covariance matrix of the returns and for w on the expected portfolio return R^{T}w whilst keeping the sum of all the weights equal to 1:
$$\sum_{i}w_{i}=1$$
$$w^{T}Cw$$
Here, C is a covariance matrix of the returns and for w on the expected portfolio return R^{T}w whilst keeping the sum of all the weights equal to 1:
$$\sum_{i}w_{i}=1$$
Efficient Frontier
The efficient frontier is the set of optimal portfolios that offers the highest expected return for a defined level of risk or the lowest risk for a given level of expected return. And all other portfolios below this curve is called sub-optimal portfolio. Because at the same risk level, those portfolios give a lower return comparing the portfolios on the efficient frontier. Notice that the minimum variance portfolio (or Global Minimum-Variance Portfolio) is on the end point of efficient frontier as shown below:
Monte Carlo Simulation
![]() |
| Figure 1: Sample Efficient Frontier |
Monte Carlo Simulation
Monte Carlo simulations are used to model the probability of different outcomes in a process that cannot easily be predicted due to the intervention of random variables. In our project, we use random walk as method to simulate the stock price trend and compare it to the actual stock price.
Results
Retrieve Data
We selected eight different companies’ stocks as our data: TWTR, AAPL, FB, GOOG, MSFT, BA, WMT and GM. We used Python to retrieve the stocks’ data for us. Luckily, there is already a library in Python called yahoo-finance. We first downloaded this library using the following command line:
Command prompt install:
pip install yahoo_finance
Once we have installed the yahoo_finance library, we used the following code to get the data with the time interval from 01/01/2016 to 06/30/2016.
After that, we combined all the adj. close prices for these eight stocks and put it in one Excel sheet as saved as ‘stockprices.xlsx’ for further reference.
![]() |
| Figure 2: Retrieve Data Code |
Figure 3: Data Import in Python
|
Minimum Variance Portfolio
Before we try to find out the minimum
variance portfolio, we will obtain the efficient frontier of our portfolio
first. The first step is; we will create random weights by the following
function:
Figure 4: Function to Create Random Weights
|
Figure 5: Random Portfolios
|
![]() |
Figure 6: Mean and Standard Deviation of Returns of
Randomly Generated
Portfolios
|
![]() |
Figure 7: The Efficient Frontier
|
As we can see from the plot, each dot represents a portfolio with their corresponding return and risk level. The red-dotted line as shown is efficient frontier. All portfolios on the frontier are optimal in a given risk level. So what we need to do next is to find out the portfolio we aim to achieve: Minimum Variance Portfolio.
The following Python function is defined to solve the quadratic programming problem we mentioned before. We aim to find out the portfolio with minimum risk level from all the random portfolios and obtain the corresponding weights. Notice that the input parameter is a returns vector and the only constraint we need to set is that all weights must sum up to one.
Figure 8: Minimum Variance Solver Function
|
Figure 9: The Optimal Weights
|
Back-testing
After we calculated the weights of the eight stocks in the portfolio, we used the following code to run the back-testing on Quantopian. We assigned the weights and set the function to rebalance the investing portfolio one hour after market opens. At last, we also called another function to record and plot the profit of our portfolio at the end of each day.
![]() |
Figure 10: Back-testing Code
|
Figure 11: Back-testing Result
|
Develop Monte Carlo Simulation
The method of Monte Carlo Simulation was briefly introduced in this class, but it is widely used in the financial world. So we want to have a try and see what we can get with this method. From previous part for establishing the minimum-variance portfolio, we noticed that the WMT stock has an especially huge weight than any other stock in the portfolio. That is because the main idea of minimum variance portfolio is to reduce the variance among the portfolio. It might assign huge weight on some certain stocks for this purpose. Since this portfolio is mainly built up with WMT stock. We decided to develop simulation with Monte Carlo method for this picked stock.
The Python code we used for Monte Carlo Simulation is attached below:
![]() |
Figure 12: Monte Carlo Simulation Python Code
|
![]() |
Figure 13: Comparison Between Real Price and Monte
Carlo Simulation Price
|
Summary
In summary, we used eight stocks to create a minimum variance portfolio and compared its performance with real stock market by back-testing in Quantopian. In addition, we used Monte Carlo method to simulate prices for a certain stock. As expected, the portfolio’s performance is much better than the benchmark. And that is the reason why we need to create portfolio. By creating a portfolio, we maximize the expected return and minimize the risk according to our risk tolerance.
Acknowledgment
We should definitely give our greatest gratitude towards Professor Do as he has provided us with such great lecture materials and genuine help along this semester. The skills we learned from this course would be beneficial in our further study and career. And we also have to thank every group member in this fantastic group – even though we only have two people. Working with each other was such a pleasant adventure. Every single effort matters in the team work. At last, we want to send our best wishes to all the classmates in this class. We would not be having this interesting class without any of you. Thanks, everyone.






