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

Improve SimpleEWMA zero value handling #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,30 @@ func NewMovingAverage(age ...float64) MovingAverage {
type SimpleEWMA struct {
// The current value of the average. After adding with Add(), this is
// updated to reflect the average of all values seen thus far.
value float64
value *float64
}

// Add adds a value to the series and updates the moving average.
func (e *SimpleEWMA) Add(value float64) {
if e.value == 0 { // this is a proxy for "uninitialized"
e.value = value
if e.value == nil { // this is a proxy for "uninitialized"
e.value = &value
} else {
e.value = (value * DECAY) + (e.value * (1 - DECAY))
*e.value = (value * DECAY) + (e.Value() * (1 - DECAY))
}
}

// Value returns the current value of the moving average.
func (e *SimpleEWMA) Value() float64 {
return e.value
if e.value == nil { // this is a proxy for "uninitialized"
return 0
} else {
return *e.value
}
}

// Set sets the EWMA's value.
func (e *SimpleEWMA) Set(value float64) {
e.value = value
e.value = &value
}

// VariableEWMA represents the exponentially weighted moving average of a series of
Expand Down