-
Notifications
You must be signed in to change notification settings - Fork 22
Simulation Functions
The smooth package provides simulation functions for generating synthetic time series data from various state-space models. These are useful for Monte Carlo experiments, model validation, and understanding model behaviour.
Note: Not yet implemented in Python.
| Function | Model | Description |
|---|---|---|
sim.es() |
ETS | Simulates from Exponential Smoothing models |
sim.ssarima() |
ARIMA | Simulates from State-Space ARIMA models |
sim.ces() |
CES | Simulates from Complex Exponential Smoothing |
sim.gum() |
GUM | Simulates from Generalised Univariate Models |
sim.sma() |
SMA | Simulates from Simple Moving Average models |
sim.oes() |
oETS | Simulates occurrence probabilities |
simulate() |
Any | Simulates from a fitted model |
Simulates data from the ETS framework with predefined or randomly generated parameters.
library(smooth)
# Basic simulation: ETS(A,N,N) with random parameters
set.seed(41)
sim <- sim.es("ANN", frequency=12, obs=120)
# With specific parameters
sim <- sim.es("MAdM", frequency=12, obs=120,
phi=0.95, persistence=c(0.1, 0.05, 0.01))
# Multiple time series
sim <- sim.es("MNN", frequency=12, obs=120, nsim=50,
probability=0.2, initial=10, persistence=0.1)
# Time series with pre-generated errors
randErrors <- function(...){
# A silly example with whatever
return(sample(c(1:12), 120, replace=T))
}
sim <- sim.es("MNN", frequency=12, obs=120, randomizer="randErrors")| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
model |
character | - | "ANN" | ETS model type (e.g., "ANN", "MAM", "MAdM") |
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Seasonal frequency |
persistence |
numeric vector | - | NULL | Smoothing parameters (alpha, beta, gamma). If NULL, values are generated |
phi |
numeric | - | 1 | Damping parameter. Ignored if no trend |
initial |
numeric vector | - | NULL | Initial states for level and trend (max length 2). If NULL, generated |
initialSeason |
numeric vector | - | NULL | Initial seasonal coefficients. Length should equal frequency. If NULL, generated |
bounds |
character | - | "usual" | Bounds for persistence: "usual", "admissible", or "restricted" |
randomizer |
character | - | "rnorm" | Random number generator function name (e.g., "rnorm", "rlnorm", "rt", "rlaplace", "rs") |
probability |
numeric | - | 1 | Probability of occurrence (can be vector for time-varying probability) |
... |
- | - | - | Additional parameters passed to randomizer (e.g., mean, sd for rnorm()) |
| Element | Description |
|---|---|
model |
Model name |
data |
Simulated time series (vector or matrix if nsim > 1) |
states |
State matrix (or array if nsim > 1). States in columns, time in rows |
persistence |
Smoothing parameters used (vector or matrix if nsim > 1) |
phi |
Damping parameter value used |
initial |
Initial values (vector or matrix) |
initialSeason |
Initial seasonal coefficients (vector or matrix) |
profile |
The final profile produced in the simulation |
probability |
Vector of probabilities used |
intermittent |
Type of intermittent model used |
residuals |
Error terms used (vector or matrix) |
occurrence |
Occurrence variable values (vector or matrix) |
logLik |
True log-likelihood value |
# Simple ETS(A,N,N)
sim <- sim.es("ANN", frequency=12, obs=120)
plot(sim)
# Multiplicative trend with damping
sim <- sim.es("MAdM", frequency=12, obs=120,
phi=0.95, persistence=c(0.1, 0.05, 0.01))
plot(sim)
# Using Log-Normal errors for multiplicative models
sim <- sim.es("MAdM", frequency=12, obs=120,
phi=0.95, persistence=c(0.1, 0.05, 0.01),
randomizer="rlnorm", meanlog=0, sdlog=0.015)
# Intermittent demand
sim <- sim.es("MNN", frequency=12, obs=120,
probability=0.2, initial=10, persistence=0.1)
plot(sim)
# With initial seasonal values
sim <- sim.es("MMM", persistence=c(0.1, 0.1, 0.1), initial=c(2000, 1),
initialSeason=c(1.1, 1.05, 0.9, 0.95), frequency=4, obs=80,
mean=0, sd=0.01)Simulates data from State-Space ARIMA models.
# Default: ARIMA(0,1,1)
sim <- sim.ssarima(frequency=12, obs=120, nsim=10)
# SARIMA(0,1,1)(1,0,2)_12 with drift
sim <- sim.ssarima(orders=list(ar=c(0,1), i=c(1,0), ma=c(1,2)),
lags=c(1,12), constant=TRUE, obs=120)| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
orders |
list | - | list(ar=0, i=1, ma=1) | ARIMA orders per lag. List with ar, i, ma vectors |
lags |
numeric vector | - | 1 | Seasonal lags. Length must match length of order vectors |
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Time series frequency |
arma |
list | - | NULL | Fixed AR/MA parameters (lag-wise order). Can be passed from an estimated ssarima |
constant |
logical/numeric | - | FALSE | Include constant/drift. Can be TRUE or a specific value |
initial |
numeric vector | - | NULL | Initial state values. If NULL, generated from uniform distribution |
bounds |
character | - | "admissible" | Bounds for AR/MA: "admissible" or "none" |
randomizer |
character | - | "rnorm" | Random number generator function name |
probability |
numeric | - | 1 | Occurrence probability (can be vector) |
... |
- | - | - | Additional parameters passed to randomizer |
| Element | Description |
|---|---|
model |
ARIMA model specification |
arma |
List of AR/MA parameters (list of matrices if nsim > 1) |
constant |
Constant values (vector if nsim > 1) |
initial |
Initial state values (matrix if nsim > 1) |
profile |
The final profile produced in the simulation |
data |
Simulated time series (vector or matrix) |
states |
State array |
residuals |
Generated errors (vector or matrix) |
occurrence |
Occurrence variable values |
logLik |
True log-likelihood |
# ARIMA(0,1,1)
sim <- sim.ssarima(frequency=12, obs=120)
plot(sim)
# Seasonal ARIMA with drift
sim <- sim.ssarima(orders=list(ar=c(0,1), i=c(1,0), ma=c(1,2)),
lags=c(1,12), constant=TRUE, obs=120)
plot(sim)
# Intermittent SARIMA
sim <- sim.ssarima(orders=list(ar=c(1,0), i=c(0,1), ma=c(2,1)),
lags=c(1,7), obs=120, probability=0.2)
plot(sim)
# High frequency SARIMA(1,0,2)_1(0,1,1)_7(1,0,1)_30
sim <- sim.ssarima(orders=list(ar=c(1,0,1), i=c(0,1,0), ma=c(2,1,1)),
lags=c(1,7,30), obs=360, nsim=10)Simulates data from CES models.
# CES(n) - non-seasonal
sim <- sim.ces(frequency=12, obs=120)
# CES(s) - simple seasonal
sim <- sim.ces("simple", frequency=24, obs=240)
# CES(p) - partial seasonal
sim <- sim.ces("partial", b=0.2, frequency=12, obs=240, nsim=10)
# CES(f) - full seasonal
sim <- sim.ces("full", frequency=12, obs=240, nsim=10)| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
seasonality |
character | - | "none" | Seasonality type: "none", "simple", "partial", "full" |
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Seasonal frequency (must be > 1 for seasonal models) |
a |
complex | - | NULL | First complex smoothing parameter. CES is sensitive to these values |
b |
complex/numeric | - | NULL | Second complex smoothing parameter. Real for "partial", complex for "full". NULL for "none"/"simple" |
initial |
complex matrix | - | NULL | Initial states. For "simple"/"partial"/"full", first two columns contain non-seasonal components |
randomizer |
character | - | "rnorm" | Random number generator function name |
probability |
numeric | - | 1 | Occurrence probability (can be vector) |
... |
- | - | - | Additional parameters passed to randomizer |
| Element | Description |
|---|---|
model |
CES model type |
a |
Complex parameter a (vector if nsim > 1) |
b |
Complex parameter b (NULL for "none"/"simple", vector if nsim > 1) |
initial |
Initial state values (array if nsim > 1) |
profile |
The final profile produced in the simulation |
data |
Simulated time series (vector or matrix) |
states |
State matrix/array |
residuals |
Generated errors (vector or matrix) |
occurrence |
Occurrence variable values |
logLik |
Log-likelihood of the constructed model |
# Non-seasonal CES
sim <- sim.ces(frequency=12, obs=120)
plot(sim)
# Simple seasonal (solar-like patterns)
set.seed(41)
sim <- sim.ces("simple", frequency=24, obs=240)
plot(sim)
# Full seasonal
sim <- sim.ces("full", frequency=12, obs=240, nsim=10)
plot(sim)Simulates data from GUM. Best practice is to simulate from a fitted model.
# Generate complex multi-frequency data
set.seed(41)
sim <- sim.gum(orders=c(1,1,1), lags=c(1,48,336), nsim=1,
frequency=336, obs=3360,
measurement=rep(1,3), transition=diag(3),
persistence=c(0.045, 0.162, 0.375),
initial=initialMatrix) # Provide initial values| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
orders |
numeric vector | - | c(1) | Orders for each lag (number of states per lag type) |
lags |
numeric vector | - | c(1) | Lags for components. Length must match orders
|
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Time series frequency |
measurement |
numeric vector | - | NULL | Measurement vector w. If NULL, randomly generated (0-1) |
transition |
matrix/vector | - | NULL | Transition matrix F. If vector, formed as matrix(transition, nc, nc) |
persistence |
numeric vector | - | NULL | Persistence vector g (smoothing parameters). If NULL, randomly generated |
initial |
matrix | - | NULL | Initial states. If NULL, generated from uniform distribution |
randomizer |
character | - | "rnorm" | Random number generator function name |
probability |
numeric | - | 1 | Occurrence probability (can be vector) |
... |
- | - | - | Additional parameters passed to randomizer |
| Element | Description |
|---|---|
model |
GUM model name |
measurement |
Measurement matrix w |
transition |
Transition matrix F |
persistence |
Persistence vector (smoothing parameters) |
initial |
Initial state values (array if nsim > 1) |
profile |
The final profile produced in the simulation |
data |
Simulated time series (vector or matrix) |
states |
State array |
residuals |
Generated errors (vector or matrix) |
occurrence |
Occurrence variable values |
logLik |
Log-likelihood of the constructed model |
# GUM(1[1]) - simple model
sim <- sim.gum(orders=c(1), lags=c(1), obs=120, nsim=100)
plot(sim)
# GUM(1[1],1[4]) - seasonal model
sim <- sim.gum(orders=c(1,1), lags=c(1,4), frequency=4, obs=80,
nsim=100, transition=c(1, 0, 0.9, 0.9))
plot(sim)Simulates data from SMA models.
# SMA(10)
sim <- sim.sma(order=10, obs=240, frequency=12)
plot(sim)| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
order |
integer | - | NULL | SMA order. If NULL, random order from 1 to 100 is selected |
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Time series frequency |
initial |
numeric vector | - | NULL | Initial state values. If NULL, generated |
randomizer |
character | - | "rnorm" | Random number generator function name |
probability |
numeric | - | 1 | Occurrence probability (can be vector) |
... |
- | - | - | Additional parameters passed to randomizer |
| Element | Description |
|---|---|
model |
SMA model name |
data |
Simulated time series (vector or matrix) |
states |
State matrix (or array if nsim > 1) |
initial |
Initial values (vector or matrix) |
probability |
Vector of probabilities used |
intermittent |
Type of intermittent model used |
residuals |
Generated errors (vector or matrix) |
occurrence |
Occurrence variable values |
logLik |
Log-likelihood of the constructed model |
# SMA(10) with normal errors
sim <- sim.sma(order=10, obs=40, randomizer="rnorm", mean=0, sd=100)
plot(sim)Simulates occurrence probabilities using ETS for intermittent demand modelling. The function calls for sim.es() and then does internal transformations of the simulated data to align with the occurrence models.
# Simple occurrence model
sim <- sim.oes("MNN", frequency=12, obs=60, initial=1,
randomizer="rlnorm", meanlog=0, sdlog=0.1)
# General occurrence with model A and model B
sim <- sim.oes("MNN", frequency=12, obs=60, occurrence="general",
modelB="MNN", persistence=0.2, persistenceB=0.1)| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
model |
character | - | "MNN" | ETS model type for model A. "MZZ" models recommended for positive errors |
obs |
integer | - | 10 | Number of observations |
nsim |
integer | - | 1 | Number of series to simulate |
frequency |
integer | - | 1 | Seasonal frequency |
occurrence |
character | - | "odds-ratio" | Occurrence type: "odds-ratio", "inverse-odds-ratio", "direct", "general" |
bounds |
character | - | "usual" | Bounds for persistence: "usual", "admissible", "restricted" |
randomizer |
character | - | "rlnorm" | Random number generator (rlnorm, rinvgauss, rgamma, rnorm recommended) |
persistence |
numeric vector | - | NULL | Smoothing parameters for model A |
phi |
numeric | - | 1 | Damping parameter for model A |
initial |
numeric vector | - | NULL | Initial states for model A |
initialSeason |
numeric vector | - | NULL | Initial seasonal coefficients for model A |
modelB |
character | - | model | ETS model type for model B (used in "general" and "inverse-odds-ratio") |
persistenceB |
numeric vector | - | persistence | Smoothing parameters for model B |
phiB |
numeric | - | phi | Damping parameter for model B |
initialB |
numeric vector | - | initial | Initial states for model B |
initialSeasonB |
numeric vector | - | initialSeason | Initial seasonal coefficients for model B |
... |
- | - | - | Additional parameters passed to randomizer (shared by model A and B) |
| Element | Description |
|---|---|
model |
Model name |
modelA |
Model A object, generated using sim.es() |
modelB |
Model B object, generated using sim.es() |
probability |
The probability values generated by the model |
occurrence |
Type of occurrence used in the model |
logLik |
Log-likelihood of the constructed model |
# Log-normal errors
sim <- sim.oes("MNN", frequency=12, obs=60, initial=1,
randomizer="rlnorm", meanlog=0, sdlog=0.1)
plot(sim$probability)
# Using inverse Gaussian (requires statmod package)
sim <- sim.oes("MNN", frequency=12, obs=60,
randomizer="rinvgauss", mean=1, dispersion=0.5)
# Generate iETS(MNN) with TSB style probabilities
oETSMNN <- sim.oes("MNN", obs=50, occurrence="d", persistence=0.2, initial=1,
randomizer="rlnorm", meanlog=0, sdlog=0.3)
iETSMNN <- sim.es("MNN", obs=50, frequency=12, persistence=0.2, initial=4,
probability=oETSMNN$probability)Simulates new data from a fitted smooth model using its estimated parameters.
# Fit model
model <- adam(AirPassengers, "MMM", lags=12)
# Simulate 50 series of 100 observations
simData <- simulate(model, nsim=50, obs=100)
# Compare original and simulated
par(mfcol=c(1,2))
plot(model,7)
plot(simData)| Parameter | Type (R) | Type (Python) | Default | Description |
|---|---|---|---|---|
object |
adam/smooth | - | - | Fitted model |
nsim |
integer | - | 1 | Number of series to simulate |
seed |
integer | - | NULL | Random seed |
obs |
integer | - | nobs(object) | Number of observations |
# From ADAM
model <- adam(BJsales)
simData <- simulate(model, nsim=50, obs=100)
# From ETS
model <- es(BJsales)
simData <- simulate(model, nsim=50, obs=100)
# From SSARIMA
model <- auto.ssarima(BJsales)
simData <- simulate(model, nsim=50, obs=100)
# From CES
model <- auto.ces(BJsales)
simData <- simulate(model, nsim=50, obs=100)
# From GUM
model <- auto.gum(BJsales)
simData <- simulate(model, nsim=50, obs=100)
# From SMA
model <- sma(BJsales)
simData <- simulate(model, nsim=50, obs=1000)For multiplicative models, use lower variance for random errors:
# Recommended for multiplicative models
sim <- sim.es("MAdM", frequency=12, obs=120,
randomizer="rlnorm", meanlog=0, sdlog=0.015)Note on multiplicative errors: In case of multiplicative error models, the randomizer generates 1+e_t, not e_t. This means the mean should typically be 1, not zero.
When possible, simulate from fitted models to ensure realistic parameters:
# Fit to real data first
model <- adam(realData, "ZXZ", lags=12)
# Then simulate
simulated <- simulate(model, nsim=100, obs=500)# Generate many replications for Monte Carlo
nsim <- 1000
results <- matrix(NA, nsim, 2)
for(i in 1:nsim) {
sim <- sim.es("AAN", frequency=12, obs=120)
fit <- adam(sim$data, "ZXZ", lags=12, silent=TRUE)
results[i,] <- coef(fit)[c("alpha","beta")]
}
# Analyse estimation properties
colMeans(results)
apply(results, 2, sd)| Bound Type | Description |
|---|---|
"usual" |
Standard bounds from Hyndman et al. (2008) p.156 |
"restricted" |
Similar to "usual" but with upper bound equal to 0.3 |
"admissible" |
Bounds from tables 10.1 and 10.2 of Hyndman et al. (2008) |
For SSARIMA, bounds ensure stability and stationarity when set to "admissible".