Sling Academy
Home/Rust/Calculating Rolling Statistics Over Arrays and Vectors in Rust

Calculating Rolling Statistics Over Arrays and Vectors in Rust

Last updated: January 03, 2025

When working with time series data or any sequential dataset, calculating rolling statistics is a common requirement. Rust, with its emphasis on memory safety and performance, is an excellent choice for implementing efficient algorithms for rolling statistics over arrays and vectors. In this article, we'll explore how to compute rolling averages, sums, and other statistics using Rust.

Understanding Rolling Statistics

Rolling statistics involve calculating statistical measures over a sliding window of a fixed size. For example, if you have an array of numerical data, a rolling average is computed by taking the average of a specified number of consecutive elements, shifting the window by one element each time.

Implementing Rolling Sum in Rust

Let's start by implementing a function to calculate a rolling sum. We'll use vectors in Rust to store our data.


fn rolling_sum(data: &[i32], window_size: usize) -> Vec {
    if window_size == 0 || window_size > data.len() {
        return vec![];
    }
    let mut result = Vec::new();
    let mut window_sum: i32 = data.iter().take(window_size).sum();
    result.push(window_sum);

    for i in window_size..data.len() {
        window_sum += data[i] - data[i - window_size];
        result.push(window_sum);
    }

    result
}

fn main() {
    let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
    let window_size = 3;
    let sums = rolling_sum(&data, window_size);
    println!("Rolling sums: {:?}", sums);
}

The rolling_sum function initializes a sum for the first window, then iteratively updates this sum by adding the next element and subtracting the element that is no longer in the window. This provides an efficient O(n) computation.

Calculating Rolling Averages

Once you have a rolling sum, computing a rolling average is straightforward, as it is simply the sum divided by the window size. Let's implement a function to calculate rolling averages:


fn rolling_average(data: &[i32], window_size: usize) -> Vec {
    let sums = rolling_sum(data, window_size);
    sums.iter().map(|&sum| sum as f64 / window_size as f64).collect()
}

fn main() {
    let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
    let window_size = 3;
    let averages = rolling_average(&data, window_size);
    println!("Rolling averages: {:?}", averages);
}

By leveraging our rolling_sum function, the rolling_average function becomes a simple transformation.

Attribution Considerations with Rolling Measures

One should be aware of edge cases such as when the window size is zero or greater than the data length. Our implementation defaults to returning an empty vector in such cases, indicating that the operation could not be performed.

Advanced Rolling Statistics

Beyond basic sums and averages, other rolling statistics like rolling variance, standard deviation, or even complex custom metrics can be implemented using similar techniques. Here's a basic framework for rolling variance:


fn rolling_variance(data: &[i32], window_size: usize) -> Vec {
    if window_size == 0 || window_size > data.len() {
        return vec![];
    }
    let mut variances = Vec::new();
    let mut sum = 0;
    let mut sum_of_squares = 0;

    for i in 0..window_size {
        sum += data[i];
        sum_of_squares += data[i] * data[i];
    }

    variances.push(variance(window_size, sum, sum_of_squares));

    for i in window_size..data.len() {
        sum += data[i] - data[i - window_size];
        sum_of_squares += data[i] * data[i] - data[i - window_size] * data[i - window_size];
        variances.push(variance(window_size, sum, sum_of_squares));
    }

    variances
}

fn variance(count: usize, sum: i32, sum_of_squares: i32) -> f64 {
    let n = count as f64;
    (sum_of_squares as f64 - (sum as f64 * sum as f64) / n) / n
}

This code computes the rolling variance by separately maintaining the sum and the sum of squares within each window, allowing for a numerically stable calculation.

Conclusion

Rust makes it practical to implement efficient, safe calculations for rolling statistics over arrays and vectors. With these foundational techniques, more complex analyses can also be performed, taking full advantage of Rust's performance benefits. Whether you're working with financial time series, experimental data, or any application where rolling calculations provide insight, these implementations can serve as a fundamental building block. Happy coding!

Next Article: Implementing Fast Matrix Multiplication Algorithms in Rust

Previous Article: Computing Correlation and Covariance in Rust

Series: Math and Numbers in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior