Path: blob/master/Time Forecasting using Python/1.2 Understanding Moving Average and Its Implementation.ipynb
3074 views
Moving average forecast is a simple and widely used method for time series forecasting. It works by taking the average of a subset of the most recent data points to make predictions for future values.
The moving average method is a simple yet effective technique used for smoothing time series data and identifying trends or patterns. It involves calculating the average of a fixed number of consecutive data points, referred to as the window size or the period.
The moving average method smooths out short-term fluctuations in the data and highlights long-term trends. It is commonly used for forecasting and trend analysis in various fields, including finance, economics, and signal processing.
how the moving average forecast typically works:
Select a Window Size: Decide on the number of previous data points (the window size) to include in the moving average calculation. This window size determines the smoothing effect of the forecast.
Calculate the Moving Average: For each time step, calculate the average of the data points within the selected window.
Make Predictions: Use the calculated moving average as the forecast for the next time step.
Repeat: As new data becomes available, update the moving average by including the latest data point and removing the oldest data point from the window. Then, repeat the process of making predictions.
Moving average forecast can be implemented using different types of moving averages, such as:
Simple Moving Average (SMA): This is the most basic form of moving average, where each data point in the window is given equal weight.
Weighted Moving Average (WMA): In WMA, different weights are assigned to each data point in the window. Usually, more recent data points are given higher weights.
Exponential Moving Average (EMA): EMA gives more weight to recent observations while still considering older data. It is calculated using a smoothing factor that exponentially decreases with time.
A simple example to show how Moving Average works
x=[5,7,9,6,10,8,12,11]
Calculating Moving Avearge using Python Libraries
Forecasting using Moving Average
Forecasting using a moving average in Python involves extending the concept of a moving average to predict future values based on past data.
Example Walkthrough:
Input Data: Suppose you input the following data: 10 12 14 16 18 20
Window Size: You choose a window size of 3.
Moving Averages: The moving averages will be calculated as:
(10 + 12 + 14) / 3 = 12
(12 + 14 + 16) / 3 = 14
(14 + 16 + 18) / 3 = 16
(16 + 18 + 20) / 3 = 18
Forecast: The forecasted next value will be (18 + 20) / 2 = 19 (using a window size of 2 in this case).
A Basic workflow for time series analysis, including data generation, visualization, stationarity testing, differencing, model fitting, forecasting, and visualization of forecasted values.(ARIMA)
Fitting the MA Model:
Key Terms Explained:
Dependent Variable: y This indicates the variable being modeled (the time series data). No. Observations: 24
The number of data points used in the model. In this case, there are 24 observations in the time series data.
Model: ARIMA(0, 0, 1)
This specifies the model configuration: ARIMA(p, d, q): p: The number of autoregressive terms (AR). Here, it's 0. d: The number of differences to make the series stationary (differencing). Here, it's 0. q: The number of moving average terms (MA). Here, it's 1, which means the model includes a moving average component with one lag.
Log Likelihood: -94.595
This is a measure of how well the model fits the data. A higher (less negative) log likelihood indicates a better fit. It is used in calculating the information criteria.
AIC (Akaike Information Criterion): 195.189
AIC is a metric used to compare different models. It takes into account the model's goodness of fit and the number of parameters. Lower AIC values indicate a better model fit.
BIC (Bayesian Information Criterion): 198.724
Similar to AIC, BIC also measures model fit but with a greater penalty for models with more parameters. Lower BIC values suggest a better model.
HQIC (Hannan-Quinn Information Criterion): 196.127
HQIC is another criterion for model comparison, balancing fit and complexity. It typically falls between AIC and BIC in terms of penalty for the number of parameters.
Covariance Type: opg
This refers to the type of covariance matrix used for estimating the model's standard errors. "opg" stands for outer product of gradients.
Coefficients Table:
const (Constant Term): 133.3215 his is the estimated constant (intercept) of the model. It represents the baseline level of the series.
ma.L1 (Moving Average Term): 0.7537
This is the coefficient for the MA(1) term. It indicates the influence of the lagged forecast error on the current value.
sigma2 (Variance of the Residuals): 149.9109
This is the estimated variance of the model residuals (errors). It measures the variability of the forecast errors.
Statistical Tests
Ljung-Box (L1) (Q): 2.13
This test checks whether the residuals from the model are independent. A higher Q statistic suggests less independence.
Prob(Q): 0.14
The p-value for the Ljung-Box test. A higher p-value indicates that the residuals are likely independent, suggesting that the model has captured the time series structure well.
Jarque-Bera (JB): 0.87
A test for normality of residuals. It checks if the residuals are normally distributed. Prob(JB): 0.65 The p-value for the Jarque-Bera test. A higher p-value indicates that the residuals are likely normally distributed.
Heteroskedasticity (H): 3.31
This test checks for variability in the residuals. Heteroskedasticity suggests varying residual variance over time. Prob(H) (two-sided): 0.11
The p-value for the heteroskedasticity test. A higher p-value suggests that the residuals' variance is not significantly varying.
Skew: 0.17
Measures the asymmetry of the residuals. Values close to 0 indicate a symmetrical distribution.
Kurtosis: 2.13
Measures the "tailedness" of the residuals. A value of 3 would indicate a normal distribution, with lower values indicating lighter tails and higher values indicating heavier tails.
Summary
In summary, the output gives insights into how well the ARIMA(0, 0, 1) model fits the data and whether the residuals (errors) from the model meet the assumptions of the model (e.g., normality, independence). The AIC, BIC, and HQIC help in comparing this model to others, while statistical tests evaluate the model's residuals.
Model: By fitting an MA(1) model (order=(0, 0, 3)), the forecast is based solely on the error term from the last observation.
Forecast: The output provides a prediction for the next 5 time steps, reflecting the pattern of past data.
Why Use Only MA?
Using only the MA component is appropriate when the time series data exhibits random fluctuations around a mean without strong trends or seasonal patterns. This model is simpler than a full ARIMA model and can be effective for short-term forecasting in such cases.