NAG C Library Function Document

nag_tsa_cp_pelt_user (g13nbc)


nag_tsa_cp_pelt_user (g13nbc) 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.


#include <nag.h>
#include <nagg13.h>
void  nag_tsa_cp_pelt_user (Integer n, double beta, Integer minss, double k,
void (*costfn)(Integer ts, Integer nr, const Integer r[], double c[], Nag_Comm *comm, Integer *info),
Integer *ntau, Integer tau[], Nag_Comm *comm, NagError *fail)


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 nag_tsa_cp_pelt_user (g13nbc) 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 


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


1:     n IntegerInput
On entry: n, the length of the time series.
Constraint: n2.
2:     beta doubleInput
On entry: β, the penalty term.
There are a number of standard ways of setting β, including:
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:     k doubleInput
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 function, supplied by the userExternal Function
The cost function, C. costfn must calculate a vector of costs for a number of segments.
The specification of costfn is:
void  costfn (Integer ts, Integer nr, const Integer r[], double c[], Nag_Comm *comm, Integer *info)
1:     ts IntegerInput
On entry: a reference time point.
2:     nr IntegerInput
On entry: number of segments being considered.
3:     r[dim] const IntegerInput
On entry: time points which, along with ts, define the segments being considered, 0r[i-1]n for i=1,2,nr.
4:     c[dim] doubleOutput
On exit: the cost function, C, with
c[i-1]= Cyri:t ​ if ​t>ri, Cyt:ri ​ otherwise.  
where t=ts and ri=r[i-1].
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:     comm Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to costfn.
userdouble *
iuserInteger *
The type Pointer will be void *. Before calling nag_tsa_cp_pelt_user (g13nbc) you may allocate memory and initialize these pointers with various quantities for use by costfn when called from nag_tsa_cp_pelt_user (g13nbc) (see Section in How to Use the NAG Library and its Documentation).
6:     info Integer *Input/Output
On entry: info=0.
On exit: set info to a nonzero value if you wish nag_tsa_cp_pelt_user (g13nbc) to terminate with fail.code= NE_USER_STOP.
Note: costfn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by nag_tsa_cp_pelt_user (g13nbc). If your code inadvertently does return any NaNs or infinities, nag_tsa_cp_pelt_user (g13nbc) is likely to produce unexpected results.
6:     ntau Integer *Output
On exit: m, the number of change points detected.
7:     tau[dim] IntegerOutput
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.
8:     comm Nag_Comm *
The NAG communication argument (see Section in How to Use the NAG Library and its Documentation).
9:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, minss=value.
Constraint: minss2.
On entry, n=value.
Constraint: n2.
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 2.7.6 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
User requested termination.


Not applicable.

Parallelism and Performance

nag_tsa_cp_pelt_user (g13nbc) is not threaded in any implementation.

Further Comments

nag_tsa_cp_pelt (g13nac) 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 nag_tsa_cp_pelt (g13nac) can be used without the need to provide a cost function routine.


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.

Program Text

Program Text (g13nbce.c)

Program Data

Program Data (g13nbce.d)

Program Results

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