#' Compute Dynamics Similarity Coefficient (DSC) between two time series
#'
#' @param x Numeric vector, first time series.
#' @param y Numeric vector, second time series.
#' @param n_s Integer, number of states. Default 15.
#' @param lag Integer, lag order. Default 1.
#' @param th_step Numeric, threshold step size. Default 0.005.
#'
#' @return A numeric value between 0 and 1, where higher values indicate greater similarity of intrinsic dynamics.
#' @export
#'
#' @references
#' Nie, Chun-Xiao. "Distinguishing time series generated by different intrinsic dynamics using quantile graphs." Communications in Nonlinear Science and Numerical Simulation (2026): 110002.
#' @examples
#' x <- arima.sim(model = list(ar = 0.5), n = 1000)
#' y <- arima.sim(model = list(ar = -0.5), n = 1000)
#' dsc(x, y, n_s = 15, lag = 1)
dsc <- function(x, y, n_s = 15, lag = 1, th_step = 0.005) {
  Px <- transition_matrix(x, n_s, lag)
  Py <- transition_matrix(y, n_s, lag)

  max_th <- 1 / n_s
  thresholds <- seq(0, max_th, by = th_step)

  jaccards <- vapply(thresholds, function(th) {
    Ax <- (Px > th) * 1      # strict inequality as requested
    Ay <- (Py > th) * 1
    inter <- sum(Ax & Ay)
    union <- sum(Ax | Ay)
    if (union == 0) 1 else inter / union
  }, numeric(1))

  mean(jaccards)
}
