godist

package module
Version: v0.0.0-...-7f34213 Latest Latest
Warning

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

Go to latest
Published: May 11, 2015 License: MIT Imports: 4 Imported by: 0

README

godist

Build Status

GoDoc

godist provides some Go implementations of useful continuous and discrete probability distributions, as well as some handy methods for working with them.

The general idea is that I will add to these over time, but that each distribution will implement the following interface:

type Distribution interface{
	// distribution mean
	Mean() (float64, error)

	// distribution median
	Median() (float64, error)

	// distribution mode
	Mode() (float64, error)

	// distribution variance
	Variance() (float64, error)

	// generate a random value according to the probability distribution
	Float64() (float64, error)
}

In practice, distributions may also provide other useful methods, where appropriate.

The intentions of godist is not to provide the fastest, most efficient implementations, but instead to provide idiomatic Go implementations that can be easily understood and extended. Having said that, where there are useful and well-understood numerical tricks and tools to improve performance, these have been utilised and documented.

Contributions welcome!

Current Distributions
  • Beta Distribution
  • Empirical Distribution

Documentation

Overview

Package godist provides a collection of useful continuous and discrete probability distributions, as well as helpful associated methods around them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Beta

type Beta struct {
	Alpha float64
	Beta  float64
}

A Beta distribution is a continuous probability distribution on the range [0, 1], which can be formed using shape parameters α, β > 0.

Beta distributions have many uses, with one of the more common ones being to model random variables.

func (Beta) Float64

func (beta Beta) Float64() (float64, error)

Float64 returns a random variate from the Beta Distribution.

Float64 makes use of four different algorithms for generating random variates, depending on the values of α and β. This implementation is based on the work done by Kevin Karplus in gen_beta.c (https://compbio.soe.ucsc.edu/gen_sequence/gen_beta.c)

func (Beta) Mean

func (beta Beta) Mean() (float64, error)

Mean returns the mean of the Beta distribution, i.e., α / (α + β)

func (Beta) Median

func (beta Beta) Median() (float64, error)

Median returns the median of the Beta distribution.

Since there is no closed-form expression for the median of a Beta distribution, we use a number of known closed-form special cases, and an approximation for the general case where α > 1 and β > 1.

Currently, Median cannot calculate the median where α and β are < 1 unless α = β.

func (Beta) Mode

func (beta Beta) Mode() (float64, error)

Mode returns the mode of the Beta distribution.

func (Beta) Variance

func (beta Beta) Variance() (float64, error)

Variance returns the variance of the Beta Distribution.

type Distribution

type Distribution interface {
	Mean() (float64, error)
	Median() (float64, error)
	Mode() (float64, error)
	Variance() (float64, error)

	// generate a random value according to the probability distribution
	Float64() (float64, error)
}

Distribution is the interface that defines useful methods for understanding specific instances of distributions, and sampling random variates from them.

type Empirical

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

An Empirical distribution in the context of the godist package is essentially just a sample of discrete values.

All values added to an Empirical Distribution remain in memory, and while some efficiencies have been made around memoising certain values, in general calls to Median and Mode currently involve re-sorting the entire sample in the Empirical distribution.

func (*Empirical) Add

func (e *Empirical) Add(values ...float64)

Add adds one or more values to the empirical sample.

Add carries out some operations to improve the efficiency of other method calls, which is the main reason why the underlying sample data-structure is not exported.

func (*Empirical) Float64

func (e *Empirical) Float64() (float64, error)

Float64 returns a randomly sampled value from the Empirical distribution.

func (*Empirical) Mean

func (e *Empirical) Mean() (float64, error)

Mean returns the distribution mean.

func (*Empirical) Median

func (e *Empirical) Median() (float64, error)

Median calculates the distribution median.

Median returns a memoised median if either: (1) the distribution has not been updated since the last call to Median, or (2) all values added to the distribution since the last call are equal to the median of the distribution.

In the case that the distribution sample size is even, the mean of the two middle values is returned.

func (*Empirical) Mode

func (e *Empirical) Mode() (float64, error)

Mode calculates the distribution mode.

Mode returns a memoised if either: (1) the distribution has not been updated since the last call to Mode, or (2) all values added to the distribution since the last call are equal to the mode of the distribution.

In the case that the distribution is multi-modal, the smallest mode is returned.

func (*Empirical) Size

func (e *Empirical) Size() float64

Size returns the number of samples in the distribution.

func (*Empirical) Variance

func (e *Empirical) Variance() (float64, error)

Variance returns the distribution variance.

type InvalidDistributionError

type InvalidDistributionError struct{ S string }

func (InvalidDistributionError) Error

func (e InvalidDistributionError) Error() string

type UnsupportedError

type UnsupportedError struct{ S string }

func (UnsupportedError) Error

func (e UnsupportedError) Error() string

Jump to

Keyboard shortcuts

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