YouTube2Text

Quant Finance with Python | Stock Market Modeling (easy) — Transcript

by Daniel Boctor · 21,715 words · 2,967 segments · language en · Watch on YouTube

Full transcript

  1. 0:00This video is going to cover stochcastic
  2. 0:02modeling, specifically geometric brownie
  3. 0:04in motion. We're going to be taking
  4. 0:05these seemingly complex looking
  5. 0:07formulas, and we're going to be
  6. 0:08implementing them directly inside of
  7. 0:10Python. And even though this looks kind
  8. 0:11of complex, this entire function here is
  9. 0:14literally just six lines of code. And
  10. 0:16just to give you a little preview of
  11. 0:17what we're going to be building up to in
  12. 0:18this video, by the time you get to the
  13. 0:20end, we're actually going to be building
  14. 0:22this interactive plot completely from
  15. 0:24scratch. This means that we can change
  16. 0:26the input parameters on our model in
  17. 0:27real time such as let's say like
  18. 0:29lowering the volatility for example or
  19. 0:31let's say making the expecttor to turn
  20. 0:33negative. Of course they're going to be
  21. 0:34explaining all of this from scratch and
  22. 0:36this entire interactive plotting thing
  23. 0:37is literally just 10 lines of code. Now
  24. 0:40the cool thing is that I'm making this
  25. 0:41video accessible to everyone even
  26. 0:43complete beginners. This means that
  27. 0:45everything we need both the math the
  28. 0:47code and the finance I'm going to be
  29. 0:49explaining completely from scratch.
  30. 0:51You're not going to need any external
  31. 0:52prerequisite knowledge. So last thing,
  32. 0:55even if you don't know or don't care
  33. 0:57about stochcastic models or even about
  34. 0:59finance in general, I would still advise
  35. 1:01you stick around because we're actually
  36. 1:02going to be covering a lot of ground in
  37. 1:04this video, including just Python in
  38. 1:06general, as well as vectorization,
  39. 1:08obviously, numpy and pandas libraries,
  40. 1:10plotting with mapplot lib, um, as well
  41. 1:12as IPI widgets, which is how we're
  42. 1:13actually able to build these interactive
  43. 1:15UIs incredibly easily directly in a
  44. 1:17Python notebook. We don't need HTML or
  45. 1:20JavaScript or anything web- based. Um,
  46. 1:22and we're also going to be going over
  47. 1:23some time complexity stuff as well.
  48. 1:25We're actually going to be discussing
  49. 1:26how we can actually make our Python code
  50. 1:28more efficient over time. So, with all
  51. 1:30of that being said, let's get started.
  52. 1:33So, this is a stochcastic model,
  53. 1:35sometimes referred to as a stochcastic
  54. 1:37process. A stochastic model is just some
  55. 1:39type of model that involves some form of
  56. 1:41inherent randomness. In this video,
  57. 1:44we're going to be stochastically
  58. 1:45modeling stock market returns. So, I
  59. 1:48know that sounds confusing, but
  60. 1:49essentially all that means is that we're
  61. 1:51going to be generating random numbers,
  62. 1:53but they're not just going to be any
  63. 1:54random numbers. They're going to be
  64. 1:56random numbers that are a reasonable
  65. 1:58close approximation of actual stock
  66. 2:01market returns. Simply put, each one of
  67. 2:03these lines on this chart is trying to
  68. 2:05mimic a reasonable path that the actual
  69. 2:08stock market might take. Each one of
  70. 2:10these paths can be called a random walk.
  71. 2:12Now, as I said, the model that we're
  72. 2:14covering in this video specifically is
  73. 2:16called geometric brownie in motion. This
  74. 2:18is an example of something we can call a
  75. 2:20non-meanverting stochastic model, which
  76. 2:22we can use to model stock market
  77. 2:24returns. This is opposed to something we
  78. 2:26can call a mean reverting stochastic
  79. 2:28model like the CIR model, which we can
  80. 2:30actually use to model fixed income or
  81. 2:32bond prices. So, you might be asking,
  82. 2:34why do we want to randomly generate
  83. 2:36stock returns that mimic real world
  84. 2:38stock returns? Why not just pull actual
  85. 2:40real world historic data? And of course,
  86. 2:42that's a good question. And let's just
  87. 2:44take a step back here for a second to
  88. 2:46explain what we're actually doing. So,
  89. 2:48let's say that we want to design a
  90. 2:49portfolio strategy. This could be maybe
  91. 2:51like a dynamic hedging strategy or
  92. 2:53something, but it doesn't really matter
  93. 2:54what it is. Now, if you want to test the
  94. 2:56efficacy of a portfolio strategy in
  95. 2:59order to get useful information on its
  96. 3:00performance, we can do this in two
  97. 3:02different steps. First, we'll need to
  98. 3:05gather some data. This data could either
  99. 3:07be real world historic stock market data
  100. 3:09or of course it could just be randomly
  101. 3:11generated data that mimics real world
  102. 3:13data. Now second once we have our data
  103. 3:17now we can go ahead and do like a Monte
  104. 3:18Carlo simulation or really whatever else
  105. 3:21we want to do to actually test the
  106. 3:23portfolio strategy using the data. So in
  107. 3:25short you can think of the brownie in
  108. 3:27motion model as just being like a
  109. 3:29building block or a tool in our toolbox
  110. 3:31that will come in handy later. Now, of
  111. 3:33course, it might even be better to use
  112. 3:35historical data. Obviously, there's
  113. 3:36merits to both, and that's sort of like
  114. 3:38out of the scope of this video, at least
  115. 3:40for now. All you really need to know is
  116. 3:42that we're going to be building a model
  117. 3:43that can generate close approximations
  118. 3:45of actual stock market returns. Let's go
  119. 3:48ahead and get started. So, real quick
  120. 3:50before we get started, the video that
  121. 3:52you're currently watching is a part two
  122. 3:54in my Quant Finance series. About 2 and
  123. 3:561/2 years ago, I made Quant Finance 101,
  124. 3:59which is just a general foundational
  125. 4:01level introduction. and I said if there
  126. 4:03was interest I would turn it into a
  127. 4:04series and at first there wasn't much
  128. 4:06interest and then after 2 and 1/2 years
  129. 4:08it just blew up out of nowhere and
  130. 4:10everybody was you know telling me please
  131. 4:11continue the series so here we are in
  132. 4:14either case I just wanted to say that
  133. 4:15while there is no external prerequisite
  134. 4:18knowledge needed I will be assuming that
  135. 4:20past this point everyone did watch quant
  136. 4:22finance 101 which is the first video in
  137. 4:24the series that's just going to give you
  138. 4:26a basic foundation of numpy and pandas
  139. 4:28along with the basics of calculating
  140. 4:30risk and return so if you didn't watch
  141. 4:31this video. Go ahead and give it a watch
  142. 4:33and then we should be good to go. Now,
  143. 4:35recently I did make two additional
  144. 4:37finance videos, although these were not
  145. 4:39done in Python. These were just raw
  146. 4:41finance videos. So, these two are not
  147. 4:43needed to continue. Um, now a lot of
  148. 4:45people in the asset pricing video were
  149. 4:47asking me about factor regressions. Of
  150. 4:49course, we're going to be doing a
  151. 4:50dedicated video later on on factor
  152. 4:51regressions. Um, but I will talk a
  153. 4:53little more about that at the end of
  154. 4:55today's video. Now, before we actually
  155. 4:58implement these formulas inside of
  156. 4:59Python, I'm just going to be explaining
  157. 5:01what they are mathematically. Now, fair
  158. 5:03warning, while I have these formulas on
  159. 5:05the screen, this might be incredibly
  160. 5:07confusing. I'm going to just go more in
  161. 5:09depth than what's actually needed. Once
  162. 5:11we get to the Python part, it should be
  163. 5:12pretty straightforward. So, if you get
  164. 5:14lost and you don't know what I'm talking
  165. 5:15about, that's completely okay. So, this
  166. 5:18is the formula for geometric brownie in
  167. 5:20motion in continuous time. And all we
  168. 5:23need to worry about for now is that this
  169. 5:24whole complex looking formula is
  170. 5:26essentially just going to give us the
  171. 5:28percentage change or the percentage
  172. 5:30return of a stock or of a stock market
  173. 5:32index over some time step. And what
  174. 5:35we're going to do in Python is we're
  175. 5:37going to run this entire formula, you
  176. 5:38know, hundreds or thousands of times to
  177. 5:41get a percentage return for many
  178. 5:42different consecutive time steps. And
  179. 5:44then we're going to be geometrically
  180. 5:46linking, right? just compounding those
  181. 5:48returns over time in order to get, you
  182. 5:50know, a random walk over maybe like a
  183. 5:5210-year sample. Now, we don't need to
  184. 5:54worry too much about how this works in
  185. 5:55continuous time. We're actually going to
  186. 5:57be implementing it in discreet time, but
  187. 5:59just for those of you that are maybe
  188. 6:01like math majors or finance majors or
  189. 6:03have a background in calculus or
  190. 6:04anything like that, you're probably
  191. 6:06going to have some intuition um of how
  192. 6:08this works. Essentially, the time step
  193. 6:10here is dt. Um, and in this case, dt is
  194. 6:13just going to be an infantessimally tiny
  195. 6:15amount of time. So not one day, not one
  196. 6:18minute, not one second, not one nancond,
  197. 6:21right? Even smaller. It's just like an
  198. 6:23infinitely tiny amount of time. Um, and
  199. 6:25ST just means the price of the stock or
  200. 6:28the price of the stock market index at
  201. 6:30time t. And DST just means the change in
  202. 6:34dollar value of the stock between time t
  203. 6:37and time t + dt. So dst literally just
  204. 6:41means how much the price moves in that
  205. 6:43very amount of time. when we divide that
  206. 6:45by ST itself, the whole thing
  207. 6:47essentially just becomes a percentage
  208. 6:49return over this infantestimally tiny
  209. 6:51time step. That's just, you know, to
  210. 6:53give some intuition for the people that
  211. 6:55sort of might understand this a bit
  212. 6:56more. Um, in reality, we don't need to
  213. 6:58worry about that. What we're actually
  214. 7:00going to be doing in this video in
  215. 7:01Python is we're going to be implementing
  216. 7:03this in discretet time. So, if you watch
  217. 7:06that first Quant Finance video I made,
  218. 7:08this formula should look somewhat
  219. 7:10familiar to you. Um, this literally just
  220. 7:12means the percentage return of a stock
  221. 7:14in between time t and time dt. So it's
  222. 7:17the same as the continuous version. It's
  223. 7:19just that dt is now going to be some,
  224. 7:22you know, some time step that we can
  225. 7:23choose. So let me just go ahead and
  226. 7:25simplify this as much as I possibly can.
  227. 7:28So all of this is just a percentage
  228. 7:31return in one given time step in dt. Um,
  229. 7:34and the time step now can really be
  230. 7:36anything. It can be a day, a month, a
  231. 7:38year. Now, in this video in Python, it's
  232. 7:40going to be a parameter. So, you're
  233. 7:41actually free to pick DT. Um, but in
  234. 7:44this video, we're just going to be
  235. 7:45making it be one month. So, you can just
  236. 7:47think of this as being the percentage
  237. 7:49return of the stock market index over 1
  238. 7:52month is equal to all of this stuff. So,
  239. 7:55let me go ahead and write this out as
  240. 7:56simply as I can. So, the actual return
  241. 7:59over a one month time step, right? The
  242. 8:01data point of this random walk for this
  243. 8:03one month that we're going to be using
  244. 8:04is going to be equal to the expected
  245. 8:07return of the stock market index which
  246. 8:09we can call MO. MO just means the
  247. 8:11expected return or the average return on
  248. 8:14the market. And then we're going to be
  249. 8:16doing plus sigma.
  250. 8:19And sigma just means the volatility or
  251. 8:21the standard deviation. So I know this
  252. 8:24might be confusing but in short and
  253. 8:26we're going to be generating hundreds or
  254. 8:27thousands of per month returns but the
  255. 8:30actual return in any given month is
  256. 8:33equal to the expected return plus the
  257. 8:35volatility around the expected return.
  258. 8:38This is just a simple decomposition. Now
  259. 8:40in general how things work in finance is
  260. 8:43any sort of figure that's just sort of
  261. 8:44quoted unless otherwise stated tends to
  262. 8:47be annual. Right? So in this case, we're
  263. 8:50looking at the return per a given time
  264. 8:52step. The MO is going to be we're going
  265. 8:55to see later. It's going to make more
  266. 8:56sense later, but the average expected
  267. 8:58return is annual. So we're essentially
  268. 9:00just doing MO, but we need to scale it
  269. 9:03down to DT. So we're taking the annual
  270. 9:05expected return and then just scaling it
  271. 9:08down to say the expected return just for
  272. 9:10a single month. And then same as the
  273. 9:11sigma, we're going to be taking, you
  274. 9:13know, the annual sigma, right? The
  275. 9:15annual standard deviation or volatility
  276. 9:16on a stock market index. And then of
  277. 9:18course when we do this in Python we're
  278. 9:20going to say let's just convert it from
  279. 9:22annual to just you know per one month or
  280. 9:24per one time step. That's why these
  281. 9:26numbers involve a DT in them. Now this
  282. 9:29is just going to be like a little
  283. 9:30digression just for those that are
  284. 9:32interested. This character here is sigma
  285. 9:34but we're not actually seeing a moo. The
  286. 9:37reason why is we're actually further
  287. 9:39decomposing moo. And I'm going to go to
  288. 9:40the next slide. And these three
  289. 9:42characters together are moo. We're just
  290. 9:44further decomposing them just for those
  291. 9:46that are interested. When we actually
  292. 9:48implement this in Python, there's just
  293. 9:50going to be a single character called
  294. 9:51moo. Um, but if you think back to my
  295. 9:53video on asset pricing, right? This is
  296. 9:55just for the just for those that are
  297. 9:57interested. But if you think back to my
  298. 9:58video on asset pricing, how can we
  299. 10:00further decompose the return on let's
  300. 10:03say the stock market? Well, I said that
  301. 10:05we can decompose the return on the stock
  302. 10:07market into the risk-free rate plus the
  303. 10:10risk premium. And the same applies for
  304. 10:12the value premium, the profitability
  305. 10:14premium. In this we're just thinking
  306. 10:15about like the global total stock
  307. 10:16market. Um, but r here is equal to the
  308. 10:19risk-free rate. And then we're going to
  309. 10:21be adding the risk-free rate to the risk
  310. 10:24premium. So these two figures sigma
  311. 10:27lambda being multiplied together is
  312. 10:29equal to the risk premium. And how we
  313. 10:31sort of do that further decomposition is
  314. 10:34quite interesting. Sigma as we know is
  315. 10:36just the volatility and then lambda is
  316. 10:38the sharp ratio. So, I know in my first
  317. 10:41finance video, uh my first quant finance
  318. 10:43video, we touched on the raw sharp
  319. 10:45ratio. Now, the raw sharp ratio and the
  320. 10:47regular sharp ratio is basically just a
  321. 10:50risk adjusted return metric. The raw
  322. 10:53sharp ratio just means how much return
  323. 10:56you get per unit of risk. Um and then
  324. 10:59the that's the raw sharp ratio. And then
  325. 11:01the actual sharp ratio means how much
  326. 11:04excess return you get per unit of risk
  327. 11:07where excess return is just the total
  328. 11:09return above the risk-free rate. It's
  329. 11:13essentially like the risk premium,
  330. 11:14right? I know that sounds confusing, but
  331. 11:17you know, if you think about it in
  332. 11:18simple terms, if you take how much
  333. 11:20return, if you take how much excess
  334. 11:22return you get per unit of risk and
  335. 11:25literally multiply it by the units of
  336. 11:28risk, you're just left with the excess
  337. 11:30return. The excess return is literally
  338. 11:33the risk premium. And then if you do the
  339. 11:35decomposition of risk-free rate plus
  340. 11:38risk premium, that's just the MOO, which
  341. 11:40is just the total absolute expected
  342. 11:43return. That's just a further
  343. 11:45decomposition for those that are
  344. 11:46interested. Again, in this video, we're
  345. 11:48going to further simplify it. We're just
  346. 11:50going to have MO * DT. Okay, so we're
  347. 11:53almost done here. Just to recap, the
  348. 11:56percentage return of the stock market
  349. 11:58index for one given time step, let's say
  350. 12:00one month, is equal to the expected
  351. 12:03return on the stock market index scaled
  352. 12:06down to just per time step. And then the
  353. 12:08sigma, which is the volatility of the
  354. 12:10stock market index, also scaled down
  355. 12:12into one month or one time step. You
  356. 12:14don't need to worry about why this is in
  357. 12:15a square root. Again, if you watch my
  358. 12:17previous video, you're going to
  359. 12:18understand why, but it's literally just
  360. 12:20the return is equal to the expected
  361. 12:22return plus the volatility around that
  362. 12:24return over the given time step, which
  363. 12:26is 1 month. Now, so far all of this is
  364. 12:30just like a random sort of
  365. 12:31decomposition. What actually makes this
  366. 12:34a Brownie motion stochastic model? We
  367. 12:36now need to introduce the randomness,
  368. 12:38right? So, it's this variable here
  369. 12:40called Z of T. So it's all of this and
  370. 12:43then we're going to multiply it by
  371. 12:44something that we call z of t. This is x
  372. 12:47i the moo sigma z. These are the actual
  373. 12:49variable names we're going to use in
  374. 12:51python. But all of this is going to be
  375. 12:53multiplied by z of t. And z of t is what
  376. 12:56actually matters. This is the actual
  377. 12:58brownie and motion term. So all z of t
  378. 13:01is is just going to be a random
  379. 13:03variable, right? Not just any random
  380. 13:05variable. More specifically, it's going
  381. 13:07to be a random normal. specifically a
  382. 13:10random standard normal. We can also just
  383. 13:13call it a random Gaussian variable. So,
  384. 13:16think back to your high school stats
  385. 13:17class. Let me go ahead and pull this up
  386. 13:18actually. So, I know everyone watching
  387. 13:21obviously isn't going to be like a math
  388. 13:23major or anything, right? I just want to
  389. 13:24make this video as accessible as
  390. 13:26possible because it's really quite
  391. 13:28simple. I know it sounds confusing, but
  392. 13:30it's really quite simple. So, just to
  393. 13:32give you guys some intuition as to what
  394. 13:33I'm talking about, I literally just
  395. 13:35search standard uh standard normal
  396. 13:37distribution on Google. And let's just
  397. 13:39take a look at this image, right? If you
  398. 13:40think back to like high school stats
  399. 13:42class, this is just a bell curve, right?
  400. 13:44This is a standard normal distribution.
  401. 13:46Um, if you recall, a standard normal
  402. 13:48distribution will have an average of
  403. 13:51zero, right? It will have a mean of zero
  404. 13:53and it will have a unit variance, which
  405. 13:55just means that like the standard
  406. 13:56deviation is equal to one, right? We're
  407. 13:58just taking a look at this bell curve
  408. 13:59and all z of t is we're just going to
  409. 14:02take this bell curve and we're going to
  410. 14:04pick a random number off this bell
  411. 14:06curve. Right? the number. It could be
  412. 14:07zero, it could be one, it could be
  413. 14:10negative 0.5. We're just going to yink a
  414. 14:12random number off of this bell curve and
  415. 14:14that's it. That's all z of t is. That's
  416. 14:16all a random Gaussian is. We're just
  417. 14:18going to yink a random number off this
  418. 14:19bell curve and plug it into the formula
  419. 14:22and that's it. And then every single
  420. 14:24time step, every single month we run the
  421. 14:26formula, we are going to be yoinking a
  422. 14:28different random z of t off of this bell
  423. 14:31curve. That's really all we're doing.
  424. 14:33So, let me go ahead and just explain all
  425. 14:35of this in complete plain English. This
  426. 14:37is what we're actually going to be doing
  427. 14:38in Python. We're going to go onto that
  428. 14:40standard normal bell curve and we're
  429. 14:42going to yink off this completely random
  430. 14:44standard normal number. We're going to
  431. 14:46go ahead and we're going to apply a
  432. 14:48standard deviation to it. And then we're
  433. 14:50going to go ahead and apply a mean to
  434. 14:52it. And then we're done. And that's just
  435. 14:54the return over a given month. And then
  436. 14:56we're just going to keep on yinking
  437. 14:58random numbers, applying volatility,
  438. 15:00applying a mean. And we're going to do
  439. 15:02this hundreds and thousands of times.
  440. 15:04And that's just how we get our returns.
  441. 15:06So one of the important things to note
  442. 15:08is that every single time you run this
  443. 15:10formula and get a one-mon return, it's
  444. 15:12going to be an independent increment,
  445. 15:15right? So at every single time step,
  446. 15:17there's going to be like an equal
  447. 15:18probability that a return is like above
  448. 15:20the mean or below the mean. Um so what
  449. 15:22that means is that there's no
  450. 15:24autocorrelation, right? Which means like
  451. 15:26there's no serial correlation, right?
  452. 15:28There's no momentum, there's no
  453. 15:29reversals. It's this completely random
  454. 15:32walk where every single time step,
  455. 15:34right, 50% chance it's above the mean,
  456. 15:3550% uh 50% chance it's below the mean.
  457. 15:38Now, efficient markets people, if you
  458. 15:40think back to my last couple of videos,
  459. 15:42right, efficient markets people are
  460. 15:44going to say this actually is how
  461. 15:46markets should function, right? There
  462. 15:48shouldn't be any sort of momentum in
  463. 15:50markets, right? There shouldn't be
  464. 15:51serial correlation. And that's actually
  465. 15:53sort of true, right? And this is why
  466. 15:55geometric uh geometric branding motion
  467. 15:58is considered to be a reasonable
  468. 16:00approximation of the stock market. Now
  469. 16:02of course if you go ahead and you look
  470. 16:04closely at the data you are going to
  471. 16:06find some serial correlation right. Um
  472. 16:09now in addition to this since these are
  473. 16:11random normals right random Gaussian
  474. 16:13variables there's no skewess and there's
  475. 16:16no curtosis. So that means that we're
  476. 16:18also making the assumption that asset
  477. 16:21returns are normally distributed. right
  478. 16:24now in real life you know that's sort of
  479. 16:26true but in real life there might be
  480. 16:28some skewess there might be some
  481. 16:30kurtosis and there might be some
  482. 16:32autocorrelation and this model is not
  483. 16:34going to capture any of that so as I
  484. 16:37said it is widely recognized as a
  485. 16:39reasonable model but it's definitely not
  486. 16:42a perfect model now of course we could
  487. 16:45start to build a better more realistic
  488. 16:47model that captures some of these things
  489. 16:50so this is going to be out of scope of
  490. 16:52this video you could skip ahead. This is
  491. 16:54just going to be for those of you like
  492. 16:55me that are interested, right? So, let
  493. 16:57me tell you this, right? So, when we
  494. 16:59actually implement this in Python, the
  495. 17:00MO and the sigma, right? Um the
  496. 17:02volatility and the expected return,
  497. 17:04these are just going to be fixed
  498. 17:05parameters, right? We're just going to
  499. 17:07hand it, let's say, like a 7% expected
  500. 17:09return, maybe like a 15% sigma, and
  501. 17:12we're just going to call it a day. These
  502. 17:13are just going to be hard-coded numbers.
  503. 17:15But think about this. In reality,
  504. 17:18everything is time varying, right? It's
  505. 17:20not in this model. The only source of
  506. 17:22randomness is going to be Z. But in
  507. 17:24reality, everything is time varying. So
  508. 17:27we could build a more realistic more
  509. 17:29general model that uses something that
  510. 17:31we can call a stochcastic opportunity
  511. 17:33set where the inputs into the stochastic
  512. 17:36model are in and of themselves also a
  513. 17:39stochcastic process. So that means that
  514. 17:41we would have two layers of randomness.
  515. 17:44So again, we're not going to be doing
  516. 17:46this more advanced version in this
  517. 17:47video. But why do we assume that MOU and
  518. 17:50sigma are stationary over time? In fact,
  519. 17:52let me go back to my last slide. So when
  520. 17:54we decompose the expected return,
  521. 17:56obviously R is the risk-free rate. If
  522. 17:58you think back to my previous videos in
  523. 18:00finance, we generally consider the
  524. 18:01risk-free rate to be 30-day Treasury
  525. 18:03bills. And the interest rate on these
  526. 18:0530-day United States Treasury bills
  527. 18:07obviously changes over time. It's not
  528. 18:09stationary, right? um in the risk-free
  529. 18:11rate changing over time that's actually
  530. 18:14directly observable. But the really
  531. 18:16interesting thing is that we have really
  532. 18:18strong empirical reasons to believe that
  533. 18:21the sigma which is the volatility and
  534. 18:23the lambda which is the sharp ratio or
  535. 18:25the actual risk premium is actually also
  536. 18:28time varying. Right? So in the future
  537. 18:30maybe in future videos you know we can
  538. 18:32go ahead and we can use like various
  539. 18:33statistical techniques to actually
  540. 18:35estimate risk premiums going forward and
  541. 18:38expected returns going forward and also
  542. 18:40volatilities even covariances right now
  543. 18:43you know we can talk about like the arch
  544. 18:45and garsh models right obviously this is
  545. 18:47beyond the scope of this video it's just
  546. 18:49really interesting to note in fact in my
  547. 18:51last video that I made on asset pricing
  548. 18:53um I actually uh answered a commenter in
  549. 18:55my comments and I actually brought up
  550. 18:56something called the cape the cap PE,
  551. 18:59which is the cyclically adjusted price
  552. 19:01earnings. Um, now obviously like
  553. 19:03expected return forecasting of the ERP
  554. 19:06and of these factors, that's like pretty
  555. 19:08complex and that's well beyond the scope
  556. 19:10of this video for today. Um, but I was
  557. 19:12saying that like one of the only
  558. 19:14reliable metrics we have to actually
  559. 19:17forecast expected returns, at least in
  560. 19:19the stock market, is with the cape,
  561. 19:21which is the cyclically adjusted price
  562. 19:22earnings. um that's essentially just a
  563. 19:24PE like a price earnings ratio but that
  564. 19:27actually goes back 10 years instead of
  565. 19:29just like one year or whatnot and it's
  566. 19:31also inflation adjusted and that has a
  567. 19:33bunch of added benefits of making that
  568. 19:35signal of valuation more clear. It sort
  569. 19:38of you know goes through and smooths out
  570. 19:40a lot of effects that come up because of
  571. 19:42accounting standards and the business
  572. 19:43cycle and all this stuff. Um you know
  573. 19:45and then obviously sort of people that
  574. 19:46study finance understand that you know
  575. 19:48expected returns are inversely
  576. 19:50correlated with valuations. So if you
  577. 19:52have a clear signal evaluations, right?
  578. 19:54And you know, again, we can sort of talk
  579. 19:56about all this stuff later. Uh but the
  580. 19:58cape was obviously developed by somebody
  581. 19:59called Robert Schiller. Obviously, you
  582. 20:01can go read his books and you know, I
  583. 20:03was talking in my last video too about
  584. 20:05like Eugene FMA and Ken French. I think
  585. 20:07I brought up uh Cliff Asess right now.
  586. 20:09All these researchers, at least for me,
  587. 20:11you know, I learn you sort of almost
  588. 20:13like connect with these researchers a
  589. 20:15little bit. Uh personally, I know way
  590. 20:17more researchers within finance than I
  591. 20:19do like on the computer science side. uh
  592. 20:21you know there's like Robert not John
  593. 20:23Cochran Robert Novie Marks I think I
  594. 20:25talked about Andrew Ang wrote a whole
  595. 20:27book on asset pricing I actually
  596. 20:28recommended that to some commenters and
  597. 20:30then obviously you know there's like
  598. 20:31Robert Merin Jack Bogle Marowitz Scholes
  599. 20:34uh and then Bill Sharp is the guy that
  600. 20:35actually invented this lambda right that
  601. 20:37actually invented the sharp ratio um
  602. 20:39either way right so the moral of the
  603. 20:40story is that there are ways to generate
  604. 20:43more reasonable you know more realistic
  605. 20:45stochcastic scenarios um but in this
  606. 20:47video we're just going to be uh sticking
  607. 20:49with this sort of you know simple base
  608. 20:51version of geometric brownie motion and
  609. 20:53let's go ahead and implement this in
  610. 20:55Python. Okay, so let's go ahead and
  611. 20:57essentially just take this formula and
  612. 20:59implement it best we can inside of
  613. 21:01Python. So just to keep the scope of
  614. 21:04this video open uh to beginners and to
  615. 21:06everyone, I'm just going to explain
  616. 21:07everything from scratch. So what I'm
  617. 21:09using right now is just called an
  618. 21:10interactive Python notebook. Ideally,
  619. 21:12you'll go ahead and you know follow
  620. 21:14along with me writing your own code as
  621. 21:15you watch the video. Um, so if you've
  622. 21:17never used one of these before, you
  623. 21:18know, you can go ahead and use like a
  624. 21:20Jupyter notebook or whatever you need to
  625. 21:21use. In this case, I'm just using one of
  626. 21:23these Python notebooks directly inside
  627. 21:25of VS Code. This is just what I prefer.
  628. 21:27Um, you know, you can go ahead and
  629. 21:29literally just make a file and just give
  630. 21:31it
  631. 21:32for interactive Python notebook. That's
  632. 21:34just the file extension. And we just
  633. 21:36have this concept of cells. We just
  634. 21:37write regular Python code, right? Let's
  635. 21:39just say X= 10 in these cells, right?
  636. 21:42You pick a kernel, whatever. And it
  637. 21:44basically just lets us almost in a way
  638. 21:46pry into like a Python runtime in real
  639. 21:49time. Right? So if you never saw this
  640. 21:50before, if x is equal to 10, we can come
  641. 21:53down here to a new cell and we can for
  642. 21:55example, you know, print x and now we
  643. 21:57get 10. And if we go ahead and, you
  644. 21:59know, uh, x +als 5 in a different cell.
  645. 22:02Next time we reference x, it's going to
  646. 22:04print 15, right? It almost like saves
  647. 22:06state. We can go ahead and just execute
  648. 22:08Python right in the runtime in real
  649. 22:10time. This is used broadly and widely
  650. 22:13not just within finance but throughout
  651. 22:15really just all you know numerical
  652. 22:17mathematical statistical applications
  653. 22:19within Python. So this is also going to
  654. 22:20be good practice. Um last thing to note
  655. 22:23you're not going to see me in this video
  656. 22:24using print statements a whole lot.
  657. 22:26Essentially the last variable that's
  658. 22:28sort of like referenced, right? The last
  659. 22:30variable that's like left in a cell by
  660. 22:32default will be printed, right? Right.
  661. 22:33So if you remove the print statement and
  662. 22:35just execute a cell, um the last thing
  663. 22:37in the cell will just be printed by
  664. 22:38default um just to avoid confusion.
  665. 22:41Okay, so let's get started. So let's go
  666. 22:43ahead and all of this geometric brownie
  667. 22:45in motion stuff we're just going to make
  668. 22:46a function for that's going to handle
  669. 22:48everything. So let's go ahead and define
  670. 22:49a function and let's just call it
  671. 22:51geometric brownie in motion GBM. Um and
  672. 22:54let's think about the input parameters,
  673. 22:56right? So we just want to implement this
  674. 22:58formula inside of this function. So
  675. 23:00obviously we're going to need moo and
  676. 23:02obviously we're going to need sigma and
  677. 23:03we're going to need dt as well. But if
  678. 23:05we think more generally this whole
  679. 23:06formula just gives us a return for one
  680. 23:09given time step and we want to calculate
  681. 23:12you know hundreds or thousands of these
  682. 23:14time steps right so there's many
  683. 23:16different ways to do this. I'm just
  684. 23:17going to go over what I think is most
  685. 23:19easy and most intuitive right um so
  686. 23:21let's go ahead and have a parameter that
  687. 23:23we're going to call number of years.
  688. 23:25Let's just default it to 10. So that
  689. 23:27just means that if you want to run this
  690. 23:29random walk over, you know, a 10-year
  691. 23:32sample or 20-year sample, whatever,
  692. 23:34right? Literally just the length of the
  693. 23:35sample is going to be given by number of
  694. 23:37years, that's pretty straightforward.
  695. 23:38Um, now obviously we're going to need
  696. 23:40moo and sigma. As I said, these are just
  697. 23:43going to be hard-coded stationary
  698. 23:45parameters. How we actually calculate
  699. 23:47expected return is completely out of the
  700. 23:49scope of this video. Let's just pick
  701. 23:51something halfway reasonable. So, as I
  702. 23:53said, let's just use a 7% annualized
  703. 23:56expected return on the stock market.
  704. 23:58We're just going to write that in
  705. 23:59decimal form as 0.07. And then same
  706. 24:01thing for the sigma. Let's just use
  707. 24:030.15, which just means, you know, like
  708. 24:0615% standard deviation on the market.
  709. 24:08Um, and if we look at this formula,
  710. 24:10we're going to need one more thing,
  711. 24:11which is DT. Now, I could go ahead and
  712. 24:14like actually do DT. Um, but in reality,
  713. 24:17DT is going to be like 1 over 12. So to
  714. 24:20make things more simple, um let's go
  715. 24:21ahead and actually make this parameter
  716. 24:24called steps per year, which is just the
  717. 24:26granularity. We actually saw this in the
  718. 24:28last video. And we're going to default
  719. 24:30this to 12, which just means the monthly
  720. 24:32granularity, right? If there's 12 time
  721. 24:33steps in a year, we're obviously dealing
  722. 24:36with the monthly granularity. And then
  723. 24:37dt is just going to be equal to 1 / 12.
  724. 24:40So for now, this is all we need. Let's
  725. 24:42actually start writing the function. So
  726. 24:44as I said, we are going to need dt
  727. 24:46explicitly. um that's just going to be
  728. 24:48equal to one over the steps per year,
  729. 24:51right? Since, you know, one given time
  730. 24:52step is just going to be 1 over 12. Um
  731. 24:55and now let's think about what else we
  732. 24:57need. The last thing we're going to need
  733. 24:58is we're going to need something that
  734. 25:00we're going to call number of steps.
  735. 25:02This just means how many time steps are
  736. 25:04there over the entire sample, right? So
  737. 25:07if you think about it, if you want to
  738. 25:09run the sample for some number of years,
  739. 25:12let's say 10 years, and there's 12
  740. 25:14different time steps, right, per year,
  741. 25:16if you literally just go ahead and
  742. 25:18multiply the number of years by the
  743. 25:19steps per year, this is just the total
  744. 25:21number of steps throughout the entire
  745. 25:23sample. Okay, not bad. Um, now let's go
  746. 25:26ahead and continue. Let's now go ahead
  747. 25:29and actually implement Z of T. So let's
  748. 25:32call this Z. And as we know, Z is just
  749. 25:35going to be equal to this random
  750. 25:37standard normal, right? This random
  751. 25:38Gaussian variable. So, you know, there's
  752. 25:40a few different ways to do it. Um, now
  753. 25:42how I'm going to do it is I'm just going
  754. 25:44to use a numpy library. So, we are going
  755. 25:46to need, you know, a couple different
  756. 25:47dependencies. I think there's like four
  757. 25:49different dependencies in today's video
  758. 25:51in total. Um, so what we're going to do
  759. 25:53is let's actually just make a cell here
  760. 25:55and we're going to go ahead and we're
  761. 25:56going to import numpy. Now, most of the
  762. 25:58time when we import numpy, we just say
  763. 26:00as um np just to abbreviate. Now, you're
  764. 26:03going to need to go ahead and pip
  765. 26:05install numpy if you haven't already. Um
  766. 26:08now, an easy way to do it directly in a
  767. 26:09notebook just to make sure it's like for
  768. 26:11the right environment is we can actually
  769. 26:13just do a percent time um a percent
  770. 26:15sign. This is like a Python magic
  771. 26:17command. I actually previously in my
  772. 26:19channel would do like you know percent
  773. 26:21time it right to like you know whatever.
  774. 26:23Um, and you can actually do percent pip
  775. 26:26install numpy directly in a cell instead
  776. 26:28of opening a terminal. Obviously, it's
  777. 26:30already installed for me. Um, so you
  778. 26:32know, it will just say already
  779. 26:33installed, but for you guys, you can do
  780. 26:35this and this will work. And then you
  781. 26:36just go ahead and do import numpy as MP.
  782. 26:39Um, and then you should be good. Um, and
  783. 26:41of course, don't forget to actually
  784. 26:43execute the cell. Okay, so the numpy
  785. 26:45function that we're actually going to be
  786. 26:46using is going to be mprandom.normal.
  787. 26:50And this is just a function that will
  788. 26:52literally give us a random normal. As we
  789. 26:54can see, 1.43. Every single time we run
  790. 26:56this function, we're literally just
  791. 26:58going to get a random normal number.
  792. 27:00Now, you wouldn't know this unless you
  793. 27:01actually went and read the
  794. 27:02documentation. Now, obviously, right
  795. 27:04now, you know, we have like large
  796. 27:06language models of stuff. So, you could
  797. 27:07also just ask claude or, you know,
  798. 27:09whatever you need to ask. But, you know,
  799. 27:11obviously when I started doing all this,
  800. 27:13right, this is when we still went ahead
  801. 27:14and looked up documentation all the
  802. 27:15time. You know, as everything is in
  803. 27:18life, the less you use a skill, the more
  804. 27:19monatrophy, right? Um, I'm not going to
  805. 27:21bore you obviously with too much
  806. 27:22documentation. But if you look at the
  807. 27:23documentation for this function, we're
  808. 27:25going to see a couple different
  809. 27:25parameters. And, you know, you wouldn't
  810. 27:27know unless I told you this. Um, but in
  811. 27:29this case, we're going to be worried
  812. 27:29about the size parameter. We can go
  813. 27:32ahead and we can hand the size parameter
  814. 27:34to this function. And let's just say we
  815. 27:36set this number to five. Now, when we
  816. 27:38run this function, we're going to get
  817. 27:39back an array that contains five
  818. 27:41different random uh five different
  819. 27:43random normals, right? We can make this
  820. 27:4510. We're going to get 10 different
  821. 27:46random normals. So what we're going to
  822. 27:47do in this case is we're going to get uh
  823. 27:50we're going to set z equal to
  824. 27:52numpy.random.normal.
  825. 27:55And think about what we're going to set
  826. 27:56the size to, right? How many random
  827. 27:58normals do we want for this whole thing.
  828. 28:01Now we could open up an explicit for
  829. 28:03loop and just generate one and we could
  830. 28:06sort of, you know, do this whole
  831. 28:07function in a for loop. I'm going to
  832. 28:08talk more about time complexity later.
  833. 28:10It turns out that with broadcasting and
  834. 28:12vectorization, we can almost do all of
  835. 28:14this in one step. I'll explain that in
  836. 28:16just a second. So over the entire
  837. 28:18sample, right, how many Z do we need?
  838. 28:21Well, we need one Z per time step. So
  839. 28:24we're going to need n steps worth of Z,
  840. 28:28right? Um so in this case, if there's 10
  841. 28:30years um and there's 12 steps per year,
  842. 28:32we're going to need 120 different size,
  843. 28:34right? If you set this to 120, we're
  844. 28:36literally going to get 120 different
  845. 28:38random normals. We're essentially just
  846. 28:39getting all of the Z's we need um for
  847. 28:42the entire function. And now what we can
  848. 28:44do is we can go ahead and we can
  849. 28:46actually implement the formula. So you
  850. 28:48think back to my first quant finance
  851. 28:49video, we generally use if you want
  852. 28:51returns, right? This function is going
  853. 28:52to give us returns or this formula is
  854. 28:54going to give us returns. We generally
  855. 28:56will just call returns as either r or
  856. 28:58rats as the variable name. Of course,
  857. 29:00you can call it whatever you want. And
  858. 29:01let's go ahead and implement this
  859. 29:03formula. So we have mu time dt. Now, we
  860. 29:07could do mu * dt. And this looks correct
  861. 29:12because that is how it looks in the
  862. 29:14formula. We're going to need to do like
  863. 29:16a little segue here, right? If you watch
  864. 29:18my first quant uh quant finance video,
  865. 29:21you might understand why this is
  866. 29:23technically not correct. Now, I know
  867. 29:25this is confusing. You can easily just
  868. 29:27skip ahead and not worry about the
  869. 29:29details on this, but I'm going to give
  870. 29:30you guys a little bit of a demo um just
  871. 29:32if you're interested. So the goal here
  872. 29:34for MOO of DT is MOO is an annualized
  873. 29:37return and we literally just need right
  874. 29:39this term is literally just turning an
  875. 29:40annualized return into a monthly return.
  876. 29:43So if you think back to that first video
  877. 29:45how can we do that right now if we just
  878. 29:48do this this is technically incorrect
  879. 29:50and you know you might have went through
  880. 29:52this video without even realizing it.
  881. 29:53I'm just going to address why now don't
  882. 29:55worry if it's boring right so let's say
  883. 29:56we have an annualized return of 0.07 07
  884. 29:59and we're just going to multiply it by
  885. 30:01dt. And we know dt is 1 / 12, right? If
  886. 30:04we just did moot * dt, this is what we
  887. 30:06would get. And I'm just going to give
  888. 30:07you guys a little sort of example here.
  889. 30:09We don't need to worry about all like
  890. 30:10the floating point, you know, inaccuracy
  891. 30:12or anything like that. Um, and if we do
  892. 30:14this, this is going to tell us that, you
  893. 30:16know, 0.583%
  894. 30:19per month is the return per month,
  895. 30:22right? And then if we go ahead, again,
  896. 30:23don't worry about the floating stuff. If
  897. 30:24we go ahead and we take this number and
  898. 30:26then we just multiply it by 12 months a
  899. 30:28year, well, it looks like the an
  900. 30:29annualized return is back to being 0.07.
  901. 30:32So, it looks like we did things right.
  902. 30:35But again, if you think back to the um
  903. 30:37think back to my first video, how do we
  904. 30:39actually get an annualized return? So,
  905. 30:41we're going to pretend that this is the
  906. 30:42monthly return, even though this is
  907. 30:44wrong, and we're just going to calculate
  908. 30:45an annualized return. So, if you think
  909. 30:47back to my first video, we do the 1 plus
  910. 30:49r format. This is literally a return. So
  911. 30:52we're going to do 1 plus r and then in
  912. 30:54that video we would do apra for
  913. 30:56dotproduct. Of course this is not a
  914. 30:58vector or a time series or a return
  915. 31:00series. So obviously this isn't going to
  916. 31:01work. This is just an individual number
  917. 31:03in Python. Um instead of prodra we can
  918. 31:06actually just raise it to the 12. And if
  919. 31:08you want to know you know why we're
  920. 31:09doing that that's the same as taking one
  921. 31:11month's return multiplying it by a
  922. 31:13second month multiplying by a third
  923. 31:14month and then just doing that 12 times
  924. 31:16in a row. And then once we're done with
  925. 31:18that we can just go ahead and minus one.
  926. 31:20Uh again, this will make sense if you
  927. 31:22watch the first video. This is just
  928. 31:24turning a monthly return into an annual
  929. 31:26return. And if you run this cell, we can
  930. 31:28now see that the annual annualized
  931. 31:30return is 7.2%
  932. 31:33even though obviously it sort of should
  933. 31:34have been 7%. So I'm just showing you
  934. 31:37that if you just use multiplication,
  935. 31:39essentially you're getting into the case
  936. 31:40where we're calculating arithmetic
  937. 31:42returns, but in reality, we need to
  938. 31:44calculate geometric returns. This is why
  939. 31:47this is called geometric brownie in
  940. 31:48motion. Um you know essentially again we
  941. 31:50talked about in the first video but the
  942. 31:52return that the investor eats. We call
  943. 31:54it in finance you know the return that
  944. 31:55you eat just means the return like the
  945. 31:58actual economic reality that you
  946. 32:00experience is the geometric return.
  947. 32:02Let's see how we can correct this. So in
  948. 32:04reality to turn this 0.07% annualized
  949. 32:07return into a monthly return we would
  950. 32:10actually need to go ahead and do 1 + r
  951. 32:14and then we need to go ahead and raise
  952. 32:15it right not to the 12. We're going the
  953. 32:17other way around. We're doing annual to
  954. 32:19monthly, so you need to raise it to 1
  955. 32:21over 12. And then when we're done with
  956. 32:23this entire thing, we can just subtract
  957. 32:25our one. Let's go ahead and run this.
  958. 32:27And now we get 0.056
  959. 32:30instead of 0.58.
  960. 32:32So this is a lower number. And if we
  961. 32:34want to just go ahead and sort of verify
  962. 32:36that we're correct, we can go ahead and
  963. 32:39now turn the actual correct answer of
  964. 32:41the monthly return into an annualized
  965. 32:42return. Again, we do 1 plus r. We can
  966. 32:45now just raise it um to the 12. And now
  967. 32:48we can go ahead and subtract one. And
  968. 32:50now we should get back. Again, don't
  969. 32:52worry about the inaccuracy. Now we
  970. 32:53actually get back 0.07. Uh in fact, let
  971. 32:56me just give another real quick example.
  972. 32:57This is uh actually really interesting
  973. 32:59to note. Some firms will actually
  974. 33:01intentionally kind of blur the lines
  975. 33:04between arithmetic and geometric returns
  976. 33:06to essentially deceive investors. So let
  977. 33:09me just make up this example here just
  978. 33:10cuz it's really interesting. Let's just
  979. 33:12pretend that an asset management firm
  980. 33:14that's developing some marketing
  981. 33:16materials, uh, let's say that they use
  982. 33:18this to their advantage. So, let's say
  983. 33:19that the actual economic reality, their
  984. 33:22actual annualized return, let's just
  985. 33:24say, I'm just going to make up the
  986. 33:25numbers, is equal to 5% a year. So, if a
  987. 33:28fund actually gets 5% a year, and let's
  988. 33:31just say that they get 5% a year every
  989. 33:33single year for 5 years. If you want to
  990. 33:35calculate the compound return at the end
  991. 33:37of that 5year sample, right, we're just
  992. 33:39going to do 1 plus r raised to 5 years
  993. 33:42minus one, right? So this would be the
  994. 33:44compound growth if a firm earns 5% for 5
  995. 33:47years in a row. And when we run this,
  996. 33:49they earned 27% at the end of their
  997. 33:52sample. Now, how some firms essentially
  998. 33:55deceive investors is they'll take this
  999. 33:57geometric return and they will literally
  1000. 33:59just divide it by 5 years and they get
  1001. 34:020.055%
  1002. 34:03a year. and they'll literally go out and
  1003. 34:06say, "We earn 5.5% a year." Well, in
  1004. 34:10reality, obviously, we know that they
  1005. 34:11earn 5% a year, not 5.5% a year. In
  1006. 34:16fact, if we actually believed that they
  1007. 34:17earned 5.5% a year, we can go ahead and
  1008. 34:20take this number and do the whole 1 plus
  1009. 34:22r on this number and then we can
  1010. 34:25actually raise this to the five, right?
  1011. 34:27And if they were telling the truth, well
  1012. 34:29then they actually would have earned 30%
  1013. 34:31at the end of their 5-year sample, not
  1014. 34:3327%. So all of this was a segue that was
  1015. 34:37probably confusing. If you watch the
  1016. 34:38first video, maybe you have some
  1017. 34:40intuition as to what I'm talking about.
  1018. 34:41Um, we can literally ignore all of this
  1019. 34:44and we can just sort of fix up this
  1020. 34:45formula. So let's go ahead now. Um, and
  1021. 34:47instead of doing moo* dt with, uh, which
  1022. 34:49is arithmetic, let's now do geometric.
  1023. 34:52So we're actually going to need to do 1
  1024. 34:54plus the moo. and then we're going to
  1025. 34:56raise it to the DT and then we're going
  1026. 34:58to do minus one. So it's literally
  1027. 35:00everything I talked about. We can go
  1028. 35:02ahead and just delete all these cells
  1029. 35:03now. Um and that's what we're actually
  1030. 35:05going to do. Um and now let's go ahead
  1031. 35:07and continue the formula. So the rest of
  1032. 35:09this is going to be very simple. As we
  1033. 35:11can see, I'm literally just following
  1034. 35:12what's up here. So we're going to do
  1035. 35:13sigma and we're going to multiply sigma.
  1036. 35:15We can just use numpy square root
  1037. 35:18function and multiply sigma by the
  1038. 35:21roo<unk> of dt. Again, if you watch the
  1039. 35:22first video, that's just how we scale
  1040. 35:24the sigma, right? the volatility. Um,
  1041. 35:26and then we're going to go ahead and
  1042. 35:27multiply it by Z, right? And we're not
  1043. 35:30calling this Z of T. We're just calling
  1044. 35:32this Z cuz in this case it contains,
  1045. 35:34right, 10 * 12, 120 different size. And,
  1046. 35:37you know, we can run this. It's not
  1047. 35:38going to output anything. We're just
  1048. 35:39defining the function. But this is all
  1049. 35:41we need. Um, and now let's go ahead and
  1050. 35:43return something. And there's different
  1051. 35:45sort of things we can return, right? We
  1052. 35:47could just return rats. And that's going
  1053. 35:49to give us a numpy array. Um, a numpy n
  1054. 35:52dimensional array. Let me just go ahead
  1055. 35:54and show you how that looks. Right. So,
  1056. 35:56right now, we're just getting this numpy
  1057. 35:58array. And if you ever get stuck while
  1058. 36:00you follow along, just use the type
  1059. 36:02function. This is going to be your best
  1060. 36:03friend. It's just going to tell you what
  1061. 36:04data type we're looking at. As we can
  1062. 36:06see, it's a numpy n-dimensional array.
  1063. 36:09I'm going to talk about what that is in
  1064. 36:10just a second. Um, now for the rest of
  1065. 36:12this video, same as my last video, we're
  1066. 36:15really going to do everything we need to
  1067. 36:17do um in general in pandas. The z stuff
  1068. 36:19we can leave as a numpy thing since
  1069. 36:21we're just using it right away. maybe we
  1070. 36:22don't need a reference I again. Um but
  1071. 36:24in this case we're going to be using
  1072. 36:26pandas. So let's go ahead and import
  1073. 36:28pandas as pd and make sure to run that
  1074. 36:32cell. Um again you can go ahead and do
  1075. 36:35percent sign pip install pandas if you
  1076. 36:38don't have pandas installed right if
  1077. 36:39you're following along. Of course for me
  1078. 36:40it's installed already. Um and now what
  1079. 36:42we can do is we can actually cast this
  1080. 36:45from a numpy array into either a pandas
  1081. 36:47series or a panda um or a pandanda's
  1082. 36:49data frame. So if you cast this into a
  1083. 36:51series um and run this, it's now of type
  1084. 36:54series. Let's actually take a look at
  1085. 36:56it. Now this is a series. Um a series is
  1086. 36:59just sort of like this one-dimensional
  1087. 37:00time series. If you watch my last video,
  1088. 37:03um be general, at least me personally,
  1089. 37:05be generally uh want to view things as a
  1090. 37:07data frame. We're going to see why in
  1091. 37:09just a second. Um it looks a little
  1092. 37:10nicer. And essentially this is what
  1093. 37:12we're left with. So as I said, now we're
  1094. 37:14going to get a dataf frame. This is what
  1095. 37:16it looks like. If we do type, we can see
  1096. 37:18it's a dataf frame. Um, and then there's
  1097. 37:20something else that's going to be of
  1098. 37:21interest, which is going to be dotshape.
  1099. 37:24And this is just going to tell us the
  1100. 37:26dimensions of this data frame. A dataf
  1101. 37:28frame is just like a vector. Again, the
  1102. 37:30first video will sort of explain that.
  1103. 37:32Um, data frames will actually print the
  1104. 37:34shape out by default. We can see that
  1105. 37:35this is 120 rows by one column, right?
  1106. 37:38It's sort of condensing it down in the
  1107. 37:39middle, but we have 120 different data
  1108. 37:41points. Now, this is going to be another
  1109. 37:44little digression just for those of you
  1110. 37:45that are interested, but you might be
  1111. 37:47wondering, we know that Z, we know that
  1112. 37:50there's 120 different Z figures in this
  1113. 37:53Z numpy array, but moo is just a regular
  1114. 37:56number, which means that's just a
  1115. 37:58singular number. DT is just a singular
  1116. 38:00number. One is just a singular number.
  1117. 38:02Sigma is just a singular number. Right?
  1118. 38:04All of this stuff is just an individual
  1119. 38:06number within Python. and then we're
  1120. 38:08multiplying one number by this array of
  1121. 38:10120 numbers and we still get 120
  1122. 38:12numbers. Um this is just going to be a
  1123. 38:14little digression. So essentially this
  1124. 38:16works because we're doing something
  1125. 38:18called broadcasting. So let me just
  1126. 38:20explain what's actually happening um in
  1127. 38:22simple terms. So let me just go ahead
  1128. 38:24and construct uh just a regular let's
  1129. 38:26just say numpy array and let's just
  1130. 38:28construct this out of you know elements
  1131. 38:311 through 5. Right? So if you run this
  1132. 38:33this is all this looks like. Again, we
  1133. 38:35can go ahead and check out the type,
  1134. 38:37which is a numpy n-dimensional array.
  1135. 38:39And if we want to know how many
  1136. 38:41dimensions are in this n-dimensional
  1137. 38:43array, we can do nim for number of
  1138. 38:46dimensions. And it will say there's just
  1139. 38:47one dimension. Which is why when we run
  1140. 38:49this, it essentially just looks like a
  1141. 38:51regular Python list except it's a numpy
  1142. 38:53array. Um, so if we go ahead and we
  1143. 38:56multiply this one dimension that
  1144. 38:58contains five different data points by
  1145. 39:00let's just say two, what's going to
  1146. 39:02happen is something called broadcasting.
  1147. 39:04That just means that every single
  1148. 39:05element within this array is going to be
  1149. 39:07multiplied by two independently. So when
  1150. 39:09we run this, we now have 2 4 6 8 10. So
  1151. 39:13the same thing happened on this line is
  1152. 39:15we're essentially applying the sigma and
  1153. 39:17applying the mean to every single one of
  1154. 39:20these 120 eyes independently. And you
  1155. 39:23know that's really all we're doing.
  1156. 39:25Okay, so the function's literally done
  1157. 39:27already. Um let's go ahead and let's
  1158. 39:30call GBM, right? We're just going to use
  1159. 39:32the default parameters for now is fine.
  1160. 39:34And this is the random walk that we get
  1161. 39:36back. So let's go ahead and let's think
  1162. 39:38about things. So what are we actually
  1163. 39:41getting back? Is it prices? Is it
  1164. 39:43returns? Right? In this case, it's
  1165. 39:45returns. Right? Each one of these data
  1166. 39:47points is a monthly return. Let's just
  1167. 39:50go ahead and save this in a variable
  1168. 39:51called rats, right? So rats is what we
  1169. 39:55just saw. It's monthly returns. And
  1170. 39:57let's go ahead and let's plot this. So
  1171. 40:00if you think back to my last video, um
  1172. 40:02because this is directly already a
  1173. 40:03pandas dataf frame, we can literally
  1174. 40:05just call plot on it. Um now, same
  1175. 40:08thing, you might need to go ahead and do
  1176. 40:11hit install.
  1177. 40:13Oops, install mattplot lib. Again, for
  1178. 40:16me it's installed already. Um, by
  1179. 40:18default, the plotting engine that Pandas
  1180. 40:20uses is going to be Maplot lib again on
  1181. 40:22that website I talked about last time. I
  1182. 40:24was using like plotty. We're actually
  1183. 40:26going to do way more in-depth plotting
  1184. 40:28stuff um later on in the video. For now,
  1185. 40:30we literally are just going to do the
  1186. 40:31umplot method. It's going to handle
  1187. 40:33everything for us. And when we run this,
  1188. 40:35let's see what we get back. Boom. It
  1189. 40:38doesn't look too nice visually. Um, but
  1190. 40:40this is how returns look. We're not
  1191. 40:43actually calculating like a wealth index
  1192. 40:45yet, but let's go ahead and do that now.
  1193. 40:47As soon as we do this, hopefully things
  1194. 40:48will be more clear. So, if you think
  1195. 40:50back to my last video, how do we turn
  1196. 40:52this return series into, let's say, a
  1197. 40:55wealth index, which represents the
  1198. 40:56growth of $1. Well, let's go ahead. We
  1199. 40:59know that we need to do 1 plus rats,
  1200. 41:01right? 1 plus r. And if you wanted the
  1201. 41:04overall compound growth, we would dopra,
  1202. 41:06which is product. But we want to be able
  1203. 41:08to plot this. We don't just want the
  1204. 41:10terminal wealth. We want to see the
  1205. 41:11wealth at every single consecutive time
  1206. 41:14step. So if you think back to that
  1207. 41:15video, we're actually going to do cumro,
  1208. 41:18which stands for cumulative product. We
  1209. 41:20also introduced last video cumulative
  1210. 41:22maximum. Um in this case, we're going to
  1211. 41:23need the cumulative product. And this is
  1212. 41:26literally going to be um the wealth
  1213. 41:28index. Let's go ahead and put that in a
  1214. 41:30variable and let's take this and let's
  1215. 41:33plot it and let's see what we have. And
  1216. 41:35beautiful, right? Like look at this,
  1217. 41:37right? So you know, it's really not that
  1218. 41:40much code. But it's really quite simple
  1219. 41:41now that you're seeing and we're
  1220. 41:42essentially already done. Like this is a
  1221. 41:44mathematically sound complete working
  1222. 41:47brownie in motion function. Now we're
  1223. 41:49going to go ahead and we're going to
  1224. 41:50start to improve it and clean it up and
  1225. 41:52do a lot more interesting things. Um now
  1226. 41:54every time I run this cell, if you look
  1227. 41:56at the path, it's going to be slightly
  1228. 41:57different every single time we run it.
  1229. 42:00Um of course that's because it's a
  1230. 42:01stochastic model. Those zies are coming
  1231. 42:04out of that numpy random function um
  1232. 42:06differently every single time. Okay, so
  1233. 42:08let's go ahead and let's start to
  1234. 42:10improve this function. So in the intro
  1235. 42:12of this video, I probably would have
  1236. 42:14showed you a similar looking chart,
  1237. 42:16except there probably would have been
  1238. 42:17like 50 or 100 different random walks on
  1239. 42:19it, not just one. Let's go ahead and I'm
  1240. 42:22just going to sort of iterate over this
  1241. 42:24function, maybe just adding like one or
  1242. 42:26two new things at a time. So let me just
  1243. 42:27copy and paste it. And now let's go
  1244. 42:29ahead and have the ability to do this on
  1245. 42:32multiple scenarios, not just one. Um, so
  1246. 42:34let's go ahead and define a new
  1247. 42:36parameter. Let's just call this um n
  1248. 42:39scenarios and let's default this to like
  1249. 42:4250 for now. Um and this is going to
  1250. 42:44control how many random walks there are
  1251. 42:46on the graph. And this is actually going
  1252. 42:49to be really simple. Um we're not going
  1253. 42:50to again we're not going to need a for
  1254. 42:52loop. I'm going to talk about time
  1255. 42:53complexity in just a second. But a lot
  1256. 42:55of the time when we do this sort of
  1257. 42:57vectorzed stuff and statistical
  1258. 42:58numerical stuff in Python, we really
  1259. 43:01almost in a way want to avoid for loops.
  1260. 43:03I actually talked about that on one of
  1261. 43:05my first videos ever on my channel. It
  1262. 43:07was the third video about time
  1263. 43:08complexity. We actually used a financial
  1264. 43:10example in that video and in the next
  1265. 43:12video on space complexity. I think we
  1266. 43:14actually also did rolling returns. You
  1267. 43:15can see how this finance stuff, you
  1268. 43:17know, sort of seeps in. Um, but you
  1269. 43:19know, we're going to talk about time
  1270. 43:20complexity in a second. But in general,
  1271. 43:22we want to avoid for loops. We want to
  1272. 43:24push back as much logic we can almost
  1273. 43:27like into these sort of like numpy and
  1274. 43:29pandas libraries instead of doing it
  1275. 43:31oursel. Um, anyways, we're actually
  1276. 43:33going to take advantage of broadcasting
  1277. 43:35again. So, all we really want to do is
  1278. 43:37we want to change the shape of our
  1279. 43:39returns. Instead of being, you know, one
  1280. 43:42by 120, we want it to be maybe like 50
  1281. 43:45by 120. Um, so let me go ahead and
  1282. 43:47showcase this. Um, let's just set size
  1283. 43:49to, let's say, 120, right? This is what
  1284. 43:51we're doing right now. Um, and again, if
  1285. 43:54we go ahead and we do, you know, n dim,
  1286. 43:56we can see that it's only one dimension.
  1287. 43:59Now, there's no way you would have known
  1288. 44:00this unless you read the documentation,
  1289. 44:02but it turns out that size could be a
  1290. 44:04single number, but it could actually be
  1291. 44:06a tpple that contains two numbers, and
  1292. 44:08then it will actually give us a
  1293. 44:10two-dimensional array of random normals
  1294. 44:12instead of one. So, we can go ahead and
  1295. 44:14set this to let's say number of steps.
  1296. 44:16And then for the second one, we're
  1297. 44:18actually going to use number of
  1298. 44:19scenarios, right? In this case, if you
  1299. 44:21don't default that to 50, this is how
  1300. 44:23this function call looks. And if you run
  1301. 44:25this now, I know it might not look too
  1302. 44:27pretty. Um, but we can see that the
  1303. 44:28shape is actually 120x 50. Um, we can
  1304. 44:32actually go ahead and cast this into a
  1305. 44:35PDA frame just if you want to visualize
  1306. 44:37it better. And now we can see that we
  1307. 44:39have 120 rows by 50 columns. And you
  1308. 44:41know, this is sort of how we can do this
  1309. 44:43over, you know, 50 or 100 different
  1310. 44:45scenarios. Um, so let's actually go
  1311. 44:47ahead and do that. So all we're going to
  1312. 44:50do is instead of just number of steps,
  1313. 44:52we're going to do number of steps and
  1314. 44:53then number of scenarios. And it's
  1315. 44:56actually that simple. We can go ahead
  1316. 44:57and run this. Let me just reuse this
  1317. 45:00plotting stuff and let's see how this
  1318. 45:02looks.
  1319. 45:04And boom. Right? It's that simple. Now,
  1320. 45:06obviously, this doesn't look the best
  1321. 45:08because of the legend. Um, we can go
  1322. 45:10ahead and clean that up. Or what we
  1323. 45:11could do is we actually could just say
  1324. 45:13legend equals false. Um, that will take
  1325. 45:15care of that for us. And the chart looks
  1326. 45:18kind of tiny. and it looks kind of
  1327. 45:19squished. Um, we can actually also fix
  1328. 45:21that up with a parameter called fig
  1329. 45:23size, which just means figure size. In
  1330. 45:25fact, we're going to be talking all
  1331. 45:27about figures later on in the video. Um,
  1332. 45:29this is a tpple. Uh, we're just going to
  1333. 45:31hand this, let's just say like 126, and
  1334. 45:33it should be a little bit bigger now.
  1335. 45:35And we're done, right? I probably would
  1336. 45:36have showed you something like this in
  1337. 45:37the intro of the video. And like, you
  1338. 45:40know, it's barely any code. Now, you can
  1339. 45:41start to see, you know, the math stuff.
  1340. 45:43Like, it looked complex, but it's really
  1341. 45:45incredibly simple. And this is
  1342. 45:47beautiful. And every time I rerun this
  1343. 45:49cell, we're going to get, you know,
  1344. 45:50different random walks back. And, you
  1345. 45:53know, we're going to see, we're going to
  1346. 45:54do the interactive stuff at the end of
  1347. 45:55the video. Let me just, you know, change
  1348. 45:57number of scenarios to 100. And now,
  1349. 45:59when we rerun this, there's going to be
  1350. 46:00100 different random walks instead of
  1351. 46:02just 50. That's really all there is to
  1352. 46:04it. Okay. So, again, we're we're done,
  1353. 46:07right? The function works. But let's
  1354. 46:09just go ahead and let's keep on
  1355. 46:10improving it. So, let me go ahead and
  1356. 46:12take what I have already. Um, and
  1357. 46:14there's a couple different things we
  1358. 46:15might want to do. Now, first of all,
  1359. 46:17this function right now is giving us
  1360. 46:19returns, which is completely okay. It's
  1361. 46:20up to us what we want. Um, but right
  1362. 46:23now, I'm just interested in sort of
  1363. 46:24plotting it. So, instead of needing to
  1364. 46:26sort of do all this stuff after we call
  1365. 46:28the function, let's just go ahead and do
  1366. 46:29this inside of the function. So, instead
  1367. 46:31of just directly returning the data
  1368. 46:33frame of returns, let's go ahead now
  1369. 46:35and, you know, just update rats to just
  1370. 46:38be a dataf frame now. Um, and now what
  1371. 46:40we're going to return is going to be the
  1372. 46:41wealth index directly. So, we can just
  1373. 46:43do 1 + rats. prod directly inside of the
  1374. 46:47function. And really really that's all
  1375. 46:49we're doing. Um next time we call the
  1376. 46:51function GBM, we can just plot it
  1377. 46:53directly, right? Maybe we still want
  1378. 46:55legend to be false. And maybe we still
  1379. 46:57want fig size to be 126. Um but now we
  1380. 47:00don't need to do this qro stuff outside
  1381. 47:02of the function. Now we're just getting
  1382. 47:04back a wealth index directly from the
  1383. 47:05function. That's just one additional
  1384. 47:07thing we're adding. Um now the second
  1385. 47:09thing I'm going to add. So right now
  1386. 47:11each random block here represents the
  1387. 47:13growth of $1. They all start at one and
  1388. 47:16they don't exactly start at one. We're
  1389. 47:18actually going to be fixing that in just
  1390. 47:19a second. Um, that's just more of like a
  1391. 47:21plotting issue mathematically. They all
  1392. 47:23start at one. Um, and then they grow
  1393. 47:25over the 10ear sample. In this case, $1
  1394. 47:27grew to, you know, maybe just over $5.
  1395. 47:30But what if we want to actually change
  1396. 47:31the initial wealth? That's actually a
  1397. 47:33really easy change. We can make this a
  1398. 47:35parameter and then that will be
  1399. 47:36available to change once we actually get
  1400. 47:38into the interactive plotting stuff. So
  1401. 47:40instead of the growth of $1, let's maybe
  1402. 47:42default it to the growth of $100. Um we
  1403. 47:44can call this maybe like initial though
  1404. 47:47if you think back to the formula. Um
  1405. 47:49right s of t is sort of like the price
  1406. 47:52at time t. Um in this case we can
  1407. 47:54actually also call it s of zero. Of
  1408. 47:56course it's just a variable name. It
  1409. 47:57just means the initial starting price um
  1410. 47:59for the index. And let's just start
  1411. 48:01things off by default at $100. And I
  1412. 48:04think we would have probably covered
  1413. 48:05this in my last video when we did wealth
  1414. 48:06index stuff. Um but it's a super easy
  1415. 48:08change. We literally can just do S0
  1416. 48:11multiplied by all this stuff and now it
  1417. 48:13will be the growth of $100 instead of
  1418. 48:15the growth of $1. We can go ahead and
  1419. 48:17run this and it won't look any
  1420. 48:18different. But if you look at the axis
  1421. 48:20now, we can see that everything starts
  1422. 48:21at $100. So that's just another really
  1423. 48:23simple change. So again, we're more or
  1424. 48:26less done. Um, but now we're going to do
  1425. 48:28something really cool. So now we're
  1426. 48:30going to see, all right, we're happy
  1427. 48:31with this function for now. Let's go
  1428. 48:33ahead and see if we can make it more
  1429. 48:35efficient. Now again, there's no way you
  1430. 48:37would have known to do this if you
  1431. 48:39didn't read the documentation or if I
  1432. 48:41didn't tell you myself. Um, but it turns
  1433. 48:42out there's actually really useful
  1434. 48:44additional parameters inside of this
  1435. 48:46numpy random normal function.
  1436. 48:48Specifically, there's going to be lock
  1437. 48:50for location and scale and we're going
  1438. 48:52to see how we can actually use these to
  1439. 48:53make the function more efficient. So as
  1440. 48:56I said when we do these sort of like
  1441. 48:57numerical applications in Python um we
  1442. 49:00essentially want to do as little
  1443. 49:01computation ourself in Python and we
  1444. 49:04want to push as much of it as possible
  1445. 49:07into these sort of libraries like pandas
  1446. 49:10and numpy that are written in C and
  1447. 49:12they're highly efficient. Um and I
  1448. 49:13talked all about this in my time
  1449. 49:15complexity video. I would actually
  1450. 49:16recommend you watch that though it's not
  1451. 49:18necessary. Um now let's also talk about
  1452. 49:20the sort of concept of like loops. So we
  1453. 49:23don't see any actual for loops natively
  1454. 49:26inside of Python. But anytime we have an
  1455. 49:29operation even where we just add like a
  1456. 49:31one to a number rights is not one
  1457. 49:34individual number. Rhetts is this
  1458. 49:37massive data frame right? This massive
  1459. 49:39like two-dimensional vector you can say
  1460. 49:41of you know 120 by 100 right there's
  1461. 49:44hundreds if not thousands of data points
  1462. 49:46that will be inside of rats depending on
  1463. 49:48the parameters. So when we do one plus
  1464. 49:50rats, we're not adding one number to
  1465. 49:52another number. We're adding one number
  1466. 49:54to hundreds or thousands of numbers,
  1467. 49:57right? So anytime we see this
  1468. 49:59multiplication or we see this, there's
  1469. 50:01actually an implicit loop that's
  1470. 50:03happening. Um, let's just make things
  1471. 50:05more efficient. So instead of generating
  1472. 50:08random normals and then applying a moo
  1473. 50:10and sigma, it turns out that we can
  1474. 50:12apply the moo and sigma inside of the
  1475. 50:14random normal function itself. So, as I
  1476. 50:17showed you in documentation, there's um
  1477. 50:20it accepts a parameter called lock,
  1478. 50:21which is essentially just location,
  1479. 50:23which essentially just means the mean.
  1480. 50:25So, by default, the distribution has a
  1481. 50:27mean of zero. But we can literally just
  1482. 50:29hand it our mean
  1483. 50:32as a parameter. And now, right, this
  1484. 50:35numpy function is going to be applying
  1485. 50:37all of these means for us. We don't
  1486. 50:39actually need to like worry about doing
  1487. 50:41it oursel. Um, and then in addition to
  1488. 50:43that, there's another parameter called
  1489. 50:45scale, which literally just means the
  1490. 50:47standard deviation. So, same thing here.
  1491. 50:50We can actually go ahead and rip this
  1492. 50:51out and just do it directly inside of
  1493. 50:54the function, right? In fact, we can
  1494. 50:56just remove these brackets here if we
  1495. 50:57want to, right? And now we're just
  1496. 50:58generating random normal numbers that
  1497. 51:01already have the standard deviation and
  1498. 51:02mean that we need. We can now just fully
  1499. 51:05get rid of that. And then instead of Z,
  1500. 51:07we can just call these our returns
  1501. 51:09already. And that's it. Right? this is
  1502. 51:11and you know you can go ahead and
  1503. 51:13actually time how long this takes to
  1504. 51:14execute on your machine. It might be a
  1505. 51:17lot quicker or it might actually be the
  1506. 51:19same amount of time just depending on
  1507. 51:21your exact hardware and you know
  1508. 51:22obviously this goes deeper but in
  1509. 51:24general this is like excellent practice.
  1510. 51:26Um so now same thing we can go ahead and
  1511. 51:29we can just call and plot this function
  1512. 51:31and of course it's not going to look any
  1513. 51:33different. Let's make sure we run this.
  1514. 51:35Same thing we still get everything we
  1515. 51:36need. Okay, now last sort of thing on
  1516. 51:39efficiency. You know, I don't know if
  1517. 51:41you guys find this like boring or
  1518. 51:42whatever. Obviously, we're going to get
  1519. 51:43to the interactive plotting stuff, but
  1520. 51:45this is like fascinating, right? This
  1521. 51:47video isn't just for finance. This is
  1522. 51:50for everything. It's for coding. It's
  1523. 51:52for mathematics, right? So, I'm just
  1524. 51:53going to sort of walk through
  1525. 51:54everything. Um, again, you could just
  1526. 51:56leave this function and we could just be
  1527. 51:57done and that's completely okay. It's
  1528. 51:59mathematically correct. But, we can
  1529. 52:00actually make it even more efficient. So
  1530. 52:03if we take a look at this negative one
  1531. 52:05here, right, we're handing it the mean
  1532. 52:07and then we're turning it into a per
  1533. 52:08month mean and then we subtract a one,
  1534. 52:11even though the next time we sort of use
  1535. 52:13this RTS variable, we're just adding a
  1536. 52:15one to it again. And when we add the one
  1537. 52:17over here, we're adding a one to, you
  1538. 52:20know, let's say like 120 different data
  1539. 52:22points or potentially even thousands or
  1540. 52:25tens of thousands of data points. Um,
  1541. 52:26why would we sort of subtract a one and
  1542. 52:28then add a one to the same thing? we can
  1543. 52:30just get rid of both of those operations
  1544. 52:32um and literally like get rid of another
  1545. 52:34loop. So we can literally just not
  1546. 52:37subtract one and then we don't have a
  1547. 52:39need to add one anymore and this will
  1548. 52:42still work as intended. Um now we might
  1549. 52:44want to make this a variable name
  1550. 52:45instead of rats. This technically, you
  1551. 52:47know, is like rats + 1 or rats p1. Um
  1552. 52:52but everything will still work as
  1553. 52:53intended, you know, and we can go ahead.
  1554. 52:55We might as well just clean up the
  1555. 52:56function while we're at it. We don't
  1556. 52:57need a whole separate line. Let's just
  1557. 53:00put that there. Um, and we should be
  1558. 53:01good, right? Why would we sort of do all
  1559. 53:03that unnecessary computation? Um, we can
  1560. 53:05now run this cell again and run this
  1561. 53:07again. And of course, visually there's
  1562. 53:09no changes, right? Mathematically with
  1563. 53:11the numbers, there's no changes. Um, we
  1564. 53:12just made it more efficient. Okay, so
  1565. 53:15again, the function is good. It's a
  1566. 53:17beautiful function. It's a beautiful
  1567. 53:19chart. It's mathematically correct. We
  1568. 53:21could just call it a day and we could
  1569. 53:22just move on to interactive plotting.
  1570. 53:24But there's one last thing I want to
  1571. 53:26address because it's kind of annoying me
  1572. 53:28and it might be annoying you too. So,
  1573. 53:30you know, the goal here is we just want
  1574. 53:31to graph out like the growth of $100.
  1575. 53:34But when we look here, we can see that
  1576. 53:36all of these lines are not starting at
  1577. 53:38$100 instead of all starting on the same
  1578. 53:40data point. There's like a massive gap.
  1579. 53:42And obviously, right, you might sort of
  1580. 53:44know if you're following along. Um, but
  1581. 53:45we're applying the first day's return to
  1582. 53:48the first data point that's already in
  1583. 53:50our wealth index. We can fix this by
  1584. 53:52essentially prepending just like a
  1585. 53:54static one. Um, and then the chart's
  1586. 53:56going to look better. But we need to be
  1587. 53:58extremely careful with how we do that or
  1588. 54:01we could actually sort of like mess up
  1589. 54:04the mathematical integrity of the
  1590. 54:06function. So, I'm going to go through a
  1591. 54:08quick example here just to show you guys
  1592. 54:10um exactly what I mean. Now, if this
  1593. 54:12part doesn't make sense, obviously feel
  1594. 54:14free to skip ahead. Um, but I think this
  1595. 54:16is actually pretty useful. So, I'm going
  1596. 54:19to go ahead and we're going to do two
  1597. 54:20different functions and I'm going to
  1598. 54:22show you how you might think we can fix
  1599. 54:25the graph and it will actually mess up
  1600. 54:27the function mathematically and then
  1601. 54:28we'll go through the actual proper way.
  1602. 54:30Um, so the first function where we're
  1603. 54:32not yet addressing this issue, we know
  1604. 54:34it's completely good mathematically. We
  1605. 54:36can go ahead and call this let's just
  1606. 54:38say GBM original and we're going to go
  1607. 54:40ahead in this function and we're going
  1608. 54:43to go ahead and we're going to insert
  1609. 54:44this static one. and we want to set up
  1610. 54:46an experiment so we can make sure we're
  1611. 54:48not messing up the function
  1612. 54:50mathematically. Um, so one sort of thing
  1613. 54:52we might just want to look at is let's
  1614. 54:54just say the terminal wealth. So we want
  1615. 54:56to look at, you know, the very last data
  1616. 54:58point and then we're actually going to
  1617. 55:00end up shrinking the sigma down to zero
  1618. 55:02just to remove the volatility so we can
  1619. 55:04get accurate results. This is just like
  1620. 55:06a tiny sort of mini little test, right?
  1621. 55:08So let's go ahead and run this cell. Um,
  1622. 55:10and let's go ahead and call GBM
  1623. 55:12original. Um, and let's go ahead. Well,
  1624. 55:14let's just run the function, right? We
  1625. 55:16get back this two-dimensional data
  1626. 55:17frame, 120 rows by 100 columns. Um, and
  1627. 55:20let's say we want to look at the
  1628. 55:22terminal wealth. So, we just want to
  1629. 55:23look at the very last time step, not all
  1630. 55:26120 time steps. Um, so, you know, if you
  1631. 55:28think back to my first video, we have,
  1632. 55:30you know, lock for location or IO for
  1633. 55:32integer location. And we can go ahead
  1634. 55:35and just hand this a negative one.
  1635. 55:36That's just going to give us the very
  1636. 55:38last index here. Let's go ahead and run
  1637. 55:40this. Now, we only get one back.
  1638. 55:42Although because this is now single
  1639. 55:44instead of multi-dimensional, this is
  1640. 55:46actually giving us again we can go ahead
  1641. 55:47and do type. Um this is actually going
  1642. 55:50to be a series instead of a dataf frame.
  1643. 55:52Let's just check this. Sure enough, it
  1644. 55:54is a series. Um if you want to keep it
  1645. 55:56as a data frame, you know, we could cast
  1646. 55:57it again or we can just take a little
  1647. 55:59shortcut and use the colon slicing
  1648. 56:01operator. And now we just write it
  1649. 56:03really doesn't matter. It's all the same
  1650. 56:04data. Um it just depends how you want to
  1651. 56:05look at it. And now let's say that we
  1652. 56:07want to view um the average terminal
  1653. 56:10wealth. So right we have you know all
  1654. 56:13these sort of trials here. What if we
  1655. 56:15just want to view the average um we can
  1656. 56:17just take the mean and this alone is
  1657. 56:20going to give us some issues because
  1658. 56:21we're actually using the wrong axis. Um
  1659. 56:23you essentially need to specify do you
  1660. 56:25want to take the mean you know over like
  1661. 56:27the rows or over the columns. Um you
  1662. 56:30know and if we didn't sort of slice it
  1663. 56:32we wouldn't have that issue. So you know
  1664. 56:34again there's no right answer. There's
  1665. 56:35many different ways to do it. Um, but if
  1666. 56:37you want to sort of fix this up, um, we
  1667. 56:39would just need to specify, um, an axis
  1668. 56:41of one just to make sure we're actually
  1669. 56:43summing um, along um, along that sort of
  1670. 56:46horizontal row. Um, and we should be
  1671. 56:48good. Now, the issue is that every
  1672. 56:50single time I run this, even though
  1673. 56:52we're taking the average over 100
  1674. 56:53different trials, you know, we're still
  1675. 56:55going to get a slightly different number
  1676. 56:56back every single time. So, for the sake
  1677. 56:59of this experiment, let's go ahead and
  1678. 57:01just set the volatility to zero. Now,
  1679. 57:04when we run this, right, 196.715,
  1680. 57:07right? Every time we run this, it's the
  1681. 57:09exact same number. Um, obviously, we
  1682. 57:11don't even need to take an average
  1683. 57:13anymore since all of these numbers are
  1684. 57:14going to be the same, right? We can go
  1685. 57:16ahead and do one scenario. We can just
  1686. 57:18snip the first one off. I'm just going
  1687. 57:20to leave it as this for now. Um, you
  1688. 57:22know, it it doesn't really matter.
  1689. 57:23Obviously, there's a bunch of ways to do
  1690. 57:24it. Um, the only thing that matters is
  1691. 57:25we are just looking at the terminal
  1692. 57:27wealth, um, you know, over this, uh, 120
  1693. 57:30time steps using these input parameters.
  1694. 57:32And the correct answer is we're going to
  1695. 57:34finish on average with $196. So now
  1696. 57:37let's go ahead into this function and
  1697. 57:40let me show you how you might think you
  1698. 57:41can fix the issue but would actually
  1699. 57:44sort of mess up the function. So let's
  1700. 57:46call this GBM overwrite. And you might
  1701. 57:50say, you know, you said just sort of
  1702. 57:52make that very first data point just a
  1703. 57:54static one. So we could go ahead and we
  1704. 57:57could just index right into that very
  1705. 57:59first time step and set everything equal
  1706. 58:02to one. And broadcasting is going to
  1707. 58:03take care of this. And if we go ahead
  1708. 58:06and graph this, right, let's go ahead
  1709. 58:08and graph this by doing plot. And then
  1710. 58:11we might need to do legend equals false
  1711. 58:14um fig size. Right? If we go ahead and
  1712. 58:15graph this, it's going to look like the
  1713. 58:17issues fixed. Uh let me just go ahead
  1714. 58:20and change those parameters back to
  1715. 58:230.15. Um, I'll change this back when we
  1716. 58:25actually do the test. Um, but this is
  1717. 58:26actually going to look like it solved
  1718. 58:28the issue, right? Every single data
  1719. 58:30point, every time we run this, the very
  1720. 58:32sort of first initial time step is now
  1721. 58:34centered on that one, which then later
  1722. 58:36becomes 100. But the issue here is
  1723. 58:38because we overwrote um that very first
  1724. 58:41sort of days return. We're actually sort
  1725. 58:44of messing up our numbers. So, let's go
  1726. 58:45ahead here and let's do the same test
  1727. 58:48for this. Um, now don't forget, we're
  1728. 58:50going to change this back to a sigma of
  1729. 58:52zero. Um, and let's go ahead and take
  1730. 58:54this and we're going to run this in just
  1731. 58:56a second. Now, just so you can sort of
  1732. 58:58think about this, what do you think the
  1733. 59:00results of the test are going to be?
  1734. 59:01Right now, ideally, if we actually sort
  1735. 59:04of prepend that one, we're not actually
  1736. 59:06going to change the average terminal
  1737. 59:07wealth, right? We shouldn't actually be
  1738. 59:08changing the mathematics of the
  1739. 59:10function. We really just want to sort of
  1740. 59:11solve this plotting issue. Um, right?
  1741. 59:14But because the MO is on average, right,
  1742. 59:17we're handing it a positive expected
  1743. 59:18return and then we're obviously adding
  1744. 59:20one to those numbers. That means that
  1745. 59:22when we hit this line, since we're
  1746. 59:24overwriting the first day's return,
  1747. 59:25essentially what we're doing is we're
  1748. 59:27trading a number that is going to be
  1749. 59:28bigger than one on average with just the
  1750. 59:31number one. So if you trade a number
  1751. 59:33that's bigger than one with one, right?
  1752. 59:35Meaning if we overwrite a positive
  1753. 59:37return with a flat return, that means
  1754. 59:39that at the end of the sample on
  1755. 59:41average, we're going to have less
  1756. 59:42wealth. So in this case, the normal
  1757. 59:44case, we should end with $196. But in
  1758. 59:47the overwrite case, uh, oops, we didn't
  1759. 59:50do the right function name there. In the
  1760. 59:51overwrite case, we actually have 195
  1761. 59:54instead of 196. So, we actually messed
  1762. 59:58up our numbers mathematically, right?
  1763. 1:00:00This sort of like average return is
  1764. 1:00:01messed up. The volatility might be
  1765. 1:00:03messed up. And, you know, you probably
  1766. 1:00:04wouldn't have noticed this. Um, super
  1767. 1:00:06simple fix. Let's just go ahead and fix
  1768. 1:00:08it now. So, this example has already
  1769. 1:00:09taken up like a lot of time even though
  1770. 1:00:11it's not that serious. Um, so I'm going
  1771. 1:00:13to go ahead and kind of just like blaze
  1772. 1:00:14through this. Um, all we're really doing
  1773. 1:00:16is we're just going to be prepending.
  1774. 1:00:18Let's call this prepend. We're just
  1775. 1:00:19going to be, um, prepending the
  1776. 1:00:20appropriate size. Uh, essentially just
  1777. 1:00:22like an array just full of ones. Um,
  1778. 1:00:24prepending it. And then, you know, we're
  1779. 1:00:26not actually overwriting data. Um, so
  1780. 1:00:28let's just go ahead and get that done.
  1781. 1:00:29So, let's just take the rats p1. Um, and
  1782. 1:00:32then we're going to be updating this.
  1783. 1:00:33And we're going to be using a function
  1784. 1:00:34called pd.conat. I believe I talked
  1785. 1:00:37about this in my first video. Um, it
  1786. 1:00:38takes in an array of, in this case, it's
  1787. 1:00:41going to be two different things in this
  1788. 1:00:42array. Two different data frames. This
  1789. 1:00:44is going to concatenate both together.
  1790. 1:00:45So the first one we're going to go ahead
  1791. 1:00:47and we're going to call the pd.dataf
  1792. 1:00:49frame constructor and we're actually
  1793. 1:00:50going to hand it um a numpy
  1794. 1:00:53function. Um what this function is going
  1795. 1:00:55to do is it's basically just going to
  1796. 1:00:56make a numpy n dimensional array of
  1797. 1:00:59ones. And now we can just go ahead and
  1798. 1:01:00specify the shape. And in this case the
  1799. 1:01:03shape is just going to be one by the
  1800. 1:01:05number of scenarios. Um, now if you want
  1801. 1:01:07to know exactly why, go ahead and make a
  1802. 1:01:08new cell if you're following along and,
  1803. 1:01:10you know, see if you can sort of almost
  1804. 1:01:12do this before I write the code, right?
  1805. 1:01:14Um, I'm I'm sort of just going to gloss
  1806. 1:01:15over this. We're literally just
  1807. 1:01:16inserting, you know, one whole column of
  1808. 1:01:18ones. Um, and then the second argument
  1809. 1:01:20is we're just going to be prepending
  1810. 1:01:22this column of ones with our existing
  1811. 1:01:24data frame. We can actually just copy
  1812. 1:01:26that from here. Um, and that's basically
  1813. 1:01:28it. Now, when we actually run this, or
  1814. 1:01:31let's just go ahead and do this now. Um,
  1815. 1:01:33so let's just go ahead and change that
  1816. 1:01:34to that. And this is called GBM prepend.
  1817. 1:01:38Now when we run this function, we're
  1818. 1:01:39going to see something look a little
  1819. 1:01:41weird. Um the issue is this is sort of
  1820. 1:01:43like the row that was returned by the
  1821. 1:01:45numpy ones function. Um this sort of you
  1822. 1:01:47know has an index of zero whereas our
  1823. 1:01:49first day also has an index of zero. We
  1824. 1:01:51can essentially avoid this um by just
  1825. 1:01:53handing an additional flag into concat
  1826. 1:01:56called ignore index. We're just going to
  1827. 1:01:57set this equal to true. Again go ahead
  1828. 1:01:59and read the documentation and we'll
  1829. 1:02:00give you the details. It's not that big
  1830. 1:02:02of a deal. um that's just sort of going
  1831. 1:02:04to fix that issue and make the indexes
  1832. 1:02:05align. Um and that's really all we need
  1833. 1:02:08to do. And now the data won't be messed
  1834. 1:02:10up. So now let's go ahead and let's do
  1835. 1:02:12the same test but on prepend um the
  1836. 1:02:15sigma zero, right? Let's run this. We
  1837. 1:02:17can rerun those. It won't change. And
  1838. 1:02:19when we run this, ideally we're going to
  1839. 1:02:20get a terminal wealth of exactly this
  1840. 1:02:23number. Let's see. And perfect. Right.
  1841. 1:02:25So this was just a test just to sort of
  1842. 1:02:27see are we messing up the function
  1843. 1:02:28mathematically. I probably spent too
  1844. 1:02:30much time on it. Um, but this is
  1845. 1:02:32essentially just a proper function. Now,
  1846. 1:02:34now obviously something else to note,
  1847. 1:02:36um, is you're probably seeing this,
  1848. 1:02:38right? I'm seeing it, too. Um, because
  1849. 1:02:39we're just prepending, uh, this entire
  1850. 1:02:41sort of like row of ones is we now have
  1851. 1:02:43121 rows instead of 100 rows, which
  1852. 1:02:46makes sense. Um, now in this video, I'm
  1853. 1:02:48like okay with that personally. I'm just
  1854. 1:02:50going to leave it. Um, obviously, you're
  1855. 1:02:52more than free to come up here and, you
  1856. 1:02:54know, number of scenarios or number of
  1857. 1:02:56steps, right? You can go ahead and just
  1858. 1:02:57do like a minus one if you want to
  1859. 1:02:59correct that. that's completely okay.
  1860. 1:03:01Um, you know, but it doesn't actually uh
  1861. 1:03:04it doesn't actually like mess up the
  1862. 1:03:05mathical mathematical integrity of the
  1863. 1:03:07function. So, I'm just going to let it
  1864. 1:03:09have, you know, 121 instead of 120 time
  1865. 1:03:12steps in this case. That's okay with me
  1866. 1:03:14personally. Um, so let's go ahead and
  1867. 1:03:16let's take this function now and let's
  1868. 1:03:18just remove the prepend. And going
  1869. 1:03:20forward, this is the actual GBM function
  1870. 1:03:24that we're happy with. Um, we can go
  1871. 1:03:26ahead and we can do the whole plotting
  1872. 1:03:27thing. So let's call GBM and do plot and
  1873. 1:03:31let's just turn off the legend and let's
  1874. 1:03:33do fig size 126 and right we still
  1875. 1:03:38didn't change the sigma back. That was
  1876. 1:03:39just for that sort of example. Let's
  1877. 1:03:41just change that back to 0.15. Let's run
  1878. 1:03:43this. And beautiful. Right now we know
  1879. 1:03:46that the mathematics is still intact. Um
  1880. 1:03:48and we can now see on the chart we have
  1881. 1:03:50the statically inserted data point. So
  1882. 1:03:52everything sort of starts instead of
  1883. 1:03:54having a gap and we're done. Right? This
  1884. 1:03:56is beautiful. Right? I could literally
  1885. 1:03:57print this out, put it on my wall, put
  1886. 1:03:58in a picture frame. Right, we're done.
  1887. 1:04:01Okay, so now that we're done, and again,
  1888. 1:04:03you know, this video could easily be
  1889. 1:04:05like 500 hours long. We could keep on
  1890. 1:04:07tweaking things, but now that we're
  1891. 1:04:09done, um, let's go ahead and actually do
  1892. 1:04:11the interactive plotting. This is
  1893. 1:04:13probably going to be like the most sort
  1894. 1:04:14of fun part of the video. Um, we can
  1895. 1:04:16actually just do some, uh, some markdown
  1896. 1:04:20here. Let me just call this interactive
  1897. 1:04:23plotting. Um, in all the code I'm
  1898. 1:04:25probably going to, you know, upload to
  1899. 1:04:26my GitHub after and you can reference
  1900. 1:04:28the code yourself, though I think it
  1901. 1:04:29might be helpful if you code along. So,
  1902. 1:04:31let me give you guys the sort of premise
  1903. 1:04:33or the demo of how we get started with
  1904. 1:04:35making this plot interactive. Um, so we
  1905. 1:04:38are going to be needing a library which
  1906. 1:04:39is called IPI widgets. So, we're going
  1907. 1:04:41to go ahead and we're going to import
  1908. 1:04:43ipi widgets. Um, let's just say as
  1909. 1:04:46widgets. And this is going to be the
  1910. 1:04:48library that's going to enable us to do
  1911. 1:04:49all of this stuff. This is like an
  1912. 1:04:51awesome library. This is incredibly
  1913. 1:04:53helpful not just within computational
  1914. 1:04:55finance but within all sorts of things
  1915. 1:04:57inside of Python. Um so let's sort of
  1916. 1:05:00say how this works. Now of course same
  1917. 1:05:02thing you might need to go ahead and do
  1918. 1:05:04you know a pip install IPI widgets as
  1919. 1:05:07always right just if you get lost or
  1920. 1:05:09whatever. Um of course for me it's
  1921. 1:05:10installed already. Um and let's go ahead
  1922. 1:05:12and take a look at what we can actually
  1923. 1:05:13do. So the premise of how we actually
  1924. 1:05:15build these interactive UIs is every
  1925. 1:05:18single thing we have like an input for
  1926. 1:05:20visually corresponds with an actual
  1927. 1:05:22parameter into a Python function. So
  1928. 1:05:25before we do this with this sort of
  1929. 1:05:27actual chart, let's just do this in
  1930. 1:05:29general for an extremely basic Python
  1931. 1:05:31function, right? So let's just define a
  1932. 1:05:33function literally just funk defaf and
  1933. 1:05:35let's just say n for a number is the
  1934. 1:05:38input. Um, and all this is going to do
  1935. 1:05:40is it's just going to return, let's just
  1936. 1:05:42say like n minus two. We're just going
  1937. 1:05:44to double a number. Obviously, if you go
  1938. 1:05:46ahead and run funk of number five, we're
  1939. 1:05:48going to get back 10. Right? This is all
  1940. 1:05:50we're doing. And let's see how we can
  1941. 1:05:52actually plot this interactively. So,
  1942. 1:05:54let's go ahead and we're going to do
  1943. 1:05:55widgets. And at first, we're just going
  1944. 1:05:57to use a method called interact. Um,
  1945. 1:06:00next time we use this on the actual
  1946. 1:06:01plot, I'm going to use interactive
  1947. 1:06:03instead of interact. They're basically
  1948. 1:06:05the same thing. This is just going to
  1949. 1:06:06handle sort of everything for us. And
  1950. 1:06:08then we go ahead and we hand to
  1951. 1:06:10widgets.inact the function that we want
  1952. 1:06:13to make interactive such as funk. Um,
  1953. 1:06:16now if we just do this, we're actually
  1954. 1:06:17going to get an error. Let's go ahead
  1955. 1:06:19and run this. It's basically just going
  1956. 1:06:20to say, you know, it can't find uh a
  1957. 1:06:23widget for n. Um, all we need to do is
  1958. 1:06:25we either need to default n. Um, or we
  1959. 1:06:28can do something else. So if we default
  1960. 1:06:30n and we run this, boom, right? We now
  1961. 1:06:33have a slider for n. it defaults to 10
  1962. 1:06:36and the output here is 20. And like this
  1963. 1:06:38is literally all the code we need,
  1964. 1:06:40right? The widget. Interactive function
  1965. 1:06:41does everything for us. If we slide this
  1966. 1:06:43up, we can now see it's changing in real
  1967. 1:06:45time. Um, and then if we don't want to
  1968. 1:06:47default this, we can actually have a
  1969. 1:06:49little more granular control. We can
  1970. 1:06:50essentially just hand it the argument
  1971. 1:06:53called n. And now we can go ahead and
  1972. 1:06:55this is actually a tpple that we hand
  1973. 1:06:56it. And essentially what goes into this
  1974. 1:06:58tpple is like start stop. So let's say
  1975. 1:07:01we want the minimum to be zero and we
  1976. 1:07:03want the max to be 100. That means when
  1977. 1:07:06we run this uh or let's run this that
  1978. 1:07:08means that we uh the default is going to
  1979. 1:07:09be the midpoint in between zero and 100
  1980. 1:07:11and you know zero is the cap and 100 is
  1981. 1:07:14the maximum. And that's all that means.
  1982. 1:07:16Now if you notice this whole time as we
  1983. 1:07:18did stuff in this notebook, anytime we
  1984. 1:07:20run a function when it's done running
  1985. 1:07:22we're going to see that check mark and
  1986. 1:07:24while it's running we're going to see
  1987. 1:07:25that sort of swirling thing. Every
  1988. 1:07:27single time we slide the slider, we're
  1989. 1:07:29actually calling funk with a new end. So
  1990. 1:07:31anytime we rerun this, we can actually
  1991. 1:07:33see that sort of thing executing. That's
  1992. 1:07:35just because we're rerunning the
  1993. 1:07:37function. Um and then the third sort of
  1994. 1:07:39thing in here, uh this is going to be
  1995. 1:07:40useful later on. Um is actually the
  1996. 1:07:42step. So if we make the step two, that
  1997. 1:07:45means that you know 50 is going to jump
  1998. 1:07:46up to 52 54 in this case. You know, we
  1999. 1:07:49can only run funk now on like an even
  2000. 1:07:51number. You know, we can sort of get
  2001. 1:07:52into this later. Um but that's really
  2002. 1:07:54all there is to this. Okay, so let's go
  2003. 1:07:56ahead and let's actually do this. This
  2004. 1:07:58is going to be like the best part of the
  2005. 1:08:00video by far. Um, so let me define a new
  2006. 1:08:02function. I don't need to. I could just
  2007. 1:08:04use the um use the existing function,
  2008. 1:08:06but I want to show you guys a couple
  2009. 1:08:07extra things in addition to the
  2010. 1:08:09interactive plotting. Um, so I'm going
  2011. 1:08:11to call this um interactive geometric
  2012. 1:08:14brownie in motion. Um, and this is the
  2013. 1:08:16function that eventually I will hand to
  2014. 1:08:18the IPI widgets. So let's go ahead and
  2015. 1:08:20expose only the parameters that we
  2016. 1:08:23actually want to change. Right? So, we
  2017. 1:08:25probably want to change the moo. We can
  2018. 1:08:27just default that again. And we probably
  2019. 1:08:29want to change the sigma. Um, we can
  2020. 1:08:32also default the sigma. And it's really
  2021. 1:08:34up to us how many things we want actual
  2022. 1:08:36sliders for. Um, in this case, I would
  2023. 1:08:39say the number of years. Sure, let's
  2024. 1:08:41also add that. Um, and then definitely
  2025. 1:08:43the number of scenarios. Um, let's just
  2026. 1:08:45default that to 100. Let's also add the
  2027. 1:08:47number of scenarios. Um, and then maybe
  2028. 1:08:49we also just want to change, you know,
  2029. 1:08:50the initial wealth. So, maybe we also
  2030. 1:08:52want to expose S0, right? Um, I mean, in
  2031. 1:08:55this case, this is literally just
  2032. 1:08:56everything besides the steps per year.
  2033. 1:08:58I'm just going to leave that out for
  2034. 1:08:59now. Again, there's no right answer.
  2035. 1:09:01This is just what we actually want to
  2036. 1:09:02expose in the UI. And this function is
  2037. 1:09:05just going to be a wrapper for the
  2038. 1:09:06actual GBM function. Um, so the last one
  2039. 1:09:09that we left in memory is this one. This
  2040. 1:09:11is the one that we said we're happy
  2041. 1:09:12with. So, let's just go ahead and let's
  2042. 1:09:15just call GBM. This is the fully working
  2043. 1:09:18good GBM function. And let's just go
  2044. 1:09:20ahead and hand it right number of years.
  2045. 1:09:24And let's hand it number of scenarios.
  2046. 1:09:28This might be kind of boring. Let me
  2047. 1:09:30just get this done. Mu equals mu. Sigma
  2048. 1:09:33equals sigma. Um, and s0 equals s0. And
  2049. 1:09:37then, you know, the only thing we're
  2050. 1:09:38kind of choosing to leave out is the
  2051. 1:09:40steps per year. That will just be
  2052. 1:09:41defaulted to 12. Again, it's fully up to
  2053. 1:09:43you what you want to expose to the UI.
  2054. 1:09:44We could just make everything exposed to
  2055. 1:09:46the UI. And before we actually go ahead
  2056. 1:09:48and do interactive plotting, let me just
  2057. 1:09:51show you some more really cool things we
  2058. 1:09:52can do within mapplot lib. So let's just
  2059. 1:09:55go ahead and make this function fully
  2060. 1:09:56self-contained for plotting. Um so
  2061. 1:09:59again, let's just go ahead and save this
  2062. 1:10:00into or technically it won't be rats
  2063. 1:10:02anymore now that we're actually
  2064. 1:10:04returning um the wealth index after the
  2065. 1:10:06cumroad. Technically these are now
  2066. 1:10:08prices not returns, right? Just good to
  2067. 1:10:09make the distinction. Um and let's go
  2068. 1:10:11ahead and let's just plot these. So
  2069. 1:10:14let's just go ahead and do prices.plot
  2070. 1:10:16plot and you know we kept needing to
  2071. 1:10:18make legend false and fig size. Let's
  2072. 1:10:20just go ahead and do these inside the
  2073. 1:10:22function. Um and now when we call this
  2074. 1:10:24function we're just going to get that
  2075. 1:10:25graph directly. And now I'm just going
  2076. 1:10:28to introduce something within mapplot
  2077. 1:10:30lib. So so far in this video we've just
  2078. 1:10:32been doing plot and it works perfectly
  2079. 1:10:34great by default. Um but let's go ahead
  2080. 1:10:36and sort of interface with these graphs
  2081. 1:10:38a little more. It's actually like
  2082. 1:10:40surprising how powerful mapplot lib
  2083. 1:10:43actually is in terms of customization.
  2084. 1:10:46So, anytime we doplot, obviously, it
  2085. 1:10:48essentially just outputs the plot. Um,
  2086. 1:10:50but it's actually going to return
  2087. 1:10:52something. And the thing it's going to
  2088. 1:10:54return, it's kind of confusing. I'm
  2089. 1:10:56going to talk about more. Um, I'm I'm
  2090. 1:10:57going to talk about this a little more
  2091. 1:10:59in a second, but we have two things to
  2092. 1:11:00be aware of in Maplot lib. We have
  2093. 1:11:02something called a figure and something
  2094. 1:11:04called an axis. And the figure you can
  2095. 1:11:06think of as being like the entire plot.
  2096. 1:11:09And then the axis in this case does not
  2097. 1:11:11mean like the x or y axis. This is not
  2098. 1:11:14the same thing as setting axis equal to
  2099. 1:11:16zero or one within pandas or numpy. This
  2100. 1:11:18is an unrelated concept of an axis where
  2101. 1:11:20the axis just means like a whole chart.
  2102. 1:11:23Now, so far there's only been one axis
  2103. 1:11:26per figure, but eventually we're going
  2104. 1:11:28to be actually doing two axises on a
  2105. 1:11:31figure. I know that sounds confusing.
  2106. 1:11:33Um, you'll see what I mean in just a
  2107. 1:11:34second, right? But in the intro, I
  2108. 1:11:36probably would have showed you a stock
  2109. 1:11:37chart that looked like this and a
  2110. 1:11:39histogram. And you know, the stock chart
  2111. 1:11:41is an axis. the histogram is an axis and
  2112. 1:11:43then both being plotted on the same
  2113. 1:11:45figure. The figure sort of includes both
  2114. 1:11:48of them. Um so it turns out that the
  2115. 1:11:50thing that this returns is actually an
  2116. 1:11:52axis like a mapplot lib um axis object.
  2117. 1:11:55We generally just use ax as the variable
  2118. 1:11:58name. Of course you can use whatever you
  2119. 1:11:59want. Um in this we now actually have a
  2120. 1:12:01reference to the axis. And what we can
  2121. 1:12:03do in this case is we can actually add
  2122. 1:12:05things to it. So let's say I just want
  2123. 1:12:08like a dotted line or a dashed line just
  2124. 1:12:10statically a horizontal line going
  2125. 1:12:12through one or going through 100 on the
  2126. 1:12:15y-axis just so I can sort of visualize
  2127. 1:12:18how many you know stocks sort of made
  2128. 1:12:19money and how many lost money over the
  2129. 1:12:21sample. Now of course you know this
  2130. 1:12:23video could easily be 500 hours long.
  2131. 1:12:25You know a static line's cool. How about
  2132. 1:12:27a line where we actually superimpose
  2133. 1:12:29inflation like the CPI? like you know we
  2134. 1:12:32could actually like pull live CPI data
  2135. 1:12:35and then sort of plot that and then we
  2136. 1:12:37can calculate summary stats of like how
  2137. 1:12:39many trials beat inflation and like
  2138. 1:12:41obviously there's so much stuff we can
  2139. 1:12:42do um but we're just going to be keeping
  2140. 1:12:44the scope um limited in this video um so
  2141. 1:12:46let's go ahead and we're going to
  2142. 1:12:48reference um this ax right this axis
  2143. 1:12:50object that we have and again there's no
  2144. 1:12:52way you would know to do this unless you
  2145. 1:12:54looked up documentation but it turns out
  2146. 1:12:56that there's a function called ax h is
  2147. 1:12:59for horizontal and then line just like
  2148. 1:13:02axis horizontal line. And this is just a
  2149. 1:13:04function that's going to add a
  2150. 1:13:05horizontal line onto this axis, which
  2151. 1:13:08just means the entire chart. Um, and the
  2152. 1:13:10only thing we're going to need to hand
  2153. 1:13:11this is a y, which is just where we want
  2154. 1:13:13to plot it. Um, and in this case, we
  2155. 1:13:15just want it to be centered at s0. So,
  2156. 1:13:18if you go ahead and run this function
  2157. 1:13:19and run this, boom, we can now see that
  2158. 1:13:21we have a static line going through um,
  2159. 1:13:24s0, which is good. Um, but it doesn't
  2160. 1:13:26really look too good. Um, so again, you
  2161. 1:13:29know, you can read the documentation and
  2162. 1:13:30you can learn about all the different
  2163. 1:13:32sort of styling options we have
  2164. 1:13:34available to us. Um, one of them is
  2165. 1:13:35called ls. Pretty sure that stands for
  2166. 1:13:37like a line style. And we can go ahead
  2167. 1:13:39and we can make this equal to any number
  2168. 1:13:41of things. Um, I'm pretty sure if we do
  2169. 1:13:42a colon, it will actually now be a
  2170. 1:13:44dotted line. Sure enough, that's dotted.
  2171. 1:13:47Um, what I like to do is I like to do um
  2172. 1:13:49a dashed line. I just think it looks
  2173. 1:13:51better. Obviously, this is just in my
  2174. 1:13:52opinion. Um, and now let's actually make
  2175. 1:13:54it visible, um, by doing the color. Um,
  2176. 1:13:58and let's just set the color equal to
  2177. 1:14:00black. And this should probably look
  2178. 1:14:01more visible now. And there we go. Right
  2179. 1:14:03now, this actually looks good. Um, you
  2180. 1:14:05know, I'm just sort of showing you the
  2181. 1:14:07actual power of mapplot lib. Um, in my
  2182. 1:14:09opinion, this is pretty good. Now, we
  2183. 1:14:10can go ahead and move on to making this
  2184. 1:14:12interactive. So, this should be pretty
  2185. 1:14:14easy. So, we're just going to need to do
  2186. 1:14:16widgets. In this case, I'm going to use
  2187. 1:14:19um interactive instead of interact.
  2188. 1:14:21Again, you're free to read the
  2189. 1:14:22documentation later. Um, and all we need
  2190. 1:14:25to really do is just hand it that
  2191. 1:14:27interactive GBM function. And there are
  2192. 1:14:29going to be some issues with this. Um,
  2193. 1:14:31though, let's actually work through them
  2194. 1:14:32in real time. So, it looks good. It's
  2195. 1:14:35that same chart except all the things we
  2196. 1:14:38exposed as a Python parameter in the Py
  2197. 1:14:40um in the Python function are now
  2198. 1:14:41available to us. But there's going to be
  2199. 1:14:43some issues here, right? Because if we
  2200. 1:14:45take, you know, s0, this can actually
  2201. 1:14:48become negative, which, you know, it
  2202. 1:14:50technically doesn't break the Python
  2203. 1:14:52code, but we probably don't want a
  2204. 1:14:54negative starting. Um, though, however,
  2205. 1:14:57if number of scenarios goes negative,
  2206. 1:14:59obviously, we're going to get an issue.
  2207. 1:15:00Negative dimensions are not allowed,
  2208. 1:15:02which is fully expected. Um, so let's
  2209. 1:15:03just go ahead and sort of clean this up
  2210. 1:15:05for ourselves. Um, so this might be kind
  2211. 1:15:07of boring, but I'm still going to do
  2212. 1:15:08this in real time just for just for the
  2213. 1:15:10people that are following along that
  2214. 1:15:11might want to see this. Um, I'm just
  2215. 1:15:12going to, you know, paste this in here
  2216. 1:15:14just to just so I don't waste time
  2217. 1:15:15typing. Um, and then number of years.
  2218. 1:15:17All of these are going to be a tpple.
  2219. 1:15:18Remember, it's sort of like start, stop,
  2220. 1:15:20step. Um, so let's say we want the
  2221. 1:15:22number of years to at minimum be a one.
  2222. 1:15:24And let's just cap it at 30. Um, that
  2223. 1:15:26will do number of scenarios, you know,
  2224. 1:15:28let's do between one and maybe let's say
  2225. 1:15:31like a,000. Uh, moo and sigma, let's do
  2226. 1:15:34these as well. Um, let's say the MOO, we
  2227. 1:15:36want to go from negative, you know, 10%,
  2228. 1:15:39which is just 0.1 to positive 0.1. Uh,
  2229. 1:15:43sigma, of course, we don't want that
  2230. 1:15:44going negative. So, let's say the sigma
  2231. 1:15:46can go from zero to maybe like 0.3, 30%.
  2232. 1:15:50Um, and then for this, let's just go
  2233. 1:15:52ahead and say maybe $100 to $1,000. Now,
  2234. 1:15:56let's run this. And it looks good. Um,
  2235. 1:15:59except there's going to be a slight
  2236. 1:16:01issue here. Let's take a look at like MO
  2237. 1:16:03and Sigma. If we move down the MOO, we
  2238. 1:16:05sort of messed up the step. Um, right,
  2239. 1:16:07we didn't specify step on any of these.
  2240. 1:16:09Essentially, how it works is whatever
  2241. 1:16:10sort of number it sort of defaults to,
  2242. 1:16:12that's just going to be the step. So,
  2243. 1:16:14because the MOO is like 0.1 is just
  2244. 1:16:16going to use 0.1 as the granularity. We
  2245. 1:16:19now sort of lost ability. Um, so let's
  2246. 1:16:21just go ahead and say, you know, 0.01
  2247. 1:16:24for the MOO. Um, same thing with the
  2248. 1:16:26sigma. We're going to make that 0.01.
  2249. 1:16:28Um, and with all that being said, now we
  2250. 1:16:31should be good. So let's go ahead and
  2251. 1:16:33actually sort of play around with this,
  2252. 1:16:35right? So let's just take a look. So
  2253. 1:16:36number of years is equal to 10. Now,
  2254. 1:16:38right now, you know, the winning sort of
  2255. 1:16:40stock closes out at, you know, maybe
  2256. 1:16:42like 500 bucks or something. If we
  2257. 1:16:44increase the sample, we should start to
  2258. 1:16:46see terminal wealths that are obviously
  2259. 1:16:48higher. Right? Now the axis goes up to
  2260. 1:16:50800. Right? If we actually make this 30,
  2261. 1:16:53now we can end a 30-year sample with
  2262. 1:16:54like, you know, $6,000, right? Instead
  2263. 1:16:56of $500, right? We're just sort of
  2264. 1:16:58getting some intuition um as to how this
  2265. 1:17:00actually works. Sure enough, it looks
  2266. 1:17:02like it's working. Um, now the MOO,
  2267. 1:17:04right, the average stock obviously
  2268. 1:17:06closes above this dotted black line
  2269. 1:17:08since we have a positive expected
  2270. 1:17:09return. If we make this flat, it's going
  2271. 1:17:11to look somewhat more flat. And if we
  2272. 1:17:13make this negative, right, if we make
  2273. 1:17:15this, you know, negative 10% a year,
  2274. 1:17:17most of the time we're going to close
  2275. 1:17:18below the black line. If we now increase
  2276. 1:17:20the number of years, right, they're now
  2277. 1:17:22all going to start to close below the
  2278. 1:17:24black line. So, this is, you know, this
  2279. 1:17:26is how we can actually sort of adjust
  2280. 1:17:27these parameters to our liking. Um,
  2281. 1:17:29Sigma is also really cool. If we lower
  2282. 1:17:31sigma, it's actually going to, you know,
  2283. 1:17:33converge on more or less a straight
  2284. 1:17:35line, right? They're all going to almost
  2285. 1:17:37look like one line. If you go and make
  2286. 1:17:39this, you know, 0.01,
  2287. 1:17:41right? You can now see we're sort of
  2288. 1:17:43like converging on the average path. Um,
  2289. 1:17:46and you can see it's not like straight.
  2290. 1:17:47It's actually curved because of the way
  2291. 1:17:49like compounding works. Um, same thing
  2292. 1:17:51for when the moo's positive, right? It
  2293. 1:17:53sort of curves up exponentially. Um, so
  2294. 1:17:55yeah, so we're basically done the
  2295. 1:17:57function. Um, and now what I'm going to
  2296. 1:17:58do, probably last thing, is I'm going to
  2297. 1:18:01go ahead and we're going to add a
  2298. 1:18:02histogram so we can visualize um a
  2299. 1:18:05histogram of terminal wealth. So, this
  2300. 1:18:08is going to be a lot of fun. This is
  2301. 1:18:11probably going to be like the highlight
  2302. 1:18:12of the video. Um, and again, as I said,
  2303. 1:18:14this video could literally be like a
  2304. 1:18:15thousand hours long. Um, I need to pick
  2305. 1:18:18some spot to end off. I think I'll just
  2306. 1:18:19do the histogram and then I'll probably
  2307. 1:18:21just call it a day. So, we're going to
  2308. 1:18:23be just sticking with this function, but
  2309. 1:18:24we're going to be heavily modifying it.
  2310. 1:18:26And this is like just great practice for
  2311. 1:18:29you know working with like mapplot lib
  2312. 1:18:30and stuff. Um so now we're going to be
  2313. 1:18:33essentially calling like a subplot
  2314. 1:18:35function. I'm going to explain all of
  2315. 1:18:36this. So you probably would have seen in
  2316. 1:18:38the intro of the video how this works.
  2317. 1:18:40We're going to be plotting two different
  2318. 1:18:41things. One of the things is we're still
  2319. 1:18:43going to be plotting the prices. We can
  2320. 1:18:45just leave that variable intact. That's
  2321. 1:18:46completely okay. And then the second
  2322. 1:18:48thing is we want a histogram of terminal
  2323. 1:18:49wealth as I said. So let's go ahead
  2324. 1:18:52right before we worry about plotting. We
  2325. 1:18:54obviously just need the data to plot in
  2326. 1:18:55the first place. Um, so we have prices
  2327. 1:18:58and now we have terminal wealth. And we
  2328. 1:19:00actually did this earlier in the video
  2329. 1:19:02during that experiment we did. If you
  2330. 1:19:04think about how we actually get terminal
  2331. 1:19:05wealth, um, it's literally just going to
  2332. 1:19:07be prices integer location of -1. Um,
  2333. 1:19:12we're literally, you know, just grabbing
  2334. 1:19:13the very last price on the very last
  2335. 1:19:15time step and that is the terminal
  2336. 1:19:17wealth since the whole thing's a wealth
  2337. 1:19:19index. Um, should be pretty
  2338. 1:19:20straightforward so far. So prices and
  2339. 1:19:22terminal wealth is what we're actually
  2340. 1:19:24going to be plotting. So so far in this
  2341. 1:19:27video, everything we did in terms of
  2342. 1:19:29plotting, we just used the dotplot
  2343. 1:19:31method for on a pandas object. And you
  2344. 1:19:34know, even though this returned another
  2345. 1:19:35mapplot lib object that we used another
  2346. 1:19:36method on, we didn't actually need to
  2347. 1:19:38import mapplotib into our notebook yet,
  2348. 1:19:41right? It's actually kind of funny. We
  2349. 1:19:42used it so much without even importing
  2350. 1:19:44it. Um, now we're going to need, you
  2351. 1:19:46know, more sort of fine grain control
  2352. 1:19:48within mapplot lib to do all this
  2353. 1:19:50histogram stuff, which I think is so
  2354. 1:19:51interesting. Um, so let's go ahead and
  2355. 1:19:53we're actually going to need to import
  2356. 1:19:54it since we are going to need to use the
  2357. 1:19:56subplots method. So we're just going to
  2358. 1:19:58say import mapplot lib um pipplot. This
  2359. 1:20:01is just what we've been using. Um, and
  2360. 1:20:03then we generally just say as plt for
  2361. 1:20:06plot. That's just what we do. Make sure
  2362. 1:20:08you run it. Um, and we should be good
  2363. 1:20:10now to get started. Um so what we're
  2364. 1:20:12going to be doing right is we're going
  2365. 1:20:13to be plotting two different things um
  2366. 1:20:15on the same figure right two plots on
  2367. 1:20:16the same figure and we're going to need
  2368. 1:20:18a special function now that lives right
  2369. 1:20:20within the map lib library and it's
  2370. 1:20:21called subplots. So let's just go ahead
  2371. 1:20:23and call subplots and this is how we can
  2372. 1:20:26sort of set up our figure. Um again
  2373. 1:20:29there's no way you would know to do this
  2374. 1:20:30if you didn't read documentation. I'm
  2375. 1:20:32just sort of going to like walk you
  2376. 1:20:33through what we're doing and then as
  2377. 1:20:35soon as I plot it will look a lot more
  2378. 1:20:37clear. Um, so one of the parameters we
  2379. 1:20:39need for this this figure of multiple
  2380. 1:20:42plots, you can essentially think of this
  2381. 1:20:44as almost as if you're building like a
  2382. 1:20:45dashboard, right? We could have 10
  2383. 1:20:47different plots on this figure. We can
  2384. 1:20:49really have whatever we want. Um, and
  2385. 1:20:51they're organized essentially in a grid
  2386. 1:20:53layout. So we actually need to specify
  2387. 1:20:54the number of rows and the number of
  2388. 1:20:56columns where, you know, every single
  2389. 1:20:59thing within these rows and columns are
  2390. 1:21:01entire plots like a whole line graph, a
  2391. 1:21:03whole histogram um, are going to be
  2392. 1:21:05organized into this grid, right? It's
  2393. 1:21:06super useful. Um, in this case, there's
  2394. 1:21:08only going to be two different things.
  2395. 1:21:10So, we're going to have one row and
  2396. 1:21:12we're going to have two columns. End
  2397. 1:21:13call is going to be equal to two. Um,
  2398. 1:21:16and what this entire dots subplots
  2399. 1:21:18function returns, it's a little
  2400. 1:21:20confusing, but it's going to return two
  2401. 1:21:22different things. So, we can go ahead
  2402. 1:21:23and unpack this um directly. The first
  2403. 1:21:26thing is going to be a figure. And we're
  2404. 1:21:28not actually even going to use the
  2405. 1:21:30figure. It's just going to be returned,
  2406. 1:21:32right? It's going to return two things.
  2407. 1:21:33So, we're unpacking. Um, and then the
  2408. 1:21:35second thing is actually going to be a
  2409. 1:21:36tpple. And then each element in the
  2410. 1:21:39tpple is going to be the axis for the
  2411. 1:21:42specific plot. In this case, there's
  2412. 1:21:44going to be two, one for prices and one
  2413. 1:21:46for terminal wealth. So, we can actually
  2414. 1:21:48go ahead and call this, you know, wealth
  2415. 1:21:52axis. And then let's just go ahead and
  2416. 1:21:54call the other one, um, let's just say
  2417. 1:21:56hist axis for histogram. Right? So, we
  2418. 1:21:59have two different axises coming out of
  2419. 1:22:00here. And we're going to see how this
  2420. 1:22:01looks in just a second. Now, just so we
  2421. 1:22:03can gain a little bit of intuition um as
  2422. 1:22:05to what we're working with as we write
  2423. 1:22:07this code, um we don't need to worry
  2424. 1:22:08about this for now. Let's just go ahead
  2425. 1:22:10and simply just print out the entire
  2426. 1:22:12figure. Um in this, there's not going to
  2427. 1:22:14be anything populated right now, but
  2428. 1:22:16this is sort of going to let us know
  2429. 1:22:17what's going on. When we said one row by
  2430. 1:22:19two columns, we now sort of have these
  2431. 1:22:21two different figures that are on the
  2432. 1:22:23same row. They're side by side. um just
  2433. 1:22:25for the intuition if we instead said
  2434. 1:22:28number of rows is two and number of
  2435. 1:22:29columns is one now now these would be
  2436. 1:22:32stacked on top of each other right so
  2437. 1:22:34this is sort of like you know we're just
  2438. 1:22:35building plots in a grid um and then
  2439. 1:22:37we're just going to you know keep on
  2440. 1:22:38doing this until we have a beautiful
  2441. 1:22:40looking graph so this is what we have
  2442. 1:22:42right now so this is going to become
  2443. 1:22:43more clear once we actually plot the
  2444. 1:22:45data um you would have seen this in the
  2445. 1:22:47intro of the video though um by default
  2446. 1:22:49right we're not sharing the x or y axis
  2447. 1:22:52we can see you know we have an x axis
  2448. 1:22:53and a y- axis for both of these figures.
  2449. 1:22:56Um, for the histogram, we're going to
  2450. 1:22:57see when we add the data, but we
  2451. 1:22:59actually want to share the yaxis, which
  2452. 1:23:01means we only want to use one y-axis.
  2453. 1:23:03Um, it turns out there's actually
  2454. 1:23:04something called share y, and we can
  2455. 1:23:07just go ahead and set that equal to
  2456. 1:23:08true. And we're going to go ahead and,
  2457. 1:23:09you know, show what changed after I add
  2458. 1:23:11in each argument. Um, now we can see,
  2459. 1:23:13right, it's the same thing, but we now
  2460. 1:23:15only have a single y-axis, um, since
  2461. 1:23:17we're sharing it. And of course, we
  2462. 1:23:19probably just want to make these bigger.
  2463. 1:23:20Um, so we can go ahead and do fix size
  2464. 1:23:23on this directly. Um, we're going to
  2465. 1:23:24make this a little bigger than the uh
  2466. 1:23:26126 I did before. Let's make this maybe
  2467. 1:23:29like 248 or 2410 should be good. Let's
  2468. 1:23:33see. Uh, yeah, that should be fine. Just
  2469. 1:23:35cuz, you know, this is the figure um for
  2470. 1:23:37both plots combined. So, it definitely
  2471. 1:23:39will need to be bigger. Um, now I just
  2472. 1:23:41realized we should probably actually put
  2473. 1:23:42the data on the plot before we continue
  2474. 1:23:45cleaning it up just so you can see what
  2475. 1:23:47I'm actually talking about. So, let's go
  2476. 1:23:49ahead and actually populate these empty
  2477. 1:23:51graphs with the data. Um, so let's go
  2478. 1:23:54ahead and do prices first. So, we're
  2479. 1:23:56still just going to do prices.plot, but
  2480. 1:23:58we need this to not, you know, just be
  2481. 1:24:00floating out of nowhere. We need to
  2482. 1:24:02specify that we want this specifically
  2483. 1:24:04to be right here. Um, so how we do this
  2484. 1:24:06is there's actually a parameter called
  2485. 1:24:08ax for axis and we're going to specify,
  2486. 1:24:11right, that this prices variable, which
  2487. 1:24:13is just a data frame. It's what our own
  2488. 1:24:15function outputs as a data frame. We
  2489. 1:24:17want to plot this and the location is
  2490. 1:24:20going to be on this axis called wealth
  2491. 1:24:22axis which is this sort of empty plot
  2492. 1:24:24here. Um now if I just go ahead and run
  2493. 1:24:26the cell this should work. Let's take a
  2494. 1:24:28look. Sure enough it does work. Of
  2495. 1:24:30course we're have a have a bit of
  2496. 1:24:32cleanup to do but right this is just how
  2497. 1:24:34we plot the data. Um now we already know
  2498. 1:24:36that we probably specifically within
  2499. 1:24:38this want to do legend equals false.
  2500. 1:24:41Let's go ahead and do that as well. Um
  2501. 1:24:43and we should be good. Um, and then last
  2502. 1:24:46thing is this rainbow stuff might be a
  2503. 1:24:48little much once we have both plots. So,
  2504. 1:24:50we can go ahead and just use the color
  2505. 1:24:52parameter color equals. And we can set
  2506. 1:24:54this to whatever we want. Um, in this
  2507. 1:24:56case, I might just do like blue. Um,
  2508. 1:24:58though blue, I believe, yeah, blue is
  2509. 1:25:00like incredibly sort of dark and
  2510. 1:25:02saturated. Most colors are. Um, let me
  2511. 1:25:04just leave it on like sky blue. And we
  2512. 1:25:06should be good to call it a day. Doesn't
  2513. 1:25:08look that bad. Um there's also another
  2514. 1:25:09parameter called like alpha which um if
  2515. 1:25:12you want it to sort of be a little like
  2516. 1:25:14transparent we can make alpha 0.5 and
  2517. 1:25:17then it will be more faded. You know we
  2518. 1:25:18don't want that or anything. Um but you
  2519. 1:25:20know I would advise you read the
  2520. 1:25:21documentation. You know there's like a
  2521. 1:25:23lot of different ways we can customize
  2522. 1:25:24this. So now we're at least plotting um
  2523. 1:25:26our stocks. Um and now what I'm going to
  2524. 1:25:28do is I'm just going to add that
  2525. 1:25:29horizontal line back that I got rid of.
  2526. 1:25:32Um so we can do that in a very similar
  2527. 1:25:33way. we can just go ahead and call you
  2528. 1:25:35know the wealth axis directly and then
  2529. 1:25:37we can do the ax h for horizontal line
  2530. 1:25:41um where y is equal to s0 and then ls
  2531. 1:25:45for line style um I believe we did a
  2532. 1:25:48double dash for dash line and then let's
  2533. 1:25:50just make the color for this completely
  2534. 1:25:52black um because we're calling you know
  2535. 1:25:54this method on wealth axis it will be
  2536. 1:25:57within this plot um so we should be good
  2537. 1:25:59um and now let's go ahead and focus on
  2538. 1:26:01actually plotting the terminal wealth
  2539. 1:26:02histogram so let's go ahead and do that
  2540. 1:26:04here. Let's go ahead and do terminal
  2541. 1:26:06wealth. Um, and we're going to do plot.
  2542. 1:26:09But in this type, right, by default, we
  2543. 1:26:11just have this like nice looking line
  2544. 1:26:12graph for the stocks, right? Um, but
  2545. 1:26:14what we actually want to do is we want
  2546. 1:26:15to do histogram. Lucky for us, of
  2547. 1:26:17course, mapplot lib just has a histogram
  2548. 1:26:19method directly. We're just telling it
  2549. 1:26:21what type of plot we actually want. Um,
  2550. 1:26:23and then same thing, ax. In this case,
  2551. 1:26:26we don't want to plot it on this plot.
  2552. 1:26:27We want to plot it on this plot, which
  2553. 1:26:29we're calling hist ax. Um, now we're
  2554. 1:26:32gonna change this up a bit, but let's go
  2555. 1:26:33ahead and see what we have.
  2556. 1:26:35Now, it doesn't look the best, but it
  2557. 1:26:37still is working, right? This is a
  2558. 1:26:39histogram, but you know, we're sort of
  2559. 1:26:41sharing the axis. The whole thing's
  2560. 1:26:43messed up. Let's go ahead and clean this
  2561. 1:26:44up. Now, um, so one of the things we
  2562. 1:26:46want to do is we don't want to plot this
  2563. 1:26:48horizontally. We sort of want it to look
  2564. 1:26:50nice where the stock comes up here and
  2565. 1:26:52then the bar just comes out of the side,
  2566. 1:26:54not out of the bottom. Um, that's a
  2567. 1:26:56super easy change. We just have a
  2568. 1:26:58parameter called orientation and we can
  2569. 1:27:02just set that equal to horizontal.
  2570. 1:27:06Should work. Let's give this a try.
  2571. 1:27:08Perfect. Right now, this is starting to
  2572. 1:27:10look better. So, we still have some
  2573. 1:27:12further adjustments to make. Um, one of
  2574. 1:27:14the very annoying things is there's this
  2575. 1:27:15massive gap in between the two plots.
  2576. 1:27:17Um, it turns out that we can actually
  2577. 1:27:19get rid of that quite easily. For that,
  2578. 1:27:21we need, and again, you would need to
  2579. 1:27:22read the documentation. You wouldn't
  2580. 1:27:24know how to do this. Um, but we can do
  2581. 1:27:26pls
  2582. 1:27:27subplots
  2583. 1:27:29adjust. Um, and when we do this, we're
  2584. 1:27:31not, you know, using one or the other
  2585. 1:27:33axis. We're sort of using like the
  2586. 1:27:35overarching sort of general um general
  2587. 1:27:38like entire figure. Um, and we're going
  2588. 1:27:39to go ahead and we're going to use an
  2589. 1:27:41argument called wp space, which just
  2590. 1:27:43means like whites space. And we're just
  2591. 1:27:44going to set that equal to zero. And
  2592. 1:27:46we're going to go ahead and run this and
  2593. 1:27:48run this. And there we go. Right now
  2594. 1:27:50they're like perfectly touching each
  2595. 1:27:52other. Um, there's no space in between
  2596. 1:27:53them. So, you know, let's keep on going
  2597. 1:27:55here. Um, one of the things that right
  2598. 1:27:57now is annoying me is, you know, this is
  2599. 1:27:59taking up like 50% of the width and
  2600. 1:28:01that's taking up 50% of the width. Um,
  2601. 1:28:03you know, I'd way rather have the line
  2602. 1:28:05sort of be maybe there like this sort of
  2603. 1:28:08center line. I want more space allocated
  2604. 1:28:10to the actual stocks and less to the
  2605. 1:28:11histogram. Um, we can go ahead and do
  2606. 1:28:13this. There's a special parameter. I
  2607. 1:28:15actually had to open the documentation
  2608. 1:28:16to look at it. Um, but let's go ahead
  2609. 1:28:18here. Um, and it's going to be called
  2610. 1:28:20gridspec. There we go. gridspec keyword
  2611. 1:28:24and the specific keyword, right? This is
  2612. 1:28:25going to be dictionary. The specific
  2613. 1:28:26keyword that we're going to be using um
  2614. 1:28:28is actually called width ratios. And
  2615. 1:28:31this width ratios, oops, that was weird.
  2616. 1:28:34Um this width ratios is going to be a
  2617. 1:28:37Python list. Um and the list is
  2618. 1:28:39essentially just going to be the ratio
  2619. 1:28:41between one and the other. So if we do
  2620. 1:28:43like 32, that means that the line chart
  2621. 1:28:45is going to be like three units relative
  2622. 1:28:48to two units. Um if you run Oops. If you
  2623. 1:28:50run this, we should see this line sort
  2624. 1:28:52of jump over that way. And there we go.
  2625. 1:28:55Sure enough, that works. Um, in this
  2626. 1:28:56case, I'm actually just going to make it
  2627. 1:28:58double the space. So, if I say 21, that
  2628. 1:29:00means that the stock area is going to be
  2629. 1:29:02double the area of the histogram. Um,
  2630. 1:29:04and there we go. Sure enough, that's
  2631. 1:29:06what we have. So, we could call this a
  2632. 1:29:08day. This isn't that bad, but the
  2633. 1:29:10histogram is not that granular. It looks
  2634. 1:29:13pretty bad. we probably want more bins
  2635. 1:29:15so we have more granular uh more
  2636. 1:29:18granular like results sort of visually
  2637. 1:29:20in the histogram. Um so we can go ahead
  2638. 1:29:22and there's literally just going to be a
  2639. 1:29:24parameter called bins and we can make
  2640. 1:29:27this maybe like 50 should be good. Let's
  2641. 1:29:29go ahead and see. There we go. Right,
  2642. 1:29:31that's what I'm talking about. We don't
  2643. 1:29:32want those bars extremely thick. Um
  2644. 1:29:34let's do, you know, 50 bins. Um this
  2645. 1:29:36isn't bad. Um, now another thing that's
  2646. 1:29:38annoying me is this doesn't really look
  2647. 1:29:41that much like a histogram to me just
  2648. 1:29:43cuz all these bars are kind of like
  2649. 1:29:44merging together. Um, in my opinion,
  2650. 1:29:46this doesn't look the best. Of course,
  2651. 1:29:48this part is like completely subjective,
  2652. 1:29:50right? We're just styling the graph.
  2653. 1:29:51It's already looking pretty pretty good.
  2654. 1:29:53Um, but we can go here and there's
  2655. 1:29:55something that's going to be called EC,
  2656. 1:29:57which means edge color. And if we set
  2657. 1:29:59the edge color equal to the background
  2658. 1:30:01color, which is white, it's now going to
  2659. 1:30:04draw a little sort of white edge around
  2660. 1:30:06each bar. And it's basically going to
  2661. 1:30:08look exactly like that. Um, and last
  2662. 1:30:11thing, let me just make the color also
  2663. 1:30:14equal to sky blue. Um, of course, we
  2664. 1:30:16could make it a different color. Let me
  2665. 1:30:18just see. That should be pretty good.
  2666. 1:30:20How does this look? Not bad, right?
  2667. 1:30:23That's actually pretty good so far. Um,
  2668. 1:30:25now obviously, you know, we could keep
  2669. 1:30:26on going and we could keep on styling
  2670. 1:30:28this. You know, for the thumbnail of
  2671. 1:30:30this video, I'm probably going to do
  2672. 1:30:31some variant where I make the background
  2673. 1:30:33dark and then make all of these, you
  2674. 1:30:35know, some color set that's like rainbow
  2675. 1:30:36or something, right? You know, there's
  2676. 1:30:38really no limit to what we can do. Um,
  2677. 1:30:39but this is this is pretty good, right?
  2678. 1:30:41I I think this is good for this video.
  2679. 1:30:43And of course, you know, now we can go
  2680. 1:30:45ahead and and mess around with this,
  2681. 1:30:46right? Again, if we lower the sigma,
  2682. 1:30:48right? There's not much not much uh
  2683. 1:30:50standard deviation. We can increase
  2684. 1:30:52this, right? We can increase number of
  2685. 1:30:53scenarios now to a thousand, right? And
  2686. 1:30:55like look how cool this is, right? Like
  2687. 1:30:57we can now see how that Z of T variable,
  2688. 1:31:00right? These are random standard normal
  2689. 1:31:02Gaussian variables. We actually more or
  2690. 1:31:04less have like a normally distributed,
  2691. 1:31:06right, with a mean and a sigma, but this
  2692. 1:31:08normally distributed sort of terminal
  2693. 1:31:10wealth. Um, and we're done, right?
  2694. 1:31:12That's really that's really all there is
  2695. 1:31:14for the video. Now, obviously, you know,
  2696. 1:31:16we could just keep on adding things and
  2697. 1:31:18keep on changing things, right? Um, in
  2698. 1:31:20that first Quant Finance video, I
  2699. 1:31:21brought up like draw downs. We could
  2700. 1:31:23actually go ahead and calculate the max
  2701. 1:31:25draw down for each of these simulations
  2702. 1:31:28and, you know, even plot the draw downs
  2703. 1:31:29or say, you know, which one had the most
  2704. 1:31:31devastating draw down. Um, there's also
  2705. 1:31:33Maplot annotations which I brought up
  2706. 1:31:35last time as well. We can even put
  2707. 1:31:38numbers like directly on the graph. So
  2708. 1:31:40like draw down, summary stats, right?
  2709. 1:31:42Like the mean and median, return, and
  2710. 1:31:44all like all the stuff we could
  2711. 1:31:45literally add in text right on the
  2712. 1:31:47graph. it would change this number and
  2713. 1:31:49it would change in real time. You know,
  2714. 1:31:51like originally I wasn't even going to
  2715. 1:31:52add the histogram into the video. The
  2716. 1:31:54histogram is kind of too cool to not
  2717. 1:31:56add, but this is how we can actually use
  2718. 1:31:58Python and use a couple of these
  2719. 1:32:00libraries to actually start doing like
  2720. 1:32:02actual analysis, right? So, yeah. So,
  2721. 1:32:05that's really all there is for this
  2722. 1:32:07video. You know, this easily could have
  2723. 1:32:09been a thousand hours long, right? I
  2724. 1:32:10need to stop somewhere. Um, now, you
  2725. 1:32:12know, if there's interest, obviously
  2726. 1:32:14I'll keep the series going, right? Let
  2727. 1:32:15me know what you think. Um, for next
  2728. 1:32:17video I was thinking I might actually do
  2729. 1:32:19another stochcastic modeling video, but
  2730. 1:32:21instead of, you know, stock market
  2731. 1:32:23stochastic modeling, I might actually
  2732. 1:32:25use the CIR model and then do stochastic
  2733. 1:32:27modeling on bonds on fixed income. Uh,
  2734. 1:32:30in fact, there's a lot of stuff we can
  2735. 1:32:31do with fixed income. We can actually
  2736. 1:32:33model, you know, zero coupon bonds in
  2737. 1:32:35coupon bearing bonds and we can actually
  2738. 1:32:37also get like the instantaneous rate as
  2739. 1:32:39well. And, you know, we can use like
  2740. 1:32:41oilers's number and, you know, we can
  2741. 1:32:42actually like derive all of this stuff
  2742. 1:32:44directly in Python. It's actually like
  2743. 1:32:46fascinating. Um, and then in the intro
  2744. 1:32:48of this video, um, I said that I was
  2745. 1:32:50going to say a little something about
  2746. 1:32:51factor aggressions. Um, let me go ahead
  2747. 1:32:53and just talk about that now just
  2748. 1:32:54because there was some interest on that.
  2749. 1:32:56So, yeah. So, you know, just to give you
  2750. 1:32:58maybe like a little maybe preview or
  2751. 1:33:00whatever of what's to come. Um, I was
  2752. 1:33:01saying in my asset pricing video at the
  2753. 1:33:03very end of that super long video from a
  2754. 1:33:05month or two ago, I was showing this
  2755. 1:33:07website. It's not being hosted or
  2756. 1:33:08anything. I'll probably fix it up and
  2757. 1:33:10throw it back on production at some
  2758. 1:33:11point in time. Um, I don't use it
  2759. 1:33:12anymore. I built it many many many years
  2760. 1:33:14ago when I was first getting started
  2761. 1:33:16with quant. Um it was more of like a
  2762. 1:33:18portfolio project. It basically just
  2763. 1:33:20gave us like this UI that's hosted in
  2764. 1:33:22the web and you can like save portfolios
  2765. 1:33:24to your account and stuff, right? And
  2766. 1:33:25you can plot all this stuff and you know
  2767. 1:33:27this page just for back testing
  2768. 1:33:28securities. You can already build this
  2769. 1:33:30yourself if you watch that 9minute Quant
  2770. 1:33:32Finance 101 video I made. It's actually
  2771. 1:33:34really simple. Um in this case I'm
  2772. 1:33:36actually using like plotty.js. It's it's
  2773. 1:33:39actually something Let me actually pull
  2774. 1:33:41that up. It's actually pretty
  2775. 1:33:42interesting. So in the video that you
  2776. 1:33:44just watched all of the plotting we did
  2777. 1:33:45was done in mapplot lib which is just
  2778. 1:33:48the default plotting backend that pandas
  2779. 1:33:50uses. It turns out we can actually go
  2780. 1:33:52into pandas right dot
  2781. 1:33:53option.plotting.backend
  2782. 1:33:55and this is what I did for that website.
  2783. 1:33:56And we can actually change the default
  2784. 1:33:58plotting engine and we can actually use
  2785. 1:34:00any plotting library we want in Python
  2786. 1:34:03not just mapplotib. Um and I use plotty
  2787. 1:34:06for a couple different reasons in this
  2788. 1:34:07project and this is like a deprecated
  2789. 1:34:09project. This isn't a big deal or
  2790. 1:34:10anything. Um it's just cool to note that
  2791. 1:34:12this actually has like a plotty.js um
  2792. 1:34:15CDN. So this means that when my backend
  2793. 1:34:18does all this stuff, this plot, this
  2794. 1:34:20whole interactive plot, all of the
  2795. 1:34:21JavaScript that's being used here is
  2796. 1:34:23actually served from Plotty to the users
  2797. 1:34:26browser instead of from I was hosting
  2798. 1:34:28this originally on a Raspberry Pi. So
  2799. 1:34:30it's actually really slow and this
  2800. 1:34:31actually helped to speed it up. It's
  2801. 1:34:33actually, you know, it's pretty cool as
  2802. 1:34:34all you know. Anyways, um, so just cuz
  2803. 1:34:36some people were asking, just to give
  2804. 1:34:38you a little sort of like sneak peek
  2805. 1:34:39preview into how all this like Python
  2806. 1:34:42stuff and quant stuff can actually be
  2807. 1:34:43useful in the real world, just because
  2808. 1:34:45stochastic modeling isn't the most sort
  2809. 1:34:47of useful. Um, you know, obviously we
  2810. 1:34:49have like rolling returns and stuff. We
  2811. 1:34:51can really do whatever we want. I might
  2812. 1:34:52even put this website back on prod and
  2813. 1:34:54then update the website in real time
  2814. 1:34:57with each Quant Finance video I make.
  2815. 1:34:59So, you know, I might even have a tab
  2816. 1:35:01called like stochastic modeling and
  2817. 1:35:02stuff, right? Um, either way, let's say
  2818. 1:35:04we want to do a factor regression, and
  2819. 1:35:06this is just like a random thought. It
  2820. 1:35:07doesn't really matter, but let's say we
  2821. 1:35:08want to do a factor regression on two
  2822. 1:35:10value ETFs, and we want to determine
  2823. 1:35:13which value ETF is more exposed to the
  2824. 1:35:15value premium cuz this isn't really
  2825. 1:35:18something, you know, maybe it will say
  2826. 1:35:19on like the fund website, but they're
  2827. 1:35:21not really going to make claims like
  2828. 1:35:22this. You sort of need to like gather
  2829. 1:35:24this data yourself. So, let's say
  2830. 1:35:26there's like, you know, one value ETF is
  2831. 1:35:28called VLU, and I don't own these or
  2832. 1:35:29anything, right? This is barely even an
  2833. 1:35:31analysis. This is just just a little
  2834. 1:35:33sneak peek, I guess. Um, and there's
  2835. 1:35:35another value ETF called IUSV, and
  2836. 1:35:37they're both US value ETFs. And let's
  2837. 1:35:40say we want to know which one is a
  2838. 1:35:41deeper value, which means like which
  2839. 1:35:43one's more exposed to value if we're
  2840. 1:35:45really like targeting value. Um, and we
  2841. 1:35:47can actually do this, as I was saying,
  2842. 1:35:48with the factor regression. This is how
  2843. 1:35:50this stuff might actually look. In this
  2844. 1:35:52case, I'm just using the Fama French US
  2845. 1:35:545 factor model. And if you remember from
  2846. 1:35:56that video, um, HML, um, high minus low,
  2847. 1:35:59you know, book to market, right? just
  2848. 1:36:00the value sort of factor and we can see
  2849. 1:36:02a loading of 0.34 on IOSV and 0.45 45 on
  2850. 1:36:06VUE, right? This means that VLUE is
  2851. 1:36:09actually a deeper value. It's actually
  2852. 1:36:12more exposed to the value premium,
  2853. 1:36:14right? Remember how I was talking about
  2854. 1:36:15like market beta and stuff? This is
  2855. 1:36:17essentially the beta for value. Um, I
  2856. 1:36:20might even do this maybe next or
  2857. 1:36:21something, you know, if you guys still
  2858. 1:36:22want factor aggression stuff. Um, and
  2859. 1:36:24then the interesting thing that I was
  2860. 1:36:25saying, let's grab maybe like a momentum
  2861. 1:36:27ETF like MTUM. And what I did is I
  2862. 1:36:31pulled the famous French five factor
  2863. 1:36:32model and I actually add momentum to it.
  2864. 1:36:34I kind of like made my own six factor
  2865. 1:36:36model. And when we run this, we can
  2866. 1:36:37actually look at the factor loadings on
  2867. 1:36:39the momentum factor here called MOM. Um,
  2868. 1:36:42and it turns out value is actually like
  2869. 1:36:44a little bit sort of like short
  2870. 1:36:46momentum. In fact, AQR published some
  2871. 1:36:48research on doing like a value momentum
  2872. 1:36:50barbell. Um, I recommended some uh some
  2873. 1:36:52AQR papers to some people in my last
  2874. 1:36:54video. I know I'm just rambling, right?
  2875. 1:36:55I just find it interesting. Um, but we
  2876. 1:36:57can see that the momentum ETF is
  2877. 1:36:58massively exposed to the momentum
  2878. 1:37:00factor, right? it can kind of actually
  2879. 1:37:02in a way almost guide decision-m right
  2880. 1:37:04obviously we're not getting to that
  2881. 1:37:06caliber yet this isn't an analysis or
  2882. 1:37:08anything um I'm just showing you how
  2883. 1:37:09this can sort of be useful is we can
  2884. 1:37:11actually you know see you know this is
  2885. 1:37:12more value than the other um and then we
  2886. 1:37:14also have like rolling regressions which
  2887. 1:37:17is really interesting we're going to
  2888. 1:37:18talk about rolling returns rolling
  2889. 1:37:20regressions um even like the
  2890. 1:37:22non-stationerity of volatility and even
  2891. 1:37:24covariant stuff one of the issues with
  2892. 1:37:26diversification right the quote is they
  2893. 1:37:28say diversification fails you when you
  2894. 1:37:30need it the post, you know, when the
  2895. 1:37:32market crashes, right? Everything might
  2896. 1:37:34crash at the same time. Correlations
  2897. 1:37:35almost in a way converge on one
  2898. 1:37:37depending on what asset you're looking
  2899. 1:37:39at. Um, you know, if you harvest these
  2900. 1:37:41sort of value premiums in a long short,
  2901. 1:37:43that's when you're truly not going to be
  2902. 1:37:45correlated. If you harvest them in a
  2903. 1:37:47long only, you're still going to reap
  2904. 1:37:48the benefits, but they will be like
  2905. 1:37:50massively diminished. In this case,
  2906. 1:37:52right, the value fund, these will never
  2907. 1:37:54have a loading of like one or 1.1 or
  2908. 1:37:570.9. it's never going to reach that
  2909. 1:37:59level since these are long only funds.
  2910. 1:38:01If we did this on a long short fund, we
  2911. 1:38:03could actually see a loading actually
  2912. 1:38:05get close to one. Um either way, if I go
  2913. 1:38:07ahead and do like a rolling regression,
  2914. 1:38:10this is actually going to show us that
  2915. 1:38:11factor premiums themselves are
  2916. 1:38:13non-stationary. They should sort of more
  2917. 1:38:15or less not change that much. Um but we
  2918. 1:38:18can do additional analysis with this. In
  2919. 1:38:19this case, I just have a plot for every
  2920. 1:38:21single ETF and we can actually look and
  2921. 1:38:23see over time how their exposure to the
  2922. 1:38:26premiums change. Um, you know, there's
  2923. 1:38:28like so much stuff we can do within
  2924. 1:38:29Quant. So, you know, I guess just let me
  2925. 1:38:32know what you want to see. Let me know
  2926. 1:38:33if you want the series to continue. Um,
  2927. 1:38:35I have no clue how long I've been
  2928. 1:38:37filming for. Obviously, this video is
  2929. 1:38:38not scripted. The first Quant Finance
  2930. 1:38:40video, 101, was scripted, so it was able
  2931. 1:38:43to be way more condensed. I probably
  2932. 1:38:44wasted way too much time, I guess. Let
  2933. 1:38:46me know which style of video you want to
  2934. 1:38:48see, too, if anyone even made it this
  2935. 1:38:50far. Um, but I think that's really kind
  2936. 1:38:52of everything. So, as always, if you
  2937. 1:38:54made it this far, you might be
  2938. 1:38:56interested in subscribing to the
  2939. 1:38:57channel, checking out some of my other
  2940. 1:38:58content, and turning on notifications.
  2941. 1:39:00Um, now I really do appreciate the
  2942. 1:39:02support on all of my videos. Um, and I
  2943. 1:39:05really also specifically appreciate the
  2944. 1:39:07support on my finance videos. As I was
  2945. 1:39:09saying in my asset pricing video, you
  2946. 1:39:10know, not that many people know, but you
  2947. 1:39:13know, I study like two things formally
  2948. 1:39:15just as much as I study computer science
  2949. 1:39:17and cyber and bug bounties and software
  2950. 1:39:19engine, all that stuff. I'm also all the
  2951. 1:39:21time, you know, looking into like asset
  2952. 1:39:23pricing, quant finance, all the stuff.
  2953. 1:39:25It's literally like half my life and it
  2954. 1:39:26doesn't really get represented on the
  2955. 1:39:28channel, you know. So, if there's like
  2956. 1:39:30interest for this type of thing, you
  2957. 1:39:31know, that's like literally awesome, you
  2958. 1:39:33know. I think this video out of every
  2959. 1:39:35video I ever made on my channel, I
  2960. 1:39:37probably had the most fun making this
  2961. 1:39:38video out of any video. Um, and yeah,
  2962. 1:39:42you know, if there's interest, obviously
  2963. 1:39:43we'll do we'll do Mark Quant stuff. Um,
  2964. 1:39:45you know, this is like this is like
  2965. 1:39:47everything to me. Like this is this is
  2966. 1:39:49incredibly interesting. Anyways, so I
  2967. 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.