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) |
|
C++ Header Interface
#include <nag.h> extern "C" {
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 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,
g13nbf 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:
– Integer
Input
-
On entry: , the length of the time series.
Constraint:
.
-
2:
– 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. 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:
– Integer
Input
-
On entry: the minimum distance between two change points, that is .
Constraint:
.
-
4:
– Real (Kind=nag_wp)
Input
-
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:
– Subroutine, supplied by the user.
External Procedure
-
The cost function,
.
costfn must calculate a vector of costs for a number of segments.
The specification of
costfn is:
Fortran Interface
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) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
costfn_ (const Integer &ts, const Integer &nr, const Integer r[], double c[], double y[], Integer iuser[], double ruser[], Integer &info) |
}
|
-
1:
– Integer
Input
-
On entry: a reference time point.
-
2:
– Integer
Input
-
On entry: number of segments being considered.
-
3:
– Integer array
Input
-
On entry: time points which, along with
ts, define the segments being considered,
for
.
-
4:
– Real (Kind=nag_wp) array
Output
-
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:
– 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,
.
-
6:
– Integer array
User Workspace
-
7:
– 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:
– Integer
Input/Output
-
On entry: .
On exit: set
info to a nonzero value if you wish
g13nbf to terminate with
.
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:
– Integer
Output
-
On exit: , the number of change points detected.
-
7:
– 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.
-
8:
– 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,
of interest.
-
9:
– Integer array
User Workspace
-
10:
– 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:
– Integer
Input/Output
-
On entry:
ifail must be set to
,
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
.
When the value 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: .
-
On entry, .
Constraint: .
-
User requested termination.
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
Not applicable.
8
Parallelism and Performance
g13nbf is not threaded in any implementation.
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 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
10.2
Program Data
10.3
Program Results
This example plot shows the original data series and the estimated change points.