d01 Chapter Contents
d01 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_multid_quad_monte_carlo_1 (d01xbc)

## 1  Purpose

nag_multid_quad_monte_carlo_1 (d01xbc) evaluates an approximation to the integral of a function over a hyper-rectangular region, using a Monte–Carlo method. An approximate relative error estimate is also returned. This function is suitable for low accuracy work.

## 2  Specification

 #include #include
 double (*f)(Integer ndim, const double x[], Nag_User *comm),
Nag_MCMethod method, Nag_Start cont, const double a[], const double b[], Integer *mincls, Integer maxcls, double eps, double *finest, double *acc, double **comm_arr, Nag_User *comm, NagError *fail)

## 3  Description

nag_multid_quad_monte_carlo_1 (d01xbc) uses an adaptive Monte–Carlo method based on the algorithm described by Lautrup (1971). It is implemented for integrals of the form:
 $∫ a 1 b 1 ∫ a 2 b 2 ⋯ ∫ a n b n f x 1 , x 2 , … , x n dxn ⋯ dx2 dx1 .$
Upon entry, unless the argument ${\mathbf{method}}=\mathrm{Nag_OneIteration}$, the function subdivides the integration region into a number of equal volume subregions. Inside each subregion the integral and the variance are estimated by means of pseudorandom sampling. All contributions are added together to produce an estimate for the whole integral and total variance. The variance along each coordinate axis is determined and the function uses this information to increase the density and change the widths of the sub-intervals along each axis, so as to reduce the total variance. The total number of subregions is then increased by a factor of two and the program recycles for another iteration. The program stops when a desired accuracy has been reached or too many integral evaluations are needed for the next cycle.

## 4  References

Lautrup B (1971) An adaptive multi-dimensional integration procedure Proc. 2nd Coll. Advanced Methods in Theoretical Physics, Marseille

## 5  Arguments

1:     ndimIntegerInput
On entry: the number of dimensions of the integral, $n$.
Constraint: ${\mathbf{ndim}}\ge 1$.
2:     ffunction, supplied by the userExternal Function
f must return the value of the integrand $f$ at a given point.
The specification of f is:
 double f (Integer ndim, const double x[], Nag_User *comm)
1:     ndimIntegerInput
On entry: the number of dimensions of the integral.
2:     x[ndim]const doubleInput
On entry: the coordinates of the point at which the integrand must be evaluated.
3:     commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ should be cast to the required type, e.g., struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument comm below.)
3:     methodNag_MCMethodInput
On entry: the method to be used.
${\mathbf{method}}=\mathrm{Nag_OneIteration}$
The function uses only one iteration of a crude Monte–Carlo method with maxcls sample points.
${\mathbf{method}}=\mathrm{Nag_ManyIterations}$
The function subdivides the integration region into a number of equal volume subregions.
Constraint: ${\mathbf{method}}=\mathrm{Nag_OneIteration}$ or $\mathrm{Nag_ManyIterations}$.
4:     contNag_StartInput
On entry: the continuation state of the evaluation of the integrand.
${\mathbf{cont}}=\mathrm{Nag_Cold}$
Indicates that this is the first call to the function with the current integrand and arguments ndim, a and b.
${\mathbf{cont}}=\mathrm{Nag_Hot}$
Indicates that a previous call has been made with the same arguments ndim, a and b with the same integrand. Please note that method must not be changed.
${\mathbf{cont}}=\mathrm{Nag_Warm}$
Indicates that a previous call has been made with the same arguments ndim, a and b but that the integrand is new. Please note that method must not be changed.
Constraint: ${\mathbf{cont}}=\mathrm{Nag_Cold}$, $\mathrm{Nag_Warm}$ or $\mathrm{Nag_Hot}$.
5:     a[ndim]const doubleInput
On entry: the lower limits of integration, ${a}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
6:     b[ndim]const doubleInput
On entry: the upper limits of integration, ${b}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
7:     minclsInteger *Input/Output
On entry: mincls must be set to the minimum number of integrand evaluations to be allowed.
Constraint: $0\le {\mathbf{mincls}}<{\mathbf{maxcls}}$.
On exit: mincls contains the total number of integrand evaluations actually used by nag_multid_quad_monte_carlo_1 (d01xbc).
8:     maxclsIntegerInput
On entry: the maximum number of integrand evaluations to be allowed. In the continuation case this is the number of new integrand evaluations to be allowed. These counts do not include zero integrand values.
Constraints:
• ${\mathbf{maxcls}}>{\mathbf{mincls}}$;
• ${\mathbf{maxcls}}\ge 4×\left({\mathbf{ndim}}+1\right)$.
9:     epsdoubleInput
On entry: the relative accuracy required.
Constraint: ${\mathbf{eps}}\ge 0.0$.
10:   finestdouble *Output
On exit: the best estimate obtained for the integral.
11:   accdouble *Output
On exit: the estimated relative accuracy of finest.
12:   comm_arrdouble **Input/Output
On entry: if ${\mathbf{cont}}=\mathrm{Nag_Warm}$ or $\mathrm{Nag_Hot}$, the memory pointed to and allocated by a previous call of nag_multid_quad_monte_carlo_1 (d01xbc) must be unchanged.
If ${\mathbf{cont}}=\mathrm{Nag_Cold}$ then appropriate memory is allocated internally by nag_multid_quad_monte_carlo_1 (d01xbc).
On exit: comm_arr contains information about the current sub-interval structure which could be used in later calls of nag_multid_quad_monte_carlo_1 (d01xbc). In particular, ${\mathbf{comm_arr}}\left[j-1\right]$ gives the number of sub-intervals used along the $j$th coordinate axis.
When this information is no longer useful, or before a subsequent call to nag_multid_quad_monte_carlo_1 (d01xbc) with ${\mathbf{cont}}=\mathrm{Nag_Cold}$ is made, you should free the storage contained in this pointer using the NAG macro NAG_FREE. Note this memory will have been allocated and needs to be freed only if the error exit NE_NOERROR or NE_QUAD_MAX_INTEGRAND_EVAL occurs. Otherwise, no memory needs to be freed.
13:   commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$, of type Pointer, allows you to communicate information to and from f(). An object of the required type should be declared, e.g., a structure, and its address assigned to the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ by means of a cast to Pointer in the calling program, e.g., comm.p = (Pointer)&s. The type Pointer is void *.
14:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_GE
On entry, ${\mathbf{mincls}}=〈\mathit{\text{value}}〉$ while ${\mathbf{maxcls}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{mincls}}<{\mathbf{maxcls}}$.
NE_2_INT_ARG_LT
On entry, ${\mathbf{maxcls}}=〈\mathit{\text{value}}〉$ while ${\mathbf{ndim}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{maxcls}}\ge 4×\left({\mathbf{ndim}}+1\right)$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument cont had an illegal value.
On entry, argument method had an illegal value.
NE_INT_ARG_LE
On entry, ${\mathbf{mincls}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mincls}}>0$.
NE_INT_ARG_LT
On entry, ${\mathbf{ndim}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ndim}}\ge 1$.
maxcls was too small to obtain the required accuracy.
In this case nag_multid_quad_monte_carlo_1 (d01xbc) returns a value of finest with estimated relative error acc, but acc will be greater than eps. This error exit may be taken before maxcls nonzero integrand evaluations have actually occurred, if the function calculates that the current estimates could not be improved before maxcls was exceeded.
NE_REAL_ARG_LT
On entry, eps must not be less than 0.0: ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.

## 7  Accuracy

A relative error estimate is output through the argument acc. The confidence factor is set so that the actual error should be less than acc 90% of the time. If you desire a higher confidence level then a smaller value of eps should be used.

The running time for nag_multid_quad_monte_carlo_1 (d01xbc) will usually be dominated by the time used to evaluate the integrand f, so the maximum time that could be used is approximately proportional to maxcls.
For some integrands, particularly those that are poorly behaved in a small part of the integration region, this function may terminate with a value of acc which is significantly smaller than the actual relative error. This should be suspected if the returned value of mincls is small relative to the expected difficulty of the integral. Where this occurs, nag_multid_quad_monte_carlo_1 (d01xbc) should be called again, but with a higher entry value of mincls (e.g., twice the returned value) and the results compared with those from the previous call.
The exact values of finest and acc on return will depend (within statistical limits) on the sequence of random numbers generated within this function by calls to nag_random_continuous_uniform (g05cac). Separate runs will produce identical answers unless the part of the program executed prior to calling this function also calls (directly or indirectly) functions from Chapter g05, and the series of such calls differs between runs. If desired, you may ensure the identity or difference between runs of the results returned by this function, by calling nag_random_init_repeatable (g05cbc) or nag_random_init_nonrepeatable (g05ccc) respectively, immediately before calling this function.

## 9  Example

This example calculates the integral
 $∫ 0 1 ∫ 0 1 ∫ 0 1 ∫ 0 1 4 x 1 x 3 2 exp 2 x 1 x 3 1 + x 2 + x 4 2 dx1 dx2 dx3 dx4 = 0.575364 .$

### 9.1  Program Text

Program Text (d01xbce.c)

None.

### 9.3  Program Results

Program Results (d01xbce.r)