Skip to content

Commit

Permalink
Merge pull request #9 from multiprocessio/pe/welfords-method-for-stddev
Browse files Browse the repository at this point in the history
Online algorithm for stddev
  • Loading branch information
eatonphil authored Aug 22, 2022
2 parents 2ef0fb5 + 94cb4cd commit 9f6825a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'` |
Expand Down
18 changes: 15 additions & 3 deletions aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9f6825a

Please sign in to comment.