-
Notifications
You must be signed in to change notification settings - Fork 22
SMA
SMA (Simple Moving Average) implements simple moving average in state-space form with automatic order selection. This provides a statistically rigorous foundation for the classic moving average method.
Note: SMA is currently available only in R.
The function constructs an AR model in state-space form based on the simple moving average concept:
yₜ = (1/n) Σⱼ₌₁ⁿ yₜ₋ⱼ
This is equivalent to an AR(n) process, allowing proper parameter estimation and prediction interval construction.
Note that the forecast from the SMA in the state space is not a straight line! It is a multistep conditional expectation from the respective AR(n) model with provided parameters. See Svetunkov & Petropoulos (2018) for more details.
library(smooth)
# SMA with specific order
sma(y, order=12, h=18, holdout=TRUE)
# Automatic order selection
sma(y, h=18, holdout=TRUE)
# With specific information criterion
sma(y, order=NULL, ic="BIC", h=18, holdout=TRUE)
# Fast search (modified ternary search)
sma(y, h=18, holdout=TRUE, fast=TRUE)
# Full search
sma(y, h=18, holdout=TRUE, fast=FALSE)model <- sma(y, order=12, h=18, holdout=TRUE)
# Point forecasts
forecast(model, h=18)
# With prediction intervals
plot(forecast(model, h=18, interval="empirical"))
plot(forecast(model, h=18, interval="parametric"))| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
y |
vector/ts | TBA | - | Time series data |
order |
integer/NULL | TBA | NULL | SMA order (NULL for auto-selection) |
ic |
character | TBA | "AICc" | Information criterion for order selection |
h |
integer | TBA | 10 | Forecast horizon |
holdout |
logical | TBA | FALSE | Use holdout validation |
silent |
logical | TBA | TRUE | Suppress output |
fast |
logical | TBA | TRUE | Use fast ternary search |
-
"AICc": Corrected AIC (default) -
"AIC": Akaike Information Criterion -
"BIC": Bayesian Information Criterion -
"BICc": Corrected BIC
When order=NULL, the function automatically selects the optimal order:
-
Fast mode (
fast=TRUE): Modified ternary search - finds local minimum quickly -
Full mode (
fast=FALSE): Exhaustive search - guarantees global minimum but slower
The sma() function internally uses adam() with model="NNN" and AR coefficients all equal to 1/n. It returns an object of class "adam" containing:
| Element | Type (R) | Description |
|---|---|---|
model |
character | Model name (e.g., "SMA(12)") |
timeElapsed |
difftime | Time elapsed for model construction |
call |
call | The function call |
orders |
integer | The SMA order (accessible via orders(model)) |
| Element | Type (R) | Description |
|---|---|---|
states |
matrix | State matrix (observations × states) |
transition |
matrix | Transition matrix F (all elements = 1/n) |
persistence |
numeric vector | Persistence vector g (all elements = 1/n) |
measurement |
numeric vector | Measurement vector w |
initial |
numeric vector | Initial state vector values |
initialType |
character | Type of initial values used |
| Element | Type (R) | Description |
|---|---|---|
fitted |
vector | Fitted values |
forecast |
vector | Point forecasts for h steps ahead |
lower |
vector | Lower bound of prediction interval (NA if interval=FALSE) |
upper |
vector | Upper bound of prediction interval (NA if interval=FALSE) |
residuals |
vector | Model residuals |
errors |
matrix | Matrix of 1 to h steps ahead errors (for multistep losses) |
| Element | Type (R) | Description |
|---|---|---|
s2 |
numeric | Residual variance (adjusted for degrees of freedom) |
logLik |
numeric | Log-likelihood value |
lossValue |
numeric | Cost function value |
loss |
character | Type of loss function used |
nParam |
matrix | Table of estimated/provided parameters |
| Element | Type (R) | Description |
|---|---|---|
y |
vector/ts | Original data |
holdout |
vector/ts | Holdout part of original data |
interval |
character | Type of interval requested |
level |
numeric | Confidence level for interval |
cumulative |
logical | Whether forecast was cumulative |
| Element | Type (R) | Description |
|---|---|---|
ICs |
named numeric vector | IC values for each tested order (1 to maxOrder) |
| Element | Type (R) | Description |
|---|---|---|
accuracy |
numeric vector | Accuracy measures if holdout is TRUE |
model <- sma(y, h=18, holdout=TRUE)
# Model name
model$model
# Get the order
orders(model)
# Fitted values and residuals
fitted(model)
residuals(model)
# Information criteria
AIC(model)
BIC(model)
# If order was auto-selected, see all tested IC values
model$ICsSMA supports various interval types:
# Empirical intervals (bootstrap-based)
forecast(model, h=12, interval="empirical")
# Parametric intervals (normal assumption)
forecast(model, h=12, interval="parametric")
# No intervals
forecast(model, h=12, interval="none")Unlike stats::filter() which only provides smoothed values, smooth::sma():
- Provides proper prediction intervals
- Allows automatic order selection
- Returns state-space components
- Offers information criteria
SMA is appropriate when:
- Simple averaging is conceptually appropriate
- You want automatic order selection
- You need proper uncertainty quantification
- The series has no clear trend or seasonality
For trend or seasonal data, consider ES or ADAM.
SMA isn't currently available in Python smooth.
For simple moving average smoothing without forecasting, use pandas:
y.rolling(window=n).mean()- Svetunkov, I., & Petropoulos, F. (2018). Old dog, new tricks: a modelling view of simple moving averages. International Journal of Production Research, 56(18), 6034-6047. DOI: 10.1080/00207543.2017.1380326