18: Time Series
18.4 Use GARCH for better volatility modeling - Video Tutorials & Practice Problems
<v Voiceover>One drawback of ARIMA models</v> is that they don't always handle varying volatility so well. To make up for this, we've come up with GARCH models. This is a problem that often occurs in financial data, so we're going to look at AT&T stock data to illustrate GARCH models. To pull the data, we will use a package called "quantmod." It's a great package with all sorts of financial tools. What we will do is, "att <- getSymbols," and we will use the symbol "T" for AT&T. Say, "auto.assign=FALSE)." That's an old thing they used to do. You would actually call it without using the assignment operator, and it would automatically put a variable into your workspace. They're phasing that out, and we just don't like it anymore. In fact, they even get you a warning telling you they're phasing out that old method. It also gives you a warning about not being able to have the right download life. Don't worry, it worked. What this did, it went to Yahoo and pulled down the stock data. What we should do now is, "require(xts)." This is an advanced time series package. Now we can look at the AT&T data. We can see it has the opening, the high, the low, the close, the volume, and the adjusted. All these dates going back to January 3, 2007. If we want, we can plot this very simply with the built in plotting functionality, and it actually makes a very nice graph. You look at this, and it only plotted one of the series, because it's only showing you the pertinent one. We see this. It's a nice looking plot, and we see there is quite a bit of volatility and it's moving around quite a bit. One other nice feature they have in this package, in case you're used to Bloomberg terminals, they let you sort of recreate that, but in case that wasn't enough, you can also do, "addBBands," and that adds even more information. Then if you need a little bit more, you can add, "addMACD(32, 50, 12), and these are just some parameters. Now we look at it. It's the same chart, but it just has all these extra features. It shows volume. It shows upper and low. This is something you might see on CNBC or Bloomberg TV. For now, we are just going to focus on the closing price. We're going to say, "attClose <- att$T.Close." If we look at its class, we can see that as an xts and a zoo object. There's a standard TS object NR. Zoo is a better TS object, and xts is an extension of zoo. If we look at the head of AT&T close, we can see we have the time series data, and we still have the date data. Now, a GARCH Model is actually a fairly complicated model. It is estimated as such. Epsilon of T is the noise, and that is modeled as Sigma T times E of t. E of t is assumed to be generalized white noise. It's random data, random errors. Sigma T is formulated as this long equation. Don't worry about it. These are ... The Alphas and Betas are parameters for orders of the GARCH Model. It's important to understand when you're fitting these on a daily basis, but for now let's let R do all of the work. There are a number of packages out there. Right now, the best seems to be RU GARCH, so we'll load that up. This requires a few steps from us. The first thing is, we need to build a spec. This is where we're specifying the model. In order to build a GARCH Model, we first need to make a specification. You'll say, "ugarchspec" gets on the variance model. Remember, GARCH is really modeling the variance, the volatility. We're going to specify that the model should be in sGARCH, and the garchOrder should be a 1, 1. The mean model, which is ... This is focusing on the trend, not the volatility, is going to be an ARMA model, so I'll have "armaOrder" "1, 1." One auto-regressive component, one moving average component. We're going to say the distribution model is "std." We run this, and it just builds a little spec object for us. Now we're ready to fit the model itself. We will say, "attGarch." Spec is the ATT spec we just created, and the data will be the ATT close data. I try to use attGarch as if it was a function, when I meant to be assigning that as a variable. The function itself is, "ugarchfit." "Ugarchfit" takes in a specification, takes in the data, and returns a GARCH Model. If we look at it, it spits out a lot of information, so much so we have to scroll off the screen. The most important information spit out by this model are the criteria such as AIC and BIC and the optimal parameters. These are our coefficients. It's for the average. It's for the AR component, the MA component, and for the GARCH parameters. These are what make up the model. To see how well we did, we would like to plot the residuals of this model. Ideally, we want white noise. There are some. Let's zoom in. It does look somewhat like white noise, but we can tell that the variance is changing at different points on the time series. We might want to do a little bit more modeling, but for now we will continue moving on to other aspects of this. There is a lot of diagnostic information available to us, such as an ACF of the residuals. That will be, "plot(attGarch," we will say, "which = 10)," and that automatically gives us this nice plot. If we wanted to, we could have just wrote, "plot(attGarch)" and it would then allow us to choose which one we wanted. For instance, we could have done 2, to see 1% of the AR limits. We have all these options to us. For now, we will just exit out. Of course, with time series predictions are possible, and this is done with a uGarch boot function. We'll say, "attPred <- ugarchboot." It takes the GARCH Model, and we'll say, "predict 50 states ahead." We're dealing with daily data. Predicting out 50 days isn't going to hurt us too much. We are going to say, "method=c("Partial," and back up to full. We'll say go for the first one. We run this, and now we can say, "plot(attPred, which=2)." This gives us our predictions for the future. Unlike the forecast plot method, which gave us the past, this just gives us the prediction for the future of where it's heading along with the confidence intervals. Being this is financial data, we might consider working with log returns. That is the log of today minus the log of yesterday, because those should be more independent. To do this, we will first create a new time series called attLog and this gets the diff(log(attClose)), and we don't want to take the first one, because the first one will be NA. Now we can build a specification for this model. We will call it, "attLogSpec." We need to make sure we spell variance correctly. The variance model again will just be an sGARCH, and the garch order will be (1, 1). The mean model will be another ar (1, 1). The distribution model will once again be std. Now we actually fit the model. "AttLogGarch <- ugarchfit(spec=attLogSpec," and the data is attLog. It is Spec with a lower case "P." Models fit. Now we want to compare this to the previous model we had, attGarch, so we will check out the info criteria of attGarch to get its AIC, wchich is .99. The infocriteria for attLogGarch is -5.89. That is a significant drop in AIC, meaning modeling on these log returns did a much better job, which makes sense as they are likely to be more independent. It's important to remember that the purpose of GARCH Models is not to fit the signal better, but to better capture the volatility. Volatility, especially in finance is incredibly important, because that represents the risk. If you can capture and model the risk better, you can see what you're doing, and that is where GARCH comes in. 0