Code Monkey home page Code Monkey logo

simpleaccumulator's Introduction

SimpleAccumulator

Crates.io docs.rs Crates.io

This crate is inspired by Boost::Accumulator which supports incremental statistical computation (online algorithms). This is a work in progress but usable. Please write integration tests before using it in production.

Read Documentation

Notes

  • 2023-12-20: Version 0.6 is a major rewrite that fix many embarassing bugs. In 0.6+, we are relying on watermill crate for underlying algorithms.

Usage:

use simple_accumulator::SimpleAccumulator;

fn main() {
    let k = [1, 2, 3, 4];

    // If second argument is `None` then accumulator stores all the data. 
    let mut x = SimpleAccumulator::new(&k, Some(10));

    println!("{:?}", x);
    
    x.push(5);
    println!("{:?}", x);

    print!("{}", x.mean());
    print!("{}", x.median());
    print!("{}", x.variance());
    print!("{}", x.sum());
    print!("{}", x.kurtosis());
    ...
}

simpleaccumulator's People

Contributors

deepsourcebot avatar dilawar avatar purnatag avatar sn99 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

purnatag sn99

simpleaccumulator's Issues

Hide inner fields when Serializing

Currently, all fields are being serialized.

    risk_scores: SimpleAccumulator {
        vec: [
            0.85,
            0.85,
            0.75,
        ],
        stats: [],
        r_mean: 0.8166666666666667,
        r_variance: NaN,
        r_total: 3,
        mean: 0.0,
        variance: 0.0,
        min: 0.0,
        min_: 0.0,
        max: 0.0,
        max_: 0.0,
        median: 0.0,
        len: 3,
        capacity: 4,
        fixed_capacity: false,
        last_write_position: 0,
        accumulate: false,
        skewness: 0.0,
        kurtosis: 0.0,
        bimodality: 0.0,
    },

Hide all fields that are not related to data/statistics e.g. capacity, last_write_position, accumulate, fixed_capacity, r_* etc.

Variance is always zero in `default()`

Using version 0.5.0. Created simple accumulator using default() and pushed element. I was expecting statistics to be updated automatically.

 SimpleAccumulator {
    vec: [
        32.0,
        24.0,
        17.0,
        32.0,
        19.0,
        29.0,
        23.0,
        23.0,
        16.0,
        31.0,
        23.0,
        11.0,
        24.0,
        35.0,
        12.0,
        37.0,
        24.0,
        13.0,
        32.0,
        17.0,
        25.0,
        18.0,
        34.0,
        16.0,
        32.0,
        23.0,
        25.0,
        15.0,
        24.0,
        22.0,
        35.0,
        13.0,
        20.0,
        37.0,
        17.0,
        29.0,
        24.0,
        23.0,
        13.0,
        22.0,
        38.0,
        15.0,
        22.0,
        31.0,
        16.0,
        24.0,
        34.0,
        21.0,
        12.0,
        24.0,
        22.0,
        24.0,
        36.0,
        23.0,
        24.0,
        17.0,
        24.0,
        29.0,
        17.0,
        30.0,
        24.0,
        15.0,
        32.0,
        24.0,
        22.0,
        24.0,
        23.0,
        19.0,
        19.0,
        24.0,
        36.0,
        22.0,
        25.0,
        18.0,
        16.0,
        37.0,
        10.0,
        26.0,
        33.0,
        24.0,
        18.0,
        30.0,
        16.0,
        32.0,
        23.0,
        23.0,
        18.0,
        19.0,
        33.0,
        17.0,
        25.0,
        22.0,
        17.0,
        29.0,
        25.0,
        15.0,
        25.0,
        33.0,
        18.0,
    ],
    stats: [],
    r_mean: 23.585858585858585,
    r_variance: NaN,
    r_total: 99,
    mean: 0.0,
    variance: 0.0,
    min: 0.0,
    min_: 0.0,
    max: 0.0,
    max_: 0.0,
    median: 0.0,
    len: 99,
    capacity: 128,
    fixed_capacity: false,
    last_write_position: 0,
    accumulate: false,
    skewness: 0.0,
    kurtosis: 0.0,
    bimodality: 0.0,
}

When new or with_fixed_capacity like let mut acc = SimpleAccumulator::with_fixed_capacity::<f64>(&[], 10, true); then behaviour is as expected.

SimpleAccumulator {
    vec: [
        31.0,
        22.0,
        24.0,
        22.0,
        15.0,
        28.0,
        20.0,
        34.0,
        9.0,
        17.0,
    ],
    stats: [
        22.2,
        511.6,
        -299.0399999999979,
        60328.43199999999,
    ],
    r_mean: 23.454545454545453,
    r_variance: NaN,
    r_total: 99,
    mean: 22.2,
    variance: 51.160000000000004,
    min: 9.0,
    min_: 15.0,
    max: 34.0,
    max_: 31.0,
    median: 22.0,
    len: 10,
    capacity: 10,
    fixed_capacity: true,
    last_write_position: 8,
    accumulate: true,
    skewness: -0.08172096386692998,
    kurtosis: 2.3049471189437107,
    bimodality: 0.0028973835800625873,
}

Requires hotfix and we need to yank all affected versions.

Bug in Accumulator with fixed capacity

Consider this case where I make an accumulator of fixed size 3.

#[test]
fn test_sanity() {
    const CAPACITY : usize = 3;
    let mut acc = SimpleAccumulator::with_fixed_capacity::<f64>(&vec![], CAPACITY, true);

    let data = vec![0.0, 1.1, 2.2, 3.3, 4.4];
    for &v in &data {
        acc.push(v);
    }
    println!("{:?}", acc);
    assert_eq!(acc.vec.len(), 3);
    assert_eq!(acc.vec, vec![3.3,4.4,2.2]);
}
SimpleAccumulator { vec: [0.0, 3.3, 4.4], mean: 2.566666666666667, population_variance: 3.495555555555556, min: 0.0, min_: 0.0, max: 4.4, max_: 1.1, median: 2.3833333333333337, len: 3, capacity: 3, fixed_capacity: true, last_write_position: 2, accumulate: true }
thread 'test_sanity' panicked at 'assertion failed: `(left == right)`
  left: `[0.0, 3.3, 4.4]`,
 right: `[3.3, 4.4, 2.2]`', tests\test_accumulator_fixed.rs:16:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test test_sanity ... FAILED```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.