4 个版本 (破坏性)
0.4.0 | 2023 年 9 月 2 日 |
---|---|
0.3.0 | 2023 年 9 月 2 日 |
0.2.0 | 2023 年 9 月 2 日 |
0.1.0 | 2023 年 9 月 1 日 |
#279 在 金融
52KB
755 行
时间延迟 — 创建时间延迟时间序列数据
此软件包提供 lag_matrix
和相关函数,用于创建类似于 MATLAB 的 lagmatrix
的时间序列分析时间延迟版本。
通过 ndarray
软件包功能,支持 ndarray 的 Array1
和 Array2
特性。
示例
对于单时间序列
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();
// The function is also available via the CreateLagMatrix.
// All methods take an IntoIterator<Item = usize> for the lags.
let other = data.lag_matrix([0, 1, 2, 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
]
);
assert_eq!(lagged, other);
}
对于行包含时间序列的矩阵
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), 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,
]
);
}
对于列包含时间序列的矩阵
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), 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
]
);
}
依赖项
~0–285KB