Home  /  Products  /  Stata 18  /  Vectorized numerical integration

<- See Stata 18's new features


  • Convenient solution for a vector of integration problems

  • Additive quadrature model

  • Robustness to singular points

Numerical integration is used in many computations of integrals when the analytic solutions are not available or difficult to calculate. Vectorized numerical integration approximates a vector of univariate numerical integrations simultaneously.

Mata's new class, QuadratureVec(), is functionally the same as Quadrature(), except that it handles a vector of integration problems more conveniently. More precisely, QuadratureVec() approximates a vector of univariate integrals numerically by the adaptive Gauss–Kronrod method (the adaptive Simpson method is also provided for comparison).

QuadratureVec() is used in the same way as Quadrature() in only four steps, namely, creating an instance of the class QuadratureVec(), specifying the evaluator functions, setting the limits, and performing the computations.

Let's see it work

The new Mata class QuadratureVec() is created for the storage and computation of vectorized numerical integration.

Here is an example to approximate the following three integrals:

\[ \int_1^2 2x\ dx\] \[\int_0^{\pi} {\rm sin}(x)\, dx\] \[\int_0^1 \exp(x)\, dx \]

We define evaluator functions f1(), f2(), and f3():

. mata: 
mata (type end to exit)
: real scalar f1 (real scalar x) {return(2*x)} : real scalar f2 (real scalar x) {return(sin(x))} : real scalar f3 (real scalar x) {return(exp(x))}

Having defined the evaluator functions, we follow the four steps that are required each time we use the QuadratureVec() class. First, we create an instance q of the QuadratureVec() class:

: q = QuadratureVec()

Second, we use setEvaluator() to point to the evaluator functions, defined as the column vector evaluator:

: evaluator = (&f1() \ &f2() \ &f3())
: q.setEvaluator(evaluator)

Third, we use setLimits() to specify the lower and upper limits, defined as limits:

: limits = ((1, 2) \ (0,pi()) \ (0,1))
: q.setLimits(limits)

Fourth, we use integrate() to compute the approximations:

: q.integrate()
1 3
2 2
3 1.718281828
: end

We find that

\[ \int_1^2 2x\ dx = 3\] \[\int_0^{\pi} {\rm sin}(x)\, dx = 2\] \[\int_0^1 \exp(x)\, dx = {1.718281828} \]

Tell me more

Learn more about Mata's numerical integration features.

Read more about numerical integration in the Stata Mata Reference Manual; see [M] Quadrature().

View all the new features in Stata 18.

Made for data science.

Get started today.