{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Created by Sanskar Hasija**\n\n**📈Exploring Time Series plots: Beginners Guide📈**\n\n**16 NOVEMBER 2021**\n","metadata":{"execution":{"iopub.status.busy":"2021-11-16T06:54:31.05342Z","iopub.execute_input":"2021-11-16T06:54:31.053777Z","iopub.status.idle":"2021-11-16T06:54:31.059578Z","shell.execute_reply.started":"2021-11-16T06:54:31.053737Z","shell.execute_reply":"2021-11-16T06:54:31.058724Z"}}},{"cell_type":"markdown","source":"# <center>📈EXPLORING TIME SERIES PLOTS: BEGINNERS GUIDE📈 </center>\n## <center>If you find this notebook useful, support with an upvote👍</center>","metadata":{}},{"cell_type":"markdown","source":"### This notebook is an introductory notebook for visualizing and understanding different plots in time series data.\n### Various plots covered in this notebook are :\n\n### [1. Trend](#1) ###\n### [2. Seasonality](#2) ##\n### [3. Noise](#3) ###\n### [4. Autocorrelation](#4) ###\n### [5. Break Point](#5) ###\n    ","metadata":{}},{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\nRANDOM_SEED = 12\ntime = np.arange(5*365 + 1 ) # 5 years","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-16T07:57:33.402007Z","iopub.execute_input":"2021-11-16T07:57:33.402365Z","iopub.status.idle":"2021-11-16T07:57:33.408293Z","shell.execute_reply.started":"2021-11-16T07:57:33.402327Z","shell.execute_reply":"2021-11-16T07:57:33.407119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <center>Trend</center>","metadata":{}},{"cell_type":"code","source":"def plot_series(time , series , format = \"-\" , start = 0, end = None , label = None , color = None):\n    plt.plot(time[start:end] , series[start:end] , format , label = label , color = color)\n    plt.xlabel(\"Time\")\n    plt.ylabel(\"Value\")\n    if label:\n        plt.legend(fontsize = 14)\n    plt.grid(True)\n\ndef trend(time , slope = 0 ):\n    return slope * time","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:33.414771Z","iopub.execute_input":"2021-11-16T07:57:33.415468Z","iopub.status.idle":"2021-11-16T07:57:33.422484Z","shell.execute_reply.started":"2021-11-16T07:57:33.415415Z","shell.execute_reply":"2021-11-16T07:57:33.421515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Trend Plot 1 ","metadata":{}},{"cell_type":"code","source":"slope = 0.1\nseries = trend(time , slope)\nplt.figure(figsize = (20,6))\nplot_series(time , series , color = \"purple\")\nplt.title(\" Trend Plot - 1\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-16T07:57:33.425226Z","iopub.execute_input":"2021-11-16T07:57:33.425613Z","iopub.status.idle":"2021-11-16T07:57:33.732877Z","shell.execute_reply.started":"2021-11-16T07:57:33.425566Z","shell.execute_reply":"2021-11-16T07:57:33.731959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Trend Plot 2 ","metadata":{}},{"cell_type":"code","source":"slope = -1\nseries = trend(time , slope)\nplt.figure(figsize = (20,6))\nplot_series(time , series , color = \"purple\")\nplt.title(\" Trend Plot - 2\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:36.467844Z","iopub.execute_input":"2021-11-16T07:57:36.468127Z","iopub.status.idle":"2021-11-16T07:57:36.725376Z","shell.execute_reply.started":"2021-11-16T07:57:36.468094Z","shell.execute_reply":"2021-11-16T07:57:36.724422Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# <center>Seasonality</center>","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:23:31.890208Z","iopub.execute_input":"2021-11-16T07:23:31.891092Z","iopub.status.idle":"2021-11-16T07:23:31.896547Z","shell.execute_reply.started":"2021-11-16T07:23:31.891042Z","shell.execute_reply":"2021-11-16T07:23:31.895417Z"}}},{"cell_type":"code","source":"def seasonal_pattern(season_time):\n    return np.where(season_time<0.45,\n                   np.cos(season_time* 2 *np.pi),\n                   1 / np.exp(3*season_time))\n\ndef seasonality(time , period , amplitude = 1 , phase = 0 ):\n    season_time = ((time + phase) %  period ) / period\n    return amplitude *seasonal_pattern(season_time)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:40.370071Z","iopub.execute_input":"2021-11-16T07:57:40.370597Z","iopub.status.idle":"2021-11-16T07:57:40.377674Z","shell.execute_reply.started":"2021-11-16T07:57:40.370554Z","shell.execute_reply":"2021-11-16T07:57:40.376659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Seasonality Plot 1 ","metadata":{}},{"cell_type":"code","source":"amplitude = 40\nseries = seasonality(time , period = 365, amplitude = amplitude , phase = 0 )\nplt.figure(figsize=(20,6))\nplot_series(time , series, color = \"green\")\nplt.title(\" Seasonality Plot - 1\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:41.210757Z","iopub.execute_input":"2021-11-16T07:57:41.211748Z","iopub.status.idle":"2021-11-16T07:57:41.467537Z","shell.execute_reply.started":"2021-11-16T07:57:41.211692Z","shell.execute_reply":"2021-11-16T07:57:41.466952Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Seasonality Plot 2","metadata":{}},{"cell_type":"code","source":"amplitude = 100\nseries = seasonality(time , period = 90, amplitude = amplitude , phase = 25 )\nplt.figure(figsize=(20,6))\nplot_series(time , series, color = \"green\")\nplt.title(\" Seasonality Plot - 2\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-16T07:57:46.395592Z","iopub.execute_input":"2021-11-16T07:57:46.396385Z","iopub.status.idle":"2021-11-16T07:57:46.675453Z","shell.execute_reply.started":"2021-11-16T07:57:46.396338Z","shell.execute_reply":"2021-11-16T07:57:46.674332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Seasonality + Trend\n### Combined plot for seasonality and trend together","metadata":{}},{"cell_type":"code","source":"baseline = 10\nslope = 0.08\namplitude = 40\nseries = baseline + trend(time , slope) + seasonality(time , period = 365 , amplitude= amplitude)\nplt.figure(figsize=(20,6))\nplot_series(time , series, color = \"green\")\nplt.title(\" Seasonality + Trend Plot \", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:50.585006Z","iopub.execute_input":"2021-11-16T07:57:50.585295Z","iopub.status.idle":"2021-11-16T07:57:50.827939Z","shell.execute_reply.started":"2021-11-16T07:57:50.58525Z","shell.execute_reply":"2021-11-16T07:57:50.827086Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n# <center>Noise</center>","metadata":{}},{"cell_type":"code","source":"def white_noise(time, noise_level = 1 , seed = None):\n    random = np.random.RandomState(seed)\n    return random.random(len(time)) * noise_level","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:57:55.498558Z","iopub.execute_input":"2021-11-16T07:57:55.499111Z","iopub.status.idle":"2021-11-16T07:57:55.505566Z","shell.execute_reply.started":"2021-11-16T07:57:55.499068Z","shell.execute_reply":"2021-11-16T07:57:55.504406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Noise Plot ","metadata":{}},{"cell_type":"code","source":"noise_level = 10\nnoise = white_noise(time , noise_level , seed = RANDOM_SEED)\nplt.figure(figsize=(20,6))\nplot_series(time[:200] , noise[:200], color = \"blue\")\nplt.title(\"  Noise Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:12.059816Z","iopub.execute_input":"2021-11-16T07:58:12.06024Z","iopub.status.idle":"2021-11-16T07:58:12.362535Z","shell.execute_reply.started":"2021-11-16T07:58:12.060197Z","shell.execute_reply":"2021-11-16T07:58:12.361899Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Noise + Seasonality + Trend \n### Combined plot for noise , seasonality and trend together","metadata":{}},{"cell_type":"code","source":"series = baseline + trend(time , slope) + seasonality(time , period = 365 , amplitude= amplitude)\nseries += white_noise(time , noise_level = 10 , seed = RANDOM_SEED)\nplt.figure(figsize=(20,6))\nplot_series(time , series, color = \"blue\")\nplt.title(\"  Noise + Seasonality + Trend Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:18.046498Z","iopub.execute_input":"2021-11-16T07:58:18.046947Z","iopub.status.idle":"2021-11-16T07:58:18.289806Z","shell.execute_reply.started":"2021-11-16T07:58:18.046898Z","shell.execute_reply":"2021-11-16T07:58:18.288611Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n# <center>Autocorrelation</center>","metadata":{}},{"cell_type":"code","source":"def autocorrelation_1(time , amplitude , seed = None):\n    rnd = np.random.RandomState(seed)\n    a1 = 0.5\n    a2 = -0.1\n    rnd_ar = rnd.randn(len(time) + 50)\n    rnd_ar[:50] = 100\n    for step in range(50, len(time) + 50 ):\n        rnd_ar[step] += a1 * rnd_ar[step - 50]\n        rnd_ar[step] += a2 * rnd_ar[step - 33]\n    return rnd_ar[50:] * amplitude\n\ndef autocorrelation_2(time, amplitude, seed=None):\n    rnd = np.random.RandomState(seed)\n    a1 = 0.8\n    ar = rnd.randn(len(time) + 1)\n    for step in range(1, len(time) + 1):\n        ar[step] += a1 * ar[step - 1]\n    return ar[1:] * amplitude","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:23.20271Z","iopub.execute_input":"2021-11-16T07:58:23.203137Z","iopub.status.idle":"2021-11-16T07:58:23.212027Z","shell.execute_reply.started":"2021-11-16T07:58:23.203106Z","shell.execute_reply":"2021-11-16T07:58:23.211167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Autocorrelation Plot 1","metadata":{}},{"cell_type":"code","source":"series = autocorrelation_1(time , amplitude , seed = RANDOM_SEED)\nplt.figure(figsize=(20,6))\nplot_series(time[:200] , series[:200] , color = \"red\" )\nplt.title(\"Autocorrelation Plot - 1 \", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:24.950924Z","iopub.execute_input":"2021-11-16T07:58:24.951292Z","iopub.status.idle":"2021-11-16T07:58:25.227455Z","shell.execute_reply.started":"2021-11-16T07:58:24.951236Z","shell.execute_reply":"2021-11-16T07:58:25.226492Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Autocorrelation Plot 2","metadata":{}},{"cell_type":"code","source":"series = autocorrelation_2(time , amplitude , seed = RANDOM_SEED)\nplt.figure(figsize=(20,6))\nplot_series(time[:200] , series[:200] , color = \"red\")\nplt.title(\"Autocorrelation Plot - 2 \", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:28.767626Z","iopub.execute_input":"2021-11-16T07:58:28.768069Z","iopub.status.idle":"2021-11-16T07:58:29.047589Z","shell.execute_reply.started":"2021-11-16T07:58:28.768029Z","shell.execute_reply":"2021-11-16T07:58:29.046421Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Autocorrelation + Trend \n### Combined plot for Autocorrelation and trend together","metadata":{}},{"cell_type":"code","source":"amplitude = 10\nslope = 2\nseries = autocorrelation_1(time ,amplitude , seed = RANDOM_SEED) + trend(time , slope)\nplt.figure(figsize=(20,6))\nplot_series(time[:200], series[:200] , color = \"red\")\nplt.title(\"Autocorrelation + Trend Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:32.109817Z","iopub.execute_input":"2021-11-16T07:58:32.110528Z","iopub.status.idle":"2021-11-16T07:58:32.566086Z","shell.execute_reply.started":"2021-11-16T07:58:32.11047Z","shell.execute_reply":"2021-11-16T07:58:32.56528Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Autocorrelation + Seasonality  + Trend \n### Combined plot for Autocorrelation, seasonality and trend together","metadata":{}},{"cell_type":"code","source":"amplitude = 10\nslope = 2\nseries = autocorrelation_1(time , amplitude , seed = RANDOM_SEED ) + seasonality(time , period= 50 , amplitude = 150) + trend(time , slope)\nplt.figure(figsize=(20,6))\nplot_series(time[:300], series[:300] , color = \"red\")\nplt.title(\"Autocorrelation + Seasonality + Trend Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:35.483843Z","iopub.execute_input":"2021-11-16T07:58:35.484342Z","iopub.status.idle":"2021-11-16T07:58:35.729381Z","shell.execute_reply.started":"2021-11-16T07:58:35.484292Z","shell.execute_reply":"2021-11-16T07:58:35.728348Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Autocorrelation + Seasonality  + Noise + Trend \n### Combined plot for Autocorrelation, seasonality, noise and trend together","metadata":{}},{"cell_type":"code","source":"amplitude = 10\nslope = 2\nseries = autocorrelation_1(time , amplitude , seed = RANDOM_SEED ) + seasonality(time , period= 50 , amplitude = 150) + trend(time , slope)\nseries += white_noise(time , noise_level=100)\nplt.figure(figsize=(20,6))\nplot_series(time[:300], series[:300] , color = \"red\")\nplt.title(\"Autocorrelation + Seasonality + Noise + Trend Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:40.747523Z","iopub.execute_input":"2021-11-16T07:58:40.747844Z","iopub.status.idle":"2021-11-16T07:58:41.008047Z","shell.execute_reply.started":"2021-11-16T07:58:40.747811Z","shell.execute_reply":"2021-11-16T07:58:41.007014Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <center>Break Point</center>","metadata":{}},{"cell_type":"markdown","source":"## Break Point plot at time = 200\n### Combined plot for Autocorrelation, seasonality, noise ,trend and break point together","metadata":{}},{"cell_type":"code","source":"amplitude1 = 10\namplitude2 = 5\nslope1 = 2\nslope2 = -2\nseries1 = autocorrelation_2(time, amplitude1, seed=RANDOM_SEED) + seasonality(time, period=50, amplitude=150) + trend(time, slope1)\nseries2 = autocorrelation_2(time, amplitude2, seed=RANDOM_SEED) + seasonality(time, period=50, amplitude=2) + trend(time, slope2) + 750+  white_noise(time , 30) \nseries1[200:] = series2[200:]\nseries1 += white_noise(time, noise_level=100 , seed= RANDOM_SEED)\nplt.figure(figsize=(20,6))\nplot_series(time[:300], series1[:300] , color = \"indigo\")\nplt.axvline(x=200 , color = \"black\")\nplt.title(\"Autocorrelation + Seasonality + Noise + Trend + Break Point Plot\", fontdict = {'fontsize' : 20} )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:58:45.957339Z","iopub.execute_input":"2021-11-16T07:58:45.957673Z","iopub.status.idle":"2021-11-16T07:58:46.223797Z","shell.execute_reply.started":"2021-11-16T07:58:45.957642Z","shell.execute_reply":"2021-11-16T07:58:46.222792Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}