Learn R Programming

RSDC (version 1.1-2)

rsdc_simulate: Simulate Multivariate Regime-Switching Data (TVTP)

Description

Simulates a multivariate time series from a regime-switching model with time-varying transition probabilities (TVTP) driven by covariates X. Transition probabilities are generated via a multinomial logistic (softmax) link; observations are drawn from regime-specific Gaussian distributions.

Usage

rsdc_simulate(n, X, beta, mu, sigma, N, seed = NULL)

Value

A list with:

states

Integer vector of length n; the simulated regime index at each time.

observations

Numeric matrix \(n \times K\); the simulated observations.

transition_matrices

Array \(N \times N \times n\); the transition matrix \(P_t\) used at each time step (with \(P_1\) undefined by construction; see Details).

Arguments

n

Integer. Number of time steps to simulate.

X

Numeric matrix \(n \times p\) of covariates used to form the transition probabilities. Row X[t, ] corresponds to covariates available at time t. Only rows 1:(n-1) are used to transition from t-1 to t.

beta

Numeric array \(N \times N \times p\). Softmax coefficients for the multinomial transition model; beta[i, j, ] parameterizes the transition from state \(i\) to state \(j\).

mu

Numeric matrix \(N \times K\). Regime-specific mean vectors.

sigma

Numeric array \(K \times K \times N\). Regime-specific covariance (here, correlation/variance) matrices; each \(K \times K\) slice must be symmetric positive definite.

N

Integer. Number of regimes.

seed

Optional integer. If supplied, sets the RNG seed for reproducibility.

Details

  • Initial state and first draw: The initial regime \(S_1\) is sampled uniformly; the first observation \(y_1\) is drawn from \(\mathcal{N}(\mu_{S_1}, \Sigma_{S_1})\).

  • TVTP via softmax: For \(t \ge 2\), the row \(i\) of \(P_t\) is $$P_t(i, j) = \frac{\exp\!\big(X_{t-1}^\top \beta_{i,j}\big)} {\sum_{h=1}^N \exp\!\big(X_{t-1}^\top \beta_{i,h}\big)}\,,$$ computed with log-sum-exp stabilization.

  • Sampling: Given \(S_{t-1}\), draw \(S_t\) from the categorical distribution with probabilities \(P_t(S_{t-1}, \cdot)\) and \(y_t \sim \mathcal{N}(\mu_{S_t}, \Sigma_{S_t})\).

See Also

rsdc_hamilton (filter/evaluation), rsdc_estimate (estimators), rsdc_forecast (forecasting)

Examples

Run this code
set.seed(123)
n <- 200; K <- 3; N <- 2; p <- 2
X <- cbind(1, scale(seq_len(n)))

beta <- array(0, dim = c(N, N, p))
beta[1, 1, ] <- c(1.2,  0.0)
beta[2, 2, ] <- c(1.0, -0.1)

mu <- rbind(c(0, 0, 0),
            c(0, 0, 0))
rho <- rbind(c(0.10, 0.05, 0.00),
             c(0.60, 0.40, 0.30))
Sig <- array(0, dim = c(K, K, N))
for (m in 1:N) {
  R <- diag(K); R[lower.tri(R)] <- rho[m, ]; R[upper.tri(R)] <- t(R)[upper.tri(R)]
  Sig[, , m] <- R
}
sim <- rsdc_simulate(n = n, X = X, beta = beta, mu = mu, sigma = Sig, N = N, seed = 99)

Run the code above in your browser using DataLab