Skip to content

Simulation Functions

Ivan Svetunkov edited this page Apr 10, 2026 · 6 revisions

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.

Overview

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

sim.es() - Exponential Smoothing Simulation

Simulates data from the ETS framework with predefined or randomly generated parameters.

R Usage

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")

Parameters

Parameter Type (R) Type (Python) Default Description
model character TBA "ANN" ETS model type (e.g., "ANN", "MAM", "MAdM")
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Seasonal frequency
persistence numeric vector TBA NULL Smoothing parameters (alpha, beta, gamma). If NULL, values are generated
phi numeric TBA 1 Damping parameter. Ignored if no trend
initial numeric vector TBA NULL Initial states for level and trend (max length 2). If NULL, generated
initialSeason numeric vector TBA NULL Initial seasonal coefficients. Length should equal frequency. If NULL, generated
bounds character TBA "usual" Bounds for persistence: "usual", "admissible", or "restricted"
randomizer character TBA "rnorm" Random number generator function name (e.g., "rnorm", "rlnorm", "rt", "rlaplace", "rs")
probability numeric TBA 1 Probability of occurrence (can be vector for time-varying probability)
... TBA - TBA Additional parameters passed to randomizer (e.g., mean, sd for rnorm())

Output

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

Examples

# 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)

sim.ssarima() - ARIMA Simulation

Simulates data from State-Space ARIMA models.

R Usage

# 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)

Parameters

Parameter Type (R) Type (Python) Default Description
orders list TBA list(ar=0, i=1, ma=1) ARIMA orders per lag. List with ar, i, ma vectors
lags numeric vector TBA 1 Seasonal lags. Length must match length of order vectors
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Time series frequency
arma list TBA NULL Fixed AR/MA parameters (lag-wise order). Can be passed from an estimated ssarima
constant logical/numeric TBA FALSE Include constant/drift. Can be TRUE or a specific value
initial numeric vector TBA NULL Initial state values. If NULL, generated from uniform distribution
bounds character TBA "admissible" Bounds for AR/MA: "admissible" or "none"
randomizer character TBA "rnorm" Random number generator function name
probability numeric TBA 1 Occurrence probability (can be vector)
... TBA - TBA Additional parameters passed to randomizer

Output

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

Examples

# 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)

sim.ces() - Complex Exponential Smoothing Simulation

Simulates data from CES models.

R Usage

# 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)

Parameters

Parameter Type (R) Type (Python) Default Description
seasonality character TBA "none" Seasonality type: "none", "simple", "partial", "full"
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Seasonal frequency (must be > 1 for seasonal models)
a complex TBA NULL First complex smoothing parameter. CES is sensitive to these values
b complex/numeric TBA NULL Second complex smoothing parameter. Real for "partial", complex for "full". NULL for "none"/"simple"
initial complex matrix TBA NULL Initial states. For "simple"/"partial"/"full", first two columns contain non-seasonal components
randomizer character TBA "rnorm" Random number generator function name
probability numeric TBA 1 Occurrence probability (can be vector)
... TBA - TBA Additional parameters passed to randomizer

Output

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

Examples

# 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)

sim.gum() - Generalised Univariate Model Simulation

Simulates data from GUM. Best practice is to simulate from a fitted model.

R Usage

# 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

Parameters

Parameter Type (R) Type (Python) Default Description
orders numeric vector TBA c(1) Orders for each lag (number of states per lag type)
lags numeric vector TBA c(1) Lags for components. Length must match orders
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Time series frequency
measurement numeric vector TBA NULL Measurement vector w. If NULL, randomly generated (0-1)
transition matrix/vector TBA NULL Transition matrix F. If vector, formed as matrix(transition, nc, nc)
persistence numeric vector TBA NULL Persistence vector g (smoothing parameters). If NULL, randomly generated
initial matrix TBA NULL Initial states. If NULL, generated from uniform distribution
randomizer character TBA "rnorm" Random number generator function name
probability numeric TBA 1 Occurrence probability (can be vector)
... TBA - TBA Additional parameters passed to randomizer

Output

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

Examples

# 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)

sim.sma() - Simple Moving Average Simulation

Simulates data from SMA models.

R Usage

# SMA(10)
sim <- sim.sma(order=10, obs=240, frequency=12)
plot(sim)

Parameters

Parameter Type (R) Type (Python) Default Description
order integer TBA NULL SMA order. If NULL, random order from 1 to 100 is selected
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Time series frequency
initial numeric vector TBA NULL Initial state values. If NULL, generated
randomizer character TBA "rnorm" Random number generator function name
probability numeric TBA 1 Occurrence probability (can be vector)
... TBA - TBA Additional parameters passed to randomizer

Output

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

Examples

# SMA(10) with normal errors
sim <- sim.sma(order=10, obs=40, randomizer="rnorm", mean=0, sd=100)
plot(sim)

sim.oes() - Occurrence ETS Simulation

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.

R Usage

# 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)

Parameters

Parameter Type (R) Type (Python) Default Description
model character TBA "MNN" ETS model type for model A. "MZZ" models recommended for positive errors
obs integer TBA 10 Number of observations
nsim integer TBA 1 Number of series to simulate
frequency integer TBA 1 Seasonal frequency
occurrence character TBA "odds-ratio" Occurrence type: "odds-ratio", "inverse-odds-ratio", "direct", "general"
bounds character TBA "usual" Bounds for persistence: "usual", "admissible", "restricted"
randomizer character TBA "rlnorm" Random number generator (rlnorm, rinvgauss, rgamma, rnorm recommended)
persistence numeric vector TBA NULL Smoothing parameters for model A
phi numeric TBA 1 Damping parameter for model A
initial numeric vector TBA NULL Initial states for model A
initialSeason numeric vector TBA NULL Initial seasonal coefficients for model A
modelB character TBA model ETS model type for model B (used in "general" and "inverse-odds-ratio")
persistenceB numeric vector TBA persistence Smoothing parameters for model B
phiB numeric TBA phi Damping parameter for model B
initialB numeric vector TBA initial Initial states for model B
initialSeasonB numeric vector TBA initialSeason Initial seasonal coefficients for model B
... TBA - TBA Additional parameters passed to randomizer (shared by model A and B)

Output

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

Examples

# 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)

simulate() - Simulate from Fitted Model

Simulates new data from a fitted smooth model using its estimated parameters.

R Usage

# 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)

Parameters

Parameter Type (R) Type (Python) Default Description
object adam/smooth TBA - Fitted model
nsim integer TBA 1 Number of series to simulate
seed integer TBA NULL Random seed
obs integer TBA nobs(object) Number of observations

Examples

# 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)

Best Practices

Parameter Selection

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.

Simulation from Fitted Models

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)

Monte Carlo Studies

# 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)

Bounds for Parameter Generation

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".

See Also

Clone this wiki locally