Internals: Integrals

A very common task during optimisation and for various post-fitting tasks is the calculation of the integral of the detection function. That is:

\[\int_\text{left}^\text{right} g(x,\mathbf{z},\theta) dx\]

where $\text{left}$ and $\text{right}$ are the left and right truncation points, which are shortened to $l$ and $r$ below, respectively.

This document provides some analytic results that improve computation time.

Computations were performed using Maxima 5.30.0 (with some simplification by hand). The (wxMaxima) workbook used for the computations may be downloaded here.

Note: For the cases where covariates occur in the detection function, we just have a bunch of different $\sigma$s, so we just do the integral many times for the different values of $\sigma$.

General key plus adjustments (K+A) results

The general form of the K+A model is:

\[g(x, \mathbf{z}, \theta) = K(x, \mathbf{z}, \theta) \left( 1 + A(x, \mathbf{z}, \theta) \right)\]

where $A$ is some sum of adjustment terms (cosines, simple polynomials or Hermite polynomials).

Integrating that:

\[\int_l^r g(x, \mathbf{z}, \theta) dx = \int_r^l K(x, \mathbf{z}, \theta) \left( 1 + A(x, \mathbf{z}, \theta) \right) dx \\ = \int_l^r K(x, \mathbf{z}, \theta) dx + \int_l^r K(x, \mathbf{z}, \theta) A(x,\mathbf{z}, \theta ) dx\]

and since $A$ is a series, we can write $A(x,\mathbf{z},\theta) = \sum_j A_j(x,\mathbf{z},\theta)$, so:

\[g(x, \mathbf{z}, \theta) = \int_l^r K(x, \mathbf{z}, \theta) dx + \sum_j \int_l^r K(x, \mathbf{z}, \theta) A_j(x, \mathbf{z}, \theta)dx\]

Half-normal detection function

For half-normal, $g$ has the following form:

\[g(x,\mathbf{z},\sigma) = \exp\left(-\frac{x^2}{2 \sigma^2}\right)\]

Half-normal, key only

\[\int_l^r \exp\left(-\frac{x^2}{2 \sigma^2} \right) dx = \sigma\sqrt{\frac{\pi }{2}}\left(\mathrm{erf}\left( \frac{r}{\sqrt{2}\sigma}\right)-\mathrm{erf}\left( \frac{l}{\sqrt{2}\sigma}\right)\right)\]

Note to define the error function in R, use:

erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1

Half-normal with cosine adjustments

\[g(x,\mathbf{z},\sigma, a_1, a_2, \ldots) = \exp\left(-\frac{x^2}{2 \sigma^2}\right) \left[ 1+\sum_{j=1}^m a_j\cos\left(\frac{j \pi x}{w}\right)\right]\]

by the above, we need only to calculate

\[\int_l^r K(x,\mathbf{z},\theta)A_j(x,\mathbf{z},\theta)dx\]

which only changes due to differing $a_j$ and $j$ in this case, so the integral we’re interested in is:

\[\int_l^r \exp\left(-\frac{x^2}{2 \sigma^2}\right) a_j\cos\left(\frac{j \pi x}{w}\right) dx =\] \[\frac{\sqrt{\pi }{a}_{j} \sigma}{2^{\frac{3}{2}}}{e}^{−\frac{(j \pi \sigma)^{2}}{2 w^{2}}}\left[ \mathrm{erf}\left( \frac{r w + i j \pi \sigma^2}{\sqrt{2} s w}\right) +\mathrm{erf}\left( \frac{r w− i j \pi \sigma^2}{\sqrt{2} s w}\right) −\mathrm{erf}\left( \frac{l w+i j \pi \sigma^2}{\sqrt{2} s w}\right) −\mathrm{erf}\left( \frac{l w−i j \pi \sigma^2}{\sqrt{2} s w}\right) \right]\]

where $i=\sqrt{-1}$


\[\int_0^\text{right} 1- \exp\left[ -\left(\frac{x}{\sigma}\right)^{-b} \right] dx = \text{right}-\frac{\sigma \Gamma \left(-\frac{1}{b},\left(\frac{\sigma }{\text{right}}\right)^b\right)}{b}\]


From Becker and Quang (2009),

\[\int_0^\text{left} g(x,h,r,\lambda) dx = h\lambda \mathbb{P}\left[X \leq \frac{w}{b \lambda}\right]\]

where $X \sim \Gamma(r,1)$ and

\[b = \frac{1}{\Gamma(r)} \left( \frac{r-1}{e}\right)^{r-1}\]

$r>1$ is the shape parameter of the gamma (to be estimated) and $0 < h \leq 1$.


Becker, EF, and PX Quang. A Gamma-Shaped Detection Function for Line-Transect Surveys with Mark-Recapture and Covariate Data. Journal of Agricultural, Biological, and Environmental Statistics 14(2) (2009): 207–23.