Coding the GARCH Model : Time Series Talk

ritvikmath · Intermediate ·🚀 Entrepreneurship & Startups ·6y ago
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 Math Team Update
Math Team Update
ritvikmath
2 Single Variable Calculus Volume of a Sphere - Proof 1
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
3 Single Variable Calculus Volume of a Sphere - Proof 2
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
4 Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
5 Multivariable Calculus Volume of a Sphere Proof - Double Integrals
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
6 The Euclidian Algorithm
The Euclidian Algorithm
ritvikmath
7 Proving the Chain Rule
Proving the Chain Rule
ritvikmath
8 Proving the Fundamental Theorem of Calculus Part 1
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
9 Proving the Fundamental Theorem of Calculus Part 2
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
10 Math Puzzle - Poison Perplexity
Math Puzzle - Poison Perplexity
ritvikmath
11 Math Puzzle - Poison Perplexity - Solution
Math Puzzle - Poison Perplexity - Solution
ritvikmath
12 Expected Value and Variance of Continuous Random Variables (Calculus)
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
13 Expected Value and Variance of Discrete Random Variables (No Calculus)
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
14 Array Method
Array Method
ritvikmath
15 Complex Power Series and their Derivatives
Complex Power Series and their Derivatives
ritvikmath
16 Distributions - Intro
Distributions - Intro
ritvikmath
17 The Poisson Distribution
The Poisson Distribution
ritvikmath
18 The Bernoulli Distribution
The Bernoulli Distribution
ritvikmath
19 The Binomial Distribution
The Binomial Distribution
ritvikmath
20 The Continuous Uniform Distribution
The Continuous Uniform Distribution
ritvikmath
21 The Geometric Distribution
The Geometric Distribution
ritvikmath
22 The Triangular Distribution
The Triangular Distribution
ritvikmath
23 The Exponential Distribution
The Exponential Distribution
ritvikmath
24 The Borel Distribution + Notes on Poisson Distribution
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
25 The Gamma Distribution
The Gamma Distribution
ritvikmath
26 The Normal Distribution
The Normal Distribution
ritvikmath
27 The Laplace Distribution
The Laplace Distribution
ritvikmath
28 The Chi - Squared Distribution
The Chi - Squared Distribution
ritvikmath
29 Overfitting
Overfitting
ritvikmath
30 Vector Norms
Vector Norms
ritvikmath
31 Truths Behind the Titanic : K-Nearest Neighbor
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
32 The Mathematics of Breakups
The Mathematics of Breakups
ritvikmath
33 Sillyfish
Sillyfish
ritvikmath
34 Finding Optimal Paths - Dynamic Programming
Finding Optimal Paths - Dynamic Programming
ritvikmath
35 HowToDataScience : Scraping Twitter Data
HowToDataScience : Scraping Twitter Data
ritvikmath
36 Decision Trees
Decision Trees
ritvikmath
37 Perceptron
Perceptron
ritvikmath
38 Naive Bayes
Naive Bayes
ritvikmath
39 K-Nearest Neighbor
K-Nearest Neighbor
ritvikmath
40 Evaluating Machine Learning Models
Evaluating Machine Learning Models
ritvikmath
41 Decision Tree Pruning
Decision Tree Pruning
ritvikmath
42 K-Means Clustering
K-Means Clustering
ritvikmath
43 Gaussian Mixture Model
Gaussian Mixture Model
ritvikmath
44 Data Science - Fuzzy Record Matching
Data Science - Fuzzy Record Matching
ritvikmath
45 Time Series Talk : Autocorrelation and Partial Autocorrelation
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
46 Time Series Talk : Autoregressive Model
Time Series Talk : Autoregressive Model
ritvikmath
47 Time Series Talk : Moving Average Model
Time Series Talk : Moving Average Model
ritvikmath
48 Time Series Talk : ARMA Model
Time Series Talk : ARMA Model
ritvikmath
49 Time Series Talk : ARCH Model
Time Series Talk : ARCH Model
ritvikmath
50 Time Series Talk : White Noise
Time Series Talk : White Noise
ritvikmath
51 Time Series Talk : Stationarity
Time Series Talk : Stationarity
ritvikmath
52 Time Series Talk : ARIMA Model
Time Series Talk : ARIMA Model
ritvikmath
53 Time Series Talk : Lag Operator
Time Series Talk : Lag Operator
ritvikmath
54 Time Series Talk : What is Seasonality ?
Time Series Talk : What is Seasonality ?
ritvikmath
55 Time Series Talk : Seasonal ARIMA Model
Time Series Talk : Seasonal ARIMA Model
ritvikmath
56 So ... What Actually is a Matrix ? : Data Science Basics
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
57 Derivative of a Matrix : Data Science Basics
Derivative of a Matrix : Data Science Basics
ritvikmath
58 Basics of PCA (Principal Component Analysis) : Data Science Concepts
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
59 Eigenvalues & Eigenvectors : Data Science Basics
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
60 The Covariance Matrix : Data Science Basics
The Covariance Matrix : Data Science Basics
ritvikmath

Related Reads

📰
The Luxury Tax of Being Unique: Why Your Success Today Comes at the Cost of Hate
Learn to navigate reputational risk and turn criticism into fuel for growth while preserving your authentic voice
Medium · AI
📰
How to Choose the Right Business Operating System for Your Company
Learn how to choose the right business operating system for your company to solve organizational problems and drive growth
Medium · Startup
📰
The Two-Man Team
Learn how a two-man team can be a powerful unit for achieving success, with lessons from the author's personal experiences and biblical inspiration
Medium · Startup
📰
When Partners Become Competitors: Business Takeaways From Apple OpenAI Lawsuit
Learn vital business lessons from the Apple OpenAI lawsuit on protecting intellectual property and employee expertise when partners become competitors
Forbes Innovation
Up next
Watch this before applying for jobs as a developer.
Tech With Tim
Watch →