{"id":2414,"date":"2022-02-08T02:32:11","date_gmt":"2022-02-08T02:32:11","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/arima-garch-modelpart-2\/"},"modified":"2022-02-08T02:32:11","modified_gmt":"2022-02-08T02:32:11","slug":"arima-garch-modelpart-2","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/arima-garch-modelpart-2\/","title":{"rendered":"ARIMA-GARCH Model(Part 2)"},"content":{"rendered":"<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" class=\"wp-image-12170\" height=\"682\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/image-184.png\" width=\"1024\"\/><figcaption class=\"wp-element-caption\">Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@isaacmsmith\" rel=\"noreferrer noopener\" target=\"_blank\">Isaac Smith<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"ed2b\">Highlights<\/h2>\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n<li>Stock Price Forecasting by Time Series Model<\/li>\n<li>Reminder\uff1aIn this article, we would apply Time Series Model on trend forecasting, and no pre-preprocessing steps in\u3112olved. Therefore, if you are not familiar with the fundamentals about Time Series, please read\u00a0<a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/data-analysis-10-arima-garch-model-part-1-a011bf45f66c\" rel=\"noopener\" target=\"_blank\">\u3010Data Analysis(10)\u3011ARIMA-GARCH Model(Part 1)<\/a>\u00a0firstly.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"8cb3\">Preface<\/h2>\n<p class=\"wp-block-paragraph\" id=\"dde3\">First of all, we would implement the process to construct models so as to make you understand the application of python packages. However, in case of redundancy of this article, there is no hypothesis test. Subsequently, we would calculate the forecasted return and price. Last but not least, apply visualization to compare the prediction and actual trend to assess the result of ARMA-GARCH.<\/p>\n<p class=\"wp-block-paragraph\" id=\"77f4\">Note: we apply \u201cARMA\u201d in this article, not like the previous one \u201cARIMA\u201d. The difference is that ARIMA is capable of differencing and dealing with non-stationary data. We conducted ARIMA in previous one to make you understand Time Series profoundly. Here, the use of ARMA would make you know the alternative of Time Series Model.<\/p>\n<h2 class=\"wp-block-heading\" id=\"cfb9\">Editing Environment and Modules Required<\/h2>\n<p class=\"wp-block-paragraph\" id=\"b710\">MacOS &amp; Jupyter Notebook<\/p>\n<pre class=\"wp-block-preformatted\" id=\"b862\"><code>import numpy as np<\/code> <code>import pandas as pd<\/code> <code>import matplotlib.pyplot as plt<\/code> <code>%matplotlib inline<\/code> <code>import seaborn as sns<\/code> <code>sns.set()<\/code> <code>import tejapi<\/code> <code>tejapi.ApiConfig.api_key = 'Your Key'<\/code> <code>tejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"11fb\">Database<\/h2>\n<p class=\"wp-block-paragraph\" id=\"34f9\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=42\" rel=\"noreferrer noopener\" target=\"_blank\">Security Transaction Data Table<\/a>\uff1aListed securities with unadjusted price and index. Code is \u2018TWN\/APRCD\u2019.<\/p>\n<h2 class=\"wp-block-heading\" id=\"61a8\">Data Selection &amp; Model Construction<\/h2>\n<p class=\"wp-block-paragraph\" id=\"b205\"><strong>Step 1. Data Selection, oo5o.TW<\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"5601\"><code>data = tejapi.get('TWN\/APRCD', # \u516c\u53f8\u4ea4\u6613\u8cc7\u6599-\u6536\u76e4\u50f9<\/code> <code>            coid= '0050', # \u53f0\u706350<\/code> <code>            mdate={'gte': '2003-01-01', 'lte':'2021-12-31'},<\/code> <code>            opts={'columns': ['mdate', 'close_d', 'roi']},<\/code> <code>            chinese_column_name=True,<\/code> <code>            paginate=True)<\/code> <code>data['\u5e74\u6708\u65e5'] = pd.to_datetime(data['\u5e74\u6708\u65e5'])<\/code> <code>data = data.set_index('\u5e74\u6708\u65e5')<\/code> <code>data = data.rename(columns = {'\u6536\u76e4\u50f9(\u5143)':'\u6536\u76e4\u50f9', '\u5831\u916c\u7387\uff05':'\u65e5\u5831\u916c\u7387(%)'})<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"1f4e\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1sJiEuovKewkQP73B4DOK_Q-2.png\"\/><figcaption class=\"wp-element-caption\">\u8cc7\u6599\u8868(\u4e00)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"0c70\"><strong>Step 2. Data Split<\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"280c\"><code>train_date = data.index.get_level_values('\u5e74\u6708\u65e5') &lt;= '2020-12-31'<\/code> <code>train_data = data[train_date].drop(columns = ['\u6536\u76e4\u50f9'])<\/code> <code>test_data = data[~train_date]<\/code> <code>\uff03 \u4fdd\u7559test_data\u6536\u76e4\u50f9\uff0c\u7528\u4f86\u6bd4\u5c0d\u6a21\u578b\u9810\u6e2c\u503c<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter\" id=\"19e9\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1zI39vb6TA1mc3m58rUZOQg-2.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"e0ab\"><strong>Step 3. Selection of ARMA\u2019s parameters<\/strong><\/p>\n<p class=\"wp-block-paragraph\" id=\"3f24\">Here, we apply statsmodels to select parameters, not like the previous article, where we used pmdarima.<\/p>\n<pre class=\"wp-block-preformatted\" id=\"1e58\"><code>import statsmodels.api as sm<\/code> <code># AIC\u3001BIC\u6e96\u5247<\/code> <code>sm.tsa.stattools.arma_order_select_ic(train_data, ic=[\"aic\", \"bic\"])<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"7ed5\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/17pVQlrePp-uN8A_MASUehA-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u4e00)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"0bd6\">The BIC standard make us apply (p,q) = (0,0), which is derived from that BIC tends to conduct stricter selection on case of multi-variables. Therefore, like the previous article, we would use AIC, (p,q) = (2,2), to construct ARMA.<\/p>\n<p class=\"wp-block-paragraph\" id=\"2e08\"><strong>Step 4. ARMA Model<\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"6656\"><code>from statsmodels.tsa.arima_model import ARMA<\/code> <code>model = ARMA(train_data, order = (2, 2))<\/code> <code>arma = model.fit() <\/code> <code>print(arma.summary())<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"2127\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1BfafYpJoQqef0vnmvgFszA-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u4e8c)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"2fbc\"><strong class=\"markup--strong markup--p-strong\"><strong>Step 5. GARCH Model<\/strong><\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"c6ec\"><code>\uff03 \u53d6\u5f97ARMA\u6a21\u578b\u7684\u6b98\u5dee\u9805\u76ee<\/code> <code>arma_resid = list(arma.resid)<\/code> <code>from arch import arch_model<\/code> <code>mdl_garch = arch_model(arma_resid, vol = 'GARCH', p = 1, q = 1)<\/code> <code>garch = mdl_garch.fit()<\/code> <code>print(garch.summary())<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"2559\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/13F5jTjgjAjERkID9RiYqIw-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u4e09)<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"31e4\">Model Forecasting(Programming of Graphics is available in the Source Code)<\/h2>\n<p class=\"wp-block-paragraph\" id=\"d60a\">We apply ARMA to forecast the average and GARCH to modify the prediction interval.<\/p>\n<p class=\"wp-block-paragraph\" id=\"0f8c\"><strong>Step 1. Average Return Forecasting<\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"1fb8\"># len(train_data) = 4333, len(data) = 4577\nforecast_mu = arma.predict(start = 4333, end = 4576) \n# \u9810\u6e2c\u51fd\u5f0f\u7684end\u5305\u542b\u7576\u671f\uff0c\u6240\u4ee5\u9700\u9032\u884c4577-1=4576\u3002<\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"2348\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1WiJZNbWk5_kghfm5AbwZig-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u56db)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"6ca4\">According to above chart, we find that forecasted average return would gradually approach to 0. Fluctuations mainly happens during initial period.<\/p>\n<p class=\"wp-block-paragraph\" id=\"6326\"><strong>Step 2. Volitility Forecasting<\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"5631\"><code>garch_forecast = []<\/code> <code>for i in range(len(test_data)):<\/code> <code>    train = arma_resid[:-(len(test_data)-i)]<\/code> <code>    model = arch_model(train, vol = 'GARCH', p = 1, q = 1)<\/code> <code>    garch_fit = model.fit()<\/code> <code>    prediction = garch_fit.forecast(horizon=1)<\/code> <code>    garch_forecast.append(np.sqrt(prediction.variance.values[-1:][0]))<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"5125\">Implementing rolling forecasting to predict every single period. Hence, we code in the way making GARCH contained in the loop and store values in the list. Subsequently, we add above forecasted values to \u201ctest_data\u201d table and compute upper and lower limits of interval.<\/p>\n<pre class=\"wp-block-preformatted\" id=\"ba40\"><code>test_data['ARMA\u9810\u6e2c\u5831\u916c(%)'] = list(forecast_mu)<\/code> <code>test_data['GARCH\u9810\u6e2c\u6ce2\u52d5\u5ea6'] = (garch_forecast)<\/code> <code>test_data['\u9810\u6e2c\u5340\u9593\u4e0a\u9650'] = test_data['ARMA\u9810\u6e2c\u5831\u916c(%)'] + test_data['GARCH\u9810\u6e2c\u6ce2\u52d5\u5ea6']<\/code> <code>test_data['\u9810\u6e2c\u5340\u9593\u4e0b\u9650'] = test_data['ARMA\u9810\u6e2c\u5831\u916c(%)'] - test_data['GARCH\u9810\u6e2c\u6ce2\u52d5\u5ea6']<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"2d6e\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1vaoQXcZwKlHio09zzQgd1A-2.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"59e2\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1qVjoUa_LlMHGquBqLe8blQ-2.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"b6f8\">By above chart, it is clear that most of actual returns are in the interval. However, the individual return of much volatility cannot be predicted accurately.<\/p>\n<p class=\"wp-block-paragraph\" id=\"75a0\"><strong class=\"markup--strong markup--p-strong\"><strong>Step 3. Price Forecasting<\/strong><\/strong><\/p>\n<pre class=\"wp-block-preformatted\" id=\"a574\"><code># \u672c\u6587\u5df2\u7d93\u628atrain_data\u4e2d\u7684\u50f9\u683c\u522a\u9664\uff0c\u6240\u4ee5\u9700\u91cd\u65b0\u8a08\u7b972020-12-30\u7684\u6536\u76e4\u50f9<\/code> <code>first_price = test_data['\u6536\u76e4\u50f9'][0] \/ (1+test_data['\u65e5\u5831\u916c\u7387(%)'][0]*0.01)<\/code> <code># \u8a08\u7b97\u7b2c\u4e00\u671f\u9810\u6e2c<\/code> <code>test_data['ARMA\u9810\u6e2c\u50f9\u683c'] = first_price * (1 + test_data['ARMA\u9810\u6e2c\u5831\u916c(%)']*0.01)<\/code> <code>test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0a\u9650'] = first_price * (1 + test_data['\u9810\u6e2c\u5340\u9593\u4e0a\u9650']*0.01)<\/code> <code>test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0b\u9650'] = first_price * (1 + test_data['\u9810\u6e2c\u5340\u9593\u4e0b\u9650']*0.01)<\/code> <code># \u8a08\u7b97\u5269\u9918\u9810\u6e2c\u5340\u9593<\/code> <code>for i in range(1, len(test_data)):<\/code> <code>        test_data['ARMA\u9810\u6e2c\u50f9\u683c'][i] = test_data['\u9810\u6e2c\u50f9\u683c'][i-1] * (1 + test_data['ARMA\u9810\u6e2c\u5831\u916c(%)'][i]*0.01)<\/code> <code>        test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0a\u9650'][i] = test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0a\u9650'][i-1] * (1 + test_data['\u9810\u6e2c\u5340\u9593\u4e0a\u9650'][i]*0.01)<\/code> <code>        test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0b\u9650'][i] = test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0b\u9650'][i-1] * (1 + test_data['\u9810\u6e2c\u5340\u9593\u4e0b\u9650'][i]*0.01)<\/code> <code># \u8a08\u7b97\u5340\u9593\u5747\u50f9<\/code> <code>test_data['\u9810\u6e2c\u5e73\u5747\u50f9\u683c'] = (test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0a\u9650'] + test_data['\u9810\u6e2c\u50f9\u683c\u5340\u9593\u4e0b\u9650']) \/ 2<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"9a2f\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1vrmhSuR0IG588w30UMLG7w-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u516d)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"c2f5\">By above chart, we find that the interval expands dramatically with time elapsing. Therefore, it is not reliable enough to assess the result of prediction. we would show the first 2 month and make the conclusion.<\/p>\n<pre class=\"wp-block-preformatted\" id=\"d470\"><code>new_date = test_data.index.get_level_values('\u5e74\u6708\u65e5') &lt;= '2021-03-01'<\/code> <code>new_test = test_data[new_date]<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\" id=\"65f5\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1lTSZmnMm_s5mDQmxmf1esA-2.png\"\/><figcaption class=\"wp-element-caption\">\u5716(\u4e03)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"947a\">With the period of first two month, it is clear to observe the difference between prediction and actual data. Firstly, the interval average is closer with actual price trend than that of ARMA prediction. As for the interval prediction, we can find that the actual trend does not fall in the interval area until late January.<\/p>\n<h2 class=\"wp-block-heading\" id=\"e0d0\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\" id=\"dd8d\">Based on the last result, you would understand that performance of ARMA-GARCH model on 0050 is not reliable enough, despite the well-fitted model summary. We regard the increasingly wide interval as normal since forecasting should be more conservative with the farther prediction period. Nevertheless, according to first two month chart, we can tell that actual price exceeds predicted interval, which indicates that there is no reliability during the initial period. What brings about the situation may be the lacking consideration about seasonal or exogenous variable. Hence, if you are interested in relative issues, keep reading our articles. To boot, welcom to purchase the plans offered in\u00a0<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/index\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E Shop<\/a>\u00a0and use the well-complete database to implement your own prediction.<\/p>\n<h2 class=\"wp-block-heading\" id=\"8fa8\">Source Code<\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/gist.github.com\/tej87681088\/a095119d4f787863d3f33e09e9cfa4df#file-tejapi_medium-11-ipynb\" rel=\"noopener\" target=\"_blank\">Github<\/a><\/li>\n<\/ul>\n<h1 class=\"wp-block-heading\" id=\"4e3d\">Extended Reading<\/h1>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/data-analysis-10-arima-garch-model-part-1-a011bf45f66c\" rel=\"noopener\" target=\"_blank\">\u3010Data Analysis(10)\u3011ARIMA-GARCH Model(Part 1)<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-14-which-industries-did-three-primary-institutional-investors-invest-in-taiwan-2ec8a5fcda0d\" rel=\"noopener\" target=\"_blank\">\u3010Quant(14)\u3011Which industries did three primary institutional investors invest in Taiwan?<\/a><\/li>\n<\/ul>\n<h1 class=\"wp-block-heading\" id=\"9f1d\">Related Link<\/h1>\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<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 would implement the process to construct models so as to make you understand the application of python packages. However, in case of redundancy of this article, there is no hypothesis test. Subsequently, we would calculate the forecasted return and price. Last but not least, apply visualization to compare the prediction and actual trend to assess the result of ARMA-GARCH.<\/p>\n","protected":false},"featured_media":2413,"template":"","tags":[65,70],"insight_category":[16],"class_list":["post-2414","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tej-api","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/2414","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"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/2413"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=2414"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=2414"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=2414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}