/variance

Go implementation Welford’s method for one-pass variance computation

Primary LanguageGoMIT LicenseMIT

Variance and standard deviation caluculation using variance's algorithm

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 variance’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/variance

Install from source

git clone https://github.com/axiomhq/variance.git
cd variance
make # Run code generators, linters, sanitizers and test suits

Usage

package variance_test

import (
	"fmt"

	"github.com/axiomhq/variance"
)

func Example() {
	stats1 := variance.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 := variance.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. Every kind of help is appreciated. Before committing, make should run without any issues. Kindly check our Contributing guide on how to propose bug fixes and improvements, and submitting pull requests to the project.

License

© Axiom, Inc., 2021

Distributed under MIT License (The MIT License).

See LICENSE for more information.