NAG CL Interfaceg13nac (cp_​pelt)

Settings help

CL Name Style:

1Purpose

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

2Specification

 #include
 void g13nac (Nag_TS_ChangeType ctype, Integer n, const double y[], double beta, Integer minss, const double param[], Integer *ntau, Integer tau[], double sparam[], NagError *fail)
The function may be called by the names: g13nac or nag_tsa_cp_pelt.

3Description

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 cost function, $C\left({y}_{{\tau }_{i-1}+1:{\tau }_{i}}\right)$ g13nac solves
 $minimize m,τ ∑ i=1 m (C(yτ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.
g13nac supplies four families of cost function. Each cost function assumes that the series, $y$, comes from some distribution, $D\left(\Theta \right)$. The parameter space, $\Theta =\left\{\theta ,\varphi \right\}$ is subdivided into $\theta$ containing those parameters allowed to differ in each segment and $\varphi$ those parameters treated as constant across all segments. All four cost functions can then be described in terms of the likelihood function, $L$ and are given by:
 $C ( y ( τ i-1 +1) : τi ) = -2 ⁢ log⁡ L (θ^i,ϕ| y ( τ i-1 +1) : τi )$
where ${\stackrel{^}{\theta }}_{i}$ is the maximum likelihood estimate of $\theta$ within the $i$th segment. In all four cases setting $K=0$ satisfies equation (2). Four distributions are available: Normal, Gamma, Exponential and Poisson. Letting
 $Si= ∑ j=τi-1 τi yj$
the log-likelihoods and cost functions for the four distributions, and the available subdivisions of the parameter space are:
• Normal distribution: $\Theta =\left\{\mu ,{\sigma }^{2}\right\}$
 $-2⁢log⁡L = ∑ i=1 m ∑ j=τi-1 τi log(2⁢π) + log(σi2) + (yj-μi)2 σi2$
• Mean changes: $\theta =\left\{\mu \right\}$
 $C(yτi-1+1:τi) = ∑ j=τi-1 τi (yj-ni-1⁢Si) 2 σ2$
• Variance changes: $\theta =\left\{{\sigma }^{2}\right\}$
 $C(yτi-1+1:τi) = ni ⁢ (log( ∑ j=τi-1 τi (yj-μ) 2 )-log⁡ni)$
• Both mean and variance change: $\theta =\left\{\mu ,{\sigma }^{2}\right\}$
 $C(yτi-1+1:τi) = ni ⁢ (log( ∑ j=τi-1 τi (yj-ni-1⁢Si) 2 )-log⁡ni)$
• Gamma distribution: $\Theta =\left\{a,b\right\}$
 $-2⁢log⁡L = 2× ∑ i=1 m ∑ j=τi-1 τi log⁡Γ(ai)+ ai⁢log⁡bi+ (1-ai)⁢log⁡yj+ yj bi$
• Scale changes: $\theta =\left\{b\right\}$
 $C(yτi-1+1:τi) = 2⁢ a⁢ ni (log⁡Si-log(a⁢ni))$
• Exponential Distribution: $\Theta =\left\{\lambda \right\}$
 $- 2⁢log⁡L = 2× ∑ i=1 m ∑ j=τi-1 τi log⁡λi+ yj λi$
• Mean changes: $\theta =\left\{\lambda \right\}$
 $C(yτi-1+1:τi) = 2⁢ ni (log⁡Si-log⁡ni)$
• Poisson distribution: $\Theta =\left\{\lambda \right\}$
 $-2⁢log⁡L = 2× ∑ i=1 m ∑ j=τi-1 τi λi- ⌊yj+0.5⌋⁢log⁡λi+ log⁡Γ(⌊yj+0.5⌋+1)$
• Mean changes: $\theta =\left\{\lambda \right\}$
 $C(yτi-1+1:τi) = 2⁢ Si ⁢ (log⁡ni-log⁡Si)$
when calculating ${S}_{i}$ for the Poisson distribution, the sum is calculated for $⌊{y}_{i}+0.5⌋$ rather than ${y}_{i}$.

4References

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

5Arguments

1: $\mathbf{ctype}$Nag_TS_ChangeType Input
On entry: a flag indicating the assumed distribution of the data and the type of change point being looked for.
${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$
Data from a Normal distribution, looking for changes in the mean, $\mu$.
${\mathbf{ctype}}=\mathrm{Nag_NormalStd}$
Data from a Normal distribution, looking for changes in the standard deviation $\sigma$.
${\mathbf{ctype}}=\mathrm{Nag_NormalMeanStd}$
Data from a Normal distribution, looking for changes in the mean, $\mu$ and standard deviation $\sigma$.
${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$
Data from a Gamma distribution, looking for changes in the scale parameter $b$.
${\mathbf{ctype}}=\mathrm{Nag_ExponentialLambda}$
Data from an exponential distribution, looking for changes in $\lambda$.
${\mathbf{ctype}}=\mathrm{Nag_PoissonLambda}$
Data from a Poisson distribution, looking for changes in $\lambda$.
Constraint: ${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$, $\mathrm{Nag_NormalStd}$, $\mathrm{Nag_NormalMeanStd}$, $\mathrm{Nag_GammaScale}$, $\mathrm{Nag_ExponentialLambda}$ or $\mathrm{Nag_PoissonLambda}$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{n}}\ge 2$.
3: $\mathbf{y}\left[{\mathbf{n}}\right]$const double Input
On entry: $y$, the time series.
If ${\mathbf{ctype}}=\mathrm{Nag_PoissonLambda}$, that is the data is assumed to come from a Poisson distribution, $⌊y+0.5⌋$ is used in all calculations.
Constraints:
• if ${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$, $\mathrm{Nag_ExponentialLambda}$ or $\mathrm{Nag_PoissonLambda}$, ${\mathbf{y}}\left[\mathit{i}-1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$;
• if ${\mathbf{ctype}}=\mathrm{Nag_PoissonLambda}$, each value of y must be representable as an integer;
• if ${\mathbf{ctype}}\ne \mathrm{Nag_PoissonLambda}$, each value of y must be small enough such that${{\mathbf{y}}\left[\mathit{i}-1\right]}^{2}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$, can be calculated without incurring overflow.
4: $\mathbf{beta}$double Input
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. This is usually set to $2$ when ${\mathbf{ctype}}=\mathrm{Nag_NormalMeanStd}$ and $1$ otherwise.
If no penalty is required then set $\beta =0$. Generally, the smaller the value of $\beta$ the larger the number of suggested change points.
5: $\mathbf{minss}$Integer Input
On entry: the minimum distance between two change points, that is ${\tau }_{i}-{\tau }_{i-1}\ge {\mathbf{minss}}$.
Constraint: ${\mathbf{minss}}\ge 2$.
6: $\mathbf{param}\left[1\right]$const double Input
On entry: $\varphi$, values for the parameters that will be treated as fixed. If ${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$ then param must be supplied, otherwise param may be NULL.
If supplied, then when
${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$
${\mathbf{param}}\left[0\right]=\sigma$, the standard deviation of the normal distribution. If not supplied then $\sigma$ is estimated from the full input data,
${\mathbf{ctype}}=\mathrm{Nag_NormalStd}$
${\mathbf{param}}\left[0\right]=\mu$, the mean of the normal distribution. If not supplied then $\mu$ is estimated from the full input data,
${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$
${\mathbf{param}}\left[0\right]$ must hold the shape, $a$, for the gamma distribution,
otherwise
param is not referenced.
Constraint: if ${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$ or $\mathrm{Nag_GammaScale}$, ${\mathbf{param}}\left[0\right]>0.0$.
7: $\mathbf{ntau}$Integer * Output
On exit: $m$, the number of change points detected.
8: $\mathbf{tau}\left[{\mathbf{n}}\right]$Integer Output
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.
9: $\mathbf{sparam}\left[2×{\mathbf{n}}+2\right]$double Output
On exit: the estimated values of the distribution parameters in each segment
${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$, $\mathrm{Nag_NormalStd}$ or $\mathrm{Nag_NormalMeanStd}$
${\mathbf{sparam}}\left[2i-2\right]={\mu }_{i}$ and ${\mathbf{sparam}}\left[2i-1\right]={\sigma }_{i}$ for $i=1,2,\dots ,m$, where ${\mu }_{i}$ and ${\sigma }_{i}$ is the mean and standard deviation, respectively, of the values of $y$ in the $i$th segment.
It should be noted that ${\sigma }_{i}={\sigma }_{j}$ when ${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$ and ${\mu }_{i}={\mu }_{j}$ when ${\mathbf{ctype}}=\mathrm{Nag_NormalStd}$, for all $i$ and $j$.
${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$
${\mathbf{sparam}}\left[2i-2\right]={a}_{i}$ and ${\mathbf{sparam}}\left[2i-1\right]={b}_{i}$ for $i=1,2,\dots ,m$, where ${a}_{i}$ and ${b}_{i}$ are the shape and scale parameters, respectively, for the values of $y$ in the $i$th segment. It should be noted that ${a}_{i}={\mathbf{param}}\left[0\right]$ for all $i$.
${\mathbf{ctype}}=\mathrm{Nag_ExponentialLambda}$ or $\mathrm{Nag_PoissonLambda}$
${\mathbf{sparam}}\left[i-1\right]={\lambda }_{i}$ for $i=1,2,\dots ,m$, where ${\lambda }_{i}$ is the mean of the values of $y$ in the $i$th segment.
The remainder of sparam is used as workspace.
10: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface 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 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, ${\mathbf{ctype}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{param}}\left[0\right]=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ctype}}=\mathrm{Nag_NormalMean}$ or $\mathrm{Nag_GammaScale}$ and param has been supplied, then ${\mathbf{param}}\left[0\right]>0.0$.
NE_REAL_ARRAY
On entry, ${\mathbf{ctype}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{y}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ctype}}=\mathrm{Nag_GammaScale}$, $\mathrm{Nag_ExponentialLambda}$ or $\mathrm{Nag_PoissonLambda}$ then ${\mathbf{y}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
On entry, ${\mathbf{y}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$, is too large.
NW_TRUNCATED
To avoid overflow some truncation occurred when calculating the cost function, $C$. 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.

7Accuracy

For efficiency reasons, when calculating the cost functions, $C$ and the parameter estimates returned in sparam, this function makes use of the mathematical identities:
 $∑ j=u v yj 2 = ∑ j=1 v yj 2 - ∑ j=1 u-1 yj 2$
and
 $∑ j=1 n (yj-y¯) 2 = ( ∑ j=1 n yj2) - n ⁢ y¯ 2$
where $\overline{y}={n}^{-1}\sum _{j=1}^{n}{y}_{j}$.
The input data, $y$, is scaled in order to try and mitigate some of the known instabilities associated with using these formulations. The results returned by g13nac should be sufficient for the majority of datasets. If a more stable method of calculating $C$ is deemed necessary, g13nbc can be used and the method chosen implemented in the user-supplied cost function.

8Parallelism and Performance

g13nac is not threaded in any implementation.

None.

10Example

This example identifies changes in the mean, under the assumption that the data is normally distributed, for a simulated dataset with $100$ observations. A BIC penalty is used, that is $\beta =\mathrm{log}n\approx 4.6$, the minimum segment size is set to $2$ and the variance is fixed at $1$ across the whole input series.

10.1Program Text

Program Text (g13nace.c)

10.2Program Data

Program Data (g13nace.d)

10.3Program Results

Program Results (g13nace.r)
This example plot shows the original data series, the estimated change points and the estimated mean in each of the identified segments.