Friday, December 9, 2016

Minimum-Variance Portfolio and Monte Carlo Simulation on Selected Stocks’ Returns

Minimum-Variance Portfolio and Monte Carlo Simulation on Selected Stocks’ Returns

Fall 2016 MATH 5800-030 — Group 6 Final Project
By 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:

  1. Retrieve selected stocks' data from Yahoo Finance for a certain period.
  2. Establish a minimum-variance portfolio based on the stocks' returns.
  3. Run back-testing for our portfolio on Quantopian and compare it with the stock market.
  4. 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$$






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:
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.


Figure 2: Retrieve Data Code
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 3: Data Import in Python
In this code, we use ‘xlrd’ package for reading data from our Excel file and store it into a matrix. And the we use the ‘pandas’ package to help us to compute the daily return (‘pct_change’ function in code).

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
Then we will assign these random weights to our portfolio and compute their corresponding returns and risks.
Figure 5: Random Portfolios
After we obtained the random portfolios from the function above, we scatter it in a plot and see what happens.
Figure 6: Mean and Standard Deviation of Returns of Randomly Generated 
Portfolios
We can see that there are a lot of possible portfolios with different returns and risks. To be more clear, we will draw out the efficient frontier with a red-dotted line.
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
Finally, we obtain the minimum variance portfolio weights from this 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
And the resulting graph we got from the back-testing is shown in the below:
Figure 11: Back-testing Result
From the graph we can tell that our portfolio has a great performance during the period we selected. It has a total return rate as 21.8% with volatility of 0.23, which means our portfolio is pretty stable. And the Sharpe ratio is 1.82. For Sharpe ratio, it is a risk-adjusted measure of return that is often used to evaluate the performance of a portfolio. After calculating the excess return, it's divided by the standard deviation of the risky asset to get its Sharpe ratio. The idea of the ratio is to see how much additional return you are receiving for the additional volatility of holding the risky asset over a risk-free asset - the higher the better. Besides, the total returns curve is always above the Benchmark returns curve.

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
We plotted out the graph of real stock prices and the simulated prices for comparison.
Figure 13: Comparison Between Real Price and Monte Carlo Simulation Price
From the graph, we can see that the trend of this two price chart are very similar. Although the fluctuation of Monte Carlo Simulation is less than the real price in this scenario, it does not mean that the Monte Carlo Simulation will produce a relatively stable result for us. What we seen in this graph is just one case for simulation, as we mentioned before, Monte Carlo Simulation we used in our project is based on random walk. So the results obtained by Monte Carlo Simulation is various. Here we just present one case for comparison.

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.