exponential

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2023 License: BSD-2-Clause Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToFloat

func BoolToFloat(b bool) float32

BoolToFloat returns 1 if true and 0 if false.

Types

type MovingAvg

type MovingAvg struct {
	sync.Mutex
	// contains filtered or unexported fields
}

MovingAvg tracks the exponential moving average across a number of events and reports when it has surpassed the set cutoff.

func NewMovingAvg

func NewMovingAvg(p MovingAvgParams) *MovingAvg

NewMovingAvg creates a new MovingAvg with the given cutoff, initial average, smoothing factor, and number of events to average.

func (*MovingAvg) Intake

func (m *MovingAvg) Intake(a float32) error

Intake takes in the current average and calculates the exponential average returning true if it is over the cutoff and false otherwise.

The moving average is calculated by:

A(n) = a × (S/E) + A(n-1) × (1 − S/E)

Where:

A(n) is the current exponential moving average
A(n-1) is the previous exponential moving average
a is the intake value
S is the smoothing factor
E is the number of events the average is over

func (*MovingAvg) IsOverCutoff

func (m *MovingAvg) IsOverCutoff() bool

IsOverCutoff returns true if the average has reached the cutoff and false if it has not

type MovingAvgParams

type MovingAvgParams struct {
	// Cutoff is the maximum the moving average can reach before an error is
	// returned on intake. The value should range from 0.0 to 1.0.
	Cutoff float32

	// InitialAverage is the initial exponential moving average to start with.
	InitialAverage float32

	// SmoothingFactor, the exponential smoothing factor; gives the most recent
	// events more weight. The greater the smoothing factor, the greater the
	// influence of more recent events.
	SmoothingFactor float32

	// NumberOfEvents is the number of events to average over.
	NumberOfEvents uint32
}

MovingAvgParams are the parameters needed to create a new MovingAvg.

func DefaultMovingAvgParams

func DefaultMovingAvgParams() MovingAvgParams

DefaultMovingAvgParams returns MovingAvgParams with default values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL