nupic.math
index
python2.5/site-packages/nupic/math/__init__.py

## @file
 
nupic.math is a package containing modules related to mathematical, probabilistic
and statistical data structures and simple algorithms.
 
The primary sub-modules include (use help on these modules for additional
online documentation):
 
nupic.bindings.math
A module containing many low-level mathematical data structures and algorithms.
This module is a set of Python bindings for the Numenta C++ math libraries.
Because of this, some calling conventions may more closely reflect the underlying
C++ architecture than a typical Python module.
All classes, functions and constants of nupic.bindings.math are pre-imported
into nupic.math, and thus are accessible from nupic.math.
The module contains the following important and frequently used classes:
  SparseMatrix
  SparseTensor
  TensorIndex
  Domain
 
nupic.math.stats
Module of statistical data structures and functions used in learning algorithms
and for analysis of HTM network inputs and outputs.

 
Package Contents
       
MCMC
cross
dist
logarithms
logisticregression
mvn
proposal
stats

 
Classes
       
__builtin__.object
nupic.bindings.math.PySparseTensor
nupic.bindings.math.PyTensorIndex
nupic.math.MCMC.MHSampler
nupic.math.stats.ConditionalProbabilityTable2D
nupic.bindings.math._Domain32(__builtin__.object)
nupic.bindings.math.PyDomain

 
class ConditionalProbabilityTable2D(__builtin__.object)
    Holds frequencies in a 2D grid of bins.
Binning is not performed automatically by this class.
Bin updates must be done one row at a time.
Based on nta::SparseMatrix which is a compressed sparse row matrix.
Number of columns cannot be changed once set.
Number of rows may be increased.
Also maintains the row and column sumProp distributions.
 
  Methods defined here:
__init__(self, rowHint=None, ncols=None)
Constructs a new empty histogram with no rows or columns.
If rowHint is specified, ncols must be specified
(though not vice versa).
If ncols is specified, the number of columns cannot be changed 
thereafter.
clean_outcpd(self)
Hack to act like clean_outcpd on zeta1.TopLevelNode.
Take the max element in each to column, set it to 1, and set all the 
other elements to 0.
Only called by inferRowMaxProd() and only needed if an updateRow()
has been called since the last clean_outcpd().
grow(self, rows, cols)
Grows the histogram to have rows rows and cols columns.
Must not have been initialized before, or already have the same 
number of columns.
If rows is smaller than the current number of rows, 
does not shrink.
Also updates the sizes of the row and column sums.
 
Parameters
----------
rows: Integer number of rows.
cols: Integer number of columns.
inferRow(self, distribution)
Computes the sumProp probability of each row given the input probability
of each column. Normalizes the distribution in each column on the fly.
 
The semantics are as follows: If the distribution is P(col|e) where e is
the evidence is col is the column, and the CPD represents P(row|col), then
this calculates sum(P(col|e) P(row|col)) = P(row|e).
 
Returns array of length equal to the number of rows.
 
Parameters
----------
distribution: Array of length equal to the number of columns.
inferRowCompat(self, distribution)
Equivalent to the category inference of zeta1.TopLevel.
Computes the max_prod (maximum component of a component-wise multiply)
between the rows of the histogram and the incoming distribution.
May be slow if the result of clean_outcpd() is not valid.
 
Returns array of length equal to the number of rows.
 
Parameters
----------
distribution: Array of length equal to the number of columns.
inferRowEvidence(self, distribution)
Computes the probability of evidence given each row from the probability
of evidence given each column.  Essentially, this just means that it sums
probabilities over (normalized) rows.  Normalizes the distribution over
each row on the fly.
 
The semantics are as follows:  If the distribution is P(e|col) where e is
evidence and col is the column, and the CPD is of P(col|row), then this
calculates sum(P(e|col) P(col|row)) = P(e|row).
 
Returns array of length equal to the number of rows.
 
Parameters
----------
distribution: Array of length equal to the number of columns.
inferRowMaxProd(self, distribution)
numColumns(self)
numRows(self)
Gets the number of rows in the histogram.
Returns Integer number of rows.
updateRow(self, row, distribution)
Add distribution to row row.
Distribution should be an array of probabilities or counts.
 
Parameters
----------
row:          Integer index of the row to add to.
              May be larger than the current number of rows, in which case
              the histogram grows.
distribution: Array of length equal to the number of columns.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Domain = class PyDomain(_Domain32)
    C++ includes: PySparseTensor.hpp
 
 
Method resolution order:
PyDomain
_Domain32
__builtin__.object

Methods defined here:
__getitem__(*args, **kwargs)
__getitem__(self, i) -> std::vector<(nta::UInt32,std::allocator<(nta::UInt32)>)>
 
std::vector<nta::UInt32> PyDomain::__getitem__(int i) const
__init__(self, *args)
__init__(self, lowerHalfSpace) -> PyDomain
__init__(self, lower, upper) -> PyDomain
 
PyDomain::PyDomain(const
TIV &lower, const TIV &upper)
__repr__ = _swig_repr(self)
__str__(*args, **kwargs)
__str__(self) -> string
 
std::string
PyDomain::__str__() const
doesInclude(*args, **kwargs)
doesInclude(self, x) -> bool
 
bool
PyDomain::doesInclude(const TIV &x) const
getDimensions(*args, **kwargs)
getDimensions(self) -> PyTensorIndex
 
PyTensorIndex
PyDomain::getDimensions() const
getLowerBound(*args, **kwargs)
getLowerBound(self) -> PyTensorIndex
 
PyTensorIndex
PyDomain::getLowerBound() const
getNumOpenDims(*args, **kwargs)
getNumOpenDims(self) -> UInt32
 
nta::UInt32
PyDomain::getNumOpenDims() const
getOpenDimensions(*args, **kwargs)
getOpenDimensions(self) -> PyTensorIndex
 
PyTensorIndex
PyDomain::getOpenDimensions() const
getSliceBounds(*args, **kwargs)
getSliceBounds(self) -> PyTensorIndex
 
PyTensorIndex
PyDomain::getSliceBounds() const
getUpperBound(*args, **kwargs)
getUpperBound(self) -> PyTensorIndex
 
PyTensorIndex
PyDomain::getUpperBound() const

Data descriptors defined here:
thisown
The membership flag

Data and other attributes defined here:
__swig_destroy__ = <built-in function delete_PyDomain>

Methods inherited from _Domain32:
empty(*args, **kwargs)
empty(self) -> bool
 
bool nta::Domain< UInt
>::empty() const
getNClosedDims(*args, **kwargs)
getNClosedDims(self) -> unsigned int
 
UInt nta::Domain<
UInt >::getNClosedDims() const
getNOpenDims(*args, **kwargs)
getNOpenDims(self) -> unsigned int
 
UInt nta::Domain<
UInt >::getNOpenDims() const
hasClosedDims(*args, **kwargs)
hasClosedDims(self) -> bool
 
bool nta::Domain<
UInt >::hasClosedDims() const
includes(*args, **kwargs)
includes(self, d) -> bool
 
bool nta::Domain< UInt
>::includes(const Domain &d) const
 
Not strict inclusion.
rank(*args, **kwargs)
rank(self) -> unsigned int
 
UInt nta::Domain< UInt
>::rank() const
size_elts(*args, **kwargs)
size_elts(self) -> unsigned int
 
UInt nta::Domain< UInt
>::size_elts() const

Data descriptors inherited from _Domain32:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class MHSampler(__builtin__.object)
     Methods defined here:
__getstate__(self)
__init__(self, logPosteriorFunc, lpArgs=None, lpKeywds=None, seed=124076833)
__setstate__(self, state)
getAcceptanceStats(self)
getBurnin(self)
getCurrentValues(self)
getMAPTable(self)
getMAPs(self)
getMaximumLikelihoodTable(self)
getMaximumLikelihoods(self)
getParameterNames(self)
getProposalTable(self)
getSampleTable(self)
getSamples(self)
propose(self, currentValues)
run(self, n, initialParams=None, printIterations=True, output=None, outputInfo=None, proposalOutput=None)
setProposal(self, key, dist)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
SparseTensor = class PySparseTensor(__builtin__.object)
    C++ includes:
PySparseTensor.hpp
 
  Methods defined here:
__add__(*args, **kwargs)
__add__(self, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::__add__(const PySparseTensor &B) const
__eq__(*args, **kwargs)
__eq__(self, B) -> bool
 
bool
PySparseTensor::__eq__(const PySparseTensor &B) const
__getitem__(self, key)
__getstate__(*args, **kwargs)
__getstate__(self) -> string
 
string
PySparseTensor::__getstate__() const
__init__(self, *args)
__init__(self, state) -> PySparseTensor
__init__(self, bounds) -> PySparseTensor
__init__(self, bounds) -> PySparseTensor
__init__(self, dense) -> PySparseTensor
 
PySparseTensor::PySparseTensor(PyObject *dense)
__mul__(*args, **kwargs)
__mul__(self, x) -> PySparseTensor
 
PySparseTensor
PySparseTensor::__mul__(const nta::Real &x) const
__ne__(*args, **kwargs)
__ne__(self, B) -> bool
 
bool
PySparseTensor::__ne__(const PySparseTensor &B) const
__neg__(*args, **kwargs)
__neg__(self) -> PySparseTensor
 
PySparseTensor
PySparseTensor::__neg__() const
__repr__ = _swig_repr(self)
__setitem__(self, key, value)
__setstate__(self, s)
__str__(*args, **kwargs)
__str__(self) -> PyObject
 
PyObject *
PySparseTensor::__str__() const
__sub__(*args, **kwargs)
__sub__(self, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::__sub__(const PySparseTensor &B) const
addSlice(*args, **kwargs)
addSlice(self, which, src, dst)
 
void
PySparseTensor::addSlice(nta::UInt32 which, nta::UInt32 src,
nta::UInt32 dst)
argmax(*args, **kwargs)
argmax(self) -> PyTensorIndex
 
PyTensorIndex
PySparseTensor::argmax() const
copy(*args, **kwargs)
copy(self) -> PySparseTensor
 
PySparseTensor
PySparseTensor::copy() const
extract(*args, **kwargs)
extract(self, dim, ind) -> PySparseTensor
 
PySparseTensor
PySparseTensor::extract(nta::UInt32 dim, const TIV &ind) const
factorAdd(*args)
factorAdd(self, dims, B) -> PySparseTensor
factorAdd(self, dims, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::factorAdd(const PyTensorIndex &dims, const
PySparseTensor &B) const
factorMultiply(*args)
factorMultiply(self, dims, B) -> PySparseTensor
factorMultiply(self, dims, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::factorMultiply(const PyTensorIndex &dims, const
PySparseTensor &B) const
get(*args)
get(self, i) -> Real
get(self, i) -> Real
 
nta::Real
PySparseTensor::get(const PyTensorIndex &i) const
getBound(*args, **kwargs)
getBound(self, dim) -> UInt32
 
nta::UInt32
PySparseTensor::getBound(const nta::UInt32 dim) const
getBounds(*args, **kwargs)
getBounds(self) -> PyTensorIndex
 
PyTensorIndex
PySparseTensor::getBounds() const
getComplementBounds(*args, **kwargs)
getComplementBounds(self, dims) -> PySparseTensor
 
PySparseTensor PySparseTensor::getComplementBounds(const PyTensorIndex
&dims) const
getNNonZeros(*args, **kwargs)
getNNonZeros(self) -> UInt32
 
nta::UInt32
PySparseTensor::getNNonZeros() const
getRank(*args, **kwargs)
getRank(self) -> UInt32
 
nta::UInt32
PySparseTensor::getRank() const
getSlice(*args, **kwargs)
getSlice(self, range) -> PySparseTensor
 
PySparseTensor
PySparseTensor::getSlice(const PyDomain &range) const
getSliceWrap(self, key)
innerProduct(*args, **kwargs)
innerProduct(self, dim1, dim2, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::innerProduct(const nta::UInt32 dim1, const nta::UInt32
dim2, const PySparseTensor &B) const
marginalize(*args)
marginalize(self) -> double
marginalize(self, dims) -> PySparseTensor
marginalize(self, dims) -> PySparseTensor
 
PySparseTensor
PySparseTensor::marginalize(const PyTensorIndex &dims) const
max(*args)
max(self) -> Real
max(self, dims) -> PySparseTensor
max(self, dims) -> PySparseTensor
 
PySparseTensor
PySparseTensor::max(const PyTensorIndex &dims) const
outerProduct(*args, **kwargs)
outerProduct(self, B) -> PySparseTensor
 
PySparseTensor
PySparseTensor::outerProduct(const PySparseTensor &B) const
reduce(*args, **kwargs)
reduce(self, dim, ind)
 
void
PySparseTensor::reduce(nta::UInt32 dim, const TIV &ind)
reshape(*args, **kwargs)
reshape(self, dims) -> PySparseTensor
 
PySparseTensor
PySparseTensor::reshape(const TIV &dims) const
resize(*args)
resize(self, dims)
resize(self, dims)
 
void
PySparseTensor::resize(const PyTensorIndex &dims)
set(*args)
set(self, i, x)
set(self, i, x)
set(self, i, x)
set(self, i, x)
 
void
PySparseTensor::set(const PyTensorIndex &i, PyObject *x)
setSlice(*args, **kwargs)
setSlice(self, range, slice)
 
void
PySparseTensor::setSlice(const PyDomain &range, const PySparseTensor
&slice)
setSliceWrap(self, key, value)
setZero(*args, **kwargs)
setZero(self, range)
 
void
PySparseTensor::setZero(const PyDomain &range)
toDense(*args, **kwargs)
toDense(self) -> PyObject
 
PyObject *
PySparseTensor::toDense() const
tolist(*args, **kwargs)
tolist(self) -> PyObject
 
PyObject*
PySparseTensor::tolist() const

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
thisown
The membership flag

Data and other attributes defined here:
__swig_destroy__ = <built-in function delete_PySparseTensor>

 
TensorIndex = class PyTensorIndex(__builtin__.object)
    C++ includes: PySparseTensor.hpp
 
  Methods defined here:
__eq__(*args)
__eq__(self, j) -> bool
__eq__(self, j) -> bool
 
bool
PyTensorIndex::__eq__(const TIV &j) const
__getitem__(*args, **kwargs)
__getitem__(self, i) -> UInt32
 
nta::UInt32
PyTensorIndex::__getitem__(int i) const
__getslice__(*args, **kwargs)
__getslice__(self, i, j) -> TIV
 
TIV
PyTensorIndex::__getslice__(int i, int j) const
__getstate__(*args, **kwargs)
__getstate__(self) -> TIV
 
TIV
PyTensorIndex::__getstate__() const
__gt__(*args, **kwargs)
__gt__(self, j) -> bool
 
bool
PyTensorIndex::__gt__(const PyTensorIndex &j) const
__init__(self, *args)
__init__(self) -> PyTensorIndex
__init__(self, x) -> PyTensorIndex
__init__(self, i) -> PyTensorIndex
__init__(self, i, j) -> PyTensorIndex
__init__(self, i, j, k) -> PyTensorIndex
__init__(self, i, j, k, l) -> PyTensorIndex
__init__(self, i) -> PyTensorIndex
__init__(self, i1, i2) -> PyTensorIndex
 
PyTensorIndex::PyTensorIndex(const PyTensorIndex &i1, const
PyTensorIndex &i2)
__len__(*args, **kwargs)
__len__(self) -> UInt32
 
nta::UInt32
PyTensorIndex::__len__() const
__lt__(*args, **kwargs)
__lt__(self, j) -> bool
__ne__(*args)
__ne__(self, j) -> bool
__ne__(self, j) -> bool
 
bool
PyTensorIndex::__ne__(const TIV &j) const
__repr__ = _swig_repr(self)
__setitem__(*args, **kwargs)
__setitem__(self, i, d)
 
void
PyTensorIndex::__setitem__(int i, nta::UInt32 d)
__setslice__(*args, **kwargs)
__setslice__(self, i, j, x)
 
void
PyTensorIndex::__setslice__(int i, int j, const TIV &x)
__setstate__(self, tup)
__str__(*args, **kwargs)
__str__(self) -> string
 
std::string
PyTensorIndex::__str__() const
asTuple(*args, **kwargs)
asTuple(self) -> TIV
 
TIV
PyTensorIndex::asTuple() const
begin(*args)
begin(self) -> UInt32
begin(self) -> UInt32
 
nta::UInt32*
PyTensorIndex::begin()
end(*args)
end(self) -> UInt32
end(self) -> UInt32
 
nta::UInt32*
PyTensorIndex::end()
size(*args, **kwargs)
size(self) -> UInt32
 
nta::UInt32
PyTensorIndex::size() const

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
thisown
The membership flag

Data and other attributes defined here:
__swig_destroy__ = <built-in function delete_PyTensorIndex>

 
Functions
       
All(sequence)
Returns true if all elements of the sequence satisfy True and x.
 
Returns A boolean value.
 
Parameters
----------
sequence: Any sequence whose elements can be evaluated as booleans.
Any(sequence)
Returns true if any element of the sequence satisfies True.
Tests much faster (30%) than bool(sum(bool(x) for x in sequence)).
 
Returns A boolean value.
 
Parameters
----------
sequence: Any sequence whose elements can be evaluated as booleans.
CMultiArgMax(*args, **kwargs)
CMultiArgMax(xIn) -> PyObject
Distribution(pos, size, counts, dtype)
Returns an array of length size and type dtype that is everywhere 0,
except in the indices listed in sequence pos.  The non-zero indices
contain a normalized distribution based on the counts.
 
Returns An array of length size and element type dtype.
 
Parameters
----------
pos:    A single integer or sequence of integers that specify 
        the position of ones to be set.
size:   The total size of the array to be returned.
counts: The number of times we have observed each index.
dtype:  The element type (compatible with NumPy array())
        of the array to be returned.
GetNTAReal()
Gets the numpy dtype of the NuPIC floating point base type,
which is used for most internal calculations.
The returned value can be used with numpy functions like
numpy.array(..., dtype=dtype) and numpy.astype(..., dtype=dtype).
GetNumpyDataType(typeName)
Gets the numpy dtype associated with a particular NuPIC 
base type name. The only supported type name is 
'NTA_Real', which returns a numpy dtype of numpy.float32.
The returned value can be used with numpy functions like
numpy.array(..., dtype=dtype) and numpy.astype(..., dtype=dtype).
Indicator(pos, size, dtype)
Returns an array of length size and type dtype that is everywhere 0,
except in the index in pos.
 
Returns an array of length size and element type dtype.
 
Parameters
----------
pos:   A single integer or that specifies
       the position of the one entry that will be set.
size:  The total size of the array to be returned.
dtype: The element type (compatible with NumPy array())
       of the array to be returned.
MultiArgMax(x)
Get tuple (actually a generator) of indices where the max value of 
array x occurs. Requires that x have a max() method, as x.max() 
(in the case of NumPy) is much faster than max(x).
For a simpler, faster argmax when there is only a single maximum entry,
or when knowing only the first index where the maximum occurs,
call argmax() on a NumPy array, nupic.bindings.iorange.WrappedVector or 
nupic.NodeInput.
 
Returns Generator with the indices where the max value occurs.
 
Parameters
----------
x: Any sequence that has a max() method.
MultiIndicator(pos, size, dtype)
Returns an array of length size and type dtype that is everywhere 0,
except in the indices listed in sequence pos.
 
Returns An array of length size and element type dtype.
 
Parameters
----------
pos:   A single integer or sequence of integers that specify 
       the position of ones to be set.
size:  The total size of the array to be returned.
dtype: The element type (compatible with NumPy array())
       of the array to be returned.
Product(sequence)
Returns the product of the elements of the sequence.
Use numpy.prod() if the sequence is an array, as it will be faster.
Remember that the product of many numbers may rapidly overflow or
underflow the numeric precision of the computer.
Use a sum of the logs of the sequence elements instead when precision
should be maintained.
 
Returns A single value that is the product of all the sequence elements.
 
Parameters
----------
sequence: Any sequence whose elements can be multiplied by their
          neighbors.
SparseMatrix(*args, **keywords)
bitstringToArray(*args, **kwargs)
bitstringToArray(bitstring) -> PyObject
combinations(n, c)
cross(*sequences)
From: http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-19-sect-9.html
cross_list(*sequences)
From: http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-19-sect-9.html
lnsum0(lx)
Accepts log-values as input, exponentiates them, sums down the rows 
(first dimension), normalizes, then converts the sum back to 
log-space and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.
lscsum(lx, epsilon=None)
Accepts log-values as input, exponentiates them, computes the sum, 
then converts the sum back to log-space and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.
lscsum0(lx)
Accepts log-values as input, exponentiates them, sums down the rows 
(first dimension), then converts the sum back to log-space and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.
normalize(lx)
Accepts log-values as input, exponentiates them, 
normalizes and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.
nsum0(lx)
Accepts log-values as input, exponentiates them, sums down the rows 
(first dimension), normalizes and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.
pickByDistribution(distribution, r=None)
Pick a value according to the provided distribution.
 
@param distribution -- Probability distribution. Need not be normalized.
@param r -- Instance of random.Random. Uses the system instance if one is
  not provided.
 
Example:
pickByDistribution([.2, .1])
returns 0 two thirds of the time and 1 one third of the time.

 
Data
        __all__ = ['bitstringToArray', 'GetNTAReal', 'GetNumpyDataType', 'CMultiArgMax', 'SparseMatrix', 'SparseTensor', 'TensorIndex', 'Domain', 'Indicator', 'MultiArgMax', 'Any', 'All', 'Product', 'MultiIndicator', 'Distribution', 'ConditionalProbabilityTable2D', 'pickByDistribution', 'cross', 'cross_list', 'combinations', ...]