welford

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 3 Imported by: 0

README

Welford

Online method of calculating variance and standard deviation

Go Reference Go Workflow Coverage Status Go Report Latest Release License


Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. Contributing
  5. License

Introduction

Go implementation of Welford’s method for one-pass variance computation with D. H. D. West improved methods.

Highlights
  • Merging of several multiple sets of statistics
  • Add weighted values
Abstract

A method of improved efficiency is given for updating the mean and variance of weighted sampled data when an additional data value is included in the set Evidence is presented that the method is stable and at least as accurate as the best existing updating method.

Updating mean and variance estimates: an improved method - D. H. D. West

Installation

Install using go get
go get github.com/axiomhq/welford
Install from source
git clone https://github.com/axiomhq/welford.git
cd welford
make # Run code generators, linters, sanitizers and test suits

Usage

package welford_test

import (
	"fmt"

	"github.com/axiomhq/welford"
)

func Example() {
	stats1 := welford.New()

	stats1.Add(1)
	stats1.Add(1)
	stats1.Add(1)
	stats1.Add(0)
	stats1.Add(0)
	stats1.Add(0)

	fmt.Println(
		stats1.Mean(),
		stats1.Variance(),
		stats1.StandardDeviation(),
		stats1.VariancePopulation(),
		stats1.StandardDeviationPopulation(),
		stats1.NumDataValues(),
	)

	stats2 := welford.New()
	stats2.Add(3)

	// Merge the values of stats2 into stats1.
	stats1.Merge(stats2)

	// Reset the values in stats2.
	stats2.Clear()

	// Output: 0.5 0.3 0.5477225575051661 0.25 0.5 6
}

Contributing

Feel free to submit PRs or to fill Issues.

License

© Axiom, Inc., 2021

Distributed under MIT License (The MIT License).

See LICENSE for more information.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/axiomhq/welford"
)

func main() {
	stats1 := welford.New()

	stats1.Add(1)
	stats1.Add(1)
	stats1.Add(1)
	stats1.Add(0)
	stats1.Add(0)
	stats1.Add(0)

	fmt.Println(
		stats1.Mean(),
		stats1.Variance(),
		stats1.StandardDeviation(),
		stats1.VariancePopulation(),
		stats1.StandardDeviationPopulation(),
		stats1.NumDataValues(),
	)

	stats2 := welford.New()
	stats2.Add(3)

	// Merge the values of stats2 into stats1.
	stats1.Merge(stats2)

	// Reset the values in stats2.
	stats2.Clear()

}
Output:

0.5 0.3 0.5477225575051661 0.25 0.5 6

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stats

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

Stats of welford's algorithm for computing the mean and variance.

func New

func New() *Stats

New returns new Welford's algorithm stats. The stats are initialized to their respective zero values.

func (*Stats) Add

func (sts *Stats) Add(x float64)

Add a new value to the stats.

func (*Stats) AddWeighted

func (sts *Stats) AddWeighted(val, weight float64)

AddWeighted adds a new weighted value to the stats.

func (*Stats) Clear

func (sts *Stats) Clear()

Clear the stats to its initial state.

func (*Stats) Clone

func (sts *Stats) Clone() *Stats

Clone returns a copy of the stats.

func (*Stats) Mean

func (sts *Stats) Mean() float64

Mean returns the mean of the data.

func (*Stats) Merge

func (sts *Stats) Merge(other *Stats)

Merge other stats into the stats.

func (*Stats) NumDataValues

func (sts *Stats) NumDataValues() uint

NumDataValues returns the number of data values in the stats.

func (*Stats) ReadFrom

func (sts *Stats) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads the stats from `r`.

func (*Stats) StandardDeviation

func (sts *Stats) StandardDeviation() float64

StandardDeviation returns the standard deviation of the data, which is the square root of the variance.

func (*Stats) StandardDeviationPopulation

func (sts *Stats) StandardDeviationPopulation() float64

StandardDeviationPopulation returns the standard deviation of the data, which is the square root of the variance of the sampled data.

func (*Stats) Variance

func (sts *Stats) Variance() float64

Variance returns the variance of the data, assuming the data added was sampled.

func (*Stats) VariancePopulation

func (sts *Stats) VariancePopulation() float64

VariancePopulation returns the variance of the data, assuming the data added was not sampled.

func (*Stats) WriteTo

func (sts *Stats) WriteTo(w io.Writer) (int64, error)

WriteTo writes the stats to `w`.

Jump to

Keyboard shortcuts

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