stat

package module
v0.0.0-...-138af3f Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2013 License: GPL-3.0 Imports: 1 Imported by: 15

README

Statistics Build Status

Pure Go implementation of the GSL Statistics library.

For the API overview see Godoc.

Why create this repository when there is also "github.com/grd/statistics" ? Three reasons:

  • Updated API. The interfaces are simpler and the types are more conformal to Go.
  • License changed from GPL v2 to v3 (GNU GSL 1.15 uses GPL v3).
  • Package name "stat" is shorter than "statistics", while retaining logic.
API Interfaces:
  • It uses interfaces for the data access. So different data types can be used.
  • Two datatypes are pre-defined: Float64Slice and IntSlice.
  • For sampling purposes there is a Strider type provided.

Testing 100% pass. Testing covers the complete functionality.
Tested on Debian 6 and Windows 7, both 32- and 64-bit.

Documentation

Overview

GNU GSL Statistics library (v1.15, GPLv3) implemented in Go

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Absdev

func Absdev(data Interface) float64

func AbsdevMean

func AbsdevMean(data Interface, mean float64) float64

AbsdevMean finds the absolute deviation of the data interface

func Correlation

func Correlation(data1, data2 Interface) (res float64)

Correlation()

Calculate Pearson correlation = cov(X, Y) / (sigma_X * sigma_Y)

This routine efficiently computes the correlation in one pass of the data and makes use of the algorithm described in:

B. P. Welford, "Note on a Method for Calculating Corrected Sums of Squares and Products", Technometrics, Vol 4, No 3, 1962.

This paper derives a numerically stable recurrence to compute a sum of products

S = sum_{i=1..N} [ (x_i - mu_x) * (y_i - mu_y) ]

with the relation

S_n = S_{n-1} + ((n-1)/n) * (x_n - mu_x_{n-1}) * (y_n - mu_y_{n-1})

func Covariance

func Covariance(data1, data2 Interface) float64

func CovarianceMean

func CovarianceMean(data1, data2 Interface, mean1, mean2 float64) float64

func Kurtosis

func Kurtosis(data Interface) float64

func KurtosisMainSd

func KurtosisMainSd(data Interface, mean, sd float64) float64

func Lag1Autocorrelation

func Lag1Autocorrelation(data Interface) float64

func Lag1AutocorrelationMean

func Lag1AutocorrelationMean(data Interface, mean float64) float64

func Max

func Max(data Interface) (max float64, max_index int)

Max finds the first largest member and the members position within the data

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	largest, index := stat.Max(data)
	fmt.Printf("The largest value is %g and the index is %d", largest, index)
}
Output:

The largest value is 18.3 and the index is 3

func Mean

func Mean(data Interface) (mean float64)

Mean calculates the arithmetic mean with the recurrence relation

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	mean := stat.Mean(data)
	fmt.Printf("The sample mean is %g", mean)
}
Output:

The sample mean is 16.54

func MedianFromSortedData

func MedianFromSortedData(sortedData Interface) (median float64)

MedianFromSortedData calculates the median of the sorted data. Note that the function doesn't check wheather the data is actually sorted.

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
	"sort"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	sort.Sort(data)
	median := stat.MedianFromSortedData(data)
	fmt.Printf("Sorted dataset: %v\n", data)
	fmt.Printf("The median is %g\n", median)
}
Output:

Sorted dataset: [12.6 16.5 17.2 18.1 18.3]
The median is 17.2

func Min

func Min(data Interface) (min float64, min_index int)

Min finds the first smallest member and the members position within the data

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	smallest, index := stat.Min(data)
	fmt.Printf("The smallest value is %g and the index is %d", smallest, index)
}
Output:

The smallest value is 12.6 and the index is 4

func Minmax

func Minmax(data Interface) (min float64, min_index int, max float64, max_index int)

Minmax finds the first smallest and largest members and the members positions within the data

func PVariance

func PVariance(data1, data2 Interface) float64

PVariance finds the pooled variance of two datasets

func QuantileFromSortedData

func QuantileFromSortedData(sortedData Interface, f float64) (result float64)

QuantileFromSortedData performs the quantile function, also called percent point function or inverse cumulative distribution function, on the sorted data. Note that the function doesn't check wheather the data is actually sorted.

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
	"sort"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	sort.Sort(data)
	upperq := stat.QuantileFromSortedData(data, 0.75)
	lowerq := stat.QuantileFromSortedData(data, 0.25)

	fmt.Printf("Sorted dataset: %v\n", data)
	fmt.Printf("The upper quartile is %g\n", upperq)
	fmt.Printf("The lower quartile is %g\n", lowerq)
}
Output:

Sorted dataset: [12.6 16.5 17.2 18.1 18.3]
The upper quartile is 18.1
The lower quartile is 16.5

func Sd

func Sd(data Interface) float64

func SdMean

func SdMean(data Interface, mean float64) float64

func SdWithFixedMean

func SdWithFixedMean(data Interface, mean float64) float64

func Skew

func Skew(data Interface) float64

func SkewMeanSd

func SkewMeanSd(data Interface, mean, sd float64) (skew float64)

SkewMeanSd calculates the skewness of a dataset

func TTest

func TTest(data1, data2 Interface) float64

runs a t-test between two datasets representing independent samples. Tests to see if the difference between means of the samples is different from zero.

func Tss

func Tss(data Interface) float64

func TssMean

func TssMean(data Interface, mean float64) (res float64)

TssMean takes a dataset and finds the sum of squares about the mean

func Variance

func Variance(data Interface) float64
Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{17.2, 18.1, 16.5, 18.3, 12.6}
	variance := stat.Variance(data)
	fmt.Printf("The estimated variance is %.4f", variance)
}
Output:

The estimated variance is 5.3730

func VarianceMean

func VarianceMean(data Interface, mean float64) float64

func VarianceWithFixedMean

func VarianceWithFixedMean(data Interface, mean float64) float64

func WAbsdev

func WAbsdev(w, data Interface) float64

func WAbsdevMean

func WAbsdevMean(w, data Interface, wmean float64) (wabsdev float64)

WAbsdevMean calculates the weighted absolute deviation of a dataset

func WKurtosis

func WKurtosis(w, data Interface) float64

func WKurtosisMeanSd

func WKurtosisMeanSd(w, data Interface, wmean, wsd float64) float64

WKurtosisMean calculates the kurtosis of a dataset

func WMean

func WMean(w, data Interface) (wmean float64)

WMean calculates the weighted arithmetic mean of a dataset

func WSd

func WSd(w, data Interface) float64

func WSdMean

func WSdMean(w, data Interface, wmean float64) float64

func WSkew

func WSkew(w, data Interface) float64

func WSkewMeanSd

func WSkewMeanSd(w, data Interface, wmean, wsd float64) (wskew float64)

Compute the weighted skewness of a dataset

func WTss

func WTss(w, data Interface) float64

func WTssMean

func WTssMean(w, data Interface, wmean float64) (res float64)

WTssMean takes a dataset and finds the weighted sum of squares about wmean

func WVariance

func WVariance(w, data Interface) float64

func WVarianceMean

func WVarianceMean(w, data Interface, wmean float64) float64

func WVarianceWithFixedMean

func WVarianceWithFixedMean(w, data Interface, wmean float64) float64

func WsdWithFixedMean

func WsdWithFixedMean(w, data Interface, wmean float64) float64

Types

type Float64Slice

type Float64Slice []float64

Float64Slice is a predifined float64 slice type which implements the Interface and Sort interfaces.

func (Float64Slice) Get

func (f Float64Slice) Get(i int) float64

func (Float64Slice) Len

func (f Float64Slice) Len() int

func (Float64Slice) Less

func (f Float64Slice) Less(i, j int) bool

func (Float64Slice) Swap

func (f Float64Slice) Swap(i, j int)

type IntSlice

type IntSlice []int64

IntSlice is a predifined int64 slice type which implements the Interface and Sort interfaces.

func (IntSlice) Get

func (f IntSlice) Get(i int) float64

func (IntSlice) Len

func (f IntSlice) Len() int

func (IntSlice) Less

func (f IntSlice) Less(i, j int) bool

func (IntSlice) Swap

func (f IntSlice) Swap(i, j int)

type Interface

type Interface interface {
	Get(int) float64
	Len() int
}

Interface is used throughout the package.

type Sort

type Sort interface {
	Interface
	Less(int, int) bool
	Swap(int, int)
}

Sort is Interface with sorting functionality.

type SortStrider

type SortStrider struct {
	Sort
	// contains filtered or unexported fields
}

SortStrider strides over the data, for sampling purposes. It also has sorting functionality.

func NewSortStrider

func NewSortStrider(data Sort, stride int) SortStrider

func (SortStrider) Get

func (p SortStrider) Get(i int) float64

func (SortStrider) Len

func (p SortStrider) Len() int

func (SortStrider) Less

func (p SortStrider) Less(i, j int) bool

func (SortStrider) Swap

func (p SortStrider) Swap(i, j int)

type Strider

type Strider struct {
	Interface
	// contains filtered or unexported fields
}

Strider strides over the data, for sampling purposes.

Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{
		.0421, .0941, .1064, .0242,
		.1331, .0773, .0243, .0815,
		.1186, .0356, .0728, .0999,
		.0614, .0479}
	strider := stat.NewStrider(data, 4)
	for i := 0; i < strider.Len(); i++ {
		fmt.Println(strider.Get(i))
	}
}
Output:

0.0421
0.1331
0.1186

func NewStrider

func NewStrider(data Interface, stride int) Strider
Example
package main

import (
	"fmt"
	"github.com/grd/stat"
)

func main() {
	data := stat.Float64Slice{
		.0421, .0941, .1064, .0242,
		.1331, .0773, .0243, .0815,
		.1186, .0356, .0728, .0999,
		.0614, .0479}
	strider := stat.NewStrider(data, 4)
	fmt.Printf("mean data is %.4f\n", stat.Mean(data))
	fmt.Printf("mean strider is %.4f\n", stat.Mean(strider))
}
Output:

mean data is 0.0728
mean strider is 0.0979

func (Strider) Get

func (p Strider) Get(i int) float64

func (Strider) Len

func (p Strider) Len() int

Jump to

Keyboard shortcuts

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