Quant Finance with Python | Stock Market Modeling (easy) — Transcript
Full transcript
- 0:00This video is going to cover stochcastic
- 0:02modeling, specifically geometric brownie
- 0:04in motion. We're going to be taking
- 0:05these seemingly complex looking
- 0:07formulas, and we're going to be
- 0:08implementing them directly inside of
- 0:10Python. And even though this looks kind
- 0:11of complex, this entire function here is
- 0:14literally just six lines of code. And
- 0:16just to give you a little preview of
- 0:17what we're going to be building up to in
- 0:18this video, by the time you get to the
- 0:20end, we're actually going to be building
- 0:22this interactive plot completely from
- 0:24scratch. This means that we can change
- 0:26the input parameters on our model in
- 0:27real time such as let's say like
- 0:29lowering the volatility for example or
- 0:31let's say making the expecttor to turn
- 0:33negative. Of course they're going to be
- 0:34explaining all of this from scratch and
- 0:36this entire interactive plotting thing
- 0:37is literally just 10 lines of code. Now
- 0:40the cool thing is that I'm making this
- 0:41video accessible to everyone even
- 0:43complete beginners. This means that
- 0:45everything we need both the math the
- 0:47code and the finance I'm going to be
- 0:49explaining completely from scratch.
- 0:51You're not going to need any external
- 0:52prerequisite knowledge. So last thing,
- 0:55even if you don't know or don't care
- 0:57about stochcastic models or even about
- 0:59finance in general, I would still advise
- 1:01you stick around because we're actually
- 1:02going to be covering a lot of ground in
- 1:04this video, including just Python in
- 1:06general, as well as vectorization,
- 1:08obviously, numpy and pandas libraries,
- 1:10plotting with mapplot lib, um, as well
- 1:12as IPI widgets, which is how we're
- 1:13actually able to build these interactive
- 1:15UIs incredibly easily directly in a
- 1:17Python notebook. We don't need HTML or
- 1:20JavaScript or anything web- based. Um,
- 1:22and we're also going to be going over
- 1:23some time complexity stuff as well.
- 1:25We're actually going to be discussing
- 1:26how we can actually make our Python code
- 1:28more efficient over time. So, with all
- 1:30of that being said, let's get started.
- 1:33So, this is a stochcastic model,
- 1:35sometimes referred to as a stochcastic
- 1:37process. A stochastic model is just some
- 1:39type of model that involves some form of
- 1:41inherent randomness. In this video,
- 1:44we're going to be stochastically
- 1:45modeling stock market returns. So, I
- 1:48know that sounds confusing, but
- 1:49essentially all that means is that we're
- 1:51going to be generating random numbers,
- 1:53but they're not just going to be any
- 1:54random numbers. They're going to be
- 1:56random numbers that are a reasonable
- 1:58close approximation of actual stock
- 2:01market returns. Simply put, each one of
- 2:03these lines on this chart is trying to
- 2:05mimic a reasonable path that the actual
- 2:08stock market might take. Each one of
- 2:10these paths can be called a random walk.
- 2:12Now, as I said, the model that we're
- 2:14covering in this video specifically is
- 2:16called geometric brownie in motion. This
- 2:18is an example of something we can call a
- 2:20non-meanverting stochastic model, which
- 2:22we can use to model stock market
- 2:24returns. This is opposed to something we
- 2:26can call a mean reverting stochastic
- 2:28model like the CIR model, which we can
- 2:30actually use to model fixed income or
- 2:32bond prices. So, you might be asking,
- 2:34why do we want to randomly generate
- 2:36stock returns that mimic real world
- 2:38stock returns? Why not just pull actual
- 2:40real world historic data? And of course,
- 2:42that's a good question. And let's just
- 2:44take a step back here for a second to
- 2:46explain what we're actually doing. So,
- 2:48let's say that we want to design a
- 2:49portfolio strategy. This could be maybe
- 2:51like a dynamic hedging strategy or
- 2:53something, but it doesn't really matter
- 2:54what it is. Now, if you want to test the
- 2:56efficacy of a portfolio strategy in
- 2:59order to get useful information on its
- 3:00performance, we can do this in two
- 3:02different steps. First, we'll need to
- 3:05gather some data. This data could either
- 3:07be real world historic stock market data
- 3:09or of course it could just be randomly
- 3:11generated data that mimics real world
- 3:13data. Now second once we have our data
- 3:17now we can go ahead and do like a Monte
- 3:18Carlo simulation or really whatever else
- 3:21we want to do to actually test the
- 3:23portfolio strategy using the data. So in
- 3:25short you can think of the brownie in
- 3:27motion model as just being like a
- 3:29building block or a tool in our toolbox
- 3:31that will come in handy later. Now, of
- 3:33course, it might even be better to use
- 3:35historical data. Obviously, there's
- 3:36merits to both, and that's sort of like
- 3:38out of the scope of this video, at least
- 3:40for now. All you really need to know is
- 3:42that we're going to be building a model
- 3:43that can generate close approximations
- 3:45of actual stock market returns. Let's go
- 3:48ahead and get started. So, real quick
- 3:50before we get started, the video that
- 3:52you're currently watching is a part two
- 3:54in my Quant Finance series. About 2 and
- 3:561/2 years ago, I made Quant Finance 101,
- 3:59which is just a general foundational
- 4:01level introduction. and I said if there
- 4:03was interest I would turn it into a
- 4:04series and at first there wasn't much
- 4:06interest and then after 2 and 1/2 years
- 4:08it just blew up out of nowhere and
- 4:10everybody was you know telling me please
- 4:11continue the series so here we are in
- 4:14either case I just wanted to say that
- 4:15while there is no external prerequisite
- 4:18knowledge needed I will be assuming that
- 4:20past this point everyone did watch quant
- 4:22finance 101 which is the first video in
- 4:24the series that's just going to give you
- 4:26a basic foundation of numpy and pandas
- 4:28along with the basics of calculating
- 4:30risk and return so if you didn't watch
- 4:31this video. Go ahead and give it a watch
- 4:33and then we should be good to go. Now,
- 4:35recently I did make two additional
- 4:37finance videos, although these were not
- 4:39done in Python. These were just raw
- 4:41finance videos. So, these two are not
- 4:43needed to continue. Um, now a lot of
- 4:45people in the asset pricing video were
- 4:47asking me about factor regressions. Of
- 4:49course, we're going to be doing a
- 4:50dedicated video later on on factor
- 4:51regressions. Um, but I will talk a
- 4:53little more about that at the end of
- 4:55today's video. Now, before we actually
- 4:58implement these formulas inside of
- 4:59Python, I'm just going to be explaining
- 5:01what they are mathematically. Now, fair
- 5:03warning, while I have these formulas on
- 5:05the screen, this might be incredibly
- 5:07confusing. I'm going to just go more in
- 5:09depth than what's actually needed. Once
- 5:11we get to the Python part, it should be
- 5:12pretty straightforward. So, if you get
- 5:14lost and you don't know what I'm talking
- 5:15about, that's completely okay. So, this
- 5:18is the formula for geometric brownie in
- 5:20motion in continuous time. And all we
- 5:23need to worry about for now is that this
- 5:24whole complex looking formula is
- 5:26essentially just going to give us the
- 5:28percentage change or the percentage
- 5:30return of a stock or of a stock market
- 5:32index over some time step. And what
- 5:35we're going to do in Python is we're
- 5:37going to run this entire formula, you
- 5:38know, hundreds or thousands of times to
- 5:41get a percentage return for many
- 5:42different consecutive time steps. And
- 5:44then we're going to be geometrically
- 5:46linking, right? just compounding those
- 5:48returns over time in order to get, you
- 5:50know, a random walk over maybe like a
- 5:5210-year sample. Now, we don't need to
- 5:54worry too much about how this works in
- 5:55continuous time. We're actually going to
- 5:57be implementing it in discreet time, but
- 5:59just for those of you that are maybe
- 6:01like math majors or finance majors or
- 6:03have a background in calculus or
- 6:04anything like that, you're probably
- 6:06going to have some intuition um of how
- 6:08this works. Essentially, the time step
- 6:10here is dt. Um, and in this case, dt is
- 6:13just going to be an infantessimally tiny
- 6:15amount of time. So not one day, not one
- 6:18minute, not one second, not one nancond,
- 6:21right? Even smaller. It's just like an
- 6:23infinitely tiny amount of time. Um, and
- 6:25ST just means the price of the stock or
- 6:28the price of the stock market index at
- 6:30time t. And DST just means the change in
- 6:34dollar value of the stock between time t
- 6:37and time t + dt. So dst literally just
- 6:41means how much the price moves in that
- 6:43very amount of time. when we divide that
- 6:45by ST itself, the whole thing
- 6:47essentially just becomes a percentage
- 6:49return over this infantestimally tiny
- 6:51time step. That's just, you know, to
- 6:53give some intuition for the people that
- 6:55sort of might understand this a bit
- 6:56more. Um, in reality, we don't need to
- 6:58worry about that. What we're actually
- 7:00going to be doing in this video in
- 7:01Python is we're going to be implementing
- 7:03this in discretet time. So, if you watch
- 7:06that first Quant Finance video I made,
- 7:08this formula should look somewhat
- 7:10familiar to you. Um, this literally just
- 7:12means the percentage return of a stock
- 7:14in between time t and time dt. So it's
- 7:17the same as the continuous version. It's
- 7:19just that dt is now going to be some,
- 7:22you know, some time step that we can
- 7:23choose. So let me just go ahead and
- 7:25simplify this as much as I possibly can.
- 7:28So all of this is just a percentage
- 7:31return in one given time step in dt. Um,
- 7:34and the time step now can really be
- 7:36anything. It can be a day, a month, a
- 7:38year. Now, in this video in Python, it's
- 7:40going to be a parameter. So, you're
- 7:41actually free to pick DT. Um, but in
- 7:44this video, we're just going to be
- 7:45making it be one month. So, you can just
- 7:47think of this as being the percentage
- 7:49return of the stock market index over 1
- 7:52month is equal to all of this stuff. So,
- 7:55let me go ahead and write this out as
- 7:56simply as I can. So, the actual return
- 7:59over a one month time step, right? The
- 8:01data point of this random walk for this
- 8:03one month that we're going to be using
- 8:04is going to be equal to the expected
- 8:07return of the stock market index which
- 8:09we can call MO. MO just means the
- 8:11expected return or the average return on
- 8:14the market. And then we're going to be
- 8:16doing plus sigma.
- 8:19And sigma just means the volatility or
- 8:21the standard deviation. So I know this
- 8:24might be confusing but in short and
- 8:26we're going to be generating hundreds or
- 8:27thousands of per month returns but the
- 8:30actual return in any given month is
- 8:33equal to the expected return plus the
- 8:35volatility around the expected return.
- 8:38This is just a simple decomposition. Now
- 8:40in general how things work in finance is
- 8:43any sort of figure that's just sort of
- 8:44quoted unless otherwise stated tends to
- 8:47be annual. Right? So in this case, we're
- 8:50looking at the return per a given time
- 8:52step. The MO is going to be we're going
- 8:55to see later. It's going to make more
- 8:56sense later, but the average expected
- 8:58return is annual. So we're essentially
- 9:00just doing MO, but we need to scale it
- 9:03down to DT. So we're taking the annual
- 9:05expected return and then just scaling it
- 9:08down to say the expected return just for
- 9:10a single month. And then same as the
- 9:11sigma, we're going to be taking, you
- 9:13know, the annual sigma, right? The
- 9:15annual standard deviation or volatility
- 9:16on a stock market index. And then of
- 9:18course when we do this in Python we're
- 9:20going to say let's just convert it from
- 9:22annual to just you know per one month or
- 9:24per one time step. That's why these
- 9:26numbers involve a DT in them. Now this
- 9:29is just going to be like a little
- 9:30digression just for those that are
- 9:32interested. This character here is sigma
- 9:34but we're not actually seeing a moo. The
- 9:37reason why is we're actually further
- 9:39decomposing moo. And I'm going to go to
- 9:40the next slide. And these three
- 9:42characters together are moo. We're just
- 9:44further decomposing them just for those
- 9:46that are interested. When we actually
- 9:48implement this in Python, there's just
- 9:50going to be a single character called
- 9:51moo. Um, but if you think back to my
- 9:53video on asset pricing, right? This is
- 9:55just for the just for those that are
- 9:57interested. But if you think back to my
- 9:58video on asset pricing, how can we
- 10:00further decompose the return on let's
- 10:03say the stock market? Well, I said that
- 10:05we can decompose the return on the stock
- 10:07market into the risk-free rate plus the
- 10:10risk premium. And the same applies for
- 10:12the value premium, the profitability
- 10:14premium. In this we're just thinking
- 10:15about like the global total stock
- 10:16market. Um, but r here is equal to the
- 10:19risk-free rate. And then we're going to
- 10:21be adding the risk-free rate to the risk
- 10:24premium. So these two figures sigma
- 10:27lambda being multiplied together is
- 10:29equal to the risk premium. And how we
- 10:31sort of do that further decomposition is
- 10:34quite interesting. Sigma as we know is
- 10:36just the volatility and then lambda is
- 10:38the sharp ratio. So, I know in my first
- 10:41finance video, uh my first quant finance
- 10:43video, we touched on the raw sharp
- 10:45ratio. Now, the raw sharp ratio and the
- 10:47regular sharp ratio is basically just a
- 10:50risk adjusted return metric. The raw
- 10:53sharp ratio just means how much return
- 10:56you get per unit of risk. Um and then
- 10:59the that's the raw sharp ratio. And then
- 11:01the actual sharp ratio means how much
- 11:04excess return you get per unit of risk
- 11:07where excess return is just the total
- 11:09return above the risk-free rate. It's
- 11:13essentially like the risk premium,
- 11:14right? I know that sounds confusing, but
- 11:17you know, if you think about it in
- 11:18simple terms, if you take how much
- 11:20return, if you take how much excess
- 11:22return you get per unit of risk and
- 11:25literally multiply it by the units of
- 11:28risk, you're just left with the excess
- 11:30return. The excess return is literally
- 11:33the risk premium. And then if you do the
- 11:35decomposition of risk-free rate plus
- 11:38risk premium, that's just the MOO, which
- 11:40is just the total absolute expected
- 11:43return. That's just a further
- 11:45decomposition for those that are
- 11:46interested. Again, in this video, we're
- 11:48going to further simplify it. We're just
- 11:50going to have MO * DT. Okay, so we're
- 11:53almost done here. Just to recap, the
- 11:56percentage return of the stock market
- 11:58index for one given time step, let's say
- 12:00one month, is equal to the expected
- 12:03return on the stock market index scaled
- 12:06down to just per time step. And then the
- 12:08sigma, which is the volatility of the
- 12:10stock market index, also scaled down
- 12:12into one month or one time step. You
- 12:14don't need to worry about why this is in
- 12:15a square root. Again, if you watch my
- 12:17previous video, you're going to
- 12:18understand why, but it's literally just
- 12:20the return is equal to the expected
- 12:22return plus the volatility around that
- 12:24return over the given time step, which
- 12:26is 1 month. Now, so far all of this is
- 12:30just like a random sort of
- 12:31decomposition. What actually makes this
- 12:34a Brownie motion stochastic model? We
- 12:36now need to introduce the randomness,
- 12:38right? So, it's this variable here
- 12:40called Z of T. So it's all of this and
- 12:43then we're going to multiply it by
- 12:44something that we call z of t. This is x
- 12:47i the moo sigma z. These are the actual
- 12:49variable names we're going to use in
- 12:51python. But all of this is going to be
- 12:53multiplied by z of t. And z of t is what
- 12:56actually matters. This is the actual
- 12:58brownie and motion term. So all z of t
- 13:01is is just going to be a random
- 13:03variable, right? Not just any random
- 13:05variable. More specifically, it's going
- 13:07to be a random normal. specifically a
- 13:10random standard normal. We can also just
- 13:13call it a random Gaussian variable. So,
- 13:16think back to your high school stats
- 13:17class. Let me go ahead and pull this up
- 13:18actually. So, I know everyone watching
- 13:21obviously isn't going to be like a math
- 13:23major or anything, right? I just want to
- 13:24make this video as accessible as
- 13:26possible because it's really quite
- 13:28simple. I know it sounds confusing, but
- 13:30it's really quite simple. So, just to
- 13:32give you guys some intuition as to what
- 13:33I'm talking about, I literally just
- 13:35search standard uh standard normal
- 13:37distribution on Google. And let's just
- 13:39take a look at this image, right? If you
- 13:40think back to like high school stats
- 13:42class, this is just a bell curve, right?
- 13:44This is a standard normal distribution.
- 13:46Um, if you recall, a standard normal
- 13:48distribution will have an average of
- 13:51zero, right? It will have a mean of zero
- 13:53and it will have a unit variance, which
- 13:55just means that like the standard
- 13:56deviation is equal to one, right? We're
- 13:58just taking a look at this bell curve
- 13:59and all z of t is we're just going to
- 14:02take this bell curve and we're going to
- 14:04pick a random number off this bell
- 14:06curve. Right? the number. It could be
- 14:07zero, it could be one, it could be
- 14:10negative 0.5. We're just going to yink a
- 14:12random number off of this bell curve and
- 14:14that's it. That's all z of t is. That's
- 14:16all a random Gaussian is. We're just
- 14:18going to yink a random number off this
- 14:19bell curve and plug it into the formula
- 14:22and that's it. And then every single
- 14:24time step, every single month we run the
- 14:26formula, we are going to be yoinking a
- 14:28different random z of t off of this bell
- 14:31curve. That's really all we're doing.
- 14:33So, let me go ahead and just explain all
- 14:35of this in complete plain English. This
- 14:37is what we're actually going to be doing
- 14:38in Python. We're going to go onto that
- 14:40standard normal bell curve and we're
- 14:42going to yink off this completely random
- 14:44standard normal number. We're going to
- 14:46go ahead and we're going to apply a
- 14:48standard deviation to it. And then we're
- 14:50going to go ahead and apply a mean to
- 14:52it. And then we're done. And that's just
- 14:54the return over a given month. And then
- 14:56we're just going to keep on yinking
- 14:58random numbers, applying volatility,
- 15:00applying a mean. And we're going to do
- 15:02this hundreds and thousands of times.
- 15:04And that's just how we get our returns.
- 15:06So one of the important things to note
- 15:08is that every single time you run this
- 15:10formula and get a one-mon return, it's
- 15:12going to be an independent increment,
- 15:15right? So at every single time step,
- 15:17there's going to be like an equal
- 15:18probability that a return is like above
- 15:20the mean or below the mean. Um so what
- 15:22that means is that there's no
- 15:24autocorrelation, right? Which means like
- 15:26there's no serial correlation, right?
- 15:28There's no momentum, there's no
- 15:29reversals. It's this completely random
- 15:32walk where every single time step,
- 15:34right, 50% chance it's above the mean,
- 15:3550% uh 50% chance it's below the mean.
- 15:38Now, efficient markets people, if you
- 15:40think back to my last couple of videos,
- 15:42right, efficient markets people are
- 15:44going to say this actually is how
- 15:46markets should function, right? There
- 15:48shouldn't be any sort of momentum in
- 15:50markets, right? There shouldn't be
- 15:51serial correlation. And that's actually
- 15:53sort of true, right? And this is why
- 15:55geometric uh geometric branding motion
- 15:58is considered to be a reasonable
- 16:00approximation of the stock market. Now
- 16:02of course if you go ahead and you look
- 16:04closely at the data you are going to
- 16:06find some serial correlation right. Um
- 16:09now in addition to this since these are
- 16:11random normals right random Gaussian
- 16:13variables there's no skewess and there's
- 16:16no curtosis. So that means that we're
- 16:18also making the assumption that asset
- 16:21returns are normally distributed. right
- 16:24now in real life you know that's sort of
- 16:26true but in real life there might be
- 16:28some skewess there might be some
- 16:30kurtosis and there might be some
- 16:32autocorrelation and this model is not
- 16:34going to capture any of that so as I
- 16:37said it is widely recognized as a
- 16:39reasonable model but it's definitely not
- 16:42a perfect model now of course we could
- 16:45start to build a better more realistic
- 16:47model that captures some of these things
- 16:50so this is going to be out of scope of
- 16:52this video you could skip ahead. This is
- 16:54just going to be for those of you like
- 16:55me that are interested, right? So, let
- 16:57me tell you this, right? So, when we
- 16:59actually implement this in Python, the
- 17:00MO and the sigma, right? Um the
- 17:02volatility and the expected return,
- 17:04these are just going to be fixed
- 17:05parameters, right? We're just going to
- 17:07hand it, let's say, like a 7% expected
- 17:09return, maybe like a 15% sigma, and
- 17:12we're just going to call it a day. These
- 17:13are just going to be hard-coded numbers.
- 17:15But think about this. In reality,
- 17:18everything is time varying, right? It's
- 17:20not in this model. The only source of
- 17:22randomness is going to be Z. But in
- 17:24reality, everything is time varying. So
- 17:27we could build a more realistic more
- 17:29general model that uses something that
- 17:31we can call a stochcastic opportunity
- 17:33set where the inputs into the stochastic
- 17:36model are in and of themselves also a
- 17:39stochcastic process. So that means that
- 17:41we would have two layers of randomness.
- 17:44So again, we're not going to be doing
- 17:46this more advanced version in this
- 17:47video. But why do we assume that MOU and
- 17:50sigma are stationary over time? In fact,
- 17:52let me go back to my last slide. So when
- 17:54we decompose the expected return,
- 17:56obviously R is the risk-free rate. If
- 17:58you think back to my previous videos in
- 18:00finance, we generally consider the
- 18:01risk-free rate to be 30-day Treasury
- 18:03bills. And the interest rate on these
- 18:0530-day United States Treasury bills
- 18:07obviously changes over time. It's not
- 18:09stationary, right? um in the risk-free
- 18:11rate changing over time that's actually
- 18:14directly observable. But the really
- 18:16interesting thing is that we have really
- 18:18strong empirical reasons to believe that
- 18:21the sigma which is the volatility and
- 18:23the lambda which is the sharp ratio or
- 18:25the actual risk premium is actually also
- 18:28time varying. Right? So in the future
- 18:30maybe in future videos you know we can
- 18:32go ahead and we can use like various
- 18:33statistical techniques to actually
- 18:35estimate risk premiums going forward and
- 18:38expected returns going forward and also
- 18:40volatilities even covariances right now
- 18:43you know we can talk about like the arch
- 18:45and garsh models right obviously this is
- 18:47beyond the scope of this video it's just
- 18:49really interesting to note in fact in my
- 18:51last video that I made on asset pricing
- 18:53um I actually uh answered a commenter in
- 18:55my comments and I actually brought up
- 18:56something called the cape the cap PE,
- 18:59which is the cyclically adjusted price
- 19:01earnings. Um, now obviously like
- 19:03expected return forecasting of the ERP
- 19:06and of these factors, that's like pretty
- 19:08complex and that's well beyond the scope
- 19:10of this video for today. Um, but I was
- 19:12saying that like one of the only
- 19:14reliable metrics we have to actually
- 19:17forecast expected returns, at least in
- 19:19the stock market, is with the cape,
- 19:21which is the cyclically adjusted price
- 19:22earnings. um that's essentially just a
- 19:24PE like a price earnings ratio but that
- 19:27actually goes back 10 years instead of
- 19:29just like one year or whatnot and it's
- 19:31also inflation adjusted and that has a
- 19:33bunch of added benefits of making that
- 19:35signal of valuation more clear. It sort
- 19:38of you know goes through and smooths out
- 19:40a lot of effects that come up because of
- 19:42accounting standards and the business
- 19:43cycle and all this stuff. Um you know
- 19:45and then obviously sort of people that
- 19:46study finance understand that you know
- 19:48expected returns are inversely
- 19:50correlated with valuations. So if you
- 19:52have a clear signal evaluations, right?
- 19:54And you know, again, we can sort of talk
- 19:56about all this stuff later. Uh but the
- 19:58cape was obviously developed by somebody
- 19:59called Robert Schiller. Obviously, you
- 20:01can go read his books and you know, I
- 20:03was talking in my last video too about
- 20:05like Eugene FMA and Ken French. I think
- 20:07I brought up uh Cliff Asess right now.
- 20:09All these researchers, at least for me,
- 20:11you know, I learn you sort of almost
- 20:13like connect with these researchers a
- 20:15little bit. Uh personally, I know way
- 20:17more researchers within finance than I
- 20:19do like on the computer science side. uh
- 20:21you know there's like Robert not John
- 20:23Cochran Robert Novie Marks I think I
- 20:25talked about Andrew Ang wrote a whole
- 20:27book on asset pricing I actually
- 20:28recommended that to some commenters and
- 20:30then obviously you know there's like
- 20:31Robert Merin Jack Bogle Marowitz Scholes
- 20:34uh and then Bill Sharp is the guy that
- 20:35actually invented this lambda right that
- 20:37actually invented the sharp ratio um
- 20:39either way right so the moral of the
- 20:40story is that there are ways to generate
- 20:43more reasonable you know more realistic
- 20:45stochcastic scenarios um but in this
- 20:47video we're just going to be uh sticking
- 20:49with this sort of you know simple base
- 20:51version of geometric brownie motion and
- 20:53let's go ahead and implement this in
- 20:55Python. Okay, so let's go ahead and
- 20:57essentially just take this formula and
- 20:59implement it best we can inside of
- 21:01Python. So just to keep the scope of
- 21:04this video open uh to beginners and to
- 21:06everyone, I'm just going to explain
- 21:07everything from scratch. So what I'm
- 21:09using right now is just called an
- 21:10interactive Python notebook. Ideally,
- 21:12you'll go ahead and you know follow
- 21:14along with me writing your own code as
- 21:15you watch the video. Um, so if you've
- 21:17never used one of these before, you
- 21:18know, you can go ahead and use like a
- 21:20Jupyter notebook or whatever you need to
- 21:21use. In this case, I'm just using one of
- 21:23these Python notebooks directly inside
- 21:25of VS Code. This is just what I prefer.
- 21:27Um, you know, you can go ahead and
- 21:29literally just make a file and just give
- 21:31it
- 21:32for interactive Python notebook. That's
- 21:34just the file extension. And we just
- 21:36have this concept of cells. We just
- 21:37write regular Python code, right? Let's
- 21:39just say X= 10 in these cells, right?
- 21:42You pick a kernel, whatever. And it
- 21:44basically just lets us almost in a way
- 21:46pry into like a Python runtime in real
- 21:49time. Right? So if you never saw this
- 21:50before, if x is equal to 10, we can come
- 21:53down here to a new cell and we can for
- 21:55example, you know, print x and now we
- 21:57get 10. And if we go ahead and, you
- 21:59know, uh, x +als 5 in a different cell.
- 22:02Next time we reference x, it's going to
- 22:04print 15, right? It almost like saves
- 22:06state. We can go ahead and just execute
- 22:08Python right in the runtime in real
- 22:10time. This is used broadly and widely
- 22:13not just within finance but throughout
- 22:15really just all you know numerical
- 22:17mathematical statistical applications
- 22:19within Python. So this is also going to
- 22:20be good practice. Um last thing to note
- 22:23you're not going to see me in this video
- 22:24using print statements a whole lot.
- 22:26Essentially the last variable that's
- 22:28sort of like referenced, right? The last
- 22:30variable that's like left in a cell by
- 22:32default will be printed, right? Right.
- 22:33So if you remove the print statement and
- 22:35just execute a cell, um the last thing
- 22:37in the cell will just be printed by
- 22:38default um just to avoid confusion.
- 22:41Okay, so let's get started. So let's go
- 22:43ahead and all of this geometric brownie
- 22:45in motion stuff we're just going to make
- 22:46a function for that's going to handle
- 22:48everything. So let's go ahead and define
- 22:49a function and let's just call it
- 22:51geometric brownie in motion GBM. Um and
- 22:54let's think about the input parameters,
- 22:56right? So we just want to implement this
- 22:58formula inside of this function. So
- 23:00obviously we're going to need moo and
- 23:02obviously we're going to need sigma and
- 23:03we're going to need dt as well. But if
- 23:05we think more generally this whole
- 23:06formula just gives us a return for one
- 23:09given time step and we want to calculate
- 23:12you know hundreds or thousands of these
- 23:14time steps right so there's many
- 23:16different ways to do this. I'm just
- 23:17going to go over what I think is most
- 23:19easy and most intuitive right um so
- 23:21let's go ahead and have a parameter that
- 23:23we're going to call number of years.
- 23:25Let's just default it to 10. So that
- 23:27just means that if you want to run this
- 23:29random walk over, you know, a 10-year
- 23:32sample or 20-year sample, whatever,
- 23:34right? Literally just the length of the
- 23:35sample is going to be given by number of
- 23:37years, that's pretty straightforward.
- 23:38Um, now obviously we're going to need
- 23:40moo and sigma. As I said, these are just
- 23:43going to be hard-coded stationary
- 23:45parameters. How we actually calculate
- 23:47expected return is completely out of the
- 23:49scope of this video. Let's just pick
- 23:51something halfway reasonable. So, as I
- 23:53said, let's just use a 7% annualized
- 23:56expected return on the stock market.
- 23:58We're just going to write that in
- 23:59decimal form as 0.07. And then same
- 24:01thing for the sigma. Let's just use
- 24:030.15, which just means, you know, like
- 24:0615% standard deviation on the market.
- 24:08Um, and if we look at this formula,
- 24:10we're going to need one more thing,
- 24:11which is DT. Now, I could go ahead and
- 24:14like actually do DT. Um, but in reality,
- 24:17DT is going to be like 1 over 12. So to
- 24:20make things more simple, um let's go
- 24:21ahead and actually make this parameter
- 24:24called steps per year, which is just the
- 24:26granularity. We actually saw this in the
- 24:28last video. And we're going to default
- 24:30this to 12, which just means the monthly
- 24:32granularity, right? If there's 12 time
- 24:33steps in a year, we're obviously dealing
- 24:36with the monthly granularity. And then
- 24:37dt is just going to be equal to 1 / 12.
- 24:40So for now, this is all we need. Let's
- 24:42actually start writing the function. So
- 24:44as I said, we are going to need dt
- 24:46explicitly. um that's just going to be
- 24:48equal to one over the steps per year,
- 24:51right? Since, you know, one given time
- 24:52step is just going to be 1 over 12. Um
- 24:55and now let's think about what else we
- 24:57need. The last thing we're going to need
- 24:58is we're going to need something that
- 25:00we're going to call number of steps.
- 25:02This just means how many time steps are
- 25:04there over the entire sample, right? So
- 25:07if you think about it, if you want to
- 25:09run the sample for some number of years,
- 25:12let's say 10 years, and there's 12
- 25:14different time steps, right, per year,
- 25:16if you literally just go ahead and
- 25:18multiply the number of years by the
- 25:19steps per year, this is just the total
- 25:21number of steps throughout the entire
- 25:23sample. Okay, not bad. Um, now let's go
- 25:26ahead and continue. Let's now go ahead
- 25:29and actually implement Z of T. So let's
- 25:32call this Z. And as we know, Z is just
- 25:35going to be equal to this random
- 25:37standard normal, right? This random
- 25:38Gaussian variable. So, you know, there's
- 25:40a few different ways to do it. Um, now
- 25:42how I'm going to do it is I'm just going
- 25:44to use a numpy library. So, we are going
- 25:46to need, you know, a couple different
- 25:47dependencies. I think there's like four
- 25:49different dependencies in today's video
- 25:51in total. Um, so what we're going to do
- 25:53is let's actually just make a cell here
- 25:55and we're going to go ahead and we're
- 25:56going to import numpy. Now, most of the
- 25:58time when we import numpy, we just say
- 26:00as um np just to abbreviate. Now, you're
- 26:03going to need to go ahead and pip
- 26:05install numpy if you haven't already. Um
- 26:08now, an easy way to do it directly in a
- 26:09notebook just to make sure it's like for
- 26:11the right environment is we can actually
- 26:13just do a percent time um a percent
- 26:15sign. This is like a Python magic
- 26:17command. I actually previously in my
- 26:19channel would do like you know percent
- 26:21time it right to like you know whatever.
- 26:23Um, and you can actually do percent pip
- 26:26install numpy directly in a cell instead
- 26:28of opening a terminal. Obviously, it's
- 26:30already installed for me. Um, so you
- 26:32know, it will just say already
- 26:33installed, but for you guys, you can do
- 26:35this and this will work. And then you
- 26:36just go ahead and do import numpy as MP.
- 26:39Um, and then you should be good. Um, and
- 26:41of course, don't forget to actually
- 26:43execute the cell. Okay, so the numpy
- 26:45function that we're actually going to be
- 26:46using is going to be mprandom.normal.
- 26:50And this is just a function that will
- 26:52literally give us a random normal. As we
- 26:54can see, 1.43. Every single time we run
- 26:56this function, we're literally just
- 26:58going to get a random normal number.
- 27:00Now, you wouldn't know this unless you
- 27:01actually went and read the
- 27:02documentation. Now, obviously, right
- 27:04now, you know, we have like large
- 27:06language models of stuff. So, you could
- 27:07also just ask claude or, you know,
- 27:09whatever you need to ask. But, you know,
- 27:11obviously when I started doing all this,
- 27:13right, this is when we still went ahead
- 27:14and looked up documentation all the
- 27:15time. You know, as everything is in
- 27:18life, the less you use a skill, the more
- 27:19monatrophy, right? Um, I'm not going to
- 27:21bore you obviously with too much
- 27:22documentation. But if you look at the
- 27:23documentation for this function, we're
- 27:25going to see a couple different
- 27:25parameters. And, you know, you wouldn't
- 27:27know unless I told you this. Um, but in
- 27:29this case, we're going to be worried
- 27:29about the size parameter. We can go
- 27:32ahead and we can hand the size parameter
- 27:34to this function. And let's just say we
- 27:36set this number to five. Now, when we
- 27:38run this function, we're going to get
- 27:39back an array that contains five
- 27:41different random uh five different
- 27:43random normals, right? We can make this
- 27:4510. We're going to get 10 different
- 27:46random normals. So what we're going to
- 27:47do in this case is we're going to get uh
- 27:50we're going to set z equal to
- 27:52numpy.random.normal.
- 27:55And think about what we're going to set
- 27:56the size to, right? How many random
- 27:58normals do we want for this whole thing.
- 28:01Now we could open up an explicit for
- 28:03loop and just generate one and we could
- 28:06sort of, you know, do this whole
- 28:07function in a for loop. I'm going to
- 28:08talk more about time complexity later.
- 28:10It turns out that with broadcasting and
- 28:12vectorization, we can almost do all of
- 28:14this in one step. I'll explain that in
- 28:16just a second. So over the entire
- 28:18sample, right, how many Z do we need?
- 28:21Well, we need one Z per time step. So
- 28:24we're going to need n steps worth of Z,
- 28:28right? Um so in this case, if there's 10
- 28:30years um and there's 12 steps per year,
- 28:32we're going to need 120 different size,
- 28:34right? If you set this to 120, we're
- 28:36literally going to get 120 different
- 28:38random normals. We're essentially just
- 28:39getting all of the Z's we need um for
- 28:42the entire function. And now what we can
- 28:44do is we can go ahead and we can
- 28:46actually implement the formula. So you
- 28:48think back to my first quant finance
- 28:49video, we generally use if you want
- 28:51returns, right? This function is going
- 28:52to give us returns or this formula is
- 28:54going to give us returns. We generally
- 28:56will just call returns as either r or
- 28:58rats as the variable name. Of course,
- 29:00you can call it whatever you want. And
- 29:01let's go ahead and implement this
- 29:03formula. So we have mu time dt. Now, we
- 29:07could do mu * dt. And this looks correct
- 29:12because that is how it looks in the
- 29:14formula. We're going to need to do like
- 29:16a little segue here, right? If you watch
- 29:18my first quant uh quant finance video,
- 29:21you might understand why this is
- 29:23technically not correct. Now, I know
- 29:25this is confusing. You can easily just
- 29:27skip ahead and not worry about the
- 29:29details on this, but I'm going to give
- 29:30you guys a little bit of a demo um just
- 29:32if you're interested. So the goal here
- 29:34for MOO of DT is MOO is an annualized
- 29:37return and we literally just need right
- 29:39this term is literally just turning an
- 29:40annualized return into a monthly return.
- 29:43So if you think back to that first video
- 29:45how can we do that right now if we just
- 29:48do this this is technically incorrect
- 29:50and you know you might have went through
- 29:52this video without even realizing it.
- 29:53I'm just going to address why now don't
- 29:55worry if it's boring right so let's say
- 29:56we have an annualized return of 0.07 07
- 29:59and we're just going to multiply it by
- 30:01dt. And we know dt is 1 / 12, right? If
- 30:04we just did moot * dt, this is what we
- 30:06would get. And I'm just going to give
- 30:07you guys a little sort of example here.
- 30:09We don't need to worry about all like
- 30:10the floating point, you know, inaccuracy
- 30:12or anything like that. Um, and if we do
- 30:14this, this is going to tell us that, you
- 30:16know, 0.583%
- 30:19per month is the return per month,
- 30:22right? And then if we go ahead, again,
- 30:23don't worry about the floating stuff. If
- 30:24we go ahead and we take this number and
- 30:26then we just multiply it by 12 months a
- 30:28year, well, it looks like the an
- 30:29annualized return is back to being 0.07.
- 30:32So, it looks like we did things right.
- 30:35But again, if you think back to the um
- 30:37think back to my first video, how do we
- 30:39actually get an annualized return? So,
- 30:41we're going to pretend that this is the
- 30:42monthly return, even though this is
- 30:44wrong, and we're just going to calculate
- 30:45an annualized return. So, if you think
- 30:47back to my first video, we do the 1 plus
- 30:49r format. This is literally a return. So
- 30:52we're going to do 1 plus r and then in
- 30:54that video we would do apra for
- 30:56dotproduct. Of course this is not a
- 30:58vector or a time series or a return
- 31:00series. So obviously this isn't going to
- 31:01work. This is just an individual number
- 31:03in Python. Um instead of prodra we can
- 31:06actually just raise it to the 12. And if
- 31:08you want to know you know why we're
- 31:09doing that that's the same as taking one
- 31:11month's return multiplying it by a
- 31:13second month multiplying by a third
- 31:14month and then just doing that 12 times
- 31:16in a row. And then once we're done with
- 31:18that we can just go ahead and minus one.
- 31:20Uh again, this will make sense if you
- 31:22watch the first video. This is just
- 31:24turning a monthly return into an annual
- 31:26return. And if you run this cell, we can
- 31:28now see that the annual annualized
- 31:30return is 7.2%
- 31:33even though obviously it sort of should
- 31:34have been 7%. So I'm just showing you
- 31:37that if you just use multiplication,
- 31:39essentially you're getting into the case
- 31:40where we're calculating arithmetic
- 31:42returns, but in reality, we need to
- 31:44calculate geometric returns. This is why
- 31:47this is called geometric brownie in
- 31:48motion. Um you know essentially again we
- 31:50talked about in the first video but the
- 31:52return that the investor eats. We call
- 31:54it in finance you know the return that
- 31:55you eat just means the return like the
- 31:58actual economic reality that you
- 32:00experience is the geometric return.
- 32:02Let's see how we can correct this. So in
- 32:04reality to turn this 0.07% annualized
- 32:07return into a monthly return we would
- 32:10actually need to go ahead and do 1 + r
- 32:14and then we need to go ahead and raise
- 32:15it right not to the 12. We're going the
- 32:17other way around. We're doing annual to
- 32:19monthly, so you need to raise it to 1
- 32:21over 12. And then when we're done with
- 32:23this entire thing, we can just subtract
- 32:25our one. Let's go ahead and run this.
- 32:27And now we get 0.056
- 32:30instead of 0.58.
- 32:32So this is a lower number. And if we
- 32:34want to just go ahead and sort of verify
- 32:36that we're correct, we can go ahead and
- 32:39now turn the actual correct answer of
- 32:41the monthly return into an annualized
- 32:42return. Again, we do 1 plus r. We can
- 32:45now just raise it um to the 12. And now
- 32:48we can go ahead and subtract one. And
- 32:50now we should get back. Again, don't
- 32:52worry about the inaccuracy. Now we
- 32:53actually get back 0.07. Uh in fact, let
- 32:56me just give another real quick example.
- 32:57This is uh actually really interesting
- 32:59to note. Some firms will actually
- 33:01intentionally kind of blur the lines
- 33:04between arithmetic and geometric returns
- 33:06to essentially deceive investors. So let
- 33:09me just make up this example here just
- 33:10cuz it's really interesting. Let's just
- 33:12pretend that an asset management firm
- 33:14that's developing some marketing
- 33:16materials, uh, let's say that they use
- 33:18this to their advantage. So, let's say
- 33:19that the actual economic reality, their
- 33:22actual annualized return, let's just
- 33:24say, I'm just going to make up the
- 33:25numbers, is equal to 5% a year. So, if a
- 33:28fund actually gets 5% a year, and let's
- 33:31just say that they get 5% a year every
- 33:33single year for 5 years. If you want to
- 33:35calculate the compound return at the end
- 33:37of that 5year sample, right, we're just
- 33:39going to do 1 plus r raised to 5 years
- 33:42minus one, right? So this would be the
- 33:44compound growth if a firm earns 5% for 5
- 33:47years in a row. And when we run this,
- 33:49they earned 27% at the end of their
- 33:52sample. Now, how some firms essentially
- 33:55deceive investors is they'll take this
- 33:57geometric return and they will literally
- 33:59just divide it by 5 years and they get
- 34:020.055%
- 34:03a year. and they'll literally go out and
- 34:06say, "We earn 5.5% a year." Well, in
- 34:10reality, obviously, we know that they
- 34:11earn 5% a year, not 5.5% a year. In
- 34:16fact, if we actually believed that they
- 34:17earned 5.5% a year, we can go ahead and
- 34:20take this number and do the whole 1 plus
- 34:22r on this number and then we can
- 34:25actually raise this to the five, right?
- 34:27And if they were telling the truth, well
- 34:29then they actually would have earned 30%
- 34:31at the end of their 5-year sample, not
- 34:3327%. So all of this was a segue that was
- 34:37probably confusing. If you watch the
- 34:38first video, maybe you have some
- 34:40intuition as to what I'm talking about.
- 34:41Um, we can literally ignore all of this
- 34:44and we can just sort of fix up this
- 34:45formula. So let's go ahead now. Um, and
- 34:47instead of doing moo* dt with, uh, which
- 34:49is arithmetic, let's now do geometric.
- 34:52So we're actually going to need to do 1
- 34:54plus the moo. and then we're going to
- 34:56raise it to the DT and then we're going
- 34:58to do minus one. So it's literally
- 35:00everything I talked about. We can go
- 35:02ahead and just delete all these cells
- 35:03now. Um and that's what we're actually
- 35:05going to do. Um and now let's go ahead
- 35:07and continue the formula. So the rest of
- 35:09this is going to be very simple. As we
- 35:11can see, I'm literally just following
- 35:12what's up here. So we're going to do
- 35:13sigma and we're going to multiply sigma.
- 35:15We can just use numpy square root
- 35:18function and multiply sigma by the
- 35:21roo<unk> of dt. Again, if you watch the
- 35:22first video, that's just how we scale
- 35:24the sigma, right? the volatility. Um,
- 35:26and then we're going to go ahead and
- 35:27multiply it by Z, right? And we're not
- 35:30calling this Z of T. We're just calling
- 35:32this Z cuz in this case it contains,
- 35:34right, 10 * 12, 120 different size. And,
- 35:37you know, we can run this. It's not
- 35:38going to output anything. We're just
- 35:39defining the function. But this is all
- 35:41we need. Um, and now let's go ahead and
- 35:43return something. And there's different
- 35:45sort of things we can return, right? We
- 35:47could just return rats. And that's going
- 35:49to give us a numpy array. Um, a numpy n
- 35:52dimensional array. Let me just go ahead
- 35:54and show you how that looks. Right. So,
- 35:56right now, we're just getting this numpy
- 35:58array. And if you ever get stuck while
- 36:00you follow along, just use the type
- 36:02function. This is going to be your best
- 36:03friend. It's just going to tell you what
- 36:04data type we're looking at. As we can
- 36:06see, it's a numpy n-dimensional array.
- 36:09I'm going to talk about what that is in
- 36:10just a second. Um, now for the rest of
- 36:12this video, same as my last video, we're
- 36:15really going to do everything we need to
- 36:17do um in general in pandas. The z stuff
- 36:19we can leave as a numpy thing since
- 36:21we're just using it right away. maybe we
- 36:22don't need a reference I again. Um but
- 36:24in this case we're going to be using
- 36:26pandas. So let's go ahead and import
- 36:28pandas as pd and make sure to run that
- 36:32cell. Um again you can go ahead and do
- 36:35percent sign pip install pandas if you
- 36:38don't have pandas installed right if
- 36:39you're following along. Of course for me
- 36:40it's installed already. Um and now what
- 36:42we can do is we can actually cast this
- 36:45from a numpy array into either a pandas
- 36:47series or a panda um or a pandanda's
- 36:49data frame. So if you cast this into a
- 36:51series um and run this, it's now of type
- 36:54series. Let's actually take a look at
- 36:56it. Now this is a series. Um a series is
- 36:59just sort of like this one-dimensional
- 37:00time series. If you watch my last video,
- 37:03um be general, at least me personally,
- 37:05be generally uh want to view things as a
- 37:07data frame. We're going to see why in
- 37:09just a second. Um it looks a little
- 37:10nicer. And essentially this is what
- 37:12we're left with. So as I said, now we're
- 37:14going to get a dataf frame. This is what
- 37:16it looks like. If we do type, we can see
- 37:18it's a dataf frame. Um, and then there's
- 37:20something else that's going to be of
- 37:21interest, which is going to be dotshape.
- 37:24And this is just going to tell us the
- 37:26dimensions of this data frame. A dataf
- 37:28frame is just like a vector. Again, the
- 37:30first video will sort of explain that.
- 37:32Um, data frames will actually print the
- 37:34shape out by default. We can see that
- 37:35this is 120 rows by one column, right?
- 37:38It's sort of condensing it down in the
- 37:39middle, but we have 120 different data
- 37:41points. Now, this is going to be another
- 37:44little digression just for those of you
- 37:45that are interested, but you might be
- 37:47wondering, we know that Z, we know that
- 37:50there's 120 different Z figures in this
- 37:53Z numpy array, but moo is just a regular
- 37:56number, which means that's just a
- 37:58singular number. DT is just a singular
- 38:00number. One is just a singular number.
- 38:02Sigma is just a singular number. Right?
- 38:04All of this stuff is just an individual
- 38:06number within Python. and then we're
- 38:08multiplying one number by this array of
- 38:10120 numbers and we still get 120
- 38:12numbers. Um this is just going to be a
- 38:14little digression. So essentially this
- 38:16works because we're doing something
- 38:18called broadcasting. So let me just
- 38:20explain what's actually happening um in
- 38:22simple terms. So let me just go ahead
- 38:24and construct uh just a regular let's
- 38:26just say numpy array and let's just
- 38:28construct this out of you know elements
- 38:311 through 5. Right? So if you run this
- 38:33this is all this looks like. Again, we
- 38:35can go ahead and check out the type,
- 38:37which is a numpy n-dimensional array.
- 38:39And if we want to know how many
- 38:41dimensions are in this n-dimensional
- 38:43array, we can do nim for number of
- 38:46dimensions. And it will say there's just
- 38:47one dimension. Which is why when we run
- 38:49this, it essentially just looks like a
- 38:51regular Python list except it's a numpy
- 38:53array. Um, so if we go ahead and we
- 38:56multiply this one dimension that
- 38:58contains five different data points by
- 39:00let's just say two, what's going to
- 39:02happen is something called broadcasting.
- 39:04That just means that every single
- 39:05element within this array is going to be
- 39:07multiplied by two independently. So when
- 39:09we run this, we now have 2 4 6 8 10. So
- 39:13the same thing happened on this line is
- 39:15we're essentially applying the sigma and
- 39:17applying the mean to every single one of
- 39:20these 120 eyes independently. And you
- 39:23know that's really all we're doing.
- 39:25Okay, so the function's literally done
- 39:27already. Um let's go ahead and let's
- 39:30call GBM, right? We're just going to use
- 39:32the default parameters for now is fine.
- 39:34And this is the random walk that we get
- 39:36back. So let's go ahead and let's think
- 39:38about things. So what are we actually
- 39:41getting back? Is it prices? Is it
- 39:43returns? Right? In this case, it's
- 39:45returns. Right? Each one of these data
- 39:47points is a monthly return. Let's just
- 39:50go ahead and save this in a variable
- 39:51called rats, right? So rats is what we
- 39:55just saw. It's monthly returns. And
- 39:57let's go ahead and let's plot this. So
- 40:00if you think back to my last video, um
- 40:02because this is directly already a
- 40:03pandas dataf frame, we can literally
- 40:05just call plot on it. Um now, same
- 40:08thing, you might need to go ahead and do
- 40:11hit install.
- 40:13Oops, install mattplot lib. Again, for
- 40:16me it's installed already. Um, by
- 40:18default, the plotting engine that Pandas
- 40:20uses is going to be Maplot lib again on
- 40:22that website I talked about last time. I
- 40:24was using like plotty. We're actually
- 40:26going to do way more in-depth plotting
- 40:28stuff um later on in the video. For now,
- 40:30we literally are just going to do the
- 40:31umplot method. It's going to handle
- 40:33everything for us. And when we run this,
- 40:35let's see what we get back. Boom. It
- 40:38doesn't look too nice visually. Um, but
- 40:40this is how returns look. We're not
- 40:43actually calculating like a wealth index
- 40:45yet, but let's go ahead and do that now.
- 40:47As soon as we do this, hopefully things
- 40:48will be more clear. So, if you think
- 40:50back to my last video, how do we turn
- 40:52this return series into, let's say, a
- 40:55wealth index, which represents the
- 40:56growth of $1. Well, let's go ahead. We
- 40:59know that we need to do 1 plus rats,
- 41:01right? 1 plus r. And if you wanted the
- 41:04overall compound growth, we would dopra,
- 41:06which is product. But we want to be able
- 41:08to plot this. We don't just want the
- 41:10terminal wealth. We want to see the
- 41:11wealth at every single consecutive time
- 41:14step. So if you think back to that
- 41:15video, we're actually going to do cumro,
- 41:18which stands for cumulative product. We
- 41:20also introduced last video cumulative
- 41:22maximum. Um in this case, we're going to
- 41:23need the cumulative product. And this is
- 41:26literally going to be um the wealth
- 41:28index. Let's go ahead and put that in a
- 41:30variable and let's take this and let's
- 41:33plot it and let's see what we have. And
- 41:35beautiful, right? Like look at this,
- 41:37right? So you know, it's really not that
- 41:40much code. But it's really quite simple
- 41:41now that you're seeing and we're
- 41:42essentially already done. Like this is a
- 41:44mathematically sound complete working
- 41:47brownie in motion function. Now we're
- 41:49going to go ahead and we're going to
- 41:50start to improve it and clean it up and
- 41:52do a lot more interesting things. Um now
- 41:54every time I run this cell, if you look
- 41:56at the path, it's going to be slightly
- 41:57different every single time we run it.
- 42:00Um of course that's because it's a
- 42:01stochastic model. Those zies are coming
- 42:04out of that numpy random function um
- 42:06differently every single time. Okay, so
- 42:08let's go ahead and let's start to
- 42:10improve this function. So in the intro
- 42:12of this video, I probably would have
- 42:14showed you a similar looking chart,
- 42:16except there probably would have been
- 42:17like 50 or 100 different random walks on
- 42:19it, not just one. Let's go ahead and I'm
- 42:22just going to sort of iterate over this
- 42:24function, maybe just adding like one or
- 42:26two new things at a time. So let me just
- 42:27copy and paste it. And now let's go
- 42:29ahead and have the ability to do this on
- 42:32multiple scenarios, not just one. Um, so
- 42:34let's go ahead and define a new
- 42:36parameter. Let's just call this um n
- 42:39scenarios and let's default this to like
- 42:4250 for now. Um and this is going to
- 42:44control how many random walks there are
- 42:46on the graph. And this is actually going
- 42:49to be really simple. Um we're not going
- 42:50to again we're not going to need a for
- 42:52loop. I'm going to talk about time
- 42:53complexity in just a second. But a lot
- 42:55of the time when we do this sort of
- 42:57vectorzed stuff and statistical
- 42:58numerical stuff in Python, we really
- 43:01almost in a way want to avoid for loops.
- 43:03I actually talked about that on one of
- 43:05my first videos ever on my channel. It
- 43:07was the third video about time
- 43:08complexity. We actually used a financial
- 43:10example in that video and in the next
- 43:12video on space complexity. I think we
- 43:14actually also did rolling returns. You
- 43:15can see how this finance stuff, you
- 43:17know, sort of seeps in. Um, but you
- 43:19know, we're going to talk about time
- 43:20complexity in a second. But in general,
- 43:22we want to avoid for loops. We want to
- 43:24push back as much logic we can almost
- 43:27like into these sort of like numpy and
- 43:29pandas libraries instead of doing it
- 43:31oursel. Um, anyways, we're actually
- 43:33going to take advantage of broadcasting
- 43:35again. So, all we really want to do is
- 43:37we want to change the shape of our
- 43:39returns. Instead of being, you know, one
- 43:42by 120, we want it to be maybe like 50
- 43:45by 120. Um, so let me go ahead and
- 43:47showcase this. Um, let's just set size
- 43:49to, let's say, 120, right? This is what
- 43:51we're doing right now. Um, and again, if
- 43:54we go ahead and we do, you know, n dim,
- 43:56we can see that it's only one dimension.
- 43:59Now, there's no way you would have known
- 44:00this unless you read the documentation,
- 44:02but it turns out that size could be a
- 44:04single number, but it could actually be
- 44:06a tpple that contains two numbers, and
- 44:08then it will actually give us a
- 44:10two-dimensional array of random normals
- 44:12instead of one. So, we can go ahead and
- 44:14set this to let's say number of steps.
- 44:16And then for the second one, we're
- 44:18actually going to use number of
- 44:19scenarios, right? In this case, if you
- 44:21don't default that to 50, this is how
- 44:23this function call looks. And if you run
- 44:25this now, I know it might not look too
- 44:27pretty. Um, but we can see that the
- 44:28shape is actually 120x 50. Um, we can
- 44:32actually go ahead and cast this into a
- 44:35PDA frame just if you want to visualize
- 44:37it better. And now we can see that we
- 44:39have 120 rows by 50 columns. And you
- 44:41know, this is sort of how we can do this
- 44:43over, you know, 50 or 100 different
- 44:45scenarios. Um, so let's actually go
- 44:47ahead and do that. So all we're going to
- 44:50do is instead of just number of steps,
- 44:52we're going to do number of steps and
- 44:53then number of scenarios. And it's
- 44:56actually that simple. We can go ahead
- 44:57and run this. Let me just reuse this
- 45:00plotting stuff and let's see how this
- 45:02looks.
- 45:04And boom. Right? It's that simple. Now,
- 45:06obviously, this doesn't look the best
- 45:08because of the legend. Um, we can go
- 45:10ahead and clean that up. Or what we
- 45:11could do is we actually could just say
- 45:13legend equals false. Um, that will take
- 45:15care of that for us. And the chart looks
- 45:18kind of tiny. and it looks kind of
- 45:19squished. Um, we can actually also fix
- 45:21that up with a parameter called fig
- 45:23size, which just means figure size. In
- 45:25fact, we're going to be talking all
- 45:27about figures later on in the video. Um,
- 45:29this is a tpple. Uh, we're just going to
- 45:31hand this, let's just say like 126, and
- 45:33it should be a little bit bigger now.
- 45:35And we're done, right? I probably would
- 45:36have showed you something like this in
- 45:37the intro of the video. And like, you
- 45:40know, it's barely any code. Now, you can
- 45:41start to see, you know, the math stuff.
- 45:43Like, it looked complex, but it's really
- 45:45incredibly simple. And this is
- 45:47beautiful. And every time I rerun this
- 45:49cell, we're going to get, you know,
- 45:50different random walks back. And, you
- 45:53know, we're going to see, we're going to
- 45:54do the interactive stuff at the end of
- 45:55the video. Let me just, you know, change
- 45:57number of scenarios to 100. And now,
- 45:59when we rerun this, there's going to be
- 46:00100 different random walks instead of
- 46:02just 50. That's really all there is to
- 46:04it. Okay. So, again, we're we're done,
- 46:07right? The function works. But let's
- 46:09just go ahead and let's keep on
- 46:10improving it. So, let me go ahead and
- 46:12take what I have already. Um, and
- 46:14there's a couple different things we
- 46:15might want to do. Now, first of all,
- 46:17this function right now is giving us
- 46:19returns, which is completely okay. It's
- 46:20up to us what we want. Um, but right
- 46:23now, I'm just interested in sort of
- 46:24plotting it. So, instead of needing to
- 46:26sort of do all this stuff after we call
- 46:28the function, let's just go ahead and do
- 46:29this inside of the function. So, instead
- 46:31of just directly returning the data
- 46:33frame of returns, let's go ahead now
- 46:35and, you know, just update rats to just
- 46:38be a dataf frame now. Um, and now what
- 46:40we're going to return is going to be the
- 46:41wealth index directly. So, we can just
- 46:43do 1 + rats. prod directly inside of the
- 46:47function. And really really that's all
- 46:49we're doing. Um next time we call the
- 46:51function GBM, we can just plot it
- 46:53directly, right? Maybe we still want
- 46:55legend to be false. And maybe we still
- 46:57want fig size to be 126. Um but now we
- 47:00don't need to do this qro stuff outside
- 47:02of the function. Now we're just getting
- 47:04back a wealth index directly from the
- 47:05function. That's just one additional
- 47:07thing we're adding. Um now the second
- 47:09thing I'm going to add. So right now
- 47:11each random block here represents the
- 47:13growth of $1. They all start at one and
- 47:16they don't exactly start at one. We're
- 47:18actually going to be fixing that in just
- 47:19a second. Um, that's just more of like a
- 47:21plotting issue mathematically. They all
- 47:23start at one. Um, and then they grow
- 47:25over the 10ear sample. In this case, $1
- 47:27grew to, you know, maybe just over $5.
- 47:30But what if we want to actually change
- 47:31the initial wealth? That's actually a
- 47:33really easy change. We can make this a
- 47:35parameter and then that will be
- 47:36available to change once we actually get
- 47:38into the interactive plotting stuff. So
- 47:40instead of the growth of $1, let's maybe
- 47:42default it to the growth of $100. Um we
- 47:44can call this maybe like initial though
- 47:47if you think back to the formula. Um
- 47:49right s of t is sort of like the price
- 47:52at time t. Um in this case we can
- 47:54actually also call it s of zero. Of
- 47:56course it's just a variable name. It
- 47:57just means the initial starting price um
- 47:59for the index. And let's just start
- 48:01things off by default at $100. And I
- 48:04think we would have probably covered
- 48:05this in my last video when we did wealth
- 48:06index stuff. Um but it's a super easy
- 48:08change. We literally can just do S0
- 48:11multiplied by all this stuff and now it
- 48:13will be the growth of $100 instead of
- 48:15the growth of $1. We can go ahead and
- 48:17run this and it won't look any
- 48:18different. But if you look at the axis
- 48:20now, we can see that everything starts
- 48:21at $100. So that's just another really
- 48:23simple change. So again, we're more or
- 48:26less done. Um, but now we're going to do
- 48:28something really cool. So now we're
- 48:30going to see, all right, we're happy
- 48:31with this function for now. Let's go
- 48:33ahead and see if we can make it more
- 48:35efficient. Now again, there's no way you
- 48:37would have known to do this if you
- 48:39didn't read the documentation or if I
- 48:41didn't tell you myself. Um, but it turns
- 48:42out there's actually really useful
- 48:44additional parameters inside of this
- 48:46numpy random normal function.
- 48:48Specifically, there's going to be lock
- 48:50for location and scale and we're going
- 48:52to see how we can actually use these to
- 48:53make the function more efficient. So as
- 48:56I said when we do these sort of like
- 48:57numerical applications in Python um we
- 49:00essentially want to do as little
- 49:01computation ourself in Python and we
- 49:04want to push as much of it as possible
- 49:07into these sort of libraries like pandas
- 49:10and numpy that are written in C and
- 49:12they're highly efficient. Um and I
- 49:13talked all about this in my time
- 49:15complexity video. I would actually
- 49:16recommend you watch that though it's not
- 49:18necessary. Um now let's also talk about
- 49:20the sort of concept of like loops. So we
- 49:23don't see any actual for loops natively
- 49:26inside of Python. But anytime we have an
- 49:29operation even where we just add like a
- 49:31one to a number rights is not one
- 49:34individual number. Rhetts is this
- 49:37massive data frame right? This massive
- 49:39like two-dimensional vector you can say
- 49:41of you know 120 by 100 right there's
- 49:44hundreds if not thousands of data points
- 49:46that will be inside of rats depending on
- 49:48the parameters. So when we do one plus
- 49:50rats, we're not adding one number to
- 49:52another number. We're adding one number
- 49:54to hundreds or thousands of numbers,
- 49:57right? So anytime we see this
- 49:59multiplication or we see this, there's
- 50:01actually an implicit loop that's
- 50:03happening. Um, let's just make things
- 50:05more efficient. So instead of generating
- 50:08random normals and then applying a moo
- 50:10and sigma, it turns out that we can
- 50:12apply the moo and sigma inside of the
- 50:14random normal function itself. So, as I
- 50:17showed you in documentation, there's um
- 50:20it accepts a parameter called lock,
- 50:21which is essentially just location,
- 50:23which essentially just means the mean.
- 50:25So, by default, the distribution has a
- 50:27mean of zero. But we can literally just
- 50:29hand it our mean
- 50:32as a parameter. And now, right, this
- 50:35numpy function is going to be applying
- 50:37all of these means for us. We don't
- 50:39actually need to like worry about doing
- 50:41it oursel. Um, and then in addition to
- 50:43that, there's another parameter called
- 50:45scale, which literally just means the
- 50:47standard deviation. So, same thing here.
- 50:50We can actually go ahead and rip this
- 50:51out and just do it directly inside of
- 50:54the function, right? In fact, we can
- 50:56just remove these brackets here if we
- 50:57want to, right? And now we're just
- 50:58generating random normal numbers that
- 51:01already have the standard deviation and
- 51:02mean that we need. We can now just fully
- 51:05get rid of that. And then instead of Z,
- 51:07we can just call these our returns
- 51:09already. And that's it. Right? this is
- 51:11and you know you can go ahead and
- 51:13actually time how long this takes to
- 51:14execute on your machine. It might be a
- 51:17lot quicker or it might actually be the
- 51:19same amount of time just depending on
- 51:21your exact hardware and you know
- 51:22obviously this goes deeper but in
- 51:24general this is like excellent practice.
- 51:26Um so now same thing we can go ahead and
- 51:29we can just call and plot this function
- 51:31and of course it's not going to look any
- 51:33different. Let's make sure we run this.
- 51:35Same thing we still get everything we
- 51:36need. Okay, now last sort of thing on
- 51:39efficiency. You know, I don't know if
- 51:41you guys find this like boring or
- 51:42whatever. Obviously, we're going to get
- 51:43to the interactive plotting stuff, but
- 51:45this is like fascinating, right? This
- 51:47video isn't just for finance. This is
- 51:50for everything. It's for coding. It's
- 51:52for mathematics, right? So, I'm just
- 51:53going to sort of walk through
- 51:54everything. Um, again, you could just
- 51:56leave this function and we could just be
- 51:57done and that's completely okay. It's
- 51:59mathematically correct. But, we can
- 52:00actually make it even more efficient. So
- 52:03if we take a look at this negative one
- 52:05here, right, we're handing it the mean
- 52:07and then we're turning it into a per
- 52:08month mean and then we subtract a one,
- 52:11even though the next time we sort of use
- 52:13this RTS variable, we're just adding a
- 52:15one to it again. And when we add the one
- 52:17over here, we're adding a one to, you
- 52:20know, let's say like 120 different data
- 52:22points or potentially even thousands or
- 52:25tens of thousands of data points. Um,
- 52:26why would we sort of subtract a one and
- 52:28then add a one to the same thing? we can
- 52:30just get rid of both of those operations
- 52:32um and literally like get rid of another
- 52:34loop. So we can literally just not
- 52:37subtract one and then we don't have a
- 52:39need to add one anymore and this will
- 52:42still work as intended. Um now we might
- 52:44want to make this a variable name
- 52:45instead of rats. This technically, you
- 52:47know, is like rats + 1 or rats p1. Um
- 52:52but everything will still work as
- 52:53intended, you know, and we can go ahead.
- 52:55We might as well just clean up the
- 52:56function while we're at it. We don't
- 52:57need a whole separate line. Let's just
- 53:00put that there. Um, and we should be
- 53:01good, right? Why would we sort of do all
- 53:03that unnecessary computation? Um, we can
- 53:05now run this cell again and run this
- 53:07again. And of course, visually there's
- 53:09no changes, right? Mathematically with
- 53:11the numbers, there's no changes. Um, we
- 53:12just made it more efficient. Okay, so
- 53:15again, the function is good. It's a
- 53:17beautiful function. It's a beautiful
- 53:19chart. It's mathematically correct. We
- 53:21could just call it a day and we could
- 53:22just move on to interactive plotting.
- 53:24But there's one last thing I want to
- 53:26address because it's kind of annoying me
- 53:28and it might be annoying you too. So,
- 53:30you know, the goal here is we just want
- 53:31to graph out like the growth of $100.
- 53:34But when we look here, we can see that
- 53:36all of these lines are not starting at
- 53:38$100 instead of all starting on the same
- 53:40data point. There's like a massive gap.
- 53:42And obviously, right, you might sort of
- 53:44know if you're following along. Um, but
- 53:45we're applying the first day's return to
- 53:48the first data point that's already in
- 53:50our wealth index. We can fix this by
- 53:52essentially prepending just like a
- 53:54static one. Um, and then the chart's
- 53:56going to look better. But we need to be
- 53:58extremely careful with how we do that or
- 54:01we could actually sort of like mess up
- 54:04the mathematical integrity of the
- 54:06function. So, I'm going to go through a
- 54:08quick example here just to show you guys
- 54:10um exactly what I mean. Now, if this
- 54:12part doesn't make sense, obviously feel
- 54:14free to skip ahead. Um, but I think this
- 54:16is actually pretty useful. So, I'm going
- 54:19to go ahead and we're going to do two
- 54:20different functions and I'm going to
- 54:22show you how you might think we can fix
- 54:25the graph and it will actually mess up
- 54:27the function mathematically and then
- 54:28we'll go through the actual proper way.
- 54:30Um, so the first function where we're
- 54:32not yet addressing this issue, we know
- 54:34it's completely good mathematically. We
- 54:36can go ahead and call this let's just
- 54:38say GBM original and we're going to go
- 54:40ahead in this function and we're going
- 54:43to go ahead and we're going to insert
- 54:44this static one. and we want to set up
- 54:46an experiment so we can make sure we're
- 54:48not messing up the function
- 54:50mathematically. Um, so one sort of thing
- 54:52we might just want to look at is let's
- 54:54just say the terminal wealth. So we want
- 54:56to look at, you know, the very last data
- 54:58point and then we're actually going to
- 55:00end up shrinking the sigma down to zero
- 55:02just to remove the volatility so we can
- 55:04get accurate results. This is just like
- 55:06a tiny sort of mini little test, right?
- 55:08So let's go ahead and run this cell. Um,
- 55:10and let's go ahead and call GBM
- 55:12original. Um, and let's go ahead. Well,
- 55:14let's just run the function, right? We
- 55:16get back this two-dimensional data
- 55:17frame, 120 rows by 100 columns. Um, and
- 55:20let's say we want to look at the
- 55:22terminal wealth. So, we just want to
- 55:23look at the very last time step, not all
- 55:26120 time steps. Um, so, you know, if you
- 55:28think back to my first video, we have,
- 55:30you know, lock for location or IO for
- 55:32integer location. And we can go ahead
- 55:35and just hand this a negative one.
- 55:36That's just going to give us the very
- 55:38last index here. Let's go ahead and run
- 55:40this. Now, we only get one back.
- 55:42Although because this is now single
- 55:44instead of multi-dimensional, this is
- 55:46actually giving us again we can go ahead
- 55:47and do type. Um this is actually going
- 55:50to be a series instead of a dataf frame.
- 55:52Let's just check this. Sure enough, it
- 55:54is a series. Um if you want to keep it
- 55:56as a data frame, you know, we could cast
- 55:57it again or we can just take a little
- 55:59shortcut and use the colon slicing
- 56:01operator. And now we just write it
- 56:03really doesn't matter. It's all the same
- 56:04data. Um it just depends how you want to
- 56:05look at it. And now let's say that we
- 56:07want to view um the average terminal
- 56:10wealth. So right we have you know all
- 56:13these sort of trials here. What if we
- 56:15just want to view the average um we can
- 56:17just take the mean and this alone is
- 56:20going to give us some issues because
- 56:21we're actually using the wrong axis. Um
- 56:23you essentially need to specify do you
- 56:25want to take the mean you know over like
- 56:27the rows or over the columns. Um you
- 56:30know and if we didn't sort of slice it
- 56:32we wouldn't have that issue. So you know
- 56:34again there's no right answer. There's
- 56:35many different ways to do it. Um, but if
- 56:37you want to sort of fix this up, um, we
- 56:39would just need to specify, um, an axis
- 56:41of one just to make sure we're actually
- 56:43summing um, along um, along that sort of
- 56:46horizontal row. Um, and we should be
- 56:48good. Now, the issue is that every
- 56:50single time I run this, even though
- 56:52we're taking the average over 100
- 56:53different trials, you know, we're still
- 56:55going to get a slightly different number
- 56:56back every single time. So, for the sake
- 56:59of this experiment, let's go ahead and
- 57:01just set the volatility to zero. Now,
- 57:04when we run this, right, 196.715,
- 57:07right? Every time we run this, it's the
- 57:09exact same number. Um, obviously, we
- 57:11don't even need to take an average
- 57:13anymore since all of these numbers are
- 57:14going to be the same, right? We can go
- 57:16ahead and do one scenario. We can just
- 57:18snip the first one off. I'm just going
- 57:20to leave it as this for now. Um, you
- 57:22know, it it doesn't really matter.
- 57:23Obviously, there's a bunch of ways to do
- 57:24it. Um, the only thing that matters is
- 57:25we are just looking at the terminal
- 57:27wealth, um, you know, over this, uh, 120
- 57:30time steps using these input parameters.
- 57:32And the correct answer is we're going to
- 57:34finish on average with $196. So now
- 57:37let's go ahead into this function and
- 57:40let me show you how you might think you
- 57:41can fix the issue but would actually
- 57:44sort of mess up the function. So let's
- 57:46call this GBM overwrite. And you might
- 57:50say, you know, you said just sort of
- 57:52make that very first data point just a
- 57:54static one. So we could go ahead and we
- 57:57could just index right into that very
- 57:59first time step and set everything equal
- 58:02to one. And broadcasting is going to
- 58:03take care of this. And if we go ahead
- 58:06and graph this, right, let's go ahead
- 58:08and graph this by doing plot. And then
- 58:11we might need to do legend equals false
- 58:14um fig size. Right? If we go ahead and
- 58:15graph this, it's going to look like the
- 58:17issues fixed. Uh let me just go ahead
- 58:20and change those parameters back to
- 58:230.15. Um, I'll change this back when we
- 58:25actually do the test. Um, but this is
- 58:26actually going to look like it solved
- 58:28the issue, right? Every single data
- 58:30point, every time we run this, the very
- 58:32sort of first initial time step is now
- 58:34centered on that one, which then later
- 58:36becomes 100. But the issue here is
- 58:38because we overwrote um that very first
- 58:41sort of days return. We're actually sort
- 58:44of messing up our numbers. So, let's go
- 58:45ahead here and let's do the same test
- 58:48for this. Um, now don't forget, we're
- 58:50going to change this back to a sigma of
- 58:52zero. Um, and let's go ahead and take
- 58:54this and we're going to run this in just
- 58:56a second. Now, just so you can sort of
- 58:58think about this, what do you think the
- 59:00results of the test are going to be?
- 59:01Right now, ideally, if we actually sort
- 59:04of prepend that one, we're not actually
- 59:06going to change the average terminal
- 59:07wealth, right? We shouldn't actually be
- 59:08changing the mathematics of the
- 59:10function. We really just want to sort of
- 59:11solve this plotting issue. Um, right?
- 59:14But because the MO is on average, right,
- 59:17we're handing it a positive expected
- 59:18return and then we're obviously adding
- 59:20one to those numbers. That means that
- 59:22when we hit this line, since we're
- 59:24overwriting the first day's return,
- 59:25essentially what we're doing is we're
- 59:27trading a number that is going to be
- 59:28bigger than one on average with just the
- 59:31number one. So if you trade a number
- 59:33that's bigger than one with one, right?
- 59:35Meaning if we overwrite a positive
- 59:37return with a flat return, that means
- 59:39that at the end of the sample on
- 59:41average, we're going to have less
- 59:42wealth. So in this case, the normal
- 59:44case, we should end with $196. But in
- 59:47the overwrite case, uh, oops, we didn't
- 59:50do the right function name there. In the
- 59:51overwrite case, we actually have 195
- 59:54instead of 196. So, we actually messed
- 59:58up our numbers mathematically, right?
- 1:00:00This sort of like average return is
- 1:00:01messed up. The volatility might be
- 1:00:03messed up. And, you know, you probably
- 1:00:04wouldn't have noticed this. Um, super
- 1:00:06simple fix. Let's just go ahead and fix
- 1:00:08it now. So, this example has already
- 1:00:09taken up like a lot of time even though
- 1:00:11it's not that serious. Um, so I'm going
- 1:00:13to go ahead and kind of just like blaze
- 1:00:14through this. Um, all we're really doing
- 1:00:16is we're just going to be prepending.
- 1:00:18Let's call this prepend. We're just
- 1:00:19going to be, um, prepending the
- 1:00:20appropriate size. Uh, essentially just
- 1:00:22like an array just full of ones. Um,
- 1:00:24prepending it. And then, you know, we're
- 1:00:26not actually overwriting data. Um, so
- 1:00:28let's just go ahead and get that done.
- 1:00:29So, let's just take the rats p1. Um, and
- 1:00:32then we're going to be updating this.
- 1:00:33And we're going to be using a function
- 1:00:34called pd.conat. I believe I talked
- 1:00:37about this in my first video. Um, it
- 1:00:38takes in an array of, in this case, it's
- 1:00:41going to be two different things in this
- 1:00:42array. Two different data frames. This
- 1:00:44is going to concatenate both together.
- 1:00:45So the first one we're going to go ahead
- 1:00:47and we're going to call the pd.dataf
- 1:00:49frame constructor and we're actually
- 1:00:50going to hand it um a numpy
- 1:00:53function. Um what this function is going
- 1:00:55to do is it's basically just going to
- 1:00:56make a numpy n dimensional array of
- 1:00:59ones. And now we can just go ahead and
- 1:01:00specify the shape. And in this case the
- 1:01:03shape is just going to be one by the
- 1:01:05number of scenarios. Um, now if you want
- 1:01:07to know exactly why, go ahead and make a
- 1:01:08new cell if you're following along and,
- 1:01:10you know, see if you can sort of almost
- 1:01:12do this before I write the code, right?
- 1:01:14Um, I'm I'm sort of just going to gloss
- 1:01:15over this. We're literally just
- 1:01:16inserting, you know, one whole column of
- 1:01:18ones. Um, and then the second argument
- 1:01:20is we're just going to be prepending
- 1:01:22this column of ones with our existing
- 1:01:24data frame. We can actually just copy
- 1:01:26that from here. Um, and that's basically
- 1:01:28it. Now, when we actually run this, or
- 1:01:31let's just go ahead and do this now. Um,
- 1:01:33so let's just go ahead and change that
- 1:01:34to that. And this is called GBM prepend.
- 1:01:38Now when we run this function, we're
- 1:01:39going to see something look a little
- 1:01:41weird. Um the issue is this is sort of
- 1:01:43like the row that was returned by the
- 1:01:45numpy ones function. Um this sort of you
- 1:01:47know has an index of zero whereas our
- 1:01:49first day also has an index of zero. We
- 1:01:51can essentially avoid this um by just
- 1:01:53handing an additional flag into concat
- 1:01:56called ignore index. We're just going to
- 1:01:57set this equal to true. Again go ahead
- 1:01:59and read the documentation and we'll
- 1:02:00give you the details. It's not that big
- 1:02:02of a deal. um that's just sort of going
- 1:02:04to fix that issue and make the indexes
- 1:02:05align. Um and that's really all we need
- 1:02:08to do. And now the data won't be messed
- 1:02:10up. So now let's go ahead and let's do
- 1:02:12the same test but on prepend um the
- 1:02:15sigma zero, right? Let's run this. We
- 1:02:17can rerun those. It won't change. And
- 1:02:19when we run this, ideally we're going to
- 1:02:20get a terminal wealth of exactly this
- 1:02:23number. Let's see. And perfect. Right.
- 1:02:25So this was just a test just to sort of
- 1:02:27see are we messing up the function
- 1:02:28mathematically. I probably spent too
- 1:02:30much time on it. Um, but this is
- 1:02:32essentially just a proper function. Now,
- 1:02:34now obviously something else to note,
- 1:02:36um, is you're probably seeing this,
- 1:02:38right? I'm seeing it, too. Um, because
- 1:02:39we're just prepending, uh, this entire
- 1:02:41sort of like row of ones is we now have
- 1:02:43121 rows instead of 100 rows, which
- 1:02:46makes sense. Um, now in this video, I'm
- 1:02:48like okay with that personally. I'm just
- 1:02:50going to leave it. Um, obviously, you're
- 1:02:52more than free to come up here and, you
- 1:02:54know, number of scenarios or number of
- 1:02:56steps, right? You can go ahead and just
- 1:02:57do like a minus one if you want to
- 1:02:59correct that. that's completely okay.
- 1:03:01Um, you know, but it doesn't actually uh
- 1:03:04it doesn't actually like mess up the
- 1:03:05mathical mathematical integrity of the
- 1:03:07function. So, I'm just going to let it
- 1:03:09have, you know, 121 instead of 120 time
- 1:03:12steps in this case. That's okay with me
- 1:03:14personally. Um, so let's go ahead and
- 1:03:16let's take this function now and let's
- 1:03:18just remove the prepend. And going
- 1:03:20forward, this is the actual GBM function
- 1:03:24that we're happy with. Um, we can go
- 1:03:26ahead and we can do the whole plotting
- 1:03:27thing. So let's call GBM and do plot and
- 1:03:31let's just turn off the legend and let's
- 1:03:33do fig size 126 and right we still
- 1:03:38didn't change the sigma back. That was
- 1:03:39just for that sort of example. Let's
- 1:03:41just change that back to 0.15. Let's run
- 1:03:43this. And beautiful. Right now we know
- 1:03:46that the mathematics is still intact. Um
- 1:03:48and we can now see on the chart we have
- 1:03:50the statically inserted data point. So
- 1:03:52everything sort of starts instead of
- 1:03:54having a gap and we're done. Right? This
- 1:03:56is beautiful. Right? I could literally
- 1:03:57print this out, put it on my wall, put
- 1:03:58in a picture frame. Right, we're done.
- 1:04:01Okay, so now that we're done, and again,
- 1:04:03you know, this video could easily be
- 1:04:05like 500 hours long. We could keep on
- 1:04:07tweaking things, but now that we're
- 1:04:09done, um, let's go ahead and actually do
- 1:04:11the interactive plotting. This is
- 1:04:13probably going to be like the most sort
- 1:04:14of fun part of the video. Um, we can
- 1:04:16actually just do some, uh, some markdown
- 1:04:20here. Let me just call this interactive
- 1:04:23plotting. Um, in all the code I'm
- 1:04:25probably going to, you know, upload to
- 1:04:26my GitHub after and you can reference
- 1:04:28the code yourself, though I think it
- 1:04:29might be helpful if you code along. So,
- 1:04:31let me give you guys the sort of premise
- 1:04:33or the demo of how we get started with
- 1:04:35making this plot interactive. Um, so we
- 1:04:38are going to be needing a library which
- 1:04:39is called IPI widgets. So, we're going
- 1:04:41to go ahead and we're going to import
- 1:04:43ipi widgets. Um, let's just say as
- 1:04:46widgets. And this is going to be the
- 1:04:48library that's going to enable us to do
- 1:04:49all of this stuff. This is like an
- 1:04:51awesome library. This is incredibly
- 1:04:53helpful not just within computational
- 1:04:55finance but within all sorts of things
- 1:04:57inside of Python. Um so let's sort of
- 1:05:00say how this works. Now of course same
- 1:05:02thing you might need to go ahead and do
- 1:05:04you know a pip install IPI widgets as
- 1:05:07always right just if you get lost or
- 1:05:09whatever. Um of course for me it's
- 1:05:10installed already. Um and let's go ahead
- 1:05:12and take a look at what we can actually
- 1:05:13do. So the premise of how we actually
- 1:05:15build these interactive UIs is every
- 1:05:18single thing we have like an input for
- 1:05:20visually corresponds with an actual
- 1:05:22parameter into a Python function. So
- 1:05:25before we do this with this sort of
- 1:05:27actual chart, let's just do this in
- 1:05:29general for an extremely basic Python
- 1:05:31function, right? So let's just define a
- 1:05:33function literally just funk defaf and
- 1:05:35let's just say n for a number is the
- 1:05:38input. Um, and all this is going to do
- 1:05:40is it's just going to return, let's just
- 1:05:42say like n minus two. We're just going
- 1:05:44to double a number. Obviously, if you go
- 1:05:46ahead and run funk of number five, we're
- 1:05:48going to get back 10. Right? This is all
- 1:05:50we're doing. And let's see how we can
- 1:05:52actually plot this interactively. So,
- 1:05:54let's go ahead and we're going to do
- 1:05:55widgets. And at first, we're just going
- 1:05:57to use a method called interact. Um,
- 1:06:00next time we use this on the actual
- 1:06:01plot, I'm going to use interactive
- 1:06:03instead of interact. They're basically
- 1:06:05the same thing. This is just going to
- 1:06:06handle sort of everything for us. And
- 1:06:08then we go ahead and we hand to
- 1:06:10widgets.inact the function that we want
- 1:06:13to make interactive such as funk. Um,
- 1:06:16now if we just do this, we're actually
- 1:06:17going to get an error. Let's go ahead
- 1:06:19and run this. It's basically just going
- 1:06:20to say, you know, it can't find uh a
- 1:06:23widget for n. Um, all we need to do is
- 1:06:25we either need to default n. Um, or we
- 1:06:28can do something else. So if we default
- 1:06:30n and we run this, boom, right? We now
- 1:06:33have a slider for n. it defaults to 10
- 1:06:36and the output here is 20. And like this
- 1:06:38is literally all the code we need,
- 1:06:40right? The widget. Interactive function
- 1:06:41does everything for us. If we slide this
- 1:06:43up, we can now see it's changing in real
- 1:06:45time. Um, and then if we don't want to
- 1:06:47default this, we can actually have a
- 1:06:49little more granular control. We can
- 1:06:50essentially just hand it the argument
- 1:06:53called n. And now we can go ahead and
- 1:06:55this is actually a tpple that we hand
- 1:06:56it. And essentially what goes into this
- 1:06:58tpple is like start stop. So let's say
- 1:07:01we want the minimum to be zero and we
- 1:07:03want the max to be 100. That means when
- 1:07:06we run this uh or let's run this that
- 1:07:08means that we uh the default is going to
- 1:07:09be the midpoint in between zero and 100
- 1:07:11and you know zero is the cap and 100 is
- 1:07:14the maximum. And that's all that means.
- 1:07:16Now if you notice this whole time as we
- 1:07:18did stuff in this notebook, anytime we
- 1:07:20run a function when it's done running
- 1:07:22we're going to see that check mark and
- 1:07:24while it's running we're going to see
- 1:07:25that sort of swirling thing. Every
- 1:07:27single time we slide the slider, we're
- 1:07:29actually calling funk with a new end. So
- 1:07:31anytime we rerun this, we can actually
- 1:07:33see that sort of thing executing. That's
- 1:07:35just because we're rerunning the
- 1:07:37function. Um and then the third sort of
- 1:07:39thing in here, uh this is going to be
- 1:07:40useful later on. Um is actually the
- 1:07:42step. So if we make the step two, that
- 1:07:45means that you know 50 is going to jump
- 1:07:46up to 52 54 in this case. You know, we
- 1:07:49can only run funk now on like an even
- 1:07:51number. You know, we can sort of get
- 1:07:52into this later. Um but that's really
- 1:07:54all there is to this. Okay, so let's go
- 1:07:56ahead and let's actually do this. This
- 1:07:58is going to be like the best part of the
- 1:08:00video by far. Um, so let me define a new
- 1:08:02function. I don't need to. I could just
- 1:08:04use the um use the existing function,
- 1:08:06but I want to show you guys a couple
- 1:08:07extra things in addition to the
- 1:08:09interactive plotting. Um, so I'm going
- 1:08:11to call this um interactive geometric
- 1:08:14brownie in motion. Um, and this is the
- 1:08:16function that eventually I will hand to
- 1:08:18the IPI widgets. So let's go ahead and
- 1:08:20expose only the parameters that we
- 1:08:23actually want to change. Right? So, we
- 1:08:25probably want to change the moo. We can
- 1:08:27just default that again. And we probably
- 1:08:29want to change the sigma. Um, we can
- 1:08:32also default the sigma. And it's really
- 1:08:34up to us how many things we want actual
- 1:08:36sliders for. Um, in this case, I would
- 1:08:39say the number of years. Sure, let's
- 1:08:41also add that. Um, and then definitely
- 1:08:43the number of scenarios. Um, let's just
- 1:08:45default that to 100. Let's also add the
- 1:08:47number of scenarios. Um, and then maybe
- 1:08:49we also just want to change, you know,
- 1:08:50the initial wealth. So, maybe we also
- 1:08:52want to expose S0, right? Um, I mean, in
- 1:08:55this case, this is literally just
- 1:08:56everything besides the steps per year.
- 1:08:58I'm just going to leave that out for
- 1:08:59now. Again, there's no right answer.
- 1:09:01This is just what we actually want to
- 1:09:02expose in the UI. And this function is
- 1:09:05just going to be a wrapper for the
- 1:09:06actual GBM function. Um, so the last one
- 1:09:09that we left in memory is this one. This
- 1:09:11is the one that we said we're happy
- 1:09:12with. So, let's just go ahead and let's
- 1:09:15just call GBM. This is the fully working
- 1:09:18good GBM function. And let's just go
- 1:09:20ahead and hand it right number of years.
- 1:09:24And let's hand it number of scenarios.
- 1:09:28This might be kind of boring. Let me
- 1:09:30just get this done. Mu equals mu. Sigma
- 1:09:33equals sigma. Um, and s0 equals s0. And
- 1:09:37then, you know, the only thing we're
- 1:09:38kind of choosing to leave out is the
- 1:09:40steps per year. That will just be
- 1:09:41defaulted to 12. Again, it's fully up to
- 1:09:43you what you want to expose to the UI.
- 1:09:44We could just make everything exposed to
- 1:09:46the UI. And before we actually go ahead
- 1:09:48and do interactive plotting, let me just
- 1:09:51show you some more really cool things we
- 1:09:52can do within mapplot lib. So let's just
- 1:09:55go ahead and make this function fully
- 1:09:56self-contained for plotting. Um so
- 1:09:59again, let's just go ahead and save this
- 1:10:00into or technically it won't be rats
- 1:10:02anymore now that we're actually
- 1:10:04returning um the wealth index after the
- 1:10:06cumroad. Technically these are now
- 1:10:08prices not returns, right? Just good to
- 1:10:09make the distinction. Um and let's go
- 1:10:11ahead and let's just plot these. So
- 1:10:14let's just go ahead and do prices.plot
- 1:10:16plot and you know we kept needing to
- 1:10:18make legend false and fig size. Let's
- 1:10:20just go ahead and do these inside the
- 1:10:22function. Um and now when we call this
- 1:10:24function we're just going to get that
- 1:10:25graph directly. And now I'm just going
- 1:10:28to introduce something within mapplot
- 1:10:30lib. So so far in this video we've just
- 1:10:32been doing plot and it works perfectly
- 1:10:34great by default. Um but let's go ahead
- 1:10:36and sort of interface with these graphs
- 1:10:38a little more. It's actually like
- 1:10:40surprising how powerful mapplot lib
- 1:10:43actually is in terms of customization.
- 1:10:46So, anytime we doplot, obviously, it
- 1:10:48essentially just outputs the plot. Um,
- 1:10:50but it's actually going to return
- 1:10:52something. And the thing it's going to
- 1:10:54return, it's kind of confusing. I'm
- 1:10:56going to talk about more. Um, I'm I'm
- 1:10:57going to talk about this a little more
- 1:10:59in a second, but we have two things to
- 1:11:00be aware of in Maplot lib. We have
- 1:11:02something called a figure and something
- 1:11:04called an axis. And the figure you can
- 1:11:06think of as being like the entire plot.
- 1:11:09And then the axis in this case does not
- 1:11:11mean like the x or y axis. This is not
- 1:11:14the same thing as setting axis equal to
- 1:11:16zero or one within pandas or numpy. This
- 1:11:18is an unrelated concept of an axis where
- 1:11:20the axis just means like a whole chart.
- 1:11:23Now, so far there's only been one axis
- 1:11:26per figure, but eventually we're going
- 1:11:28to be actually doing two axises on a
- 1:11:31figure. I know that sounds confusing.
- 1:11:33Um, you'll see what I mean in just a
- 1:11:34second, right? But in the intro, I
- 1:11:36probably would have showed you a stock
- 1:11:37chart that looked like this and a
- 1:11:39histogram. And you know, the stock chart
- 1:11:41is an axis. the histogram is an axis and
- 1:11:43then both being plotted on the same
- 1:11:45figure. The figure sort of includes both
- 1:11:48of them. Um so it turns out that the
- 1:11:50thing that this returns is actually an
- 1:11:52axis like a mapplot lib um axis object.
- 1:11:55We generally just use ax as the variable
- 1:11:58name. Of course you can use whatever you
- 1:11:59want. Um in this we now actually have a
- 1:12:01reference to the axis. And what we can
- 1:12:03do in this case is we can actually add
- 1:12:05things to it. So let's say I just want
- 1:12:08like a dotted line or a dashed line just
- 1:12:10statically a horizontal line going
- 1:12:12through one or going through 100 on the
- 1:12:15y-axis just so I can sort of visualize
- 1:12:18how many you know stocks sort of made
- 1:12:19money and how many lost money over the
- 1:12:21sample. Now of course you know this
- 1:12:23video could easily be 500 hours long.
- 1:12:25You know a static line's cool. How about
- 1:12:27a line where we actually superimpose
- 1:12:29inflation like the CPI? like you know we
- 1:12:32could actually like pull live CPI data
- 1:12:35and then sort of plot that and then we
- 1:12:37can calculate summary stats of like how
- 1:12:39many trials beat inflation and like
- 1:12:41obviously there's so much stuff we can
- 1:12:42do um but we're just going to be keeping
- 1:12:44the scope um limited in this video um so
- 1:12:46let's go ahead and we're going to
- 1:12:48reference um this ax right this axis
- 1:12:50object that we have and again there's no
- 1:12:52way you would know to do this unless you
- 1:12:54looked up documentation but it turns out
- 1:12:56that there's a function called ax h is
- 1:12:59for horizontal and then line just like
- 1:13:02axis horizontal line. And this is just a
- 1:13:04function that's going to add a
- 1:13:05horizontal line onto this axis, which
- 1:13:08just means the entire chart. Um, and the
- 1:13:10only thing we're going to need to hand
- 1:13:11this is a y, which is just where we want
- 1:13:13to plot it. Um, and in this case, we
- 1:13:15just want it to be centered at s0. So,
- 1:13:18if you go ahead and run this function
- 1:13:19and run this, boom, we can now see that
- 1:13:21we have a static line going through um,
- 1:13:24s0, which is good. Um, but it doesn't
- 1:13:26really look too good. Um, so again, you
- 1:13:29know, you can read the documentation and
- 1:13:30you can learn about all the different
- 1:13:32sort of styling options we have
- 1:13:34available to us. Um, one of them is
- 1:13:35called ls. Pretty sure that stands for
- 1:13:37like a line style. And we can go ahead
- 1:13:39and we can make this equal to any number
- 1:13:41of things. Um, I'm pretty sure if we do
- 1:13:42a colon, it will actually now be a
- 1:13:44dotted line. Sure enough, that's dotted.
- 1:13:47Um, what I like to do is I like to do um
- 1:13:49a dashed line. I just think it looks
- 1:13:51better. Obviously, this is just in my
- 1:13:52opinion. Um, and now let's actually make
- 1:13:54it visible, um, by doing the color. Um,
- 1:13:58and let's just set the color equal to
- 1:14:00black. And this should probably look
- 1:14:01more visible now. And there we go. Right
- 1:14:03now, this actually looks good. Um, you
- 1:14:05know, I'm just sort of showing you the
- 1:14:07actual power of mapplot lib. Um, in my
- 1:14:09opinion, this is pretty good. Now, we
- 1:14:10can go ahead and move on to making this
- 1:14:12interactive. So, this should be pretty
- 1:14:14easy. So, we're just going to need to do
- 1:14:16widgets. In this case, I'm going to use
- 1:14:19um interactive instead of interact.
- 1:14:21Again, you're free to read the
- 1:14:22documentation later. Um, and all we need
- 1:14:25to really do is just hand it that
- 1:14:27interactive GBM function. And there are
- 1:14:29going to be some issues with this. Um,
- 1:14:31though, let's actually work through them
- 1:14:32in real time. So, it looks good. It's
- 1:14:35that same chart except all the things we
- 1:14:38exposed as a Python parameter in the Py
- 1:14:40um in the Python function are now
- 1:14:41available to us. But there's going to be
- 1:14:43some issues here, right? Because if we
- 1:14:45take, you know, s0, this can actually
- 1:14:48become negative, which, you know, it
- 1:14:50technically doesn't break the Python
- 1:14:52code, but we probably don't want a
- 1:14:54negative starting. Um, though, however,
- 1:14:57if number of scenarios goes negative,
- 1:14:59obviously, we're going to get an issue.
- 1:15:00Negative dimensions are not allowed,
- 1:15:02which is fully expected. Um, so let's
- 1:15:03just go ahead and sort of clean this up
- 1:15:05for ourselves. Um, so this might be kind
- 1:15:07of boring, but I'm still going to do
- 1:15:08this in real time just for just for the
- 1:15:10people that are following along that
- 1:15:11might want to see this. Um, I'm just
- 1:15:12going to, you know, paste this in here
- 1:15:14just to just so I don't waste time
- 1:15:15typing. Um, and then number of years.
- 1:15:17All of these are going to be a tpple.
- 1:15:18Remember, it's sort of like start, stop,
- 1:15:20step. Um, so let's say we want the
- 1:15:22number of years to at minimum be a one.
- 1:15:24And let's just cap it at 30. Um, that
- 1:15:26will do number of scenarios, you know,
- 1:15:28let's do between one and maybe let's say
- 1:15:31like a,000. Uh, moo and sigma, let's do
- 1:15:34these as well. Um, let's say the MOO, we
- 1:15:36want to go from negative, you know, 10%,
- 1:15:39which is just 0.1 to positive 0.1. Uh,
- 1:15:43sigma, of course, we don't want that
- 1:15:44going negative. So, let's say the sigma
- 1:15:46can go from zero to maybe like 0.3, 30%.
- 1:15:50Um, and then for this, let's just go
- 1:15:52ahead and say maybe $100 to $1,000. Now,
- 1:15:56let's run this. And it looks good. Um,
- 1:15:59except there's going to be a slight
- 1:16:01issue here. Let's take a look at like MO
- 1:16:03and Sigma. If we move down the MOO, we
- 1:16:05sort of messed up the step. Um, right,
- 1:16:07we didn't specify step on any of these.
- 1:16:09Essentially, how it works is whatever
- 1:16:10sort of number it sort of defaults to,
- 1:16:12that's just going to be the step. So,
- 1:16:14because the MOO is like 0.1 is just
- 1:16:16going to use 0.1 as the granularity. We
- 1:16:19now sort of lost ability. Um, so let's
- 1:16:21just go ahead and say, you know, 0.01
- 1:16:24for the MOO. Um, same thing with the
- 1:16:26sigma. We're going to make that 0.01.
- 1:16:28Um, and with all that being said, now we
- 1:16:31should be good. So let's go ahead and
- 1:16:33actually sort of play around with this,
- 1:16:35right? So let's just take a look. So
- 1:16:36number of years is equal to 10. Now,
- 1:16:38right now, you know, the winning sort of
- 1:16:40stock closes out at, you know, maybe
- 1:16:42like 500 bucks or something. If we
- 1:16:44increase the sample, we should start to
- 1:16:46see terminal wealths that are obviously
- 1:16:48higher. Right? Now the axis goes up to
- 1:16:50800. Right? If we actually make this 30,
- 1:16:53now we can end a 30-year sample with
- 1:16:54like, you know, $6,000, right? Instead
- 1:16:56of $500, right? We're just sort of
- 1:16:58getting some intuition um as to how this
- 1:17:00actually works. Sure enough, it looks
- 1:17:02like it's working. Um, now the MOO,
- 1:17:04right, the average stock obviously
- 1:17:06closes above this dotted black line
- 1:17:08since we have a positive expected
- 1:17:09return. If we make this flat, it's going
- 1:17:11to look somewhat more flat. And if we
- 1:17:13make this negative, right, if we make
- 1:17:15this, you know, negative 10% a year,
- 1:17:17most of the time we're going to close
- 1:17:18below the black line. If we now increase
- 1:17:20the number of years, right, they're now
- 1:17:22all going to start to close below the
- 1:17:24black line. So, this is, you know, this
- 1:17:26is how we can actually sort of adjust
- 1:17:27these parameters to our liking. Um,
- 1:17:29Sigma is also really cool. If we lower
- 1:17:31sigma, it's actually going to, you know,
- 1:17:33converge on more or less a straight
- 1:17:35line, right? They're all going to almost
- 1:17:37look like one line. If you go and make
- 1:17:39this, you know, 0.01,
- 1:17:41right? You can now see we're sort of
- 1:17:43like converging on the average path. Um,
- 1:17:46and you can see it's not like straight.
- 1:17:47It's actually curved because of the way
- 1:17:49like compounding works. Um, same thing
- 1:17:51for when the moo's positive, right? It
- 1:17:53sort of curves up exponentially. Um, so
- 1:17:55yeah, so we're basically done the
- 1:17:57function. Um, and now what I'm going to
- 1:17:58do, probably last thing, is I'm going to
- 1:18:01go ahead and we're going to add a
- 1:18:02histogram so we can visualize um a
- 1:18:05histogram of terminal wealth. So, this
- 1:18:08is going to be a lot of fun. This is
- 1:18:11probably going to be like the highlight
- 1:18:12of the video. Um, and again, as I said,
- 1:18:14this video could literally be like a
- 1:18:15thousand hours long. Um, I need to pick
- 1:18:18some spot to end off. I think I'll just
- 1:18:19do the histogram and then I'll probably
- 1:18:21just call it a day. So, we're going to
- 1:18:23be just sticking with this function, but
- 1:18:24we're going to be heavily modifying it.
- 1:18:26And this is like just great practice for
- 1:18:29you know working with like mapplot lib
- 1:18:30and stuff. Um so now we're going to be
- 1:18:33essentially calling like a subplot
- 1:18:35function. I'm going to explain all of
- 1:18:36this. So you probably would have seen in
- 1:18:38the intro of the video how this works.
- 1:18:40We're going to be plotting two different
- 1:18:41things. One of the things is we're still
- 1:18:43going to be plotting the prices. We can
- 1:18:45just leave that variable intact. That's
- 1:18:46completely okay. And then the second
- 1:18:48thing is we want a histogram of terminal
- 1:18:49wealth as I said. So let's go ahead
- 1:18:52right before we worry about plotting. We
- 1:18:54obviously just need the data to plot in
- 1:18:55the first place. Um, so we have prices
- 1:18:58and now we have terminal wealth. And we
- 1:19:00actually did this earlier in the video
- 1:19:02during that experiment we did. If you
- 1:19:04think about how we actually get terminal
- 1:19:05wealth, um, it's literally just going to
- 1:19:07be prices integer location of -1. Um,
- 1:19:12we're literally, you know, just grabbing
- 1:19:13the very last price on the very last
- 1:19:15time step and that is the terminal
- 1:19:17wealth since the whole thing's a wealth
- 1:19:19index. Um, should be pretty
- 1:19:20straightforward so far. So prices and
- 1:19:22terminal wealth is what we're actually
- 1:19:24going to be plotting. So so far in this
- 1:19:27video, everything we did in terms of
- 1:19:29plotting, we just used the dotplot
- 1:19:31method for on a pandas object. And you
- 1:19:34know, even though this returned another
- 1:19:35mapplot lib object that we used another
- 1:19:36method on, we didn't actually need to
- 1:19:38import mapplotib into our notebook yet,
- 1:19:41right? It's actually kind of funny. We
- 1:19:42used it so much without even importing
- 1:19:44it. Um, now we're going to need, you
- 1:19:46know, more sort of fine grain control
- 1:19:48within mapplot lib to do all this
- 1:19:50histogram stuff, which I think is so
- 1:19:51interesting. Um, so let's go ahead and
- 1:19:53we're actually going to need to import
- 1:19:54it since we are going to need to use the
- 1:19:56subplots method. So we're just going to
- 1:19:58say import mapplot lib um pipplot. This
- 1:20:01is just what we've been using. Um, and
- 1:20:03then we generally just say as plt for
- 1:20:06plot. That's just what we do. Make sure
- 1:20:08you run it. Um, and we should be good
- 1:20:10now to get started. Um so what we're
- 1:20:12going to be doing right is we're going
- 1:20:13to be plotting two different things um
- 1:20:15on the same figure right two plots on
- 1:20:16the same figure and we're going to need
- 1:20:18a special function now that lives right
- 1:20:20within the map lib library and it's
- 1:20:21called subplots. So let's just go ahead
- 1:20:23and call subplots and this is how we can
- 1:20:26sort of set up our figure. Um again
- 1:20:29there's no way you would know to do this
- 1:20:30if you didn't read documentation. I'm
- 1:20:32just sort of going to like walk you
- 1:20:33through what we're doing and then as
- 1:20:35soon as I plot it will look a lot more
- 1:20:37clear. Um, so one of the parameters we
- 1:20:39need for this this figure of multiple
- 1:20:42plots, you can essentially think of this
- 1:20:44as almost as if you're building like a
- 1:20:45dashboard, right? We could have 10
- 1:20:47different plots on this figure. We can
- 1:20:49really have whatever we want. Um, and
- 1:20:51they're organized essentially in a grid
- 1:20:53layout. So we actually need to specify
- 1:20:54the number of rows and the number of
- 1:20:56columns where, you know, every single
- 1:20:59thing within these rows and columns are
- 1:21:01entire plots like a whole line graph, a
- 1:21:03whole histogram um, are going to be
- 1:21:05organized into this grid, right? It's
- 1:21:06super useful. Um, in this case, there's
- 1:21:08only going to be two different things.
- 1:21:10So, we're going to have one row and
- 1:21:12we're going to have two columns. End
- 1:21:13call is going to be equal to two. Um,
- 1:21:16and what this entire dots subplots
- 1:21:18function returns, it's a little
- 1:21:20confusing, but it's going to return two
- 1:21:22different things. So, we can go ahead
- 1:21:23and unpack this um directly. The first
- 1:21:26thing is going to be a figure. And we're
- 1:21:28not actually even going to use the
- 1:21:30figure. It's just going to be returned,
- 1:21:32right? It's going to return two things.
- 1:21:33So, we're unpacking. Um, and then the
- 1:21:35second thing is actually going to be a
- 1:21:36tpple. And then each element in the
- 1:21:39tpple is going to be the axis for the
- 1:21:42specific plot. In this case, there's
- 1:21:44going to be two, one for prices and one
- 1:21:46for terminal wealth. So, we can actually
- 1:21:48go ahead and call this, you know, wealth
- 1:21:52axis. And then let's just go ahead and
- 1:21:54call the other one, um, let's just say
- 1:21:56hist axis for histogram. Right? So, we
- 1:21:59have two different axises coming out of
- 1:22:00here. And we're going to see how this
- 1:22:01looks in just a second. Now, just so we
- 1:22:03can gain a little bit of intuition um as
- 1:22:05to what we're working with as we write
- 1:22:07this code, um we don't need to worry
- 1:22:08about this for now. Let's just go ahead
- 1:22:10and simply just print out the entire
- 1:22:12figure. Um in this, there's not going to
- 1:22:14be anything populated right now, but
- 1:22:16this is sort of going to let us know
- 1:22:17what's going on. When we said one row by
- 1:22:19two columns, we now sort of have these
- 1:22:21two different figures that are on the
- 1:22:23same row. They're side by side. um just
- 1:22:25for the intuition if we instead said
- 1:22:28number of rows is two and number of
- 1:22:29columns is one now now these would be
- 1:22:32stacked on top of each other right so
- 1:22:34this is sort of like you know we're just
- 1:22:35building plots in a grid um and then
- 1:22:37we're just going to you know keep on
- 1:22:38doing this until we have a beautiful
- 1:22:40looking graph so this is what we have
- 1:22:42right now so this is going to become
- 1:22:43more clear once we actually plot the
- 1:22:45data um you would have seen this in the
- 1:22:47intro of the video though um by default
- 1:22:49right we're not sharing the x or y axis
- 1:22:52we can see you know we have an x axis
- 1:22:53and a y- axis for both of these figures.
- 1:22:56Um, for the histogram, we're going to
- 1:22:57see when we add the data, but we
- 1:22:59actually want to share the yaxis, which
- 1:23:01means we only want to use one y-axis.
- 1:23:03Um, it turns out there's actually
- 1:23:04something called share y, and we can
- 1:23:07just go ahead and set that equal to
- 1:23:08true. And we're going to go ahead and,
- 1:23:09you know, show what changed after I add
- 1:23:11in each argument. Um, now we can see,
- 1:23:13right, it's the same thing, but we now
- 1:23:15only have a single y-axis, um, since
- 1:23:17we're sharing it. And of course, we
- 1:23:19probably just want to make these bigger.
- 1:23:20Um, so we can go ahead and do fix size
- 1:23:23on this directly. Um, we're going to
- 1:23:24make this a little bigger than the uh
- 1:23:26126 I did before. Let's make this maybe
- 1:23:29like 248 or 2410 should be good. Let's
- 1:23:33see. Uh, yeah, that should be fine. Just
- 1:23:35cuz, you know, this is the figure um for
- 1:23:37both plots combined. So, it definitely
- 1:23:39will need to be bigger. Um, now I just
- 1:23:41realized we should probably actually put
- 1:23:42the data on the plot before we continue
- 1:23:45cleaning it up just so you can see what
- 1:23:47I'm actually talking about. So, let's go
- 1:23:49ahead and actually populate these empty
- 1:23:51graphs with the data. Um, so let's go
- 1:23:54ahead and do prices first. So, we're
- 1:23:56still just going to do prices.plot, but
- 1:23:58we need this to not, you know, just be
- 1:24:00floating out of nowhere. We need to
- 1:24:02specify that we want this specifically
- 1:24:04to be right here. Um, so how we do this
- 1:24:06is there's actually a parameter called
- 1:24:08ax for axis and we're going to specify,
- 1:24:11right, that this prices variable, which
- 1:24:13is just a data frame. It's what our own
- 1:24:15function outputs as a data frame. We
- 1:24:17want to plot this and the location is
- 1:24:20going to be on this axis called wealth
- 1:24:22axis which is this sort of empty plot
- 1:24:24here. Um now if I just go ahead and run
- 1:24:26the cell this should work. Let's take a
- 1:24:28look. Sure enough it does work. Of
- 1:24:30course we're have a have a bit of
- 1:24:32cleanup to do but right this is just how
- 1:24:34we plot the data. Um now we already know
- 1:24:36that we probably specifically within
- 1:24:38this want to do legend equals false.
- 1:24:41Let's go ahead and do that as well. Um
- 1:24:43and we should be good. Um, and then last
- 1:24:46thing is this rainbow stuff might be a
- 1:24:48little much once we have both plots. So,
- 1:24:50we can go ahead and just use the color
- 1:24:52parameter color equals. And we can set
- 1:24:54this to whatever we want. Um, in this
- 1:24:56case, I might just do like blue. Um,
- 1:24:58though blue, I believe, yeah, blue is
- 1:25:00like incredibly sort of dark and
- 1:25:02saturated. Most colors are. Um, let me
- 1:25:04just leave it on like sky blue. And we
- 1:25:06should be good to call it a day. Doesn't
- 1:25:08look that bad. Um there's also another
- 1:25:09parameter called like alpha which um if
- 1:25:12you want it to sort of be a little like
- 1:25:14transparent we can make alpha 0.5 and
- 1:25:17then it will be more faded. You know we
- 1:25:18don't want that or anything. Um but you
- 1:25:20know I would advise you read the
- 1:25:21documentation. You know there's like a
- 1:25:23lot of different ways we can customize
- 1:25:24this. So now we're at least plotting um
- 1:25:26our stocks. Um and now what I'm going to
- 1:25:28do is I'm just going to add that
- 1:25:29horizontal line back that I got rid of.
- 1:25:32Um so we can do that in a very similar
- 1:25:33way. we can just go ahead and call you
- 1:25:35know the wealth axis directly and then
- 1:25:37we can do the ax h for horizontal line
- 1:25:41um where y is equal to s0 and then ls
- 1:25:45for line style um I believe we did a
- 1:25:48double dash for dash line and then let's
- 1:25:50just make the color for this completely
- 1:25:52black um because we're calling you know
- 1:25:54this method on wealth axis it will be
- 1:25:57within this plot um so we should be good
- 1:25:59um and now let's go ahead and focus on
- 1:26:01actually plotting the terminal wealth
- 1:26:02histogram so let's go ahead and do that
- 1:26:04here. Let's go ahead and do terminal
- 1:26:06wealth. Um, and we're going to do plot.
- 1:26:09But in this type, right, by default, we
- 1:26:11just have this like nice looking line
- 1:26:12graph for the stocks, right? Um, but
- 1:26:14what we actually want to do is we want
- 1:26:15to do histogram. Lucky for us, of
- 1:26:17course, mapplot lib just has a histogram
- 1:26:19method directly. We're just telling it
- 1:26:21what type of plot we actually want. Um,
- 1:26:23and then same thing, ax. In this case,
- 1:26:26we don't want to plot it on this plot.
- 1:26:27We want to plot it on this plot, which
- 1:26:29we're calling hist ax. Um, now we're
- 1:26:32gonna change this up a bit, but let's go
- 1:26:33ahead and see what we have.
- 1:26:35Now, it doesn't look the best, but it
- 1:26:37still is working, right? This is a
- 1:26:39histogram, but you know, we're sort of
- 1:26:41sharing the axis. The whole thing's
- 1:26:43messed up. Let's go ahead and clean this
- 1:26:44up. Now, um, so one of the things we
- 1:26:46want to do is we don't want to plot this
- 1:26:48horizontally. We sort of want it to look
- 1:26:50nice where the stock comes up here and
- 1:26:52then the bar just comes out of the side,
- 1:26:54not out of the bottom. Um, that's a
- 1:26:56super easy change. We just have a
- 1:26:58parameter called orientation and we can
- 1:27:02just set that equal to horizontal.
- 1:27:06Should work. Let's give this a try.
- 1:27:08Perfect. Right now, this is starting to
- 1:27:10look better. So, we still have some
- 1:27:12further adjustments to make. Um, one of
- 1:27:14the very annoying things is there's this
- 1:27:15massive gap in between the two plots.
- 1:27:17Um, it turns out that we can actually
- 1:27:19get rid of that quite easily. For that,
- 1:27:21we need, and again, you would need to
- 1:27:22read the documentation. You wouldn't
- 1:27:24know how to do this. Um, but we can do
- 1:27:26pls
- 1:27:27subplots
- 1:27:29adjust. Um, and when we do this, we're
- 1:27:31not, you know, using one or the other
- 1:27:33axis. We're sort of using like the
- 1:27:35overarching sort of general um general
- 1:27:38like entire figure. Um, and we're going
- 1:27:39to go ahead and we're going to use an
- 1:27:41argument called wp space, which just
- 1:27:43means like whites space. And we're just
- 1:27:44going to set that equal to zero. And
- 1:27:46we're going to go ahead and run this and
- 1:27:48run this. And there we go. Right now
- 1:27:50they're like perfectly touching each
- 1:27:52other. Um, there's no space in between
- 1:27:53them. So, you know, let's keep on going
- 1:27:55here. Um, one of the things that right
- 1:27:57now is annoying me is, you know, this is
- 1:27:59taking up like 50% of the width and
- 1:28:01that's taking up 50% of the width. Um,
- 1:28:03you know, I'd way rather have the line
- 1:28:05sort of be maybe there like this sort of
- 1:28:08center line. I want more space allocated
- 1:28:10to the actual stocks and less to the
- 1:28:11histogram. Um, we can go ahead and do
- 1:28:13this. There's a special parameter. I
- 1:28:15actually had to open the documentation
- 1:28:16to look at it. Um, but let's go ahead
- 1:28:18here. Um, and it's going to be called
- 1:28:20gridspec. There we go. gridspec keyword
- 1:28:24and the specific keyword, right? This is
- 1:28:25going to be dictionary. The specific
- 1:28:26keyword that we're going to be using um
- 1:28:28is actually called width ratios. And
- 1:28:31this width ratios, oops, that was weird.
- 1:28:34Um this width ratios is going to be a
- 1:28:37Python list. Um and the list is
- 1:28:39essentially just going to be the ratio
- 1:28:41between one and the other. So if we do
- 1:28:43like 32, that means that the line chart
- 1:28:45is going to be like three units relative
- 1:28:48to two units. Um if you run Oops. If you
- 1:28:50run this, we should see this line sort
- 1:28:52of jump over that way. And there we go.
- 1:28:55Sure enough, that works. Um, in this
- 1:28:56case, I'm actually just going to make it
- 1:28:58double the space. So, if I say 21, that
- 1:29:00means that the stock area is going to be
- 1:29:02double the area of the histogram. Um,
- 1:29:04and there we go. Sure enough, that's
- 1:29:06what we have. So, we could call this a
- 1:29:08day. This isn't that bad, but the
- 1:29:10histogram is not that granular. It looks
- 1:29:13pretty bad. we probably want more bins
- 1:29:15so we have more granular uh more
- 1:29:18granular like results sort of visually
- 1:29:20in the histogram. Um so we can go ahead
- 1:29:22and there's literally just going to be a
- 1:29:24parameter called bins and we can make
- 1:29:27this maybe like 50 should be good. Let's
- 1:29:29go ahead and see. There we go. Right,
- 1:29:31that's what I'm talking about. We don't
- 1:29:32want those bars extremely thick. Um
- 1:29:34let's do, you know, 50 bins. Um this
- 1:29:36isn't bad. Um, now another thing that's
- 1:29:38annoying me is this doesn't really look
- 1:29:41that much like a histogram to me just
- 1:29:43cuz all these bars are kind of like
- 1:29:44merging together. Um, in my opinion,
- 1:29:46this doesn't look the best. Of course,
- 1:29:48this part is like completely subjective,
- 1:29:50right? We're just styling the graph.
- 1:29:51It's already looking pretty pretty good.
- 1:29:53Um, but we can go here and there's
- 1:29:55something that's going to be called EC,
- 1:29:57which means edge color. And if we set
- 1:29:59the edge color equal to the background
- 1:30:01color, which is white, it's now going to
- 1:30:04draw a little sort of white edge around
- 1:30:06each bar. And it's basically going to
- 1:30:08look exactly like that. Um, and last
- 1:30:11thing, let me just make the color also
- 1:30:14equal to sky blue. Um, of course, we
- 1:30:16could make it a different color. Let me
- 1:30:18just see. That should be pretty good.
- 1:30:20How does this look? Not bad, right?
- 1:30:23That's actually pretty good so far. Um,
- 1:30:25now obviously, you know, we could keep
- 1:30:26on going and we could keep on styling
- 1:30:28this. You know, for the thumbnail of
- 1:30:30this video, I'm probably going to do
- 1:30:31some variant where I make the background
- 1:30:33dark and then make all of these, you
- 1:30:35know, some color set that's like rainbow
- 1:30:36or something, right? You know, there's
- 1:30:38really no limit to what we can do. Um,
- 1:30:39but this is this is pretty good, right?
- 1:30:41I I think this is good for this video.
- 1:30:43And of course, you know, now we can go
- 1:30:45ahead and and mess around with this,
- 1:30:46right? Again, if we lower the sigma,
- 1:30:48right? There's not much not much uh
- 1:30:50standard deviation. We can increase
- 1:30:52this, right? We can increase number of
- 1:30:53scenarios now to a thousand, right? And
- 1:30:55like look how cool this is, right? Like
- 1:30:57we can now see how that Z of T variable,
- 1:31:00right? These are random standard normal
- 1:31:02Gaussian variables. We actually more or
- 1:31:04less have like a normally distributed,
- 1:31:06right, with a mean and a sigma, but this
- 1:31:08normally distributed sort of terminal
- 1:31:10wealth. Um, and we're done, right?
- 1:31:12That's really that's really all there is
- 1:31:14for the video. Now, obviously, you know,
- 1:31:16we could just keep on adding things and
- 1:31:18keep on changing things, right? Um, in
- 1:31:20that first Quant Finance video, I
- 1:31:21brought up like draw downs. We could
- 1:31:23actually go ahead and calculate the max
- 1:31:25draw down for each of these simulations
- 1:31:28and, you know, even plot the draw downs
- 1:31:29or say, you know, which one had the most
- 1:31:31devastating draw down. Um, there's also
- 1:31:33Maplot annotations which I brought up
- 1:31:35last time as well. We can even put
- 1:31:38numbers like directly on the graph. So
- 1:31:40like draw down, summary stats, right?
- 1:31:42Like the mean and median, return, and
- 1:31:44all like all the stuff we could
- 1:31:45literally add in text right on the
- 1:31:47graph. it would change this number and
- 1:31:49it would change in real time. You know,
- 1:31:51like originally I wasn't even going to
- 1:31:52add the histogram into the video. The
- 1:31:54histogram is kind of too cool to not
- 1:31:56add, but this is how we can actually use
- 1:31:58Python and use a couple of these
- 1:32:00libraries to actually start doing like
- 1:32:02actual analysis, right? So, yeah. So,
- 1:32:05that's really all there is for this
- 1:32:07video. You know, this easily could have
- 1:32:09been a thousand hours long, right? I
- 1:32:10need to stop somewhere. Um, now, you
- 1:32:12know, if there's interest, obviously
- 1:32:14I'll keep the series going, right? Let
- 1:32:15me know what you think. Um, for next
- 1:32:17video I was thinking I might actually do
- 1:32:19another stochcastic modeling video, but
- 1:32:21instead of, you know, stock market
- 1:32:23stochastic modeling, I might actually
- 1:32:25use the CIR model and then do stochastic
- 1:32:27modeling on bonds on fixed income. Uh,
- 1:32:30in fact, there's a lot of stuff we can
- 1:32:31do with fixed income. We can actually
- 1:32:33model, you know, zero coupon bonds in
- 1:32:35coupon bearing bonds and we can actually
- 1:32:37also get like the instantaneous rate as
- 1:32:39well. And, you know, we can use like
- 1:32:41oilers's number and, you know, we can
- 1:32:42actually like derive all of this stuff
- 1:32:44directly in Python. It's actually like
- 1:32:46fascinating. Um, and then in the intro
- 1:32:48of this video, um, I said that I was
- 1:32:50going to say a little something about
- 1:32:51factor aggressions. Um, let me go ahead
- 1:32:53and just talk about that now just
- 1:32:54because there was some interest on that.
- 1:32:56So, yeah. So, you know, just to give you
- 1:32:58maybe like a little maybe preview or
- 1:33:00whatever of what's to come. Um, I was
- 1:33:01saying in my asset pricing video at the
- 1:33:03very end of that super long video from a
- 1:33:05month or two ago, I was showing this
- 1:33:07website. It's not being hosted or
- 1:33:08anything. I'll probably fix it up and
- 1:33:10throw it back on production at some
- 1:33:11point in time. Um, I don't use it
- 1:33:12anymore. I built it many many many years
- 1:33:14ago when I was first getting started
- 1:33:16with quant. Um it was more of like a
- 1:33:18portfolio project. It basically just
- 1:33:20gave us like this UI that's hosted in
- 1:33:22the web and you can like save portfolios
- 1:33:24to your account and stuff, right? And
- 1:33:25you can plot all this stuff and you know
- 1:33:27this page just for back testing
- 1:33:28securities. You can already build this
- 1:33:30yourself if you watch that 9minute Quant
- 1:33:32Finance 101 video I made. It's actually
- 1:33:34really simple. Um in this case I'm
- 1:33:36actually using like plotty.js. It's it's
- 1:33:39actually something Let me actually pull
- 1:33:41that up. It's actually pretty
- 1:33:42interesting. So in the video that you
- 1:33:44just watched all of the plotting we did
- 1:33:45was done in mapplot lib which is just
- 1:33:48the default plotting backend that pandas
- 1:33:50uses. It turns out we can actually go
- 1:33:52into pandas right dot
- 1:33:53option.plotting.backend
- 1:33:55and this is what I did for that website.
- 1:33:56And we can actually change the default
- 1:33:58plotting engine and we can actually use
- 1:34:00any plotting library we want in Python
- 1:34:03not just mapplotib. Um and I use plotty
- 1:34:06for a couple different reasons in this
- 1:34:07project and this is like a deprecated
- 1:34:09project. This isn't a big deal or
- 1:34:10anything. Um it's just cool to note that
- 1:34:12this actually has like a plotty.js um
- 1:34:15CDN. So this means that when my backend
- 1:34:18does all this stuff, this plot, this
- 1:34:20whole interactive plot, all of the
- 1:34:21JavaScript that's being used here is
- 1:34:23actually served from Plotty to the users
- 1:34:26browser instead of from I was hosting
- 1:34:28this originally on a Raspberry Pi. So
- 1:34:30it's actually really slow and this
- 1:34:31actually helped to speed it up. It's
- 1:34:33actually, you know, it's pretty cool as
- 1:34:34all you know. Anyways, um, so just cuz
- 1:34:36some people were asking, just to give
- 1:34:38you a little sort of like sneak peek
- 1:34:39preview into how all this like Python
- 1:34:42stuff and quant stuff can actually be
- 1:34:43useful in the real world, just because
- 1:34:45stochastic modeling isn't the most sort
- 1:34:47of useful. Um, you know, obviously we
- 1:34:49have like rolling returns and stuff. We
- 1:34:51can really do whatever we want. I might
- 1:34:52even put this website back on prod and
- 1:34:54then update the website in real time
- 1:34:57with each Quant Finance video I make.
- 1:34:59So, you know, I might even have a tab
- 1:35:01called like stochastic modeling and
- 1:35:02stuff, right? Um, either way, let's say
- 1:35:04we want to do a factor regression, and
- 1:35:06this is just like a random thought. It
- 1:35:07doesn't really matter, but let's say we
- 1:35:08want to do a factor regression on two
- 1:35:10value ETFs, and we want to determine
- 1:35:13which value ETF is more exposed to the
- 1:35:15value premium cuz this isn't really
- 1:35:18something, you know, maybe it will say
- 1:35:19on like the fund website, but they're
- 1:35:21not really going to make claims like
- 1:35:22this. You sort of need to like gather
- 1:35:24this data yourself. So, let's say
- 1:35:26there's like, you know, one value ETF is
- 1:35:28called VLU, and I don't own these or
- 1:35:29anything, right? This is barely even an
- 1:35:31analysis. This is just just a little
- 1:35:33sneak peek, I guess. Um, and there's
- 1:35:35another value ETF called IUSV, and
- 1:35:37they're both US value ETFs. And let's
- 1:35:40say we want to know which one is a
- 1:35:41deeper value, which means like which
- 1:35:43one's more exposed to value if we're
- 1:35:45really like targeting value. Um, and we
- 1:35:47can actually do this, as I was saying,
- 1:35:48with the factor regression. This is how
- 1:35:50this stuff might actually look. In this
- 1:35:52case, I'm just using the Fama French US
- 1:35:545 factor model. And if you remember from
- 1:35:56that video, um, HML, um, high minus low,
- 1:35:59you know, book to market, right? just
- 1:36:00the value sort of factor and we can see
- 1:36:02a loading of 0.34 on IOSV and 0.45 45 on
- 1:36:06VUE, right? This means that VLUE is
- 1:36:09actually a deeper value. It's actually
- 1:36:12more exposed to the value premium,
- 1:36:14right? Remember how I was talking about
- 1:36:15like market beta and stuff? This is
- 1:36:17essentially the beta for value. Um, I
- 1:36:20might even do this maybe next or
- 1:36:21something, you know, if you guys still
- 1:36:22want factor aggression stuff. Um, and
- 1:36:24then the interesting thing that I was
- 1:36:25saying, let's grab maybe like a momentum
- 1:36:27ETF like MTUM. And what I did is I
- 1:36:31pulled the famous French five factor
- 1:36:32model and I actually add momentum to it.
- 1:36:34I kind of like made my own six factor
- 1:36:36model. And when we run this, we can
- 1:36:37actually look at the factor loadings on
- 1:36:39the momentum factor here called MOM. Um,
- 1:36:42and it turns out value is actually like
- 1:36:44a little bit sort of like short
- 1:36:46momentum. In fact, AQR published some
- 1:36:48research on doing like a value momentum
- 1:36:50barbell. Um, I recommended some uh some
- 1:36:52AQR papers to some people in my last
- 1:36:54video. I know I'm just rambling, right?
- 1:36:55I just find it interesting. Um, but we
- 1:36:57can see that the momentum ETF is
- 1:36:58massively exposed to the momentum
- 1:37:00factor, right? it can kind of actually
- 1:37:02in a way almost guide decision-m right
- 1:37:04obviously we're not getting to that
- 1:37:06caliber yet this isn't an analysis or
- 1:37:08anything um I'm just showing you how
- 1:37:09this can sort of be useful is we can
- 1:37:11actually you know see you know this is
- 1:37:12more value than the other um and then we
- 1:37:14also have like rolling regressions which
- 1:37:17is really interesting we're going to
- 1:37:18talk about rolling returns rolling
- 1:37:20regressions um even like the
- 1:37:22non-stationerity of volatility and even
- 1:37:24covariant stuff one of the issues with
- 1:37:26diversification right the quote is they
- 1:37:28say diversification fails you when you
- 1:37:30need it the post, you know, when the
- 1:37:32market crashes, right? Everything might
- 1:37:34crash at the same time. Correlations
- 1:37:35almost in a way converge on one
- 1:37:37depending on what asset you're looking
- 1:37:39at. Um, you know, if you harvest these
- 1:37:41sort of value premiums in a long short,
- 1:37:43that's when you're truly not going to be
- 1:37:45correlated. If you harvest them in a
- 1:37:47long only, you're still going to reap
- 1:37:48the benefits, but they will be like
- 1:37:50massively diminished. In this case,
- 1:37:52right, the value fund, these will never
- 1:37:54have a loading of like one or 1.1 or
- 1:37:570.9. it's never going to reach that
- 1:37:59level since these are long only funds.
- 1:38:01If we did this on a long short fund, we
- 1:38:03could actually see a loading actually
- 1:38:05get close to one. Um either way, if I go
- 1:38:07ahead and do like a rolling regression,
- 1:38:10this is actually going to show us that
- 1:38:11factor premiums themselves are
- 1:38:13non-stationary. They should sort of more
- 1:38:15or less not change that much. Um but we
- 1:38:18can do additional analysis with this. In
- 1:38:19this case, I just have a plot for every
- 1:38:21single ETF and we can actually look and
- 1:38:23see over time how their exposure to the
- 1:38:26premiums change. Um, you know, there's
- 1:38:28like so much stuff we can do within
- 1:38:29Quant. So, you know, I guess just let me
- 1:38:32know what you want to see. Let me know
- 1:38:33if you want the series to continue. Um,
- 1:38:35I have no clue how long I've been
- 1:38:37filming for. Obviously, this video is
- 1:38:38not scripted. The first Quant Finance
- 1:38:40video, 101, was scripted, so it was able
- 1:38:43to be way more condensed. I probably
- 1:38:44wasted way too much time, I guess. Let
- 1:38:46me know which style of video you want to
- 1:38:48see, too, if anyone even made it this
- 1:38:50far. Um, but I think that's really kind
- 1:38:52of everything. So, as always, if you
- 1:38:54made it this far, you might be
- 1:38:56interested in subscribing to the
- 1:38:57channel, checking out some of my other
- 1:38:58content, and turning on notifications.
- 1:39:00Um, now I really do appreciate the
- 1:39:02support on all of my videos. Um, and I
- 1:39:05really also specifically appreciate the
- 1:39:07support on my finance videos. As I was
- 1:39:09saying in my asset pricing video, you
- 1:39:10know, not that many people know, but you
- 1:39:13know, I study like two things formally
- 1:39:15just as much as I study computer science
- 1:39:17and cyber and bug bounties and software
- 1:39:19engine, all that stuff. I'm also all the
- 1:39:21time, you know, looking into like asset
- 1:39:23pricing, quant finance, all the stuff.
- 1:39:25It's literally like half my life and it
- 1:39:26doesn't really get represented on the
- 1:39:28channel, you know. So, if there's like
- 1:39:30interest for this type of thing, you
- 1:39:31know, that's like literally awesome, you
- 1:39:33know. I think this video out of every
- 1:39:35video I ever made on my channel, I
- 1:39:37probably had the most fun making this
- 1:39:38video out of any video. Um, and yeah,
- 1:39:42you know, if there's interest, obviously
- 1:39:43we'll do we'll do Mark Quant stuff. Um,
- 1:39:45you know, this is like this is like
- 1:39:47everything to me. Like this is this is
- 1:39:49incredibly interesting. Anyways, so I
- 1:39:50think that's it.
About this transcript
This page contains the full transcript of Quant Finance with Python | Stock Market Modeling (easy) by Daniel Boctor, generated from the public captions YouTube serves with the video. The transcript has 21,715 words across 2,967 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.