{"id":12151,"date":"2022-01-04T11:46:00","date_gmt":"2022-01-04T03:46:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=12151"},"modified":"2024-07-03T17:33:16","modified_gmt":"2024-07-03T09:33:16","slug":"arima-garch-modelpart-1","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/","title":{"rendered":"ARIMA-GARCH Model(Part 1)"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1pv2AJGKWmqbFC9NjAWXuCg.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@isaacmsmith\" rel=\"noreferrer noopener\" target=\"_blank\">Isaac Smith<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a10af96e8f51\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a10af96e8f51\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Editing_Environment_and_Modules_Required\" >Editing Environment and Modules Required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Database\" >Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Data_Selection\" >Data Selection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Data_Split\" >Data Split<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#ARIMA_Model\" >ARIMA Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#ARIMA_Diagnosis\" >ARIMA Diagnosis<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#ACFPACF\" >ACF\/PACF<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#GARCH_DiagnosisProgramming_is_available_in_the_Source_Code\" >GARCH Diagnosis(Programming is available in the Source Code)<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Residual_diagnosis\" >Residual diagnosis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#White_Noise_Test\" >White Noise Test<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.tejwin.com\/en\/insight\/arima-garch-modelpart-1\/#Source_Code\" >Source Code<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"09c7\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span>Highlights<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n\n\n\n<li>Introduction to Time Series Model<\/li>\n\n\n\n<li>Reminder\uff1aIn this article, we focus on data pre-processing and model test so as to make readers acquainted with the basic Time Series knowledge. Mathematics theorem of model and test will not be involved. However, it is required that readers know fundamental Statistics like P-value and so forth.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5fc7\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span>Preface<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"9e05\">First of all, we need to declare the Time Series concept. It is a kind of data structure showing the development of historical data by the order of time. As for Time Series Model, it is applied to analyze time series data. Further, by this model, we manage to find high-likelihood trend and make forecasting.<\/p>\n\n\n\n<p id=\"7f08\">Models we will implement are as follow\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"c71a\"><em>Autoregressive Integrated Moving Average model, ARIMA<\/em><\/p>\n<\/blockquote>\n\n\n\n<p id=\"3618\">ARIMA is a fundamental time series model. Its parameters are Autoregression(AR), Differencing and Moving Average(MA).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AR\uff1aIndicates the situation of regression on historical data.<\/li>\n\n\n\n<li>Integrated\uff1aRefers to how many order of differencing that observed data set should take so as to make it stationary.<\/li>\n\n\n\n<li>MA\uff1aRepresents the dependent relation between an observed data and residual from a moving average model derive from historical data.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"ae1e\"><em>General Autoregressive Conditional Heteroskedasticity model, GARCH<\/em><\/p>\n<\/blockquote>\n\n\n\n<p id=\"fcb4\">GARCH is used to analyze time series error. It is especially useful with application to measure volatility in investment domain. We will implement GARCH model to test residual from ARIMA so as to modify the error term. Parameters of GARCH is similar to that of ARIMA, but GARCH mainly focuses on error and variance. As for the numbers of (p,q), which is the amount we take from lagged data to fit GARCH, we will determine by ACF\/PACF with the consideration of GARCH\u2019s characteristics.<\/p>\n\n\n\n<p id=\"e2e5\">Process Outline:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Test whether input data is stationary or not.<\/li>\n\n\n\n<li>Implement ARIMA with stationary data<\/li>\n\n\n\n<li>Implement GARCH with residuals from ARIMA<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"e347\"><span class=\"ez-toc-section\" id=\"Editing_Environment_and_Modules_Required\"><\/span>Editing Environment and Modules Required<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"b8b6\">MacOS &amp; Jupyter Notebook<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u57fa\u672c\u529f\u80fd<br>import numpy as np<br>import pandas as pd# \u7e6a\u5716<br>import matplotlib.pyplot as plt<br>%matplotlib inline<br>import seaborn as sns<br>sns.set()# TEJ API<br>import tejapi<br>tejapi.ApiConfig.api_key = 'Your Key'<br>tejapi.ApiConfig.ignoretz = True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"939d\"><span class=\"ez-toc-section\" id=\"Database\"><\/span>Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=42\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">Security Transaction Data Table<\/a>\uff1aListed securities with unadjusted price and index. Code is \u2018TWN\/EWPRCD\u2019.<\/li>\n\n\n\n<li><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=50\" rel=\"noreferrer noopener\" target=\"_blank\">Return Information Data Table<\/a>\uff1aListed securities with daily return. Code is \u2018TWN\/EWPRCD2\u2019.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5da4\"><span class=\"ez-toc-section\" id=\"Data_Selection\"><\/span>Data Selection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"c54a\"><strong><em>0050.TW<\/em><\/strong>&nbsp;is target index in this article.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data_price = tejapi.get('TWN\/EWPRCD', # \u516c\u53f8\u4ea4\u6613\u8cc7\u6599-\u6536\u76e4\u50f9<br>                  coid= '0050', # \u53f0\u706350<br>                  mdate={'gte': '2003-01-01', 'lte':'2021-12-24'},<br>                  opts={'columns': ['coid', 'mdate', 'close_d', ]},<br>                  chinese_column_name=True,<br>                  paginate=True)<br>data_price = data_price.set_index('\u65e5\u671f')# \u5c0e\u5165\u4e2d\u6587<br>plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']# \u7e6a\u88fd0050\u50f9\u683c\u8d70\u52e2\u5716<br>fig, ax = plt.subplots(figsize = (10, 5))<br>plt.plot(data_price['\u6536\u76e4\u50f9'], label = '0050-price')<br>plt.title('0050\u6536\u76e4\u50f9')<br>plt.legend()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1pGQK2rtApw3LzMpic66MkA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"b2a1\">Based on above chart, it is clear that there exists a trend with time elapse. This situation is caused by that price data is non-stationary. Therefore, we conduct Augmented Dickey-Fuller(ADF) Test to check whether unit root exists in the series or not. If unit root truly exists, we would \u201cdifference\u201d raw data to convert non-stationary to stationary. We will check this by P-value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5f9estatsmodels\u6578\u64da\u5305\u5c0e\u5165ADF\u5957\u4ef6<br>from statsmodels.tsa.stattools import adfuller# \u5f9e\u7522\u51fa\u7684ADF\u5831\u8868\u64f7\u53d6P-value\u9805\u76ee<br>price_p_value = adfuller(data_price['\u6536\u76e4\u50f9'])[1]# \u8a2d\u5b9a\u5224\u5225\u5f0f\u4ee5\u53ca0.05\u7684P-value\u6a19\u6e96<br>if price_p_value &gt; 0.05:<br>    print('Nonstationary')<br>else:<br>    print('Stationary')<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"9eb7\">H0\uff1aData is Non-stationary<\/p>\n\n\n\n<p id=\"4139\">H1\uff1aData is Stationary<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/14-bugSNpRs_N5HR4cmh9Mg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"9514\">According to the result of ADF test, it is assured that price data is non-stationary. Hence, we cannot take price to fit following models.<\/p>\n\n\n\n<p id=\"e6d2\">Reason to declare the importance of being stationary or not is as follow\uff1a<\/p>\n\n\n\n<p id=\"2d30\">If data is non-stationary, its trend would be random walk. As a result, it is just a coincidence that data fits model well. It is non-sense to prove how-well a model performs. After all, model cannot interpret a random walk.<\/p>\n\n\n\n<p id=\"8665\">As know that price is not eligible for fitting model, we import \u201cReturn \u201d(Graph and ADF process is available in the Source Code)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data_ret = tejapi.get('TWN\/EWPRCD2', # \u516c\u53f8\u4ea4\u6613\u8cc7\u6599-\u5831\u916c\u7387<br>                  coid= '0050', # \u53f0\u706350<br>                  mdate={'gte': '2003-01-01', 'lte':'2021-12-24'},<br>                  opts={'col     umns': ['coid', 'mdate', 'roia', ]},<br>                  chinese_column_name=True,<br>                  paginate=True)<br>data_ret = data_ret.set_index('\u65e5\u671f')<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1JVeARtU_CGelJHkx8dxr1g.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">0050\u5831\u916c\u7387<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1so4ABPOBjdAkkpK_Uhck7g.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">ADF\u6aa2\u5b9a<\/figcaption><\/figure>\n\n\n\n<p id=\"cd2a\">According to the result of ADF test, it is assured that return data is stationary. Hence, we can take price to fit following models.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4566\"><span class=\"ez-toc-section\" id=\"Data_Split\"><\/span>Data Split<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">train_date = data.index.get_level_values('\u65e5\u671f') &lt;= '2020-12-31'<br>train_data = data[train_date]<br>test_data = data[~train_date]<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>train_data\uff1aServe as the term to construct model<\/li>\n\n\n\n<li>test_data\uff1a Serve as the term to compare with the post-modelling prediction<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p id=\"3550\">This article would only focus on data pre-processing and modelling, so we would apply test_data only in following article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ab50\"><span class=\"ez-toc-section\" id=\"ARIMA_Model\"><\/span>ARIMA Model<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"cdeb\">We apply&nbsp;<em>pmdarima<\/em>&nbsp;to find best-fitted parameter set.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pmdarima as pmdpmd_mdl = pmd.auto_arima(train_data['\u65e5\u5831\u916c\u7387(%)'], stationary = True)<br>pmd_mdl.summary()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1VuGk0U9Y8QeIjF7cQbf-Ag.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">auto_arima<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1at7V8wY-OpQS0sUTSWU-hA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"4e91\">According to above table, we would know that the best-fitted parameter set is (2, 0, 2). To boot, it is clear that every P-value is smaller than the strictest level, 0.01, so the significance of every coefficient is certain.<\/p>\n\n\n\n<p id=\"13d5\">Note\uff1aIf you notice that the name of model is SARIMAX, instead of ARIMA, it is nothing to worry about. SARIMAX is the default set of pmdarima, but the process remains ARIMA. We import statsmodel package to check this result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from statsmodels.tsa.arima.model import ARIMAmodel = ARIMA(train_data['\u65e5\u5831\u916c\u7387(%)'], order = (2, 0, 2))<br>stats_mdl = model.fit() <br>print(stats_mdl.summary())<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1aaAVzh2jhREJUzkqCUOYrA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u65e5\u5831\u916c\u7387(%)<\/figcaption><\/figure>\n\n\n\n<p id=\"4dd4\">Observing the ARIMA report by statsmodels, we know the difference of fitting level(AIC &amp; BIC) between pmdarima and statsmodels is tiny. As for the coefficient estimation table, the significance level is identical as well, though the coefficient and standard error terms differs a little.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"cf7a\"><span class=\"ez-toc-section\" id=\"ARIMA_Diagnosis\"><\/span>ARIMA Diagnosis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"3d05\"><strong>Residual diagnosis<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">stats_mdl.plot_diagnostics(figsize = (15, 10))<br>plt.show()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1hEq7fcEw4PMiR7gOmTnjzQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">ARIMA\u6a21\u578b\u8a3a\u65b7<\/figcaption><\/figure>\n\n\n\n<p id=\"6cdc\">There is no apparent trend of residual by observing left-top chart. However, the histogram(right-top) and Normal Q-Q plot(left-bottom) make us conclude that the normality of model residual is not sufficient, which indicates that ARIMA\u2019s residual still contains some explanatory variable. We would conduct white noise test to verify it.<\/p>\n\n\n\n<p id=\"b886\"><strong>White Noise Test<\/strong><\/p>\n\n\n\n<p id=\"1321\">By Ljung-Box Test, we would know whether ARIMA\u2019s residual is random or not. If the result shows that residual is white noise(random), we could conclude that ARIMA is fitted well. However, if residual is not white noise, we would further implement GARCH to interpret the explanatory variance inside error term.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from statsmodels.stats.diagnostic import acorr_ljungboxwhite_noise = acorr_ljungbox(pmd_residual, lags = [10], return_df=True)<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><cite>H0\uff1aError term is white noise<br>H1\uff1aError term is not white noise<br><\/cite><\/blockquote>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1TeVmKA11q3drlBndhgqI1A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"80f5\">Test result make us know the error term is not white noise(P-value &lt; 0.05). Therefore, we would conduct GARCH to fit ARIMA\u2019s residual.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"8a69\">GARCH Model<\/h1>\n\n\n\n<p id=\"4b2c\"><strong>ARCH Effect Test<\/strong><\/p>\n\n\n\n<p id=\"1125\">In spite of proof of that ARIMA\u2019s residual is not random, we cannot prove that term exists heteroskedasticity. We would conduct ARCH Effect Test to confirm that.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from statsmodels.stats.diagnostic import het_archLM_pvalue = het_arch(arima_resid, ddof = 4)[1]<br>print('LM-test-Pvalue:', '{:.5f}'.format(LM_pvalue))<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><cite>H0\uff1a ARCH Effect not exists<br>H1\uff1aARCH Effect exists<\/cite><\/blockquote>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1hlTGQJhSBdKJQiOKMXAwQw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><mark><span style=\"background-color:#ffffff\" class=\"has-inline-background\">ARCH Effect Test<\/span><\/mark><\/figcaption><\/figure>\n\n\n\n<p id=\"da44\">By P-value of the test, we would reject H0. Namely, we can apply GARCH to fit ARIMA\u2019s residual.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8d24\"><span class=\"ez-toc-section\" id=\"ACFPACF\"><\/span>ACF\/PACF<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"ded2\">\u900f\u904e\u89c0\u5bdf\u6b98\u5deeACF\/PACF\u5716\uff0c\u4e86\u89e3\u6b98\u5dee\u5e73\u65b9\u9805\u76ee\u7684\u6eef\u5f8c\u60c5\u5f62\uff0c\u4e26\u5c07\u89c0\u5bdf\u5230\u7684\u7d50\u679c\u5e36\u5165GARCH\u6a21\u578b\u4e2d\uff0c\u6c7a\u5b9aGARCH\u6a21\u578b\u7684\u53c3\u6578\u7d44\u5408\uff0c\u6aa2\u5b9a\u6ce2\u52d5\u5ea6\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Autocorrelation Function, ACF\uff1aMeasure correlation between series and its lagged values.<\/li>\n\n\n\n<li>Partial Autocorrelation Function, PACF\uff1aFind correlation of residuals with with the following lagged value.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">fig, ax = plt.subplots(1, 2, figsize = (18,5))pmd_residual = pmd_mdl.arima_res_.resid<br>sgt.plot_acf(pmd_residual**2, zero = False, lags = 40, ax=ax[0])<br>sgt.plot_pacf(pmd_residual**2, zero = False, lags = 40, ax=ax[1])plt.show()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1FDIZ1B5EFL9OdbInHz8lBQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">PACF<\/figcaption><\/figure>\n\n\n\n<p id=\"7ba0\">Above chart makes us know that squared-residual\u2019s autoregression is significant. Synthesizing PACF(right) and GARCH\u2019s mathematics structure, we assume that GARCH\u2019s parameter set (p,q) are (1,1), (2,1), (1,2) and (2,2). After compare model\u2019s goodness of fit and significance of those sets, we determine which set to use. In case of the redundant content, we only display the best-fitted set, (1,1).<\/p>\n\n\n\n<p id=\"6e43\">Note\uff1aGARCH\u2019s mathematics structure contains all the historical data and the weight becomes lower as the time farer.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from arch import arch_model<mark>mdl_garch = arch_model(pmd_<\/mark>residual, vol = 'GARCH', p = 1, q = 1)<br>res_fit = mdl_garch.fit()<br>res_fit.summary()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1L4Qp3NJN20o0GFPqWm9yIg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">GARCH<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1G5MEYYhjqRET1yhM3vIY6Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_XG8K105oXuXLC2VJJI_hg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"a72b\">According to above tables, we understand that every coefficient is significant. The following is diagnosis of GARCH model.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"142f\"><span class=\"ez-toc-section\" id=\"GARCH_DiagnosisProgramming_is_available_in_the_Source_Code\"><\/span>GARCH Diagnosis(Programming is available in the Source Code)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ad23\"><span class=\"ez-toc-section\" id=\"Residual_diagnosis\"><\/span><strong>Residual diagnosis<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1O8_pK_x71FRR4ZyYhUAxRw.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1PiDwGoomvHoG7KkjCtTYJg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"08e2\">Observing above chart, we can conclude that there is no trend for GARCH residual. On top of that, based on residual histogram and Normal Q-Q plot show the normality. Again, we apply white noise test to further verify that residual is random.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"61ed\"><span class=\"ez-toc-section\" id=\"White_Noise_Test\"><\/span><strong>White Noise Test<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1yC3y8HKEcGm74Sl4N5qe_g.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u767d\u566a\u97f3\u6aa2\u5b9a\u5716<\/figcaption><\/figure>\n\n\n\n<p id=\"fff3\">Test result make us know the error term is white noise(P-value &gt; 0.05). Therefore, no explanatory variable can be taken. The residual is random.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"224c\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"6684\">So far, we have gone through the whole process of modeling. Firstly, verify non-stationary and stationary data. Secondly, fit ARIMA model with return data. Lastly, fit GARCH model with residual term. You may notice the process of Statistics test is very strict. This article, however, in order to make readers acquainted with whole process quickly, spare mathematics theorems behind models and tests.<\/p>\n\n\n\n<p id=\"f952\">The above implementation of time series model is what we expect readers to understand. Following article would discuss how to apply the models to forecasting. Last but not least, credit to database of TEJ, we can easily conduct tests and models. As a result, welcome to purchase the plan offered in&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a>&nbsp;to find the data you need.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d859\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span>Source Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/eb22d1722db53619925b6dc8137f2d1a#file-10_arima-garch-1-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\"><strong>Github<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"660a\">Extended Reading<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/data-analysis-6-xgboost-algorithm-predicts-returns-part-2-f8fe73a96584\" target=\"_blank\" rel=\"noopener\">\u3010Data Analysis(6)\u3011XGBoost Algorithm Predicts Returns (Part 2)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/introduction-6-obtain-financial-data-a152f472a58f\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">\u3010Introduction(6)\u3011- Obtain Financial Data<\/a><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a18e\">Related Link<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>First of all, we need to declare the Time Series concept. It is a kind of data structure showing the development of historical data by the order of time. As for Time Series Model, it is applied to analyze time series data. Further, by this model, we manage to find high-likelihood trend and make forecasting.<\/p>\n","protected":false},"featured_media":12167,"template":"","tags":[2371,2632,3007],"insight-category":[690,50],"class_list":["post-12151","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-statistics","tag-tejapi-data-analysis","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/12151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":1,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/12151\/revisions"}],"predecessor-version":[{"id":24861,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/12151\/revisions\/24861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/12167"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=12151"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=12151"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=12151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}