{"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":"### 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":"2023-01-13T13:23:48.390388Z","iopub.execute_input":"2023-01-13T13:23:48.391373Z","iopub.status.idle":"2023-01-13T13:23:48.414992Z","shell.execute_reply.started":"2023-01-13T13:23:48.391283Z","shell.execute_reply":"2023-01-13T13:23:48.414310Z"},"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":"2023-01-13T13:24:27.095475Z","iopub.execute_input":"2023-01-13T13:24:27.095751Z","iopub.status.idle":"2023-01-13T13:24:27.101820Z","shell.execute_reply.started":"2023-01-13T13:24:27.095718Z","shell.execute_reply":"2023-01-13T13:24:27.100894Z"},"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":"2023-01-13T13:24:40.411287Z","iopub.execute_input":"2023-01-13T13:24:40.411548Z","iopub.status.idle":"2023-01-13T13:24:40.693853Z","shell.execute_reply.started":"2023-01-13T13:24:40.411518Z","shell.execute_reply":"2023-01-13T13:24:40.693210Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:24:48.080715Z","iopub.execute_input":"2023-01-13T13:24:48.081369Z","iopub.status.idle":"2023-01-13T13:24:48.332366Z","shell.execute_reply.started":"2023-01-13T13:24:48.081326Z","shell.execute_reply":"2023-01-13T13:24:48.331688Z"},"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":"2023-01-13T13:24:56.681728Z","iopub.execute_input":"2023-01-13T13:24:56.682297Z","iopub.status.idle":"2023-01-13T13:24:56.687544Z","shell.execute_reply.started":"2023-01-13T13:24:56.682260Z","shell.execute_reply":"2023-01-13T13:24:56.686534Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:25:09.491369Z","iopub.execute_input":"2023-01-13T13:25:09.491644Z","iopub.status.idle":"2023-01-13T13:25:09.751317Z","shell.execute_reply.started":"2023-01-13T13:25:09.491603Z","shell.execute_reply":"2023-01-13T13:25:09.750631Z"},"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":"2023-01-13T13:25:15.542451Z","iopub.execute_input":"2023-01-13T13:25:15.542818Z","iopub.status.idle":"2023-01-13T13:25:15.814900Z","shell.execute_reply.started":"2023-01-13T13:25:15.542772Z","shell.execute_reply":"2023-01-13T13:25:15.814238Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:25:21.780796Z","iopub.execute_input":"2023-01-13T13:25:21.781238Z","iopub.status.idle":"2023-01-13T13:25:22.028371Z","shell.execute_reply.started":"2023-01-13T13:25:21.781199Z","shell.execute_reply":"2023-01-13T13:25:22.027672Z"},"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":"2023-01-13T13:25:28.951356Z","iopub.execute_input":"2023-01-13T13:25:28.951630Z","iopub.status.idle":"2023-01-13T13:25:28.955786Z","shell.execute_reply.started":"2023-01-13T13:25:28.951593Z","shell.execute_reply":"2023-01-13T13:25:28.955115Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:25:32.111753Z","iopub.execute_input":"2023-01-13T13:25:32.112444Z","iopub.status.idle":"2023-01-13T13:25:32.371545Z","shell.execute_reply.started":"2023-01-13T13:25:32.112411Z","shell.execute_reply":"2023-01-13T13:25:32.370895Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:25:47.290433Z","iopub.execute_input":"2023-01-13T13:25:47.291161Z","iopub.status.idle":"2023-01-13T13:25:47.539411Z","shell.execute_reply.started":"2023-01-13T13:25:47.291123Z","shell.execute_reply":"2023-01-13T13:25:47.538676Z"},"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":"2023-01-13T13:25:55.765707Z","iopub.execute_input":"2023-01-13T13:25:55.766041Z","iopub.status.idle":"2023-01-13T13:25:55.774812Z","shell.execute_reply.started":"2023-01-13T13:25:55.766004Z","shell.execute_reply":"2023-01-13T13:25:55.773906Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:00.790502Z","iopub.execute_input":"2023-01-13T13:26:00.791135Z","iopub.status.idle":"2023-01-13T13:26:01.059357Z","shell.execute_reply.started":"2023-01-13T13:26:00.791098Z","shell.execute_reply":"2023-01-13T13:26:01.058661Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:04.072611Z","iopub.execute_input":"2023-01-13T13:26:04.072894Z","iopub.status.idle":"2023-01-13T13:26:04.331418Z","shell.execute_reply.started":"2023-01-13T13:26:04.072841Z","shell.execute_reply":"2023-01-13T13:26:04.330736Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:07.859645Z","iopub.execute_input":"2023-01-13T13:26:07.860119Z","iopub.status.idle":"2023-01-13T13:26:08.310075Z","shell.execute_reply.started":"2023-01-13T13:26:07.860067Z","shell.execute_reply":"2023-01-13T13:26:08.309360Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:11.671770Z","iopub.execute_input":"2023-01-13T13:26:11.672495Z","iopub.status.idle":"2023-01-13T13:26:11.919801Z","shell.execute_reply.started":"2023-01-13T13:26:11.672457Z","shell.execute_reply":"2023-01-13T13:26:11.919116Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:16.150692Z","iopub.execute_input":"2023-01-13T13:26:16.151415Z","iopub.status.idle":"2023-01-13T13:26:16.400475Z","shell.execute_reply.started":"2023-01-13T13:26:16.151376Z","shell.execute_reply":"2023-01-13T13:26:16.399781Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-01-13T13:26:20.860574Z","iopub.execute_input":"2023-01-13T13:26:20.860841Z","iopub.status.idle":"2023-01-13T13:26:21.119390Z","shell.execute_reply.started":"2023-01-13T13:26:20.860810Z","shell.execute_reply":"2023-01-13T13:26:21.118663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}