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

NAG Library Routine Document

G13NDF

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

G13NDF 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 using one of a provided set of cost functions.

2  Specification

SUBROUTINE G13NDF ( CTYPE, N, Y, BETA, MINSS, IPARAM, PARAM, MDEPTH, NTAU, TAU, SPARAM, IFAIL)
INTEGER  CTYPE, N, MINSS, IPARAM, MDEPTH, NTAU, TAU(*), IFAIL
REAL (KIND=nag_wp)  Y(N), BETA, PARAM(1), SPARAM(2*N)

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, G13NDF 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 τ.
G13NDF supplies four families of cost function. Each cost function assumes that the series, y, comes from some distribution, DΘ. The parameter space, Θ=θ,ϕ is subdivided into θ containing those parameters allowed to differ in each segment and ϕ those parameters treated as constant across all segments. All four cost functions can then be described in terms of the likelihood function, L and are given by:
C y τ i-1 + 1 : τi = -2 log L θ^i , ϕ | y τ i-1 + 1 : τi  
where the θ^i is the maximum likelihood estimate of θ within the ith segment. Four distributions are available; Normal, Gamma, Exponential and Poisson distributions. Letting
Si= j=τi-1 τi yj  
the log likelihoods and cost functions for the four distributions, and the available subdivisions of the parameter space are:

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
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

5  Parameters

1:     CTYPE – INTEGERInput
On entry: a flag indicating the assumed distribution of the data and the type of change point being looked for.
CTYPE=1
Data from a Normal distribution, looking for changes in the mean, μ.
CTYPE=2
Data from a Normal distribution, looking for changes in the standard deviation σ.
CTYPE=3
Data from a Normal distribution, looking for changes in the mean, μ and standard deviation σ.
CTYPE=4
Data from a Gamma distribution, looking for changes in the scale parameter b.
CTYPE=5
Data from an exponential distribution, looking for changes in λ.
CTYPE=6
Data from a Poisson distribution, looking for changes in λ.
Constraint: CTYPE=1, 2, 3, 4, 5 or 6.
2:     N – INTEGERInput
On entry: n, the length of the time series.
Constraint: N2.
3:     YN – REAL (KIND=nag_wp) arrayInput
On entry: y, the time series.
if CTYPE=6, that is the data is assumed to come from a Poisson distribution, y+0.5 is used in all calculations.
Constraints:
  • if CTYPE=4, 5 or 6, Yi0, for i=1,2,,N;
  • if CTYPE=6, each value of Y must be representable as an integer;
  • if CTYPE6, each value of Y must be small enough such that Yi2, for i=1,2,,N, can be calculated without incurring overflow.
4:     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. This is usually set to 2 when CTYPE=3 and 1 otherwise.
If no penalty is required then set β=0. Generally, the smaller the value of β the larger the number of suggested change points.
5:     MINSS – INTEGERInput
On entry: the minimum distance between two change points, that is τi-τi-1MINSS.
Constraint: MINSS2.
6:     IPARAM – INTEGERInput
On entry: if IPARAM=1 distributional parameters have been supplied in PARAM.
Constraints:
  • if CTYPE=4, IPARAM=1;
  • otherwise IPARAM=0 or 1.
7:     PARAM1 – REAL (KIND=nag_wp) arrayInput
On entry: ϕ, values for the parameters that will be treated as fixed. If IPARAM=0 then PARAM is not referenced.
If CTYPE=1 
  • if IPARAM=0, σ, the standard deviation of the Normal distribution, is estimated from the full input data. Otherwise σ=PARAM1.
If CTYPE=2 
  • If IPARAM=0, μ, the mean of the Normal distribution, is estimated from the full input data. Otherwise μ=PARAM1.
If CTYPE=4, PARAM1 must hold the shape, a, for the Gamma distribution, otherwise PARAM is not referenced.
Constraint: if CTYPE=1 or 4, PARAM1>0.0.
8:     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.
9:     NTAU – INTEGEROutput
On exit: m, the number of change points detected.
10:   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.
11:   SPARAM2×N – REAL (KIND=nag_wp) arrayOutput
On exit: the estimated values of the distribution parameters in each segment
CTYPE=1, 2 or 3
SPARAM2i-1=μi and SPARAM2i=σi for i=1,2,,m, where μi and σi is the mean and standard deviation, respectively, of the values of y in the ith segment.
It should be noted that σi=σj when CTYPE=1 and μi=μj when CTYPE=2, for all i and j.
CTYPE=4
SPARAM2i-1=ai and SPARAM2i=bi for i=1,2,,m, where ai and bi are the shape and scale parameters, respectively, for the values of y in the ith segment. It should be noted that ai=PARAM1 for all i.
CTYPE=5 or 6
SPARAMi=λi for i=1,2,,m, where λi is the mean of the values of y in the ith segment.
The remainder of SPARAM is used as workspace.
12:   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, CTYPE=value.
Constraint: CTYPE=1, 2, 3, 4, 5 or 6.
IFAIL=21
On entry, N=value.
Constraint: N2.
IFAIL=31
On entry, CTYPE=value and Yvalue=value.
Constraint: if CTYPE=4, 5 or 6 then Yi0.0, for i=1,2,,N.
IFAIL=32
On entry, Yvalue=value, is too large.
IFAIL=51
On entry, MINSS=value.
Constraint: MINSS2.
IFAIL=61
On entry, IPARAM=value.
Constraint: if CTYPE4 then IPARAM=0 or 1.
IFAIL=62
On entry, IPARAM=value.
Constraint: if CTYPE=4 then IPARAM=1.
IFAIL=71
On entry, CTYPE=value and PARAM1=value.
Constraint: if CTYPE=1 or 4 and IPARAM=1, then PARAM1>0.0.
IFAIL=200
To avoid overflow some truncation occurred when calculating the cost function, C. All output is returned as normal.
IFAIL=201
To avoid overflow some truncation occurred when calculating the parameter estimates returned in SPARAM. All output is returned as normal.
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

The calculation of means and sums of squares about the mean during the evaluation of the cost functions are based on the one pass algorithm of West (1979) and are believed to be stable.

8  Parallelism and Performance

G13NDF 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

None.

10  Example

This example identifies changes in the mean, under the assumption that the data is normally distributed, for a simulated dataset with 100 observations. A BIC penalty is used, that is β=logn4.6, the minimum segment size is set to 2 and the variance is fixed at 1 across the whole input series.

10.1  Program Text

Program Text (g13ndfe.f90)

10.2  Program Data

Program Data (g13ndfe.d)

10.3  Program Results

Program Results (g13ndfe.r)

This example plot shows the original data series, the estimated change points and the estimated mean in each of the identified segments.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −3 −2 −1 0 1 2 3 4 0 10 20 30 40 50 60 70 80 90 100 Value Time Example Program Simulated time series and the corresponding changes in mean gnuplot_plot_1

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

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