Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online algorithm for mean and std dev #2

Closed
genos opened this issue Jun 28, 2022 · 3 comments
Closed

Online algorithm for mean and std dev #2

genos opened this issue Jun 28, 2022 · 3 comments

Comments

@genos
Copy link
Contributor

genos commented Jun 28, 2022

First off, thank you for this library! I've been investigating various hyperparameter importance routines, and I wanted to delve deeper into fANOVA; having a Rust implementation to look through is really nice.

I do have one small suggestion: the functions functions::mean and functions::mean_and_stddev could be updated to use Welford's online algorithm, computing the mean and standard deviation in a single pass. This would also remove the need for Clone:

pub fn mean(xs: impl Iterator<Item = f64>) -> f64 {
    mean_and_stddev(xs).0
}

pub fn mean_and_stddev(xs: impl Iterator<Item = f64>) -> (f64, f64) {
    let (mut mean, mut s, mut n) = (0.0, 0.0, 0.0);
    for x in xs {
        n += 1.0;
        let delta = x - mean;
        mean += delta / n;
        s += delta * (x - mean);
    }
    assert!(n >= 2.0, "Need more than 2 values for standard deviation");
    (mean, (s / (n - 1.0)).sqrt())
}

This also uses Bessel's correction for unbiased sample standard deviation (n - 1 instead of n).

Feel free to ignore my suggestion, and thanks again for this library!

Edit: I made some errors in my code suggestions (copy-pasting from another project). Fixed now!

@sile
Copy link
Owner

sile commented Jun 28, 2022

Thank your for reaching out.
Your suggestion sounds nice to me. So, let me consider that deeper this or next weekend.
BTW, if you're interested in creating a PR to reflect the suggestion, it is, of course, very welcomed.

@genos
Copy link
Contributor Author

genos commented Jun 29, 2022

Thanks for your response! I've put up PR #3 to address/suggest what I put here, plus some other general housekeeping (update tests so they still pass, get cargo clippy to be quiet). Thanks again for your time and your great project.

@sile
Copy link
Owner

sile commented Jul 1, 2022

Addressed by #3

@sile sile closed this as completed Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants