stats

package module
v0.0.0-...-e793b1f Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2018 License: MIT Imports: 4 Imported by: 4

README

Build Status

gostats

Statistics for golang

Usage

To install, do go get github.com/r0fls/gostats. Check out gostats_test.go for a working example of using each distribution.

Example
package main

import (
    "fmt"
    "github.com/r0fls/gostats"
)

func main() {
    b := stats.Bernoulli(.5)
    fmt.Println(b.Random())

    // or fit a distribution from data...
    b = stats.FitBernoulli([0,1,1,1])
    fmt.Println(b.Random())
}
Distributions

Currently the following distributions are included:

  • Bernoulli
  • Laplace
  • Poisson
  • Geometric
  • Weibull
  • Exponential
  • Binomial
  • NegativeBinomial *

*note the negative binomial takes parameters r, p where r is the number allowed success before termination of the trials and p is the success of a given trial. The random variable produced by NegativeBinomial is the number of failures.

And each distribution has these functions:

  • Pmf or Pdf
  • Cdf
  • Quantile
  • Random

Also there is a corresponding function named FitDistrbution for each distribution, as shown in the above example with the Bernoulli. That function uses the MLE for each distribution to choose the best estimation for the parameters and returns an initialized distribution with them.

Common

Additionally there are some common functions. Most notably is LSR, which performs a least squares regression.

Additional Info

The default seed function is time.Now().UTC().UnixNano(). However you can override that by calling rand.Seed(uniqueInteger) yourself before generating random numbers.

Contributing

If you're interested in contributing, please submit a pull request, or raise an issue.

TO-DO
  • add more distributions
  • should random return an array?
  • allow updating a fitted distribution with more data

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Choose

func Choose(n int, k int) int
Example
fmt.Println(stats.Choose(4, 2))
Output:

6

func Factorial

func Factorial(n int) int

Common functions

Example
fmt.Println(stats.Factorial(4))
Output:

24

func LSR

func LSR(data [][]float64) []float64
Example
data := [][]float64{{60.0, 3.1}, {61.0, 3.6}, {62.0, 3.8}, {63, 4}, {65.0, 4.1}}
fmt.Println(stats.LSR(data))
Output:

[-7.963513513513208 0.18783783783783292]

func MeanFloat64

func MeanFloat64(data []float64) float64

func MeanInt

func MeanInt(data []int) float64

func MedianFloat64

func MedianFloat64(data []float64) float64

func MedianInt

func MedianInt(data []int) float64
Example
s := []int{2, 3, 5, 7, 11, 13}
fmt.Println(stats.MedianInt(s))
Output:

6

func Seed

func Seed()

func SumFloat64

func SumFloat64(data []float64) float64
Example
s := []float64{2, 3, 5, 7, 11, 13}
sum := stats.SumFloat64(s)
fmt.Println(sum)
Output:

41

func SumInt

func SumInt(data []int) int
Example
s := []int{2, 3, 5, 7, 11, 13}
sum := stats.SumInt(s)
fmt.Println(sum)
Output:

41

Types

type BernoulliType

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

func Bernoulli

func Bernoulli(p float64) BernoulliType

func FitBernoulli

func FitBernoulli(data []int) BernoulliType
Example
data := []int{1, 1, 1, 0, 0}
b := stats.FitBernoulli(data)
fmt.Println(b.Cdf(0))
Output:

0.4

func (BernoulliType) Cdf

func (b BernoulliType) Cdf(k int) float64

func (BernoulliType) Pmf

func (b BernoulliType) Pmf(k int) float64

func (BernoulliType) Quantile

func (b BernoulliType) Quantile(p float64) int

func (BernoulliType) Random

func (this BernoulliType) Random(k ...int) int

type BinomialType

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

func Binomial

func Binomial(n int, p float64) BinomialType

func (BinomialType) Cdf

func (b BinomialType) Cdf(k int) float64

func (BinomialType) Pmf

func (b BinomialType) Pmf(k int) float64

func (BinomialType) Quantile

func (b BinomialType) Quantile(x float64) int

func (BinomialType) Random

func (this BinomialType) Random(k ...int) int

type ExponentialType

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

func Exponential

func Exponential(l float64) ExponentialType

func FitExponential

func FitExponential(data []float64) ExponentialType
Example
data := []float64{10, 3, 3, 4, 5}
g := stats.FitExponential(data)
fmt.Println(g.Quantile(.5))
Output:

3.465735902799726

func (ExponentialType) Cdf

func (e ExponentialType) Cdf(x float64) float64

func (ExponentialType) Pdf

func (e ExponentialType) Pdf(x float64) float64

func (ExponentialType) Quantile

func (e ExponentialType) Quantile(p float64) float64

func (ExponentialType) Random

func (this ExponentialType) Random(k ...int) float64

type GeometricType

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

func FitGeometric

func FitGeometric(data []int) GeometricType
Example
data := []int{10, 3, 3, 4, 5}
g := stats.FitGeometric(data)
fmt.Println(g.Quantile(.5))
Output:

4

func Geometric

func Geometric(p float64) GeometricType

func (GeometricType) Cdf

func (g GeometricType) Cdf(k int) float64

func (GeometricType) Pmf

func (g GeometricType) Pmf(k int) float64

func (GeometricType) Quantile

func (g GeometricType) Quantile(p float64) int

func (GeometricType) Random

func (this GeometricType) Random(k ...int) int

type LaplaceType

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

func FitLaplace

func FitLaplace(data []float64) LaplaceType
Example
data := []float64{1, 1, 1, 0, 0}
l := stats.FitLaplace(data)
fmt.Println(l.Quantile(.5))
Output:

1

func Laplace

func Laplace(mean float64, b float64) LaplaceType

func (LaplaceType) Cdf

func (l LaplaceType) Cdf(x float64) float64

func (LaplaceType) Pdf

func (l LaplaceType) Pdf(x float64) float64

func (LaplaceType) Quantile

func (l LaplaceType) Quantile(p float64) float64

func (LaplaceType) Random

func (this LaplaceType) Random(k ...int) float64

type NegativeBinomialType

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

func NegativeBinomial

func NegativeBinomial(k int, p float64) NegativeBinomialType

func (NegativeBinomialType) Cdf

func (b NegativeBinomialType) Cdf(r int) float64

func (NegativeBinomialType) Pmf

func (b NegativeBinomialType) Pmf(r int) float64

func (NegativeBinomialType) Quantile

func (b NegativeBinomialType) Quantile(x float64) int

func (NegativeBinomialType) Random

func (this NegativeBinomialType) Random(k ...int) int

type PoissonType

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

func FitPoisson

func FitPoisson(data []int) PoissonType
Example
data := []int{10, 3, 3, 4, 5}
p := stats.FitPoisson(data)
fmt.Println(p.Quantile(.5))
Output:

5

func Poisson

func Poisson(m float64) PoissonType

func (PoissonType) Cdf

func (p PoissonType) Cdf(k int) float64

func (PoissonType) Pmf

func (p PoissonType) Pmf(k int) float64

func (PoissonType) Quantile

func (p PoissonType) Quantile(x float64) int

func (PoissonType) Random

func (this PoissonType) Random(k ...int) int

type WeibullType

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

func Weibull

func Weibull(l float64, k float64) WeibullType

func (WeibullType) Cdf

func (w WeibullType) Cdf(x float64) float64

func (WeibullType) Pdf

func (w WeibullType) Pdf(x float64) float64

func (WeibullType) Quantile

func (w WeibullType) Quantile(p float64) float64

func (WeibullType) Random

func (this WeibullType) Random(k ...int) float64

Jump to

Keyboard shortcuts

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