ta-statistics
A no_std, high-performance Rust library for computing rolling statistical measures on time series data, specifically designed for statistical analysis in backtesting and live trading systems.
Features
- no_std compatible: Can be used in resource-constrained environments without the standard library
- zero-runtime-allocation design: Optimized for backtesting and algorithmic trading systems with minimal runtime overhead
- Generic numeric support: Works with any float type via the
num-traitsinterface - Rolling window computations: Efficiently calculates statistics over fixed-size windows
- Comprehensive statistical measures: Over 25 different statistical functions for both single and paired time series
Single Time Series Statistics
| Category | Functions |
|---|---|
| Basic Statistics | Sum, Mean, Mode, Median, Min, Max |
| Dispersion & Volatility | Variance, Standard Deviation, Mean Absolute Deviation, Median Absolute Deviation, IQR |
| Distribution Analysis | Z-Score, Skewness, Kurtosis, Quantile |
| Regression & Trend | Linear Regression (Slope/Intercept/Angle), Linear Fit |
| Trading-Specific | Drawdown, Maximum Drawdown, Percent Change, Log Return, Rolling Diff |
Paired Time Series Statistics
| Category | Functions |
|---|---|
| Relationship Metrics | Covariance, Correlation, Beta |
| Auxiliary Calculations | Mean Product, Mean of Squares |
Installation
Or add this to your Cargo.toml:
[]
= "*"
Replace * with the latest version number.
Quick Start
For single statistics (like mean):
use SingleStatistics;
let mut stats = new;
stats.next.mean;
For paired statistics (like correlation):
use PairedStatistics;
let mut stats = new;
stats.next.corr;
Use Cases
- Technical Indicators: Build standard and custom technical indicators based on statistical metrics
- Alpha Generation: Create statistical arbitrage models using correlation and covariance
- Risk Management: Monitor drawdowns, volatility, and beta for position sizing and risk control
- Performance Analysis: Calculate return statistics and risk-adjusted metrics for strategy evaluation
- Market Regime Detection: Use distributional statistics like skewness and kurtosis to identify market regimes
Performance Considerations
- Memory usage is proportional to the window size
- Delta Degrees of Freedom correction can be applied with
set_ddof(true)for sample statistics - Uses KahanBabuskaNeumaier algorithm for compensated summation to prevent catastrophic cancellation in floating-point operations, ensuring numerical stability in rolling calculations
Example: Real-time Volatility Analysis
use SingleStatistics;
/// Calculate normalized volatility and detect regime changes in live market data
Example: Correlation-Based Pair Monitoring
use PairedStatistics;
/// Monitor correlation stability between two instruments in real-time
/// and detect statistically significant relationship breakdowns
📚 Documentation
For complete API documentation, examples, and explanations, visit: https://docs.rs/ta-statistics
License
This project is licensed under the MIT License - see the license file for details.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the license, shall be licensed as above, without any additional terms or conditions.
Disclaimer
This software is for informational purposes only. It is not intended as trading or investment advice.