*! version 1.0 8 Aug 2018

Title

integrate -- Numerical integration for one dimensional functions

Syntax

integrate
[,
options]

Main
installmata specifies that the mata code be installed in your personal directory.

lower(#) specifies the lower limit of the integral. Default value is -1.

upper(#) specifies the upper limit of the integral. Default value is 1.

function(string) specifies the function to be integrated, this must be a function of x.

quadpts(#) specifies the number of quadrature points. Default value is 80.

vectorise specifies the function needs to be vectorised.

Description

integrate is an implementation of Gaussian quadrature.
The current command does not attempt to look at numerical errors but the user can alter the number of
quadrature points to inspect any numerical instabilities.
Indefinite integrals are handled using transformation within this function.

This command has been primarily written in the MATA language but is a Stata
command. The function can be any single line expression and the integration is
with respect to x. The text from the option function() will be used to create a
new function in Mata which is then passed to the integration algorithm.

The number of quadrature points can be chosen to be any number above 1 but the
larger this number the slower the algorithm. There is no upper limit because the
quadrature points are chosen by calculating the eigenvalues and eigenvectors of a
companion matrix.

Updating this command using SSC

To obtain the latest version click the following to install the new version

ssc install integrate,replace

Options

Main

installmata lmata specifies that the mata code be installed in your personal directory.

lower(#) specifies the lower limit of the integral. To specify
that the lower limit is -infinity just specify the missing value . in this
option.

upper(#) specifies the upper limit of the integral; the default is +1. To specify
that the upper limit is +infinity just specify the missing value . in this
option.

function(string) specifies the function to be integrated. This function needs to
be defined in terms of x. If the function contains any other unknowns then it
will crash. The command is much quicker if this function is written in terms
of vector operations. If the function is written without vector operations
then the vectorise option needs to be specified and another function is
constructed that is the vector equivalent of the function (this is slower
given the extra calculations).

quadpts(#) specifies the number of quadrature points to use in the numerical
integration; the default is 100. The numerical integration function can allow
any number of quadrature points but if too many are specified then the
program will become slow.

vectorise specifies that the function specified in the function() option is not
defined in terms of vector operators. The code will generate an additional
step of creating a new function that allows x as a vector. This involves
looping over the elements of the rowvector x so will be considerably slower
but does allow flexibility in the specification of the function.

Examples

To install the mata code permanently click the following command

integrate, installmata

The distribution functions in Mata already accept vectors as arguments so can be used in the function
directly. The following examples are all standard results that can be obtained using the cumulative distribution functions.

integrate, f(normalden(x)) l(-1) u(1)

integrate, f(normalden(x)) l(-1.96) u(1.96)

integrate, f(normalden(x)) l(-1.96) u(.)

integrate, f(normalden(x)) l(.) u(.)

An example of a user-defined function would be the polynomial x+x^2+x^3
note that because this function is not defined by the appropriate vector operations
then the option vectorise needs to be used.

integrate, f(x+x^2+x^3) v l(-10) u(10)

A quicker implementation of the same function would be

integrate, f(x:+x:^2+x:^3) l(-10) u(10)

Stored results

Scalars

r(integral) The value of the integral

Author

Dr Adrian Mander, MRC Biostatistics Unit, University of Cambridge.

Email adrian.mander@mrc-bsu.cam.ac.uk

See Also

Related commands:

integrate_aq (if installed), install by
clicking ssc install integrate_aq

The MATA help file for integrate() mf_integrate.