Code Monkey home page Code Monkey logo

Comments (1)

zeim839 avatar zeim839 commented on July 3, 2024

https://github.com/VividCortex/ewma

@rcrowley @mihasya The package calculates an EWMA that is time-independent, meaning that old data points decay with each incoming metric (instead of decaying over time). If this approach were to be used, then calling NewEMWA1(), NewEMWA15(), etc. would not accurately reflect a time-sensitive EWMA.

An alternative solution is to add a timestamp field to StandardEWMA. This way, the EWMA can be calculated on demand (i.e any time Rate() is called), as opposed to concurrently, using the following formula:

[ (1 - alpha) ^ periods ] * oldRate

Where periods are the number of seconds (or any other time interval) that have passed since the last update. Here is an example in Go:

func (a *StandardEWMA) Rate() float64 {
	if atomic.LoadUint32(&a.init) == 0 {
		return 0
	}

	// Calculate periods since last update.
	lastTime := atomic.LoadInt64(&a.time)
	currentTime := time.Now().UTC().UnixNano()

        // 5-second periods. This is equivalent to Tick()-ing every 5 seconds.
	periods := math.Floor(float64(currentTime-lastTime) / float64(5e9))

	// Calculate new rate based on time since last update.
	// [(1-alpha)^periods] * lastRate
	lastRate := math.Float64frombits(atomic.LoadUint64(&a.rate))
	currentRate := math.Pow(1-a.alpha, periods) * lastRate

	return currentRate * 1e9
}

Where a.time is the Unix timestamp of when the StandardEMWA was updated last.

This would also fix Issue #283

Let me know if you agree with my approach and whether you'd like for me to open a PR.

from go-metrics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.