"Basic Statistical Arbitrage: Understanding the Math Behind Pairs Trading" by Max Margenot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this lovely quanto peon algorithmic trading Meetup I'm max Margit I'm a data scientist tekmoto peon and I'm here to talk a little bit about statistical arbitrage and some fundamental statistical concepts that go into it right so who here is well does any sort of algorithmic trading at all just a little bit and who here is in tech okay cool that was big I'm sorry Joe algorithmic trading is fun in general but who's familiar with quanto peon in the first place I've got a little spiel about what ideal is and then I'll get into the actual theoretical cool stuff that we're gonna be dealing with so quanto peon leveling Wall Street's playing field our whole shtick essentially democratizing quantitative finance in some way or another the key assumption there being that quantitative finance needs in some way to be democratized so what we mostly do is we host a bunch of free open-source tools so that people can research and develop algorithmic trading strategies and what I mean when I say now we're in the trading strategy is just any sort of instructions that terminates in finite time specifically for the purpose of trading right the most basic trade agree then just be by Apple every single day at 10:00 in the morning let's say now it's not a particularly complex or interesting algorithm but it's definitely a trading algorithm you have some instructions and you're trading based on them cool what makes things actually interesting in where we actually want to bring a computer into the mix is when we're doing more interesting quantitative analysis or any sort of statistics on these time series in order to determine whether whether we want to buy or sell so in this particular case what I want to talk about is statistical arbitrage so who here is familiar with the concept of an arbitrage opportunity cool so an arbitrage opportunity is essentially when you can make a couple trades simultaneously to get away with a risk-free profit in some way or another essentially there's an inefficiency in the it for whatever reason and you trade that in efficiency such that you can make a profit in this particular case what I mean by statistical arbitrage is exploiting an inefficiency that we found due to some sort of statistical analysis right so instead of having the world be some way and have their being and inefficiency that we can take advantage of the idea is that we have some model we have some statistical model of how the world is and how this and how the world should be and we make a trade based on that imbalance so it's more of a forecast that involves than a literal physical imbalance that we can get at so one of the most basic unit strategies of statistical arbitrage is a parent's trait and it's based on some fairly fundamental statistical concepts which are mostly what I'm going to be getting into today so if we're well I'm going to be working with is out of the quant opium lecture series which is my whole deal I run the quant opium lecture series and it's basically a set of free statistics and finance lectures in order to educate people to how the hell they make how they might write a trading algorithm right because as a company our goal is to allocate to the authors who come up with a best algorithms so people use this platform they use their tools they use these tools to research and research if they come up with an algorithm that's particularly good we will license it from them and run that algorithm on institutional capital and give that author a cut of the profit so that's our whole incentive right so we're directly incentivized to make sure that people understand what's going on because algorithmic trading has a bunch of different moving parts in it a lot of people sometimes they have a finance background sometimes they have more of the tech background and there's some sort of gap because it's very interdisciplinary in order to make it go and that's where the lecture series comes in so I'm just going to nip on over to lectures here and I'll find what is it called stationarity marvelous so a Paris trade is based on a concept called stationarity well it's based on a concept called cointegration in order to build up into cointegration and what that means first I'm going to talk about the individual components that go into it namely integration orders of integration and stationary so this title isn't exactly in the correct order for what is covered in it but I hope that you will forgive me in that case so when we deal with a stationary time series what I mean is I have a time series some set of points throughout time length that has the same parameters across time every single sample in this time space is drawn from the same exact probability distribution with the same exact parameters so in the particular case that we're going to be working with I'm theorizing that all of my stationary time series are going to be normally distributed and namely the important part here is that well a normal distribution is parametrized by the mean and standard deviation right so if I have a stationary set of samples from a normal distribution then that means there's no trend in the meat there's no trend in the standard deviation if I were to sample any segments of time across this overall span of time the mean would be the same the standard deviation would be the same and the reason this is important will I hope become player so I'm going to define this basic function here generate data point we're just going to generate a sample from a normal distribution in this particular case I'm just going to generate some samples with a mean 0 standard deviation 1 standard normal distribution nothing special our favorite distribution and I'll generate about a hundred so this is the stationary time series looks like white noise right this is the whole point that we're trying to get it this is what a stationary time series is in general it's white noise now it doesn't necessarily have to be a normal distribution it can be whatever distribution as long as all the samples are from the same distribution with the same exact parameters and I know that I'm saying it looks stationary it looks like white noise but we can never really be certain of characteristic especially a statistical characteristic just by looking at a graph in the first place right graphs are only really good for telling us what something isn't not for definitively saying this is what something is so if we were to deal with a non stationary time series let's just say that we have a trend in the mean here we'll just say that the mean depends on time then we get something like this and as we can see this is a non stationary time series we have a trend here it's a linear trend it's not that bad but when we're dealing with financial data things get significantly worse than this I get some toy example even our pathological examples are friendly so the issue here even beyond dealing with stationary time series is just that if we want simple descriptive statistics at all of a non stationary time series they're entirely meaningless right if I take the mean of this time series over all this mean is entirely non-descriptive of all of time except for maybe one instance about it but if we're at this point in time the mean that we've observed is probably gonna be somewhere down here so it's a moot point in any case the stationary is important partially because of these descriptive statistics partially because of stuff like this but also because whenever we're dealing with any sort of time series model or any sort of statistical model almost always embedded in it is an assumption of stationarity right if we're dealing with an autoregressive model so essentially a linear regression where components depend on previous components in the same time series if we're dealing with a moving average model where components depend on a moving average of previous components of the time series these have an assumption of stationarity baked into them so a lot of times we're going to be using these models when we're not necessarily sure of the statistical assumptions that go into it right and if we make a model without checking the assumptions in the first place and model breaks down or it works we don't really have any reason for knowing one way or the other right we violated the assumption in the first place and we're operating on some real or another is no reason to believe that it's going to continue to work group that it even should have worked in the first place we can't explain any of it so that's generally why we care even beyond the context of Perry's trading about stationarity so how do we actually check for stationery in the first place right I said it's a statistical property it's not enough to just look at a graph so obviously whenever we check for some statistical property there's a hypothesis test or any of 10 different hypothesis tests all to sort of stuff out the same idea we're going to be working with specifically the augmented dickey-fuller test which is just one in the Loess Trias line of stationarity tests there's also like the looks Perrin ordinary dickey-fuller and probably a couple others that have totally slipped my memory but we won't worry about them and as we might be able to expect our test says that that first time series that we generated deliberately to be stationary is stationary and that the pathological example is non stationary and that's fine right this is the test working exactly as we would expect but the thing is whenever we're building models whenever we're handling data in the real world you never know what the data generating process is that gives you that all you can do is sort of step in take a sample and go from there you never know the route process that makes it behave in that particular way in the sample that you're seeing which makes sense right because if you knew the exact relationship of what you're looking at why would you try to estimate a model in the first place you just use the relationship already you can skip all this stuff and not deal with it at all so this is why we need this this is why we care about this the issue is you can fool a hypothesis test a hypothesis test is essentially a good guess you model is it a proper model it it's a probability distribution you see how unlikely the specific event you've seen is and based on how unlikely it is you make a judgement after looking at your cutoff values so your alpha in this particular case what I'm looking at is a also a point zero one so fairly stringent gives me about a 1% chance of a false positive but over a thousand different tests over ten thousand different tests I can expect that fully 1% of those are going to be false positives right especially when we have more subtle sneaky relationships so what I'm going to do here is I'm going to generate another pathological relationship so yes usually spell evidence is the female on y'all under this area are they it's a high focus serious a is definitely stationary because it can you wait a bit permutation is that but here is that the probability is that the probability so these are p-values these when I talk about p-values I typically well what we're doing here is we're doing a definitive test with the p-value with a hypothesis test and when we're doing a definitive test for the hypothesis test we don't want to think of the degree of the p-value of one way or another we have a cut-off in this case it's point zero one we only care whether our resulting p-value is less than or greater than that value and even further than that right the hypothesis tells you whether you can reject or fail to reject a null hypothesis in this particular case the null hypothesis is that the time series is non stationary so in this first case our p-value is essentially zero so it's less than point zero one so we reject the null hypothesis that our time series is non stationary and conclude that it is a likely stationary time series if we didn't already know the generating process for it then that would be about all we get in this second case this p-value is greater than point zero one so we fail to reject the null hypothesis and conclude that it's likely non stationary and that's always so stuff isn't always clear come back yes the personal test will it capture it over the harmonization look so let's take Amina dictionary about the deviation so this the specifics of the augmented dickey-fuller test are to look for essentially a unit root in the time series and if there is a trend in the standard deviation if is eternity that of that sort of thing it will be in some way expressed in the time series and it will most likely be caught I don't remember the specifics of which moments are caught but definitely the standard deviation probably skewness and kurtosis that's more of a sort of I'll go check the specifics of the test later that you can get back to you but I'm fairly certain you'll get most of it so what I'm doing here is I'm adding a little periodic trend in the mean here right I'm going to say that instead of just T or instead of just zero for my data point I'm going to have the sine of whatever our current time is so this is going to oscillate between one negative one that's not going to get too far away from anything right one a negative one not super extreme as far as means go but definitely not stationary and here's us using our L missions to say that oh of course the hypothesis tests will pick that up right with no issues it might I'm never sure whether it's going to pick it up or not but even just looking at the graph right this is a textbook example of why we shouldn't trust graphs when we're looking for statistical properties this looks almost the same as our white noise from up above it's totally plausible that this could be a stationary time series if we did not know that by definition it is not so let's see what our test says in this case we're getting that it is likely stationary and that's just not true we know that right so if we were dealing with this example in the because it's sort of oscillating and it's bounded in some way or another this wouldn't really be too egregious right we're not going to lose too much money if we're making bets based on that time series yes because of specifically how it's generated and it just kind of speaks to the sorts of limitations that we have on everybody whenever we're dealing with modeling whenever we're dealing with tests in the first place right there's always this chance that we have a false positive like in this case or there's always some chance that we're off so we make do with the best of what we have I guess now that we have a sort of basic understanding of stationarity we can build up into kind of the next important concept building up the cointegration nobody was bakery wasn't around the woman it was random it was a generated samples from a normal distribution it's just the mean had a periodic trend in it so he in the literal sense of the word she was environment he had an algorithm as a randomness absolutely the default it was still random there is just a trend in this particular case the her sex met with like 'is likely tell you that it is still mean reverting this is just not an instance of stationary so a stationary time series is generally mean reverting but a mean reverting time series is not always stationary you can have something that reverses simply does not the mutability versus us we find much listener yes but that's still random in some way or another it's samples from the same distribution like this is all samples from the normal distribution there's just a trend in the mean it's still random there's just one component of it is in some way deterministic but even if the mean at some point in time is one or 0.95 whether the sign distribution says at that point in time we're still generating a data point in this particular case what we'd say that is okay well I want a sample from a normal distribution with a mean of 0.9 five and a standard deviation of one so I'm still getting a random variable I just have trend yeah this notion of orders of integration right whenever I have a stationary time series I can represent it as a stationary component while a deterministic component and a random component and its entirety namely I say hey YT is my stationary time series I have this infinite series of random extra information that's incorporated in and I have this stationary I have this deterministic part II tattoo when I mean when I say deterministic is at any point in time T I know what the value of that function is now we don't have to worry too much about that series not converging because as part of this representation we hold that the the squared sum of these coefficients here which are either moving average or other aggressive components depending on how you define the model but the squared sum of those is finite which makes it so that the series ultimately converges so we're not going to Peter off into infinity or negative and today at any prompt - and the reason we really clear about this is this helps the ground kind of a definition of something that's integrated of order zero which I'm not going to get too technical about but integration of order zero is kind of what we care about and stationarity implies this notion of integration of order zero well integration of order zero does not imply stationary so it's since testing for integration of order zero itself is a little annoying it's usually more convenient to just check whether something stationary in the first place because then you know for sure it's integrated for the zero and the reason we care about that there's because well let's just have a look at it right so this is our integrated of order zero time series from up above the very first time series that we generated it's stationary for so integrative or zero no issues if we want something that's integrated of order one all we have to do is cumulatively some a time series that is integrated of order zero so kind of like a poor-man's integration this is where the notion of orders of integration comes from right because we're just going to cumulatively something cumulative some accumulatively some as many times as we need and it has a similar effect to integration as well where kind of smooths out the sense from before right this looks a lot more like a price series it doesn't look like that white noise all jagged all over the place is by no means smooth but it's smoother than static similar if we cumulative Li sum that we get an integrative order to time series which is significantly smoother we can keep doing this n times until we get something that is integrated of order N and really we care about something that's integrated of order n because what we can break it down to get something that's integrated border:0 again in the first place right because it's hard to find something that's stationary in the wild it's hard to find these specific sorts of things without assuming that everything goes along with our assumptions and never actually testing for anything because a key assumption in finance is that returns are normally distributed in other words that returns are stationary we all say mean they all descend standard deviations we're not going to worry about it and this is used in a lot of models that we deal with but what we want to do is want to make sure that we actually test it and go for it from there so in order to break down from these sort of the orders of integration there's some notation that we're going to briefly introduce them not really worry about but essentially we're just doing the opposite of accumulative sum instead of a cumulative sum we're just going to do this first order difference here which we introduced with the backshift operator L so if I have some elements of a time series XT if I apply L to XT then I get the element XT minus 1 I'm just stepping back in time just back shift right so 1 minus L times XC gives us XT minus XT minus 1 which is this Delta X and if we want to break that down d times then we'll apply the 1 minus L to the back show to D 2 X 2 so we'll just do this B times to break down so if we have some time through this integrated of order n we'll just take n first order differences in practice what we're probably going to do is take a first order difference test for stationarity and go from there so I I briefly set up above that that integrated of order one time series looks like a price series and that's kind of the heart of what I'm trying to get at what have we seen whenever we see price series in the wild theoretically they're integrated of order one because they're essentially a step up from returns which are stationary and therefore integrated of order zero right if we cumulatively some the returns of some stock or some financial asset well then we get its price if we take every single return the stock has ever had from whatever it's starting prices we'll know what it's price is at this point in time the loss of the Borgias return depends on your model and depends on the type of returns you're talking about so can we talk about additive returns or percentage returns you can do both our decision uh in the upcoming example yes in the you positions of London prostitution depends on the random process there are some random processes that are not stationary right because a geometric Brownian motion or something I got to be real for prices do you have actors zombies can be modeled with additive I mean yes just because well if you observe stuff in the wild you can just take the difference of today's price and yesterday's price and you can add that to yesterday's price and you get today's price you look you're getting bigger and bigger in which case then those returns wouldn't be stationary yeah but if those returns at those additive returns are getting bigger and bigger than the percentage returns would also be getting bigger and bigger they could they definitely could be so yes returns can be non stationary and that's most of what we're going to actually observe in the wild right it's hard to find a stationary time series is kind of what I'm getting at just because well it's a nice theoretical assumption of provided that a security follows this precise geometric Brownian motion or whatever model that we think we'll dictate it and that's not always the case so what Apple some Microsoft prices so because this is an example in something on stationerity and whatnot this is an example that's posted on the internet you can infer that it's going to work in this particular case right I'm pulling pricing data over one particular year that year being 2014 for one particular company so bear with me in that we don't know the conclusion will pull the expression data will check for stationarity it's a price series right it's not going to be stationary it goes up into the right which in most cases would be good in the case of checking for stationarity not so much because it means you have a trend let's take these additive returns in this particular case we have that this additive series is likely stationary and it looks like white noise we'll also check the percentage returns right here and those are also likely stationary so we're not going to stress about it overall in both instances we have some stationarity in the returns over this particular year so we'll roll from there now whenever we have any sort of integrative order one time series which theoretically prices should be we can do this sort of setup and that's where cointegration comes in in the first place so this again is over one particular year there's no guarantee that Microsoft returns are going to continue to be stationary and in a year afterwards possibly in years before we can go and check that since we have past data but none of us can actually see the future so we won't stress about the future so the definition of cointegration specifically and why we care about it is if you have some set of integrated of order one time series if you can create a linear combination of those in degrees of order one time series such that the linear combination is stationary that set is cointegrated so basically what I'm saying is I have a bunch of price series if I can add them and subtract them and multiply by some constant value and pop them all together I can get something stationary at at the end of it I'm explaining the non stationarity of some time series with the non stationarity of some other time series this is a very difficult relationship to kind of explain it's a very difficult relationship to find right it's hard enough to find one time series of stationary run one return time series is stationary unless you take a particular year so it's going to be significantly harder to find some set or in our specific case a pair of Integrative order one time series that is stationary together right because that's a weird subtle sort of relationship and unless there's a reasonable reason for why the non stationarity of one why the randomness inherent in one time series would explain the randomness of another time series well then we have no reason to believe that it's going to continue to be so right so we want to start with some reasonable economic basis we want some reason for why we would explore a particular pair or explore a particular set a price series maybe this is based on a joint relationship on some element there in the same sector one company is eyeing a competitor of the other for a merger whatever as long as there's a reasonable basis for us to be testing and trying to find this relationship we can kind of narrow the search space in hopes that we can actually find a legitimate cointegrated relationship so in our particular case I said we're going to be dealing with just a pair of things so all that we want is if we can take one price series and some other price series and buy one or two or however so many of one or the other and buy some and short the other and this should create a stationary spread and let's say this is an example where we have y equals 2x right or y equals some alpha plus 2x where we can buy one instance of something and short two instances of something else what we're essentially doing is we're take a hedge right we buy one univ why we short to units of X we're hedging the nonsense of y with the nonsense of X and getting a stationary time series out of it and whenever we're doing any sort of algorithmic trading everything kind of comes down to making a bet and in the case where we have a stationary time series well then that means we can calculate the mean in the standard deviation provided that it's normal in this particular case right we'll just roll with a normal distribution if we have the mean and standard deviation of that normal distribution then based on what the current value of that time suit of that spread is we can make a bet as to whether it's going to revert or not to the mean now in this particular case we don't have like a time model of one something old revert we just know well we just have a notion of whether something is stationary or not so whether something is drawn from the same probability distribution or not so we just be making bets and ideally include in our logic some notion of whether we revert during that there are there other sorts of metrics that you can use like the Hurst exponent or like if we were to fit like an orange sign in one back process or some specific model that models mean reversion that would give us a sort of time notion of when something is going to revert in this particular case this has not built in so let's make some simulated data before we come up with another example from the stock market over a particular set of years that has cointegration so I'll generate x1 and x2 and I'll say hey I have x1 it's some samples from around normal and x2 is that plus a little bit of noise can you suggest that what frequency do you use when you trade cooling degree it depends on the relationship sometimes a daily basis makes sense it depends on over what time range a time series is stationary right if something is only stationary over years and years and years and years then it would probably be unreasonable to look at it over minutes that's more of a judgment sort of thing it's something to be not too sharing one time scale issue yep this is something called like asymptotic stationarity the more samples that you have you approach stationarity you see this with a lot of annoying time series models yeah so we have x1 we generate something that's integrative or zero something stationary or cumulative Lee summit to get our price series and then we'll add some noise and that's going to be x2 and that's right here in this particular case obviously they're going to move together right just because well ones the other plus some noise right in this case if we want to calculate this spread this this is Z is this x2 guy just to kind of prove to us that the other is also integrated of order 1 the additive differences of x2 are stationary so they're both integrated of order 1 and we'll say hey we have this new time series that is spread between x2 minus x1 so this is our linear combination right all right by one unit of x2 and I short one unit of x1 sweet simple and we get stationarity and it looks like white noise it's great if we're actually dealing with pricing data you're never going to have something that's nice you're never going to have one instance of one minus one instance of the other that is way too sweet and elegant to actually happen and financial data is really just disgusting so there's no reason we should ever expect that in the first place right so we need some way in order to actually estimate this I may have given that away the base but the basic way that we're going to deal with this is with just with a linear regression right I'm going to say that I can express the returns of one with the returns of the other with some difference in between them hmm this makes sense this is the same way that you would compute any sort of simple naive hedge right if you're dealing with any sort of exposure to the market the team takes this exact same form I have the returns of my portfolio and they're equal to some intercept term plus beta time the returns of the market and this beta would tell me how much of my returns how much of my strategy is made up of the returns of the market with some extra noise if I have a high beta value in this particular case where I have a portfolio and the market then that means that my portfolio is very exposed to the market it's probably not particularly novel or interesting what I want in that case is to have a high alpha value a high unexplained return relative to the market itself in this case I'm not super interested in that in this case I just want to know what beta is in the first place right because what I'm saying is okay well x2 is some extra stuff some deterministic thing plus some noise and also this other stock ideally in this pair so if I found some cointegrated pair I want to know what that beta is so that I can know what's along and what's a short this is where I'm going to get that 1 X 2 minus 1 X 1 I'm going to instead buy 1 X 2 and short beta X 1 that's my goal here so in this case I have a pair of securities over a particular year and I have their prices in this particular instance this is a pair of alternative-energy securities and if you've gone to any of our other lectures this is a pair that we discover in the pair's tracking lecture which is a pretty neat one but the basic hypothesis here is that okay well it's two securities in the same sector they're going to have some relationship to each other right it may not be a strong relationship but it provides a good basis to jump off for to try to find a co integrative relationship to see if there's actually something meaningful out of that so I'm gonna take my pricing data for a B G B and F SLR and I'll plot them right here and again it doesn't look nice right if I was just looking at prices looking at price series not that I should ever really be looking at price series about statistics firmly in hand I would have no idea or I would have no notion of any sort of relationship between them right especially with this big ugly gap here that's a little sketchy but I'm not even sure what's going on here right there just kind of going all over the place it's just two prices I pulled them at random essentially so if I compose this linear regression here I see that okay well I have a 1.5 beta here with a B GB so if I'm going to compose this linear regression what I'm going to do is I'm going to buy one instance of F SLR and I'm going to short one instance of a B GB and I might have gotten those two backwards but so without loss of generality sort of thing back so plot this spread here we test it we get to its stationery according to our hypothesis test which is nice we've got a weird ugly jump here but the hypothesis says says that it's stationery is hard enough to find a reasonable signal in the first place this is all part of like my research step right I found some potential pair my test says I can get stationery out of it share further down the pipeline I make all this and get rid of it right it's totally possible right because this is just again over one particular instance of time over 2014 to 2015 it is not necessarily so that this is going to remain stationary this particular spread especially with 1.5 units right that's one particular spread calculated over one particular set of time so another sort of thing that might give us pause right is well this is over 2014 2015 I built my linear regression over 2014 to 2015 so this beta value is based on all of this time it's not based on what I would have known at this point in time or what I would have known at this point in time right that beta is going to be different so if I actually wanted to sort of trade this or deal with it I would need some sort of constantly updating value so that I have a meaningful notion of the relationship here so it's not just x2 minus 1.5 x1 because I don't know about that it's probably going to be significantly higher here or it's going to be something else so that's going to change you over time this is an instance of well look ahead bias right in this particular case we're not going to stress about I'm just kind of calling it out so that nobody else calls me out about it later so we have this stationarity between these guys right that's great fun fantastic again it's only a forecast right I don't have any sort of notion of time skill over when it's going to revert and this is just one pair again if I'm dealing with multiple pairs well if I'm dealing with pairs strategies in general or what I want to be doing is making as many different independent bets as possible this is one bet on one spread one constructed asset that's a terrible idea if I want to actually be trading pairs I want to be trading as many as I possibly can I want to be trading 100 200 300 pairs as many different independent bets as I could make right if I told you that you had a 51% edge in blackjack and here's $10,000 to go play with you're not going to place that or at least you shouldn't place that on one single table you're going to take the smallest unit bet that you can make and you're going to bet it that many individual tables because well the more bets you make especially if they're independent like 51% chance independent every single time that's how you're going to get that 1% edge not from placing it all on black we're just so going to be 50/50 sort of setup it don't worry about it I'm mixing my metaphors it's fine we won't stress about it but in the same sort of case right we have some notion of how this spread is constructed we have a stationary probability distribution normal I can make bets based on how far away from the mean I am and I want to make as many different bets of those as possible yes just double-check so that the last moment did that was on hi students not returns or I was in Pisces it should be on returns it should be a bit understood I've been on the reversion to did return no prices directly yeah this right and so so then because my next thought was with cap n right here yes you said it yourself and know which I can see do it return yes so what what was the rationale the rationale is that this is a basic and in many cases flawed example this should be on returns if I was going to actually go and diagnose that regression that were probably the first thing that I look at and then change an update in the lecture which is on my list god yes so the kind of classical way that we handle calculating a pair doing stuff with that is actually on the log returns so you do this sort of exact same layout as I've done where I have some pair I take the returns I take the log of those returns and I calculate the linear regression between the log of those returns it's called the angle Granger method in case any of you guys want to look it up similarly the cointegration tests that I linked down at the bottom is also based on this same exact sort of notion I take this linear regression I check where that with a linear regression is stationary or not and go from there and that tells me whether it's kind of grated because what I did was kind of like an annoying setup right I take my prices I do a linear regression and then I check whether that difference there is stationary this already does that for me with some other built-in things in the documentation that I don't remember off the top of my head but that's all there for better reading on that specific method pallet work but this is just a super simple way that you can build a basic quant strategy right it's based on an incredibly simple assumption when we deal with time series based on this idea of stationary we can build up from there because as long as you have some reasonable thing some reasonable model of how the world is then you can make a bet on it right all you need is some modicum of information more than someone else I need is a 1% edge in order to make a relatively reasonable bet as long as you make as many Bette's in that same way as you can so that's just kind of a background what makes a payers trade go a background of some super basic statistical arbitrage and that's really all that I have here right statistical arbitrage it can be as simple or as complex as possible and this particular instance I made a bad linear regression between some canned examples and really didn't do anything else right I incorporate some look-ahead bias and boom example if I wanted to do this in a smarter way right I'd be calculating rolling linear regressions first of all the returns might be calculating rolling wintery regressions between them so that I have a constantly updating notion of what this spread is on top of that well yeah yeah it's all based on that point right you're deciding value yes so let's let's say that I am great and I've prepared adequately we're looking for via the 2015 year this is what my calculated spread is but if one if I want to kind of actively trade and update it this is still built over an entire year right I'd want to roll that window forward day by day and well how would I actually trade on this right I've said a little bit that it's mean reverting so what I essentially mean is that we're going to oscillate back towards this Center so we're going to make bets whenever we're particularly low or particularly high but in that same vein this mean the mean that I know about at this point in time is different from the meme that I know about in this point in time so even beyond this rolling spread that I need the throwing notion of the spread I need a rolling notion of the mean and of the standard deviation because well let's say that I want to make bets when I'm one standard deviation from the mean on either side I want to make bets when I'm particularly low and I'll go along this spread and I want to make that's what I'm particularly high and I'll go short this spread I need a meaningful notion of what that standard deviation is right so those also have to be constantly updating if you're actively going to bring this so I can be done with moving averages that can be done with more sophisticated models with horizontal BEC that can be done with common filters if you like common filters which are the rich man's moving average or I like to call the moving average department Coleman filter and we see how that kind of goes back and forth but there's a million different ways that you can iterate and improve them right and this is why this is such a prevalent strategy in general whenever we're dealing with font stuff it's a unit strategy we can add as much complexity as we want or we can keep it simple and just deal with moving average is nothing particularly special so I don't know stuff really gets about you can be about as creative as you want whenever you're dealing with quant strategies as long as you have a reasonable idea of whether you have more information than someone else right because this stationarity fiscal integration between some pair of stocks if you know about it it's a subtle relationship and sneaky people spend a lot of time researching pairs people spend a lot of time trying to suss this sort of thing out and ideally if that pair still exists but relationships will exist then you can trade it and hopefully not too many other people are trading in other words that gaps can close so you want to be constantly researching constantly coming up with more to fill out your portfolio of 100 pairs 200 pairs 300 pairs and ideally once individual pairs break and go out of cointegration you'll still have this overall portfolio that is nice and cointegrated and wonderful so that's my talk thank you question is there notion of risking a given pair straight and and similarly there is a notion of reducing risk you know core control via systematic way in a portfolio fair trade so like you know the Medellin markets where whole you have some covariance and your of you're basically trying to minimize negative progress you see that's none or their framework for that or just because you have have like Aleutians an idea of how how risky want you to carry it is right yeah so as I just described it a portfolio of pairs is a portfolio of independent bets so kind of going from there based on that I probably assess the covariance in the first place but I would be operating under the assumption that they're independent from each other especially because kind of the nature of how a pair is traded you get at you get rid of the market just because you're hedging one thing against another and they probably have similarly proportional ratios of the market and we get that out of the picture so you're just trading this constructed assets kind of independent from other things and especially if this relationship is just based on the pairs involved which is what it should be then that relationship should be independent from any other pair relationship so I wouldn't be stressing too hard about so you just want as many vets as possible as many best as possible yeah ideally they should be independent they're probably going to be but that's definitely something worth checking you'd probably check the covariance or the l2 risk or whatever risk metrics that tickle your fancy right what whatever you think is a meaningful motion to volatility for a given pair for a given set of pairs that's something to look at when you do the construction but I'm what I'm essentially proposing is I have 200 pairs right I said nothing about how I want to allocate amongst those 200 pairs I could allocate amongst them based on some sort of Markowitz mean variance portfolio I could minimize all to risk instead I could minimize whatever if I felt like minimizing or I could just equal weight and not deal with it it affected how how often do they rebalance and Amin is changing happen impacted how often do these rebalancing it depends on the pair daily is reasonable it's reasonable depending on what timescale of data you're looking at your monkey you get older you have any disorders one yeah yeah it really depends on the individual pair like if something is mean reverting or stationary or for a very long period of time you're going to be making very infrequent bets right but if you can find stationarity over like a monthly scale I'd step down and try to treat it daily similarly if I found stationarity over years and years and years I would try to trade it monthly or yearly instead again this is super rare tempis the data that your company's relaxed we have the social I don't know off the top of my head but let's take a little journey over to the data page because it's all there so I know our fundamental did it is for Morningstar and there's a bunch of other possible data sets that you can trade with they come from various different sources and Daniel do the fish to do some new time they complete the game all of 2008 over gay people the most granular data that we have is minute Li yeah we don't have a tick level or intra minute at all mostly what we're interested is algorithmic trading not necessarily high frequency stuff obvious mini-documentary time true huh if you're a live trading and I'll go if you're dealing or if you're simulating a backtest it's simulate as if it was real time yeah check out this page if you want to see the other stuff that's available who else might be providing data we're at a Boston actually yeah so no I'm just here for the weekend I was in Seattle last week given a couple talks from the workshop and now I'm here giving a talk tomorrow Nessa well I mentioned before that we have a bunch of assumptions that go into AR and ma models similarly we have a bunch of assumptions that go into linear regressions and if you're just doing a linear regression of one price versus another you're probably violating a good chunk of them I would suspect that there's going to be some heteroscedasticity or something like that in that regression specifically which would be like a trend in the variance which blows everything out of the water and makes it not viable I mean we will start to be perhaps inferior like mine or this one that there's an elastic I don't think I can say anything about the actual strategy that we've allocated research for pairs I know that that's a common thing the question was whether you can use a correlation or covariance between pairs to your model relationship and civic collaboration I dislike it just because correlations just kind of a measure of whether things go in the same direction or not more often than not whereas in the particular case of cointegration that says something more meaningful namely stationarity like I find that to be a stronger condition and since I tend to be fairly conservative whenever I'm looking at ideas and trying to decide whether idea is good or not I'm going to the air on I'm going to err on the side of looking at the stronger condition than looking at the weaker condition integral integration is sponsored and Korea's commitment and the reason for that is you want any more diverse applies one multiple of these hairs and typically point Ricky who have physical implemental is life in terms of what they carry any of it so you can actually make do sizes various ways yeah oh yeah that's not a very good point right so if you're doing correlations right how do you deal with the bet sizes if you have like the mean and standard deviation of this hypothesize this spread then you can use that to meaningfully bet and construct your sizes whereas with the correlation I don't really see how that could be done in the same sort of systematic way I know it's probably fun Michael crosses but mathematically you can certainly have a coordinated relationship with as your correlation we are so like mathematically you don't say that running practice yeah the reasonable pairs are going to have both basically because you can have a pathological example of one without the other and then one without the other that's the other one you don't have like in terms of our square and how much mean that cold-cold correlation is you might have correlation but you don't know how much meaningful valid well they're Sunday employees really will get some something or recently we have some pathological examples of both in some of our other lectures namely an example of cointegration without correlation is a square wave and something stationary because that's just silly there's going to be no correlation whatsoever but it's going great cool any other questions I'm going to hang out for a little while so if you don't ask me a question now feel free to ask me a question later all right thank you so much [Applause]
Info
Channel: Quantopian
Views: 113,606
Rating: 4.9161105 out of 5
Keywords: finance, quantitative finance, risk, risk analysis, math, statistics, algorithms, algorithmic trading
Id: g-qvFjvyqcs
Channel Id: undefined
Length: 54min 51sec (3291 seconds)
Published: Tue Jul 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.