detect_cp: Detect change points on time series.

View source: R/detect_cp.R

detect_cpR Documentation

Detect change points on time series.

Description

The detect_cp function detect change points on univariate and multivariate time series.

Usage

detect_cp(
  data,
  n_iterations,
  n_burnin = 0,
  q = 0.5,
  params = list(),
  kernel,
  print_progress = TRUE,
  user_seed = 1234
)

Arguments

data

a vector or a matrix. If a vector the algorithm for univariate time series is used. If a matrix, where rows are the observations and columns are the times, then the algorithm for multivariate time series is used.

n_iterations

number of MCMC iterations.

n_burnin

number of iterations that must be excluded when computing the posterior estimate.

q

probability of performing a split at each iteration.

params

a list of parameters:

If data is an univariate time series the following must be specified:

  • a, b, c parameters of the Normal-Gamma prior for \mu and \lambda.

  • prior_var_phi variance for the proposal in the N(0,\sigma^2_\phi) posterior estimate of \theta.

  • par_theta_c parameter of the shifted Gamma prior of \theta.

  • par_theta_d parameter of the shifted Gamma prior of \theta.

If the time series is multivariate the following must be specified:

  • m_0, k_0, nu_0, S_0 parameters for the Normal-Inverse-Wishart prior for (\mu,\lambda).

  • prior_var_phi variance for the proposal in the N(0,\sigma^2_\phi) posterior estimate of \theta.

  • par_theta_c parameter of the shifted Gamma prior of \theta.

  • par_theta_d parameter of the shifted Gamma prior of \theta.

If data are survival functions:

  • M number of Monte Carlo iterations when computing the likelihood of the survival function.

  • xi recovery rate fixed constant for each population at each time.

  • a0,b0 parameters for the computation of the integrated likelihood of the survival functions.

  • I0_var variance for the Metropolis-Hastings estimation of the proportion of infected at time 0.

  • p prior average number of change points for each order.

kernel

can be "ts" if data are time series or "epi" if data are survival functions.

print_progress

If TRUE (default) print the progress bar.

user_seed

seed for random distribution generation.

Value

A DetectCpObj class object containing

  • $data vector or matrix with the data.

  • $n_iterations number of iterations.

  • $n_burnin number of burn-in iterations.

  • $orders matrix where each entries is the assignment of the realization to a block. Rows are the iterations and columns the times.

  • $time computational time.

  • $gammaMCMC traceplot for \gamma.

  • $phi_MCMC_01 a 0/1 vector, the n-th element is equal to 1 if the proposed \gamma was accepted, 0 otherwise.

  • $sigma_MCMC traceplot for \sigma.

  • $sigma_MCMC_01 a 0/1 vector, the n-th element is equal to 1 if the proposed \sigma was accepted, 0 otherwise.

  • $theta_MCMC traceplot for \theta.

  • I0_MCMC traceplot for I_0.

  • I0_MCMC_01 a 0/1 vector, the n-th element is equal to 1 if the proposed I_0 was accepted, 0 otherwise.

  • kernel_ts if TRUE data are time series.

  • kernel_epi if TRUE data are survival functions.

  • $univariate_ts TRUE if data is an univariate time series, FALSE if it is a multivariate time series.

References

Martínez, A. F., & Mena, R. H. (2014). On a Nonparametric Change Point Detection Model in Markovian Regimes. Bayesian Analysis, 9(4), 823–858. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1214/14-BA878")}

Corradin, R., Danese, L., & Ongaro, A. (2022). Bayesian nonparametric change point detection for multivariate time series with missing observations. International Journal of Approximate Reasoning, 143, 26–43. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1016/j.ijar.2021.12.019")}

Corradin, R., Danese, L., KhudaBukhsh, W. R., & Ongaro, A. (2024). Model-based clustering of time-dependent observations with common structural changes. arXiv preprint arXiv:2410.09552.

Examples


## Univariate time series

data_vec <- as.numeric(c(rnorm(50,0,0.1), rnorm(50,1,0.25)))

out <- detect_cp(data = data_vec, n_iterations = 2500, n_burnin = 500,
                 params = list(a = 1, b = 1, c = 0.1), kernel = "ts")

print(out)

## Multivariate time series

data_mat <- matrix(NA, nrow = 3, ncol = 100)

data_mat[1,] <- as.numeric(c(rnorm(50,0,0.100), rnorm(50,1,0.250)))
data_mat[2,] <- as.numeric(c(rnorm(50,0,0.125), rnorm(50,1,0.225)))
data_mat[3,] <- as.numeric(c(rnorm(50,0,0.175), rnorm(50,1,0.280)))

out <- detect_cp(data = data_mat, n_iterations = 2500, n_burnin = 500,
                 params = list(m_0 = rep(0,3), k_0 = 0.25, nu_0 = 4,
                               S_0 = diag(1,3,3)), kernel = "ts")

print(out)


## Survival functions

data_mat <- matrix(NA, nrow = 1, ncol = 100)

betas <- c(rep(0.45, 25),rep(0.14,75))

inf_times <- sim_epi_data(10000, 10, 100, betas, 1/8)

inf_times_vec <- rep(0,100)
names(inf_times_vec) <- as.character(1:100)

for(j in 1:100){
 if(as.character(j) %in% names(table(floor(inf_times)))){
   inf_times_vec[j] = table(floor(inf_times))[which(names(table(floor(inf_times))) == j)]
 }
}

data_mat[1,] <- inf_times_vec

out <- detect_cp(data = data_mat, n_iterations = 500, n_burnin = 100,
                 params = list(M = 250, xi = 1/8, a0 = 40, b0 = 10), kernel = "epi")

print(out)



BayesChange documentation built on June 10, 2025, 9:09 a.m.