movingaverage

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

golang-moving-average

Moving average implementation for Go. View the documentation.

Usage

import "github.com/SimonWaldherr/golang-moving-average"

ma := movingaverage.New(5) // 5 is the window size
ma.Add(10)
ma.Add(15)
ma.Add(20)
ma.Add(1)
ma.Add(1)
ma.Add(5) // This one will effectively overwrite the first value (10 in this example)
avg := ma.Avg() // Will return 8.4

Concurrency

By default the library is not thread safe. It is however possible to wrap the object in a thread safe manner that can be used concurrently from many routines:

ma := movingaverage.Concurrent(movingaverage.New(5)) // concurrent safe version
ma.Add(10)
avg := ma.Avg() // Will return 10.0

Min/Max/Count

Basic operations are possible:

ma := movingaverage.New(5) // 5 is the window size
min, err := ma.Min() // min will return lowest value, error is set if there's no values yet
max, err := ma.Max() // max will return highest value, error is set if there's no values yet
count := ma.Count() // count will return number of filled slots

Partially used windows

In case you define a window of let's say 5 and only put in 2 values, the average will be based on those 2 values.

Window 5 - Values: 2, 2 - Average: 2 (not 0.8)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentMovingAverage

type ConcurrentMovingAverage struct {
	// contains filtered or unexported fields
}

func Concurrent

func Concurrent(ma *MovingAverage) *ConcurrentMovingAverage

func (*ConcurrentMovingAverage) Add

func (c *ConcurrentMovingAverage) Add(values ...float64)

func (*ConcurrentMovingAverage) Avg

func (*ConcurrentMovingAverage) Count

func (c *ConcurrentMovingAverage) Count() int

func (*ConcurrentMovingAverage) Max

func (*ConcurrentMovingAverage) Min

func (*ConcurrentMovingAverage) SlotsFilled

func (c *ConcurrentMovingAverage) SlotsFilled() bool

func (*ConcurrentMovingAverage) Values

func (c *ConcurrentMovingAverage) Values() []float64

type MovingAverage

type MovingAverage struct {
	Window int
	// contains filtered or unexported fields
}
Example
values := movingaverage.New(4)

values.Add(1)
fmt.Printf("Values: %#v\nSum: %.0f, Arithmetic: %.1f, Median: %.1f, Geometric: %.2f\n\n",
	values.FilledValues(),
	values.Sum(),
	values.Arithmetic(),
	values.Median(),
	values.Geometric(),
)

values.Add(2)
fmt.Printf("Values: %#v\nSum: %.0f, Arithmetic: %.1f, Median: %.1f, Geometric: %.2f\n\n",
	values.FilledValues(),
	values.Sum(),
	values.Arithmetic(),
	values.Median(),
	values.Geometric(),
)

values.Add(6)
fmt.Printf("Values: %#v\nSum: %.0f, Arithmetic: %.1f, Median: %.1f, Geometric: %.2f\n\n",
	values.FilledValues(),
	values.Sum(),
	values.Arithmetic(),
	values.Median(),
	values.Geometric(),
)

values.Add(3)
fmt.Printf("Values: %#v\nSum: %.0f, Arithmetic: %.1f, Median: %.1f, Geometric: %.2f\n\n",
	values.FilledValues(),
	values.Sum(),
	values.Arithmetic(),
	values.Median(),
	values.Geometric(),
)

values.Add(0)
fmt.Printf("Values: %#v\nSum: %.0f, Arithmetic: %.1f, Median: %.1f, Geometric: %.2f\n\n",
	values.FilledValues(),
	values.Sum(),
	values.Arithmetic(),
	values.Median(),
	values.Geometric(),
)
Output:

Values: []float64{1}
Sum: 1, Arithmetic: 1.0, Median: 1.0, Geometric: 1.00

Values: []float64{1, 2}
Sum: 3, Arithmetic: 1.5, Median: 1.5, Geometric: 1.41

Values: []float64{1, 2, 6}
Sum: 9, Arithmetic: 3.0, Median: 2.0, Geometric: 2.29

Values: []float64{1, 2, 3, 6}
Sum: 12, Arithmetic: 3.0, Median: 2.5, Geometric: 2.45

Values: []float64{0, 2, 3, 6}
Sum: 11, Arithmetic: 2.8, Median: 2.5, Geometric: 0.00

func New

func New(window int) *MovingAverage

func (*MovingAverage) Add

func (ma *MovingAverage) Add(values ...float64)

func (*MovingAverage) Arithmetic added in v1.0.2

func (ma *MovingAverage) Arithmetic() float64

func (*MovingAverage) Avg

func (ma *MovingAverage) Avg() float64

func (*MovingAverage) Count

func (ma *MovingAverage) Count() int

func (*MovingAverage) FilledValues added in v1.0.2

func (ma *MovingAverage) FilledValues() []float64

func (*MovingAverage) Geometric added in v1.0.2

func (ma *MovingAverage) Geometric() float64

func (*MovingAverage) Max

func (ma *MovingAverage) Max() (float64, error)

func (*MovingAverage) Median

func (ma *MovingAverage) Median() float64

func (*MovingAverage) Min

func (ma *MovingAverage) Min() (float64, error)

func (*MovingAverage) SetIgnoreInfValues

func (ma *MovingAverage) SetIgnoreInfValues(ignoreInfValues bool)

func (*MovingAverage) SetIgnoreNanValues

func (ma *MovingAverage) SetIgnoreNanValues(ignoreNanValues bool)

func (*MovingAverage) SlotsFilled

func (ma *MovingAverage) SlotsFilled() bool

func (*MovingAverage) Sum added in v1.0.2

func (ma *MovingAverage) Sum() float64

func (*MovingAverage) Values

func (ma *MovingAverage) Values() []float64

Jump to

Keyboard shortcuts

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