An R6 class specifying a cost function
Details
Creates an instance of costFunc R6 class, used in initialisation of change-point detection modules. Currently
supports the following cost functions:
L1 cost function: $$c_{L_1}(y_{(a+1)...b}) := \sum_{t = a+1}^{b} \| y_t - \tilde{y}_{(a+1)...b} \|_1$$ where \(\tilde{y}_{(a+1)...b}\) is the coordinate-wise median of the segment. If \(a \ge b - 1\), return 0.
L2 cost function: $$c_{L_2}(y_{(a+1)...b}) := \sum_{t = a+1}^{b} \| y_t - \bar{y}_{(a+1)...b} \|_2^2$$ where \(\bar{y}_{(a+1)...b}\) is the empirical mean of the segment. If \(a \ge b - 1\), return 0.
SIGMA cost function: $$c_{\Sigma}(y_{(a+1)...b}) := (b - a)\log \det \hat{\Sigma}_{(a+1)...b}$$ where \(\hat{\Sigma}_{(a+1)...b}\) is the empirical covariance matrix of the segment without Bessel's correction. Here, if
addSmallDiag = TRUE, a small biasepsilonis added to the diagonal of estimated covariance matrices to improve numerical stability.
By default,addSmallDiag = TRUEandepsilon = 1e-6. In caseaddSmallDiag = TRUE, if the covariance matrix is numerically singular (its log-determinant cannot be computed) or its log-determinant is smaller than the lower boundp*log(epsilon), return(b - a)*p*log(epsilon), otherwise, output an error message.VAR(r) cost function: $$c_{\mathrm{VAR}}(y_{(a+1)...b}) := \sum_{t = \max(a, r)+1}^{b} \left\| y_t - \hat c - \sum_{j=1}^r \hat A_j y_{t-j} \right\|_2^2$$ where \(\hat c\) and \(\hat A_j\) are the OLS estimates of the intercept and VAR coefficients on the segment. The lagged values \(y_{t-j}\) may come from before \(a+1\), so only the first \(r\) observations of the whole series lack a full set of lags. If the system is singular, an approximate (minimum-norm) least-squares solve is used. If \(b-a < p*r+1\) (i.e., not enough observations), or \(a > n-r\) (where
nis the time series length), return 0."LinearL2" for piecewise linear regression process with constant noise variance $$c_{\text{LinearL2}}(y_{(a+1):b}) := \sum_{t=a+1}^b \| y_t - X_t \hat{\beta} \|_2^2$$ where \(\hat{\beta}\) are OLS estimates on segment \((a+1):b\). If segment is shorter than the minimum number of points needed for OLS, return 0.
"LinearSIGMA" for piecewise linear regression process with varying noise covariance $$c_{\text{LinearSIGMA}}(y_{(a+1):b}) := (b-a)\log \det \hat\Sigma_{(a+1):b}$$ where \(\hat\Sigma_{(a+1):b}\) is the empirical covariance matrix of OLS residuals \(y - X\hat{\beta}\) on segment \((a+1):b\), divided by \(b-a\) with no degrees-of-freedom correction for the fitted coefficients, and otherwise estimated the same way as in the SIGMA cost function (including the
addSmallDiag/epsilonstabilisation and lower-bound fallback)."LinearL1" for piecewise linear regression process under L1 (least absolute deviations) loss $$c_{\text{LinearL1}}(y_{(a+1):b}) := \sum_{t=a+1}^b \| y_t - X_t \hat{\beta} \|_1$$ where \(\hat{\beta}\) is fit column-by-column via Iteratively Reweighted Least Squares (IRLS), iterated until the change in the fit's cost is within
tolormaxIteriterations are reached. Unlike the other regression cost functions, this has no \(O(1)\)-per-segment closed form, since IRLS must be re-run on each queried segment."Custom" for a user-defined cost function supplied from R $$c_{\text{Custom}}(y_{(a+1):b}) := \text{evalFun}(y_{(a+1):b}, a, b)$$ where
evalFunis a user-supplied function called on the raw segment matrix, plus the segment's own(a,b]bounds – the latter letevalFunalign the segment against any externally-captured, position-indexed data (e.g. a weight vector or exogenous seriesevalFuncloses over) without the package needing to know that data exists. Because each call crosses back into R, this is substantially slower per call than the built-in costs above; see$evalFunand$paramFun.
If active binding $costFunc is modified (via assignment operator), the default parameters will be used.
Methods
$new()Initialises a
costFuncobject.$pass()Describes the
costFuncobject.$clone()Clones the
costFuncobject.
Author
Minh Long Nguyen edelweiss611428@gmail.com
Active bindings
costFuncCharacter. Cost function. Can be accessed or modified via
$costFunc. IfcostFuncis modified and required parameters are missing, the default parameters are used.pVARInteger. Vector autoregressive order. Can be accessed or modified via
$pVAR.addSmallDiagLogical. Whether to add a bias value to the diagonal of estimated covariance matrices to stabilise matrix operations. Can be accessed or modified via
$addSmallDiag.epsilonDouble. A bias value added to the diagonal of estimated covariance matrices to stabilise matrix operations. Can be accessed or modified via
$epsilon.interceptLogical. Whether to include the intercept in regression problems. Can be accessed or modified via
$intercept.tolDouble. IRLS convergence tolerance: iteration stops once the change in the fit's cost falls below
tol. Can be accessed or modified via$tol.maxIterInteger. Maximum number of IRLS iterations. Can be accessed or modified via
$maxIter.evalFunFunction. Required for
costFunc = "Custom". A user-defined cost function, called asevalFun(segment, a, b), wheresegmentis the numeric matrix of rows(a+1):bfor the queried segment(a,b](0-indexed, same convention as$eval(a, b)).aandbletevalFunalignsegmentagainst externally-captured, position-indexed data it closes over (e.g.externalSeries[(a+1):b]), which the package itself never needs to see. Must return a single numeric value. Can be accessed or modified via$evalFun.paramFunFunction or
NULL. Optional forcostFunc = "Custom". A user-defined function called asparamFun(segment, a, b)(same convention asevalFun), used by$get_params()to report segment-level estimates. IfNULL(default),$get_params()returns an empty list for"Custom". Can be accessed or modified via$paramFun.
Methods
Method new()
Initialises a costFunc object.
Usage
costFunc$new(costFunc, ...)Arguments
costFuncCharacter. Cost function. Supported values include
"L2","VAR", and"SIGMA". Default:L2....Optional named parameters required by specific cost functions.
If any required parameters are missing or null, default values will be used.For
"L1"and"L2", there is no extra parameter.For
"SIGMA", supported parameters are:addSmallDiagLogical. If
TRUE, add a small value to the diagonal of estimated covariance matrices to stabilise matrix operations. Default:TRUE.epsilonDouble. If
addSmallDiag = TRUE, a small positive value added to the diagonal of estimated covariance matrices to stabilise matrix operations. Default:1e-6.
For
"VAR",pVARis required:pVARInteger. Vector autoregressive order. Must be a positive integer. Default:
1L.
For
"LinearL2",interceptis required:interceptLogical. Whether to include the intercept in regression problems. Default:
TRUE.
For
"LinearSIGMA", supported parameters are:interceptLogical. Whether to include the intercept in regression problems. Default:
TRUE.addSmallDiagLogical. If
TRUE, add a small value to the diagonal of estimated residual covariance matrices to stabilise matrix operations. Default:TRUE.epsilonDouble. If
addSmallDiag = TRUE, a small positive value added to the diagonal of estimated residual covariance matrices to stabilise matrix operations. Default:1e-6.
For
"LinearL1", supported parameters are:interceptLogical. Whether to include the intercept in regression problems. Default:
TRUE.tolDouble. IRLS convergence tolerance: iteration stops once the change in the fit's cost falls below
tol. Default:1e-6.maxIterInteger. Maximum number of IRLS iterations. Default:
1000L.
For
"Custom", supported parameters are:evalFunFunction. Required. See
$evalFunfor details.paramFunFunction or
NULL. Optional. See$paramFunfor details.
Examples
## L2 costFunc (default)
costFuncObj = costFunc$new()
costFuncObj$pass()
#> $costFunc
#> [1] "L2"
#>
## SIGMA costFunc
costFuncObj = costFunc$new(costFunc = "SIGMA")
costFuncObj$pass()
#> $costFunc
#> [1] "SIGMA"
#>
#> $addSmallDiag
#> [1] TRUE
#>
#> $epsilon
#> [1] 1e-06
#>
# Modify active bindings
costFuncObj$epsilon = 10^-5
costFuncObj$pass()
#> $costFunc
#> [1] "SIGMA"
#>
#> $addSmallDiag
#> [1] TRUE
#>
#> $epsilon
#> [1] 1e-05
#>
costFuncObj$costFunc = "VAR"
costFuncObj$pass()
#> $costFunc
#> [1] "VAR"
#>
#> $pVAR
#> [1] 1
#>