NAG CL Interface
g13nec (cp_​binary_​user)

1 Purpose

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>
void  g13nec (Integer n, double beta, Integer minss, Integer mdepth,
void (*chgpfn)(Nag_TS_SegSide side, Integer u, Integer w, Integer minss, Integer *v, double cost[], Nag_Comm *comm, Integer *info),
Integer *ntau, Integer tau[], Nag_Comm *comm, NagError *fail)
The function may be called by the names: g13nec or nag_tsa_cp_binary_user.

3 Description

Let y1:n=yj:j=1,2,,n denote a series of data and τ=τi:i=1,2,,m denote a set of m ordered (strictly monotonic increasing) indices known as change points with 1τin and τm=n. For ease of notation we also define τ0=0. The m change points, τ, split the data into m segments, with the ith segment being of length ni and containing yτi-1+1:τi.
Given a cost function, Cyτi-1+1:τi, g13nec gives an approximate solution to
minimize m,τ i=1 m Cyτi-1+1:τi + β  
where β is a penalty term used to control the number of change points. The solution is obtained in an iterative manner as follows:
  1. 1.Set u=1, w=n and k=0
  2. 2.Set k=k+1. If k>K, where K is a user-supplied control parameter, then terminate the process for this segment.
  3. 3.Find v that minimizes
    Cyu:v + Cyv+1:w  
  4. 4.Test
    Cyu:v + Cyv+1:w + β < Cyu:w (1)
  5. 5.If inequality (1) is false then the process is terminated for this segment.
  6. 6.If inequality (1) is true, then v is added to the set of change points, and the segment is split into two subsegments, yu:v and yv+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 τ.

4 References

Chen J and Gupta A K (2010) Parametric Statistical Change Point Analysis With Applications to Genetics Medicine and Finance Second Edition Birkhäuser

5 Arguments

1: n Integer Input
On entry: n, the length of the time series.
Constraint: n2.
2: beta double Input
On entry: β, the penalty term.
There are a number of standard ways of setting β, including:
SIC or BIC
β=p×logn.
AIC
β=2p.
Hannan-Quinn
β=2p×loglogn.
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 β=0. Generally, the smaller the value of β the larger the number of suggested change points.
3: minss Integer Input
On entry: the minimum distance between two change points, that is τi-τi-1minss.
Constraint: minss2.
4: mdepth Integer Input
On entry: K, the maximum depth for the iterative process, which in turn puts an upper limit on the number of change points with m2K.
If K0 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: chgpfn function, supplied by the user External 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: side Nag_TS_SegSide Input
On entry: flag indicating what chgpfn must calculate and at which point of the Binary Segmentation it has been called.
side=Nag_FirstSegCall
only Cyu:w need be calculated and returned in cost[0], neither v nor the other elements of cost need be set. In this case, u=1 and w=n.
side=Nag_SecondSegCall
all elements of cost and v must be set. In this case, u=1 and w=n.
side=Nag_LeftSubSeg
the segment, yu: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.
side=Nag_RightSubSeg
the segment, yu: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 side=Nag_LeftSubSeg and Nag_RightSubSeg may allow for chgpfn to be implemented in a more efficient manner. See Section 10 for one such example.
The first call to chgpfn will always have side=Nag_FirstSegCall and the second call will always have side=Nag_SecondSegCall. All subsequent calls will be made with side=Nag_LeftSubSeg or Nag_RightSubSeg.
2: u Integer Input
On entry: u, the start of the segment of interest.
3: w Integer Input
On entry: w, the end of the segment of interest.
4: minss Integer Input
On entry: the minimum distance between two change points, as passed to g13nec.
5: v Integer * Output
On exit: if side=Nag_FirstSegCall then v need not be set.
if sideNag_FirstSegCall then v, the proposed change point. That is, the value which minimizes
minimize v Cyu:v + Cyv+1:w  
for v=u+minss-1 to w-minss.
6: cost[3] double Output
On exit: costs associated with the proposed change point, v.
If side=Nag_FirstSegCall then cost[0]=Cyu:w and the remaining two elements of cost need not be set.
If sideNag_FirstSegCall then
  • cost[0]= Cyu:v + Cyv+1:w .
  • cost[1]= Cyu:v .
  • cost[2]= Cyv+1:w .
7: comm Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to chgpfn.
userdouble *
iuserInteger *
pPointer 
The type Pointer will be void *. Before calling g13nec you may allocate memory and initialize these pointers with various quantities for use by chgpfn when called from g13nec (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
8: info Integer * Input/Output
On entry: info=0.
On exit: in most circumstances info should remain unchanged.
If info is set to a strictly positive value then g13nec terminates with fail.code= 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 g13nec continues as normal. If info was set to a strictly negative value at any point and no other errors occur then g13nec will terminate with fail.code= NW_POTENTIAL_PROBLEM.
Note: chgpfn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g13nec. If your code inadvertently does return any NaNs or infinities, g13nec is likely to produce unexpected results.
6: ntau Integer * Output
On exit: m, the number of change points detected.
7: tau[dim] Integer Output
Note: the dimension, dim, of the array tau must be at least
  • minnminss,2mdepth, when mdepth>0;
  • nminss, otherwise.
On exit: the first m elements of tau hold the location of the change points. The ith segment is defined by yτi-1+1 to yτi, where τ0=0 and τi=tau[i-1],1im.
The remainder of tau is used as workspace.
8: comm Nag_Comm *
The NAG communication argument (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
9: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, minss=value.
Constraint: minss2.
On entry, n=value.
Constraint: n2.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_USER_STOP
User requested termination by setting info=value.
NW_POTENTIAL_PROBLEM
User requested a segment to be skipped by setting info=value.

7 Accuracy

Not applicable.

8 Parallelism and Performance

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.

9 Further Comments

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 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 100 observations. A penalty, β 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.
The cost function used is
Cyτi-1+1:τi = 2 a ni logSi - log a ni  
where a is a shape parameter that is fixed for all segments and ni=τi-τi-1+1.

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.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 0.5 1 1.5 2 2.5 3 3.5 4 0 10 20 30 40 50 60 70 80 90 100 Value Time Example Program Simulated time series and the corresponding changes in scale b, assuming y = Ga(2.1,b) gnuplot_plot_1