Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_tsa_cp_pelt (g13na)

## Purpose

nag_tsa_cp_pelt (g13na) 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.

## Syntax

[tau, sparam, ifail] = g13na(ctype, y, 'n', n, 'beta', beta, 'minss', minss, 'param', param)
[tau, sparam, ifail] = nag_tsa_cp_pelt(ctype, y, 'n', n, 'beta', beta, 'minss', minss, 'param', param)

## 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 cost function, $C\left({y}_{{\tau }_{i-1}+1:{\tau }_{i}}\right)$ nag_tsa_cp_pelt (g13na) 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.
nag_tsa_cp_pelt (g13na) 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 log2⁢π + logσi2 + yj-μi2 σi2$
• Mean changes: $\theta =\left\{\mu \right\}$
 $Cyτi-1+1:τi = ∑ j=τi-1 τi yj - ni-1 ⁢ Si 2 σ2$
• Variance changes: $\theta =\left\{{\sigma }^{2}\right\}$
 $Cyτ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\}$
 $Cyτ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\}$
 $Cyτ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\}$
 $Cyτ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\}$
 $Cyτ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}$.

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{ctype}$int64int32nag_int scalar
A flag indicating the assumed distribution of the data and the type of change point being looked for.
${\mathbf{ctype}}=1$
Data from a Normal distribution, looking for changes in the mean, $\mu$.
${\mathbf{ctype}}=2$
Data from a Normal distribution, looking for changes in the standard deviation $\sigma$.
${\mathbf{ctype}}=3$
Data from a Normal distribution, looking for changes in the mean, $\mu$ and standard deviation $\sigma$.
${\mathbf{ctype}}=4$
Data from a Gamma distribution, looking for changes in the scale parameter $b$.
${\mathbf{ctype}}=5$
Data from an exponential distribution, looking for changes in $\lambda$.
${\mathbf{ctype}}=6$
Data from a Poisson distribution, looking for changes in $\lambda$.
Constraint: ${\mathbf{ctype}}=1$, $2$, $3$, $4$, $5$ or $6$.
2:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
$y$, the time series.
if ${\mathbf{ctype}}=6$, that is the data is assumed to come from a Poisson distribution, $⌊y+0.5⌋$ is used in all calculations.
Constraints:
• if ${\mathbf{ctype}}=4$, $5$ or $6$, ${\mathbf{y}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$;
• if ${\mathbf{ctype}}=6$, each value of y must be representable as an integer;
• if ${\mathbf{ctype}}\ne 6$, each value of y must be small enough such that ${{\mathbf{y}}\left(\mathit{i}\right)}^{2}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$, can be calculated without incurring overflow.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array y.
$n$, the length of the time series.
Constraint: ${\mathbf{n}}\ge 2$.
2:     $\mathrm{beta}$ – double scalar
Default:
• if ${\mathbf{ctype}}=3$, $2×\mathrm{log}{\mathbf{n}}$;
• otherwise $\mathrm{log}{\mathbf{n}}$.
$\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}}=3$ 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.
3:     $\mathrm{minss}$int64int32nag_int scalar
Default: $2$
The minimum distance between two change points, that is ${\tau }_{i}-{\tau }_{i-1}\ge {\mathbf{minss}}$.
Constraint: ${\mathbf{minss}}\ge 2$.
4:     $\mathrm{param}\left(1\right)$ – double array
$\varphi$, values for the parameters that will be treated as fixed. If ${\mathbf{ctype}}=4$ then param must be supplied.
${\mathbf{ctype}}=1$
${\mathbf{param}}\left(1\right)=\sigma$, the standard deviation of the normal distribution. If not supplied then $\sigma$ is estimated from the full input data,
${\mathbf{ctype}}=2$
${\mathbf{param}}\left(1\right)=\mu$, the mean of the normal distribution. If not supplied then $\mu$ is estimated from the full input data,
${\mathbf{ctype}}=4$
${\mathbf{param}}\left(1\right)$ must hold the shape, $a$, for the gamma distribution,
otherwise
param is not referenced.
Constraint: if ${\mathbf{ctype}}=1$ or $4$, ${\mathbf{param}}\left(1\right)>0.0$.

### Output Parameters

1:     $\mathrm{tau}\left(\mathbf{ntau}\right)$int64int32nag_int array
The dimension of the array tau will be $\mathbf{ntau}$
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\right),1\le i\le m$.
2:     $\mathrm{sparam}\left(\right)$ – double array
Note: sparam will be an array of size $\left(\mathbf{ntau}\right)$ If , and of size $\left(2,\mathbf{ntau}\right)$ otherwise.
The estimated values of the distribution parameters in each segment
${\mathbf{ctype}}=1$, $2$ or $3$
${\mathbf{sparam}}\left(1,i\right)={\mu }_{i}$ and ${\mathbf{sparam}}\left(2,i\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}}=1$ and ${\mu }_{i}={\mu }_{j}$ when ${\mathbf{ctype}}=2$, for all $i$ and $j$.
${\mathbf{ctype}}=4$
${\mathbf{sparam}}\left(1,i\right)={a}_{i}$ and ${\mathbf{sparam}}\left(2,i\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(1\right)$ for all $i$.
${\mathbf{ctype}}=5$ or $6$
${\mathbf{sparam}}\left(i\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.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=11$
Constraint: ${\mathbf{ctype}}=1$, $2$, $3$, $4$, $5$ or $6$.
${\mathbf{ifail}}=21$
Constraint: ${\mathbf{n}}\ge 2$.
${\mathbf{ifail}}=31$
Constraint: if ${\mathbf{ctype}}=4$, $5$ or $6$ then ${\mathbf{y}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
${\mathbf{ifail}}=32$
On entry, ${\mathbf{y}}\left(_\right)=_$, is too large.
${\mathbf{ifail}}=51$
Constraint: ${\mathbf{minss}}\ge 2$.
${\mathbf{ifail}}=71$
Constraint: if ${\mathbf{ctype}}=1$ or $4$ and param has been supplied, then ${\mathbf{param}}\left(1\right)>0.0$.
W  ${\mathbf{ifail}}=200$
To avoid overflow some truncation occurred when calculating the cost function, $C$. All output is returned as normal.
W  ${\mathbf{ifail}}=201$
To avoid overflow some truncation occurred when calculating the parameter estimates returned in sparam. All output is returned as normal.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

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 $\stackrel{-}{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 nag_tsa_cp_pelt (g13na) should be sufficient for the majority of datasets. If a more stable method of calculating $C$ is deemed necessary, nag_tsa_cp_pelt_user (g13nb) can be used and the method chosen implemented in the user-supplied cost function.

None.

## Example

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.
function g13na_example

fprintf('g13na example results\n\n');

% Input series
y = [ 0.00; 0.78;-0.02; 0.17; 0.04;-1.23; 0.24; 1.70; 0.77; 0.06;
0.67; 0.94; 1.99; 2.64; 2.26; 3.72; 3.14; 2.28; 3.78; 0.83;
2.80; 1.66; 1.93; 2.71; 2.97; 3.04; 2.29; 3.71; 1.69; 2.76;
1.96; 3.17; 1.04; 1.50; 1.12; 1.11; 1.00; 1.84; 1.78; 2.39;
1.85; 0.62; 2.16; 0.78; 1.70; 0.63; 1.79; 1.21; 2.20;-1.34;
0.04;-0.14; 2.78; 1.83; 0.98; 0.19; 0.57;-1.41; 2.05; 1.17;
0.44; 2.32; 0.67; 0.73; 1.17;-0.34; 2.95; 1.08; 2.16; 2.27;
-0.14;-0.24; 0.27; 1.71;-0.04;-1.03;-0.12;-0.67; 1.15;-1.10;
-1.37; 0.59; 0.44; 0.63;-0.06;-0.62; 0.39;-2.63;-1.63;-0.42;
-0.73; 0.85; 0.26; 0.48;-0.26;-1.77;-1.53;-1.39; 1.68; 0.43];

% Type of change point(s) being looked for
% (change in mean, assuming a Normal distribution)
ctype = int64(1);

% Standard deviation to use for Normal distribution
param = 1;

% The routines used in this example issue warnings, but return
% sensible restults, so save current warning state and turn warnings on
warn_state = nag_issue_warnings();
nag_issue_warnings(true);

[tau,sparam,ifail] = g13na( ...
ctype,y,'param',param);

% Reset the warning state to its initial value
nag_issue_warnings(warn_state);

% Print the results
fprintf('  -- Change Points --         --- Distribution ---\n');
fprintf('  Number     Position              Parameters\n');
fprintf(' ==================================================\n');
for i = 1:numel(tau)
fprintf('%5d%13d%16.2f%16.2f\n', i, tau(i), sparam(1:2,i));
end

% Plot the results
fig1 = figure;

% Plot the original series
plot(y,'Color','red');

% Mark the change points, drop the last one as it is always
% at the end of the series
xpos = transpose(double(tau(1:end-1))*ones(1,2));
ypos = diag(ylim)*ones(2,numel(tau)-1);
line(xpos,ypos,'Color','black');

% Plot the estimated mean in each segment
xpos = transpose(cat(2,cat(1,1,tau(1:end-1)),tau));
ypos = ones(2,1)*sparam(1,:);
line(xpos,ypos,'Color','green');

title({'{\bf g13na Example Plot}',
'Simulated time series and corresponding changes in mean'});
xlabel('{\bf Time}');
ylabel('{\bf Value}');


g13na example results

-- Change Points --         --- Distribution ---
Number     Position              Parameters
==================================================
1           12            0.34            1.00
2           32            2.57            1.00
3           49            1.45            1.00
4           52           -0.48            1.00
5           70            1.20            1.00
6          100           -0.23            1.00

This example plot shows the original data series, the estimated change points and the estimated mean in each of the identified segments.