NAG Library Function Document
nag_tsa_cp_pelt_user (g13nbc)
1
Purpose
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.
2
Specification
#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) |
|
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 user-supplied cost function,
nag_tsa_cp_pelt_user (g13nbc) 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
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:
– IntegerInput
-
On entry: , the length of the time series.
Constraint:
.
- 2:
– doubleInput
-
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. The value of
will depend on the cost function being used.
If no penalty is required then set . Generally, the smaller the value of the larger the number of suggested change points.
- 3:
– IntegerInput
-
On entry: the minimum distance between two change points, that is .
Constraint:
.
- 4:
– doubleInput
-
On entry:
, the constant value that satisfies equation
(2). If
exists, it is unlikely to be unique in such cases, it is recommened that the largest value of
, that satisfies equation
(2), is chosen. No check is made that
is the correct value for the supplied cost function.
- 5:
– function, supplied by the userExternal Function
-
The cost function,
.
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:
– IntegerInput
-
On entry: a reference time point.
- 2:
– IntegerInput
-
On entry: number of segments being considered.
- 3:
– const IntegerInput
-
On entry: time points which, along with
ts, define the segments being considered,
for
.
- 4:
– doubleOutput
-
On exit: the cost function,
, with
where
and
.
It should be noted that if
for any value of
then it will be true for all values of
. Therefore the inequality need only be tested once per call to
costfn.
- 5:
– Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to
costfn.
- user – double *
- iuser – Integer *
- p – Pointer
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 3.3.1.1 in How to Use the NAG Library and its Documentation).
- 6:
– Integer *Input/Output
-
On entry: .
On exit: set
info to a nonzero value if you wish
nag_tsa_cp_pelt_user (g13nbc) to terminate with
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:
– Integer *Output
-
On exit: , the number of change points detected.
- 7:
– IntegerOutput
-
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.
- 8:
– Nag_Comm *
-
The NAG communication argument (see
Section 3.3.1.1 in How to Use the NAG Library and its Documentation).
- 9:
– NagError *Input/Output
-
The NAG error argument (see
Section 3.7 in How to Use the NAG Library and its Documentation).
6
Error Indicators and Warnings
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
See
Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
- NE_BAD_PARAM
-
On entry, argument had an illegal value.
- NE_INT
-
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
-
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.
- NE_NO_LICENCE
-
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.
- NE_USER_STOP
-
User requested termination.
7
Accuracy
Not applicable.
8
Parallelism and Performance
nag_tsa_cp_pelt_user (g13nbc) is not threaded in any implementation.
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.
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 observations. A penalty, of is used and the minimum segment size is set to . The shape parameter is fixed at across the whole input series.
The cost function used is
where
is a shape parameter that is fixed for all segments and
.
10.1
Program Text
Program Text (g13nbce.c)
10.2
Program Data
Program Data (g13nbce.d)
10.3
Program Results
Program Results (g13nbce.r)
This example plot shows the original data series and the estimated change points.