# Relative tolerances for near-zero integrals

**URL:** <https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141>\
**Category:** Numerics\
**Created:** [June 10, 2020, 4:21pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141 "2020-06-10T16:21:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [June 10, 2020, 4:21pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/1 "2020-06-10T16:21:05Z")

</div>

I have a question somewhat unrelated to the previous discussion.

What is a good way to specify relative tolerance in the case where the exact value of the integral is close to zero?

I’ve looked at QuadGK internals and there the relative error is calculated by dividing sum of errors over the sum of integrals over segments. In the case of almost zero-valued integral, I could think about calculating the sum of absolute values of integrals over segments and then divide total error by this new sum.

What do you think of such an approach?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 10, 2020, 4:30pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/2 "2020-06-10T16:30:46Z")

</div>

> [@Gregstrq](#):
>
> What is a good way to specify relative tolerance in the case where the exact value of the integral is close to zero?

In this case you want an absolute tolerance (via the `atol` keyword), perhaps in addition to a relative tolerance.

(In general, you should open a new topic rather than posting unrelated questions to old topics.)

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [June 10, 2020, 4:53pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/3 "2020-06-10T16:53:20Z")

</div>

Thank you for the splitting and sorry for the overusing the old topic.

It is good to have as few parameters as possible. So I could rephrase my question as: Is there a good heuristic to estimate desired absolute tolerance if relative tolerance is specified for near-zero integral?

The approach that I proposed, in some sense, estimates L1 norm of the function and uses it to estimate desired absolute tolerance.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 10, 2020, 6:07pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/4 "2020-06-10T18:07:30Z")

</div>

> [@Gregstrq](#):
>
> Is there a good heuristic to estimate desired absolute tolerance if relative tolerance is specified for near-zero integral?

I would estimate it as a relative tolerance multiplied by the integral of the absolute value of the integrand (maybe estimated to low accuracy, e.g. by a fixed-order rule), or a relative tolerance multiplied by the length of the integration interval multiplied by a typical magnitude of the integrand. (That is, you need a scale according to which you determine what is a “small” error.)

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [June 10, 2020, 6:24pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/5 "2020-06-10T18:24:03Z")

</div>

> [@stevengj](#):
>
> I would estimate it as a relative tolerance multiplied by the integral of the absolute value of the integrand (maybe estimated to low accuracy, e.g. by a fixed-order rule), or a relative tolerance multiplied by the length of the integration interval multiplied by a typical magnitude of the integrand.

Agreed.  
But, if it is a natural way, why it is not used in QuadGK, for example, by default?

After line 11 here:

> <https://github.com/JuliaMath/QuadGK.jl/blob/514db3640b1dea68e431a682d52fe6f077469802/src/adapt.jl#L11>

One could insert

```julia
I′ = sum(s -> nrm(s.I), segs)

```

And then use this `I′` instead of `I` in line 24, where the relative tolerance check is performed.

I do not insist on doing it. Rather I am curious as to why it may be a bad idea? Or may be not…

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 10, 2020, 9:45pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/6 "2020-06-10T21:45:08Z")

</div>

> [@Gregstrq](#):
>
> But, if it is a natural way, why it is not used in QuadGK, for example, by default?

Because integrating |f(x)| or similar is expensive just to compute an absolute error tolerance that is rarely needed.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 10, 2020, 9:47pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/7 "2020-06-10T21:47:16Z")

</div>

> [@Gregstrq](#):
>
> ```julia
> I′ = sum(s -> nrm(s.I), segs)
> 
> ```
> 
> And then use this `I′` instead of `I` in line 24, where the relative tolerance check is performed.

This is not the same thing as integrating |f(x)|, because it only sums the absolute values of the subsegment integrals, and I’m not sure how reliable it would be as a way to estimate an `atol`.

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [June 10, 2020, 10:08pm UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/8 "2020-06-10T22:08:13Z")

</div>

Actually it is not that bad. We can say exactly that `I′ > |I|`. At the same time `I′` gives us a lower bound on the integral of |f(x)|. And the more segments we have, the more accurate this bound is: the difference with the integral of |f(x)| comes from the segments where the function changes its sign. The more segments we have, the smaller the segments are =\> the difference becomes less significant.

In the case where the integral has not a near-zero value, there should be no significant difference between the usage of `I′` and `|I|` for the relative tolerance check. It is because the integrals of f(x) and of |f(x)| should be of the same order in this case.

In the situation where we encounter the integral with a near-zero value, |I′| should quickly become a good approximation of the integral of |f(x)| with the growth of the number of segments.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 11, 2020, 1:22am UTC](https://discourse.julialang.org/t/relative-tolerances-for-near-zero-integrals/41141/9 "2020-06-11T01:22:25Z")

</div>

That’s true. It might be reasonable to use as a default
