This submodule contains various mathematical functions. Most of them are imported directly from theano.tensor (see there for more details). Doing any kind of math with PyMC3 random variables, or defining custom likelihoods or priors requires you to use these theano expressions rather than NumPy or Python code.

dot(a, b) Computes the dot product of two variables.
constant(x[, name, ndim, dtype])
flatten(x[, outdim]) Reshapes the variable x by keeping the first outdim-1 dimension size(s) of x the same, and making the last dimension size of x equal to the multiplication of its remaining dimension size(s).
zeros_like(model[, dtype, opt]) equivalent of numpy.zeros_like
ones_like(model[, dtype, opt]) equivalent of numpy.ones_like
stack(*tensors, **kwargs) Stack tensors in sequence on given axis (default is 0).
concatenate(tensor_list[, axis]) Alias for `join`(axis, *tensor_list).
sum(input[, axis, dtype, keepdims, acc_dtype]) Computes the sum along the given axis(es) of a tensor input.
prod(input[, axis, dtype, keepdims, ...]) Computes the product along the given axis(es) of a tensor input.
lt a < b
gt a > b
le a <= b
ge a >= b
eq a == b
neq a != b
switch if cond then ift else iff
clip Clip x to be between min and max.
where if cond then ift else iff
and_ bitwise a & b
or_ bitwise a | b
abs_ |`a`|
exp e^`a`
log base e logarithm of a
cos cosine of a
sin sine of a
tan tangent of a
cosh hyperbolic cosine of a
sinh hyperbolic sine of a
tanh hyperbolic tangent of a
sqr square of a
sqrt square root of a
erf error function
erfinv inverse error function
dot(a, b) Computes the dot product of two variables.
maximum elemwise maximum. See max for the maximum in one tensor
minimum elemwise minimum. See min for the minimum in one tensor
sgn sign of a
ceil ceiling of a
floor floor of a
det Matrix determinant.
matrix_inverse Computes the inverse of a matrix \(A\).
extract_diag Return the diagonal of a matrix.
matrix_dot(*args) Shorthand for product between several dots.
trace(X) Returns the sum of diagonal elements of matrix X.
sigmoid Generalizes a scalar op to tensors.
logsumexp(x[, axis])
invlogit(x[, eps])
class pymc3.math.BatchedDiag

Fast BatchedDiag allocation

class pymc3.math.LogDet

Compute the logarithm of the absolute determinant of a square matrix M, log(abs(det(M))) on the CPU. Avoids det(M) overflow/ underflow.


Once PR #3959 ( by harpone is merged, this must be removed.

pymc3.math.block_diagonal(matrices, sparse=False, format='csr')

See scipy.sparse.block_diag or scipy.linalg.block_diag for reference

  • matrices (tensors) –
  • format (str (default 'csr')) – must be one of: ‘csr’, ‘csc’
  • sparse (bool (default False)) – if True return sparse format


pymc3.math.expand_packed_triangular(n, packed, lower=True, diagonal_only=False)

Convert a packed triangular matrix into a two dimensional array.

Triangular matrices can be stored with better space efficiancy by storing the non-zero values in a one-dimensional array. We number the elements by row like this (for lower or upper triangular matrices):

[[0 - - -]     [[0 1 2 3]
 [1 2 - -]      [- 4 5 6]
 [3 4 5 -]      [- - 7 8]
 [6 7 8 9]]     [- - - 9]
  • n (int) – The number of rows of the triangular matrix.
  • packed (theano.vector) – The matrix in packed format.
  • lower (bool, default=True) – If true, assume that the matrix is lower triangular.
  • diagonal_only (bool) – If true, return only the diagonal of the matrix.
pymc3.math.tround(*args, **kwargs)

Temporary function to silence round warning in Theano. Please remove when the warning disappears.