{"id":16248,"date":"2021-09-21T17:44:38","date_gmt":"2021-09-21T12:14:38","guid":{"rendered":"http:\/\/ismiletechnologies.com\/?p=16248"},"modified":"2021-10-29T18:22:18","modified_gmt":"2021-10-29T12:52:18","slug":"anomaly-detection-in-a-time-series","status":"publish","type":"post","link":"https:\/\/ismiletechnologies.com\/en_us\/technology\/anomaly-detection-in-a-time-series\/","title":{"rendered":"Anomaly Detection in a Time Series"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Anomaly detection can be applied to a time series where we want to create a baseline model and determine the deviation of the observations with the baseline. From deviation, we can get information such that if it is large enough, the observation is deemed abnormal and is flagged.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;In general, novelty and outlier detection does not tell us why something is possibly an outlier, but the conditions and causes led to an unusual observation. For example, generally, there are cases where we observe server logs; anomalous observations may result from some equipment or code breakdown or something malignant like a security breach.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What should be your plan of attack be for analysing the time series?<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, generally, we visualize the data; after that, our goal is to generate the baseline model, so we can ask ourselves if there is any drift, and if present, we have to remove that from our data set.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The other thing we need to ask ourselves is some periodicity in our dataset. We have to find those and remove them from our model, so the residual is left behind; we can do some other&nbsp;modelling&nbsp;on those residuals. So this is the overall plan of attack.\u202f\u202f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Fourier Analysis<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is hard to tell when there are periodic&nbsp;behaviours&nbsp;in the time series where we can better spot the dominant frequencies that support the time series using Fourier analysis. So here, I have taken a Fourier analysis code to give an idea about the dominant frequencies.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From this graph, we got to see that these time series consist of four dominant frequencies, which are as follows:-&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Daily\u00a0<\/li><li>Twice-daily\u00a0<\/li><li>Three times a day\u00a0<\/li><li>Four times a day.\u00a0<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u202fIf we rephrase it, it would be 6, 8, 12, and 24 hours.\u202f&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"110\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-68.png\" alt=\"\" class=\"wp-image-16253\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-68.png 602w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-68-300x55.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Output Graph:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"558\" height=\"342\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69.png\" alt=\"Anomaly Detection in a time series\" class=\"wp-image-16255\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69.png 558w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69-300x184.png 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">From this graph, we got to see that this time series has four dominant frequencies: daily, twice-daily, three times a day, and four times a day. In other words, 6, 8, 12, and 24 hour periods.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Initial baseline model<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, we create an initial baseline model to get an idea of the data and know whether the initial baseline model is adequate for the time series. Generally, before using the initial baseline model, we create some custom transformers (if needed) to work with our pandas&#8217; time-series data. Most custom transformer is used to generate Fourier components, transform&nbsp;Date Time&nbsp;objects into a unit of time, etc.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So after using the custom transformer and the initial baseline model, the residuals are generated to reveal whether the time series has a lot of shock events, a sudden increase in energy usage probably due to sudden and short use of products. So there&#8217;s a need to&nbsp;analyse&nbsp;the residuals for any temporal correlations.\u202f&nbsp;<\/p>\n\n\n\t\t<div data-elementor-type=\"page\" data-elementor-id=\"15862\" class=\"elementor elementor-15862\" data-elementor-post-type=\"elementor_library\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-94a262c elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"94a262c\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;gradient&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-50 elementor-top-column elementor-element elementor-element-68690f5\" data-id=\"68690f5\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-1edb394 elementor-widget elementor-widget-spacer\" data-id=\"1edb394\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"spacer.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-spacer\">\n\t\t\t<div class=\"elementor-spacer-inner\"><\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-df3f191 elementor-widget elementor-widget-heading\" data-id=\"df3f191\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Download our ebooks<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3a45da8 elementor-widget elementor-widget-text-editor\" data-id=\"3a45da8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Get directly to your inbox<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-78e286f elementor-widget elementor-widget-spacer\" data-id=\"78e286f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"spacer.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-spacer\">\n\t\t\t<div class=\"elementor-spacer-inner\"><\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t<div class=\"elementor-column elementor-col-50 elementor-top-column elementor-element elementor-element-258867b\" data-id=\"258867b\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-81cdaed elementor-align-center elementor-widget elementor-widget-button\" data-id=\"81cdaed\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t\t\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-md\" href=\"http:\/\/ismiletechnologies.com\/ebook\/\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t<span class=\"elementor-button-icon\">\n\t\t\t\t<svg aria-hidden=\"true\" class=\"e-font-icon-svg e-fas-cloud-download-alt\" viewBox=\"0 0 640 512\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><path d=\"M537.6 226.6c4.1-10.7 6.4-22.4 6.4-34.6 0-53-43-96-96-96-19.7 0-38.1 6-53.3 16.2C367 64.2 315.3 32 256 32c-88.4 0-160 71.6-160 160 0 2.7.1 5.4.2 8.1C40.2 219.8 0 273.2 0 336c0 79.5 64.5 144 144 144h368c70.7 0 128-57.3 128-128 0-61.9-44-113.6-102.4-125.4zm-132.9 88.7L299.3 420.7c-6.2 6.2-16.4 6.2-22.6 0L171.3 315.3c-10.1-10.1-2.9-27.3 11.3-27.3H248V176c0-8.8 7.2-16 16-16h48c8.8 0 16 7.2 16 16v112h65.4c14.2 0 21.4 17.2 11.3 27.3z\"><\/path><\/svg>\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Download Now<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-674ec99 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"674ec99\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-0c8c567\" data-id=\"0c8c567\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-e5c6ef1 elementor-widget elementor-widget-spacer\" data-id=\"e5c6ef1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"spacer.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-spacer\">\n\t\t\t<div class=\"elementor-spacer-inner\"><\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Noice Based Features<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first thing we want to unveil is the correlation of past residuals with current values. An autocorrelation plot will inform us whether the time series elements are positively correlated, independent of each other, or negatively correlated. In short, it tells the characteristic time scale of the process to guide us when generating noise-based features.\u202f\u202f\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>z-Score<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since there is little temporal correlation with residual values, we assume that the residuals are independently sampled from the same distribution. Given this probabilistic perspective, we can quantify the degree of an anomaly to each observation if we know the distribution the residuals get sampled from.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the distribution has one peak, there is a lower probability of observing values far from the peak. The z-score is a relative measure of how far away a value is from the mean, normalized by the standard deviation.\u202f&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"558\" height=\"342\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69.png\" alt=\"Anomaly Detection in a time series\" class=\"wp-image-16254\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69.png 558w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-69-300x184.png 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How should we decide the appropriate z-score cutoff?&nbsp;<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we set the z-score large, it will increase the range of standard points or inliers, and if we set the z-score small, it will decrease the range of standard points. So if we view there is no perfect answer for this, we should consider that it all depends on the precision and recall we want in our analysis.\u202f\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Rolling z-score<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The calculation of the z-score relied on the entire time series for calculating the mean and standard deviation. We will usually be streaming observations for anomaly detection with time series, and the entire series will not be available.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, we can calculate the z-score on a window of observations rather than the whole time history. The advantage of rolling z-score is that not hold a large amount of data in memory, and it also reflects the fact that it is better to use current values. However, the observation is uncommon, and the rolling z-score is more adaptive to recent changes in the process.\u202f\u00a0<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><strong>\u202f<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, we have learned about how anomaly detection gets used in a time series. A deep analysis would be a necessity while&nbsp;analysing&nbsp;every aspect of time series. We have learned how Fortier analysis, Z-score, initial baseline model, and noise&nbsp;base features can turn out to be so valuable while planning to&nbsp;analyse&nbsp;a time series. Moreover, the helpful method of finding out the&nbsp;cut-off&nbsp;of Z-score can be applied to the practical methods using anomaly detection time series.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the first free consultation for discussing Anomaly detection gets used in time series,<span class=\"has-inline-color has-vivid-cyan-blue-color\">\u202f<a href=\"http:\/\/ismiletechnologies.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\">click here<\/a><\/span>.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anomaly detection can be applied to a time series where we want to create a baseline model and determine the deviation of the observations with the baseline. From deviation, we can get information such that if it is large enough, the observation is deemed abnormal and is flagged.&nbsp; &nbsp;In general, novelty and outlier detection does [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":16397,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-16248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/comments?post=16248"}],"version-history":[{"count":3,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16248\/revisions"}],"predecessor-version":[{"id":20091,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16248\/revisions\/20091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media\/16397"}],"wp:attachment":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media?parent=16248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/categories?post=16248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/tags?post=16248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}