Coding the GARCH Model : Time Series Talk
Skills:
ML Pipelines90%
Key Takeaways
Covers coding a GARCH model in Python using the arch model library for time series analysis
Full Transcript
hey guys welcome back so we're finally gonna get into that video about how do you code a GARCH model in Python so the only really new library I would say you'd need is this one called arch model so although it's called arch model it's gonna allow us to model any type of GARCH process so if you don't have that you can go ahead and just do pip install arch model and you should be able to get that now just as a quick theoretical reminder before we go into the code this is what a GARCH to to model which is the simulated process we'll be creating in just a moment looks like the reason it's a GARCH tutu is because first off we're modeling the time series which here's a sub T based on two lags priors so a sub t minus 1 and a sub T minus 2 that's why there's a two in this position for GARCH two two and the second two in the order of the process comes from the fact that the time series is also a function explicitly of its volatility from last period here Sigma sub T minus one and two periods ago right here which is Sigma sub T minus two so the whole story the guard's process remember is telling is that the time series is a function of not only its own lagged values from whatever periods in the past but also its volatility or standard deviation or just colloquially how jumpy it is from some periods in the past as well now if you've seen my theoretical video on GARCH you'll notice that the names of the parameters are slightly different for example I'm using Omega as the constant alphas here and betas here but that's only because the output of this software package arch model is going to give those as the names of the parameters I want to keep my equation consistent so it's easier for us to understand alright so let's go ahead and first simulate some GARCH data I didn't want to use a real data set here because I wanted to see if we could get exactly those parameters back when we simulate and then try to recover those parameters using the arch model package so I'll just quickly walk through how do I create the data so I say I want a thousand periods of data we can consider these as days or weeks months whatever here's me in putting all of my parameters so I've chosen Omega the constant here as 0.5 alpha 1 will be 0.1 alpha 2 will be 0.2 beta 1 3 and beta 2.4 so we're gonna see how close to these we can get when we fit a car ch2 2 model to this data so I go ahead and just create it literally based on the equation above so although this may look complicated you see that the form is literally Omega plus alpha 1 times this series one period ago squared and you can read the equation and it's literally taken from this mathematical formulation right here alright so when I do that this is what my simulated GARCH 2 2 data looks like before even doing any more formal testing you can already tell that this may be a good candidate for the GARCH model because there's periods in here of high volatility such as from 400 to roughly 600 there's a very high volatility here versus periods of much lower volatility for example here maybe 550 u is a area of lower volatility now let's actually plot that volatility to see where it's the highest and where it's the lowest usually you cannot explicitly do this but because we have simulated a process we had the volatility at every time period specifically the volatility is given by this part that's the square root excluding this epsilon sub T so this is given this is the volatility again that's coming straight from the theoretical GARCH video that I have a video for so we see the volatility looks like this and it's really best if we plot the volatility and the series on the same plot which is what I do here and you can see that the red volatility is highest exactly when the blue series is jumping around the most so we see that matches up really well now if we were just given this data and we didn't know what was the generating process how would we know that we should start with a GARCH to to model well there's a lot of ways but here's kind of a heuristic that we already know about which is the P ACF plot recall that P AC f stands for partial autocorrelation function and let's think about what happens if I were to look at the square of the data I've generated so that is the square of a sub T without really getting to the math here that's going to square the left hand side and the right hand side which means that the square of my series will be some function of the 1 lagged square of my series and the 1 lagged and the two-legged square of my series so I should see a spike in the PD CF at one and a spike in the PA CF at two and it should sort of shut off after that let's see if that's what I see indeed that is sort of what I see we see this spike at one pretty high spike it - pretty high and then it sort of shuts off of course the spike at three and four are above the windows here so you can make an argument for considering them but an argument for not considering them is that after two it shuts off rather rapidly here so you could potentially start with a GARCH four or something but we'll just start with a guards to process here so that's our reasoning for starting with a guards - to process now we're going to split up our data into a training and testing set so notice we generated a thousand periods of data we're going to be keeping a hundred of them at the end for testing and we're using 900 for training so that's just what I do here now fitting the model is as always pretty simple but that's not really the part that's going to make you a good data scientist or economist it's going to be really knowing what is the underlying math behind this model so to actually fit the model we create a model object from the arch model library putting the training data and putting in the lags so P equals 2 and Q equals 2 so this is a GARCH - to process I'll just note here that of course this is also good for making an arch process you simply just make u equal to 0 now you have some kind of arch process now we go ahead and fit it by just doing model dot fit and we store that in a model variable we see lots of things getting printed out this is just some stuff about convergence so now we can look at what the estimated parameters are by doing model fit summary and there's a lot of good information here but let's go right down to the bottom where it tells us what the estimated coefficients are keep in mind that Omega was supposed to be 0.5 so it's estimated as point 67 a little bit off but not very very off and it is significant which means that we should keep it so that's correct and on that note it says that we should keep each one of our coefficients which is correct because they were literally used to generate the process in the first place so it's good news that we see that fact again when we fit the model going back to the actual values of the coefficients alpha one was 0.1 estimated as 0.15 not to off alpha two was point two estimated just pretty much around that value beta 1 was 0.3 pretty close here with the point 27 and beta 2 was supposed to be 0.4 again we get pretty close here so we see that it's not getting exactly the parameters used to generate the process but I would consider this a success in terms of how close they are and more importantly the fact that they're all predicted to be relevant in our final model of course all that's left now is to try and predict the volatility so the whole point of GARCH and arch is to predict the volatility of your time series in future time periods the reason being that if you think the time series is going to be really volatile next week that might affect your decisions for example for buying the stock or whatever nature of your data you're looking at so to predict stuff again it's pretty straightforward you take your model fit which was the result of fitting the model here and you go ahead and do forecast horizon is equal to some number this is basically how many periods in advance you would like to predict the volatility for and you can look at this code for how to actually plot it you see that this orange line is the predicted volatility of the data on our hundred days of testing and the blue line here is the actual volatility we see it's surprisingly very good of course there is a period at which it stops being good but that's pretty far in for example even up until maybe 50 so maybe about half of the testing set it's doing a pretty good job of generally capturing the volatility increasing from zero to 50 now just this experiment let's see what happens if we predict way way in advance as we know most time series predictions if you start predicting way in advance the model prediction is just going to degenerate into something not very useful and it's no different here if we predict a thousand periods in advance we see that for the first 50 it was pretty strong then it becomes weak and then it kind of just levels off to some constant value so that's pretty expected and as the last thing we do in this video let's look at the rolling forecast origin as a refresher we've looked at this in a couple videos but this was the prediction of volatility of all hundred future periods at once but usually if you're using some kind of prediction in practice you're going to predict just one or two periods in events and then once those one or two periods data become available then you'll include those in your prediction and then predict the next one or two periods so I won't go through the code of rolling forecasts origin but we see that if we predict only one period in advance instead of a hundred periods in advance all of this all at once we get a very very very strong prediction of volatility so we see here blue is the true volatility and orange is the predicted volatility if we're only ever predicting one period in advance so we see that this GARCH model does a very very good job on this data and that should be somewhat expected because the GARCH - to model what was is what was used to generate the data in the first place all right so hopefully this helps you to get an idea of a how to generate GARCH data B how to figure out what order of GARCH model you might want to use and see how to finally fit the model and make some relevant predictions on your data so in a next video we'll be looking at GARCH actually on some real data we'll be looking at some stock forecasting all right so the code will be available in the description below and until next time
Original Description
All about coding the GARCH Model in Time Series Analysis!
Code used in this video:
https://github.com/ritvikmath/Time-Series-Analysis/blob/master/GARCH%20Model.ipynb
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ritvikmath · ritvikmath · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Math Team Update
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
The Euclidian Algorithm
ritvikmath
Proving the Chain Rule
ritvikmath
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
Math Puzzle - Poison Perplexity
ritvikmath
Math Puzzle - Poison Perplexity - Solution
ritvikmath
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
Array Method
ritvikmath
Complex Power Series and their Derivatives
ritvikmath
Distributions - Intro
ritvikmath
The Poisson Distribution
ritvikmath
The Bernoulli Distribution
ritvikmath
The Binomial Distribution
ritvikmath
The Continuous Uniform Distribution
ritvikmath
The Geometric Distribution
ritvikmath
The Triangular Distribution
ritvikmath
The Exponential Distribution
ritvikmath
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
The Gamma Distribution
ritvikmath
The Normal Distribution
ritvikmath
The Laplace Distribution
ritvikmath
The Chi - Squared Distribution
ritvikmath
Overfitting
ritvikmath
Vector Norms
ritvikmath
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
The Mathematics of Breakups
ritvikmath
Sillyfish
ritvikmath
Finding Optimal Paths - Dynamic Programming
ritvikmath
HowToDataScience : Scraping Twitter Data
ritvikmath
Decision Trees
ritvikmath
Perceptron
ritvikmath
Naive Bayes
ritvikmath
K-Nearest Neighbor
ritvikmath
Evaluating Machine Learning Models
ritvikmath
Decision Tree Pruning
ritvikmath
K-Means Clustering
ritvikmath
Gaussian Mixture Model
ritvikmath
Data Science - Fuzzy Record Matching
ritvikmath
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
Time Series Talk : Autoregressive Model
ritvikmath
Time Series Talk : Moving Average Model
ritvikmath
Time Series Talk : ARMA Model
ritvikmath
Time Series Talk : ARCH Model
ritvikmath
Time Series Talk : White Noise
ritvikmath
Time Series Talk : Stationarity
ritvikmath
Time Series Talk : ARIMA Model
ritvikmath
Time Series Talk : Lag Operator
ritvikmath
Time Series Talk : What is Seasonality ?
ritvikmath
Time Series Talk : Seasonal ARIMA Model
ritvikmath
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
Derivative of a Matrix : Data Science Basics
ritvikmath
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
The Covariance Matrix : Data Science Basics
ritvikmath
More on: ML Pipelines
View skill →Related Reads
📰
📰
📰
📰
The Luxury Tax of Being Unique: Why Your Success Today Comes at the Cost of Hate
Medium · AI
How to Choose the Right Business Operating System for Your Company
Medium · Startup
The Two-Man Team
Medium · Startup
When Partners Become Competitors: Business Takeaways From Apple OpenAI Lawsuit
Forbes Innovation
🎓
Tutor Explanation
DeepCamp AI