pub fn functional_difference(data: &FdMatrix) -> Result<FdMatrix, FdarError>Expand description
Functional first-difference operator.
Computes the first-order difference of a time-ordered curve series, mirroring
ftsa::diff.fts with lag = 1. For a series of N curves on an m-point grid,
the output is an (N-1) × m matrix where:
D[i, j] = data[(i+1, j)] - data[(i, j)] for i in 0..=(N-2), j in 0..=(m-1)§Round-trip tolerance
The original series is recoverable from D and the first curve via a running
cumulative sum:
reconstructed[0, j] = data[(0, j)]
reconstructed[i, j] = reconstructed[i-1, j] + D[i-1, j] for i >= 1This round-trips within machine precision (|reconstructed[i,j] - data[i,j]| < 1e-10 for typical f64 inputs). The tolerance is tight because first-differencing and cumulative-summation are exact inverse operations in floating-point arithmetic (no approximation is involved, only floating-point rounding).
§Higher-order differencing
Only order-1 (lag-1) differencing is provided. Higher-order or lag-d
differencing can be achieved by applying functional_difference repeatedly:
functional_difference(&functional_difference(&data)?)?. Convenience wrappers
for order and lag parameters are a deferred extension.
§Arguments
data— Time-ordered functional observations (N × m, column-major). Rows are curves ordered from earliest to latest.
§Errors
FdarError::InvalidDimension—datahas fewer than 2 rows (N < 2). Differencing a single curve or empty matrix is undefined.
§Examples
use fdars_core::{FdMatrix, functional_difference};
// Three curves on a 5-point grid.
let mut data = FdMatrix::zeros(3, 5);
for i in 0..3 {
for j in 0..5 {
data[(i, j)] = (i as f64) * (j as f64 + 1.0);
}
}
let diff = functional_difference(&data).unwrap();
assert_eq!(diff.shape(), (2, 5)); // N-1 rows