NAG FL Interface
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 <nag.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) |
|
C++ Header Interface
#include <nag.h> extern "C" {
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) |
}
|
The routine may be called by the names g13naf or nagf_tsa_cp_pelt.
3
Description
Let denote a series of data and denote a set of ordered (strictly monotonic increasing) indices known as change points with and . For ease of notation we also define . The change points, , split the data into segments, with the th segment being of length and containing .
Given a cost function,
g13naf solves
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
such that
for all
.
g13naf supplies four families of cost function. Each cost function assumes that the series,
, comes from some distribution,
. 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,
and are given by:
where
is the maximum likelihood estimate of
within the
th segment. In all four cases setting
satisfies equation
(2). Four distributions are available: Normal, Gamma, Exponential and Poisson. Letting
the log-likelihoods and cost functions for the four distributions, and the available subdivisions of the parameter space are:
- Normal distribution:
- Mean changes:
- Variance changes:
- Both mean and variance change:
- Gamma distribution:
- Exponential Distribution:
- Poisson distribution:
- Mean changes:
when calculating for the Poisson distribution, the sum is calculated for rather than .
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:
– Integer
Input
-
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 .
- Data from an exponential distribution, looking for changes in .
- Data from a Poisson distribution, looking for changes in .
Constraint:
, , , , or .
-
2:
– Integer
Input
-
On entry: , the length of the time series.
Constraint:
.
-
3:
– Real (Kind=nag_wp) array
Input
-
On entry: , the time series.
if , that is the data is assumed to come from a Poisson distribution, is used in all calculations.
Constraints:
- if , or , , for ;
- if , each value of y must be representable as an integer;
- if , each value of y must be small enough such that, for , can be calculated without incurring overflow.
-
4:
– Real (Kind=nag_wp)
Input
-
On entry:
, the penalty term.
There are a number of standard ways of setting
, including:
- SIC or BIC
- AIC
- Hannan-Quinn
where
is the number of parameters being treated as estimated in each segment. This is usually set to
when
and
otherwise.
If no penalty is required then set . Generally, the smaller the value of the larger the number of suggested change points.
-
5:
– Integer
Input
-
On entry: the minimum distance between two change points, that is .
Constraint:
.
-
6:
– Integer
Input
-
On entry: if
distributional parameters have been supplied in
param.
Constraints:
- if , ;
- otherwise or .
-
7:
– Real (Kind=nag_wp) array
Input
-
On entry:
, values for the parameters that will be treated as fixed. If
then
param is not referenced.
If supplied, then when
- , the standard deviation of the normal distribution. If not supplied then is estimated from the full input data,
- , the mean of the normal distribution. If not supplied then is estimated from the full input data,
- must hold the shape, , for the gamma distribution,
- otherwise
- param is not referenced.
Constraint:
if or , .
-
8:
– Integer
Output
-
On exit: , the number of change points detected.
-
9:
– Integer array
Output
-
On exit: the first
elements of
tau hold the location of the change points. The
th segment is defined by
to
, where
and
.
The remainder of
tau is used as workspace.
-
10:
– Real (Kind=nag_wp) array
Output
-
On exit: the estimated values of the distribution parameters in each segment
- , or
-
and
for , where and is the mean and standard deviation, respectively, of the values of in the th segment.
It should be noted that when and when , for all and .
-
and
for , where and are the shape and scale parameters, respectively, for the values of in the th segment. It should be noted that for all .
- or
-
for , where is the mean of the values of in the th segment.
The remainder of
sparam is used as workspace.
-
11:
– Integer
Input/Output
-
On entry:
ifail must be set to
,
or
to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value
or
is recommended. If message printing is undesirable, then the value
is recommended. Otherwise, the value
is recommended.
When the value or is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: , , , , or .
-
On entry, .
Constraint: .
-
On entry, and .
Constraint: if , or then , for .
-
On entry, , is too large.
-
On entry, .
Constraint: .
-
On entry, .
Constraint: if then or .
-
On entry, .
Constraint: if then .
-
On entry, and .
Constraint: if or and , then .
-
To avoid overflow some truncation occurred when calculating the cost function, . 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.
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.
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.
Dynamic memory allocation failed.
See
Section 9 in the Introduction to the NAG Library FL Interface for further information.
7
Accuracy
For efficiency reasons, when calculating the cost functions,
and the parameter estimates returned in
sparam, this routine makes use of the mathematical identities:
and
where
.
The input data,
, 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
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.
None.
10
Example
This example identifies changes in the mean, under the assumption that the data is normally distributed, for a simulated dataset with observations. A BIC penalty is used, that is , the minimum segment size is set to and the variance is fixed at across the whole input series.
10.1
Program Text
10.2
Program Data
10.3
Program Results
This example plot shows the original data series, the estimated change points and the estimated mean in each of the identified segments.