detector_update: Update a Detector with New Observations

View source: R/RcppExports.R

detector_updateR Documentation

Update a Detector with New Observations

Description

Adds a new observation to the detector's internal state, updates the sufficient statistics and prunes the set of candidate changepoints.

Usage

detector_update(det_ptr, y, lambda = 1)

Arguments

det_ptr

A "focus_detector" object created by detector_create().

y

Numeric vector with the new observation. For univariate detectors, this should be a scalar (length-1 vector). For multivariate detectors, this should be a vector matching the number of dimensions.

lambda

Numeric scalar. Weight of the new observation: the internal time counter is incremented by lambda rather than by one, which allows for a non-constant background rate (e.g., observation-specific exposures). Default is 1.0, i.e., one observation per update.

Value

The same "focus_detector" object that was passed in. The detector is updated in place (no copy is made), so the return value is provided only for convenience, for example to chain calls with the native pipe operator, as in det |> detector_update(y) |> get_statistics(family = "gaussian").

Examples

# Univariate example
det <- detector_create(type = "univariate")
detector_update(det, 0.5)
detector_update(det, 1.2)

# Multivariate example
det_mv <- detector_create(type = "multivariate")
detector_update(det_mv, c(0.5, 1.2, -0.3))

## Online (sequential) example
# Generate data with a changepoint
set.seed(123)
Y <- c(rnorm(500, mean = 0), rnorm(500, mean = 1))
det <- detector_create(type = "univariate")
stat_trace <- numeric(length(Y))
threshold <- 20
for (i in seq_along(Y)) {
  detector_update(det, Y[i])
  r <- get_statistics(det, family = "gaussian")
  stat_trace[i] <- r$stat
  if (!is.null(r$stat) && r$stat > threshold) {
    cat("Online detection at", i, "estimate tau =", r$changepoint, "\n")
    plot(stat_trace[1:i], type = "l", ylab = "Test Statistic", xlab = "Time")
    break
  }
}



focus documentation built on Sept. 17, 2026, 1:08 a.m.