Trait timelag::LagMatrixFromArray
source · pub trait LagMatrixFromArray<A>where
A: Copy,{
// Required method
fn lag_matrix(
&self,
lags: usize,
fill: A,
stride: usize
) -> Result<Array2<A>, LagError>;
}ndarray only.Expand description
Provides the lag_matrix function for Array1 and Array2 types.
Required Methods§
sourcefn lag_matrix(
&self,
lags: usize,
fill: A,
stride: usize
) -> Result<Array2<A>, LagError>
fn lag_matrix( &self, lags: usize, fill: A, stride: usize ) -> Result<Array2<A>, LagError>
Create a time-lagged matrix of time series values.
This function creates lagged copies of the provided data and pads them with a placeholder value. The source data is interpreted as increasing time steps with every subsequent element; as a result, earlier (lower index) elements of the source array will be retained while later (higher index) elements will be dropped with each lag. Lagged versions are prepended with the placeholder.
Arguments
lags- The number of lagged versions to create.fill- The value to use to fill in lagged gaps.stride- The number of elements between lagged versions in the resulting vector. If set to0ordata.len(), no padding is introduced. Values larger thandata.len()creates padding entries set to thefillvalue.
Returns
A vector containing lagged copies of the original data, or an error.
For N datapoints and M lags, the result can be interpreted as an M×N matrix with
lagged versions along the rows. With strides S >= N, the resulting matrix is of shape M×S
with an M×N submatrix at 0×0 and padding to the right.
Example
use timelag::prelude::*;
let data = [1.0, 2.0, 3.0, 4.0];
// Using infinity for padding because NaN doesn't equal itself.
let lag = f64::INFINITY;
let padding = f64::INFINITY;
// Create three lagged versions.
// Use a stride of 5 for the rows, i.e. pad with one extra entry.
let lagged = data.lag_matrix(3, lag, 5).unwrap();
assert_eq!(
lagged,
&[
1.0, 2.0, 3.0, 4.0, padding, // original data
lag, 1.0, 2.0, 3.0, padding, // first lag
lag, lag, 1.0, 2.0, padding, // second lag
lag, lag, lag, 1.0, padding, // third lag
]
);