nag_tsa_cp_pelt (g13nac) (PDF version)
g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_tsa_cp_pelt (g13nac)


    1  Purpose
    7  Accuracy

1  Purpose

nag_tsa_cp_pelt (g13nac) 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

#include <nag.h>
#include <nagg13.h>
void  nag_tsa_cp_pelt (Nag_TS_ChangeType ctype, Integer n, const double y[], double beta, Integer minss, const double param[], Integer *ntau, Integer tau[], double sparam[], NagError *fail)

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 nag_tsa_cp_pelt (g13nac) 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.
nag_tsa_cp_pelt (g13nac) 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) Parameteric Statisical 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 Nag_TS_ChangeTypeInput
On entry: a flag indicating the assumed distribution of the data and the type of change point being looked for.
Data from a Normal distribution, looking for changes in the mean, μ.
Data from a Normal distribution, looking for changes in the standard deviation σ.
Data from a Normal distribution, looking for changes in the mean, μ and standard deviation σ.
Data from a Gamma distribution, looking for changes in the scale parameter b.
Data from an exponential distribution, looking for changes in λ.
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 IntegerInput
On entry: n, the length of the time series.
Constraint: n2.
3:     y[n] const doubleInput
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.
  • 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 that y[i-1]2, for i=1,2,,n, can be calculated without incurring overflow.
4:     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. 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 IntegerInput
On entry: the minimum distance between two change points, that is τi-τi-1minss.
Constraint: minss2.
6:     param[1] const doubleInput
On entry: ϕ, values for the parameters that will be treated as fixed. If ctypeNag_GammaScale, param may be set to NULL.
If ctype=Nag_NormalMean 
  • if param is NULL, σ, the standard deviation of the Normal distribution, is estimated from the full input data. Otherwise σ=param[0].
If ctype=Nag_NormalStd 
  • If param is NULL, μ, the mean of the Normal distribution, is estimated from the full input data. Otherwise μ=param[0].
If 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:     ntau Integer *Output
On exit: m, the number of change points detected.
8:     tau[n] 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.
9:     sparam[2×n+2] doubleOutput
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.
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.
10:   fail NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in the Essential Introduction 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.
An unexpected error has been triggered by this function. Please contact NAG.
See Section 3.6.6 in the Essential Introduction for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.6.5 in the Essential Introduction for further information.
On entry, ctype=value and param[0]=value.
Constraint: if ctype=Nag_NormalMean or Nag_GammaScale and paramis notNULL, then param[0]>0.0.
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.
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

For efficiency reasons, when calculating the cost functions, C and the parameter estimates returned in sparam, this function makes use of the mathematical identities:
j=u v yj 2 = j=1 v yj 2 - j=1 u-1 yj 2  
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 nag_tsa_cp_pelt (g13nac) should be sufficient for the majority of datasets. If a more stable method of calculating C is deemed necessary, nag_tsa_cp_pelt_user (g13nbc) can be used and the method chosen implemented in the user-supplied cost function.

8  Parallelism and Performance

Not applicable.

9  Further Comments


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

10.2  Program Data

Program Data (g13nace.d)

10.3  Program Results

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

nag_tsa_cp_pelt (g13nac) (PDF version)
g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual

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