<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Herrera on Medium]]></title>
        <description><![CDATA[Stories by Daniel Herrera on Medium]]></description>
        <link>https://medium.com/@corredaniel1500?source=rss-b559b613378f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2PC3msg7SoWPR7pNiyTYgA.jpeg</url>
            <title>Stories by Daniel Herrera on Medium</title>
            <link>https://medium.com/@corredaniel1500?source=rss-b559b613378f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:07:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@corredaniel1500/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Forecasting Volatility: Deep Dive into ARCH & GARCH Models]]></title>
            <link>https://medium.com/@corredaniel1500/forecasting-volatility-deep-dive-into-arch-garch-models-46cd1945872b?source=rss-b559b613378f------2</link>
            <guid isPermaLink="false">https://medium.com/p/46cd1945872b</guid>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[volatility-trading]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[time-series-analysis]]></category>
            <category><![CDATA[volatility]]></category>
            <dc:creator><![CDATA[Daniel Herrera]]></dc:creator>
            <pubDate>Wed, 07 Jun 2023 13:59:31 GMT</pubDate>
            <atom:updated>2023-06-20T15:47:33.853Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>If you have been around statistical models, you’ve likely worked with linear regression, logistic regression and several other mean modeling approaches. If not, that’s okay! A mean model is quite simply a model that predicts the average outcome value based on one or more predictor variables. The use of these mean models are plentiful across several domains; however, there also exist series of models to predict the conditional variance, which have various uses in financial data.</p><p>We will build on a previous Medium Post, <a href="https://medium.com/@corredaniel1500/introduction-to-arma-models-with-financial-data-6ceb8b52fdd6">&quot;<strong>Introduction to ARMA Models with Financial Data</strong>&quot;,</a> to explore the ARCH (Autoregressive Conditional Heteroskedasticity) and GARCH (Generalized Autoregressive Conditional Heteroskedasticity) models. The goal is to walk away with an understanding of introductory economic theory, the concept of volatility, the motivations and model statements of ARCH and GARCH, recognizing when these models are appropriate, model implementation, and forecasting.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>import pandas as pd<br>import pmdarima as pm<br>import yfinance as yf<br><br>from arch import arch_model<br>from pmdarima.model_selection import train_test_split<br>from scipy.stats import chi2, jarque_bera<br>from sklearn.metrics import mean_absolute_percentage_error, mean_squared_error<br>from statsmodels.graphics.tsaplots import plot_acf, plot_pacf<br>from statsmodels.stats.diagnostic import acorr_ljungbox<br>from statsmodels.tsa.stattools import acf, adfuller<br>from statsmodels.graphics.gofplots import qqplot<br>from statsmodels.tsa.arima.model import ARIMA</pre><h4>Data Import</h4><p>We begin by loading historical S&amp;P 500 ($SPY) data from yfinance.</p><pre>spy = yf.Ticker(&quot;SPY&quot;)<br>hist = spy.history(start = &quot;2010-01-04&quot;, end = &quot;2020-02-01&quot;)<br>df = pd.DataFrame(hist, columns=[&#39;Close&#39;])<br>df.head</pre><pre>Date                                 <br>2010-01-04 00:00:00-05:00   88.117889<br>2010-01-05 00:00:00-05:00   88.351151<br>2010-01-06 00:00:00-05:00   88.413376<br>2010-01-07 00:00:00-05:00   88.786560<br>2010-01-08 00:00:00-05:00   89.082054<br>...                               ...<br>2020-01-27 00:00:00-05:00  307.216400<br>2020-01-28 00:00:00-05:00  310.435852<br>2020-01-29 00:00:00-05:00  310.179321<br>2020-01-30 00:00:00-05:00  311.185974<br>2020-01-31 00:00:00-05:00  305.535522</pre><h3>Background</h3><p>In the <a href="https://medium.com/@corredaniel1500/introduction-to-arma-models-with-financial-data-6ceb8b52fdd6">previous post</a>, we concluded that the closing price of the S&amp;P 500 could be modeled adequately by a random walk model. The efficient market hypothesis will illustrate why the random walk model is appropriate and lead us to consider methods to capture risk and opportunities for profit within this economic framework.</p><h4>Efficient Market Hypothesis:</h4><p>The efficient market hypothesis states that the price of a stock is reflective of an efficient market — that is, all information about the company is reflected in the current value of the stock. While controversial, this view is at the basis of a lot of economic theory. Under this framework, there does not exist an essentially riskless or minimally riskless profit-taking because there are no market inefficiencies that lead to a difference between a current stock value and its actual value, i.e., no arbitrage can exist. It implies that consistently beating the market is impossible based on information about the price of a security up until time <em>t</em>, since all information is available publicly through time <em>t</em>.</p><p>This can be mathematically expressed in the following way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/0*DQ7PE31yw4PhNMI3" /></figure><p>where Xₜ is the current asset price/return and we condition on all previous prices/returns.</p><p>However, this should not be confused with an implication that there is no way to make a profit. Recall, arbitrage reflects a <em>riskless</em> method to extract profit. However, there exist investments which have considerably more risk and create opportunities for profit through that risk. As a result, we can capture the notion of risk through modeling volatility to make profits.</p><h4>Volatility</h4><p>Volatility and risk are closely related terms in financial assets. In the stock market, volatility refers to the amount and frequency of price fluctuations. Meanwhile, risk refers to the probability of losing money when investing in a particular stock.</p><p>We can connect the two — risk and volatility — by sticking with a general concept that the higher the volatility, the higher the risk of a financial asset. We can visualize this distinction is as follows:</p><ul><li>Company A has a stock price of $100 which we are confident will only move +/- 5% in the next quarter.</li><li>Company B has a stock price of $100 which we are confident can move between +/- 50% in the next quarter.</li></ul><p>Albeit a very simplified example, we can see that with the added volatility of company B there comes an added notion of risk with the asset.</p><h4>Volatility Clustering</h4><p>We are able to capture volatility clustering — periods of high volatility or low volatility — via modeling with ARCH. Thus, we can leverage these notions of risk and volatility in the hopes of making profits on assets while still abiding by the principles of the efficient market hypothesis, which limit us to producing trivial, non-informative responses to the conditional expected value of a stock or return at time <em>t</em>.</p><h4>Returns</h4><p>Let&#39;s take a small detour to mention the modeling of returns rather than prices. This is a convenient way to model financial assets and as such will be important to conceptualize.</p><p>Let Rₜ denote the return of an asset at time <em>t</em>, Pₜ denote the price of the asset at time <em>t</em> and Pₜ₋₁ denote the price of the asset at the previous time period <em>t-1</em>. We can formulate the one-period return of an asset as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/0*69cxORfhVoTFyMtM" /></figure><p>Let rₜ denote log return, which is more commonly used because log returns have an additive property that simple returns do not. We formulate the one-period log return of an asset as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/0*7cD3XPfULnJ4TdSh" /></figure><pre># Convert prices to log returns <br>df[&#39;Return&#39;] = np.pad(np.diff(np.log(df[&#39;Close&#39;])) * 100, (1, 0), &#39;constant&#39;, constant_values=np.nan)</pre><h3>ARCH Model</h3><h4>Model Framework</h4><p>We have mentioned previously that although we are limited in calculating the predicted price of a financial asset in the stock market due to the efficient market hypothesis, we can predict volatility.</p><p>The model we will use for this is called auto-regressive conditional heteroskedastic model of order P, ARCH(P).</p><p>Recall, in various cases such as linear regression and even the ARMA(p,q) model, we want a homoskedastic model (constant variance across observations). However, in this case, we specify that there is actually heteroskedasticity (non-constant variance). Since volatility is conceptually linked with variance, we are interested in modeling how the variance changes over time.</p><h4>Model Statement</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/0*XKSGDAftwF9di9Qa" /></figure><p>where 𝜀ₜ is IID(0,1) and b₀, b₁, … , bₚ &gt; 0.</p><p>Xₜ can be the price, returns or log returns of an asset, modeled by the sum of its squared historical values X²ₜ₋ⱼ. For simplicity, we define Xₜ to be the log returns of an asset.</p><p>In expectation, Xₜ² is the marginal variance of the log returns. Why is this the case? Recall the relationship between variance and expected value of var(X) = E[X²] - E[X]². We know that E[Xₜ] = 0 because expected returns are trivially 0 due to the efficient market hypothesis. Thus, E[Xₜ]² = 0, and we are left with the marginal variance 𝜎² = E[Xₜ²], which is constant and does not depend on <em>t.</em></p><p>We build on the concept of constant marginal variance to incorporate heteroskedasticity by modeling the <strong>volatility at time t (𝜎ₜ²)</strong>, <strong>which is the</strong> <strong>conditional variance of the time series</strong> and<strong> </strong>is directly influenced by the squared log return Xₜ².</p><p>We can use all of the above to intuitively see that if Xₜ swings rapidly (i.e., the log return increases or decreases swiftly), Xₜ² is high and thus the volatility 𝜎ₜ² at time <em>t</em> is high. This is how ARCH models capture price swings and their impact on the volatility of an underlying asset.</p><h3>GARCH Model</h3><h4>Model Statement</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/0*RiUKUZBwENiHoqQW" /></figure><p>where 𝜀ₜ is IID(0,1) and b₀, b₁, … , bₚ,a₀, a₁, … , aₙ ≠ 0.</p><h4>Model Framework</h4><p>GARCH is the generalized auto-regressive conditional heteroskedastic model of order (P,Q) and is an extension of the ARCH(P) model. We notice the additional term shown above when defining the conditional variance (volatility 𝜎ₜ² at time <em>t</em>), which allows for modeling of the conditional variance to be dependent on lagged versions of itself using a linear combination of a₀, a₁, … , aₙ and the conditional variances at previous times. This addition to the model statement makes GARCH models more flexible and able to capture the persistence of volatility.</p><p>We can thus reduce the model statement by decomposing the parts of the model into:</p><p>1) ARCH effects: effects attributed to allow for lagged squared log returns Xₜ²</p><p>2) GARCH effects: effects attributed to allow for lagged conditional variances 𝜎ₜ²</p><h3>Exploratory Data Analysis</h3><p>We begin the exploratory data analysis (EDA) phase by plotting a trace plot of the returns over time to visualize volatility clustering. Notice that there are periods with more volatility, illustrated by higher highs and lower lows, which cluster together and periods where we see less volatility. For example, observe the jagged nature of the returns for the S&amp;P 500 in 2010-2012 (high volatility) compared to the relatively stable period from 2013-2015 (low volatility).</p><pre># Add trace plot<br>plt.figure(figsize=(8,5))<br>plt.plot(df[&#39;Return&#39;])<br>plt.ylabel(&quot;Return %&quot;)<br>plt.title(&#39;Returns of S&amp;P 500&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*clID9bRPC-CyoVabfYdGiA.png" /></figure><p>We then generate autocorrelation (ACF) and partial autocorrelation (PACF) plots to visualize how previous returns with different lags are correlated with one another. Recall that because of properties of stationarity, the correlation and covariance at different points in time should be the same for the same gap — also know as lag <em>h</em> — regardless of the exact point in time <em>t</em>. That is, ACF and PACF should be constant across the same lag in time and should depend only on the lag <em>h </em>and not directly on <em>t</em>. A more detailed explanation is available in the <a href="https://medium.com/@corredaniel1500/introduction-to-arma-models-with-financial-data-6ceb8b52fdd6">previous article</a>.</p><pre># Plot ACF and PACF<br>diff_ts = df[&#39;Return&#39;].iloc[1:]<br><br><br>fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))<br>plot_acf(diff_ts, ax=ax1, lags=10)<br>ax1.set_ylim(-0.5, .5) <br>ax1.set_title(&quot;Autocorrelation in Returns&quot;)<br>plot_pacf(diff_ts, ax=ax2, lags=10)<br>ax2.set_ylim(-0.5, .5)  <br>ax2.set_xlabel(&quot;Lag&quot;)  <br>ax2.set_title(&quot;Partial Autocorrelation in Returns&quot;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*rrxL_CYwdwU-0-jj-q2O_w.png" /></figure><p>The above ACF and PACF plots do not indicate that there are ARCH and GARCH effects present. In particular, we would want indication to believe the series in not independent, which would visually be seen as lags for which the ACF and PACF values are outside the blue area. This would imply a significant difference in the ACF or PACF from 0 and would allow us to model the ARCH effects.</p><p>To ensure that this is an independent series, or rather to test that it is not, we remember that an independent series is one in which transformations of the series are themselves independent. Thus, we can repeat the process above using the squared returns or the absolute returns. We utilize the absolute returns, as this is generally done in financial data to diminish the effects of outliers.</p><pre># Plot ACF and PACF of absolute returns<br>abs_returns = diff_ts.abs()<br><br>fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))<br>plot_acf(abs_returns, ax=ax1, lags=10)<br>ax1.set_ylim(-.5, .5) <br>ax1.set_title(&quot;Autocorrelation in Absolute Returns&quot;)<br>plot_pacf(abs_returns, ax=ax2, lags=10)<br>ax2.set_ylim(-.5, .5)  <br>ax2.set_title(&quot;Partial Autocorrelation in Absolute Returns&quot;)<br>ax2.set_xlabel(&quot;Lag&quot;)  <br><br>plt.show();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*18kTYhOC519kSm0YP1UcTw.png" /></figure><p>After the above transformation to absolute returns, there is now a pattern indicative of ARCH and GARCH effects being present. In the PACF, there is not a big drop until about lag 4, which might indicate that we should have an order of P,Q up to 4. For simplicity and illustration we will use a GARCH(1,1) model before assessing and testing different specifications. We will more formally test these effects using the McLeod-Li test for ARCH and GARCH effects. The nuances and mathematical formulations of the test statistics are left out below but the concept and hypothesis are summarized.</p><h4>McLeod-Li Test</h4><p>The McLeod-Li test proposes the following null and alternative hypotheses to test for white noise behavior in the residuals of the series:</p><p>H₀: there are no ARCH effects in the time series</p><p>Hₐ: there are ARCH effects in the time series</p><p>We thus first fit a model of GARCH(1,1) and then apply the McLeod-Li test to the residuals. The result of the test is a p-value &lt; 0.001 and thus we conclude that there are ARCH effects in the time series.</p><pre>def mcleod_li_test(x, k):<br>    &quot;&quot;&quot;<br>    Calculates the McLeod-Li test statistic for a time series with k lags.<br>    Returns the test statistic and its p-value.<br>    &quot;&quot;&quot;<br>    n = len(x)<br>    x_sq = x ** 2<br>    x_sum = np.sum(x_sq)<br>    x_lag_sum = np.sum(x_sq[:-k])<br>    test_stat = n * (n + 2) * x_lag_sum / (x_sum ** 2)<br>    df = k<br>    p_value = 1 - chi2.cdf(test_stat, df)<br>    return test_stat, p_value</pre><pre>test_stat, p_value = mcleod_li_test(abs_returns, 50)<br>print(&quot;McLeod-Li test statistic: &quot;, test_stat)<br>print(&quot;p-value: &quot;, p_value)</pre><pre>McLeod-Li test statistic:  2929.754<br>p-value:  0.0</pre><h3>Fit GARCH(P,Q) Model</h3><p>Below, we fit the GARCH(1,1) model to the data. We implement the process by using a training and testing data split of 80%/20%, with more recent historical data serving as the test set.</p><p>We use arch_model() from the arch package and specify that the data is of mean zero and modeled with a GARCH process. We specify the arguments of P,Q as 1,1 and choose not to standardize (“rescale”) the data.</p><pre># Fit GARCH (1,1)<br>y_train, y_test = train_test_split(abs_returns, train_size= 0.8)<br><br>garch_mod = arch_model(y_train, mean = &quot;Zero&quot;,  vol=&#39;Garch&#39;, p=1, q=1, rescale=False)<br>res_garch = garch_mod.fit()</pre><pre>Iteration:      1,   Func. Count:      5,   Neg. LLF: 4134.426614213216<br>Iteration:      2,   Func. Count:     14,   Neg. LLF: 3069.3112782531625<br>Iteration:      3,   Func. Count:     20,   Neg. LLF: 3281.6598598167393<br>Iteration:      4,   Func. Count:     25,   Neg. LLF: 2411.996065119879<br>Iteration:      5,   Func. Count:     30,   Neg. LLF: 2403.725012109664<br>Iteration:      6,   Func. Count:     34,   Neg. LLF: 2403.6940641153606<br>Iteration:      7,   Func. Count:     38,   Neg. LLF: 2403.691097819412<br>Iteration:      8,   Func. Count:     42,   Neg. LLF: 2403.6909625646995<br>Iteration:      9,   Func. Count:     46,   Neg. LLF: 2403.6908993457237<br>Iteration:     10,   Func. Count:     49,   Neg. LLF: 2403.6908993458132<br>Optimization terminated successfully    (Exit mode 0)<br>            Current function value: 2403.6908993457237<br>            Iterations: 10<br>            Function evaluations: 49<br>            Gradient evaluations: 10</pre><p>We recall the GARCH(P,Q) model statement from above and provide estimates for the parameters as a result of our GARCH model fit:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/0*5HjyM5Q1fKALXr-b" /></figure><p>where 𝜀ₜ is IID(0,1) and b₀, b₁, … , bₚ,a₀, a₁, … , aₙ ≠ 0.</p><p>Our fitted model is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/263/0*KuS7NOAZRoxlOPbS" /></figure><p>where 𝜎ₜ² is the conditional variance at time <em>t</em>, X²ₜ₋₁ is the squared return at time <em>t-1</em>, and 𝜎²ₜ₋₁ is the conditional variance at time <em>t-1</em>.</p><p>The parameter estimates for the GARCH(1,1) model are b₀= 0.0309, b₁= 0.14, and a₁= 0.82.</p><pre># Model summary <br>print(res_garch.summary())</pre><pre>Zero Mean - GARCH Model Results                        <br>==============================================================================<br>Dep. Variable:                 Return   R-squared:                       0.000<br>Mean Model:                 Zero Mean   Adj. R-squared:                  0.000<br>Vol Model:                      GARCH   Log-Likelihood:               -2403.69<br>Distribution:                  Normal   AIC:                           4813.38<br>Method:            Maximum Likelihood   BIC:                           4830.23<br>                                        No. Observations:                 2028<br>Date:                Tue, May 16 2023   Df Residuals:                     2028<br>Time:                        13:10:35   Df Model:                            0<br>                              Volatility Model                              <br>============================================================================<br>                 coef    std err          t      P&gt;|t|      95.0% Conf. Int.<br>----------------------------------------------------------------------------<br>omega          0.0309  8.422e-03      3.667  2.450e-04 [1.438e-02,4.740e-02]<br>alpha[1]       0.1432  2.548e-02      5.621  1.900e-08   [9.329e-02,  0.193]<br>beta[1]        0.8196  2.771e-02     29.575 3.116e-192     [  0.765,  0.874]<br>============================================================================<br><br>Covariance estimator: robust</pre><h3>Forecasting Volatility</h3><h4>N-Step Ahead Forecast</h4><p>Using the GARCH model from above, we are able to forecast volatilities 𝜎ₜ² at time <em>t</em>, which are non-trivial predictions. The model makes these forecasts by accessing data from log returns Xₜ from which 𝜎ₜ₊₁² can be calculated (n-step-ahead forecast). We then use these forecasts to extend the forecast of volatility to times t+1, t+2, …, t+n. We do not continually update Xₜ as time <em>t</em> increases, but rather limit our forecasts for all future dates to be made solely on the current date.</p><p>The following plots show our predicted volatility using this method.</p><pre># Forecast the test set <br>yhat = res_garch.forecast(horizon = y_test.shape[0], reindex=True)</pre><pre>fig,ax = plt.subplots(figsize=(10,8))<br>ax.spines[[&#39;top&#39;,&#39;right&#39;]].set_visible(False)<br># Plot test set returns<br>plt.plot(diff_ts[-y_test.shape[0]:])<br># Plot volatility estimates for test set<br>plt.plot(y_test.index, np.sqrt(yhat.variance.values[-1,:]))<br>plt.title(&#39;S&amp;P 500 Volatility Prediction&#39;)<br>plt.legend([&#39;True Daily Log Returns&#39;, &#39;Predicted Volatility&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*8KR2zy7ou59q2dhJTpsZaQ.png" /></figure><pre># Conditional volatility by model fitting<br>res_garch.plot(annualize=&quot;D&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*xuuJWLPW1rk74x6c9__Dqw.png" /></figure><h4>One-Step Ahead Rolling Forecast</h4><p>Another suitable and more pragmatic approach would be to only make one-step ahead predictions based on information up until time<em> t </em>and update the information to the model in real time as time <em>t</em> becomes time t+1, t+2, etc. More simply, with each passing day, we update Xₜ, Xₜ₊₁, Xₜ₊₂ with the actual, observed return from that day to model one time step ahead.</p><p>Below, we see how well this rolling volatility prediction is able to capture the volatility in our unseen test data.</p><pre>rolling_preds = []<br><br>for i in range(y_test.shape[0]):<br>    train = abs_returns[:-(y_test.shape[0]-i)]<br>    model = arch_model(train, p=1, q=1, rescale = False)<br>    model_fit = model.fit(disp=&#39;off&#39;)<br>    # One step ahead predictor<br>    pred = model_fit.forecast(horizon=1, reindex=True)<br>    rolling_preds.append(np.sqrt(pred.variance.values[-1,:][0]))<br><br>rolling_preds = pd.Series(rolling_preds, index=y_test.index)</pre><pre># Plot of predicted test data rolling volatility predictions<br>fig,ax = plt.subplots(figsize=(10,4))<br>ax.spines[[&#39;top&#39;,&#39;right&#39;]].set_visible(False)<br>plt.plot(rolling_preds)<br>plt.title(&#39;S&amp;P 500 Rolling Volatility Prediction&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*q1u5ewjdsxla4uPpj0zHAw.png" /></figure><pre># Compare n-step-ahead and one-step-ahead rolling predictions<br>fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))<br>ax1.spines[[&#39;top&#39;,&#39;right&#39;]].set_visible(False)<br>ax1.plot(diff_ts[-y_test.shape[0]:])<br>ax1.plot(y_test.index, np.sqrt(yhat.variance.values[-1,:]))<br>ax1.set_title(&quot;S&amp;P 500 Volatility N-Step Predictions&quot;)<br>ax1.legend([&#39;True Daily Returns&#39;, &#39;Predicted Volatility&#39;])<br><br>ax.spines[[&#39;top&#39;,&#39;right&#39;]].set_visible(False)<br>ax2.plot(diff_ts[-y_test.shape[0]:])<br>ax2.plot(y_test.index,rolling_preds)<br>ax2.set_title(&quot;S&amp;P 500 Volatility Rolling Predictions&quot;)<br>ax2.legend([&#39;True Daily Returns&#39;, &#39;Predicted Rolling Volatility&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*AhjKKOsGA70-uQqpcu5dYw.png" /></figure><p>The rolling volatility predictions has several strengths. It generates spikes in predicted volatility that line up with periods of actual higher variance of daily returns. We also notice its ability to predict volatility clustering — certain periods have lower variance followed by periods of large variance. Additionally, it appears to do well in forecasting these volatility spikes in advance of the swings in the true daily returns.</p><h3>Model Diagnostics</h3><p>Since model diagnostics are often an overlooked step, we will spend some time assessing whether our fitted model is valid and provide direction for next steps.</p><h4>Jarque-Bera Test</h4><p>We make an assumption in the GARCH(P,Q) model about the distribution of our errors: if the model is specified correctly, then the errors are normally distributed with mean 0 and constant variance. The Jarque-Bera test checks if the model errors are normally distributed.</p><p>The formula for the test statistic is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/0*1PSkNlx6TYW-PTRf" /></figure><p>where n is the size of the data, S is the sample skewness (which measures the symmetry of the data relative to the mean) and K is the sample kurtosis (which measures the shape of distribution, especially the tails).</p><p>We know that the kurtosis of the normal distribution is 3, which is useful as a reference. If greater than 3, we have a sample that is heavier-tailed than normal distribution. If less than 3, we have a sample that is lighter-tailed than the normal distribution.</p><p>We can gather the intuition that using the symmetry and shape of the sample relative to known values for the normal distribution would help us arrive at the following hypothesis:</p><p>H₀: The data has a normal distribution.</p><p>Hₐ: The data does not have a normal distribution.</p><pre># Calculate the standardized residuals on the test data<br>std_resid = res_garch.resid / res_garch.conditional_volatility<br><br># Perform Jarque-Bera test using scipy.stats<br>jb_test = jarque_bera(std_resid)<br>print(&quot;Jarque-Bera test statistic:&quot;, jb_test[0])<br>print(&quot;p-value:&quot;, jb_test[1])</pre><pre>Jarque-Bera test statistic: 1950.9105880383472<br>p-value: 0.0</pre><p>The Jarque-Bera test shows us that we do not have standardized residuals that follow the normal distribution. While beyond the scope of this post, the next step is to specify different distributions in the GARCH model that are heavier-tailed for the error terms, such as the Student’s t-distribution.</p><h3>Conclusion</h3><p>Our journey into ARCH and GARCH models took us away from the general framework of mean modeling to variance modeling. It took us to economics and the efficient market hypothesis and finely combed through the math that builds these powerful models. It gave us tools in the EDA phase to recognize when to use the ARCH and GARCH models. We fit models and built forecasts that could be done on a rolling basis to predict the volatility in the most practical manner. Lastly, we ended on an exploration of what can we do to improve our model by subtly changing our assumptions.</p><p>On that final note, we can modify the ARCH and GARCH approaches many different ways. Importantly, we can choose to combine this GARCH approach with an ARMA model to give rise to the ARMA + GARCH model. Stay tuned for the next post to read about this versatile approach!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4hT0oN8lOinZyFNo.jpg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46cd1945872b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to ARMA Models with Financial Data]]></title>
            <link>https://medium.com/@corredaniel1500/introduction-to-arma-models-with-financial-data-6ceb8b52fdd6?source=rss-b559b613378f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ceb8b52fdd6</guid>
            <category><![CDATA[time-series-analysis]]></category>
            <category><![CDATA[financial-modeling]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <dc:creator><![CDATA[Daniel Herrera]]></dc:creator>
            <pubDate>Sat, 20 May 2023 17:48:47 GMT</pubDate>
            <atom:updated>2023-05-20T17:48:47.262Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>There is something about statistical models that <em>really </em>pulls at my curiosity.</p><p>The way we can mathematically represent a data process and leverage its properties to forecast into the future seems equal parts wizardry and mathematical genius. Could it be the manner in which we arrive at set assumptions about the data, basing our modeling approach on domain theory? Or am I drawing on a competitive need to find the best performing model to predict the future?</p><p>Whatever draws you in, let’s explore the inner workings of Autoregressive Moving Average (ARMA) models. We navigate through loading financial data, detailing model assumptions, discussing our model statement, obtaining our estimated model parameters, yielding predictions, and measuring model performance.</p><h3>Data Import</h3><p>Let’s begin by importing the following libraries to get our hands on financial data.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>import pandas as pd<br>import pmdarima as pm<br>import yfinance as yf<br><br>from pmdarima.model_selection import train_test_split<br>from sklearn.metrics import mean_absolute_percentage_error, mean_squared_error<br>from statsmodels.graphics.tsaplots import plot_acf, plot_pacf<br>from statsmodels.tsa.stattools import adfuller<br>from statsmodels.tsa.arima.model import ARIMA</pre><p>We can use the yfinance package in Python which uses Yahoo’s publicly available API to pull historical data. This walkthrough will use data from the S&amp;P 500 ($SPY). We create a ticker object for $SPY allowing us to access methods or attributes of the object. Below we define start and end dates in string format for the period of historical data we want to pull, using the history method.</p><pre>spy = yf.Ticker(&quot;SPY&quot;)<br>hist = spy.history(start = &quot;2010-01-04&quot;, end = &quot;2020-02-01&quot;)<br>df = pd.DataFrame(hist, columns=[&#39;Close&#39;])<br>df.head</pre><pre>Date                                 <br>2010-01-04 00:00:00-05:00   88.117889<br>2010-01-05 00:00:00-05:00   88.351151<br>2010-01-06 00:00:00-05:00   88.413376<br>2010-01-07 00:00:00-05:00   88.786560<br>2010-01-08 00:00:00-05:00   89.082054<br>...                               ...<br>2020-01-27 00:00:00-05:00  307.216400<br>2020-01-28 00:00:00-05:00  310.435852<br>2020-01-29 00:00:00-05:00  310.179321<br>2020-01-30 00:00:00-05:00  311.185974<br>2020-01-31 00:00:00-05:00  305.535522</pre><h3>Stationarity: an important assumption</h3><p>To begin a data analysis of time series data using ARMA modeling, it is useful to look at a plot of historical stock prices over time (referred to as a “trace plot”) to check for stationarity.</p><pre># Add trace plot<br>plt.figure(figsize=(8,5))<br>plt.plot(df[&#39;Close&#39;]);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*dDpnBjv-86S2NYNtw5blHg.png" /><figcaption>S&amp;P 500 Trace Plot</figcaption></figure><p>Let’s understand the importance of stationarity conceptually and then mathematically. Oftentimes, it is the case that we would like to make predictions about a future point in time based on historical data. For example, we may wish to predict the return of an asset in the years 2023–2030 based on historical data from the 2015–2022. It is necessary for making valid forecasts that the mean, variance and covariance estimates we make for historical data (2015–2022) are similar to those of future data (2023–2030) — in layman terms, we must have evidence that properties of the past will carry forward into the future. We say that these estimates depend not on time point <em>t</em> but on the lag <em>h </em>between any time point <em>s</em> and <em>t </em>(i.e.,<em> </em>|<em>s-t</em>|). In order for stationarity to be satisfied, these estimates would not differ across different time points <em>t, </em>as long as the lags between two time points are equivalent. For example, if stationarity is satisfied, we can conclude that the relationship — covariance — between the one year lag period of 2015 and 2016 is equivalent to the relationship between future one year lag periods of 2023 and 2024, 2024 and 2025, and so on.</p><p>This important property allows us to make valid statistical inferences about the time series data and leverage classical forecasting models such as ARMA.</p><p>Formally, in order for stationarity to be satisfied, the following must be true:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/0*Evh31EYYU8kqsxPP" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/0*JlR1lEjyvGYQE26H" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/0*sfOlFKrPJsUmrtXp" /></figure><p>In the above trace plot, stationarity is not satisfied: the mean price of the asset is not constant and depends on <em>t, </em>and the variance, which we will informally describe as jaggedness, is higher in some time periods than others.</p><p>We can employ methods to ensure stationarity such as differencing our data. Currently, our data are represented as Xₜ where each time point has a stock price at close associated with it. However, we may be interested in differencing our data in order to achieve stationarity. We subtract the current observation by the previous to give us a new time series Yₜ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/0*1iq5_PBGhOuiXrty" /></figure><p>The plots below indicate that differencing helped remove any noticeable trends or patterns. Notably, we now have a constant mean and variance, both of which do not depend on <em>t</em>.</p><pre>fig, (ax1, ax2) = plt.subplots(2, sharex=True)<br>fig.suptitle(&#39;$SPY price: Effect of Differencing&#39;)<br>ax1.plot(df[&#39;Close&#39;])<br>ax1.set_ylabel(&#39;price&#39;)<br>ax1.set_title(&#39;Original&#39;)<br>ax2.plot(df[&#39;Close&#39;].diff())<br>ax2.set_title(&quot;Differenced&quot;)<br>ax2.set_ylabel(&#39;difference price&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*rf-cXQJ-ZFetVtfQXJzieA.png" /></figure><p>Additionally, we use a more formal Augmented Dickey-Fuller (ADF) test, to test a null hypothesis that the series is not stationary. This test confirms our visual interpretation of stationarity for the differenced data.</p><pre>from statsmodels.tsa.stattools import adfuller<br><br>def ADF_Cal(x):<br>    result = adfuller(x)<br>    ADF_stat = result[0]<br>    p = result[1]<br>    print(&quot;ADF Statistic: %f&quot; % ADF_stat)<br>    print(&quot;p-value: %f&quot; % p)<br>    print(&quot;Critical Values&quot;)<br>    levels = [.01, .05, .1]<br>    i = 0<br>    for key,value in result[4].items():<br>        print(&#39;\t%s: %.3f&#39; % (key,value))<br>        hyp = p &lt; levels[i]<br>        if ADF_stat &lt; value:<br>            cert = (1-levels[i])*100<br>            print(&#39;Reject H0: {}&#39;.format(hyp))<br>            break<br>        i = i+1<br>        if i &gt;= 3:<br>            print(&quot;Less than 90% certain that data is stationary&quot;)<br>            print(&#39;Reject H0: {}&#39;.format(hyp))<br><br>ADF_Cal(df[&#39;Close&#39;].diff()[1:])</pre><pre>ADF Statistic: -11.415859<br>p-value: 0.000000<br>Critical Values<br>    1%: -3.433<br>Reject H0: True</pre><h3>ARMA Model Statement</h3><p>An ARMA(p,q) model has two components: AR(p) and MA(q).</p><p>AR(p) refers to the autoregressive nature of the data. In the simplest case, this is equivalent to Xₜ<em> = </em>φ<em>X</em>ₜ₋₁ + Wₜ. This model assigns weight φ to a previous observation and adds noise Wₜ which we generally assume to be normally distributed. If we have several orders of the autoregressive component (p ≥ 1), we can define the coefficients φ₁, φ₂, …, φₚ within a characteristic polynomial such as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/0*9As8tn02TO1mDCmX" /></figure><p>which can be rewritten within a characteristic polynomial as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/0*-6xVxlZEH4bE9bU9" /></figure><p>Here is an example of an order 2, p = 2, ARMA(2,0) process below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/0*5H4BjuoTCtu7Lvhr" /></figure><p>which can be equivalently be rewritten as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/0*-ujL0eqGS7W19OpE" /></figure><p>MA(q) refers to the moving average component of the data. In the simplest case, we have Xₜ<em> = </em>θ<em>W</em>ₜ₋₁ + Wₜ<em>. </em>This takes the average over a sliding window. If we have several orders of the moving average component (q ≥ 1), we can also define the coefficients θ₁, θ₂, …, θₙ within a characteristic polynomial such as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/0*FwFFIO42lNZekin0" /></figure><p>which can be rewritten as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/0*GCcDyBXp6xCJz4tu" /></figure><p>Combining the above, we reach the ARMA(p,q) model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/0*JCgyEdH-gkf0Qsu2" /></figure><p>We can then rewrite the full ARMA(p,q) model by combining the characteristic polynomial notations for the AR(p) and MA(q) components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/140/0*aZrRTKj68-vaH4iM" /></figure><h3>ACF &amp; PACF Plots: Selecting orders of p and q</h3><p>Autocovariance function (ACF) and partial autocorrelation function (PACF) plots help us decide on the order, p and q to approximate an appropriate ARMA(p,q) model for the data.</p><p>ACF is the autocovariance of lag <em>h</em>. Recall, for a stationary time series the covariance of different time periods must not depend on<em> t </em>but on |<em>s-t</em>|.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/0*gMcC3Cd_A3mzq1NR" /></figure><p>The autocovariance between observations Xₜ and Xₜ₋₁ is equivalent to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/164/0*4EmPrx-uM7SkSFQw" /></figure><p>We expect this value to be the same between <strong>all</strong> observations which are one year (<em>h</em> = 1) apart.</p><p>PACF is the partial autocorrelation of lag <em>h</em>. The partial autocorrelation at lag <em>h</em> is the correlation between the time series data point at lag <em>k </em>and the same data point lagged by <em>h</em> time units, while controlling for the influence of all other data points between them. For example, at lag 3, partial autocorrelation removes the effect lags 1 and 2 have on computing the correlation. We can roughly use the visual cues below to determine what appropriate cutoffs for our ARMA() model would be.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*QB-SMvKv9SenJ8GOURcSYQ.png" /></figure><pre># Plot ACF and PACF<br>diff_ts = df[&#39;Close&#39;].diff()[1:]<br>fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))<br>plot_acf(diff_ts, ax=ax1, lags = 10)<br>plot_pacf(diff_ts, ax=ax2, lags = 10)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*K03VXhyF4BLQizhu0sOmpw.png" /></figure><p>While ACF and PACF can be used to determine the orders of p and q as described above, python’s pmdarima auto_arima() function will itself determine the orders of p and q to use for our ARMA(p,q) model. We implement the process by using a training and testing data split of 80%/20%, with more recent historical data serving as the test set.</p><p>It is important to note for reading model output that the differencing operation we applied to the data makes running an ARMA(p,q) model on the differenced data equivalent to running an ARIMA(p,d,q) model on the original data, where d is the order of differencing (d = 1 in this case).</p><pre># Split train test<br>y_train, y_test = train_test_split(df, train_size= 0.8)<br># Fit model<br>model=pm.auto_arima(y_train,start_p=0,d=1,start_q=0,<br>          max_p=5,max_d=1,max_q=5, start_P=0,<br>          D=0, start_Q=0, max_P=5,max_D=0,<br>          max_Q=5, seasonal=False,<br>          error_action=&#39;warn&#39;,trace=True,<br>          supress_warnings=True,<br>          random_state=20,n_fits=50)</pre><pre>Performing stepwise search to minimize aic<br> ARIMA(0,1,0)(0,0,0)[0] intercept   : AIC=6573.277, Time=1.52 sec<br> ARIMA(1,1,0)(0,0,0)[0] intercept   : AIC=6573.373, Time=0.08 sec<br> ARIMA(0,1,1)(0,0,0)[0] intercept   : AIC=6573.368, Time=0.10 sec<br> ARIMA(0,1,0)(0,0,0)[0]             : AIC=6580.893, Time=0.03 sec<br> ARIMA(1,1,1)(0,0,0)[0] intercept   : AIC=6574.633, Time=0.37 sec<br><br>Best model:  ARIMA(0,1,0)(0,0,0)[0] intercept<br>Total fit time: 2.095 seconds</pre><pre>model.summary()</pre><pre>SARIMAX Results                                <br>==============================================================================<br>Dep. Variable:                      y   No. Observations:                 2029<br>Model:               SARIMAX(0, 1, 0)   Log Likelihood               -3284.637<br>Date:                Fri, 19 May 2023   AIC                           6573.274<br>Time:                        13:39:48   BIC                           6584.503<br>Sample:                             0   HQIC                          6577.394<br>                               - 2029                                         <br>Covariance Type:                  opg                                         <br>==============================================================================<br>                 coef    std err          z      P&gt;|z|      [0.025      0.975]<br>------------------------------------------------------------------------------<br>intercept      0.0843      0.028      3.045      0.002       0.030       0.139<br>sigma2         1.4939      0.030     49.504      0.000       1.435       1.553<br>===================================================================================<br>Ljung-Box (L1) (Q):                   1.91   Jarque-Bera (JB):               837.71<br>Prob(Q):                              0.17   Prob(JB):                         0.00<br>Heteroskedasticity (H):               1.50   Skew:                            -0.44<br>Prob(H) (two-sided):                  0.00   Kurtosis:                         6.02<br>===================================================================================</pre><h3>Estimated Model and Interpretation</h3><p>The automated process above selected an ARIMA(0,1,0) model for the original data, <em>X</em>ₜ. Since the data is differenced, this ARIMA(0,1,0) is equivalent to a random walk model given that Yₜ<em> = X</em>ₜ ₋ <em>X</em>ₜ₋₁ and our ARIMA(0,0,0) model is Yₜ₋ μ = Wₜ (random walk by definition).</p><p>In conjunction, our ACF and PACF plots show an absence of lagged patterns. Thus, this confirms that a random walk is most appropriate. Our results support previous findings in financial modeling literature which have confirmed that the most parsimonious ARMA(p,q) models and random walk models outperform other complex modeling processes (including deep learning models) with regularity.</p><p>The estimated model for this process is:</p><p>Yₜ -0.0843 = Wₜ , where Wₜ ∼ white noise(1.5)</p><p>Recall that Yₜ is our differenced data: Yₜ<em> = X</em>ₜ ₋ <em>X</em>ₜ₋₁</p><h3>Additional Model for Comparison</h3><p>We are interested in seeing how well our random walk model performs compared to a more complex model, which also serves to provide a better intuition of ARMA models more generally. Our more complex model is an ARMA(1,0) process on the differenced data — or equivalently ARIMA(1,1,0) — which seems to have performed well according to AIC values.</p><p>The estimated model for this process is:</p><p>Yₜ ₋ 0.0843 = -0.306 (<em>X</em>ₜ ₋ 0.0843) + Wₜ , where Wₜ ∼ wn(1.5) and Yₜ<em> = X</em>ₜ ₋ <em>X</em>ₜ₋₁ (i.e. our differenced dataset)</p><pre># Fit AR(1) model on training data<br>ar_mod = ARIMA(y_train, order=(1,1,0))<br>ar_fit = ar_mod.fit()<br>print(ar_fit.summary())</pre><pre>SARIMAX Results                                <br>==============================================================================<br>Dep. Variable:                  Close   No. Observations:                 2029<br>Model:                 ARIMA(1, 1, 0)   Log Likelihood               -3288.773<br>Date:                Thu, 18 May 2023   AIC                           6581.547<br>Time:                        11:34:48   BIC                           6592.777<br>Sample:                             0   HQIC                          6585.667<br>                               - 2029                                         <br>Covariance Type:                  opg                                         <br>==============================================================================<br>                 coef    std err          z      P&gt;|z|      [0.025      0.975]<br>------------------------------------------------------------------------------<br>ar.L1         -0.0258      0.015     -1.742      0.082      -0.055       0.003<br>sigma2         1.5000      0.030     49.627      0.000       1.441       1.559<br>===================================================================================<br>Ljung-Box (L1) (Q):                   0.05   Jarque-Bera (JB):               866.58<br>Prob(Q):                              0.83   Prob(JB):                         0.00<br>Heteroskedasticity (H):               1.52   Skew:                            -0.46<br>Prob(H) (two-sided):                  0.00   Kurtosis:                         6.07<br>===================================================================================</pre><h3>Diagnostic Plots</h3><p><strong>Random Walk Model Diagnostics</strong></p><p>We run the diagnostic plots to check that the random walk model is appropriate and a good fit to the data.</p><pre>plt.rc(&quot;figure&quot;, figsize=(16,8))<br>model.plot_diagnostics()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*sxRsczR4nrddDzopJY8N9Q.png" /></figure><p>We see in the correlogram that there is not significant correlation as most lags ≥ 1 fall within the bands. Thus, the time series residuals have low correlation with lagged versions of itself. This supports the finding that the time series is a random walk, as previous observations are not correlated with subsequent observations.</p><p>The normal Q-Q plot shows some deviation from normality at the tails and the density plot does show a slight deviation from normal. While this is generally concerning, we will see how we can manage and use these differences in subsequent analysis — ARCH and GARCH modeling.</p><p>We see in the Ljung-Box test from the model output that the probability is 0.17, so we fail to reject the null that the residuals are independently distributed, which is good and implies i.i.d.</p><p><strong>ARMA(1,0) Model Diagnostics</strong></p><p>We see very similar results as above from the ARMA(1,0) model’s correlogram, normal Q-Q plot, and Ljung-Box test, which validates our model selection.</p><pre>ar_fit.plot_diagnostics()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*QIP1gNSUZx-ECmCuDyrqSg.png" /></figure><h3>Forecasting</h3><p>Below we plot the historical data along with prediction intervals for the random walk and ARMA(1,0) models:</p><pre># Obtain predictions from random walk model <br>rw_preds = pd.DataFrame(model.predict(n_periods = y_test.shape[0]))<br>rw_preds[&#39;date&#39;] = y_test.index<br>rw_preds = rw_preds.set_index(&#39;date&#39;)<br>rw_preds.columns = [&#39;predictions&#39;]<br><br># Get CI <br>model_pred_ci = model.predict(y_test.shape[0], return_conf_int=True)<br>pred, ci = model_pred_ci<br>lower = list(list(zip(*ci))[0])<br>upper = list(list(zip(*ci))[1])<br><br># Make plot of predictions<br>plt.plot(y_train,label=&quot;Training&quot;)<br>plt.plot(y_test,label=&quot;Testing&quot;)<br><br>plt.plot(rw_preds[&#39;predictions&#39;],label=&quot;Predicted&quot;)<br>plt.fill_between(y_test.index, lower, upper, color=&#39;blue&#39;, alpha=0.3)<br><br>plt.legend(loc = &#39;upper left&#39;)<br>plt.title(&#39;Predictions using Random Walk Model&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*Re5AW-k1vxHIh21j5D57TA.png" /></figure><pre># Obtain predictions from ARMA(1,0) model on X_t<br>ar_preds = pd.DataFrame(ar_fit.forecast(y_test.shape[0]))<br>ar_preds[&#39;date&#39;] = y_test.index<br>ar_preds = ar_preds.set_index(&#39;date&#39;)<br>ar_preds.columns = [&#39;predictions&#39;]<br><br># Get CI <br>ar_pred_ci = ar_fit.get_forecast(y_test.shape[0]).conf_int()<br>ar_pred_ci[&#39;date&#39;] = y_test.index<br>ar_pred_ci = ar_pred_ci.set_index(&#39;date&#39;)<br># Make plot of predictions<br>plt.plot(y_train,label=&quot;Training&quot;)<br>plt.plot(y_test,label=&quot;Testing&quot;)<br>plt.plot(ar_preds[&#39;predictions&#39;],label=&quot;Predicted&quot;)<br>plt.fill_between(ar_pred_ci.index, ar_pred_ci.iloc[:,0], ar_pred_ci.iloc[:,1], color=&#39;blue&#39;, alpha=0.3)<br>plt.legend(loc = &#39;upper left&#39;)<br>plt.title(&#39;Predictions using ARMA(1,0)&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*vu_YYr-pnEXgGMHaauuqgg.png" /></figure><h3>Performance Metrics (MAPE and MSE)</h3><p>To assess model performance, we use two metrics:</p><ul><li>Mean Average Percent Error (MAPE)</li><li>Mean Squared Error (MSE)</li></ul><p>MAPE is commonly used in time series data and calculated by taking the average percent difference between the predicted and actual values. MAPE provides benefits such as providing a more intuitive understanding of model performance and being a relative rather than absolute metric of comparison. Mathematically, it is calculated as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/236/0*Qw6ObytWiqmP9dny" /></figure><p>MSE is another common performance metric which is an absolute metric of comparison that magnifies the impact of outliers — in this context, predictions far from actual values — due to the squared term. One benefit of MSE is that it remains in the same units (albeit squared) of the original forecasted value. It is calculated as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/0*rjgLkI-UqjCZ0no4" /></figure><pre>data = {&#39;MAPE&#39;: [mean_absolute_percentage_error(y_test,ar_preds[&#39;predictions&#39;]) * 100, mean_absolute_percentage_error(y_test,rw_preds[&#39;predictions&#39;]) * 100],<br>        &#39;MSE&#39;: [mean_squared_error(y_test,ar_preds[&#39;predictions&#39;]), mean_squared_error(y_test,rw_preds[&#39;predictions&#39;])]}<br><br>df = pd.DataFrame(data, index = [&quot;AR1&quot;, &quot;RW&quot;])<br>df</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*go3cIoQA4gXYLpfk3BKrug.png" /></figure><p>We find for the ARMA(1,0) and the random walk model mean average percentage errors (MAPE) of 5.56 and 6.29, indicating that, on average, the predictions have an error of 5.56% and 6.29%, respectively, relative to the actual values. Additionally, MSE values of 391.28 and 355.30 suggests that, on average, the predictions deviate from the actual values by approximately 391.28 dollars squared.</p><h3>Conclusion</h3><p>There we have it — an introduction to the ARMA modeling process. We validated a commonly agreed upon finding that financial asset data can be adequately modeled as a random walk process by building our understanding of the math, assumptions and model selection process behind ARMA models.</p><p>ARMA is the building block of many classical time series approaches. From here, we can layer on many other components to include seasonality, model the variance rather than the mean, and even include many time series process in parallel. I hope you enjoyed and return for further parts of this series!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lNf4JTW95cq7Upbs" /><figcaption>Stock Exchange Board: Pixabay</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ceb8b52fdd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>