g13nef detects change points in a univariate time series, that is, the time points at which some feature of the data, for example the mean, changes. Change points are detected using binary segmentation for a user-supplied cost function.
The routine may be called by the names g13nef or nagf_tsa_cp_binary_user.
3Description
Let ${y}_{1:n}=\{{y}_{j}:j=1,2,\dots ,n\}$ denote a series of data and $\tau =\{{\tau}_{i}:i=1,2,\dots ,m\}$ denote a set of $m$ ordered (strictly monotonic increasing) indices known as change points with $1\le {\tau}_{i}\le n$ and ${\tau}_{m}=n$. For ease of notation we also define ${\tau}_{0}=0$. The $m$ change points, $\tau $, split the data into $m$ segments, with the $i$th segment being of length ${n}_{i}$ and containing ${y}_{{\tau}_{i-1}+1:{\tau}_{i}}$.
Given a cost function, $C\left({y}_{{\tau}_{i-1}+1:{\tau}_{i}}\right)$, g13nef gives an approximate solution to
5.If inequality (1) is false then the process is terminated for this segment.
6.If inequality (1) is true, then $v$ is added to the set of change points, and the segment is split into two subsegments, ${y}_{u:v}$ and ${y}_{v+1:w}$. The whole process is repeated from step 2 independently on each subsegment, with the relevant changes to the definition of $u$ and $w$ (i.e., $w$ is set to $v$ when processing the left-hand subsegment and $u$ is set to $v+1$ when processing the right-hand subsegment.
The change points are ordered to give $\tau $.
4References
Chen J and Gupta A K (2010) Parametric Statistical Change Point Analysis With Applications to GeneticsMedicine and FinanceSecond Edition Birkhäuser
5Arguments
1: $\mathbf{n}$ – IntegerInput
On entry: $n$, the length of the time series.
Constraint:
${\mathbf{n}}\ge 2$.
2: $\mathbf{beta}$ – Real (Kind=nag_wp)Input
On entry: $\beta $, the penalty term.
There are a number of standard ways of setting $\beta $, including:
where $p$ is the number of parameters being treated as estimated in each segment. The value of $p$ will depend on the cost function being used.
If no penalty is required then set $\beta =0$. Generally, the smaller the value of $\beta $ the larger the number of suggested change points.
3: $\mathbf{minss}$ – IntegerInput
On entry: the minimum distance between two change points, that is ${\tau}_{i}-{\tau}_{i-1}\ge {\mathbf{minss}}$.
Constraint:
${\mathbf{minss}}\ge 2$.
4: $\mathbf{mdepth}$ – IntegerInput
On entry: $K$, the maximum depth for the iterative process, which in turn puts an upper limit on the number of change points with $m\le {2}^{K}$.
If $K\le 0$ then no limit is put on the depth of the iterative process and no upper limit is put on the number of change points, other than that inherent in the length of the series and the value of minss.
5: $\mathbf{chgpfn}$ – Subroutine, supplied by the user.External Procedure
chgpfn must calculate a proposed change point, and the associated costs, within a specified segment.
On entry: flag indicating what chgpfn must calculate and at which point of the Binary Segmentation it has been called.
${\mathbf{side}}=\mathrm{-1}$
only $C\left({y}_{u:w}\right)$ need be calculated and returned in ${\mathbf{cost}}\left(1\right)$, neither v nor the other elements of cost need be set. In this case, $u=1$ and $w=\mathrm{n}$.
${\mathbf{side}}=0$
all elements of cost and v must be set. In this case, $u=1$ and $w=\mathrm{n}$.
${\mathbf{side}}=1$
the segment, ${y}_{u:w}$, is a left-hand side subsegment from a previous iteration of the Binary Segmentation algorithm. All elements of cost and v must be set.
${\mathbf{side}}=2$
the segment, ${y}_{u:w}$, is a right-hand side subsegment from a previous iteration of the Binary Segmentation algorithm. All elements of cost and v must be set.
The distinction between ${\mathbf{side}}=1$ and $2$ may allow for chgpfn to be implemented in a more efficient manner.
The first call to chgpfn will always have ${\mathbf{side}}=\mathrm{-1}$ and the second call will always have ${\mathbf{side}}=0$. All subsequent calls will be made with ${\mathbf{side}}=1$ or $2$.
2: $\mathbf{u}$ – IntegerInput
On entry: $u$, the start of the segment of interest.
3: $\mathbf{w}$ – IntegerInput
On entry: $w$, the end of the segment of interest.
4: $\mathbf{minss}$ – IntegerInput
On entry: the minimum distance between two change points, as passed to g13nef.
5: $\mathbf{v}$ – IntegerOutput
On exit: if ${\mathbf{side}}=\mathrm{-1}$ then v need not be set.
if ${\mathbf{side}}\ne \mathrm{-1}$ then $v$, the proposed change point. That is, the value which minimizes
9: $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
chgpfn is called with the arguments iuser and ruser as supplied to g13nef. You should use the arrays iuser and ruser to supply information to chgpfn.
10: $\mathbf{info}$ – IntegerInput/Output
On entry: ${\mathbf{info}}=0$.
On exit: in most circumstances info should remain unchanged.
If info is set to a strictly positive value then g13nef terminates with ${\mathbf{ifail}}={\mathbf{51}}$.
If info is set to a strictly negative value the current segment is skipped (i.e., no change points are considered in this segment) and g13nef continues as normal. If info was set to a strictly negative value at any point and no other errors occur then g13nef will terminate with ${\mathbf{ifail}}={\mathbf{52}}$.
chgpfn must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which g13nef is called. Arguments denoted as Input must not be changed by this procedure.
Note:chgpfn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g13nef. If your code inadvertently does return any NaNs or infinities, g13nef is likely to produce unexpected results.
6: $\mathbf{ntau}$ – IntegerOutput
On exit: $m$, the number of change points detected.
7: $\mathbf{tau}(*)$ – Integer arrayOutput
Note: the dimension of the array tau
must be at least
$\mathrm{min}\phantom{\rule{0.125em}{0ex}}(\lceil \frac{{\mathbf{n}}}{{\mathbf{minss}}}\rceil ,{2}^{{\mathbf{mdepth}}})$ if ${\mathbf{mdepth}}>0$, and at least $\lceil \frac{{\mathbf{n}}}{{\mathbf{minss}}}\rceil $ otherwise.
On exit: the first $m$ elements of tau hold the location of the change points. The $i$th segment is defined by ${y}_{({\tau}_{i-1}+1)}$ to ${y}_{{\tau}_{i}}$, where ${\tau}_{0}=0$ and ${\tau}_{i}={\mathbf{tau}}\left(i\right),1\le i\le m$.
8: $\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Data
y is not used by g13nef, but is passed directly to chgpfn and may be used to pass information to this routine. y will usually be used to pass (functions of) the time series, $y$ of interest.
10: $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
iuser and ruser are not used by g13nef, but are passed directly to chgpfn and may be used to pass information to this routine.
11: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=11$
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{n}}\ge 2$.
${\mathbf{ifail}}=31$
On entry, ${\mathbf{minss}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{minss}}\ge 2$.
${\mathbf{ifail}}=51$
User requested termination by setting ${\mathbf{info}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=52$
User requested a segment to be skipped by setting ${\mathbf{info}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
Not applicable.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
g13nef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.
9Further Comments
g13ndf performs the same calculations for a cost function selected from a provided set of cost functions. If the required cost function belongs to this provided set then g13ndf can be used without the need to provide a cost function routine.
10Example
This example identifies changes in the scale parameter, under the assumption that the data has a gamma distribution, for a simulated dataset with $100$ observations. A penalty, $\beta $ of $3.6$ is used and the minimum segment size is set to $3$. The shape parameter is fixed at $2.1$ across the whole input series.