pub struct Pelt { /* private fields */ }Expand description
PELT changepoint detector.
Implements the Pruned Exact Linear Time algorithm for detecting multiple changepoints in a univariate time series.
§Examples
use u_analytics::detection::{Pelt, CostFunction, Penalty};
// Data with a mean shift at index 50
let mut data: Vec<f64> = vec![0.0; 50];
data.extend(vec![5.0; 50]);
let pelt = Pelt::new(CostFunction::L2, Penalty::Bic).unwrap();
let result = pelt.detect(&data);
assert!(!result.changepoints.is_empty());
// The detected changepoint should be near index 50
assert!((result.changepoints[0] as i64 - 50).unsigned_abs() <= 2);§References
Killick, R., Fearnhead, P., & Eckley, I.A. (2012). “Optimal Detection of Changepoints with a Linear Computational Cost”, JASA 107(500), pp. 1590-1598.
Implementations§
Source§impl Pelt
impl Pelt
Sourcepub fn new(cost: CostFunction, penalty: Penalty) -> Option<Self>
pub fn new(cost: CostFunction, penalty: Penalty) -> Option<Self>
Creates a new PELT detector with the given cost function and penalty.
Uses a default minimum segment length of 2.
§Returns
None if a custom penalty is not positive or not finite.
§Reference
Killick et al. (2012), §2.2: penalty must be positive to avoid trivial solutions (changepoint at every observation).
Sourcepub fn with_min_segment_len(
cost: CostFunction,
penalty: Penalty,
min_segment_len: usize,
) -> Option<Self>
pub fn with_min_segment_len( cost: CostFunction, penalty: Penalty, min_segment_len: usize, ) -> Option<Self>
Creates a PELT detector with a custom minimum segment length.
§Parameters
cost: Cost function for segment evaluationpenalty: Penalty per changepointmin_segment_len: Minimum number of observations per segment. Must be >= 2 (needed for variance estimation in Normal cost).
§Returns
None if parameters are invalid.
Sourcepub fn detect(&self, data: &[f64]) -> PeltResult
pub fn detect(&self, data: &[f64]) -> PeltResult
Detects changepoints in the given data.
Returns a PeltResult containing the detected changepoint indices.
If the data is too short (fewer than 2 * min_segment_len observations),
no changepoints can be detected and an empty result is returned.
Non-finite values (NaN, Infinity) are not supported in the input. The data should be pre-cleaned; non-finite values will lead to incorrect cost computations.
§Examples
use u_analytics::detection::{Pelt, CostFunction, Penalty};
// Two changepoints: shift up at 30, shift down at 70
let mut data = vec![0.0; 30];
data.extend(vec![3.0; 40]);
data.extend(vec![0.0; 30]);
let pelt = Pelt::new(CostFunction::L2, Penalty::Bic).unwrap();
let result = pelt.detect(&data);
assert_eq!(result.changepoints.len(), 2);§Complexity
Expected O(n), worst case O(n^2).
Sourcepub fn detect_multi(&self, signals: &[&[f64]]) -> Option<MultiPeltResult>
pub fn detect_multi(&self, signals: &[&[f64]]) -> Option<MultiPeltResult>
Detects changepoints in multivariate (multi-signal) data.
Each inner slice represents one signal channel. All channels must have the same length. The cost function is applied independently to each channel and summed — a single set of changepoints is returned that applies to all channels simultaneously.
The penalty scales with the number of channels: penalty * n_channels.
§Parameters
signals: slice of signal channels, each of lengthn
§Returns
None if channels have inconsistent lengths.
Otherwise returns Some(MultiPeltResult).
§Examples
use u_analytics::detection::{Pelt, CostFunction, Penalty};
let signal_a: Vec<f64> = [vec![0.0; 50], vec![5.0; 50]].concat();
let signal_b: Vec<f64> = [vec![0.0; 50], vec![3.0; 50]].concat();
let pelt = Pelt::new(CostFunction::L2, Penalty::Bic).unwrap();
let result = pelt.detect_multi(&[&signal_a, &signal_b]).unwrap();
assert!(!result.changepoints.is_empty());§Reference
Killick, R. & Eckley, I.A. (2014). “changepoint: An R Package for Changepoint Analysis”, Journal of Statistical Software 58(3).
§Complexity
Expected O(n * k), worst case O(n^2 * k) where k = number of channels.