NestedLogit.make_P_nest#

NestedLogit.make_P_nest(U, W, betas_fixed, lambdas_nests, level)[source]#

Calculate the probability of choosing a nest.

This function collates the exponentiated inclusive value (exp_W_nest) for each alternative group (nest), sums them, and then normalizes across nests to obtain the probability of selecting a nest. The within-nest conditional probabilities (P_y_given) are computed in the make_exp_nest method.

This is used within the PyMC model to construct the tree-based aggregation of utilities, where lower-level nodes (alternative-specific utilities) are passed upward in the tree structure to compute nest-level and top-level choice probabilities.

Parameters:
UTensorVariable

Tensor of systematic utilities with shape (n_obs, n_alternatives).

WTensorVariable | None

Fixed covariates design matrix (if used), else None.

betas_fixedTensorVariable

Alternative-specific coefficients for the fixed covariates.

lambdas_nestsTensorVariable

A Beta random variable for each of the nests

levelstr

Which nesting level to compute (“top” or “mid”), indicating whether to compute probabilities for top-level or mid-level nests.

Returns:
conditional_probsdict[str, dict[str, TensorVariable]]

Dictionary for each nest containing: - “exp”: the exponentiated inclusive value of the nest. - “P_y_given”: conditional choice probabilities within the nest.

nest_probsdict[str, TensorVariable]

Dictionary mapping each nest to its overall selection probability.

Raises:
ValueError

If the nesting structure is invalid or the nest name is not found.