# NAG FL Interfaced01gbf (md_​mcarlo)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

d01gbf returns 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 routine is suitable for low accuracy work.

## 2Specification

Fortran Interface
 Subroutine d01gbf ( ndim, a, b, f, eps, acc,
 Integer, Intent (In) :: ndim, maxcls, lenwrk Integer, Intent (Inout) :: mincls, ifail Real (Kind=nag_wp), External :: f Real (Kind=nag_wp), Intent (In) :: a(ndim), b(ndim), eps Real (Kind=nag_wp), Intent (Inout) :: wrkstr(lenwrk), finest Real (Kind=nag_wp), Intent (Out) :: acc
#include <nag.h>
 void d01gbf_ (const Integer *ndim, const double a[], const double b[], Integer *mincls, const Integer *maxcls, double (NAG_CALL *f)(const Integer *ndim, const double x[]),const double *eps, double *acc, const Integer *lenwrk, double wrkstr[], double *finest, Integer *ifail)
The routine may be called by the names d01gbf or nagf_quad_md_mcarlo.

## 3Description

d01gbf uses an adaptive Monte Carlo method based on the algorithm described in Lautrup (1971). It is implemented for integrals of the form:
 $∫ a1 b1 ∫ a2 b2 ⋯ ∫ an bn f (x1,x2,…,xn) dxn ⋯ dx2 dx1 .$
Upon entry, unless lenwrk has been set to the minimum value $10×{\mathbf{ndim}}$, the routine 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 routine 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.

## 4References

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

## 5Arguments

1: $\mathbf{ndim}$Integer Input
On entry: $n$, the number of dimensions of the integral.
Constraint: ${\mathbf{ndim}}\ge 1$.
2: $\mathbf{a}\left({\mathbf{ndim}}\right)$Real (Kind=nag_wp) array Input
On entry: the lower limits of integration, ${a}_{i}$, for $\mathit{i}=1,2,\dots ,n$.
3: $\mathbf{b}\left({\mathbf{ndim}}\right)$Real (Kind=nag_wp) array Input
On entry: the upper limits of integration, ${b}_{i}$, for $\mathit{i}=1,2,\dots ,n$.
4: $\mathbf{mincls}$Integer Input/Output
On entry: must be set
• either to the minimum number of integrand evaluations to be allowed, in which case ${\mathbf{mincls}}\ge 0$;
• or to a negative value. In this case, the routine assumes that a previous call had been made with the same arguments ndim, a and b and with either the same integrand (in which case d01gbf continues calculation) or a similar integrand (in which case d01gbf begins the calculation with the subdivision used in the last iteration of the previous call). See also wrkstr.
On exit: contains the number of integrand evaluations actually used by d01gbf.
5: $\mathbf{maxcls}$Integer Input
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)$.
6: $\mathbf{f}$real (Kind=nag_wp) Function, supplied by the user. External Procedure
f must return the value of the integrand at a given point.
The specification of f is:
Fortran Interface
 Function f ( ndim, x)
 Real (Kind=nag_wp) :: f Integer, Intent (In) :: ndim Real (Kind=nag_wp), Intent (In) :: x(ndim)
 double f (const Integer *ndim, const double x[])
1: $\mathbf{ndim}$Integer Input
On entry: $n$, the number of dimensions of the integral.
2: $\mathbf{x}\left({\mathbf{ndim}}\right)$Real (Kind=nag_wp) array Input
On entry: the coordinates of the point at which the integrand $f$ must be evaluated.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d01gbf is called. Arguments denoted as Input must not be changed by this procedure.
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d01gbf. If your code inadvertently does return any NaNs or infinities, d01gbf is likely to produce unexpected results.
7: $\mathbf{eps}$Real (Kind=nag_wp) Input
On entry: the relative accuracy required.
Constraint: ${\mathbf{eps}}\ge 0.0$.
8: $\mathbf{acc}$Real (Kind=nag_wp) Output
On exit: the estimated relative accuracy of finest.
9: $\mathbf{lenwrk}$Integer Input
On entry: the dimension of the array wrkstr as declared in the (sub)program from which d01gbf is called.
For maximum efficiency, lenwrk should be about
 $3×ndim×(maxcls/4)1/ndim+7×ndim.$
If lenwrk is given the value $10×{\mathbf{ndim}}$ then the subroutine uses only one iteration of a crude Monte Carlo method with maxcls sample points.
Constraint: ${\mathbf{lenwrk}}\ge 10×{\mathbf{ndim}}$.
10: $\mathbf{wrkstr}\left({\mathbf{lenwrk}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: if ${\mathbf{mincls}}<0$, wrkstr must be unchanged from the previous call of d01gbf – except that for a new integrand ${\mathbf{wrkstr}}\left({\mathbf{lenwrk}}\right)$ must be set to $0.0$. See also mincls.
On exit: contains information about the current sub-interval structure which could be used in later calls of d01gbf. In particular, ${\mathbf{wrkstr}}\left(j\right)$ gives the number of sub-intervals used along the $j$th coordinate axis.
11: $\mathbf{finest}$Real (Kind=nag_wp) Input/Output
On entry: must be unchanged from a previous call to d01gbf.
On exit: the best estimate obtained for the integral.
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 $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. 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:
Note: in some cases d01gbf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{eps}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{eps}}\ge 0.0$.
On entry, lenwrk is too small. ${\mathbf{lenwrk}}=⟨\mathit{\text{value}}⟩$. Minimum possible dimension: $⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{maxcls}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{mincls}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxcls}}>{\mathbf{mincls}}$.
On entry, ${\mathbf{maxcls}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ndim}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxcls}}\ge 4×\left({\mathbf{ndim}}+1\right)$.
On entry, ${\mathbf{ndim}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ndim}}\ge 1$.
${\mathbf{ifail}}=2$
maxcls too small to obtain required accuracy eps. ${\mathbf{maxcls}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{eps}}=⟨\mathit{\text{value}}⟩$.
${\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

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 want a higher confidence level then a smaller value of eps should be used.

## 8Parallelism and Performance

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

The running time for d01gbf 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, d01gbf 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, d01gbf 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 routine.

## 10Example

This example calculates the integral
 $∫01∫01∫01∫014x1x3exp(2x1x3) (1+x2+x4) 2dx1dx2dx3dx4=0.575364.$

### 10.1Program Text

Program Text (d01gbfe.f90)

None.

### 10.3Program Results

Program Results (d01gbfe.r)