Learn R Programming

RSDC (version 1.1-2)

rsdc_hamilton: Hamilton Filter (Fixed P or TVTP)

Description

Runs the Hamilton (1989) filter for a multivariate regime-switching correlation model. Supports either a fixed (time-invariant) transition matrix \(P\) or time-varying transition probabilities (TVTP) built from exogenous covariates X via a logistic link. Returns filtered/smoothed regime probabilities and the log-likelihood.

Usage

rsdc_hamilton(y, X = NULL, beta = NULL, rho_matrix, K, N, P = NULL)

Value

A list with:

filtered_probs

\(N \times T\) matrix of filtered probabilities \(\Pr(S_t = j \mid \Omega_t)\).

smoothed_probs

\(N \times T\) matrix of smoothed probabilities \(\Pr(S_t = j \mid \Omega_T)\).

log_likelihood

Scalar log-likelihood of the model given y.

Arguments

y

Numeric matrix \(T \times K\) of observations (e.g., standardized residuals/returns). Columns are treated as mean-zero with unit variance; only the correlation structure is modeled.

X

Optional numeric matrix \(T \times p\) of covariates for TVTP. Required if beta is supplied.

beta

Optional numeric matrix \(N \times p\). TVTP coefficients; row \(i\) governs persistence of regime \(i\) via plogis(X[t, ] %*% beta[i, ]).

rho_matrix

Numeric matrix \(N \times C\) of regime correlation parameters, where \(C = K(K-1)/2\). Each row is the lower-triangular part (by lower.tri) of a regime's correlation matrix.

K

Integer. Number of observed series (columns of y).

N

Integer. Number of regimes.

P

Optional \(N \times N\) fixed transition matrix. Used only when X or beta is NULL.

Details

  • Correlation rebuild: For regime \(m\), a correlation matrix \(R_m\) is reconstructed from rho_matrix[m, ] (lower-triangular fill + symmetrization). Non-PD proposals are penalized.

  • Transition dynamics:

    • Fixed P: If X or beta is missing, a constant \(P\) is used (user-provided via P; otherwise uniform \(1/N\) rows).

    • TVTP: With X and beta, diagonal entries use plogis(X[t, ] %*% beta[i, ]). Off-diagonals are equal and sum to \(1 - p_{ii,t}\). For \(N=1\), \(P_t = [1]\).

  • Numerical safeguards: A small ridge is added before inversion; if filtering degenerates at a time step, log_likelihood = -Inf is returned.

References

hamilton1989RSDC

See Also

rsdc_likelihood and rsdc_estimate.

Examples

Run this code
set.seed(1)
T <- 50; K <- 3; N <- 2
y <- scale(matrix(rnorm(T * K), T, K), center = TRUE, scale = TRUE)

# Example rho: two regimes with different average correlations
rho <- rbind(c(0.10, 0.05, 0.00),
             c(0.60, 0.40, 0.30))  # lower-tri order for K=3

# Fixed-P filtering
Pfix <- matrix(c(0.9, 0.1,
                 0.2, 0.8), nrow = 2, byrow = TRUE)
out_fix <- rsdc_hamilton(y = y, X = NULL, beta = NULL,
                         rho_matrix = rho, K = K, N = N, P = Pfix)
str(out_fix$filtered_probs)

# TVTP filtering (include an intercept yourself)
X <- cbind(1, scale(seq_len(T)))
beta <- rbind(c(1.2, 0.0),
              c(0.8, -0.1))
out_tvtp <- rsdc_hamilton(y = y, X = X, beta = beta,
                          rho_matrix = rho, K = K, N = N)
out_tvtp$log_likelihood

Run the code above in your browser using DataLab