Moving Average Process - Applied Time Series Analysis in Python and TensorFlow

Data Science with Marco · Intermediate ·💰 FinTech & AI for Finance Professionals ·5y ago

Key Takeaways

The video covers the Moving Average (MA) process, a forecasting model that uses past forecast errors to predict the next point in time, and demonstrates how to simulate and model an MA process of order 2 in Python using the Statsmodels library.

Full Transcript

all right now let's cover the moving average model the moving average model is used for forecasting it uses the past forecast errors to predict the next point in time we refer to the moving average model as the maq model where q is the order mathematically the moving average model is expressed like this where z is white noise and theta is some weight in the case of a moving average process of order two we would express it like this with parameters going only to t minus two here we see a simulation of a moving average process of order two we will do the same in python in the next lesson again just by looking at the time series it is difficult to say anything about it you can even argue that you can't recognize that it is an ma2 process just by looking at the plot therefore we plot the acf function what do you notice well it seems that after lag two the autocorrelation is not significant anymore so now you know that we can use the acf plot to estimate the order q of a moving average model after like q the auto correlation should not be significant anymore alright now let's run some simulations in python and see this for ourselves alright so with the theory of the moving average process covered we are now ready to apply it in python and i will start off by importing the libraries that we will need for this project so from statsmodels.graphics.tsa let's import plot acf then from statsmodels.tsa.arima process we will import arma process and from statsmodels.tsa.eremy.model we will import our rima we will also need from stats models dot tsa dot stat tools we will need acf and of course matplotlib dot pi plot as plt and numpy as np and finally the jupiter magic matplotlib inline awesome once this is done i will set the figure size for this notebook so plt.rc params figure dot fix size and i will set it equal to 10 by seven and a half awesome and now we are ready to start off by simulating a moving average process of order two so i will quickly right here simulate ma2 process and the process will have the following equation so it's going to be that y at time t will be equal to 0.9 z at time t minus 1 and then we will add 0.3 z at time t minus so as you can see because we have t minus one and t minus two we then have a process of order two awesome and so to simulate it we first need to define both an ma2 array and an ar2 array i know we haven't talked yet about ar the auto regressive portion but it will be the subject of the following lesson so for now let's define our ma2 array which is the array containing the coefficients for the lags for the moving average process so ma2 is going to be a numpy array and then we start off with the coefficient at lag zero now as you can see at t minus one the coefficient is zero point nine t minus two the coefficient is zero point three so at lag zero t minus zero or t well the lag is equal to one and technically the coefficient at lag zero is almost exclusively equal to one so the array should always start with one and then we move on so the coefficient at lag zero will be zero point nine and coefficient at lag two is 0.3 and those are the coefficients for our moving average process and now for the purpose of simulation we also need to specify the ar process in this case we do not have an ar process and so the array will be equal to zero so numpy array however the coefficient that lag zero is always one okay so one here but then we put zero and zero and this way we say that there is no auto regressive portion here perfect and now you can quickly print both arrays just to show what it looks like and no surprise here those are simply two arrays uh the first one with our m a process the coefficient so one zero point nine zero one three and there are one one zero zero for the ar process and in this case is going to be null because we have zero uh for both lags one and two now we are ready to simulate the process so ma2 process is going to be equal to arma process pass in both arrays so ar2 first and then me2 and then we will generate some samples so generate sample and specify the number of samples in this case i will set it to a thousand awesome and with this done we can plot our simulation so plt.plot the ma2 process let's give it a title so plt.title will be the moving average process of order two and we are ready to show the plot and you should get something similar to this again it might differ a bit in your case because there is some difference in initialization now this is a bit hard to see that it is an ma2 process right there are a lot of samples here it almost looks like white noise to us so let's just zoom in on a part of the graph so i will say that plt.plot uh ma2 process uh i will still give it the same title so plt dot title moving average process of order two and now i will set a limit on the x-axis so only between 0 and 200 that way we can zoom a little bit on the plot and now you should get the following so as you can see a bit clearer how the time series is behaving awesome and now let's test the acf so as you know if the acf of an ama process purely am a process we should not see significant peaks after the order so that means that if we plot the autocorrelation function now we should not see a significant peak after lag two because we are simulating an m a process of order two so let's check that for ourselves let's plot the acf of our ma2 process and i will specify that the maximum number of lags here to be equal to 20 and semicolon and you should get the following now as you can see at lag zero the it's always equal to one so that's expected that's okay and then lag one lack two our significance but after that everything is uh non-significant because everything is in this light blue area so as expected we are indeed in the presence of an ma2 process right because there are no significant peaks after lag two awesome now let's try to model our simulation to see if we can get back the parameters that we set up at first so we are going to model it and see if we can get back 0.9 and 0.3 all right so going back down i will say that the ma model is going to be equal to arima and i will pass in the data of the ma2 process and then the order is going to be equal to 0 0 and 2. i know this is something that we haven't seen just yet but for now let's just say that zero here means for the ar process so we don't have an ar process here so it's going to be equal to zero this is order of differencing we haven't seen this yet so we set it to zero as well and this is the order of the m a process and because we know we have simulated an m a process of order two we can specify here that we want a an equation a model right of order two for dma process and then enforce stationarity i will set it equal to false and then we are going to fit the model right away and then let's print out a summary of the model so m a model dot summary and let's run this cell and you should get the following so as you can see uh we have our coefficients here so for the lag 1 we get 0.92 and for lag two we get 0.29 so still very close right we have 0.9 and 0.3 and the model gives us 0.92 and 0.297 awesome and so that's it for the simulation of an ma2 process so thank you very much for taking this free preview with me as always there is a link in the description below if you want to take the full course the link will have a promo code applied to it already so you can click on the link and you'll get the course with 87 off and if by any chance you click on the link and the promo code has expired feel free to send me an email it will also be in the description and i will send you a coupon code so that you get the course on sale so thank you very much and i'll see you on the next one

Original Description

👉 Get the course at 87% off: 📚 Link to the notebook: https://github.com/marcopeix/AppliedTimeSeriesAnalysisWithPython/blob/main/HOTSAP_MA.ipynb Email me for a coupon if the one above expired: peixmarco@gmail.com ----------------------------------- Let’s cover the moving average model. The moving average model is used for forecasting. It uses the past forecast errors to predict the next point in time. We refer to the moving average model as the MA(q) model, where q is the order. Here, we see a simulation of a moving average process of order 2. We will do the same in Python Then, we plot the ACF function. What do you notice? It seems that after lag 2, the autocorrelation is not significant anymore. Now you know that we can use the ACF plot to estimate the order q of a moving average model! After lag q, the autocorrelation should not be significant anymore. Alright, now let’s run some simulations in Python and see this for ourselves!
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Data Science with Marco · Data Science with Marco · 14 of 38

1 Linear Regression in Python | Data Science with Marco
Linear Regression in Python | Data Science with Marco
Data Science with Marco
2 Classification in Python | logistic regression, LDA, QDA | Data Science With Marco
Classification in Python | logistic regression, LDA, QDA | Data Science With Marco
Data Science with Marco
3 Resampling and Regularization | Data Science with Marco
Resampling and Regularization | Data Science with Marco
Data Science with Marco
4 Decision Trees | Data Science with Marco
Decision Trees | Data Science with Marco
Data Science with Marco
5 Suppor Vector Machine (SVM) in Python | Data Science with Marco
Suppor Vector Machine (SVM) in Python | Data Science with Marco
Data Science with Marco
6 Unsupervised Learning | PCA and Clustering | Data Science with Marco
Unsupervised Learning | PCA and Clustering | Data Science with Marco
Data Science with Marco
7 Data Science Portfolio Project: Regression #1 | Data Science with Marco
Data Science Portfolio Project: Regression #1 | Data Science with Marco
Data Science with Marco
8 Data Science Portfolio Project: Regression #2 | Data Science with Marco
Data Science Portfolio Project: Regression #2 | Data Science with Marco
Data Science with Marco
9 What Are Time Series - Applied Time Series Analysis in Python and TensorFlow
What Are Time Series - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
10 Basic Statistics - Applied Time Series Analysis in Python and TensorFlow
Basic Statistics - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
11 Autocorrelation and White Noise - Applied Time Series Analysis in Python and TensorFlow
Autocorrelation and White Noise - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
12 Stationarity and Differencing - Applied Time Series Analysis in Python and TensorFlow
Stationarity and Differencing - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
13 Random Walk Model - Applied Time Series Analysis in Python and TensorFlow
Random Walk Model - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
Moving Average Process - Applied Time Series Analysis in Python and TensorFlow
Moving Average Process - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
15 Autoregressive Process - Applied Time Series Analysis in Python and TensorFlow
Autoregressive Process - Applied Time Series Analysis in Python and TensorFlow
Data Science with Marco
16 ARMA Model - Time Series Analysis in Python and TensorFlow
ARMA Model - Time Series Analysis in Python and TensorFlow
Data Science with Marco
17 What is data science?
What is data science?
Data Science with Marco
18 Answering DATA SCIENCE questions #1 - Why learn SQL when Python and R exist?
Answering DATA SCIENCE questions #1 - Why learn SQL when Python and R exist?
Data Science with Marco
19 R vs Python in the Industry - Data Science Q&A #datascience #datasciencecareer #careeradvice
R vs Python in the Industry - Data Science Q&A #datascience #datasciencecareer #careeradvice
Data Science with Marco
20 Data science or data engineering - which is best for you? #datascience #datasciencecareer
Data science or data engineering - which is best for you? #datascience #datasciencecareer
Data Science with Marco
21 Where to find data for data science projetcs? #datascience #datasciencecareer
Where to find data for data science projetcs? #datascience #datasciencecareer
Data Science with Marco
22 Data science certificates on resume? #datascience #datasciencecareer #careeradvice
Data science certificates on resume? #datascience #datasciencecareer #careeradvice
Data Science with Marco
23 Should you aim for data science or data engineering? | Data Science Q&A #1
Should you aim for data science or data engineering? | Data Science Q&A #1
Data Science with Marco
24 Don't waste time on this | #datascience #datasciencecareer
Don't waste time on this | #datascience #datasciencecareer
Data Science with Marco
25 Low-code AI tools - are they good? | #datascience #datasciencecareer #careeradvice
Low-code AI tools - are they good? | #datascience #datasciencecareer #careeradvice
Data Science With Marco
26 How to grow as a data scientist after 2+ years of experience? #datascience #datasciencecareer
How to grow as a data scientist after 2+ years of experience? #datascience #datasciencecareer
Data Science with Marco
27 Transition into DATA SCIENCE without a masters or bootcamp #careertransition
Transition into DATA SCIENCE without a masters or bootcamp #careertransition
Data Science With Marco
28 How to improve your data science profile?
How to improve your data science profile?
Data Science With Marco
29 How to learn Python for data science?
How to learn Python for data science?
Data Science With Marco
30 Does Scrum/Agile work for data science?
Does Scrum/Agile work for data science?
Data Science With Marco
31 What are the major roles in analytics and how to choose?
What are the major roles in analytics and how to choose?
Data Science with Marco
32 Thoughts and advice for a live SQL coding round
Thoughts and advice for a live SQL coding round
Data Science With Marco
33 Data science interview question: difference between type 1 and type 2 error
Data science interview question: difference between type 1 and type 2 error
Data Science With Marco
34 Feature selection in machine learning | Full course
Feature selection in machine learning | Full course
Data Science With Marco
35 Anomaly detection in time series with Python | Data Science with Marco
Anomaly detection in time series with Python | Data Science with Marco
Data Science With Marco
36 Podcast - TimeGPT, predicting the future, and more
Podcast - TimeGPT, predicting the future, and more
Data Science With Marco
37 Big announcement - Revealing my new book
Big announcement - Revealing my new book
Data Science With Marco
38 Get Started in Time Series Forecasting in Python | Full Course
Get Started in Time Series Forecasting in Python | Full Course
Data Science With Marco

The Moving Average process is a forecasting model that uses past forecast errors to predict the next point in time. This video demonstrates how to simulate and model an MA process of order 2 in Python using the Statsmodels library.

Key Takeaways
  1. Import necessary libraries
  2. Simulate an MA process of order 2
  3. Plot the simulated time series
  4. Calculate and plot the autocorrelation function
  5. Model the MA process using ARIMA
💡 The autocorrelation function can be used to estimate the order of an MA process

Related Reads

📰
Build a Remittance App with the Afriex Business API
Learn to build a remittance app using the Afriex Business API for seamless cross-border transactions
Dev.to · Victory Lucky
📰
Why FinTech Is Growing So Fast — And What the Next 10 Years Could Look Like
FinTech is growing rapidly, enabling fast and convenient financial transactions, and is expected to continue shaping the financial landscape over the next decade
Medium · Startup
📰
$100,000 in Support. Four Weeks. One Goal — Turn African Fintech Founders Into Operators. Bankiffy Fintech Accelerator BFA 2026 Cohort 1 Is Now Open
Learn how the Bankiffy Fintech Accelerator supports African fintech founders in turning their ideas into operational businesses
Techpoint Africa
📰
Nigeria’s fintechs built payments. Now they’re becoming banks.
Nigeria's fintechs are transitioning into banks by obtaining MFB licenses to diversify revenue streams beyond transaction fees
TechCabal
Up next
Can Nepal bridge its digital financial divide? CellPay has a plan.
AI InterConnect
Watch →