#online #stream #stats #generic #statistics #stream-processing #serializable

online-statistics

极快、泛型和可序列化的在线统计

8个版本

0.2.6 2022年9月10日
0.2.5 2022年9月9日
0.2.4 2022年8月27日
0.1.0 2022年7月23日

#1085 in 数学

Download history 29/week @ 2024-03-11 9/week @ 2024-03-18 26/week @ 2024-04-01 8/week @ 2024-04-08 13/week @ 2024-04-15 14/week @ 2024-04-22 15/week @ 2024-05-13 8/week @ 2024-05-20 4/week @ 2024-05-27 15/week @ 2024-06-03 15/week @ 2024-06-10 14/week @ 2024-06-17 27/week @ 2024-06-24

71 每月下载量

MIT 许可证

74KB
1.5K SLoC

Rust在线统计 🦀

online-statistics 是一个用于极快、泛型和可序列化在线统计的Crate 🦀。

快速入门


让我们计算在线中位数并将其序列化

use online_statistics::quantile::Quantile;
use online_statistics::stats::Univariate;
let data: Vec<f64> = vec![9., 7., 3., 2., 6., 1., 8., 5., 4.];
let mut running_median: Quantile<f64> = Quantile::new(0.5_f64).unwrap();
for x in data.into_iter() {
    running_median.update(x); // update the current statistics
    println!("The actual median value is: {}", running_median.get());
}
assert_eq!(running_median.get(), 5.0);

// Convert the statistic to a JSON string.
let serialized = serde_json::to_string(&running_median).unwrap();

// Convert the JSON string back to a statistic.
let deserialized: Quantile<f64> = serde_json::from_str(&serialized).unwrap();

现在让我们使用迭代器计算在线总和

use online_statistics::iter::IterStatisticsExtend;
let data: Vec<f64> = vec![1., 2., 3.];
let vec_true: Vec<f64> = vec![1., 3., 6.];
for (d, t) in data.into_iter().online_sum().zip(vec_true.into_iter()) {
    assert_eq!(d, t); //       ^^^^^^^^^^
}

您还可以计算滚动统计;以下示例中,让我们计算前两个数据的滚动总和


use online_statistics::rolling::Rolling;
use online_statistics::stats::Univariate;
use online_statistics::variance::Variance;
let data: Vec<f64> = vec![9., 7., 3., 2., 6., 1., 8., 5., 4.];
let mut running_var: Variance<f64> = Variance::default();
// We wrap `running_var` inside the `Rolling` struct.
let mut rolling_var: Rolling<f64> = Rolling::new(&mut running_var, 2).unwrap();
for x in data.into_iter() {
    rolling_var.update(x);
}
assert_eq!(rolling_var.get(), 0.5);

安装


将以下行添加到您的 cargo.toml

[dependencies]
online-statistics = "0.2.6"

可用的统计

统计 可滚动?
平均值
方差
总和
最小值
最大值
计数
分位数
峰值到峰值
指数加权平均值
指数加权方差
四分位数间距
峰度
偏度
协方差

灵感来源


Python中的river库的stats模块极大地启发了这个Crate。

依赖关系

~1.4–2.2MB
~48K SLoC