NAG Library Function Document
nag_tsa_cp_binary_user (g13nec)
1 Purpose
nag_tsa_cp_binary_user (g13nec) 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.
2 Specification
#include <nag.h> |
#include <nagg13.h> |
|
3 Description
Let denote a series of data and denote a set of ordered (strictly monotonic increasing) indices known as change points with and . For ease of notation we also define . The change points, , split the data into segments, with the th segment being of length and containing .
Given a cost function,
, nag_tsa_cp_binary_user (g13nec) gives an approximate solution to
where
is a penalty term used to control the number of change points. The solution is obtained in an iterative manner as follows:
1. |
Set , and |
2. |
Set . If , where is a user-supplied control parameter, then terminate the process for this segment. |
3. |
Find that minimizes
|
4. |
Test
|
5. |
If inequality (1) is false then the process is terminated for this segment. |
6. |
If inequality (1) is true, then is added to the set of change points, and the segment is split into two subsegments, and . The whole process is repeated from step 2 independently on each subsegment, with the relevant changes to the definition of and (i.e., is set to when processing the left hand subsegment and is set to when processing the right hand subsegment. |
The change points are ordered to give .
4 References
Chen J and Gupta A K (2010) Parameteric Statisical Change Point Analysis With Applications to Genetics Medicine and Finance Second Edition Birkhäuser
5 Arguments
- 1:
– IntegerInput
-
On entry: , the length of the time series.
Constraint:
.
- 2:
– doubleInput
-
On entry:
, the penalty term.
There are a number of standard ways of setting
, including:
- SIC or BIC
- .
- AIC
- .
- Hannan-Quinn
- .
where
is the number of parameters being treated as estimated in each segment. The value of
will depend on the cost function being used.
If no penalty is required then set . Generally, the smaller the value of the larger the number of suggested change points.
- 3:
– IntegerInput
-
On entry: the minimum distance between two change points, that is .
Constraint:
.
- 4:
– IntegerInput
-
On entry:
, the maximum depth for the iterative process, which in turn puts an upper limit on the number of change points with
.
If
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:
– function, supplied by the userExternal Function
-
chgpfn must calculate a proposed change point, and the associated costs, within a specified segment.
The specification of
chgpfn is:
void |
chgpfn (Nag_TS_SegSide side,
Integer u,
Integer w,
Integer minss,
Integer *v,
double cost[],
Nag_Comm *comm, Integer *info)
|
|
- 1:
– Nag_TS_SegSideInput
-
On entry: flag indicating what
chgpfn must calculate and at which point of the Binary Segmentation it has been called.
- only need be calculated and returned in , neither v nor the other elements of cost need be set. In this case, and .
- all elements of cost and v must be set. In this case, and .
- the segment, , is a left hand side subsegment from a previous iteration of the Binary Segmentation algorithm. All elements of cost and v must be set.
- the segment, , 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
and
may allow for
chgpfn to be implemented in a more efficient manner. See section
Section 10 for one such example.
The first call to
chgpfn will always have
and the second call will always have
. All subsequent calls will be made with
or
.
- 2:
– IntegerInput
-
On entry: , the start of the segment of interest.
- 3:
– IntegerInput
-
On entry: , the end of the segment of interest.
- 4:
– IntegerInput
-
On entry: the minimum distance between two change points, as passed to nag_tsa_cp_binary_user (g13nec).
- 5:
– Integer *Output
-
On exit: if
then
v need not be set.
if
then
, the proposed change point. That is, the value which minimizes
for
to
.
- 6:
– doubleOutput
-
On exit: costs associated with the proposed change point,
.
If
then
and the remaining two elements of
cost need not be set.
If
then
- .
- .
- .
- 7:
– Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to
chgpfn.
- user – double *
- iuser – Integer *
- p – Pointer
The type Pointer will be
void *. Before calling nag_tsa_cp_binary_user (g13nec) you may allocate memory and initialize these pointers with various quantities for use by
chgpfn when called from nag_tsa_cp_binary_user (g13nec) (see
Section 3.2.1.1 in the Essential Introduction).
- 8:
– Integer *Input/Output
-
On entry: .
On exit: in most circumstances
info should remain unchanged.
If
info is set to a strictly positive value then nag_tsa_cp_binary_user (g13nec) terminates with
NE_USER_STOP.
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 nag_tsa_cp_binary_user (g13nec) continues as normal. If
info was set to a strictly negative value at any point and no other errors occur then nag_tsa_cp_binary_user (g13nec) will terminate with
NW_POTENTIAL_PROBLEM.
- 6:
– Integer *Output
-
On exit: , the number of change points detected.
- 7:
– IntegerOutput
-
Note: the dimension,
dim, of the array
tau
must be at least
- when ;
- otherwise.
On exit: the first
elements of
tau hold the location of the change points. The
th segment is defined by
to
, where
and
.
The remainder of
tau is used as workspace.
- 8:
– Nag_Comm *
-
The NAG communication argument (see
Section 3.2.1.1 in the Essential Introduction).
- 9:
– NagError *Input/Output
-
The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
See
Section 3.2.1.2 in the Essential Introduction for further information.
- NE_BAD_PARAM
-
On entry, argument had an illegal value.
- NE_INT
-
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
-
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
An unexpected error has been triggered by this function. Please contact
NAG.
See
Section 3.6.6 in the Essential Introduction for further information.
- NE_NO_LICENCE
-
Your licence key may have expired or may not have been installed correctly.
See
Section 3.6.5 in the Essential Introduction for further information.
- NE_USER_STOP
-
User requested termination by setting .
- NW_POTENTIAL_PROBLEM
-
User requested a segment to be skipped by setting .
7 Accuracy
Not applicable.
8 Parallelism and Performance
nag_tsa_cp_binary_user (g13nec) 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 function. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
nag_tsa_cp_binary (g13ndc) 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
nag_tsa_cp_binary (g13ndc) can be used without the need to provide a cost function routine.
10 Example
This example identifies changes in the scale parameter, under the assumption that the data has a gamma distribution, for a simulated dataset with observations. A penalty, of is used and the minimum segment size is set to . The shape parameter is fixed at across the whole input series.
The cost function used is
where
is a shape parameter that is fixed for all segments and
.
10.1 Program Text
Program Text (g13nece.c)
10.2 Program Data
Program Data (g13nece.d)
10.3 Program Results
Program Results (g13nece.r)
This example plot shows the original data series and the estimated change points.