NAG FL Interface
g13nef (cp_binary_user)
1
Purpose
g13nef 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 binary segmentation for a user-supplied cost function.
2
Specification
Fortran Interface
Subroutine g13nef ( |
n, beta, minss, mdepth, chgpfn, ntau, tau, y, iuser, ruser, ifail) |
Integer, Intent (In) |
:: |
n, minss, mdepth |
Integer, Intent (Inout) |
:: |
iuser(*), ifail |
Integer, Intent (Out) |
:: |
ntau, tau(*) |
Real (Kind=nag_wp), Intent (In) |
:: |
beta |
Real (Kind=nag_wp), Intent (Inout) |
:: |
y(*), ruser(*) |
External |
:: |
chgpfn |
|
C Header Interface
#include <nag.h>
void |
g13nef_ (const Integer *n, const double *beta, const Integer *minss, const Integer *mdepth, void (NAG_CALL *chgpfn)(const Integer *side, const Integer *u, const Integer *w, const Integer *minss, Integer *v, double cost[], 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 |
g13nef_ (const Integer &n, const double &beta, const Integer &minss, const Integer &mdepth, void (NAG_CALL *chgpfn)(const Integer &side, const Integer &u, const Integer &w, const Integer &minss, Integer &v, double cost[], 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 g13nef or nagf_tsa_cp_binary_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 cost function,
,
g13nef gives an approximate solution to
where
is a penalty term used to control the number of change points. The solution is obtained in an iterative manner as follows:
-
1.Set , and
-
2.Set . If , where is a user-supplied control parameter, then terminate the process for this segment.
-
3.Find that minimizes
-
4.Test
-
5.If inequality (1) is false then the process is terminated for this segment.
-
6.If inequality (1) is true, then is added to the set of change points, and the segment is split into two subsegments, and . The whole process is repeated from step 2 independently on each subsegment, with the relevant changes to the definition of and (i.e., is set to when processing the left-hand subsegment and is set to when processing the right-hand subsegment.
The change points are ordered to give .
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
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:
– Integer
Input
-
On entry:
, the maximum depth for the iterative process, which in turn puts an upper limit on the number of change points with
.
If
then no limit is put on the depth of the iterative process and no upper limit is put on the number of change points, other than that inherent in the length of the series and the value of
minss.
-
5:
– Subroutine, supplied by the user.
External Procedure
-
chgpfn must calculate a proposed change point, and the associated costs, within a specified segment.
The specification of
chgpfn is:
Fortran Interface
Integer, Intent (In) |
:: |
side, u, w, minss |
Integer, Intent (Inout) |
:: |
iuser(*), info |
Integer, Intent (Out) |
:: |
v |
Real (Kind=nag_wp), Intent (Inout) |
:: |
y(*), ruser(*) |
Real (Kind=nag_wp), Intent (Out) |
:: |
cost(3) |
|
C Header Interface
void |
chgpfn_ (const Integer *side, const Integer *u, const Integer *w, const Integer *minss, Integer *v, double cost[], double y[], Integer iuser[], double ruser[], Integer *info) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
chgpfn_ (const Integer &side, const Integer &u, const Integer &w, const Integer &minss, Integer &v, double cost[], double y[], Integer iuser[], double ruser[], Integer &info) |
}
|
-
1:
– Integer
Input
-
On entry: flag indicating what
chgpfn must calculate and at which point of the Binary Segmentation it has been called.
- only need be calculated and returned in , neither v nor the other elements of cost need be set. In this case, and .
- all elements of cost and v must be set. In this case, and .
- the segment, , is a left-hand side subsegment from a previous iteration of the Binary Segmentation algorithm. All elements of cost and v must be set.
- the segment, , is a right-hand side subsegment from a previous iteration of the Binary Segmentation algorithm. All elements of cost and v must be set.
The distinction between
and
may allow for
chgpfn to be implemented in a more efficient manner. See
Section 10 for one such example.
The first call to
chgpfn will always have
and the second call will always have
. All subsequent calls will be made with
or
.
-
2:
– Integer
Input
-
On entry: , the start of the segment of interest.
-
3:
– Integer
Input
-
On entry: , the end of the segment of interest.
-
4:
– Integer
Input
-
On entry: the minimum distance between two change points, as passed to g13nef.
-
5:
– Integer
Output
-
On exit: if
then
v need not be set.
if
then
, the proposed change point. That is, the value which minimizes
for
to
.
-
6:
– Real (Kind=nag_wp) array
Output
-
On exit: costs associated with the proposed change point,
.
If
then
and the remaining two elements of
cost need not be set.
If
then
- .
- .
- .
-
7:
– Real (Kind=nag_wp) array
User Data
-
chgpfn is called with
y as supplied to
g13nef. You are free to use the array
y to supply information to
chgpfn.
y is supplied in addition to
iuser and
ruser for ease of coding as in most cases
chgpfn will require (functions of) the time series,
.
-
8:
– Integer array
User Workspace
-
9:
– Real (Kind=nag_wp) array
User Workspace
-
chgpfn is called with the arguments
iuser and
ruser as supplied to
g13nef. You should use the arrays
iuser and
ruser to supply information to
chgpfn.
-
10:
– Integer
Input/Output
-
On entry: .
On exit: in most circumstances
info should remain unchanged.
If
info is set to a strictly positive value then
g13nef terminates with
.
If
info is set to a strictly negative value the current segment is skipped (i.e., no change points are considered in this segment) and
g13nef continues as normal. If
info was set to a strictly negative value at any point and no other errors occur then
g13nef will terminate with
.
chgpfn must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
g13nef is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: chgpfn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
g13nef. If your code inadvertently
does return any NaNs or infinities,
g13nef is likely to produce unexpected results.
-
6:
– Integer
Output
-
On exit: , the number of change points detected.
-
7:
– Integer array
Output
Note: the dimension of the array
tau
must be at least
if
, and at least
otherwise.
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
g13nef, but is passed directly to
chgpfn 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
g13nef, but are passed directly to
chgpfn and may be used to pass information to this routine.
-
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: .
-
On entry, .
Constraint: .
-
User requested termination by setting .
-
User requested a segment to be skipped by setting .
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
g13nef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
g13ndf 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
g13ndf 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.