Goulib.stats module

very basic statistics functions

Goulib.stats.mean_var(data)[source]

mean and variance by stable algorithm :param :return: float (mean, variance) of data uses a stable algo by Knuth

Goulib.stats.mean(data)[source]
Returns:mean of data
Goulib.stats.avg(data)
Returns:mean of data
Goulib.stats.variance(data)[source]
Returns:variance of data, faster (?) if mean is already available
Goulib.stats.var(data)
Returns:variance of data, faster (?) if mean is already available
Goulib.stats.stddev(data)[source]
Returns:standard deviation of data
Goulib.stats.confidence_interval(data, conf=0.95)[source]
Returns:(low,high) bounds of 95% confidence interval of data
Goulib.stats.median(data, is_sorted=False)[source]
Returns:median of data
Goulib.stats.mode(data, is_sorted=False)[source]
Returns:mode (most frequent value) of data
Goulib.stats.kurtosis(data)[source]
Goulib.stats.covariance(data1, data2)[source]
Goulib.stats.stats(l)[source]
Returns:min,max,sum,sum2,avg,var of a list
class Goulib.stats.Stats(data=[], mean=None, var=None)[source]

Bases: object

an object that computes mean, variance and modes of data that is appended to it as in a list (but actual values are not stored)

__init__(data=[], mean=None, var=None)[source]
__repr__()[source]
append(x)[source]

add data x to Stats

extend(data)[source]
remove(data)[source]

remove data from Stats :param data: value or iterable of values

sum
sum1
sum2
mean
avg
average
mu
variance
var
stddev
sigma
__add__(other)[source]
__sub__(other)[source]
__mul__(other)[source]
__neg__()[source]
__pow__(n)[source]
covariance(other)[source]
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

class Goulib.stats.Discrete(data)[source]

Bases: Goulib.stats.Stats

discrete probability density function

Parameters:data – can be:
  • list of equiprobable values (uniform distribution)
  • dict of x:p values:probability pairs
__init__(data)[source]
Parameters:data – can be:
  • list of equiprobable values (uniform distribution)
  • dict of x:p values:probability pairs
__call__(x)[source]
__add__(other)
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__le__

Return self<=value.

__lt__

Return self<value.

__mul__(other)
__ne__

Return self!=value.

__neg__()
__new__()

Create and return a new object. See help(type) for accurate signature.

__pow__(n)
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__()
__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

__sub__(other)
append(x)

add data x to Stats

average
avg
covariance(other)
extend(data)
mean
mu
remove(data)

remove data from Stats :param data: value or iterable of values

sigma
stddev
sum
sum1
sum2
var
variance
class Goulib.stats.PDF(pdf, data=[])[source]

Bases: Goulib.expr.Expr, Goulib.stats.Stats

probability density function

__init__(pdf, data=[])[source]
__call__(x=None, **kwargs)[source]
__add__(right)
__and__(right)
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__div__(right)
__eq__(other)
__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__ = None
__invert__()
__le__

Return self<=value.

__lshift__(dx)
__lt__(other)
__mul__(right)
__ne__

Return self!=value.

__neg__()
__new__()

Create and return a new object. See help(type) for accurate signature.

__or__(right)
__pow__(n)
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__()
__rmul__(right)
__rshift__(dx)
__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__()
__sub__(right)
__truediv__(right)
__xor__(right)
append(x)

add data x to Stats

applx(f, var='x')

function composition f o self = self(f(x))

apply(f, right=None)

function composition self o f = f(self(x))

average
avg
covariance(other)
extend(data)
html(**kwargs)
isconstant
Returns:True if Expr evaluates to a constant number of bool
latex()
Returns:string LaTex formula
mean
mu
plot(**kwargs)

renders on IPython Notebook (alias to make usage more straightforward)

png(**kwargs)
remove(data)

remove data from Stats :param data: value or iterable of values

render(fmt='svg', **kwargs)
save(filename, **kwargs)
sigma
stddev
sum
sum1
sum2
svg(**kwargs)
var
variance
Goulib.stats.normal_pdf(x, mu, sigma)[source]

Return the probability density function at x

class Goulib.stats.Normal(data=[], mean=0, var=1)[source]

Bases: Goulib.stats.PDF

represents a normal distributed variable the base class (list) optionally contains data

if data is specified, it it used to fit a normal law

__init__(data=[], mean=0, var=1)[source]

if data is specified, it it used to fit a normal law

sigma
__str__()[source]
latex()[source]
linear(a, b=0)[source]
Returns:a*self+b
__mul__(a)[source]
__div__(a)[source]
__truediv__(a)
__add__(other)[source]
__radd__(other)[source]
__neg__()[source]
__sub__(other)[source]
__rsub__(other)[source]
covariance(other)[source]
cov(other)
pearson(other)[source]
correlation(other)
corr(other)
__and__(right)
__call__(x=None, **kwargs)
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__(other)
__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__ = None
__invert__()
__le__

Return self<=value.

__lshift__(dx)
__lt__(other)
__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__or__(right)
__pow__(n)
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__()
__rmul__(right)
__rshift__(dx)
__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__xor__(right)
append(x)

add data x to Stats

applx(f, var='x')

function composition f o self = self(f(x))

apply(f, right=None)

function composition self o f = f(self(x))

average
avg
extend(data)
html(**kwargs)
isconstant
Returns:True if Expr evaluates to a constant number of bool
mean
mu
plot(**kwargs)

renders on IPython Notebook (alias to make usage more straightforward)

png(**kwargs)
remove(data)

remove data from Stats :param data: value or iterable of values

render(fmt='svg', **kwargs)
save(filename, **kwargs)
stddev
sum
sum1
sum2
svg(**kwargs)
var
variance
Goulib.stats.linear_regression(x, y, conf=None)[source]
Parameters:
  • x,y – iterable data
  • conf – float confidence level [0..1]. If None, confidence intervals are not returned
Returns:

b0,b1,b2, (b0

Return the linear regression parameters and their <prob> confidence intervals.

ex: >>> linear_regression([.1,.2,.3],[10,11,11.5],0.95)