Skip to content

Commit

Permalink
refactor: means sampling
Browse files Browse the repository at this point in the history
- used fused multiply add when computing arithmetic mean
- process harmonic and geometric means in one pass
  • Loading branch information
jqnatividad committed Dec 8, 2024
1 parent 5da6829 commit c2cdf78
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,21 @@ impl OnlineStats {
// See also: https://api.semanticscholar.org/CorpusID:120126049
let oldmean = self.mean;
self.size += 1;
let size_f64 = self.size as f64;

// Use fused multiply-add for better performance & higher accuracy
let delta = sample - oldmean;
self.mean += delta / (self.size as f64);
let delta2 = sample - self.mean;
self.q += delta * delta2;

// Update harmonic mean sum (avoid division by zero)
if sample != 0.0 {
self.harmonic_sum += 1.0 / sample;
}

// Update geometric mean tracking
if sample == 0.0 {
self.has_zero = true;
} else if sample < 0.0 {
self.has_negative = true;
} else if sample > 0.0 {
self.geometric_sum += sample.ln();
self.mean = oldmean + delta / size_f64;
self.q = delta.mul_add(sample - self.mean, self.q);

// Update harmonic and geometric means in one pass
match sample {
0.0 => self.has_zero = true,
s if s < 0.0 => self.has_negative = true,
s => {
self.harmonic_sum += 1.0 / s;
self.geometric_sum += s.ln();
}
}
}

Expand Down

0 comments on commit c2cdf78

Please sign in to comment.