1. Local methods (derivative.dlocal)
1.1. FiniteDifference
- derivative.dlocal.FiniteDifference.__init__(self, k, **kwargs)
Compute the symmetric numerical derivative of equally-spaced data using the Taylor series. Derivatives at the boundaries are computed with the available reduction of the window or first order finite difference.
Finite difference schemes are also available in numpy.gradient and scipy.misc.derivative.
- Parameters
k (int) – Interpolate the data with a polynomial through the 2k+1 points x[i-k], …, x[i], … x[i+k]
**kwargs – Optional keyword arguments.
- Keyword Arguments
periodic (bool) – If True, wrap the data. Assumes that x[-1 + 1] = x[0]. If False, truncate at edges. Default False.
1.2. SavitzkyGolay
- derivative.dlocal.SavitzkyGolay.__init__(self, order, left, right, **kwargs)
Compute the numerical derivative by first finding the best (least-squares) polynomial of order m < 2k+1 using the 2k+1 points in the neighborhood [t-left, t+right]. The derivative is computed from the coefficients of the polynomial. The default edge behavior is to truncate the window at the offending edge. The data does not need to be sampled at equal timesteps.
A simple symmetric version of the Savitzky-Galoy filter is available as scipy.signal.savgol_filter.
- Parameters
left (float) – Left edge of the window is t-left
right (float) – Right edge of the window is t+right
order (int) – Order of polynomial. Expects 0 <= order < points in window.
**kwargs – Optional keyword arguments.
- Keyword Arguments
iwindow (bool) – Whether to use an indexed window. If True, left and right act as indicies for t instead of as lengths in units of t. Default False.
periodic (bool) – If True, wrap the data. Assumes that x[-1 + 1] = x[0]. If False, truncate at edges. Default False.
period (float) – If periodic is true, set this as the period of the data. Default is t[-1]-t[0], which is likely an inacurate estimate.
- Raises
RankWarning – The fit may be poorly conditioned if order >= points in window.
2. Global methods (derivative.dglobal)
2.1. Spectral
- derivative.dglobal.Spectral.__init__(self, **kwargs)
Compute the numerical derivative by first computing the FFT. In Fourier space, derivatives are multiplication by i*phase; compute the IFFT after.
- Parameters
**kwargs – Optional keyword arguments.
- Keyword Arguments
filter – Optional. A function that takes in frequencies and outputs weights to scale the coefficient at the input frequency in Fourier space. Input frequencies are the discrete fourier transform sample frequencies associated with the domain variable. Look into python signal processing resources in scipy.signal for common filters.
2.2. Spline
- derivative.dglobal.Spline.__init__(self, s, **kwargs)
Compute the numerical derivative of data x using a spline. The Cubic spline is the default choice because it minimizes the curvature of the fit. Compute the derivative from the form of the fit Spline polynomials.
- Parameters
s (float) – Amount of smoothing
- Keyword Arguments
order (int) – Default is cubic spline (3). Supports 1 <= order <= 5.
periodic (bool) – Default False.
- Raises
TypeError – length of input > self.order must hold for spline interpolation.
2.3. TrendFiltered
- derivative.dglobal.TrendFiltered.__init__(self, order, alpha, **kwargs)
Compute the numerical derivative using Total Squared Variations,
\[\min_u \frac{1}{2} \|A u - (x-x_0)\|_2^2 + \alpha \|D^{\textrm{order}+1} u\|_1\]where A is the linear integral operator, and D is the linear derivative operator. The vector u finds a global derivative that is a piecewise function made up of polynomials of the provided order.
If order=0, this is equivalent to the total-variation derivative (c.f. R. Chartrand, [2]).
- Parameters
order (int) – Order of the inner LASSO derivative
alpha (float) – Regularization hyper-parameter for the LASSO problem.
**kwargs – Keyword arguments for the sklearn LASSO function.
- Raises
ValueError – Requires that the number of points n > order + 1 to compute the objective.
ConvergenceWarning – The Lasso optimization may fail to converge. Try increasing ‘max_iter’.
2.4. Kalman
- derivative.dglobal.Kalman.__init__(self, alpha=None)
Fit the derivative assuming that given data are noisy measurements
The Kalman smoother is the maximum likelihood estimator (MLE) for a process whose derivative obeys a Brownian motion. Equivalently, it is the MLE for a process whose measurement errors are drawn from standard normal distributions. Specifically, it minimizes the convex objective
\[\min_{x, \dot x} \|Hx-z\|_{R^{-1}}^2 + \alpha \|G(x, \dot x)\|_{Q^{-1}}^2\]In this implementation, the we have fixed H = R \(\equiv \mathbb{1}\) and let
\[\begin{split}Q \equiv \left[\begin{array}{cc}\Delta t & \Delta t^2/2 \\ \Delta t^2/2 & \Delta t^3/3\end{array}\right].\end{split}\]
- Parameters
alpha (float) – Ratio of measurement error variance to assumed process variance.
2.5. Kernel
- derivative.dglobal.Kernel.__init__(self, sigma: float = 1, lmbd: float = 0, kernel: str = 'gaussian')
Fit the derivative assuming a gaussian process specified by kernels
- Parameters
sigma – parameter of kernel function
lmbd – noise variance
kernel – name of kernel function
3. Utilities (derivative.utils)
- derivative.utils.deriv(n, dx, order)
Matrix derivative. Equi-spaced derivative via forward finite differences, mapping R^n into R^(n-order).
- Parameters
n (int) – Number of data points
dx (float) – Width of x[k+1] - x[k]
order (int) – Order of finite difference method to use.
- Returns
An n-by-(n-order) matrix derivative.
- Return type
(
ndarray
of float)- Raises
ValueError – Requires n >= 2 and n - order >= 1.
- derivative.utils.integ(n, dx=1)
Equi-spaced anti-derivative via the trapezoid rule mapping R^n to R^n.
- Parameters
n (int) – Number of data points
dx (float) – Width of x[k+1] - x[k]
- Returns
An n-by-n matrix integral.
- Return type
(
ndarray
of float)- Raises
ValueError – Requires n in positive integers.