moment

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package moment provides a library of data structures/algorithms for calculating online moment-based statistics from a stream of data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(wrapper CoreWrapper) error

Init sets a CoreWrapper up with a core for consuming.

Types

type Core

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

Core is a struct that stores fundamental information for moments of a stream.

func NewCore

func NewCore(config *CoreConfig) (*Core, error)

NewCore instantiates a Core struct based on a provided config.

func (*Core) Clear

func (c *Core) Clear()

Clear clears all stats being tracked.

func (*Core) Count

func (c *Core) Count() int

Count returns the number of values seen seen globally.

func (*Core) Lock

func (c *Core) Lock()

Lock locks the core internals for writing.

func (*Core) Mean

func (c *Core) Mean() (float64, error)

Mean returns the mean of values seen.

func (*Core) Push

func (c *Core) Push(x float64) error

Push adds a new value for a Core object to consume.

func (*Core) RLock

func (c *Core) RLock()

RLock locks the core internals for reading.

func (*Core) RUnlock

func (c *Core) RUnlock()

RUnlock undoes a single RLock call.

func (*Core) Sum

func (c *Core) Sum(k int) (float64, error)

Sum returns the kth-power centralized sum of values seen. In other words, this returns the kth power sum of the differences of the values seen from their mean.

func (*Core) Unlock

func (c *Core) Unlock()

Unlock undoes a Lock call.

func (*Core) UnsafeClear

func (c *Core) UnsafeClear()

UnsafeClear clears all stats being tracked, but does not lock. This should only be used if the user plans to make use of the Lock()/Unlock() Core methods.

func (*Core) UnsafeCount

func (c *Core) UnsafeCount() int

UnsafeCount returns the number of values seen seen globally, but does not lock. This should only be used if the user plans to make use of the [R]Lock()/[R]Unlock() Core methods.

func (*Core) UnsafeMean

func (c *Core) UnsafeMean() (float64, error)

UnsafeMean returns the mean of values seen, but does not lock. This should only be used if the user plans to make use of the [R]Lock()/[R]Unlock() Core methods.

func (*Core) UnsafePush

func (c *Core) UnsafePush(x float64) error

UnsafePush adds a new value for a Core object to consume, but does not lock. This should only be used if the user plans to make use of the Lock()/Unlock() Core methods.

func (*Core) UnsafeSum

func (c *Core) UnsafeSum(k int) (float64, error)

UnsafeSum returns the kth-power centralized sum of values seen, but does not lock. This should only be used if the user plans to make use of the [R]Lock()/[R]Unlock() Core methods.

type CoreConfig

type CoreConfig struct {
	Sums   SumsConfig // sums tracked must be positive
	Window *int       // must be 0 if decay is set, must be nonnegative in general
	Decay  *float64   // optional, must lie in the interval (0, 1)
}

CoreConfig is the struct containing configuration options for instantiating a Core object.

func MergeConfigs

func MergeConfigs(configs ...*CoreConfig) (*CoreConfig, error)

MergeConfigs merges CoreConfig objects.

type CoreWrapper

type CoreWrapper interface {
	SetCore(*Core)
	Config() *CoreConfig
}

CoreWrapper is the interface for an entity that wraps around a Core for stats. The methods below are required for setting up a Core for the wrapper.

type EWMA

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

EWMA is a metric that tracks the exponentially weighted moving average.

func NewEWMA

func NewEWMA(decay float64) *EWMA

NewEWMA instantiates a EWMA struct.

func (*EWMA) Clear

func (a *EWMA) Clear()

Clear resets the metric.

func (*EWMA) Config

func (a *EWMA) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*EWMA) IsSetCore

func (a *EWMA) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*EWMA) Push

func (a *EWMA) Push(x float64) error

Push adds a new value for EWMA to consume.

func (*EWMA) SetCore

func (a *EWMA) SetCore(c *Core)

SetCore sets the Core.

func (*EWMA) String

func (a *EWMA) String() string

String returns a string representation of the metric.

func (*EWMA) Value

func (a *EWMA) Value() (float64, error)

Value returns the value of the exponentially weighted moving average.

type EWMMoment

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

EWMMoment is a metric that tracks the kth exponentially weighted sample central moment.

func NewEWMMoment

func NewEWMMoment(k int, decay float64) *EWMMoment

NewEWMMoment instantiates a EWMMoment struct.

func (*EWMMoment) Clear

func (m *EWMMoment) Clear()

Clear resets the metric.

func (*EWMMoment) Config

func (m *EWMMoment) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*EWMMoment) IsSetCore

func (m *EWMMoment) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*EWMMoment) Push

func (m *EWMMoment) Push(x float64) error

Push adds a new value for EWMMoment to consume.

func (*EWMMoment) SetCore

func (m *EWMMoment) SetCore(c *Core)

SetCore sets the Core.

func (*EWMMoment) String

func (m *EWMMoment) String() string

String returns a string representation of the metric.

func (*EWMMoment) Value

func (m *EWMMoment) Value() (float64, error)

Value returns the value of the kth exponentially weighted sample central moment.

type EWMStd

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

EWMStd is a metric that tracks the exponentially weighted sample standard deviation.

func NewEWMStd

func NewEWMStd(decay float64) *EWMStd

NewEWMStd instantiates an EWMStd struct.

func (*EWMStd) Clear

func (s *EWMStd) Clear()

Clear resets the metric.

func (*EWMStd) Config

func (s *EWMStd) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*EWMStd) IsSetCore

func (s *EWMStd) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*EWMStd) Push

func (s *EWMStd) Push(x float64) error

Push adds a new value for EWMStd to consume.

func (*EWMStd) SetCore

func (s *EWMStd) SetCore(c *Core)

SetCore sets the Core.

func (*EWMStd) String

func (s *EWMStd) String() string

String returns a string representation of the metric.

func (*EWMStd) Value

func (s *EWMStd) Value() (float64, error)

Value returns the value of the exponentially weighted sample standard deviation.

type Kurtosis

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

Kurtosis is a metric that tracks the sample excess kurtosis.

func NewGlobalKurtosis

func NewGlobalKurtosis() *Kurtosis

NewGlobalKurtosis instantiates a global Kurtosis struct. This is equivalent to calling NewKurtosis(0).

func NewKurtosis

func NewKurtosis(window int) *Kurtosis

NewKurtosis instantiates a Kurtosis struct.

func (*Kurtosis) Clear

func (k *Kurtosis) Clear()

Clear resets the metric.

func (*Kurtosis) Config

func (k *Kurtosis) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*Kurtosis) IsSetCore

func (k *Kurtosis) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*Kurtosis) Push

func (k *Kurtosis) Push(x float64) error

Push adds a new value for Kurtosis to consume.

func (*Kurtosis) SetCore

func (k *Kurtosis) SetCore(c *Core)

SetCore sets the Core.

func (*Kurtosis) String

func (k *Kurtosis) String() string

String returns a string representation of the metric.

func (*Kurtosis) Value

func (k *Kurtosis) Value() (float64, error)

Value returns the value of the sample excess kurtosis.

type Mean

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

Mean is a metric that tracks the mean.

func NewGlobalMean

func NewGlobalMean() *Mean

NewGlobalMean instantiates a global Mean struct. This is equivalent to calling NewMean(0).

func NewMean

func NewMean(window int) *Mean

NewMean instantiates a Mean struct.

func (*Mean) Clear

func (m *Mean) Clear()

Clear resets the metric.

func (*Mean) Config

func (m *Mean) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*Mean) IsSetCore

func (m *Mean) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*Mean) Push

func (m *Mean) Push(x float64) error

Push adds a new value for Mean to consume.

func (*Mean) SetCore

func (m *Mean) SetCore(c *Core)

SetCore sets the Core.

func (*Mean) String

func (m *Mean) String() string

String returns a string representation of the metric.

func (*Mean) Value

func (m *Mean) Value() (float64, error)

Value returns the value of the mean.

type Metric

type Metric interface {
	stream.SimpleMetric
	CoreWrapper
}

Metric is the interface for a metric that tracks the moment of a stream. Any Metric that will actually be consuming values (i.e. will have its Push method called) needs to be passed into the Init() method, which sets it up with a Core for consuming values and keeping track of centralized sums.

type Moment

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

Moment is a metric that tracks the kth sample central moment.

func New

func New(k int, window int) *Moment

New instantiates a Moment struct.

func NewGlobal

func NewGlobal(k int) *Moment

NewGlobal instantiates a global Moment struct. This is equivalent to calling New(k, 0).

func (*Moment) Clear

func (m *Moment) Clear()

Clear resets the metric.

func (*Moment) Config

func (m *Moment) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*Moment) IsSetCore

func (m *Moment) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*Moment) Push

func (m *Moment) Push(x float64) error

Push adds a new value for Moment to consume.

func (*Moment) SetCore

func (m *Moment) SetCore(c *Core)

SetCore sets the Core.

func (*Moment) String

func (m *Moment) String() string

String returns a string representation of the metric.

func (*Moment) Value

func (m *Moment) Value() (float64, error)

Value returns the value of the kth sample central moment.

type Skewness

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

Skewness is a metric that tracks the adjusted Fisher-Pearson sample skewness.

func NewGlobalSkewness

func NewGlobalSkewness() *Skewness

NewGlobalSkewness instantiates a global Skewness struct. This is equivalent to calling NewSkewness(0).

func NewSkewness

func NewSkewness(window int) *Skewness

NewSkewness instantiates a Skewness struct.

func (*Skewness) Clear

func (s *Skewness) Clear()

Clear resets the metric.

func (*Skewness) Config

func (s *Skewness) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*Skewness) IsSetCore

func (s *Skewness) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*Skewness) Push

func (s *Skewness) Push(x float64) error

Push adds a new value for Skewness to consume.

func (*Skewness) SetCore

func (s *Skewness) SetCore(c *Core)

SetCore sets the Core.

func (*Skewness) String

func (s *Skewness) String() string

String returns a string representation of the metric.

func (*Skewness) Value

func (s *Skewness) Value() (float64, error)

Value returns the value of the adjusted Fisher-Pearson sample skewness.

type Std

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

Std is a metric that tracks the sample standard deviation.

func NewGlobalStd

func NewGlobalStd() *Std

NewGlobalStd instantiates a global Std struct. This is equivalent to calling NewStd(0).

func NewStd

func NewStd(window int) *Std

NewStd instantiates an Std struct.

func (*Std) Clear

func (s *Std) Clear()

Clear resets the metric.

func (*Std) Config

func (s *Std) Config() *CoreConfig

Config returns the CoreConfig needed.

func (*Std) IsSetCore

func (s *Std) IsSetCore() bool

IsSetCore returns if the core has been set.

func (*Std) Push

func (s *Std) Push(x float64) error

Push adds a new value for Std to consume.

func (*Std) SetCore

func (s *Std) SetCore(c *Core)

SetCore sets the Core.

func (*Std) String

func (s *Std) String() string

String returns a string representation of the metric.

func (*Std) Value

func (s *Std) Value() (float64, error)

Value returns the value of the sample standard deviation.

type SumsConfig

type SumsConfig map[int]bool

SumsConfig is an alias for a map of ints to bools; this configures the sums that a Core object will track.

Jump to

Keyboard shortcuts

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