# NAG FL Interfaceg13ndf (cp_​binary)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g13ndf 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 using one of a provided set of cost functions.

## 2Specification

Fortran Interface
 Subroutine g13ndf ( n, y, beta, ntau, tau,
 Integer, Intent (In) :: ctype, n, minss, iparam, mdepth Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ntau, tau(*) Real (Kind=nag_wp), Intent (In) :: y(n), beta, param(1) Real (Kind=nag_wp), Intent (Out) :: sparam(2*n)
#include <nag.h>
 void g13ndf_ (const Integer *ctype, const Integer *n, const double y[], const double *beta, const Integer *minss, const Integer *iparam, const double param[], const Integer *mdepth, Integer *ntau, Integer tau[], double sparam[], Integer *ifail)
The routine may be called by the names g13ndf or nagf_tsa_cp_binary.

## 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)$, g13ndf gives an approximate solution to
 $minimize m,τ ∑ i=1 m (C(yτi-1+1:τi)+β)$
where $\beta$ is a penalty term used to control the number of change points. The solution is obtained in an iterative manner as follows:
1. 1.Set $u=1$, $w=n$ and $k=0$
2. 2.Set $k=k+1$. If $k>K$, where $K$ is a user-supplied control parameter, then terminate the process for this segment.
3. 3.Find $v$ that minimizes
 $C(yu:v) + C(yv+1:w)$
4. 4.Test
 $C(yu:v) + C(yv+1:w) + β < C(yu:w)$ (1)
5. 5.If inequality (1) is false then the process is terminated for this segment.
6. 6.If inequality (1) is true, then $v$ is added to the set of change points, and the segment is split into two subsegments, ${y}_{u:v}$ and ${y}_{v+1:w}$. The whole process is repeated from step 2 independently on each subsegment, with the relevant changes to the definition of $u$ and $w$ (i.e., $w$ is set to $v$ when processing the left-hand subsegment and $u$ is set to $v+1$ when processing the right-hand subsegment.
The change points are ordered to give $\tau$.
g13ndf 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 the ${\stackrel{^}{\theta }}_{i}$ is the maximum likelihood estimate of $\theta$ within the $i$th segment. Four distributions are available; Normal, Gamma, Exponential and Poisson distributions. 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
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## 5Arguments

1: $\mathbf{ctype}$Integer Input
On entry: 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: $\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)$Real (Kind=nag_wp) array Input
On entry: $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.
4: $\mathbf{beta}$Real (Kind=nag_wp) 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}}=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.
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{iparam}$Integer Input
On entry: if ${\mathbf{iparam}}=1$ distributional parameters have been supplied in param.
Constraints:
• if ${\mathbf{ctype}}=4$, ${\mathbf{iparam}}=1$;
• otherwise ${\mathbf{iparam}}=0$ or $1$.
7: $\mathbf{param}\left(1\right)$Real (Kind=nag_wp) array Input
On entry: $\varphi$, values for the parameters that will be treated as fixed. If ${\mathbf{iparam}}=0$ then param is not referenced.
If supplied, then when
${\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$.
8: $\mathbf{mdepth}$Integer Input
On entry: $K$, the maximum depth for the iterative process, which in turn puts an upper limit on the number of change points with $m\le {2}^{K}$.
If $K\le 0$ 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.
9: $\mathbf{ntau}$Integer Output
On exit: $m$, the number of change points detected.
10: $\mathbf{tau}\left(*\right)$Integer array Output
Note: the dimension of the array tau must be at least $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(⌈\frac{{\mathbf{n}}}{{\mathbf{minss}}}⌉,{2}^{{\mathbf{mdepth}}}\right)$ if ${\mathbf{mdepth}}>0$, and at least $⌈\frac{{\mathbf{n}}}{{\mathbf{minss}}}⌉$ otherwise.
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\right),1\le i\le m$.
The remainder of tau is used as workspace.
11: $\mathbf{sparam}\left(2×{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: the estimated values of the distribution parameters in each segment
${\mathbf{ctype}}=1$, $2$ or $3$
${\mathbf{sparam}}\left(2i-1\right)={\mu }_{i}$ and ${\mathbf{sparam}}\left(2i\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(2i-1\right)={a}_{i}$ and ${\mathbf{sparam}}\left(2i\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.
The remainder of sparam is used as workspace.
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=11$
On entry, ${\mathbf{ctype}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ctype}}=1$, $2$, $3$, $4$, $5$ or $6$.
${\mathbf{ifail}}=21$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
${\mathbf{ifail}}=31$
On entry, ${\mathbf{ctype}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{y}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
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(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$, is too large.
${\mathbf{ifail}}=51$
On entry, ${\mathbf{minss}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{minss}}\ge 2$.
${\mathbf{ifail}}=61$
On entry, ${\mathbf{iparam}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ctype}}\ne 4$ then ${\mathbf{iparam}}=0$ or $1$.
${\mathbf{ifail}}=62$
On entry, ${\mathbf{iparam}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ctype}}=4$ then ${\mathbf{iparam}}=1$.
${\mathbf{ifail}}=71$
On entry, ${\mathbf{ctype}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{param}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ctype}}=1$ or $4$ and ${\mathbf{iparam}}=1$, then ${\mathbf{param}}\left(1\right)>0.0$.
${\mathbf{ifail}}=200$
To avoid overflow some truncation occurred when calculating the cost function, $C$. All output is returned as normal.
${\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$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The calculation of means and sums of squares about the mean during the evaluation of the cost functions are based on the one pass algorithm of West (1979) and are believed to be stable.

## 8Parallelism and Performance

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

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 (g13ndfe.f90)

### 10.2Program Data

Program Data (g13ndfe.d)

### 10.3Program Results

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