### Interactive Streaming Example with Welford Source: https://context7.com/schneiderfelipe/welford/llms.txt An interactive example that reads numeric values from standard input and calculates running statistics (mean and variance) until non-numeric input is provided. ```rust use std::error::Error; use std::io::{stdin, stdout, Write}; use welford::Welford; fn main() -> Result<(), Box> { let mut w = Welford::::new(); loop { print!("Enter a number (non-numeric to quit): "); stdout().flush()?; let mut buf = String::new(); stdin().read_line(&mut buf)?; match buf.trim().parse::() { Ok(value) => w.push(value), Err(_) => break, } } println!("mean: {:?}", w.mean()); println!("variance: {:?}", w.var()); Ok(()) } ``` -------------------------------- ### Create Unweighted Welford Calculator Source: https://context7.com/schneiderfelipe/welford/llms.txt Use `Welford::new()` to create an instance for accumulating samples with uniform weight. The type `T` must implement numeric traits. No samples are stored, only running statistics. ```rust use welford::Welford; fn main() { let mut w = Welford::::new(); // Push individual samples w.push(2.0); w.push(4.0); w.push(4.0); w.push(4.0); w.push(5.0); w.push(5.0); w.push(7.0); w.push(9.0); println!("mean: {:?}", w.mean()); // Some(5.0) println!("variance: {:?}", w.var()); // Some(4.571428571428571) } ``` -------------------------------- ### Welford::new Source: https://context7.com/schneiderfelipe/welford/llms.txt Creates a new Welford instance for accumulating samples with uniform weight. The type parameter T must implement numeric traits from num-traits (e.g., f32, f64). No samples are stored; only running statistics are kept. ```APIDOC ## Welford::new ### Description Creates a new `Welford` instance for accumulating samples with uniform weight. The type parameter `T` must implement numeric traits from `num-traits` (e.g., `f32`, `f64`). No samples are stored; only running statistics are kept. ### Method Associated function (constructor) ### Parameters None ### Request Example ```rust use welford::Welford; let mut w = Welford::::new(); ``` ### Response - **Welford** - A new, empty Welford calculator instance. ``` -------------------------------- ### Merge Welford Statistics from Multiple Partitions Source: https://context7.com/schneiderfelipe/welford/llms.txt Shows how to merge statistics from two independent Welford calculators. This is useful for parallel processing, where partial results from different threads can be combined. ```rust use welford::Welford; fn main() { // Partition 1: even numbers 2, 4, 6, 8 let mut w1 = Welford::::new(); for &x in &[2.0, 4.0, 6.0, 8.0] { w1.push(x); } // Partition 2: odd numbers 1, 3, 5, 7 let mut w2 = Welford::::new(); for &x in &[1.0, 3.0, 5.0, 7.0] { w2.push(x); } // Merge partitions — equivalent to processing [1..8] as one stream w1.merge(w2); assert_eq!(w1.mean(), Some(4.5)); assert_eq!(w1.var(), Some(6.0)); println!("merged mean: {:?}", w1.mean()); // Some(4.5) println!("merged var: {:?}", w1.var()); // Some(6.0) } ``` -------------------------------- ### Add Unweighted Sample Source: https://context7.com/schneiderfelipe/welford/llms.txt The `push` method adds a single sample with an implicit weight of 1. It internally calls `push_weighted(value, 1)` and can only be used on the unweighted `Welford` variant. ```rust use welford::Welford; fn main() { let mut w = Welford::::new(); let data = [10.0f32, 20.0, 30.0, 40.0, 50.0]; for &x in &data { w.push(x); } assert_eq!(w.mean(), Some(30.0)); // variance of uniformly spaced values println!("variance: {:?}", w.var()); // Some(250.0) } ``` -------------------------------- ### Retrieve Running Mean Source: https://context7.com/schneiderfelipe/welford/llms.txt The `mean` method returns `Some(mean)` if samples exist, otherwise `None`. The mean is updated incrementally and requires no extra computation at query time. ```rust use welford::Welford; fn main() { let mut w = Welford::::new(); assert_eq!(w.mean(), None); // no samples yet w.push(1.0); assert_eq!(w.mean(), Some(1.0)); w.push(3.0); assert_eq!(w.mean(), Some(2.0)); // (1 + 3) / 2 w.push(5.0); assert_eq!(w.mean(), Some(3.0)); // (1 + 3 + 5) / 3 } ``` -------------------------------- ### Create Weighted Welford Calculator Source: https://context7.com/schneiderfelipe/welford/llms.txt Use `Welford::with_weights()` to create an instance that accepts samples with explicit frequency weights. Weights are treated as frequencies. Both value type `T` and weight type `W` must implement numeric traits. ```rust use welford::Welford; fn main() { let mut w = Welford::::with_weights(); // Equivalent to pushing 1.0 three times, 3.0 twice, 5.0 once w.push_weighted(1.0, 3.0); w.push_weighted(3.0, 2.0); w.push_weighted(5.0, 1.0); println!("mean: {:?}", w.mean()); // Some(2.0) println!("variance: {:?}", w.var()); // Some(2.0) } ``` -------------------------------- ### Add Weighted Sample Source: https://context7.com/schneiderfelipe/welford/llms.txt The `push_weighted` method adds a sample with an explicit weight. It updates the running mean and mean-squared deviation using a numerically stable recurrence relation and is available on both weighted and unweighted variants. ```rust use welford::Welford; fn main() { let mut w = Welford::::with_weights(); // Sensor readings with reliability-based frequency counts w.push_weighted(98.6, 5); // high-confidence readings w.push_weighted(99.1, 2); // moderate-confidence readings w.push_weighted(97.8, 1); // low-confidence reading println!("weighted mean: {:?}", w.mean()); println!("weighted variance: {:?}", w.var()); } ``` -------------------------------- ### Calculate Running Sample Variance with Welford Source: https://context7.com/schneiderfelipe/welford/llms.txt Demonstrates how to use the Welford struct to calculate the running sample variance. Variance is undefined for fewer than two samples and Bessel's correction is used for unbiased estimation. ```rust use welford::Welford; fn main() { let mut w = Welford::::new(); w.push(1.0); assert_eq!(w.var(), None); // undefined with a single sample w.push(3.0); assert_eq!(w.var(), Some(2.0)); // sample variance of [1, 3] w.push(5.0); assert_eq!(w.var(), Some(4.0)); // sample variance of [1, 3, 5] // Derive standard deviation from variance let std_dev = w.var().map(f64::sqrt); println!("std dev: {:?}", std_dev); // Some(2.0) } ``` -------------------------------- ### Welford::with_weights Source: https://context7.com/schneiderfelipe/welford/llms.txt Creates a new Welford instance that accepts samples with explicit frequency weights. Weights are treated as frequencies, meaning a weight of 3 for a value is equivalent to pushing that value three times. Both T (value type) and W (weight type) must implement numeric traits. ```APIDOC ## Welford::with_weights ### Description Creates a new `Welford` instance that accepts samples with explicit frequency weights. Weights are treated as frequencies (not reliabilities), meaning a weight of `3` for a value is equivalent to pushing that value three times. Both `T` (value type) and `W` (weight type) must implement numeric traits. ### Method Associated function (constructor) ### Parameters None ### Request Example ```rust use welford::Welford; let mut w = Welford::::with_weights(); ``` ### Response - **Welford** - A new, empty Welford calculator instance with support for weights. ``` -------------------------------- ### Welford::push Source: https://context7.com/schneiderfelipe/welford/llms.txt Adds a single sample with an implicit weight of 1 to the running calculation. Internally delegates to push_weighted(value, 1). Can only be called on Welford (i.e., the unweighted variant where W = usize). ```APIDOC ## Welford::push ### Description Adds a single sample with an implicit weight of 1 to the running calculation. Internally delegates to `push_weighted(value, 1)`. Can only be called on `Welford` (i.e., the unweighted variant where `W = usize`). ### Method Instance method ### Parameters - **value** (T) - The sample value to add. ### Request Example ```rust use welford::Welford; let mut w = Welford::::new(); w.push(10.0); ``` ### Response None. Updates the internal state of the Welford calculator. ``` -------------------------------- ### Welford::mean Source: https://context7.com/schneiderfelipe/welford/llms.txt Retrieves the running mean. Returns Some(mean) if at least one sample has been added, or None if the calculator is empty. The mean is updated incrementally with each call to push or push_weighted and requires no extra computation at query time. ```APIDOC ## Welford::mean ### Description Returns `Some(mean)` if at least one sample has been added, or `None` if the calculator is empty. The mean is updated incrementally with each call to `push` or `push_weighted` and requires no extra computation at query time. ### Method Instance method ### Parameters None ### Request Example ```rust use welford::Welford; let mut w = Welford::::new(); w.push(1.0); println!("mean: {:?}", w.mean()); // Some(1.0) ``` ### Response - **Option** - `Some(mean)` if samples exist, otherwise `None`. ``` -------------------------------- ### Welford::merge Source: https://context7.com/schneiderfelipe/welford/llms.txt Combines the state of another `Welford` instance into the current one. This allows for merging statistics from different data partitions, enabling parallel computation. ```APIDOC ## `Welford::merge` — Combine two independent calculators ### Description Merges the state of another `Welford` instance into the current one, producing the combined mean and variance as if all samples had been processed by a single calculator. Enables parallel computation: split data across threads, compute partial statistics independently, then merge results. ### Usage Example ```rust use welford::Welford; // Partition 1 let mut w1 = Welford::::new(); for &x in &[2.0, 4.0, 6.0, 8.0] { w1.push(x); } // Partition 2 let mut w2 = Welford::::new(); for &x in &[1.0, 3.0, 5.0, 7.0] { w2.push(x); } // Merge partitions w1.merge(w2); assert_eq!(w1.mean(), Some(4.5)); assert_eq!(w1.var(), Some(6.0)); ``` ``` -------------------------------- ### Welford::var Source: https://context7.com/schneiderfelipe/welford/llms.txt Retrieves the running variance. Returns Some(variance) if at least one sample has been added, or None if the calculator is empty. The variance is updated incrementally with each call to push or push_weighted and requires no extra computation at query time. ```APIDOC ## Welford::var ### Description Returns `Some(variance)` if at least one sample has been added, or `None` if the calculator is empty. The variance is updated incrementally with each call to `push` or `push_weighted` and requires no extra computation at query time. ### Method Instance method ### Parameters None ### Request Example ```rust use welford::Welford; let mut w = Welford::::new(); w.push(2.0); w.push(4.0); println!("variance: {:?}", w.var()); // Some(2.0) ``` ### Response - **Option** - `Some(variance)` if samples exist, otherwise `None`. ``` -------------------------------- ### Welford::push_weighted Source: https://context7.com/schneiderfelipe/welford/llms.txt Adds a sample with an explicit weight to the running calculation. Available on both the weighted and unweighted variants. Updates the internal running mean and mean-squared deviation using Welford's numerically stable recurrence relation. ```APIDOC ## Welford::push_weighted ### Description Adds a sample with an explicit weight to the running calculation. Available on both the weighted and unweighted variants. Updates the internal running mean and mean-squared deviation using Welford's numerically stable recurrence relation. ### Method Instance method ### Parameters - **value** (T) - The sample value to add. - **weight** (W) - The frequency weight associated with the sample. ### Request Example ```rust use welford::Welford; let mut w = Welford::::with_weights(); w.push_weighted(98.6, 5); ``` ### Response None. Updates the internal state of the Welford calculator. ``` -------------------------------- ### Welford::var Source: https://context7.com/schneiderfelipe/welford/llms.txt Retrieves the running sample variance. Returns `Some(variance)` if more than one sample has been added, otherwise `None`. Uses Bessel's correction for unbiased estimation. ```APIDOC ## `Welford::var` — Retrieve the running sample variance ### Description Returns `Some(variance)` if more than one sample has been added, or `None` otherwise (variance is undefined for fewer than 2 samples). Uses Bessel's correction (divides by `n - 1`) for unbiased sample variance estimation. ### Usage Example ```rust use welford::Welford; let mut w = Welford::::new(); w.push(1.0); assert_eq!(w.var(), None); w.push(3.0); assert_eq!(w.var(), Some(2.0)); w.push(5.0); assert_eq!(w.var(), Some(4.0)); ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.