pycmtensor.functions#

PyCMTensor functions module

Module Contents#

pycmtensor.functions.exp_mov_average(batch_avg: aesara.tensor.var.TensorVariable, moving_avg: aesara.tensor.var.TensorVariable, alpha: float = 0.1)[source]#

Calculates the exponential moving average (EMA) of a new minibatch

Parameters:
  • batch_avg (TensorVariable) – the new batch value of the mean

  • moving_avg (TensorVariable) – the moving value of the accumulated mean

  • alpha (float) – the moving average factor of the batch mean

Returns:

the new moving average

Return type:

TensorVariable

Note

The moving average will decay by the difference between the existing value and the new value multiplied by the moving average factor. A higher alpha value results in faster changing moving average.

Formula:

\[x_{EMA} = \alpha * x_t + x_{EMA} * (1-\alpha)\]
pycmtensor.functions.logit(utility: Union[list, tuple, aesara.tensor.var.TensorVariable], avail: Union[list, tuple, aesara.tensor.var.TensorVariable] = None)[source]#

Computes the Logit function, with availability conditions.

Parameters:
  • utility (list, tuple, TensorVariable) – list of \(M\) utility equations

  • avail (list, tuple, TensorVariable) – list of \(M\) availability conditions, if no availability conditions are provided, defaults to 1 for all availabilities.

Returns:

A NxM matrix of probabilities.

Return type:

TensorVariable

Note

The 0-th dimension is the numbering of alternatives.

pycmtensor.functions.rmse(y_hat: aesara.tensor.var.TensorVariable, y: aesara.tensor.var.TensorVariable)[source]#

Computes the root mean squared error (RMSE) between pairs of observations

Parameters:
  • y_hat (TensorVariable) – model estimated values

  • y (TensorVariable) – ground truth values

Returns:

symbolic scalar representation of the rmse

Return type:

TensorVariable

Note

Tensor is flattened to an N-vector if the input args are \(N\times 1\) matrices.

Formula:

\[RMSE = \sqrt{\frac{1}{N}\sum_{i=1}^N(\hat{y}_i-y_i)^2}\]
pycmtensor.functions.mae(y_hat: aesara.tensor.var.TensorVariable, y: aesara.tensor.var.TensorVariable)[source]#

Computes the mean absolute error (MAE) between pairs of observations

Parameters:
  • y_hat (TensorVariable) – model estimated values

  • y (TensorVariable) – ground truth values

Returns:

symbolic scalar representation of the mae

Return type:

TensorVariable

Note

Tensor is flattened to an N-vector if the input args are \(N\times 1\) matrices.

Formula:

\[MAE = \frac{\sum_{i=1}^N|\hat{y}_i-y_i|}{N}\]
pycmtensor.functions.kl_divergence(p: aesara.tensor.var.TensorVariable, q: aesara.tensor.var.TensorVariable)[source]#

Computes the KL divergence loss between discrete distributions p and q.

Parameters:
  • p (TensorVariable) – model output probabilities

  • q (TensorVariable) – ground truth probabilities

Returns:

a symbolic representation of the KL loss with

Return type:

TensorVariable

Note

Formula:

\[\begin{split}L = \begin{cases} \sum_{i=1}^N (p_i * log(p_i/q_i)) & p>0\\ 0 & p<=0 \end{cases}\end{split}\]
pycmtensor.functions.kl_multivar_norm(m0, v0, m1, v1, epsilon=1e-06)[source]#

Computes the KL divergence loss between two multivariate normal distributions.

Parameters:
  • m0 – mean vector of the first Normal m.v. distribution \(N_0\)

  • v0 – (co-)variance matrix of the first Normal m.v. distribution \(N_0\)

  • m1 – mean vector of the second Normal m.v. distribution \(N_1\)

  • v1 – (co-)variance of the second Normal m.v. distribution \(N_1\)

  • epsilon (float) – small value to prevent divide-by-zero error

Note

k = dimension of the distribution.

Formula:

\[D_{KL}(N_0||N_1) = 0.5 * \Big(\ln\big(\frac{|v_1|}{|v_0|}\big) + trace(v_1^{-1} v_0) + (m_1-m_0)^T v_1^{-1} (m_1-m_0) - k\Big)\]

In variational inference, the kl divergence is the relative entropy between a diagonal multivariate Normal and a standard Normal distribution, N(0, 1), therefore, for VI, m1=aet.constant(0), v1=aet.constant(1)

For two univariate distributions, dimensions of m0,m1,v0,v1 = 0

pycmtensor.functions.errors(prob: aesara.tensor.var.TensorVariable, y: aesara.tensor.var.TensorVariable)[source]#

Symbolic representation of the discrete prediction as a percentage error.

Parameters:
  • prob (TensorVariable) – matrix describing the choice probabilites

  • y (TensorVariable) – the TensorVariable referencing the choice column

Returns:

the mean prediction error over the input y

Return type:

TensorVariable

pycmtensor.functions.hessians(ll: aesara.tensor.var.TensorVariable, params: list[pycmtensor.expressions.Beta])[source]#

Symbolic representation of the Hessian matrix given the log likelihood.

Parameters:
  • ll (TensorVariable) – the loglikelihood to compute the gradients over

  • params (list) – list of params to compute the gradients over

Returns:

the Hessian matrix

Return type:

TensorVariable

Note

Parameters with status=1 are ignored.

pycmtensor.functions.bhhh(ll: aesara.tensor.var.TensorVariable, params: list[pycmtensor.expressions.Beta])[source]#

Symbolic representation of the Berndt-Hall-Hall-Hausman (BHHH) algorithm given the log likelihood.

Parameters:
  • ll (TensorVariable) – the loglikelihood to compute the gradients over

  • params (list) – list of params to compute the gradients over

Returns:

the outer product of the gradient with ndim=2

Return type:

TensorVariable

Note

Parameters with status=1 are ignored.

pycmtensor.functions.gnorm(cost: aesara.tensor.var.TensorVariable, params: list[pycmtensor.expressions.Beta])[source]#

Symbolic representation of the gradient norm given the cost.

Parameters:
  • cost (TensorVariable) – the cost to compute the gradients over

  • params (list) – list of params to compute the gradients over

Returns:

the gradient norm value

Return type:

TensorVariable

Note

Parameters with status=1 are ignored.