AD Compatible Numerical Differentiation & Integration with Discrete Data


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.


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.

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:

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

1 Like