# AD Compatible Numerical Differentiation & Integration with Discrete Data

**URL:** https://discourse.julialang.org/t/ad-compatible-numerical-differentiation-integration-with-discrete-data/92068
**Category:** General Usage
**Created:** [December 24, 2022, 12:02am UTC](https://discourse.julialang.org/t/ad-compatible-numerical-differentiation-integration-with-discrete-data/92068 "2022-12-24T00:02:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ejmeitz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejmeitz/32/45283_2.png) [@ejmeitz](https://discourse.julialang.org/u/ejmeitz)
#### Post date: [December 24, 2022, 12:02am UTC](https://discourse.julialang.org/t/ad-compatible-numerical-differentiation-integration-with-discrete-data/92068/1 "2022-12-24T00:02:14Z")

</div>

Hello,

I’m trying to calculate some numerical derivatives & integrals from _discrete_ data inside of a loss function. This loss function must be compatible with auto-differentiation to find the minimum. I’ve tried the NumericalIntegration.jl package and it does not work. There is a rather long stack trace but I believe it is because I am mutating or calling setindex! inside of the loss function to calculate the integral. So I either need a way to do cumtrapz and central difference on discrete data in Julia or a package that has this already. I spent awhile looking and was kind of surprised that most of the packages only work on functions of x and not discrete data. Any help would be appreciated.

Thanks

---

<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: [December 24, 2022, 1:22am UTC](https://discourse.julialang.org/t/ad-compatible-numerical-differentiation-integration-with-discrete-data/92068/2 "2022-12-24T01:22:19Z")

</div>

> [@ejmeitz](#):
>
> I’m trying to calculate some numerical derivatives & integrals from _discrete_ data inside of a loss function.

Numerical integration and differentiation of discrete data implicitly constructs an interpolating function from your data, so why not just do that explicitly? Depending on what your data looks like, do some kind of fit or spline or other interpolation, and then integrate/differentiate your interpolation/fit.

Of course if you just want trapezoidal-rule integration, it’s a trivial function to write yourself in an AD-compatible way.

> [@ejmeitz](#):
>
> I spent awhile looking and was kind of surprised that most of the packages only work on functions of x and not discrete data. Any help would be appreciated.

Integration from a fixed set of data points is a _completely_ different problem from integrating functions that can be evaluated from arbitrary points. The latter is generally _much_ more efficient (especially if you have equally spaced points, which are a terrible choice for smooth functions). That’s why when you search for “numerical integration” the first things that pop up are packages that require you to specify a function (so that the algorithm can choose the evaluation points).

That being said, there are several packages for:

- integrating discrete data from given points: [NumericalIntegration.jl](https://github.com/dextorious/NumericalIntegration.jl), [Trapz.jl](https://github.com/francescoalemanno/Trapz.jl), [Romberg.jl](https://github.com/fgasdia/Romberg.jl), …
- constructing continuous interpolations from discrete data (which you can then integrate, differentiate, etcetetera): [Interpolations.jl](https://github.com/JuliaMath/Interpolations.jl), [Dierckx.jl](https://github.com/kbarbary/Dierckx.jl), [BasicBSplines.jl](https://github.com/hyrodium/BasicBSpline.jl), [BaryRational.jl](https://github.com/macd/BaryRational.jl), [Surrogates.jl](https://github.com/SciML/Surrogates.jl), [FastChebInterp.jl](https://github.com/stevengj/FastChebInterp.jl), and many others …

Where does your discrete data come from? Can you choose the evaluation points, or are they necessarily equally spaced? Is your function smooth?
