g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_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 using one of a provided set of cost function.

## 2  Specification

 #include #include
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 ${y}_{1:n}=\left\{{y}_{j}:j=1,2,\dots ,n\right\}$ denote a series of data and $\tau =\left\{{\tau }_{i}:i=1,2,\dots ,m\right\}$ denote a set of $m$ ordered (strictly monotonic increasing) indices known as change points with $1\le {\tau }_{i}\le n$ and ${\tau }_{m}=n$. For ease of notation we also define ${\tau }_{0}=0$. The $m$ change points, $\tau$, split the data into $m$ segments, with the $i$th segment being of length ${n}_{i}$ and containing ${y}_{{\tau }_{i-1}+1:{\tau }_{i}}$.
Given a user-supplied cost function, $C\left({y}_{{\tau }_{i-1}+1:{\tau }_{i}}\right)$ nag_tsa_cp_pelt_user (g13nbc) solves
 $minimize m,τ ∑ i=1 m Cyτi-1+1:τi + β$ (1)
where $\beta$ 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
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:    $\mathbf{n}$IntegerInput
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{n}}\ge 2$.
2:    $\mathbf{beta}$doubleInput
On entry: $\beta$, the penalty term.
There are a number of standard ways of setting $\beta$, including:
SIC or BIC
$\beta =p×\mathrm{log}\left(n\right)$
AIC
$\beta =2p$
Hannan-Quinn
$\beta =2p×\mathrm{log}\left(\mathrm{log}\left(n\right)\right)$
where $p$ is the number of parameters being treated as estimated in each segment. The value of $p$ will depend on the cost function being used.
If no penalty is required then set $\beta =0$. Generally, the smaller the value of $\beta$ the larger the number of suggested change points.
3:    $\mathbf{minss}$IntegerInput
On entry: the minimum distance between two change points, that is ${\tau }_{i}-{\tau }_{i-1}\ge {\mathbf{minss}}$.
Constraint: ${\mathbf{minss}}\ge 2$.
4:    $\mathbf{k}$doubleInput
On entry: $K$, the constant value that satisfies equation (2). If $K$ exists, it is unlikely to be unique in such cases, it is recommened that the largest value of $K$, that satisfies equation (2), is chosen. No check is made that $K$ is the correct value for the supplied cost function.
5:    $\mathbf{costfn}$function, supplied by the userExternal Function
The cost function, $C$. 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:    $\mathbf{ts}$IntegerInput
On entry: a reference time point.
2:    $\mathbf{nr}$IntegerInput
On entry: number of segments being considered.
3:    $\mathbf{r}\left[{\mathbf{nr}}\right]$const IntegerInput
On entry: time points which, along with ts, define the segments being considered, $0\le {\mathbf{r}}\left[i-1\right]\le n$ for $i=1,2,\dots {\mathbf{nr}}$.
4:    $\mathbf{c}\left[{\mathbf{nr}}\right]$doubleOutput
On exit: the cost function, $C$, with
 $c[i-1]= Cyri:t ​ if ​t>ri, Cyt:ri ​ otherwise.$
where $t={\mathbf{ts}}$ and ${r}_{i}={\mathbf{r}}\left[i-1\right]$.
It should be noted that if $t>{r}_{i}$ for any value of $i$ then it will be true for all values of $i$. Therefore the inequality need only be tested once per call to costfn.
5:    $\mathbf{comm}$Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to costfn.
userdouble *
iuserInteger *
pPointer
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.2.1.1 in the Essential Introduction).
6:    $\mathbf{info}$Integer *Input/Output
On entry: ${\mathbf{info}}=0$.
On exit: set info to a nonzero value if you wish nag_tsa_cp_pelt_user (g13nbc) to terminate with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_USER_STOP.
6:    $\mathbf{ntau}$Integer *Output
On exit: $m$, the number of change points detected.
7:    $\mathbf{tau}\left[{\mathbf{n}}\right]$IntegerOutput
On exit: the first $m$ elements of tau hold the location of the change points. The $i$th segment is defined by ${y}_{\left({\tau }_{i-1}+1\right)}$ to ${y}_{{\tau }_{i}}$, where ${\tau }_{0}=0$ and ${\tau }_{i}={\mathbf{tau}}\left[i-1\right],1\le i\le m$.
The remainder of tau is used as workspace.
8:    $\mathbf{comm}$Nag_Comm *
The NAG communication argument (see Section 3.2.1.1 in the Essential Introduction).
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.2.1.2 in the Essential Introduction for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{minss}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{minss}}\ge 2$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
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 3.6.6 in the Essential Introduction for further information.
NE_NO_LICENCE
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.
NE_USER_STOP
User requested termination.

Not applicable.

## 8  Parallelism and Performance

Not applicable.

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 $100$ observations. A penalty, $\beta$ of $3.6$ is used and the minimum segment size is set to $3$. The shape parameter is fixed at $2.1$ across the whole input series.
The cost function used is
 $Cyτi-1+1:τi = 2⁢ a⁢ ni log⁡Si - log a⁢ ni$
where $a$ is a shape parameter that is fixed for all segments and ${n}_{i}={\tau }_{i}-{\tau }_{i-1}+1$.

### 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.