NAG Library Routine Document

g13naf (cp_pelt)

1
Purpose

g13naf 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 the PELT (Pruned Exact Linear Time) algorithm using one of a provided set of cost functions.

2
Specification

Fortran Interface
Subroutine g13naf ( ctype, n, y, beta, minss, iparam, param, ntau, tau, sparam, ifail)
Integer, Intent (In):: ctype, n, minss, iparam
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: ntau, tau(n)
Real (Kind=nag_wp), Intent (In):: y(n), beta, param(1)
Real (Kind=nag_wp), Intent (Out):: sparam(2*n+2)
C Header Interface
#include <nagmk26.h>
void  g13naf_ (const Integer *ctype, const Integer *n, const double y[], const double *beta, const Integer *minss, const Integer *iparam, const double param[], Integer *ntau, Integer tau[], double sparam[], Integer *ifail)

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 g13naf solves
minimize m,τ i=1 m Cyτi-1+1:τi + β (1)
where β is a penalty term used to control the number of change points. This minimization is performed using the PELT algorithm of Killick et al. (2012). The PELT algorithm is guaranteed to return the optimal solution to (1) if there exists a constant K such that
C y u+1 : v + C y v+1 : w + K C y u+1 : w (2)
for all u<v<w.
g13naf 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 θ^i is the maximum likelihood estimate of θ within the ith segment. In all four cases setting K=0 satisfies equation (2). Four distributions are available: Normal, Gamma, Exponential and Poisson. 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
Killick R, Fearnhead P and Eckely I A (2012) Optimal detection of changepoints with a linear computational cost Journal of the American Statistical Association 107:500 1590–1598

5
Arguments

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 thatyi2, 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 supplied, then when
ctype=1
param1=σ, the standard deviation of the normal distribution. If not supplied then σ is estimated from the full input data,
ctype=2
param1=μ, the mean of the normal distribution. If not supplied then μ is estimated from the full input data,
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:     ntau – IntegerOutput
On exit: m, the number of change points detected.
9:     taun – Integer arrayOutput
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.
10:   sparam2×n+2 – 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.
11:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

For efficiency reasons, when calculating the cost functions, C and the parameter estimates returned in sparam, this routine makes use of the mathematical identities:
j=u v yj 2 = j=1 v yj 2 - j=1 u-1 yj 2  
and
j=1 n yj-y- 2 = j=1 n yj2 - n y- 2  
where y-=n-1j=1nyj.
The input data, y, is scaled in order to try and mitigate some of the known instabilities associated with using these formulations. The results returned by g13naf should be sufficient for the majority of datasets. If a more stable method of calculating C is deemed necessary, g13nbf can be used and the method chosen implemented in the user-supplied cost function.

8
Parallelism and Performance

g13naf is not threaded in any implementation.

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

10.2
Program Data

Program Data (g13nafe.d)

10.3
Program Results

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