NAG CL Interface
g13ndc (cp_​binary)

Settings help

CL Name Style:


1 Purpose

g13ndc 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

#include <nag.h>
void  g13ndc (Nag_TS_ChangeType ctype, Integer n, const double y[], double beta, Integer minss, const double param[], Integer mdepth, Integer *ntau, Integer tau[], double sparam[], NagError *fail)
The function may be called by the names: g13ndc or nag_tsa_cp_binary.

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, C(yτi-1+1:τi), g13ndc gives an approximate solution to
minimize m,τ i=1 m (C(yτ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
    C(yu:v) + C(yv+1:w)  
  4. 4.Test
    C(yu:v) + C(yv+1:w) + β < C(yu: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 τ.
g13ndc 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) Parametric Statistical 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 Arguments

1: ctype Nag_TS_ChangeType Input
On entry: a flag indicating the assumed distribution of the data and the type of change point being looked for.
ctype=Nag_NormalMean
Data from a Normal distribution, looking for changes in the mean, μ.
ctype=Nag_NormalStd
Data from a Normal distribution, looking for changes in the standard deviation σ.
ctype=Nag_NormalMeanStd
Data from a Normal distribution, looking for changes in the mean, μ and standard deviation σ.
ctype=Nag_GammaScale
Data from a Gamma distribution, looking for changes in the scale parameter b.
ctype=Nag_ExponentialLambda
Data from an exponential distribution, looking for changes in λ.
ctype=Nag_PoissonLambda
Data from a Poisson distribution, looking for changes in λ.
Constraint: ctype=Nag_NormalMean, Nag_NormalStd, Nag_NormalMeanStd, Nag_GammaScale, Nag_ExponentialLambda or Nag_PoissonLambda.
2: n Integer Input
On entry: n, the length of the time series.
Constraint: n2.
3: y[n] const double Input
On entry: y, the time series.
If ctype=Nag_PoissonLambda, that is the data is assumed to come from a Poisson distribution, y+0.5 is used in all calculations.
Constraints:
  • if ctype=Nag_GammaScale, Nag_ExponentialLambda or Nag_PoissonLambda, y[i-1]0, for i=1,2,,n;
  • if ctype=Nag_PoissonLambda, each value of y must be representable as an integer;
  • if ctypeNag_PoissonLambda, each value of y must be small enough such thaty[i-1]2, for i=1,2,,n, can be calculated without incurring overflow.
4: beta double Input
On entry: β, the penalty term.
There are a number of standard ways of setting β, including:
SIC or BIC
β=p×log(n)
AIC
β=2p
Hannan-Quinn
β=2p×log(log(n))
where p is the number of parameters being treated as estimated in each segment. This is usually set to 2 when ctype=Nag_NormalMeanStd 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 Integer Input
On entry: the minimum distance between two change points, that is τi-τi-1minss.
Constraint: minss2.
6: param[1] const double Input
On entry: ϕ, values for the parameters that will be treated as fixed. If ctype=Nag_GammaScale then param must be supplied, otherwise param may be NULL.
If supplied, then when
ctype=Nag_NormalMean
param[0]=σ, the standard deviation of the normal distribution. If not supplied then σ is estimated from the full input data,
ctype=Nag_NormalStd
param[0]=μ, the mean of the normal distribution. If not supplied then μ is estimated from the full input data,
ctype=Nag_GammaScale
param[0] must hold the shape, a, for the gamma distribution,
otherwise
param is not referenced.
Constraint: if ctype=Nag_NormalMean or Nag_GammaScale, param[0]>0.0.
7: 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.
8: ntau Integer * Output
On exit: m, the number of change points detected.
9: tau[dim] Integer Output
Note: the dimension, dim, of the array tau must be at least
  • min(nminss,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.
10: sparam[2×n] double Output
On exit: the estimated values of the distribution parameters in each segment
ctype=Nag_NormalMean, Nag_NormalStd or Nag_NormalMeanStd
sparam[2i-2]=μi and sparam[2i-1]=σ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=Nag_NormalMean and μi=μj when ctype=Nag_NormalStd, for all i and j.
ctype=Nag_GammaScale
sparam[2i-2]=ai and sparam[2i-1]=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=param[0] for all i.
ctype=Nag_ExponentialLambda or Nag_PoissonLambda
sparam[i-1]=λ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.
11: 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_REAL
On entry, ctype=value and param[0]=value.
Constraint: if ctype=Nag_NormalMean or Nag_GammaScale and param has been supplied, then param[0]>0.0.
NE_REAL_ARRAY
On entry, ctype=value and y[value]=value.
Constraint: if ctype=Nag_GammaScale, Nag_ExponentialLambda or Nag_PoissonLambda then y[i-1]0.0, for i=1,2,,n.
On entry, y[value]=value, is too large.
NW_TRUNCATED
To avoid overflow some truncation occurred when calculating the cost function, C. All output is returned as normal.
To avoid overflow some truncation occurred when calculating the parameter estimates returned in sparam. All output is returned as normal.

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

Background information to multithreading can be found in the Multithreading documentation.
g13ndc 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

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 (g13ndce.c)

10.2 Program Data

Program Data (g13ndce.d)

10.3 Program Results

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