1
$\begingroup$

I have a 1D domain (let's say the interval $(0,1)$) on which I randomly sample $N$ points from the uniform distribution. I have a function $f\colon (0,1) \to \mathbb{R}$ which is integrable.

What is the best way to numerically integrate $f$ based on these grid points, given that I want $N$ to be as low as possible?

My $f$ comes from a neural network and to evaluate the loss I need to perform the integral. So I want to keep $N$ as low as possible (eg. 1000).

I've read about the Monte Carlo integral and the trapezoidal rule. Is there a rule of thumb for which is better given that my grid points are uniformly distributed and are there better ways?

I don't know if there is a better way of taking grid points (than uniformly distributed) -- but I'd like to get them randomly because it helps train my model. But maybe there is a different better distribution of points to get in order to give a more accurate integral. Also would your suggestions change if I had a 2d domain like the unit square instead of the unit integral? From what I read, MC is not good for low dimensional problems (but it's very easy to implement in Pytorch). I am aware that there is a methodology to check how many grid points one needs for a good MC integral (which involves finding the std etc), but this seems too messy for my setting.

$\endgroup$
6
  • $\begingroup$ Why do you want to take the integral? What problem are you trying to solve by doing it? $\endgroup$
    – Tim
    Commented Jul 28, 2023 at 9:55
  • $\begingroup$ It is an $L^1$ function (possibly continuous) that is related to a PDE weak solution. I want to take the integral to compute how far away it is from satisfying the PDE. So it enters into my loss function... But I guess this info may be not what you are looking for exactly $\endgroup$
    – math_guy
    Commented Jul 28, 2023 at 9:57
  • $\begingroup$ Check out Sobol' sequences: en.wikipedia.org/wiki/Sobol_sequence. $\endgroup$
    – jbowman
    Commented Jul 28, 2023 at 15:02
  • 1
    $\begingroup$ Without assumptions on the smoothness of $f$ you have no control over the error rate or any way to estimate it, so all bets are off. When $f$ is differentiable (of whatever degree), classical methods are available and readily implemented, including Riemann sums, the Trapezoidal Rule, Simpson's Rule, and its generalizations: consult a Calculus text. Better modern methods adapt their samples to the behavior of $f$, focusing them where $f$ appears to be varying the most. In other words, random sampling will not succeed in making $N$ as low as possible. Also, what do you mean by "grid" points? $\endgroup$
    – whuber
    Commented Jul 28, 2023 at 18:05
  • $\begingroup$ @whuber By grid points, I just mean the points on the domain on which I have access to function values. $\endgroup$
    – math_guy
    Commented Jul 29, 2023 at 8:31

1 Answer 1

1
$\begingroup$

[ Still under editing]

There is no definitive rule that tells us what numeric integration method is best for all functions $f$, it really depends on the function. As far as I understand, you want to do it by Monte Carlo integration, which is fine. You can do definitely better than sampling independent points over the domain, as long the dimension is low.

Simple stratified sampling

As a start, have a look stratified sampling, a simple yet very effective way to randomly sample points, and still improve the precision of your Monte Carlo integral.

To this end, partition the integration domain into $N$ equally sized intervals / squares/ cubes, and sample one point uniformly in each interval. This way, clusters and holes in the point pattern of sampling points are avoided, which yields much more precise results than uniform sampling.

In practice, if you want to integrate over an interval $[a, b]$, say, you would

  • generate $N$ uniform random numbers (between 0 and 1) $U_1,\dots,U_N$
  • calculate the grid points for integration as $$ X_i = a + (i - 1 + U_i)/N \cdot(b-a),\quad i=1,\dots, N $$
  • and as usual get the Monte Carlo estimate for your integral by multiplying the average of $f(X_i)$ with the width $(b-a)$ of the intergration interval.

The method readily extends to higher dimensions. It is worth while pursuing for dimensions up to three, maybe four, but for higher dimensions the gain in precision compared to independent sampling decays rapidly.

.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.