An R6 class for fast segment-cost evaluation and parameter estimation, without a detection algorithm.
Details
costFactory builds the C++ cost module selected by $costFunc at $fit(), and keeps it, so every query reuses
its precomputations, as PELT, binSeg and Window do. $eval() and $get_params() call the module directly:
segments are (a, b] with 0-based a, and all checks are done in C++.
$new() only stores the costFunc object; $fit() validates and attaches the data. $costFunc is an active
binding, so it can be inspected or replaced after construction – if data has already been supplied, replacing it
automatically triggers $fit() again.
$get_params() returns:
"L1":median."L2":mean."SIGMA":meanandcov(plusepsilonon the diagonal ifaddSmallDiag = TRUE)."VAR","LinearL2"and"LinearL1":coef, intercept first."LinearSIGMA":coefandcov(plusepsilonon the diagonal ifaddSmallDiag = TRUE)."Custom":params, the output ofparamFun; an empty list ifparamFunisNULL.
Both covs are biased maximum-likelihood estimates: divided by the segment length b - a, with no Bessel
(n - 1) or degrees-of-freedom correction.
Methods
$new()Initialises a
costFactoryobject.$fit()Constructs the
C++cost module.$eval()Evaluates the cost of a segment.
$get_params()Returns the parameter estimates of a segment.
$segments()Returns the cost and parameter estimates of each segment of a given segmentation.
$clone()Clones the
R6object.
Author
Minh Long Nguyen edelweiss611428@gmail.com
Huy Nhat Minh Nguyen sleepysnorlax0115@gmail.com
Active bindings
costFuncR6object of classcostFunc. Can be accessed or modified via$costFunc. ModifyingcostFuncwill automatically trigger$fit()if atsMathas already been fitted.
Methods
Method new()
Initialises a costFactory object. Does not build the C++ cost module; call $fit() for that.
Usage
costFactory$new(costFunc)Method fit()
Validates the supplied data and constructs the C++ cost module selected by $costFunc.
Arguments
tsMatNumeric matrix. Time series of size \(n \times p\). If
NULL, the method will use the previously assignedtsMat(i.e., from a prior$fit(tsMat)). Default:NULL.covariatesNumeric matrix with
nrows, used by"LinearL2","LinearSIGMA"and"LinearL1". IfNULLand no priorcovariateswere set, the model is force-fitted with only an intercept. Default:NULL.
Method get_params()
Returns the parameter estimates of the segment (a, b] with the module's get_params().
Method segments()
Returns the cost and parameter estimates of each segment of a given segmentation.
Examples
set.seed(1)
tsMat = cbind(c(rnorm(100, 0), rnorm(100, 5, 5)))
cf = costFactory$new(costFunc$new("L2"))
cf$fit(tsMat)
cf$eval(0, 100)
#> [1] 79.86945
cf$get_params(0, 100)
#> $mean
#> [1] 0.1088874
#>
cf$segments(c(100, 200))
#> [[1]]
#> [[1]]$Start
#> [1] 0
#>
#> [[1]]$End
#> [1] 100
#>
#> [[1]]$Cost
#> [1] 79.86945
#>
#> [[1]]$Params
#> [[1]]$Params$mean
#> [1] 0.1088874
#>
#>
#>
#> [[2]]
#> [[2]]$Start
#> [1] 100
#>
#> [[2]]$End
#> [1] 200
#>
#> [[2]]$Cost
#> [1] 2270.892
#>
#> [[2]]$Params
#> [[2]]$Params$mean
#> [1] 4.81096
#>
#>
#>
# `costFunc` is an active binding: swapping it re-fits automatically.
cf$costFunc = costFunc$new("SIGMA")
#> `costFunc` has been updated. Re-fitting the model.
cf$eval(0, 100)
#> [1] -22.47755