Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sunsided/timelag-rs

Creating time lagged time series data
https://github.com/sunsided/timelag-rs

forecasting forecasting-time-series rust signal-processing time-series

Last synced: 2 days ago
JSON representation

Creating time lagged time series data

Awesome Lists containing this project

README

        

# timelag — creating time-lagged time series data

[![Crates.io](https://img.shields.io/crates/v/timelag)](https://crates.io/crates/timelag)
[![Crates.io](https://img.shields.io/crates/l/timelag)](https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/sunsided/timelag-rs/rust.yml)](https://github.com/sunsided/timelag-rs/actions/workflows/rust.yml)
[![docs.rs](https://img.shields.io/docsrs/timelag)](https://docs.rs/timelag/)
[![Safety Dance][safety-image]][safety-link]
[![codecov](https://codecov.io/gh/sunsided/timelag-rs/graph/badge.svg?token=8LB5R8EQUP)](https://codecov.io/gh/sunsided/timelag-rs)

---

This crate provides the `lag_matrix` and related functions to create time-lagged versions of time series similar
to MATLAB's [`lagmatrix`](https://mathworks.com/help/econ/lagmatrix.html) for time series analysis.

Support for [ndarray](https://crates.io/crates/ndarray)'s `Array1` and `Array2` traits is available via the
`ndarray` crate feature.

## Examples

For singular time series:

```rust
use timelag::lag_matrix;

fn singular_series() {
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 = lag_matrix(&data, 0..=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
]
);

// The function is also available via the CreateLagMatrix trait.
// All methods take an IntoIterator for the lags.
// The stride can be defaulted to `0` to produce tightly-packed consecutive values.
let lagged = data.lag_matrix([0, 1, 2, 3], lag, 0).unwrap();
assert_eq!(
lagged,
&[
1.0, 2.0, 3.0, 4.0,
lag, 1.0, 2.0, 3.0,
lag, lag, 1.0, 2.0,
lag, lag, lag, 1.0,
]
);

assert_eq!(lagged, other);
}
```

For matrices with time series along their rows:

```rust
use timelag::{lag_matrix_2d, MatrixLayout};

fn matrix_rows() {
let data = [
1.0, 2.0, 3.0, 4.0,
-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;

let lagged = lag_matrix_2d(&data, MatrixLayout::RowWise(4), 0..=3, lag, 5).unwrap();

assert_eq!(
lagged,
&[
1.0, 2.0, 3.0, 4.0, padding, // original data
-1.0, -2.0, -3.0, -4.0, padding,
lag, 1.0, 2.0, 3.0, padding, // first lag
lag, -1.0, -2.0, -3.0, padding,
lag, lag, 1.0, 2.0, padding, // second lag
lag, lag, -1.0, -2.0, padding,
lag, lag, lag, 1.0, padding, // third lag
lag, lag, lag, -1.0, padding,
]
);
}
```

For matrices with time series along their columns:

```rust
use timelag::{lag_matrix_2d, MatrixLayout};

fn matrix_columns() {
let data = [
1.0, -1.0,
2.0, -2.0,
3.0, -3.0,
4.0, -4.0
];

// Using infinity for padding because NaN doesn't equal itself.
let lag = f64::INFINITY;
let padding = f64::INFINITY;

// Example row stride of nine: 2 time series × (1 original + 3 lags) + 1 extra padding.
let lagged = lag_matrix_2d(&data, MatrixLayout::ColumnWise(4), 0..=3, lag, 9).unwrap();

assert_eq!(
lagged,
&[
// original
// |-----| first lag
// | | |-----| second lag
// | | | | |-----| third lag
// | | | | | | |-----|
// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
1.0, -1.0, lag, lag, lag, lag, lag, lag, padding,
2.0, -2.0, 1.0, -1.0, lag, lag, lag, lag, padding,
3.0, -3.0, 2.0, -2.0, 1.0, -1.0, lag, lag, padding,
4.0, -4.0, 3.0, -3.0, 2.0, -2.0, 1.0, -1.0, padding
]
);
}
```

[safety-image]: https://img.shields.io/badge/unsafe-opt_in-success.svg

[safety-link]: https://github.com/rust-secure-code/safety-dance/