#Select random weights and normalize to set the sum to 1 Simulation_res = np.zeros((4+len(stock)-1,num_iterations))
#Set the number of iterations to 10000 and define an array to hold the simulation results initially set to all zeros The number of rows in the array is equal to the number of iterations. That's why we use the 'len function' while defining the array. The number of columns will change with the number of stocks in the portfolio as we have to store the weights for all the stocks. The number of columns in the array is 7 to hold portfolio return, standard deviation, Sharpe Ratio and the weights of all stocks. We will define one result array initially containing all zeroes and will save the simulation results in this array. The array will hold the returns, standard deviation, Sharpe ratio and weights for each step in the iteration. Let us define an array to hold the result of each iteration. #Calculate mean returns and covariances of all four the stocks We will now calculate the mean return of all stocks and the covariance matrix. #Compute stock returns and print the returns in percentage format We will then convert these stock prices into returns and will save this under the name ‘ stock_ret’.
Import matplotlib.pyplot as plt #Fetch data from yahoo and save under DataFrame named 'data'ĭata = web.DataReader(stock,data_source="yahoo",start='',end='') #Arrange the data in ascending order Let’s start by importing relevant libraries and fetching the data for the stocks for Dec 2017. Out of these 10000 results for returns and corresponding standard deviation, we can then achieve portfolio optimization by identifying a portfolio that satisfies on any of the 3 conditions discussed above. For the purpose of this blog, we will restrict ourselves to 10000 such iterations. Higher the number of iterations, higher will be the accuracy of the optimization but at the cost of computation and time. The number of iterations depends on the error that the trader is willing to accept. We’ll then change the weights and assign some random values and repeat the above procedure. At every particular combination of these weights, we will compute the return and standard deviation of the portfolio and save it. One important point to keep in mind is that the sum of the weights should always sum up to 1. In this simulation, we will assign random weights to the stocks. This simulation is extensively used in portfolio optimization. Before moving on to the step-by-step process, let us quickly have a look at Monte Carlo Simulation. We will use python to demonstrate how portfolio optimization can be achieved. We will then run Monte Carlo Simulations on our portfolio to get the optimal weights for the stocks. We will compute the return and standard deviation of the portfolio as we did in the previous blog and record it. To start with, we’ll assign random weights to all four stocks, keeping the sum of the weights to be 1. Let us consider a portfolio consisting of four stocks in banking/financial services sector, namely: Bank of America (BAC), Goldman Sachs (GS), JP Morgan Chase & Co (JPM) and Morgan Stanley (MS). The formulae for converting daily returns and standard deviation to an annual basis are as shown (assuming 252 trading days in a year):Īnnual Return = Daily Return * 252 Annual Standard Deviation = Daily Standard Deviation * 252 However, in practice, we work with annual returns and standard deviation. To simplify our analysis in this blog, we will deal with daily returns and standard deviation and will consider only 1 month of stock data (Dec 2017).
#Monte carlo in excel average and standard deviation how to
In this blog, we will see how to do portfolio optimization by changing these weights. In the previous blog of this series, we saw how to compute the mean and the risk (or standard deviation) of a portfolio containing ' n' number of stocks, each stock ' i' having a weight of ' w i'.