mathstats

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSampleSize        = errors.New("sample is too small")
	ErrZeroVariance      = errors.New("sample has zero variance")
	ErrMismatchedSamples = errors.New("samples have different lengths")
)

Functions

func Bounds

func Bounds(xs []float64) (min float64, max float64)

Bounds returns the minimum and maximum values of xs.

func GeoMean

func GeoMean(xs []float64) float64

GeoMean returns the geometric mean of xs. xs must be positive.

func Mean

func Mean(xs []float64) float64

Mean returns the arithmetic mean of xs.

func StdDev

func StdDev(xs []float64) float64

StdDev returns the sample standard deviation of xs.

func Variance

func Variance(xs []float64) float64

Variance returns the sample variance of xs.

Types

type LocationHypothesis

type LocationHypothesis int

A LocationHypothesis specifies the alternative hypothesis of a location test such as a t-test or a Mann-Whitney U-test. The default (zero) value is to test against the alternative hypothesis that they differ.

const (
	// LocationLess specifies the alternative hypothesis that the
	// location of the first sample is less than the second. This
	// is a one-tailed test.
	LocationLess LocationHypothesis = -1

	// LocationDiffers specifies the alternative hypothesis that
	// the locations of the two samples are not equal. This is a
	// two-tailed test.
	LocationDiffers LocationHypothesis = 0

	// LocationGreater specifies the alternative hypothesis that
	// the location of the first sample is greater than the
	// second. This is a one-tailed test.
	LocationGreater LocationHypothesis = 1
)

type Sample

type Sample struct {
	// Xs is the slice of sample values.
	Xs []float64

	// Sorted indicates that Xs is sorted in ascending order.
	Sorted bool
}

Sample is a collection of possibly weighted data points.

func (Sample) Bounds

func (s Sample) Bounds() (min float64, max float64)

Bounds returns the minimum and maximum values of the Sample.

If the Sample is weighted, this ignores samples with zero weight.

This is constant time if s.Sorted and there are no zero-weighted values.

func (*Sample) Clear

func (s *Sample) Clear()

Clear resets this sample so it contains 0 values

func (Sample) Copy

func (s Sample) Copy() *Sample

Copy returns a copy of the Sample.

The returned Sample shares no data with the original, so they can be modified (for example, sorted) independently.

func (*Sample) FilterOutliers

func (s *Sample) FilterOutliers()

FilterOutliers updates this sample in-place by removing all the values that are outliers

func (Sample) GeoMean

func (s Sample) GeoMean() float64

GeoMean returns the geometric mean of the Sample. All samples values must be positive.

func (Sample) IQR

func (s Sample) IQR() float64

IQR returns the interquartile range of the Sample.

This is constant time if s.Sorted and s.Weights == nil.

func (Sample) Mean

func (s Sample) Mean() float64

Mean returns the arithmetic mean of the Sample.

func (*Sample) Percentile

func (s *Sample) Percentile(pctile float64) float64

Percentile returns the pctileth value from the Sample. This uses interpolation method R8 from Hyndman and Fan (1996).

pctile will be capped to the range [0, 1]. If len(xs) == 0 or all weights are 0, returns NaN.

Percentile(0.5) is the median. Percentile(0.25) and Percentile(0.75) are the first and third quartiles, respectively.

This is constant time if s.Sorted and s.Weights == nil.

func (*Sample) Sort

func (s *Sample) Sort() *Sample

Sort sorts the samples in place in s and returns s.

A sorted sample improves the performance of some algorithms.

func (Sample) StdDev

func (s Sample) StdDev() float64

StdDev returns the sample standard deviation of the Sample.

func (Sample) Sum

func (s Sample) Sum() float64

Sum returns the (possibly weighted) sum of the Sample.

func (Sample) Variance

func (s Sample) Variance() float64

Variance returns the variance of xs

func (Sample) Weight

func (s Sample) Weight() float64

Weight returns the total weight of the Sasmple.

type TDist

type TDist struct {
	V float64
}

A TDist is a Student's t-distribution with V degrees of freedom.

func (TDist) Bounds

func (t TDist) Bounds() (float64, float64)

func (TDist) CDF

func (t TDist) CDF(x float64) float64

func (TDist) PDF

func (t TDist) PDF(x float64) float64

type TTestResult

type TTestResult struct {
	// N1 and N2 are the sizes of the input samples. For a
	// one-sample t-test, N2 is 0.
	N1, N2 int

	// T is the value of the t-statistic for this t-test.
	T float64

	// DoF is the degrees of freedom for this t-test.
	DoF float64

	// AltHypothesis specifies the alternative hypothesis tested
	// by this test against the null hypothesis that there is no
	// difference in the means of the samples.
	AltHypothesis LocationHypothesis

	// P is p-value for this t-test for the given null hypothesis.
	P float64
}

A TTestResult is the result of a t-test.

func OneSampleTTest

func OneSampleTTest(x TTestSample, μ0 float64, alt LocationHypothesis) (*TTestResult, error)

OneSampleTTest performs a one-sample t-test on sample x. This tests the null hypothesis that the population mean is equal to μ0. This assumes the distribution of the population of sample means is normal.

func PairedTTest

func PairedTTest(x1, x2 []float64, μ0 float64, alt LocationHypothesis) (*TTestResult, error)

PairedTTest performs a two-sample paired t-test on samples x1 and x2. If μ0 is non-zero, this tests if the average of the difference is significantly different from μ0. If x1 and x2 are identical, this returns nil.

func TwoSampleTTest

func TwoSampleTTest(x1, x2 TTestSample, alt LocationHypothesis) (*TTestResult, error)

TwoSampleTTest performs a two-sample (unpaired) Student's t-test on samples x1 and x2. This is a test of the null hypothesis that x1 and x2 are drawn from populations with equal means. It assumes x1 and x2 are independent samples, that the distributions have equal variance, and that the populations are normally distributed.

func TwoSampleWelchTTest

func TwoSampleWelchTTest(x1, x2 TTestSample, alt LocationHypothesis) (*TTestResult, error)

TwoSampleWelchTTest performs a two-sample (unpaired) Welch's t-test on samples x1 and x2. This is like TwoSampleTTest, but does not assume the distributions have equal variance.

type TTestSample

type TTestSample interface {
	Weight() float64
	Mean() float64
	Variance() float64
}

A TTestSample is a sample that can be used for a one or two sample t-test.

Jump to

Keyboard shortcuts

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