NAG FL Interface
g13nbf (cp_​pelt_​user)

1 Purpose

g13nbf 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 and a user-supplied cost function.

2 Specification

Fortran Interface
Subroutine g13nbf ( n, beta, minss, k, costfn, ntau, tau, y, iuser, ruser, ifail)
Integer, Intent (In) :: n, minss
Integer, Intent (Inout) :: iuser(*), ifail
Integer, Intent (Out) :: ntau, tau(n)
Real (Kind=nag_wp), Intent (In) :: beta, k
Real (Kind=nag_wp), Intent (Inout) :: y(*), ruser(*)
External :: costfn
C Header Interface
#include <nag.h>
void  g13nbf_ (const Integer *n, const double *beta, const Integer *minss, const double *k,
void (NAG_CALL *costfn)(const Integer *ts, const Integer *nr, const Integer r[], double c[], double y[], Integer iuser[], double ruser[], Integer *info),
Integer *ntau, Integer tau[], double y[], Integer iuser[], double ruser[], Integer *ifail)
The routine may be called by the names g13nbf or nagf_tsa_cp_pelt_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 user-supplied cost function, Cyτi-1+1:τi g13nbf 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

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: n Integer Input
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 Integer Input
On entry: the minimum distance between two change points, that is τi-τi-1minss.
Constraint: minss2.
4: k Real (Kind=nag_wp) Input
On entry: K, the constant value that satisfies equation (2). If K exists, it is unlikely to be unique in such cases, it is recommened that the largest value of K, that satisfies equation (2), is chosen. No check is made that K is the correct value for the supplied cost function.
5: costfn Subroutine, supplied by the user. External Procedure
The cost function, C. costfn must calculate a vector of costs for a number of segments.
The specification of costfn is:
Fortran Interface
Subroutine costfn ( ts, nr, r, c, y, iuser, ruser, info)
Integer, Intent (In) :: ts, nr, r(nr)
Integer, Intent (Inout) :: iuser(*), info
Real (Kind=nag_wp), Intent (Inout) :: y(*), ruser(*)
Real (Kind=nag_wp), Intent (Out) :: c(nr)
C Header Interface
void  costfn_ (const Integer *ts, const Integer *nr, const Integer r[], double c[], double y[], Integer iuser[], double ruser[], Integer *info)
1: ts Integer Input
On entry: a reference time point.
2: nr Integer Input
On entry: number of segments being considered.
3: rnr Integer array Input
On entry: time points which, along with ts, define the segments being considered, 0rin for i=1,2,nr.
4: cnr Real (Kind=nag_wp) array Output
On exit: the cost function, C, with
ci= Cyri:t ​ if ​t>ri, Cyt:ri ​ otherwise.  
where t=ts and ri=ri.
It should be noted that if t>ri for any value of i then it will be true for all values of i. Therefore the inequality need only be tested once per call to costfn.
5: y* Real (Kind=nag_wp) array User Data
costfn is called with y as supplied to g13nbf. You are free to use the array y to supply information to costfn.
y is supplied in addition to iuser and ruser for ease of coding as in most cases costfn will require (functions of) the time series, y.
6: iuser* Integer array User Workspace
7: ruser* Real (Kind=nag_wp) array User Workspace
costfn is called with the arguments iuser and ruser as supplied to g13nbf. You should use the arrays iuser and ruser to supply information to costfn.
8: info Integer Input/Output
On entry: info=0.
On exit: set info to a nonzero value if you wish g13nbf to terminate with ifail=51.
costfn must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which g13nbf is called. Arguments denoted as Input must not be changed by this procedure.
Note: costfn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g13nbf. If your code inadvertently does return any NaNs or infinities, g13nbf is likely to produce unexpected results.
6: ntau Integer Output
On exit: m, the number of change points detected.
7: taun Integer array Output
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) array User Data
y is not used by g13nbf, but is passed directly to costfn 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 array User Workspace
10: ruser* Real (Kind=nag_wp) array User Workspace
iuser and ruser are not used by g13nbf, but are passed directly to costfn and may be used to pass information to this routine.
11: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface 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, n=value.
Constraint: n2.
ifail=31
On entry, minss=value.
Constraint: minss2.
ifail=51
User requested termination.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

Not applicable.

8 Parallelism and Performance

g13nbf is not threaded in any implementation.

9 Further Comments

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

10.2 Program Data

Program Data (g13nbfe.d)

10.3 Program Results

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