Learn R Programming

BayesChange (version 2.1.3)

detect_cp: 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
)

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.

  • $phi_MCMC 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.

  • $delta_MCMC traceplot for \(\delta\).

  • 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 epidemic diffusions.

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

Arguments

data

if kernel = "ts" a vector or a matrix. If kernel = "epi" a matrix.

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 \(\delta\).

  • prior_delta_c parameter of the shifted Gamma prior of \(\delta\).

  • prior_delta_d parameter of the shifted Gamma prior of \(\delta\).

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 \(\delta\).

  • prior_delta_c parameter of the shifted Gamma prior of \(\delta\).

  • prior_delta_d parameter of the shifted Gamma prior of \(\delta\).

If data are epidemic diffusions:

  • M number of Monte Carlo iterations when computing the likelihood of the epidemic diffusion.

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

  • a0,b0 parameters for the computation of the integrated likelihood of the epidemic diffusions.

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

kernel

can be "ts" if data are time series or "epi" if data are epidemic diffusions.

print_progress

If TRUE (default) print the progress bar.

user_seed

seed for random distribution generation.

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

Run this code

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

# \donttest{
## Epidemic diffusions

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

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

Run the code above in your browser using DataLab