YouTube2Text

Q4: Portfolio Optimization – Risk Preferences In, Trades Out · Scott Sanderson — Transcript

by Chat With Traders · 13,047 words · 1,998 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Chat With Traders collaboration with
  2. 0:02Quantopian, episode four.
  3. 0:13What's happening, crew? Your host, Aaron
  4. 0:16Fifield here. Thank you for joining me.
  5. 0:18I really hope you've been enjoying these
  6. 0:21special episodes in collaboration with
  7. 0:23Quantopian up to this point and learning
  8. 0:26lots along the way.
  9. 0:28Right now, you're listening to the
  10. 0:30fourth installment. And as you'll soon
  11. 0:33hear, I'm again speaking with Delaney
  12. 0:35and also Scott Sanderson. Scott is a
  13. 0:38software engineer at Quantopian, and
  14. 0:41he's the one largely responsible for
  15. 0:44writing Zipline, which is an open-source
  16. 0:47Python library that supports the
  17. 0:49Quantopian backtesting and live trading
  18. 0:52system. Uh the topics we get into during
  19. 0:54this particular episode revolve around
  20. 0:57portfolio optimization, optimizing for
  21. 1:00maximum returns and minimal risk. We
  22. 1:04also talk about constraints that a quant
  23. 1:06may implement at a portfolio level,
  24. 1:09other considerations, and we step
  25. 1:11through how a researched, news-driven
  26. 1:13strategy, which trades gun
  27. 1:15manufacturers, could be optimized to
  28. 1:17better work within a portfolio. Now,
  29. 1:20just a couple reminders, as per usual,
  30. 1:23any questions that you would like to
  31. 1:24ask, please submit them at
  32. 1:26quantopian.com/questions.
  33. 1:30Uh so we can answer these for you on the
  34. 1:32Q&A episode. This will be part six.
  35. 1:35Uh for a full list of all the resources
  36. 1:37referenced during this episode and any
  37. 1:40previous episode, visit
  38. 1:42quantopian.com/chatwithtraders.
  39. 1:46And lastly,
  40. 1:48I'd like to give a big shout out to
  41. 1:50DataCamp. They're the ones who are
  42. 1:52backing this series. DataCamp is the one
  43. 1:56resource I always recommend to traders
  44. 1:58for whenever I get asked, "Where do I
  45. 2:00begin learning how to program?"
  46. 2:03The reason being, and the reason that
  47. 2:05DataCamp's courses are so good, is that
  48. 2:07they teach you the skills necessary to
  49. 2:10work with and understand data, which is
  50. 2:13of course exactly what you're doing when
  51. 2:15you do any form of quantitative research
  52. 2:17as a trader. So, visit datacamp.com
  53. 2:21and create a free account. You can start
  54. 2:24any one of their courses for free, and
  55. 2:26they even have some courses which are
  56. 2:28entirely free.
  57. 2:30Just to repeat the link once more,
  58. 2:32that's datacamp.com.
  59. 2:34Jump on it.
  60. 2:35All right. Well, I think that's
  61. 2:36everything. Uh, please welcome Delaney
  62. 2:39and Scott Sanderson.
  63. 2:44And we're back again, episode number
  64. 2:46four. Delaney, how's it going?
  65. 2:48Pretty good. About the same as last
  66. 2:50time. No no major changes. Excellent.
  67. 2:53And we're joined by Scott for this
  68. 2:55episode. Scott, I'm thrilled to have you
  69. 2:57here, man. Thanks for doing it. Yeah,
  70. 2:58thanks for having me on.
  71. 3:00Scott, tell us a little bit about your
  72. 3:01backstory. Like, how did you get into
  73. 3:03this? Yeah, so I had sort of an
  74. 3:05interesting introduction to finance. So,
  75. 3:09I got introduced to Faust, who's the CEO
  76. 3:12here at Quantopian, because his brother
  77. 3:14coached wrestling with my brother
  78. 3:17at
  79. 3:18day school in Newton, which is where I
  80. 3:20grew up. Um, and Faust was in like early
  81. 3:23stages of prototyping the original
  82. 3:25version of Quantopian when it was like
  83. 3:26a, you know, live He was working out of
  84. 3:28his garage and sort of had this big idea
  85. 3:31of what he wanted to be, and he was
  86. 3:33looking for people with uh, for students
  87. 3:36with math backgrounds or with sort of
  88. 3:38math and science backgrounds, which is
  89. 3:40what I was studying at the time in
  90. 3:41college. Um, and so we met up, we had
  91. 3:43coffee, and we talked about a bunch of
  92. 3:45stuff. We got along well, and so I was
  93. 3:46the original beta tester for the like
  94. 3:50early early early versions of
  95. 3:51Quantopian. Um, back when you couldn't
  96. 3:54do things like get the exception if your
  97. 3:56program crashed. You just had to sort of
  98. 3:57squint at the program and hope that
  99. 4:00your thing hope you could figure out
  100. 4:02what it what had gone wrong. Um so for a
  101. 4:05summer I was the original beta tester
  102. 4:07Quantopian and then the next summer I
  103. 4:09interned and
  104. 4:10actually helped build some of the tools
  105. 4:12for doing things like error reporting
  106. 4:14and getting logs out of your algorithms
  107. 4:18and then that experience sort of
  108. 4:19convinced me that I wanted to pursue
  109. 4:21software engineering as a career. So I
  110. 4:23ended up doing a whole bunch of computer
  111. 4:25science classes in college
  112. 4:27got really interested in game design and
  113. 4:29graphics as well from that and so when I
  114. 4:30graduated I actually worked for a game
  115. 4:32studio for about 9 months. Um but I
  116. 4:34found that I missed Quantopian, I missed
  117. 4:36finance, I missed working on open source
  118. 4:38software
  119. 4:39and so about 3 and 1/2 years ago now I
  120. 4:44came back to Quantopian and I've been an
  121. 4:45engineer here ever since. Very cool man.
  122. 4:48And prior to get involved with
  123. 4:50Quantopian right back in the early
  124. 4:52stages, did you have any interest in
  125. 4:56trading or quant finance or anything
  126. 4:58like that at the time?
  127. 5:00I mean you know I was aware of it as a
  128. 5:02thing that existed but I and I like I
  129. 5:05said I my academic background is in
  130. 5:07mathematics but it's actually pretty
  131. 5:09strongly on the pure mathematics side
  132. 5:11rather than the applied math side. So a
  133. 5:13lot of the math that's relevant to
  134. 5:15finance or for trading
  135. 5:17I wasn't actually all that familiar
  136. 5:19with. So it was really you know getting
  137. 5:21introduced to the platform and to foss
  138. 5:23and to the to this community of people
  139. 5:25who were excited about the idea that got
  140. 5:27me interested and engaged with the
  141. 5:29problem and I actually in part sort of
  142. 5:31fell in love with the problem because it
  143. 5:32is such a hard problem. You know doing
  144. 5:35quantitative finance well there's such a
  145. 5:37large incentive to do it and it's such a
  146. 5:39hard problem for a variety of reasons
  147. 5:41that I sort of fell in love with the
  148. 5:43challenge of it as much as anything
  149. 5:45else.
  150. 5:46Right. Yeah. Yeah, I can see how that
  151. 5:47would happen. Tell us a little bit about
  152. 5:50how what you're actually working on at
  153. 5:52Quantopian there. Like I Correct me if
  154. 5:54I'm wrong, but I feel as though I think
  155. 5:56you had a pretty big part to play in
  156. 5:57actually developing the Zipline package.
  157. 6:00Is that correct? Yep. Yeah, so
  158. 6:03one of the things that I did in that
  159. 6:04original internship was when we were
  160. 6:06first working towards open-sourcing
  161. 6:07Zipline. And so I did a lot of the
  162. 6:11Zipline began its life actually as this
  163. 6:13like strange very agnostic stream
  164. 6:15processing library and eventually
  165. 6:17morphed into being much more financially
  166. 6:20focused,
  167. 6:21which is sort of where it is now that
  168. 6:23it's pretty firmly a toolkit for just
  169. 6:25doing
  170. 6:27backtesting. And so
  171. 6:29most of what my responsibilities are
  172. 6:32right now is designing, building, and
  173. 6:35implementing the APIs that our users who
  174. 6:38are writing algorithms interact with. So
  175. 6:41if you're writing an algorithm, you
  176. 6:43know, an algorithm's a Python file and
  177. 6:44you can say like, you know, I want to
  178. 6:46order five shares of Apple and that
  179. 6:47order function has a bunch of things
  180. 6:49that go into it. So you might say, all
  181. 6:51right, I want to order this many shares
  182. 6:52or I want to order this dollar amount or
  183. 6:55you know, I want to order subject to
  184. 6:57different kinds of constraints or
  185. 6:58something like that.
  186. 7:00And so a lot of what my job is to sort
  187. 7:01of think about conceptually how should
  188. 7:03people be thinking about constructing
  189. 7:05their algorithms and then figuring out
  190. 7:08how to provide APIs that make it easy
  191. 7:10and clean for
  192. 7:12people to express their ideas in code
  193. 7:14without having to jump through a whole
  194. 7:15bunch of hoops. And so some of that
  195. 7:16stuff happens in Zipline. We try I like
  196. 7:20personally try to do as much of it as in
  197. 7:22Zipline as I can cuz I think it's
  198. 7:23valuable to us and to the community to
  199. 7:26have
  200. 7:27those parts of the code be open-source
  201. 7:29and be available and be resources to
  202. 7:31people who want to learn about quant
  203. 7:33finance or who want to do their own
  204. 7:34thing.
  205. 7:36So in general
  206. 7:38most of the work I do these days is is
  207. 7:40in Zipline and the pieces that I do on
  208. 7:42Quantopian tend to be things that are
  209. 7:43around like
  210. 7:45touching proprietary data sources or
  211. 7:47building like back-end infrastructure
  212. 7:49for us. Something I'll just add on to
  213. 7:51that is that something that's often
  214. 7:53times the the importance of APIs are
  215. 7:56often lost and um APIs namely like the
  216. 8:00way you interface with a problem or
  217. 8:03interface with some data
  218. 8:05is incredibly important and it's imagine
  219. 8:08that you were kind of talking about a
  220. 8:10problem but just using the wrong words
  221. 8:12and not just getting a clear picture of
  222. 8:14it. Um it's very easy to get into those
  223. 8:17kind of situations. So
  224. 8:18uh Scott does a really good job of kind
  225. 8:20of using his uh pure math background to
  226. 8:23kind of try to figure out ways to
  227. 8:25elegantly express things um and kind of
  228. 8:27like group cases as much as possible.
  229. 8:29And so as a result we've had some pretty
  230. 8:32nice pretty nice API work done in the
  231. 8:34last last couple years and I think the
  232. 8:36site has has vastly improved uh as a
  233. 8:39result. I think we is your one of your
  234. 8:42How official is the title of API Tsar?
  235. 8:44Cuz I know that's been thrown around.
  236. 8:47I think it's it's unofficially official.
  237. 8:49Unofficially official.
  238. 8:50Yeah. He is our our in-house API Tsar.
  239. 8:53Yeah, one of the one of the things that
  240. 8:55uh you'll hear me say in in meetings
  241. 8:57around here a lot is that uh there's a
  242. 8:59famous joke that there's only two hard
  243. 9:00problems in computer science which is
  244. 9:02cache invalidation and naming things.
  245. 9:05Uh and I actually would argue that
  246. 9:06naming things is often harder because
  247. 9:08you know, putting a name on something,
  248. 9:10putting a box on something, figuring out
  249. 9:12how am I going to refer to this thing
  250. 9:15often sort of dictates where the rest of
  251. 9:17the design goes and figuring out trying
  252. 9:20to what the the ontology of your system
  253. 9:22is like what are all the different
  254. 9:23entities and how are they going to talk
  255. 9:25to each other and how are they going to
  256. 9:25relate to each other. If you do that
  257. 9:28well then
  258. 9:30a lot of the rest of the system just
  259. 9:31falls out naturally and sort of models
  260. 9:34the way that you'd want to talk about
  261. 9:35something or think about something in
  262. 9:36the real world. And conversely, if you
  263. 9:38do that poorly, you end up kind of
  264. 9:40having to retroactively bolt on, you
  265. 9:42know, weird, strange things that don't
  266. 9:44make sense or don't seem natural. And
  267. 9:46so, we spend a lot of time, especially
  268. 9:48because we run other people's code, and
  269. 9:50so it's very hard for us to break or to
  270. 9:52make changes that are breaking, we spend
  271. 9:54a lot of time thinking about how do we
  272. 9:56get things right and do it right the
  273. 9:58first time and build designs that are
  274. 10:00elegant and simple and, you know, easy
  275. 10:02to use without being broken in various
  276. 10:04ways. Absolutely. Yeah, yeah, that's
  277. 10:06very important. And I think it was
  278. 10:09uh maybe earlier in 2016 you rolled out,
  279. 10:12was it Quantopian like sort of 2.0? What
  280. 10:14what was some of the bigger changes you
  281. 10:16you made then?
  282. 10:18Yeah, so the the big change with that is
  283. 10:20we had been
  284. 10:22uh increasingly trying to build tools
  285. 10:25that allowed people to trade more
  286. 10:28dynamic portfolios. So, the original
  287. 10:30earliest versions of Quantopian,
  288. 10:32basically, you had to, ahead of time,
  289. 10:35declare that you cared about these 10
  290. 10:36assets or something like that, and then
  291. 10:39you could place orders against those
  292. 10:40assets. And so, with that, you could do
  293. 10:43kind of simple basket trades or pair
  294. 10:45trades or very limited kinds of
  295. 10:47statistical arbitrage, but you couldn't
  296. 10:49do a lot of the more sophisticated
  297. 10:52quantitative strategies that we've been
  298. 10:54working towards encouraging people to
  299. 10:56do, where you're doing dynamic universe
  300. 10:58selection or you're building factor
  301. 10:59models or you're doing interesting, you
  302. 11:02know, risk hedging and that sort of
  303. 11:03thing. Um because the idea of those
  304. 11:06models is you're holding, you know,
  305. 11:07hundreds to thousands of positions, and
  306. 11:08you're making lots of small uncorrelated
  307. 11:11bets. And the idea is if I can make lots
  308. 11:13of those bets and I have a small chance
  309. 11:14of, you know, making money on each one
  310. 11:16of those, then in the aggregate, I can
  311. 11:18end up making a lot of money. And so,
  312. 11:20the goal for something like that, or the
  313. 11:22goal of those APIs was to make it easy
  314. 11:24and pleasant and intuitive to do this
  315. 11:27kind of dynamic universe selection. Um
  316. 11:29and so, the original system that we had
  317. 11:31had, you had to explicitly statically
  318. 11:34refer to some set of assets, and then we
  319. 11:36had grown
  320. 11:38um a series of APIs that
  321. 11:42had kind of been duct-taped onto that
  322. 11:44where you could implicitly set some
  323. 11:47universe of things behind the scenes
  324. 11:48based on a couple simple criteria, but
  325. 11:51then we had all these API functions
  326. 11:52where you could like ask for windows of
  327. 11:54data or ask for the current data or ask
  328. 11:56for fundamental data, and it was
  329. 11:59implicitly depending on this universe
  330. 12:01that was either set at the start of your
  331. 12:03program or set in one of these various
  332. 12:05few limited ways.
  333. 12:07And as people tried to do more
  334. 12:08sophisticated dynamic algorithms,
  335. 12:11those APIs weren't really serving the
  336. 12:14needs that they had. And so, the big
  337. 12:16change in Q2 was that we moved away from
  338. 12:18this notion that your algorithm has some
  339. 12:20implicit universe that you have to set
  340. 12:22in like specific places, and instead, we
  341. 12:24gave people a lot more flexible APIs for
  342. 12:28querying for data based on mathematical
  343. 12:30criteria. So, things like I want, you
  344. 12:31know, the top 10% of US equities by
  345. 12:35market cap or by trailing dollar volume
  346. 12:38or by some other interesting statistical
  347. 12:40property. And then, you could use those
  348. 12:42APIs to dynamically select for sets of
  349. 12:44stocks, and then you could pass those
  350. 12:46sets of stocks into other API functions
  351. 12:48to ask them for data.
  352. 12:50Um and so, that was a kind of a big
  353. 12:52conceptual shift in how the backtester
  354. 12:55and how the platform worked, and uh
  355. 12:57there were a bunch of internal changes
  356. 12:58that had to go along with it so that it
  357. 13:00could handle those dynamic queries.
  358. 13:03Right, very good, man. Very good.
  359. 13:05Well, let's get into this. Um you know,
  360. 13:07the last two episodes have revolved a
  361. 13:09lot around alpha factors, uh combining
  362. 13:12alpha factors, uh and classifiers. So, I
  363. 13:16think this episode's going to mostly be
  364. 13:18about sort of the next step. Like, what
  365. 13:20comes next? So, Delaney, do you want to
  366. 13:23take it away? I mean, what is the next
  367. 13:25step from this point onwards?
  368. 13:27I mean, obviously, this is going to be
  369. 13:29talking about it from a high level. Um
  370. 13:31so, you know, it's not always going to
  371. 13:34be exactly like this. You may have
  372. 13:36slightly different flow in your
  373. 13:38algorithm, but in general, once you've
  374. 13:40constructed
  375. 13:42uh
  376. 13:43your final model, uh and that model is
  377. 13:46usually, you know, some combination of
  378. 13:48of smaller models. Um once you've
  379. 13:50constructed your final model, which is
  380. 13:52kind of
  381. 13:53glomming together a bunch of smaller
  382. 13:54things, making one big overall
  383. 13:56prediction for each asset. Well, now you
  384. 13:59have uh a forward prediction for each
  385. 14:02asset. And that forward prediction could
  386. 14:05be that it's going to go up. It could be
  387. 14:07that it's going to go up 3% tomorrow.
  388. 14:09Like, whatever your prediction is, you
  389. 14:11know, whether it's a weak or strong
  390. 14:13prediction, you have a prediction. And
  391. 14:14the next step is, okay, well, how do you
  392. 14:16actually translate that prediction into
  393. 14:20something that you can trade? Because
  394. 14:23just the prediction itself isn't
  395. 14:25sufficient. Um
  396. 14:28you know, that's just saying, here's
  397. 14:29what I think will happen, but then the
  398. 14:31next step is, how do you actually place
  399. 14:33bets such that if you are correct, you
  400. 14:36will make money.
  401. 14:38Um and if you are incorrect, you won't
  402. 14:40lose all your money and and blow up. So,
  403. 14:43um that process of of deciding what bets
  404. 14:47to place and what positions to take out
  405. 14:49is a complicated process in and of
  406. 14:51itself, and it it's known as portfolio
  407. 14:54construction. So, when people talk about
  408. 14:56portfolio construction, they're talking
  409. 14:58about how do I construct a portfolio
  410. 15:01that, based on my expectation of where
  411. 15:05assets are going to go, will kind of try
  412. 15:08to make me the most money, while at the
  413. 15:10same time not exposing me to excess
  414. 15:14sources of risk. And so, this question
  415. 15:15has kind of been posed in many different
  416. 15:17ways using many different words. Um and
  417. 15:21uh what people do in quantitative
  418. 15:23finance is just try to really condense
  419. 15:26it down into very simple mathematical
  420. 15:28ways of talking about these constraints.
  421. 15:32And so that's going to be kind of one of
  422. 15:33the one of the subjects that we're going
  423. 15:35to talk about today. It's like how do
  424. 15:36you actually express that? So,
  425. 15:39I think Scott actually has a really good
  426. 15:41story that he actually walked through in
  427. 15:44a notebook. So he's actually and I'll
  428. 15:46let him talk about this, but I'll
  429. 15:47briefly I'll briefly sell it. He's
  430. 15:49actually working on a new API right now,
  431. 15:51which I think it's going to be amazingly
  432. 15:53useful.
  433. 15:55And what it is is it's a portfolio
  434. 15:57construction and optimization API. It
  435. 16:00allows you to do everything we're going
  436. 16:01to talk about today.
  437. 16:03Um,
  438. 16:04kind of from a from a from a very easy
  439. 16:06to use perspective. And so he's
  440. 16:08encapsulating all of these notions of
  441. 16:10trying to maximize expected profits
  442. 16:13based on predictions, trying to reduce
  443. 16:15risk exposure and and and
  444. 16:18um,
  445. 16:19trying to you know, put them all in
  446. 16:20quantitative mathematical terms. And he
  447. 16:23recently released kind of a an alpha
  448. 16:26version of this API. It's not on the
  449. 16:28platform yet, but he released an alpha
  450. 16:29version of what it might look like. You
  451. 16:32you can actually play with it right now.
  452. 16:33It's quantopian.experimental.optimize.
  453. 16:36Oh, we have like we have a we have a try
  454. 16:38at your own risk section now. Yeah. Got
  455. 16:40you. Okay, so you can actually play with
  456. 16:43it. And what we'll do is again as always
  457. 16:44we'll link to that.
  458. 16:47If you go to
  459. 16:47quantopian.com/chatwithtraders
  460. 16:49we'll have all of the content for all of
  461. 16:51each episodes linked. And so that will
  462. 16:53be
  463. 16:54one of the things you can access on the
  464. 16:56episode four section.
  465. 16:58And
  466. 17:00you can go and check it out if you want.
  467. 17:02And but in the meantime I'll let I'll
  468. 17:04let Scott actually kind of talk through
  469. 17:06this story of what happens if you have
  470. 17:09some kind of expected returns for a set
  471. 17:12of assets and then you try to naively
  472. 17:14construct a portfolio based on that.
  473. 17:17Yeah, so think it comes down to the idea
  474. 17:20that Dwayne he mentioned earlier, which
  475. 17:21is that when you're when you have some
  476. 17:24quantitative trading strategy, you reach
  477. 17:27a point in your model where you've built
  478. 17:29some prediction of what's going to
  479. 17:30happen,
  480. 17:31but you're not sure about that
  481. 17:33prediction, right? So you your your
  482. 17:35model might have a belief that various
  483. 17:37different assets are going to go up and
  484. 17:39down, but there's some amount of
  485. 17:41uncertainty around that. In addition In
  486. 17:43addition to the uncertainty about the
  487. 17:44particular assets, you also might have
  488. 17:47some amount of some amount of
  489. 17:49uncertainty around how different assets
  490. 17:50tend to move together, or you might have
  491. 17:52some prediction about how different
  492. 17:54assets tend to move together.
  493. 17:56And so you have two kind of competing
  494. 18:00objectives
  495. 18:01as you think about what trades am I
  496. 18:03going to place or what portfolio am I
  497. 18:05going to hold? So on the one hand, you
  498. 18:07want to put as much of your money as you
  499. 18:09can in the assets that you believe are
  500. 18:13going to go up or you know, put as large
  501. 18:15a short position as you can into the
  502. 18:17assets that you believe are going to go
  503. 18:18down.
  504. 18:19But on the other hand, you want to
  505. 18:21mitigate the possible risk that one of
  506. 18:24your predictions is wrong, especially if
  507. 18:26it might be badly wrong, right? So if I
  508. 18:28have some There's some asset that I
  509. 18:30think is going to go up, you know, 10%
  510. 18:33over the next month or something like
  511. 18:35that. But I think there's 30% standard
  512. 18:39deviation around that, right? So I have
  513. 18:40some prediction it's centered super
  514. 18:42high, but I'm actually really uncertain
  515. 18:44about where exactly it's going to land.
  516. 18:47I might be ambivalent about how much
  517. 18:49money I want to put in that asset
  518. 18:50because I have these competing desires
  519. 18:53to on the one hand put my money in
  520. 18:54things that are going to go up, but on
  521. 18:55the other hand not want to put my money
  522. 18:57my money in things that I have
  523. 18:58uncertainty about.
  524. 19:00And so one of the ways that you can
  525. 19:02mitigate the that second aspect, which
  526. 19:06is the the risk aversion, is to force
  527. 19:10yourself to place bets in lots of assets
  528. 19:13that you believe to be
  529. 19:16that you believe will move independent
  530. 19:17of one another. So, if I have three
  531. 19:19different assets, I think they're each
  532. 19:20going to go up, you know, 3% over some
  533. 19:22time period,
  534. 19:24and I place I could just place, you
  535. 19:26know, all of my money in one of them
  536. 19:27and, you know, roll the dice and hope
  537. 19:30that I do well on that asset, but a
  538. 19:32smarter strategy might be to do
  539. 19:34something like put, you know, a third of
  540. 19:36my money in each of them because if they
  541. 19:38all move independently
  542. 19:40then I can be right about
  543. 19:42about two of them and wrong about one of
  544. 19:43them, and I I won't, you know, I'll
  545. 19:46still end up ahead overall.
  546. 19:49And so,
  547. 19:50thinking about a simple example of this,
  548. 19:53if you imagine we have some model that
  549. 19:55produces
  550. 19:57expected returns predictions for, you
  551. 19:59know, a set of assets over some time
  552. 20:01frame. And I might say that, all right,
  553. 20:02I think Twitter is going to go up 2% and
  554. 20:04Apple's going to go up 1% and
  555. 20:05Microsoft's going to go down 1%, you
  556. 20:08know, etc. etc. etc. I have, you know,
  557. 20:10predictions against some array of
  558. 20:12assets.
  559. 20:13And I have some fixed amount of capital
  560. 20:15that I want to deploy to those assets.
  561. 20:17So, that's the other constraint that
  562. 20:18we're generally operating under is that
  563. 20:20we only have so much money to dole out
  564. 20:22among all the different assets we have,
  565. 20:24right? If we If we were perfectly
  566. 20:25confident in some prediction about a
  567. 20:27particular asset, we might say, "Well,
  568. 20:28we're going to put as much money as we
  569. 20:29possibly can into that."
  570. 20:31That's That won't work for other reasons
  571. 20:34cuz it'll The act of putting our money
  572. 20:36into the asset will move the price of
  573. 20:38that asset, but that's kind of
  574. 20:39bracketing that concern for a second.
  575. 20:42In the real world, we only have so much
  576. 20:44capital that we can put towards any
  577. 20:46single asset or towards some portfolio
  578. 20:48of assets. So, that's another constraint
  579. 20:50that we operate under.
  580. 20:51But so, if we imagine we're in this
  581. 20:53world, right, where we have a set of
  582. 20:56assets and we have expected returns
  583. 20:57predictions about them,
  584. 20:59if all we were trying to do was maximize
  585. 21:03our expected return and we had no
  586. 21:04concern for risk, then the optimal
  587. 21:07portfolio for us would be to put all of
  588. 21:09our money in the asset that had the
  589. 21:12largest expected returns. Again, that's
  590. 21:13I'm sort of bracketing the concern of
  591. 21:16slippage that goes into actually
  592. 21:17entering that position, but if we could
  593. 21:19wave a magic wand and have whatever
  594. 21:21portfolio we wanted, the optimal best
  595. 21:23possible portfolio for us would be all
  596. 21:26of our money in the asset that we think
  597. 21:27is going to go up
  598. 21:29by the the largest amount. But, for all
  599. 21:31the reasons we just talked about,
  600. 21:33uh
  601. 21:34we often don't want to do that, right?
  602. 21:36Because we're not that confident in any
  603. 21:38single one of our predictions. And so,
  604. 21:40we'd rather try to spread out our
  605. 21:43allocation to a smaller number of
  606. 21:45assets. And so, one way of thinking
  607. 21:47about this is to frame our problem as
  608. 21:50what's called a constrained optimization
  609. 21:52problem.
  610. 21:53And the idea here is we have some
  611. 21:55objective, which is something like
  612. 21:57maximize returns,
  613. 21:59and we have some uh set of constraints
  614. 22:03on that objective, right? So,
  615. 22:05I have I'm trying to choose the
  616. 22:08portfolio that maximizes my objective,
  617. 22:11but that portfolio has to respect
  618. 22:13certain constraints that I'm putting on
  619. 22:15it.
  620. 22:15And so, those constraints could be
  621. 22:17things like I only want to put I I don't
  622. 22:20want to put any more than 5% of my
  623. 22:22portfolio in any single asset or 1% of
  624. 22:24my portfolio in any single asset. And
  625. 22:27they can get more sophisticated. So, I
  626. 22:28might care about things like I want to
  627. 22:30be market neutral, right? So, I want to
  628. 22:32have about as much long exposure as I
  629. 22:34have short exposure because I don't have
  630. 22:37a strong belief about whether the market
  631. 22:39is going to go up or down over some
  632. 22:40period. Or, even if I do, I don't want
  633. 22:42my portfolio to be correlated with that.
  634. 22:45Um and you can build progressively more
  635. 22:48complex constraints that you can place
  636. 22:50on your on the portfolios that you'll
  637. 22:53allow yourself to hold. And the effect
  638. 22:55that that has is you can
  639. 22:58uh there's actually sort of really
  640. 22:59beautiful robust mathematical theory for
  641. 23:01how we can say, "If I freeze my problem
  642. 23:05as this optimization problem with
  643. 23:08certain constraints, then there are good
  644. 23:11libraries and there's good mathematical
  645. 23:13theory for how to find the portfolio
  646. 23:15that sort of probably mathematically
  647. 23:18will maximize the expected return
  648. 23:20without violating any of those
  649. 23:21constraints." Well, I I feel like
  650. 23:23something maybe it would be useful just
  651. 23:25to to talk about the this notion of
  652. 23:28optimization briefly and like
  653. 23:31one because it's a really central
  654. 23:34concept to a lot of engineering,
  655. 23:37but it's something that is outside of
  656. 23:39engineering not really well understood,
  657. 23:41I would say.
  658. 23:42And also it's very misapplied often
  659. 23:45times because for instance, people often
  660. 23:48talk about maximizing return when they
  661. 23:51forget that what they're really
  662. 23:52maximizing is estimated return, right?
  663. 23:55Because there's some model which is
  664. 23:57estimating what their return will be.
  665. 23:59And that model may be wrong. So, you
  666. 24:01know, there's another failure point that
  667. 24:02you have to be aware of.
  668. 24:04But I want to just get into this notion
  669. 24:06of optimization briefly because I think
  670. 24:08it's a really useful concept to
  671. 24:09understand and and we'll start from like
  672. 24:11the simplest example, which is
  673. 24:14imagine that you have some
  674. 24:17some you know, box in front of you and
  675. 24:20you have
  676. 24:22you know, a dial on the box that can be
  677. 24:24one in one of 100 positions, right?
  678. 24:27And then on the other side of the box
  679. 24:29there's a light bulb and you can record
  680. 24:32how bright that light bulb is, right?
  681. 24:33And and an optimization problem in this
  682. 24:36case would be figure out a dial position
  683. 24:39to
  684. 24:41maximize the brightness of the bulb,
  685. 24:43right? I want to optimize the brightness
  686. 24:46of the bulb and the control I have over
  687. 24:48the system is this dial. So, this is a
  688. 24:50very simple it's like a one-dimensional
  689. 24:52optimization problem because the dial is
  690. 24:55just one dimension. You can kind of move
  691. 24:57the dial to one position between one and
  692. 24:59100. So, a naive solution to this might
  693. 25:03be doing something very simple, which is
  694. 25:04just you try all the positions on the
  695. 25:06dial, and then you pick the best one,
  696. 25:10right?
  697. 25:11In this case, that's pretty feasible
  698. 25:13because there's only 100 positions. You
  699. 25:14can probably do that by hand,
  700. 25:17uh, you know, pretty easily. But, the
  701. 25:19net The next thing that I think is
  702. 25:21important to to pull out of this is,
  703. 25:22"Okay, well, then imagine that you
  704. 25:24plotted the brightness for each dial
  705. 25:27position in a graph." So, now you have
  706. 25:30on the x-axis the dial position, and on
  707. 25:33the y-axis the brightness of the light
  708. 25:35bulb, right? And,
  709. 25:38uh, what this gives you is kind of you
  710. 25:40can start looking at how the brightness
  711. 25:42of the bulb corresponds with the input
  712. 25:45that you put into the system. And so,
  713. 25:47you might notice that maybe it's uh
  714. 25:49quadratic dependence, or maybe it's a
  715. 25:51linear dependence on the dial or
  716. 25:53something like that. You know, you don't
  717. 25:54know what it is yet. So, this is one of
  718. 25:56the ways that engineers and quants will
  719. 25:59start attempting to understand systems
  720. 26:01is they'll look at how they respond to
  721. 26:02the inputs they put into the system. So,
  722. 26:05if you have this problem, uh it's fairly
  723. 26:08easy to imagine how you might go about
  724. 26:09solving this optimization problem,
  725. 26:11right? Where you just kind of you could
  726. 26:12just try all the options, or maybe if
  727. 26:14there were too many options to try, you
  728. 26:16could look at the curve that it
  729. 26:18produced, and then just kind of try to
  730. 26:19figure out where the maximum point on
  731. 26:21that curve was, and
  732. 26:24generalizing this, um all that a
  733. 26:26mathematical optimization is doing is
  734. 26:29just saying, "Well, now the box is a
  735. 26:30function, and the function has some
  736. 26:32inputs, which are your controls, and it
  737. 26:35has an output, right?" And so, in our
  738. 26:37case, we design a function, which is,
  739. 26:41let's say that we hold this portfolio,
  740. 26:44we hold this weight of each asset. So,
  741. 26:47if there's 100 assets we could hold, now
  742. 26:49the function has 100 variables as inputs
  743. 26:53and each variable can just be, you know,
  744. 26:55the weight that we're holding of that
  745. 26:56asset in our portfolio. And then the
  746. 26:59output is what our expected return is
  747. 27:03given that model we're using. And you
  748. 27:05know, that the model is model is in this
  749. 27:07case your your your your forecasting
  750. 27:10model, your alpha model.
  751. 27:12Um
  752. 27:13And what you're trying to do is you're
  753. 27:14just trying to tweak the weights
  754. 27:18uh such that you maximize that expected
  755. 27:20return. And again, in the
  756. 27:21one-dimensional case,
  757. 27:23uh it's a very simple problem. You can
  758. 27:25kind of imagine you can put it in your
  759. 27:26head how you might solve that. And as
  760. 27:28soon as but even if you move to say like
  761. 27:30a two-dimensional case where you have
  762. 27:32two variables you can tweak, you start
  763. 27:34seeing that well, now you can't
  764. 27:36represent it
  765. 27:38as a graph. You actually have to
  766. 27:39represent it as like uh
  767. 27:41as a a graph that has a third axis, you
  768. 27:43know, like one of the three-dimensional
  769. 27:44graphs you may have seen at some point.
  770. 27:47Um and then imagine like four variables
  771. 27:49as inputs. Well, now we're up to a
  772. 27:50four-dimensional graph and that's a
  773. 27:52problem cuz we can't really think in
  774. 27:55four dimensions, right? Humans just have
  775. 27:57no capacity to visualize four
  776. 27:59dimensions. And of course, like when
  777. 28:01we're talking about portfolio, that's
  778. 28:03actually like thousands of dimensions
  779. 28:05because there are often thousands of
  780. 28:06assets that you could hold. So,
  781. 28:08optimization, and I'm not going to go
  782. 28:10into like the the the nuts and bolts of
  783. 28:12it too much. We'll put some resources
  784. 28:14again at quantopian.com/chatwithtraders.
  785. 28:16We'll put some links to tutorials we
  786. 28:18think are really good, visualizations we
  787. 28:20think are really good um to help help
  788. 28:23explain this. But without going into the
  789. 28:24nuts and bolts too much, uh generally
  790. 28:27optimization is just a mathematical way
  791. 28:29of expressing, given any number of
  792. 28:31dimensions, try to kind of walk along
  793. 28:34this space of how bright that light bulb
  794. 28:37is, how how much my expected returns
  795. 28:40are. Try to walk along walk through that
  796. 28:43space. You can have imagine it as a
  797. 28:44landscape. Try to walk through that
  798. 28:46space and find like the highest point in
  799. 28:49that space. And that's the notice that's
  800. 28:51the notion of an optimization. And then
  801. 28:53when you add constraints into it,
  802. 28:55constraints are just like putting fences
  803. 28:57in that space, which say that you can
  804. 28:59walk through the space, but sometimes
  805. 29:00you hit fences which are I can't hold
  806. 29:03more than this of this asset or I'm not
  807. 29:05allowed to move the dial past this point
  808. 29:07because there's a physical blockage
  809. 29:09there. And so you hit that fence you
  810. 29:11say, "Okay, well,
  811. 29:12um I can't move into that space. And so
  812. 29:14now let me try tweaking my parameters in
  813. 29:16another way." So, again, without going
  814. 29:18into the nuts and bolts of how this is
  815. 29:20actually done under the hood of an
  816. 29:21optimizer, uh I think it's really
  817. 29:23important just to get that intuition for
  818. 29:25what an optimization is actually doing.
  819. 29:28Did that make any semblance of sense?
  820. 29:30Yeah, yeah, absolutely. Uh you you
  821. 29:32brought up an interesting point towards
  822. 29:33the end there about constraints that you
  823. 29:35might have um when you're optimizing
  824. 29:38your portfolio.
  825. 29:39You know, you gave a few analogy for
  826. 29:41some constraints. What about some like
  827. 29:43actual real-world um examples of some
  828. 29:46constraints that a quant might put into
  829. 29:49uh their portfolio or their their risk
  830. 29:51model? Sure, I can talk about that a
  831. 29:53little bit. So, one one constraint that
  832. 29:54actually has a really nice geometric
  833. 29:56intuition actually is uh the
  834. 30:00your maximum leverage constraint. Uh you
  835. 30:02you only have so much capital to deploy.
  836. 30:05Um and so again, if we if we think back
  837. 30:08to say Granny's uh
  838. 30:11Granny's doing it.
  839. 30:12Uh Delaney's graph example I think
  840. 30:14a long day for him. I combined graph and
  841. 30:16Delaney in my head there. If we think
  842. 30:17back to Delaney's graph example, right,
  843. 30:19where we're imagining say plotting, you
  844. 30:23know, the expected returns of some
  845. 30:24portfolio as a function of the portfolio
  846. 30:28weights that we choose,
  847. 30:29um
  848. 30:30what it means to say that we're going to
  849. 30:31constrain our leverage, right, we're
  850. 30:33going to constrain the amount of capital
  851. 30:35we're going to deploy is to draw a
  852. 30:37circle if we're in 2D or a a sphere if
  853. 30:39we're in 3D or, you know, as we get to
  854. 30:42higher dimensions, we have hyperspheres
  855. 30:44or something like that, which again we
  856. 30:45sort of lose the ability to visualize,
  857. 30:47but we can fall back on our intuition
  858. 30:49for circles and spheres, but it bounds
  859. 30:51the region of possible portfolios that
  860. 30:53we'll allow ourselves to choose. So,
  861. 30:56one that's one almost everyone in some
  862. 30:59capacity will have a a capital
  863. 31:02constraint on their portfolio.
  864. 31:04Um but then some other common ones, so
  865. 31:06often you'll see um market neutral
  866. 31:09constraints if that's the style of
  867. 31:10strategy you're uh pursuing. You know,
  868. 31:12not everyone tries to be market neutral,
  869. 31:14but it's it's a common thing in the
  870. 31:16quant space.
  871. 31:17Um another common class of constraints
  872. 31:19is people concerned with uh sector
  873. 31:22exposures. So, you might believe that
  874. 31:24companies that are in similar sectors or
  875. 31:26similar industries tend to move in
  876. 31:30correlated ways because their underlying
  877. 31:33exposures are similar, right? So, Apple
  878. 31:35and Microsoft, all things being equal,
  879. 31:38are more likely to move together than
  880. 31:41Apple and McDonald's. One one might
  881. 31:43hypothesize saying a rationale you might
  882. 31:46have for that is both Apple and
  883. 31:48Microsoft might be sensitive to changes
  884. 31:50in the price of silicon or changes in
  885. 31:52the amount of capital that US consumers
  886. 31:54have available to them to to spend or
  887. 31:57for any number of other reasons you
  888. 31:59might have reason to believe that
  889. 32:01companies that are in similar businesses
  890. 32:03will tend to have the their values
  891. 32:05fluctuate uh
  892. 32:07more together than companies that are in
  893. 32:09dissimilar businesses. And so, one of
  894. 32:12the corollaries of that is that if I put
  895. 32:14all of my money in companies that are
  896. 32:15similar in similar businesses, I'm not
  897. 32:18actually reaping very many of the
  898. 32:20diversification benefits that I might
  899. 32:22get if I had a portfolio with lots of
  900. 32:24different companies in it
  901. 32:26because again, the whole point of
  902. 32:29uh diversifying a portfolio is that I'm
  903. 32:31going to put my money into lots of
  904. 32:33different assets that I I are going to
  905. 32:35move
  906. 32:36in an uncorrelated way. And so, it's
  907. 32:39very unlikely, statistically speaking,
  908. 32:41that my portfolio will have a very big
  909. 32:44uh movement up or down, right? I'm I'm
  910. 32:47sort of constraining the likelihood that
  911. 32:49I'm going to have a big movement up in
  912. 32:50exchange for a better belief that I'm
  913. 32:52not going to have a big movement down.
  914. 32:54And
  915. 32:55humans being risk-averse tend to be
  916. 32:56willing to make that trade-off. They're
  917. 32:58tend to be willing to
  918. 33:00accept a smaller but more secure benefit
  919. 33:03in exchange for not having the
  920. 33:06possibility of a very bad thing
  921. 33:07happening to them.
  922. 33:08Um and so, one reason why you might be
  923. 33:11interested in constraining sector
  924. 33:12exposure is this intuition that
  925. 33:14different stocks that are in the same
  926. 33:16sector
  927. 33:17uh still tend to move together. And so,
  928. 33:19I might not want to put too much of my
  929. 33:21portfolio in any single stock. So, you
  930. 33:23can go from kind of very basic,
  931. 33:26easy-to-visualize constraints like how
  932. 33:29much how much money you're going to put
  933. 33:30in your portfolio and and kind of making
  934. 33:33sure that you're sector neutral. And and
  935. 33:35a way to again, another way to kind of
  936. 33:37visualize this, maybe get the intuition
  937. 33:39behind it, is you can imagine the
  938. 33:41optimization process as like a
  939. 33:42negotiation or a discussion between two
  940. 33:45people, right? And so, the one part of
  941. 33:47it says, "Hey, I came up with this great
  942. 33:50idea for how to weight my portfolio."
  943. 33:53And shows it to the risk constraints.
  944. 33:55And the risk constraints say, "You know,
  945. 33:57you can't actually trade this because
  946. 34:00this is going to put a lot of weight in
  947. 34:02your portfolio into the energy sector."
  948. 34:05Um and then, the person who's in charge
  949. 34:07of constraint coming up with the
  950. 34:09portfolio uh comes says, "Okay. Well,
  951. 34:12okay, let me let me try again." And it
  952. 34:13tweaks it a bit and it and it basically
  953. 34:15it's a discussion between these two
  954. 34:17parts where, you know, one is trying to
  955. 34:19keep their returns being maximized and
  956. 34:22the other one is just saying, "You can't
  957. 34:23do this. You can't do this. You can't do
  958. 34:25this." Um
  959. 34:27in terms of constraints, you can go all
  960. 34:29the way up to like uh
  961. 34:31uh some pretty sophisticated and hard to
  962. 34:33visualize constraints. So, I'm going to
  963. 34:36be pretty technical for a second, so
  964. 34:38please bear with me, but remember in the
  965. 34:40last episode we talked about risk
  966. 34:41factors. Risk factors being return
  967. 34:44streams, sometimes computed as the
  968. 34:46returns of a portfolio
  969. 34:49based on an alpha factor. So, if you
  970. 34:51traded an alpha factor, what would your
  971. 34:53returns be on that portfolio? So, like
  972. 34:55when you look at these risk factor
  973. 34:57return streams, often times you'll want
  974. 34:58to make sure that your portfolio doesn't
  975. 35:01have excessive correlation to these
  976. 35:02other known return streams. Uh the
  977. 35:05market is one example of this, where you
  978. 35:06want your portfolio maybe not to have
  979. 35:08excessive correlation with the market.
  980. 35:10You might also want it not to have
  981. 35:11excessive correlation with value. You
  982. 35:14might want to make sure it doesn't have
  983. 35:15excessive correlation with momentum. So,
  984. 35:17this is where the notion of kind of
  985. 35:18factor risk modeling comes in, where you
  986. 35:21can also have as constraints on your
  987. 35:22portfolio, "Okay, now let's look at this
  988. 35:25new set of weights. Let's compute the
  989. 35:27returns had I held this portfolio for
  990. 35:29the last 90 days." And then let's do a
  991. 35:31linear regression where I look at my
  992. 35:33portfolio
  993. 35:35uh versus all these other factors I'm
  994. 35:37worried about having excessive exposure
  995. 35:38to, and if any of these factors have
  996. 35:41betas above some cutoff, I I can't enter
  997. 35:44into that portfolio. So, that's that's
  998. 35:46another set of constraints that quants
  999. 35:48might apply um and and is a much more
  1000. 35:51kind of sophisticated way of thinking
  1001. 35:52about it. Another one that's more
  1002. 35:53mid-range is uh I'm sure a lot of people
  1003. 35:56might be familiar with VAR and CVAR uh
  1004. 35:59and so you can put for instance that the
  1005. 36:01the CVAR
  1006. 36:03uh the conditional value at risk uh of
  1007. 36:05your portfolio
  1008. 36:07um
  1009. 36:08cannot exceed a certain amount. And
  1010. 36:10that's another constraint that would be
  1011. 36:11very reasonable to put on your
  1012. 36:12portfolio, which is really just a
  1013. 36:13measure of the of the volatility. So,
  1014. 36:16there's a lot you can basically think of
  1015. 36:18like any constraint you want you can put
  1016. 36:19in your portfolio. A constraint could be
  1017. 36:21you can't be invested in fossil fuel
  1018. 36:24companies. That's a perfectly valid
  1019. 36:25constraint and is you know, kind of
  1020. 36:27something that people are starting to
  1021. 36:28think about now, um,
  1022. 36:30especially like as endowments are being
  1023. 36:32pressured to divest from fossil fuels.
  1024. 36:34So,
  1025. 36:35a constraint can really be anything, and
  1026. 36:36it really just depends on how on your
  1027. 36:38risk tolerances and your style of
  1028. 36:40investing.
  1029. 36:42One of the things I also want to
  1030. 36:43emphasize there is the way he's talking
  1031. 36:44about,
  1032. 36:46uh, so you're limiting my my C-bar, or
  1033. 36:48limiting my exposure to some linear
  1034. 36:50aggression. Um, and
  1035. 36:53it's important to remember that we're
  1036. 36:54always The only thing we can ever do is
  1037. 36:57limit our expected exposures to things,
  1038. 36:59our expected movements to stuff. So,
  1039. 37:02you know, we talk as though we have the
  1040. 37:03ability to maximize our returns, or
  1041. 37:06minimize our variance, or something like
  1042. 37:08that. But all we can do is build models
  1043. 37:10of what we think the world is going to
  1044. 37:11do, and then try to assume that our
  1045. 37:14functions are good enough, or that our
  1046. 37:15models are good enough, that we can
  1047. 37:18choose portfolios that would minimize
  1048. 37:20our models if they turned out to be
  1049. 37:21correct, or would maximize our models if
  1050. 37:23they turned out to be correct.
  1051. 37:25Sure. Now, Delaney, you brought up, uh,
  1052. 37:28risk factors in your answer there.
  1053. 37:31Uh, we spoke about risk factors a bit
  1054. 37:32more on episode two. So, I think it
  1055. 37:34might be, uh, beneficial just to save
  1056. 37:37any confusion, maybe if you could
  1057. 37:39differentiate between what is a risk
  1058. 37:41factor and what is a actual risk model.
  1059. 37:44Sure. So, again, a risk factor is just a
  1060. 37:47return stream. Um, so it's it's really
  1061. 37:50just kind of the returns on some asset.
  1062. 37:52And the asset is usually, um,
  1063. 37:56some known thing, a known phenomena in
  1064. 37:58the market. Uh, and examples would be
  1065. 38:01the market itself. That's probably the
  1066. 38:03most commonly used risk factor is just
  1067. 38:05the returns on the broad market. The
  1068. 38:06notion being that if your portfolio is
  1069. 38:09highly correlated with the market, then
  1070. 38:11you are taking on a lot of market risk.
  1071. 38:13If the market crashes, you are more
  1072. 38:15likely to crash if you have experienced,
  1073. 38:17you know, high historic correlation with
  1074. 38:18the market. Um, again, like Scott said,
  1075. 38:21this is all estimates, right? And so
  1076. 38:23when we're estimating this is all
  1077. 38:24estimating future risk. This is not
  1078. 38:26exactly quantifying future risk. So,
  1079. 38:30another example is value in which you
  1080. 38:34would say
  1081. 38:35compute high value stocks using your
  1082. 38:37formula and compute low value stocks
  1083. 38:40using your formula. And so now you have
  1084. 38:42an alpha factor, right? This is the
  1085. 38:44alpha factor which ranks everything by
  1086. 38:45value. Then you construct a portfolio
  1087. 38:48where you go long on high value stocks,
  1088. 38:50short on low value stocks. That's your
  1089. 38:52value portfolio. You look at the returns
  1090. 38:55of that portfolio and now because we're
  1091. 38:57back to a return stream, we're back to a
  1092. 38:59risk factor. And so what you can then do
  1093. 39:01is you can say, well, I've come up with
  1094. 39:03this interesting new strategy. But it
  1095. 39:06has very high correlation with value,
  1096. 39:09this known value risk factor. Well, that
  1097. 39:11means that maybe what you're doing isn't
  1098. 39:12that interesting. It's just taking on
  1099. 39:15risk exposure to value. So, a risk model
  1100. 39:19just tries to estimate future risk
  1101. 39:23exposure to these risk factors. And
  1102. 39:26usually how they do it is they look at
  1103. 39:27like a the trailing window. So, they'll
  1104. 39:29look at the last say 90 days or 200
  1105. 39:31days, whatever makes the most sense.
  1106. 39:33They'll look at say like the last 90
  1107. 39:35days and they'll say, over the last 90
  1108. 39:37days, if we run a linear regression
  1109. 39:41between your portfolio's return stream
  1110. 39:44and the return stream of these known
  1111. 39:47risk factors, what are the betas, the
  1112. 39:49estimated dependencies between your
  1113. 39:52portfolio and these known risk factors?
  1114. 39:54And the betas represent a quantification
  1115. 39:56of the amount of risk you're exposing
  1116. 39:59yourself to from each factor. So, that's
  1117. 40:01that's one form of a factor risk model.
  1118. 40:05And and again, like the notion of a risk
  1119. 40:06model in general can be anything. A risk
  1120. 40:09model is just trying to measure how much
  1121. 40:10risk you're taking on. This is just one
  1122. 40:12common way to do it.
  1123. 40:14Uh one one way that I often find helpful
  1124. 40:17for thinking about this idea that I'm
  1125. 40:19interested in quantifying my portfolio's
  1126. 40:22exposure to these various risk factors
  1127. 40:25is to think about the idea that
  1128. 40:27usually in some algorithm you have some
  1129. 40:30investment thesis, right? You have a
  1130. 40:32belief that certain stocks are going to
  1131. 40:34move in some way or that stocks that
  1132. 40:38have some statistical property to them
  1133. 40:40will tend to outperform stocks that
  1134. 40:42don't have that statistical property.
  1135. 40:44Um and if you can encode that idea in a
  1136. 40:47trading algorithm and deploy it uh one
  1137. 40:49of the things that you want to be sure
  1138. 40:52of or that you're interested in
  1139. 40:53understanding is whether
  1140. 40:56the reason if you're algo say your algo
  1141. 40:57does well in a back test or your algo
  1142. 40:59starts doing well in live trading
  1143. 41:01you might be interested in trying to
  1144. 41:03understand whether
  1145. 41:04the reason your algorithm does well is
  1146. 41:06because your investment thesis was true
  1147. 41:09or because or whether it's because your
  1148. 41:11the decisions that your investment
  1149. 41:13thesis implies
  1150. 41:14tend to just really be subscribing you
  1151. 41:17to some other risk factor, right? Or you
  1152. 41:19just happen to be making decisions that
  1153. 41:21were correlated with uh
  1154. 41:24some other industry, right? So uh an
  1155. 41:26example of this might be that I might
  1156. 41:28look at a whole bunch of company
  1157. 41:29fundamentals and decide that I think
  1158. 41:33that companies with high price-to-book
  1159. 41:35ratios or high uh earnings-to-capital
  1160. 41:38ratios are going to do well in over some
  1161. 41:41time period. And so I build a portfolio
  1162. 41:44that you know has a whole bunch of
  1163. 41:45company that's long a whole bunch of
  1164. 41:46companies that have you know these high
  1165. 41:49fundamental ratios and then shorts a
  1166. 41:51bunch of companies that have these low
  1167. 41:52fundamental ratios. Um but one of and
  1168. 41:54you might deploy that and it might do
  1169. 41:55well or it might not do well. Um but
  1170. 41:58either way one of the things that you
  1171. 41:59might not realize about that portfolio
  1172. 42:02if you looked at it is that
  1173. 42:03often again coming back to the sector
  1174. 42:05case often companies that report these
  1175. 42:08financial metrics are for or report
  1176. 42:09these uh
  1177. 42:11you know these these different
  1178. 42:12attributes of themselves, the way that
  1179. 42:14those attributes are reported in
  1180. 42:16different industries or in different
  1181. 42:17sectors or the way they're calculated
  1182. 42:19can be different. And so, it might be
  1183. 42:21that say tech stocks report or tend to
  1184. 42:24have higher returns on certain kinds of
  1185. 42:26investments or lower returns on certain
  1186. 42:28kinds of investments than say natural
  1187. 42:30gas companies. And so, I might have
  1188. 42:32thought that I was building this
  1189. 42:33portfolio that was interestingly
  1190. 42:35optimizing over some fundamental ratio,
  1191. 42:37but really I just accidentally built a
  1192. 42:39portfolio of all tech stocks, right? And
  1193. 42:42one way that I might come to notice that
  1194. 42:44is if I looked if I built a factor model
  1195. 42:47or a risk model of all these different,
  1196. 42:49you know, return streams and I might
  1197. 42:51look at my returns and look at the
  1198. 42:53returns of all these different risk
  1199. 42:54models and realize that oh, I'm actually
  1200. 42:57not really doing anything interesting
  1201. 42:58here. I'm basically just getting the
  1202. 43:00returns that I would get by trading in
  1203. 43:02the tech industry. And so, one one of
  1204. 43:04the things that a risk model can be good
  1205. 43:06for I think is sort of understanding
  1206. 43:08what asset classes you're subscribed to
  1207. 43:12and especially understanding what asset
  1208. 43:13classes you might have accidentally
  1209. 43:14subscribed yourself to when that wasn't
  1210. 43:17your intended investment thesis. Yeah,
  1211. 43:19that's a classic failure mode for quants
  1212. 43:21is doing something really complicated
  1213. 43:23and fancy and then just finding it at
  1214. 43:25the end of the day that it's just a
  1215. 43:28combination of three already known
  1216. 43:30things. So, you know, you come up with
  1217. 43:32some fancy ranking method and then at
  1218. 43:34the end of the day it's the same as if
  1219. 43:36you had just gone long and the consumer
  1220. 43:40cyclical sector or something, you know,
  1221. 43:42and and and the factor models give you a
  1222. 43:44way to figure that out.
  1223. 43:45Okay, so this might be a good point to
  1224. 43:48bring up biases. I mean, this is
  1225. 43:50something we spoke about in episode one
  1226. 43:54of this of this series.
  1227. 43:56You know, when quants are designing
  1228. 43:59their their risk models and building
  1229. 44:02constructing portfolios,
  1230. 44:04is that something is this a stage we you
  1231. 44:06to be mindful of biases again or not so
  1232. 44:10much?
  1233. 44:12Yeah, I mean absolutely. I mean anytime
  1234. 44:14you're doing any kind of statistics on
  1235. 44:17data,
  1236. 44:18you have to be mindful of biases. And
  1237. 44:20and again like
  1238. 44:21biases creep in in really subtle ways
  1239. 44:23and and I'll just I'll say it again.
  1240. 44:25I've said it before like one of the
  1241. 44:27things that I am commonly doing is I'll
  1242. 44:29do like tours and and kind of lectures
  1243. 44:32at the subject of you don't know how
  1244. 44:34wrong you are again like the subject of
  1245. 44:36our first podcast and and really just
  1246. 44:38trying to drive home how easy it is to
  1247. 44:41miss a lot of biases. So I'll give you a
  1248. 44:44really great example of this
  1249. 44:46which is actually like so I'm sure that
  1250. 44:48a lot of people are familiar with this
  1251. 44:50notion of Markowitz portfolio
  1252. 44:51optimization.
  1253. 44:54When Markowitz portfolio optimization
  1254. 44:55was developed, the notion was was simple
  1255. 44:58which was let's say that I have some set
  1256. 45:01of assets that I can invest in
  1257. 45:04and what I'm going to do is I'm going to
  1258. 45:05look at the historical returns of those
  1259. 45:07assets and the historical volatility of
  1260. 45:10those assets and I'm going to do a
  1261. 45:11clever optimization which gives me
  1262. 45:14weights that maximize my
  1263. 45:18you know historical returns and
  1264. 45:21maximizes the average of the historical
  1265. 45:23returns while minimizing the average of
  1266. 45:25the historical volatility of the assets.
  1267. 45:28So it's just this was kind of one of the
  1268. 45:29first implemented or published forms of
  1269. 45:32portfolio optimization and it's taught
  1270. 45:35in kind of like every investment
  1271. 45:38curriculum these days.
  1272. 45:40And and that if you try implementing
  1273. 45:42that, it just does not work at all right
  1274. 45:45now. And and the reason for that is what
  1275. 45:49you're doing is you've implicitly made
  1276. 45:51an assumption which is that you are
  1277. 45:53assuming that historic returns are
  1278. 45:58predictive of future returns and what
  1279. 46:01does that mean? Well, there's a there's
  1280. 46:03a strategy which does that and it's
  1281. 46:04called momentum. Momentum says if things
  1282. 46:07are doing well in the past, I think that
  1283. 46:09they're going to keep doing well in the
  1284. 46:10future. So, a momentum investor is is
  1285. 46:13placing these kind of bets. Now,
  1286. 46:14momentum still exists, but it you know,
  1287. 46:16you have to be careful about the time
  1288. 46:18frame and the industry and like you have
  1289. 46:20to kind of caveat it a lot more now than
  1290. 46:22you used to. Um
  1291. 46:24and and back when Markowitz portfolio
  1292. 46:26optimization was developed, there was so
  1293. 46:28much like there was so much free free
  1294. 46:31money in the market basically and and
  1295. 46:33things weren't as efficient, things
  1296. 46:34weren't as arbitraged away that you
  1297. 46:35could still make money trading off of
  1298. 46:37like a simple momentum factor. Um and
  1299. 46:41so,
  1300. 46:42it basically is important to realize,
  1301. 46:44well,
  1302. 46:45you you you've done something which
  1303. 46:46seems cool, but you've made an
  1304. 46:48assumption which is
  1305. 46:50historical returns imply future returns.
  1306. 46:52Therefore, you're actually if you ran a
  1307. 46:54return stream, if you like did this and
  1308. 46:56then looked at your return stream, you
  1309. 46:58should find yourself highly exposed to
  1310. 47:00the momentum risk factor.
  1311. 47:02Um and that would be an example of oh,
  1312. 47:04maybe you did this and then you looked
  1313. 47:05at your return stream and you found a
  1314. 47:06high dependency on the momentum risk
  1315. 47:08factor and then that would warn you oh,
  1316. 47:11oh, I might I'm probably doing something
  1317. 47:13weird if that's my high dependence.
  1318. 47:14You're also making the same assumption
  1319. 47:16for volatility. You're assuming that
  1320. 47:17historical volatility forecasts future
  1321. 47:20volatility. That may or may not be true.
  1322. 47:22You need to figure that out and validate
  1323. 47:24that. So, that's an example of like how
  1324. 47:26easy it is for kind of biased
  1325. 47:28assumptions to sneak into your model.
  1326. 47:30I'll talk about one more which is if
  1327. 47:33you're trying to estimate the future
  1328. 47:35risk of something,
  1329. 47:37um often times you'll like construct
  1330. 47:39some model which uses some input
  1331. 47:41variables to try to forecast
  1332. 47:43uh the risk at some point in the future
  1333. 47:45for some asset. Um and often times that
  1334. 47:48will be done by looking at the
  1335. 47:49volatility and you'll say, "Okay, let's
  1336. 47:51look at the last 30 days volatility and
  1337. 47:53see how predictive that is of the next
  1338. 47:5510 days volatility." And you know, maybe
  1339. 47:57that that's your model. What's going to
  1340. 47:59be really easy to do is
  1341. 48:02introduce say something like look ahead
  1342. 48:04bias into that validation process in
  1343. 48:06which you accidentally
  1344. 48:08don't shift your data in the right way
  1345. 48:10or you provide your your your model
  1346. 48:13access to future data by accident. And
  1347. 48:15so when you think you're looking at the
  1348. 48:17last 30 days, maybe you're looking at
  1349. 48:19like day minus 20 to day plus 10 by
  1350. 48:22accident. And so you're actually
  1351. 48:23including the volatility of the period
  1352. 48:26that you want to forecast in your model.
  1353. 48:28And and and it's it's kind of tricky to
  1354. 48:30visualize this, but for anybody who
  1355. 48:32actually has ever tried this, this is a
  1356. 48:34problem that I guarantee you you'll
  1357. 48:36you'll have run into is is is biases
  1358. 48:38like look ahead bias. And again,
  1359. 48:40just remember that everything is an
  1360. 48:42estimate and and it's very easy when
  1361. 48:45you're
  1362. 48:46constructing estimates, when you're
  1363. 48:47constructing models to have biases sneak
  1364. 48:50their way in. Yeah. Well, but yeah,
  1365. 48:52biases and
  1366. 48:55I would also often phrase it as you have
  1367. 48:57biases, you also have sort of implicit
  1368. 48:58assumptions, which are I guess synonyms
  1369. 49:00here. So like another assumption that
  1370. 49:03often sneaks into statistically based
  1371. 49:05models
  1372. 49:07is like assumptions about how
  1373. 49:11random variables that we're modeling are
  1374. 49:13distributed, right? So most statistics
  1375. 49:16gets a lot simpler to calculate if you
  1376. 49:18assume that lots of things are normally
  1377. 49:19distributed. And normally a normal
  1378. 49:21distribution is the the nice bell curve
  1379. 49:23that you see in on the front of every
  1380. 49:25statistics book and it lots of formulas
  1381. 49:27sort of work out nicely
  1382. 49:29if you believe that assets kind of or
  1383. 49:33that returns or that volatilities or
  1384. 49:36that any other kind of thing that you're
  1385. 49:38trying to
  1386. 49:39predict is going to be sort of shaped
  1387. 49:41like this nice bell curve.
  1388. 49:44But in practice that often tends turns
  1389. 49:46out not to be the case. And it's easy to
  1390. 49:50look at a formula that comes out of a
  1391. 49:52statistics textbook or that comes out of
  1392. 49:54a paper and not realize that that
  1393. 49:57formula that's trying to build a model
  1394. 49:59for how something's going to work in the
  1395. 50:01future is making assumptions about say
  1396. 50:04whether certain variables are normally
  1397. 50:06distributed or distributed in some other
  1398. 50:08way that may not actually be an accurate
  1399. 50:11reflection of how the world actually
  1400. 50:12works. Yeah, and and just as a as a
  1401. 50:15quick last point, like this is a very
  1402. 50:17common thing to do.
  1403. 50:19Very very common thing to do and and it
  1404. 50:21just is again, if you don't check all
  1405. 50:23your assumptions, just always check your
  1406. 50:25assumptions, but if you don't check your
  1407. 50:26assumptions, you might try to compute
  1408. 50:27something like value at risk, but in
  1409. 50:30such a way that you say, "I'm assuming
  1410. 50:32that everything's going to be normally
  1411. 50:33distributed." We actually have
  1412. 50:35a lecture in the lecture series. We'll
  1413. 50:38link to it. And basically the lecture
  1414. 50:40shows that how different real returns
  1415. 50:43distributions are from normal
  1416. 50:45distribution. So if you use an assumed
  1417. 50:47normal distribution to estimate, you
  1418. 50:49know, the conditional value of risk of
  1419. 50:51your portfolio, you'll just be way off
  1420. 50:54from the actual amount you could expect
  1421. 50:55to lose on your portfolio and in the
  1422. 50:58wrong direction, which is you think that
  1423. 50:59you will lose a lot less than you
  1424. 51:01actually will.
  1425. 51:03In practice, very very few things in
  1426. 51:05finance are normally distributed. So
  1427. 51:07this is that's a great example of of a
  1428. 51:08bias that can sneak in. Sure. Sure.
  1429. 51:11Now Dalania, I know you wanted to bring
  1430. 51:13up the gun trading strategy. I'm not
  1431. 51:16sure what the proper title for it is,
  1432. 51:18but uh
  1433. 51:19inflammatory title.
  1434. 51:23So
  1435. 51:24yeah, let let's go into that. I think
  1436. 51:26this is also very interesting and it
  1437. 51:27might help to kind of bring some of
  1438. 51:29these subjects we've been talking about
  1439. 51:31together and kind of um
  1440. 51:34yeah, bring it all together and talk
  1441. 51:35about how things are actually
  1442. 51:36implemented in the real world. So maybe
  1443. 51:39if we just start by talking about what's
  1444. 51:40the actual hypothesis of this trading
  1445. 51:42strategy.
  1446. 51:44Sure, and again we'll we'll we'll link
  1447. 51:45to this quantopian.com/chatwithtraders
  1448. 51:48so you can actually go check it out and
  1449. 51:50you know, read the article. But this was
  1450. 51:52actually some work that we did in
  1451. 51:54collaboration with a reporter at
  1452. 51:56Reuters. And
  1453. 51:59um
  1454. 51:59they this was back
  1455. 52:02I When was this published? Early this
  1456. 52:03year
  1457. 52:04in in February. And this was at a time
  1458. 52:08when there was a lot of public debate
  1459. 52:11and very unfortunately a lot of public
  1460. 52:14debate about mass shootings, especially
  1461. 52:18in the US where there's a lot of debate
  1462. 52:19about you know, like does this make
  1463. 52:21sense? Do we need to implement reforms?
  1464. 52:23There's you know, there's just a lot of
  1465. 52:25debate on both sides.
  1466. 52:27And so one thing that people have
  1467. 52:29started to think about is well, what how
  1468. 52:31can we actually quantify this? So that
  1469. 52:33rather than you know, like talking
  1470. 52:34points, we can actually maybe draw some
  1471. 52:36conclusions based on the data. An
  1472. 52:38offshoot of this was a reporter was
  1473. 52:39interested in if you tried to make a
  1474. 52:43trading strategy,
  1475. 52:46you know, based on these mass shooting
  1476. 52:48events, how would that work? And And
  1477. 52:50they're basically testing this
  1478. 52:52hypothesis that people tend to buy more
  1479. 52:56guns in the states when mass shootings
  1480. 53:00occur. So
  1481. 53:02what we did is we did we did some work
  1482. 53:04specifically James Christopher who's a
  1483. 53:06an intern at Quantopian did some work
  1484. 53:08and
  1485. 53:10on on this and they they came up with a
  1486. 53:12strategy that
  1487. 53:15says if you if you experience news
  1488. 53:19of a mass shooting event,
  1489. 53:21buy into stocks of gun companies, gun
  1490. 53:26manufacturers, ammo manufacturers, etc.
  1491. 53:29Um
  1492. 53:30So this is a strategy, right? And it's
  1493. 53:32based on a hypothesis. And again, I just
  1494. 53:34want to draw back to this notion of like
  1495. 53:36hypothesis based strategy design where
  1496. 53:38you have a hypothesis, you you kind of
  1497. 53:40like throw a bunch of stuff at the wall
  1498. 53:42and see what works in a back test, but
  1499. 53:43you have a hypothesis. The hypothesis is
  1500. 53:45that people buy guns after mass shooting
  1501. 53:48events. And so you're going to say,
  1502. 53:50well, can I actually trade off that? Do
  1503. 53:52I do I notice the difference? And so um
  1504. 53:55they tested that and they found that um
  1505. 53:58it seems in the data set that they have
  1506. 54:01uh you do experience
  1507. 54:03um a fairly good returns if you invest
  1508. 54:07in
  1509. 54:08um gun companies after either mass
  1510. 54:10shootings or political events. Um
  1511. 54:13political events are things where maybe
  1512. 54:16um
  1513. 54:17uh politicians say that they're going to
  1514. 54:18implement gun control and then that can
  1515. 54:20also be a driver of people going to buy
  1516. 54:23uh buy weapons. So it's just an
  1517. 54:26interesting example of a strategy and
  1518. 54:29uh
  1519. 54:31I think that it has some interesting
  1520. 54:32flaws which fall into the topic of
  1521. 54:35portfolio construction and um can kind
  1522. 54:38of show us some of the ideas of like,
  1523. 54:40okay, well, here's a strategy which is
  1524. 54:43looking at, how do I optimize returns
  1525. 54:46based on my models? So my model in this
  1526. 54:48case says that after this point in time,
  1527. 54:53you know, after after these events, I'm
  1528. 54:55going to forecast that these companies
  1529. 54:57will will get good returns. These these
  1530. 55:00gun companies will get good returns. And
  1531. 55:03um
  1532. 55:04the other companies I'm not really
  1533. 55:05making a forecast on them. Let's just
  1534. 55:07say that they're going to stay neutral,
  1535. 55:09whatever. I don't I don't know what's
  1536. 55:10going to happen. Um so that's your
  1537. 55:12model. And then your your your your
  1538. 55:15challenge is how do you actually turn
  1539. 55:16that into something that trades? So
  1540. 55:18naively, you might say, okay, we'll just
  1541. 55:20after each event buy into all those
  1542. 55:23companies and just sit and hold them
  1543. 55:24long.
  1544. 55:25Right? There are some problems with this
  1545. 55:28as as we've discussed. Basically that if
  1546. 55:30you just naively construct your
  1547. 55:31portfolio this way,
  1548. 55:34uh well, not only are you exposed long
  1549. 55:37only to the market,
  1550. 55:39but you're also invested completely in
  1551. 55:41one sector,
  1552. 55:43and you're also invested completely in
  1553. 55:45companies that are likely to all be very
  1554. 55:48correlated because they're all producing
  1555. 55:50the same thing, the same good. So,
  1556. 55:54um
  1557. 55:55this would be a good example of like
  1558. 55:57maybe we can briefly talk about how you
  1559. 55:59might go from this model which forecast
  1560. 56:02that gun companies will go up to a more
  1561. 56:04reasonable portfolio,
  1562. 56:07and how the constraints that we talked
  1563. 56:09about earlier might start driving your
  1564. 56:11portfolio into a more reasonable place
  1565. 56:14based on based on, you know, like
  1566. 56:17how how you constrained the problem. So,
  1567. 56:20did you have any like questions about
  1568. 56:21what I talked about or maybe we can just
  1569. 56:23jump into talking about how the
  1570. 56:24constraints might reshape your
  1571. 56:25portfolio. Well, yeah, I I think that'd
  1572. 56:27be really interesting to hear about
  1573. 56:29because obviously you pointed out there
  1574. 56:30are a couple flaws to this
  1575. 56:32hypothesis-driven
  1576. 56:34trading strategy. So,
  1577. 56:36how would you actually go about
  1578. 56:38implementing that into a portfolio and
  1579. 56:40something that would be worthwhile
  1580. 56:42trading?
  1581. 56:44So, I'll I'll let Scott jump in after
  1582. 56:46this, but the idea would be that let's
  1583. 56:48say that you had Scott's API ready to
  1584. 56:50go. And you know, you just you get this
  1585. 56:53signal of I want to trade these stocks
  1586. 56:55cuz I think they're going to go up,
  1587. 56:57right?
  1588. 56:58Well, okay, then you say, but I'm also
  1589. 57:00going to constrain that no
  1590. 57:04I can't put more than
  1591. 57:071% of my capital in any individual
  1592. 57:09asset. That's a reasonable constraint,
  1593. 57:12but let's say that there are 10 10 gun
  1594. 57:14companies total that were interested in
  1595. 57:16investing in. Well,
  1596. 57:19what's going to happen? Well, what's
  1597. 57:20going to happen is that
  1598. 57:24that optimizer is going to say, I'm
  1599. 57:26trying to get into the gun companies.
  1600. 57:28I'm trying to put all my money into the
  1601. 57:29gun companies, but the constraints
  1602. 57:31aren't letting me. They're saying I'm
  1603. 57:33going to max out at 1% of my portfolio
  1604. 57:35in each gun company, and then I'm going
  1605. 57:37to have to put the rest of it somewhere
  1606. 57:38else.
  1607. 57:39And so, you'll probably get like kind of
  1608. 57:41an even distribution across other assets
  1609. 57:44in in your universe. They just may may
  1610. 57:46or may not be in the same industry,
  1611. 57:48could be all over the place. And then
  1612. 57:49we'll talk about maybe we'll put in
  1613. 57:50another constraint, which is I cannot be
  1614. 57:54exposed to any sector. Right? And so,
  1615. 57:57what does that mean? Well, we have a
  1616. 57:58bunch of
  1617. 58:00long bets in whatever sector gun
  1618. 58:02companies live in.
  1619. 58:04Um so, that means that we also have to
  1620. 58:06put in place a bunch of short bets in
  1621. 58:08the same sector
  1622. 58:10to bring our to bring our exposure in
  1623. 58:12that sector back down to zero. We're not
  1624. 58:14exposed to that sector. And so, now the
  1625. 58:17constraints say, um because you have to
  1626. 58:19be kind of even across the sector, it's
  1627. 58:21going to push you into having some short
  1628. 58:23positions in the same sector, companies
  1629. 58:25in the same sector that aren't the gun
  1630. 58:27companies you wanted to be long in. So,
  1631. 58:30already now we're starting to get into
  1632. 58:31these interesting, you know, we've only
  1633. 58:33put in place two constraints, but we've
  1634. 58:35already switched from a portfolio that
  1635. 58:36holds 10 assets to a portfolio that
  1636. 58:38holds, let's say, like 500 assets, and a
  1637. 58:41portfolio which has longs and shorts,
  1638. 58:43just based on these two constraints.
  1639. 58:46Does that make sense?
  1640. 58:48I think so, yeah.
  1641. 58:49And maybe if you could just repeat that
  1642. 58:51part about why you need to take short
  1643. 58:53trades. Is that just a just an example
  1644. 58:56of a constraint that you might put into
  1645. 58:58your portfolio that that means that you
  1646. 59:00can't just be long only?
  1647. 59:03So, so I think
  1648. 59:05uh and this algorithm's a little bit
  1649. 59:07interesting because it's it's an event
  1650. 59:08study and and at least as it was
  1651. 59:10originally implemented, it actually has
  1652. 59:12long periods of time where it doesn't
  1653. 59:13hold any positions at all. And so, uh
  1654. 59:16that's very different than some of the
  1655. 59:18styles of strategies that we were
  1656. 59:19talking about right now. But I I think
  1657. 59:21the reason why you might be interested
  1658. 59:24in optimization or something like it for
  1659. 59:27a strategy like this goes back to the
  1660. 59:29idea that I was talking about a little
  1661. 59:31bit earlier, which is that you want to
  1662. 59:32make sure that what you're trading is
  1663. 59:34actually your original hypothesis and
  1664. 59:37not something that merely happens to
  1665. 59:38coincide with the hypothesis.
  1666. 59:41Um so in this algorithm, right, our
  1667. 59:44hypothesis is that
  1668. 59:46gun companies specifically, you know,
  1669. 59:47these 10 or these I don't know how many
  1670. 59:49assets this trades off the top of my
  1671. 59:51head, but I have a specific hypothesis
  1672. 59:54about a about the behavior of these
  1673. 59:55specific assets when these specific
  1674. 59:58events happen.
  1675. 59:59Um
  1676. 1:00:00and so
  1677. 1:00:01if I'm if that's the hypothesis I have,
  1678. 1:00:04what I want to make sure that I'm doing
  1679. 1:00:05is actually trading on that hypothesis.
  1680. 1:00:09And so it might be, for example, that
  1681. 1:00:12all of the uh gun companies that I'm
  1682. 1:00:15trading also all
  1683. 1:00:17say belong, I don't I don't know how gun
  1684. 1:00:19companies get classified by by sector by
  1685. 1:00:21industry, but
  1686. 1:00:23uh they might also all have similar
  1687. 1:00:25returns or similar risk profiles to
  1688. 1:00:28um
  1689. 1:00:29uh let's say like the retail market,
  1690. 1:00:31right? They're probably sort of behave
  1691. 1:00:32kind of similarly to sporting goods in a
  1692. 1:00:34lot of ways. Um
  1693. 1:00:35so
  1694. 1:00:37one thing that we might
  1695. 1:00:39one way of phrasing our hypothesis,
  1696. 1:00:41right, is that
  1697. 1:00:43when mass shootings happen, gun
  1698. 1:00:45companies will go up. And that might be
  1699. 1:00:46the extent of our hypothesis.
  1700. 1:00:49Um but that's actually quite hard to
  1701. 1:00:50say, right? Because if the market is
  1702. 1:00:52tanking, then even if gun companies do
  1703. 1:00:55better than everybody else, our our
  1704. 1:00:57strategy might not do well, right? If it
  1705. 1:00:59happened to be that mass shootings
  1706. 1:01:02always happened at the same time as the
  1707. 1:01:04economy was tanking, then even if our
  1708. 1:01:06hypothesis was in some sense true,
  1709. 1:01:08right, that gun companies do better on
  1710. 1:01:11average than other companies when a
  1711. 1:01:13shooting happens, we still might
  1712. 1:01:14actually lose money on the strategy
  1713. 1:01:16because
  1714. 1:01:17the whole market was tanking, so gun
  1715. 1:01:19companies went down as well, they just
  1716. 1:01:20went down less than other companies. And
  1717. 1:01:22so, we might move from our hypothesis
  1718. 1:01:25being merely that, you know, when a
  1719. 1:01:28shooting happened a gun happens a gun
  1720. 1:01:29company goes up to when a shooting
  1721. 1:01:31happens a gun company will do better
  1722. 1:01:33than other similar company or other
  1723. 1:01:35companies that otherwise might behave
  1724. 1:01:37similarly, right? And so, that's where
  1725. 1:01:39you start to be interested in this sort
  1726. 1:01:41of hedging idea or this
  1727. 1:01:43sector neutrality or market neutrality
  1728. 1:01:44idea, right? Is that my hypothesis is no
  1729. 1:01:47longer just that gun companies will
  1730. 1:01:49unconditionally go up. It's that they
  1731. 1:01:51will do better than similar companies or
  1732. 1:01:54they will do better than the market as a
  1733. 1:01:56whole.
  1734. 1:01:57Well, and so, if I wanted to trade on
  1735. 1:01:59that more refined hypothesis, then I
  1736. 1:02:02might be interested in this idea that,
  1737. 1:02:04okay, when I enter into a long position
  1738. 1:02:06in, you know,
  1739. 1:02:08these gun companies as a result of a
  1740. 1:02:10mass shooting, I might simultaneously
  1741. 1:02:12enter into a corresponding short
  1742. 1:02:14position in, you know, other companies
  1743. 1:02:16that are in the retail sector or just in
  1744. 1:02:18a broad market
  1745. 1:02:20set of companies based on certain risk
  1746. 1:02:22criteria. And then,
  1747. 1:02:24once I start if I go to that latter
  1748. 1:02:26route, right, where I'm saying, all
  1749. 1:02:27right, I'm going to enter into a a small
  1750. 1:02:29number of longs in these specific
  1751. 1:02:31companies,
  1752. 1:02:32and I'm going to enter into a large
  1753. 1:02:34number of shorts in these other
  1754. 1:02:35companies to be to be, say, diversified
  1755. 1:02:37there or to ensure that they're sort of
  1756. 1:02:39stable,
  1757. 1:02:40then I might start to be interested in
  1758. 1:02:43some of the more sophisticated exposure
  1759. 1:02:44measures that we were talking about
  1760. 1:02:46earlier, where I want to make sure that
  1761. 1:02:47I'm not accidentally just going super
  1762. 1:02:50short the food service industry or going
  1763. 1:02:52super short the telecom industry or
  1764. 1:02:54something like that, but that what I'm
  1765. 1:02:55really doing is just hedging out the
  1766. 1:02:58overall market effect or the overall
  1767. 1:03:00consumer cyclical effect. Okay. Yeah,
  1768. 1:03:03yeah, I think that that makes it a
  1769. 1:03:04little clearer.
  1770. 1:03:06Um Delaney, is there anything you'd like
  1771. 1:03:07to add more onto the this particular
  1772. 1:03:10trading strategy um an example?
  1773. 1:03:15Um no, I think that Scott like really
  1774. 1:03:18kind of touched on all the points that I
  1775. 1:03:19want to touch on and and I would just
  1776. 1:03:20say, like,
  1777. 1:03:22just to reiterate, there's like a
  1778. 1:03:23tremendous number of constraints you can
  1779. 1:03:25put on a portfolio and they all kind of
  1780. 1:03:28get at different ways you could, you
  1781. 1:03:30know, you could fall apart. And
  1782. 1:03:34this is just talking about a few
  1783. 1:03:36different constraints you could put on
  1784. 1:03:37to avoid a few, you know, try to avoid a
  1785. 1:03:39few different sources of risk. And at
  1786. 1:03:42the end of the day, like, the number of
  1787. 1:03:44constraints that a professional quant
  1788. 1:03:47shop is going to be putting on their
  1789. 1:03:49portfolios is probably going to be, you
  1790. 1:03:51know, a lot more than just saying, "We
  1791. 1:03:53want to be neutral in each sector. We
  1792. 1:03:55want to be neutral in the market. We
  1793. 1:03:57want to invest this amount of money and
  1794. 1:03:58we want to be exposed to these factors."
  1795. 1:04:00You might have a a few more a few more
  1796. 1:04:02things than that, depending on your
  1797. 1:04:03investment thesis. You might not be
  1798. 1:04:05allowed to trade certain stocks for
  1799. 1:04:06legal reasons because one of your board
  1800. 1:04:08members owns some company and, you know,
  1801. 1:04:11there's there's more complex constraints
  1802. 1:04:13that you you you can have to put on on
  1803. 1:04:15portfolios sometimes.
  1804. 1:04:17But yeah, I think one of the things I do
  1805. 1:04:19I would want to add to that is that
  1806. 1:04:21I wouldn't want to say that, you know, a
  1807. 1:04:23sector-neutral portfolio or a
  1808. 1:04:24market-neutral portfolio or or some
  1809. 1:04:26other sort of more fancifully
  1810. 1:04:27constructed portfolio is categorically
  1811. 1:04:29better than, you know, a more simple
  1812. 1:04:31event strategy or or more simply
  1813. 1:04:33constructed portfolio. But what what
  1814. 1:04:36these more sophisticated tools allow you
  1815. 1:04:38to do is refine hypotheses, right? It
  1816. 1:04:41makes sure that when I'm trying to trade
  1817. 1:04:44a strategy that I am focused on
  1818. 1:04:48building a strategy that actually
  1819. 1:04:50reflects the specific idea or the
  1820. 1:04:53specific thesis that I have, right? It
  1821. 1:04:54allows us to narrow down the sources of
  1822. 1:04:58possible reasons why an algorithm might
  1823. 1:05:00do well, so that we have a better reason
  1824. 1:05:03to believe that if an algorithm does
  1825. 1:05:05does well, it's because we were actually
  1826. 1:05:07correct about some feature of the market
  1827. 1:05:09or some feature of particular companies.
  1828. 1:05:12Mhm. Okay.
  1829. 1:05:14Um now, I'm just curious um I know there
  1830. 1:05:16was an article about this strategy on uh
  1831. 1:05:18Reuters.
  1832. 1:05:20Um I'm not sure if the article itself
  1833. 1:05:22actually kind of laid out how the
  1834. 1:05:25strategy actually traded around the
  1835. 1:05:27events. Is there anywhere we can get
  1836. 1:05:29more info on that?
  1837. 1:05:30Yeah, we'll link to the actual
  1838. 1:05:32Quantopian forum post, which goes along
  1839. 1:05:34with the article, and that actually
  1840. 1:05:36gives you the full source code of the
  1841. 1:05:38strategy. So, you can actually see
  1842. 1:05:39exactly what it's doing. Yeah, that'd be
  1843. 1:05:42great. That'd be great. And um you know,
  1844. 1:05:44obviously we can we can check that out,
  1845. 1:05:46but just while we got this opportunity,
  1846. 1:05:48how were the actual trading signals
  1847. 1:05:50generated? Like, obviously this was not
  1848. 1:05:52so much a uh
  1849. 1:05:54a
  1850. 1:05:55a strategy that's based on price action.
  1851. 1:05:57It was based on um events and news and
  1852. 1:06:00that sort of thing. Uh was it still
  1853. 1:06:02obviously still an automated strategy?
  1854. 1:06:05How
  1855. 1:06:06how was it getting the trade signals?
  1856. 1:06:07Was there a a news reading algorithm of
  1857. 1:06:09some sort?
  1858. 1:06:11So, um in this case, we were just
  1859. 1:06:12looking at a historical window. We
  1860. 1:06:14weren't trading this thing live. Uh and
  1861. 1:06:16so what we did is we just went back and
  1862. 1:06:19with the help of Reuters, we just
  1863. 1:06:22um before looking at the returns, we
  1864. 1:06:24just laid out the events that we were
  1865. 1:06:26concerned with, right? We just laid out
  1866. 1:06:28all the events which they would say like
  1867. 1:06:31they classified as yes, we believe that
  1868. 1:06:33this is an event um you know, after
  1869. 1:06:35which uh
  1870. 1:06:37the returns on gun companies would be
  1871. 1:06:38higher than comparable companies. And um
  1872. 1:06:41so, we had just like a CSV of the dates,
  1873. 1:06:44and um
  1874. 1:06:46whenever one of those dates came along,
  1875. 1:06:48we would just jump into uh jump into uh
  1876. 1:06:52gun companies. And so, it was basically
  1877. 1:06:54what you'd have to do if you wanted to
  1878. 1:06:56switch that to something that could
  1879. 1:06:57trade live is yes, you'd have to have um
  1880. 1:07:01an automated way of pulling in news of
  1881. 1:07:05these events, classifying the events as,
  1882. 1:07:08yes, this is an event which I believe is
  1883. 1:07:10likely to influence,
  1884. 1:07:13um, you know, gun companies positively,
  1885. 1:07:15uh, or not, and then kind of have that
  1886. 1:07:18event feed in live rather than have it
  1887. 1:07:20be a static CSV file. You could also
  1888. 1:07:22just have a a human press a button
  1889. 1:07:25whenever they thought that this was an
  1890. 1:07:27event, and then that would also, you
  1891. 1:07:29know, add it to the queue. Um, that's
  1892. 1:07:31perfectly reasonable as long as, you
  1893. 1:07:32know, the rest of the chain is still is
  1894. 1:07:35still quantified. I think that's an
  1895. 1:07:36interesting idea because, um, you don't
  1896. 1:07:38have to always be fully quantified.
  1897. 1:07:40There are some things that humans still
  1898. 1:07:42do better. And if your strategy is
  1899. 1:07:44trading on a frequency which is still
  1900. 1:07:45reasonable, like it's not like, you
  1901. 1:07:47know, tons of times a minute, if it's
  1902. 1:07:48trading like once a day or whatever,
  1903. 1:07:50well, you can have a human press a
  1904. 1:07:52button that says, I believe this event
  1905. 1:07:53has happened, and then the nice thing is
  1906. 1:07:55that the strategy will still go and do
  1907. 1:07:57all the portfolio optimization and risk
  1908. 1:07:59analysis and everything and construct a
  1909. 1:08:01good portfolio and trade on that
  1910. 1:08:02portfolio and get into it quickly. Um,
  1911. 1:08:05and it's a way of just like saying, I'm
  1912. 1:08:06going to take the best part of what a
  1913. 1:08:08human can do, and also the best part of
  1914. 1:08:10what a strategy and a computer can do,
  1915. 1:08:12and kind of combine them together. So,
  1916. 1:08:14you can you can get a little bit more
  1917. 1:08:15flexibility around that. Yeah, sure.
  1918. 1:08:17Okay, excellent. Well, guys, let's leave
  1919. 1:08:20it at that for now. Um, next episode,
  1920. 1:08:23uh, last one before we do the episode
  1921. 1:08:26number six, which will be Q&A. So,
  1922. 1:08:28episode five, uh, what are the key
  1923. 1:08:30topics we'll be discussing, uh, on that
  1924. 1:08:32one, Delani?
  1925. 1:08:34Well, uh, we're going to talk about good
  1926. 1:08:36and bad uses of machine learning in
  1927. 1:08:39quantitative finance or finance in
  1928. 1:08:40general, because turns out there are a
  1929. 1:08:42lot of bad uses, but there are also a
  1930. 1:08:44lot of great uses. And, uh, I think that
  1931. 1:08:47people who enter in, especially people
  1932. 1:08:49who enter in with, um, some machine
  1933. 1:08:51learning under their belt, uh,
  1934. 1:08:53oftentimes will not have a good
  1935. 1:08:56distinction of what are the good uses
  1936. 1:08:58and bad uses, because finance actually
  1937. 1:09:00behaves very differently from a lot of
  1938. 1:09:01other classical applications of machine
  1939. 1:09:04learning. There's a lot of
  1940. 1:09:05non-stationarity, conditions change all
  1941. 1:09:07the time.
  1942. 1:09:08Um you can think of this machine
  1943. 1:09:10learning algorithm is like trying to
  1944. 1:09:11learn about the world, but by the time
  1945. 1:09:13it's figured out how the world works,
  1946. 1:09:14the world has changed. Um that's a
  1947. 1:09:16classic failure mode. And also, often
  1948. 1:09:19times machine learning algorithms
  1949. 1:09:20operate in situations where the risk
  1950. 1:09:22isn't really a huge problem. So, for
  1951. 1:09:23instance, if you're trying to like
  1952. 1:09:25maximize the number of people who will
  1953. 1:09:27respond to an email that you send out as
  1954. 1:09:29a marketing firm. Um you know, there's
  1955. 1:09:32not a huge risk necessarily in that
  1956. 1:09:35case. Like, nobody's directly losing
  1957. 1:09:37money if you screw up. Um I guess if you
  1958. 1:09:39send out something really inappropriate
  1959. 1:09:41in the email, then like you could face
  1960. 1:09:43reputational damage, but often times
  1961. 1:09:45machine learning techniques fit focus on
  1962. 1:09:47maximizing reward. Um and and a big
  1963. 1:09:50thing in finance is you need to talk
  1964. 1:09:52about minimizing risk as well. So, um
  1965. 1:09:55we'll just talk a lot about kind of like
  1966. 1:09:57if you're approaching finance from a
  1967. 1:09:59machine learning perspective, what are
  1968. 1:10:00some concerns, what are some good
  1969. 1:10:02applications, what are some bad
  1970. 1:10:03applications, um and uh you know, how to
  1971. 1:10:07approach that. Excellent. And you're
  1972. 1:10:09bringing in Is it Thomas for that one?
  1973. 1:10:11Uh depends. I think Thomas may still be
  1974. 1:10:13on vacation at this point. Uh I think
  1975. 1:10:16I'm going to try to bring in um Max,
  1976. 1:10:18who's the guy who runs the lectures here
  1977. 1:10:20for us. And and his background is in um
  1978. 1:10:23stats and and and all this good stuff.
  1979. 1:10:26He's actually worked on
  1980. 1:10:28uh startups before that were trading
  1981. 1:10:29cryptocurrencies, cryptocurrency
  1982. 1:10:31options, I think. So,
  1983. 1:10:33um he has some really good experience in
  1984. 1:10:36in this kind of stuff and um
  1985. 1:10:38is is is right up there with Thomas when
  1986. 1:10:40it comes to uh evaluating these kind of
  1987. 1:10:43uh applications of machine learning.
  1988. 1:10:45That's awesome. Well, I'm keen to hear a
  1989. 1:10:46bit more about his backstory, too. That
  1990. 1:10:48sounds uh pretty interesting.
  1991. 1:10:50Yes, we finally convinced him to work
  1992. 1:10:52here.
  1993. 1:10:53All right, guys. Well, thank you very
  1994. 1:10:55much for doing this again. And Scott,
  1995. 1:10:56awesome to have you on and get your
  1996. 1:10:57input on these topics. So, yeah,
  1997. 1:10:59appreciate it, man. Thank you. Thanks
  1998. 1:11:01for having me on.

About this transcript

This page contains the full transcript of Q4: Portfolio Optimization – Risk Preferences In, Trades Out · Scott Sanderson by Chat With Traders, generated from the public captions YouTube serves with the video. The transcript has 13,047 words across 1,998 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.