From 38a6a599cc1bc96a6fb54bc0574896027502a083 Mon Sep 17 00:00:00 2001 From: Phil Eaton Date: Mon, 22 Aug 2022 16:51:41 +0000 Subject: [PATCH 1/3] Welfords method for stddev --- aggregate.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/aggregate.go b/aggregate.go index 52bd1dc..a621c68 100644 --- a/aggregate.go +++ b/aggregate.go @@ -34,17 +34,29 @@ func (m *mode) Done() any { } type stddev struct { - xs []float64 + mean float64 + count int + meanSquared float64 } func newStddev() *stddev { return &stddev{} } func (s *stddev) Step(x any) { - s.xs = append(s.xs, floaty(x)) + // Welford's method + // https://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/ + xf := floaty(x) + s.count++ + oldMean := s.mean + s.mean += (xf - s.mean) / float64(s.count) + s.meanSquared += (xf - s.mean) * (xf - oldMean) } func (s *stddev) Done() float64 { - return stat.PopStdDev(s.xs, nil) + if s.count < 2 { + return 0 + } + + return s.meanSquared / float64(s.count - 1) } type percentile struct { From 92ac57cc5158b61fa4855b88d0d316f65765c0c0 Mon Sep 17 00:00:00 2001 From: Phil Eaton Date: Mon, 22 Aug 2022 16:51:45 +0000 Subject: [PATCH 2/3] Fix for format --- aggregate.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aggregate.go b/aggregate.go index a621c68..8ef17d0 100644 --- a/aggregate.go +++ b/aggregate.go @@ -34,8 +34,8 @@ func (m *mode) Done() any { } type stddev struct { - mean float64 - count int + mean float64 + count int meanSquared float64 } @@ -56,7 +56,7 @@ func (s *stddev) Done() float64 { return 0 } - return s.meanSquared / float64(s.count - 1) + return s.meanSquared / float64(s.count-1) } type percentile struct { From 94cb4cd37b601793773d174d3a5a126fe4db5a66 Mon Sep 17 00:00:00 2001 From: Phil Eaton Date: Mon, 22 Aug 2022 16:55:28 +0000 Subject: [PATCH 3/3] Fix for readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b76f82f..4cb3781 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ func main() { ## Strings | Name(s) | Notes | Example | -|-------------------|-----------------------------------------------------------|----------------------------------------------------------------| +| ----------------- | --------------------------------------------------------- | -------------------------------------------------------------- | | repeat, replicate | | `repeat('f', 5) = 'fffff'` | | strpos, charindex | 0-indexed position of substring in string | `strpos('abc', 'b') = 1` | | reverse | | `reverse('abc') = 'cba'` |