[Music] uh thank you Adam so this talk is sort of like how do we do anomaly detection for Real World Systems this is sort of based on my experience of different algorithms I tried over the past year while working on a project called defend at stealth bits and so let's get into it so let's talk about anomaly detection first thing first what is an anomaly well that's sort of a million-dollar question because it's like sort of hard to Define like what makes something anomalous it's more of like you know when you see it it's like oh that thing's different I don't know why it's just different and you really can't come up with like a good like rigid ma formal definition of anomali it's just it's like something that's noticeably different from other things and one thing to keep in mind is that what may seem anomalous or different now may seem normal like the future uh for example a couple months ago um when Pokémon go first came out you know it's a huge increase in like foot traffic through parks that might seem anomalous at first but then it sort of became normal as like Pokemon go and like every day more and more people came to these Parks because I hear like the rare Pokemon there so you need to keep things like that in mind they're like oh this is not anomalies this is just like a new trend and so you need to be able to distinguish between those two and also one other thing like depending on the setting you do it you also have to keep in mind that it might not be in a data neutral setting so if you're like me you work in computer security malicious attackers are sort of trying not to get caught they sort of design their behavior so that they don't trigger your systems this is come different from like regular machine learning where it's like you maybe trying to classify the iris flowers like those flow those flowers are not trying to trick you it's like this flower is not trying to pretend to be this other flower and that's a huge thing you care about when you're doing anomal detection is like is your data sort of in a neutral setting or are you caring about like an adversary who's trying to trick you who's trying to get past you so I'm going sort of talk about um different ways to do this so there are two like I guess General ideas how we can do this one is so we can develop a statistical model of like what's normal so we can say like okay normal behavior follows some multivar Gan distribution and then we look at our data and look at people's behavior and say oh hey according to this model this guy only has like a one in million chance of doing this but he did it that's really rare that's really suspicious let's flag him or we can sort of do the machine learning approach and sort of like implicitly build let's like probability model so we can use like decision trees or we can use support Vector machines in which we sort of take data and then we classify users as sort of like uh normal or anomalous and so this has this big issue if we use the normal machine learning approach that anomaly should be very rare in your data like they're anomalies because they don't occur often so like you have this huge imbalance in your data set and you sort of need to address that when you do machine learning models and so over the like past let's say decade we've sort of started developing special algorithm specifically designed to deal with anomalies so these are special like probability models that sort of Tak in the fact that these rare Advan occur or machine learning models that do take in the fact that oh hey this going to be this huge imbalance in our normal examples and our anomalous examples so from this point on I'm going to talk about like anomaly detection in three different settings using a couple various algorithms so it's going to be something similar to like the last talk except a bit less mathematical and so the first problem say we have a data stream so we have data coming in real time and we want to be able to figure out like anomalies as soon as the data comes in so this is sort of like the holy gra of anomaly detection if you can like do this in real time you can sell that software to anyone it's like oh hey I can tell you attack is happening as soon it's happening boom just use our software like that's the Holy Grail of like computer security and so the idea is simple like we have the data cominging in in stream we can see the events happening but we need to be able to react quickly so we can't do anything too complicated because we have to give like a decision in like a couple seconds so we can only look at like maybe the last 100 500 events and we can't really do much uh computations we have to do something quick we have to do something sort of in memory and then we can act on rapidly so how are we going to do this well let's go back to our statistics 101 class we might have learned something called the zc scores right you might have learned you take the zcore you get a P value and if that P value is less than . 5 you do your happy dance because then you can reject the null of hypothesis but let's talk about what the zcore really means it's sort of like this step statistics that sort of measures how extreme of observation is like if the observation is Extreme enough then the Z Scout the the Z score is very high in absolute value and we reject our not hypothesis so this zcore sort this measure of extremeness and it has like two main parts it has the mean and the standard deviation and it's sort of like measure of like how many standard deviations away from the mean you are you're like three standard deviations or you're five standard deviations and if you are three standard deviations of five that's very extreme and we like reject the hypothesis for that and so we're going to use this idea of like a mean and standard deviation but we're going to modify it a bit because it turns out mean and standard deviation aren't really good at handling anomalies and so first things first like uh let's talk about how we do this in like data Stream So first we have a moving average and a moving standard deviation we look at the last end points we keep track of the mean of the last end points and we keep track the standard deviation in the last n points and then we can just calculate those zc scores on the Fly for each of those points we can say oh hey here's some point take out the moving mean divided by the moving stand ration boom we have a zcore for that and with the flag if the zcore sees some threshold if it's like above three or if it's above four we flag it but as was saying like the Medan standard have some problems they're very sensitive to extreme values and the thing is both these are very sensitive like one extreme value if you have a data set of like a thousand points and then like one value that's like super high compared to everything else it will greatly skew the standard deviation and will greatly skew the mean and as a result the value of these zc scores really decrease the effectiveness of these Z scores it decreases effect like a lot when you have extreme values so how do we deal with that and so just before I go into how we deal with that let's talk about like why this mean and standard deviation is so sensitive to these extreme values well what is the mean well it turns out you might have learned means okay you take the the data points you divide by the number there are boom that's the number but what actual optimization is that doing well it turns out that the mean is actually the number that solves this optimization at top you're basically trying to do this optim if you've taken like a functional analysis course this is the optimization the L2 Norm essentially what happens is you have you trying to pick some number s such that when you take the S uh each of your observations minus s square that sum that up and take the uh the square root of that you find the s that sort of minimizes that so it's like basically you have a vector of points you have your vector s which is just like a vector filled with sssss and you're trying to minimize the distance between the two and this is a big issue because that squared term so if this is small like this is one this is an issue but if we have like x i- s being 10,000 10,000 squared is a big number it's like 1 time 10 to 10 okay so that's going to like greatly impact that sum so that's result those extreme values are going to greatly those extreme deviations are going to greatly imp impact the sum impact the sum and thereby impact the mean we don't want that well what can we do about it well we can use the median and it turns out the median is actually this optimization in the L1 space um so basically what you do when you take the median is you're trying to optimize find well that should be an M instead of an S my bad um so basically what you're trying to do is you have your list of numbers and trying to find M such you minimize the absolute um distance so like take the absolute value of the distance and you sum that up and find the M that minimizes that and so when you do this it turns out that the median is actually more robust against the extreme values you have that deviation of 10,000 it only contributes 10,000 10,000 to the sum doesn't contribute uh would be the 10 billion to the sum and so as a result like median and this optimization in the L1 Norm is more robust to extreme values and this is an idea we're going to come back I'm going to talk about later on in this slide but just remember that that L2 optimization is sensitive to outliers L1 is uh robust and I guess General thing for people who are like interested in mathematics the higher LP spaces you go the more sensive you are to extreme values so if you do like an L3 optimization or an L5 or even lity you're highly sensitive to your outliers and just to sort of visualize this so basically how I did is I created a data set of like 100 points of just like a value 100 and I threw in one extreme value and so the extremeness of the values on the bottom on the x axis and then I show how the mean and standard deviation are affected by it and if you notice you sort of see this like exponential shape it should be more quadratic but as you see like just by extreme increasing one value just fixing the data set and only changing one value uh it can greatly affect the mean and greatly affects the standard deviation so this is where we talked about the median so let's talk about how can we the median is sort of like analogous to our mean it's sort of like a measure of central C centrality so now what's the analogous of the central uh standard deviation well that's called the median absolute deviation and it's given by this formula essentially the median absolute deviation is sort of like a way to think about the spread of data standard deviation is one way to think about spread but it's sensitive to our extremeness so let's talk about the median absolute deviation it's basically you take each number um each of your observations subtract the median and take the median of that and so the standard deviation you might remember is sort of like the mean of the deviation of from the means and so the median absolute deviation is sort of like the median from the deviation from the median and sort of gives you this way there a measure of spread from of the data that sort of doesn't care about those extreme values and one way to visualize this is say we have the data set the Blue Points represent our data this red arrow pointing up represents the mean the bottom uh the green arrow pointing down represents the median and the left and right arrows represent the measure of spread so for the red left red arrow and the right right the right red arrow we have two standard deviations and so we have this one point out here this one blue point extreme value and it's greatly throwing off the standard deviations like most of the data is within two standard deviations just because that blue point is right out there on the other hand we have the median and we have two median absolute deviations above and below the median and you can see that like it doesn't really care about this like extreme value out here it's much smaller it's much more robust and captures more of the data and serve like better for this extreme value so now we have this median and we have this median absolute deviation let's go back to that zcore idea we can now talk about modified zc scores so instead of using zcore which is like x minus mean over standard deviations we can now do um to calculate a modified zcore you basically do x minus the median over the modified absolute deviation a median absolute deviation and then you throw in this uh multiply by this constant 6745 that's just so like um it works out nice ma mathematically when you integrate things it's just like a constant of just for the integration uh and basically this allows you to use this modified zcore and so now we can like have this better measure of extremeness to uh for our data that isn't thrown off by our outliers because like as the outliers come in they sort of increase the they would increase the mean and standard deviation and thus future points would be L suspicious or considered less extreme whereas with the modified zores because we're using median and median absolute deviation which are robust to the extremeness they sort of like stay constant Behavior as they look uh through the new points and then we can sort of use these modified Z scores in the same way we' use these uh normal Z scores so if we can say like if the modified zcore is above three or is above 3. 5 then we flag it as being highly suspicious now let's talk about density based anomaly detection so this is basically your points in space so now we have uh so now we don't have to do this in real time it's not like data streams where we need to do like real time maybe quick and dirty we sort of now have a point space we can now do like much more complicated calculations and computations and so we have our points in some end dimensional space and our goal is to find which point is sort of like noticeably different from the others and so if you look at this plot you can kind of see like which one is noticeably different this this guy way up here that's like way far away from all the other points and the goal of like density based anomal detections is we want to be able to figure out this person we want to be able to flag him as being different from everyone else and so just a quick primer about like we talk about density based methods in like machine learning like DB scan things like what do we mean by density well we're talking about probability density so in statistics you assume that like the datas are generated by um probability density functions if you take a stat course it's like the PDF of a distribution a normal distribution has that PDF of like one over 2 pi blah blah blah blah uh for example and so the goal of density based methods is sort of like try to estimate the underlying probability density function generating the data and then based on using that we can sort of use the estimated density function to sort of classify things so for like DB scan or level set tree clustering we look at the density function and we can classify points to get cluster points together based on like do they come from the same um areas of density and so this algorithm I'm talking about is called local ey factor it's a pretty famous like anomaly detection algorithm sort of like the standard one for this type of problem and there's been many like improvements to it over the past couple years but the goal is to sort of like quantify the relative density about a point and so the idea is that like if you're anomaly you should be like Way oscillated by yourself and so the relative density about you should be very small you're coming like a very sparse region by yourself you're not like in the middle of all the points with all your friends around here like these have very high density regions this is a low density region because it's very sparse and the way we're going to do this is by sort of looking at a small neighborhood around each point and saying like Okay based on the size of this neighborhood we can sort of classify the relative it of each point and so what we're going to do this is for each data point we're going to calculate the K distance so this K distance is the near distance to the K nearest neighbor so if you've heard of like K andn algorithm K nearest neighbor you basically calculate the distance to like the K nearest point and so essentially what we're going to do and theistic behind this is that like the K distance is going to give us some notion of volume like think back to like your Elementary School uh science course you learn density is equal to mass over volume well what's our mass in this case the mass is the number of points contained in our Circle the volume is sort of like the size of the circle and so the goal is the idea behind this is that like the farther away you are from everyone else the higher like volume you should have so compared to this point like this outlier this is this K uh distance it's huge because it has to look very far away to find Five Points close to it on the other hand this guy who's in the middle of everyone doesn't have to look that far to find five other points he's likeing center of attention he can find five friends pretty easily and now so we have the uh K distance we can now talk about the reachability distance so the reachability distance is sort of like a pseudo distance metric and it sort of gives us a way to think about like how do your neighbors think about you so the C reachability distance is sort of like the max between the K distance of B and the actual distance between a and b and so the thing is if like like um if you're one of your neighbor if you so basically the idea is that like how do your neighbor see you so if this guy so the outlier his neighbors don't really see them as like one of his friends so like you may be so this red Point here and this Green Point here this is one of his nearest neighbors but he's not one of his nearest neighbors so it's like if you're an outlier your closest friends don't see you as a friend yeah that that hit bit too close to home for some of you guys right yeah so essentially if you're a normal point then your closest friends see you as one of their friends if you're an outlier your closest friends don't see you as one of their friends as essentially what that reachability distance sort of like intuitively captures and that's how we're going to use it for actually calculating Destiny so now we can actually calculate like I said the goal is to like compute the relative density this is where we're going to actually do this now so with the local reachability density we're going to sort of compute the average reachability D distance from each of his points of each of his neighbors and so we're going to use this formula and essentially what happens is that if your reachability distance to your neighbors is very low so if you like you're an outlier then the reachability distance to your neighbors should be the distance between you and them like it shouldn't be the K distance so this is going to be like a very large summation because you're going to have to like look very far to get to your five nearest neighbors and so this denominator becomes High thus the overall thing becomes a very small value so if you're an outlier this will be very small on the other hand if you're a normal point the average reachability to your neighbors should be about the same it should be about your K distance therefore this is sort of like a more constant term it should be some larger value and then we can cul the local outlier factors where we take the average local reachability dens of your neighbors and divided by the average local your local reachability distance uh local reachability density and so what this will do is sort of compare as like the density of your neighbors compared to density of you and this is important cuz like if you're a normal point the density of your neighbor should be about the same as yours like you're coming from the same space you're in the same R neighborhood so this should be about one if you're a normal point on the other hand if you're this outlier the density of your friends is much higher than the density of yours so like this value will be very big at top so large value divided by small value gives you large value on top so as a local outlier Factor score gets higher the more likely you are an outlier and so what you can do in practice when you use local outli factor is you Sly calculate this local outlier Factor score for each point if a value is three or 2.
5 um that is basically something spooky because that like number basically gives you about like how much more dense your neighbors are compared to you if your local outlier factor for this guy is like 2. 8 so 2. 9 so it's about three so say it's about three and say okay his local outlier Factor score is about three that means his neighbors come from a region that is three times as dense as uh his neighborhood that's really suspicious because that means you're coming from a very different like probably probably density region and like I said normal points should have about score between 1 and 1.