Monte Carlo Simulation of a Stock Portfolio with Python
By QuantPy
Summary
## Key takeaways - **Monte Carlo: Repeated Random Sampling**: The Monte Carlo method is a computational simulation that relies on repeated random sampling to obtain numerical results. [00:22], [00:44] - **Handles Non-Deterministic Problems**: In solving problems with complex random variables and non-normal multivariate distributions, there may not be a deterministic solution, so we rely on Monte Carlo simulation to converge on an exact solution by increasing simulations. [00:55], [01:47] - **Fetch ASX Data with Yahoo**: Use pandas_datareader with 'yahoo' to get closing prices for ASX stocks like TLS.AX, NAB.AX, append '.AX' via list comprehension, compute returns with pct_change, then derive mean returns and covariance matrix over 300 days. [02:37], [05:46] - **Random Weights Sum to One**: Generate random weights with np.random.rand(length of stocks), then normalize by dividing by their sum to ensure they equal one. [07:26], [08:20] - **Cholesky for Correlated Returns**: Daily returns = mean returns + Cholesky lower triangle of covariance × uncorrelated normal samples; this correlates samples via covariance under multivariate normal assumption. [11:36], [13:46] - **Cumulative Portfolio Paths**: For each simulation, compute daily portfolio returns as weights dot daily returns, take cumulative product, multiply by initial value like 10,000, plot over 100 days to visualize paths. [15:04], [16:52]
Topics Covered
- Monte Carlo Beats Deterministic Math
- Covariance Matrix Drives Realistic Correlations
- Cholesky Turns Covariance into Daily Returns
- Covariance Timeframe Makes or Breaks Sims
Full Transcript
welcome back youtube to the asx portfolio channel today we're going to be talking about monte carlo simulation so as you can see in the top right screen this is what we're going to be doing today we're going to be making
a simulation of portfolio out to 100 days and we're going to track that over time so we're going to be implementing in python i hope you guys really enjoy
so what is monte carlo simulation and why would you want to do it so the monte carlo method is most generally described as a computational simulation that relies on repeated random sampling to
obtain numerical results so that's a bit of a mouthful let's try and understand why we'd want to do this first so in solving problems we often rely on random variables
that have some kind of underlying distribution now depending on the problem that we're trying to solve and the random variable and their distributions there may or may
not be a deterministic solution so what i mean by this in other words if you were to place the same input into our deterministic algorithm you would always get the same
output however in practice as we start introducing more complex random variables to our models with different underlying distributions this becomes a challenge
working with the non-normal multivariate distributions and working out an exact mathematical solution so instead in practice we rely on
a method monte carlo simulation as we increase the number of simulations and therefore how many samples we're taking from these underlying distributions we hope to try
and converge on an exact solution so hopefully you guys enjoy it let's jump into the code so jumping into the code uh first thing we're going to do is import our
dependencies so first thing let's import pandas so we're going to need to import numpy we're going to need matplotlib
to graph stuff dot pi plot as plt um we're going to import date time as dt and the other thing we're going to need
to import is pandas data reader okay so from pandas data reader we're going to import data
as pdr now that is a module where we can get yahoo specific data so just pip install that and we should be good to go
so let's import data we're going to define a function called get data and that's going to be stocks
start date and end date so what we need to return is a covariance matrix and we need to return the mean returns for whatever stocks we put in there for a certain date range
so first thing we're going to do is go stock data pdr dot get
data and we're going to choose yahoo so that takes in stocks a start date
and an end date so um the next thing we're going to define is we we get a data frame back and it has a whole bunch of information bid ask close prices we're going to only
choose the column that says close prices for that information we're only interested in the daily changes so the returns of this matrix we're
going to get the stock data and use the inbuilt pandas function percent change to get the daily changes
we're going to use this returns function to compute the mean returns and the covariance matrix
so mean and return stop mean and we're going to go covariance matrix
which is returns dot co so let's return the mean returns and the covariance matrix
great so let's define our stocks so stock list [Music] we can just enter a whole bunch of asx
random stock quotes that we know telstra nab um westpac
west park and let's go santos so because i know the format of the yahoo data list we're going to use list comprehension
to add so stock for stock in stock list we're going to add the string dot ax because yahoo requires
dot ax at the end of all australian stocks when we're pulling it in that data frame so we also need to find
a start date which is going to be well let's define the end date first which is going to be dt dot date time now
dot now and then the start date is going to be a shift from that end date so dt dot
time delta and let's call that days 300 and that time range that we're specifying there is very important for how we're computing that covariance matrix because it's the covariance
matrix and that makes the world a difference uh when we're taking into this monte carlo simulation so the parameters derived there are very important let's
test this function so mean returns covariance matrix equals get data stocks
start date end date true so let's print mean returns and let's see if that works so python
activate base python um mc [Music] excellent and you can see that we've returned our mean
mean returns there so now that we have our data let's define weights for the portfolio so weights for the portfolio let's just define them randomly
dot random and the length is going to be that mean returns column there i think i'm just able to do
that um so random is going to get a number between 0 and one i believe zero and one so let's we need all of
those to sum up equally to one so mp.sum
so we just need to normalize by the sum of all those weights to get the weights matrix equal to one so let's just print weights to see that
we're doing the right thing there so once we have that now we'll be able to go into our monte carlo simulation methodology that looks good to me so note the size
it's a one by whatever our number of stocks is as an array let's jump into the monte carlo simulation monte carlo
method so first thing we're going to define is the number of simulations so mc sims let's call that 100 at the start
and we're also going to define the time range which is going to be 100 days time frame in days um we need to now before we go into our
big loop where we're going to say 4m in range 0 to mc sims you know we're going to we're going to
do do stuff going to do our monte carlo simulation mc loops we need to define some empty arrays that we're going to store
information and retrieve information from so one of these arrays is going to be mean returns um in the format of the number of days so uh for that we're going to call this
mean m mean matrix and we're going to use np full now mp4 takes a shape and it takes what we're going to fill it
with so the fill value so the fill value as you've probably guessed is going to be the mean returns what we need to do is put this in the shape of our variables here so
the t so it needs to take into consideration how many stocks we've got and the number of days so because of the way this this field value
works we've got to define it by number of days and then secondly the length let's use that weights vector to define how many
variables how many stocks are in the list so it fills it fills this based on the mean returns later on you're going to see that we're actually going to take the transpose
of this matrix in order to do the computation below so just take that for granted for now the other array is the array that we're going to store all this information in so we're going to call
that the portfolio sims matrix and that's just going to be np full
shape with fill value and we're going to go 0.0 so that there's floats floats can be added if we went 0 then only integer values can be added to
this simulation and um yeah we need floats so the shape value is going to be some factor of the number of sims and
time and in this example we're using time first and mc sims for the dimensions so just an empty array with a number of
sims and time frames so as we run through this iteration we need to use um the formula for to be able to work out
daily returns now i'm just going to check the formula up on the screen here you can see daily returns is the mean plus by [Music]
the lower lower triangle from a koleski decomposition and this represents the covariance matrix so you can look up cholesky decomposition to find that lower triangle
it's just it's just a way of representing it in this form um we're going to use it because uh it's very simple for monte carlo analysis with stocks when we assume that
we have a multivariate normal distribution um we can use the koleski decomposition to easily represent the daily returns by that formula so we're taking a whole bunch of
uncorrelated sample data that we sample from the normal distribution and we're we're correlating them with the covariance matrix through the use of this
lower triangle l so let's let's just jump into it so we're going to sample a whole bunch of uncorrelated variables mp.random dot normal
variables mp.random dot normal distribution and the size of this is going to be important and that's going to
affect the number that we have there we're going to want this in t with
the number of stocks so length weights excellent so that'll give us um t times the number of stocks that we have
uncorrelated random variables from the normal distribution now we're going to work out what that lower triangle is np
linear algebra module from numpy and colossky and we're going to give it the covariance matrix so very simple it's just going to work out
what the lower triangle is for a cholesky decomposition so as we have in the formula there we're going to just say the mean
the daily returns are the mean returns there so mean m matrix um for all those values of t
plus the inner product because we have a variable number of of lengths here so the inner product so the dot product between
all these different stock stocks in the portfolio and we're going to have the lower with said so let's make sure the dimensionality
lines up here this is the number of stocks by the number of stocks and this shape is t with weights
with the number of stocks so that should work out there and the mean value is it was t by the number of stocks but it we transformed it to be the number of
stocks by t and the reason we didn't just switch that beforehand was because the feel value associates with this second quantity here
okay so let's take it that that that works what we need to do is now record those portfolio daily returns and accumulate them
across days and then of course we want to save down this information per simulation so for all the days t for the specific simulation m we want to
take the cumulative the cumulative prod so returns the cumulative product of all the elements in this series so we're going to take
the inner product again of the weights matrix and the daily returns
and for the daily returns i think that's going to have to be the transpose yeah so here we're just evaluating what the portfolio is
for each day and we're taking the accumulated product of daily returns so that we don't have daily returns going out for the entire dime series but we have the cumulative effect of those daily
changes um what we're going to do is add a initial portfolio value initial portfolio let's say we started with ten
thousand dollars um we're going to add one because we have daily changes here so let's add one and start off at the
initial portfolio value so let's plot portfolio sims let's just check that runs first and we'll add some things to this
graph a y label which we call portfolio value so we didn't get any error messages which is good but the plot didn't show
up because i didn't say plt.show so
we've got days on the x-axis and the title is going to be mc simulation of a portfolio
a stock portfolio plt dot show so now if we run that we should get our nice image of our returns from initial portfolio ten
thousand dollars so as you can see we've now uh got our image here of um ten thousand dollars all our simulated variables
of correlated um correlated assets between them as defined by our covariance matrix so again the the covariance matrix and the time period
that we def we parameterize that over is extremely important um so you know you can play around now with the the number of simulations that you take
here the time frame and of course the components of stocks that you have and the time range
so please enjoy using this using this monte carlo simulation and i hope you get a lot of value out of it thank you very much for listening to asx portfolio and see you guys in the next one
don't forget to like and subscribe if you want to see more content like this cheers you
Loading video analysis...