G13NEF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G13NEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

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.

2  Specification

SUBROUTINE G13NEF ( N, BETA, MINSS, MDEPTH, CHGPFN, NTAU, TAU, Y, IUSER, RUSER, IFAIL)
INTEGER  N, MINSS, MDEPTH, NTAU, TAU(*), IUSER(*), IFAIL
REAL (KIND=nag_wp)  BETA, Y(*), RUSER(*)
EXTERNAL  CHGPFN

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, G13NEF 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. Set u=1, w=n and k=0
2. Set k=k+1. If k>K, where K is a user-supplied control parameter, then terminate the process for this segment.
3. Find v that minimizes
Cyu:v + Cyv+1:w  
4. Test
Cyu:v + Cyv+1:w + β < Cyu:w (1)
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, 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) Parameteric Statisical Change Point Analysis With Applications to Genetics Medicine and Finance Second Edition Birkhäuser

5  Parameters

1:     N – INTEGERInput
On entry: n, the length of the time series.
Constraint: N2.
2:     BETA – REAL (KIND=nag_wp)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 – INTEGERInput
On entry: the minimum distance between two change points, that is τi-τi-1MINSS.
Constraint: MINSS2.
4:     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 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 – SUBROUTINE, supplied by the user.External Procedure
CHGPFN must calculate a proposed change point, and the associated costs, within a specified segment.
The specification of CHGPFN is:
SUBROUTINE CHGPFN ( SIDE, U, W, MINSS, V, COST, Y, IUSER, RUSER, INFO)
INTEGER  SIDE, U, W, MINSS, V, IUSER(*), INFO
REAL (KIND=nag_wp)  COST(3), Y(*), RUSER(*)
1:     SIDE – INTEGERInput
On entry: flag indicating what CHGPFN must calculate and at which point of the Binary Segmentation it has been called.
SIDE=-1
only Cyu:w need be calculated and returned in COST1, neither V nor the other elements of COST need be set. In this case, u=1 and w=n.
SIDE=0
all elements of COST and V must be set. In this case, u=1 and w=n.
SIDE=1
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=2
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=1 and 2 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 SIDE=-1 and the second call will always have SIDE=0. All subsequent calls will be made with SIDE=1 or 2.
2:     U – INTEGERInput
On entry: u, the start of the segment of interest.
3:     W – INTEGERInput
On entry: w, the end of the segment of interest.
4:     MINSS – INTEGERInput
On entry: the minimum distance between two change points, as passed to G13NEF.
5:     V – INTEGEROutput
On exit: if SIDE=-1 then V need not be set.
if SIDE-1 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:     COST3 – REAL (KIND=nag_wp) arrayOutput
On exit: costs associated with the proposed change point, v.
If SIDE=-1 then COST1=Cyu:w and the remaining two elements of COST need not be set.
If SIDE-1 then
  • COST1= Cyu:v + Cyv+1:w .
  • COST2= Cyu:v .
  • COST3= Cyv+1:w .
7:     Y* – REAL (KIND=nag_wp) arrayUser Data
CHGPFN is called with Y as supplied to G13NEF. You are free to use the array Y to supply information to CHGPFN.
Y is supplied in addition to IUSER and RUSER for ease of coding as in most cases CHGPFN will require (functions of) the time series, y.
8:     IUSER* – INTEGER arrayUser Workspace
9:     RUSER* – REAL (KIND=nag_wp) arrayUser Workspace
CHGPFN is called with the parameters IUSER and RUSER as supplied to G13NEF. You are free to use the arrays IUSER and RUSER to supply information to CHGPFN as an alternative to using COMMON global variables.
10:   INFO – INTEGERInput/Output
On entry: INFO=0.
On exit: in most circumstances INFO should remain unchanged.
If INFO is set to a strictly positive value then G13NEF terminates with IFAIL=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 IFAIL=52.
CHGPFN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which G13NEF is called. Parameters denoted as Input must not be changed by this procedure.
6:     NTAU – INTEGEROutput
On exit: m, the number of change points detected.
7:     TAU* – INTEGER arrayOutput
Note: the dimension of the array TAU must be at least minNMINSS,2MDEPTH if MDEPTH>0, and at least 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=TAUi,1im.
The remainder of TAU is used as workspace.
8:     Y* – 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.
9:     IUSER* – INTEGER arrayUser Workspace
10:   RUSER* – 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 as an alternative to using COMMON global variables.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=11
On entry, N=value.
Constraint: N2.
IFAIL=31
On entry, MINSS=value.
Constraint: MINSS2.
IFAIL=51
User requested termination by setting INFO=value.
IFAIL=52
User requested a segment to be skipped by setting INFO=value.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

Not applicable.

8  Parallelism and Performance

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.

9  Further 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.

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 (g13nefe.f90)

10.2  Program Data

Program Data (g13nefe.d)

10.3  Program Results

Program Results (g13nefe.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

G13NEF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015