Introduction

Continuing on the recently born series on numerical integration, this post will introduce rectangular integration. I will describe the concept behind rectangular integration, show a function in R for how to do it, and use it to check that the distribution actually integrates to 1 over its support set. This post follows from my previous post on trapezoidal integration.

Rectangular integration is a numerical integration technique that approximates the integral of a function with a rectangle. It uses rectangles to approximate the area under the curve. Here are its features:

The rectangle’s width is determined by the interval of integration.

One rectangle could span the width of the interval of integration and approximate the entire integral.

Alternatively, the interval of integration could be sub-divided into smaller intervals of equal lengths, and rectangles would used to approximate the integral; each smaller rectangle has the width of the smaller interval.

The rectangle’s height is the function’s value at the midpoint of its base.

Within a fixed interval of integration, the approximation becomes more accurate as more rectangles are used; each rectangle becomes narrower, and the height of the rectangle better captures the values of the function within that interval.

This method is inspired by the use of Riemann sums to calculate the integral. Roughly speaking, the limit of the Riemann sums of a function as partitions become finer is the Riemann integral. A function is Riemann-integrable if this limit exists, and the Riemann sum becomes closer to the Riemann integral with a sufficiently fine partition.

Recall that the area of a rectangle is just the base multiplied by the height.

.

To adapt this to a rectangle that approximates a function over an interval ,

To approximate the integral of with rectangles over the interval ,

R Script for Integrating Over .

The following code implements rectangular integration to check that the PDF actually integrates to 1 over its support set. It defines 2 functions, creates a vector of the support set, and finally provides the integral. The 2 functions are:

dbeta.2.5(): This produces the PDF values for the Beta(2, 5) distribution. It is needed to calculate the midpoints.

rectangular.integration(): This implements rectangular integration and numerically approximates the integral. dbeta.2.5() is eventually fed into rectangular.integration as its second argument.

After the 2 functions are defined, the support set is created in beta.support, and the integral is computed.

Notice that the function itself is needed as an input for rectangular.integration(); the values of the function on the support set are not given as an input (in contrast to my example for trapezoidal integration). Thus, rectangular.integration() can only be implemented if the function values at the midpoints can be obtained. This may not always be possible, especially when some new function without an analytical expression needs to be integrated and only its values at certain points along its input are known. In my example, I am integrating the PDF of the Beta(2, 5) distribution, and any value of the PDF can be calculated in R, so this works for rectangular.integration().

If your integrand cannot be evaluated at the midpoints of your intervals, you can modify rectangular integration to use the function’s value at either the left boundary or right boundary as the rectangle’s height. These 2 values can be taken directly from the function values, but the resulting approximation is not as good as using the midpoint rule. Nonetheless, rectangular.integration() can be modified to do so, and I will leave this as an exercise for you to do.

If you try to “guess” by taking the average of and (i.e. using ), then the resulting method is just trapezoidal integration.

Great post and explanation of numerical integration. I attempted something similar using a Shiny application. I got a similar result using the input function as dbeta(x,2,5). Check it out if you’re interested: http://spark.rstudio.com/beckmw/shiny_integrate/