Forecasting with Moving Averages
Svep för att visa menyn
To forecast future values in a time series, moving averages offer a simple yet effective approach. You can use a simple moving average (SMA) to predict the next value by averaging the most recent N observations. The process for one-step-ahead forecasting with SMA involves the following steps:
- Choose the window size N, which determines how many past values you will average;
- For each time point t, calculate the average of the previous N values: (xt−N+1,xt−N+2,...,xt);
- Use this average as the forecast for the next time point (t+1);
- Slide the window forward by one period and repeat the process for each subsequent forecast.
While this method is easy to implement and understand, it has several limitations. Moving averages assume that recent values are the best predictors of the next value, which may not hold if there are trends or seasonality in the data. Furthermore, SMAs give equal weight to all N values, potentially ignoring the relevance of more recent data. As a result, forecasts may lag behind actual changes, especially in volatile or non-stationary time series.
123456789101112131415161718import pandas as pd # Example time series data data = [12, 14, 15, 16, 15, 17, 18, 20, 19, 21] N = 3 # Window size for SMA # Create a pandas Series ts = pd.Series(data) # Calculate the Simple Moving Average (SMA) sma = ts.rolling(window=N).mean() # Forecast the next value using the last N values last_n_values = ts[-N:] forecast = last_n_values.mean() print(f"Last {N} values: {list(last_n_values)}") print(f"Forecast for next value using SMA: {forecast:.2f}")
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal