Monte Carlo Introduction

The purpose of this tutorial is to demonstrate Monte Carlo Simulation in Matlab, R, and Python. We conduct our Monte Carlo study in the context of simulating daily returns for an investment portfolio.

For simplicity we will only consider three assets: Apple, Google, and Facebook. We will assume an Initial Investment of $100,000 and allocate our money evenly between the three stocks. In this case the portfolio weights \(w_i = 1/3\) for \(i = 1,2,3\).

Next, we assume that daily returns are distributed Multivariate Normal with mean vector \(\mu\) and covariance matrix \(\Sigma\). In other words, \[R_t \sim MVN(\mu, \Sigma)\] for \(t \in \{1,\dots,T\}\) where \(T\) is the final time horizon.

We will use the Cholesky Factorization in order to find Lower Triangular Matrix \(L\) such that \(LL' = \Sigma\). Then our returns can be generated by \[ R_t = \mu + LZ_t \] where \[Z_t \sim N(0,I)\] for \(t \in \{1,\dots,T\}\).

The returns will be simulated over a 30-day period, where our 30-day returns can be formulated as, \[\hat R_{30} = \prod_{t=1}^{30} (1+R_t)\]

Thus our portfolio returns for each Monte Carlo trial \(m\) become the inner product between the 30-day returns and our vector of portfolio weights \(w\), \[P_m = w \cdot \hat R_{30} \].

Dataset Summary

We use adjusted-close stock prices for Apple, Google, and Facebook from November 14th, 2017 - November 14th, 2018. Historical stock price data can be found on Yahoo Finance for these companies. Also here is the link to the data set for this tutorial ‘Stock Price Data’.

The first ten rows of data look like :

##         Date AAPL_Adj_Close GOOG_Adj_Close FB_Adj_Close
##  1: 11/15/17       166.5791        1020.91       177.95
##  2: 11/16/17       168.5693        1032.50       179.59
##  3: 11/17/17       167.6333        1019.09       179.00
##  4: 11/20/17       167.4658        1018.38       178.74
##  5: 11/21/17       170.5791        1034.49       181.86
##  6: 11/22/17       172.3721        1035.96       180.87
##  7: 11/24/17       172.3820        1040.61       182.78
##  8: 11/27/17       171.5150        1054.21       183.03
##  9: 11/28/17       170.5101        1047.41       182.42
## 10: 11/29/17       166.9732        1021.66       175.13

Languages

R

Firstly, we need to load the data

Then we extract the stock price and set initial values for Monte-Carlo parameters

Get the returns by stock price and set the investment weights

##           [,1]      [,2]      [,3]
## [1,] 0.3333333 0.3333333 0.3333333

Calculate the Covariance matrix and Mean value of Stock Returns

Use Monte-Carlo to simulate the 30-day Portfolio Returns

Visualising the result ( Simulated Portfolio Returns in 30 days)

Get some useful statistics through the results we get

## [1]  0.0009402469  0.0840585273 -0.0015423606
##       2.5%      97.5% 
## -0.1541318  0.1702711

Matlab

Load data and extract stock price

Set Monte_Carlo parameters

Calculate stock returns

Set portfolio weight

Calculate covariance matrix and mean of the stock returns

Then we use Monte-Carlo to simulate the portfolio returns in 30 days

Visualizing the result

Finally, we want to get some useful statistics:

Python

Load modules

Load data and extract stock price

Set Monte_Carlo parameters

Calculate stock returns

Set portfolio weight

Calculate covariance matrix and mean of the stock returns

Then we use Monte-Carlo to simulate the portfolio returns in 30 days

Visualizing the result

Finally, we want to get some useful statistics:

Results

For our particular example, the portfolio returns averaged over all monte carlo trials had an average close to 0. The reason the average is close to 0 is because Apple, Facebook, and Google have average returns close to 0 over the past year. Therefore, our simulated returns essentially had no drift. Also, assuming a normal distribution of the returns would not work well in practice since stock returns are typically fat-tailed and not normally distributed. However, based on our Monte Carlo Study, we do not suggest investing in this portfolio based on the low expected portfolio returns.

Note

Statistics we get using three different languages are slightly different, because in our simulation process, we have generated random numbers and these numbers cannot be exactly identical.

Reference

Yahoo Finance