import matplotlib
import matplotlib.pyplot as plot
plot.rcParams['figure.figsize'] = [6, 4]
plot.rcParams['figure.dpi'] = 100
plot.rcParams['font.family'] = "Serif"
plot.rcParams['axes.spines.top'] = False
plot.rcParams['axes.spines.right'] = False
import json
import math
import numpy
import scipy.stats as stats
import stan
# Needed to run pystan through a jupyter kernel
import nest_asyncio
nest_asyncio.apply()Discrete Choice Modeling, Part II: Coupled Utility Models
More modeling technique chapters are available on my website.
In Part I, we started our exploration of discrete choice models with independent utilities models, and the rigid substitution patterns that they imply. One of the most productive ways to introduce more sophisticated substitution patterns in discrete choice models, in particular to transcend independence from irrelevant alternatives, is to couple the components of the probabilistic utility model together. Probabilistic couplings between alternative utilities allow some alternatives to be better substitutes for each other than others.
Engineering models that yield specific substitution patterns, however, is a challenging task. In this chapter, we’ll review some of the more common discrete choice models that can be derived in closed from from coupled utility models.
1 Nested Gumbel Model
The nested Gumbel model (Daly and Zachary 1976; Williams 1977; Ben-Akiva and Lerman 1977) partitions a choice set into subsets known as nests, with the utilities of alternatives in the same nest more strongly coupled together than the utilities of alternatives across different nests.
More formally, consider partitioning a given choice set into K disjoint subsets. I will denote each subset as \mathsf{b}_{k} so that if j \in \mathsf{b}_{k} then the jth alternative is in the kth group. When implementing these models in practice, it will also useful to be able to quickly refer to the unique nest containing the jth alternative. Here, I will use the notation k(j).
Given a partition of the choice set, the nested Gumbel model is most easily defined not with a joint probability density function, but rather with a joint cumulative distribution function, \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j \in \mathsf{b}_{k}} e^{ - \frac{1}{\lambda_{k}} \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right)^{ \lambda_{k} } \right). Here, each \lambda_{k} is a positive, real-valued variable that quantifies how strongly the utilities of alternatives within the kth nest are coupled together.
When \lambda_{k} = 1, the behavior of the nested Gumbel model in the kth nest reduces to the behavior of the independent Gumbel model, at least up to the scale parameter. As \lambda_{k} decreases towards zero, the utilities of alternatives in the kth nest become more strongly coupled. In other words, larger values of the \lambda_{k} corresponding to weaker couplings.
Values of \lambda_{k} larger than one require a bit more care. They can lead to self-consistent model configurations, but not always. Moreover, when they do they are somewhat awkward to interpret. To simplify the discussion here, I will consider only 0 \le \lambda_{k} \le 1.
In theory, we could differentiate this joint cumulative distribution function with respect to each argument to derive a corresponding joint probability density function. The results, however, are neither particularly insightful nor needed to derive choice probabilities. Consequently, we will not consider the nested Gumbel probability density function here.
1.1 Nested Gumbel As A Generalization of Independent Gumbel
Interestingly, the nested Gumbel model can be derived as a particular generalization of the independent Gumbel model. Recall that the cumulative distribution function of the independent Gumbel model is given by \Pi( u_{n1}, \ldots, u_{nJ}) = \exp \left( - \sum_{j = 1}^{J} e^{ - \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right). Given a nesting structure, we can always split the sum in the exponential up into a sum over nests and then a sum over the alternatives in each nest, \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right) \right).
At this point, we can embrace a vector space perspective and recognize that the inner sums can be interpreted as 1-norms (Horn and Johnson 1985) over each nest, \Vert x_{1}, \ldots, x_{I} \Vert_{1} = \sum_{i = 1}^{I} x_{i}. These 1-norms can then be immediately generalized to p-norms, \Vert x_{1}, \ldots, x_{I} \Vert_{p} = \left( \sum_{i = 1}^{I} x_{i}^{p} \right)^{\frac{1}{p}}.
When p = 1, all of elements contribute to the norm equally. As p increases, however, the norm is increasingly dominated by the one element with the largest value. In the limit p \rightarrow \infty, the p-norm is completely determined by the largest value, \Vert x_{1}, \ldots, x_{I} \Vert_{ \infty } = \max( x_{1}, \ldots, x_{I} ).
Allowing p to vary from one nest to another defines a generalized utility model \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j \in \mathsf{b}_{k}} e^{ - p_{k} \, \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right)^{ \frac{1}{p_{k}} } \right). Taking \lambda_{k} = \frac{1}{p_{k}} then gives the nested Gumbel model.
Even if we don’t derive the nested Gumbel model in this way, the connection to p-norms provides intuition for the behavior in each nest. When \lambda_{k} = 1, for example, all of the alternatives in the nest contribute equally. As \lambda_{k} decreases, however, the one alternative with the largest utility residual u_{nj} - \mu_{nj} begins to dominate (Figure 1).
1.2 Choice Probabilities
Following our general construction, the choice probabilities are given by differentiating the joint cumulative distribution function, evaluating all arguments on the target utility, and then integrating. A few pages of calculations, which I have reserved for Appendix A.1.1, eventually yields \begin{align*} q_{nj} &= \int_{-\infty}^{+\infty} \mathrm{d} u \, \frac{ \partial \Pi }{ \partial x_{j} } ( u, \ldots, u ) \\ &= \frac{ e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj} }{ \sigma_{n} } } \, \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } }. \end{align*}
1.2.1 Conditional and Marginal Choice Probabilities
The nested Gumbel choice probabilities are, by construction, compatible with the nesting structure.
For example, the marginal probability of choosing any alternative from the kth nest simplifies to \begin{align*} q_{nk} &= \sum_{j'' \in \mathsf{b}_{k} } q_{nj''} \\ &= \sum_{j'' \in \mathsf{b}_{k} } \frac{ e^{ \frac{1}{ \lambda_{k(j'')} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \, \left( \sum_{j' \in \mathsf{b}_{k(j')}} e^{ \frac{1}{ \lambda_{k(j'')} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j'')} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } \\ &= \sum_{j'' \in \mathsf{b}_{k} } \frac{ e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \, \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } \\ &= \frac{ \left( \sum_{j'' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right) \, \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } \\ &= \frac{ \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } }. \end{align*}
Similarly, the conditional probability of choosing the jth alternative from the k(j)th nest is then \begin{align*} q_{ nj \mid k(j) } &= \frac{ q_{nj} }{ q_{nk(j)} } \\ &= \frac{ e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj} }{ \sigma_{n} } } \, \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } \frac{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } }{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} } } \\ &= \frac{ e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj} }{ \sigma_{n} } } \, \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} - 1 } }{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} } } \\ &= \frac{ e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj} }{ \sigma_{n} } } }{ \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } }. \end{align*}
Because the marginal and conditional choice probabilities are simpler than the joint choice probabilities, the conditional decomposition q_{nj} = q_{ nj \mid k(j) } \, q_{ nk(j) } are useful for interpreting the choice probabilities, analyzing their properties, and implementing them in practice.
1.2.2 Efficient Calculation
The conditional decomposition of nested Gumbel choice probabilities into marginal and conditional choice probabilities is particularly useful for practical calculations.
Within each nest, the conditional choice probabilities q_{ nj \mid k(j) } = \frac{ e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj} }{ \sigma_{n} } } }{ \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} } } } can all be evaluated at the same time by passing the scaled baseline utilities, \frac{1}{ \lambda_{k(j)} } \frac{ \mu_{nj'} }{ \sigma_{n} }, into a softmax function, (q_{nj_{1} \mid k}, \ldots, q_{nj_{J_{k}} \mid k} ) = \mathrm{softmax} \left( \frac{1}{ \lambda_{k} } \frac{ \mu_{nj_{1}} }{ \sigma_{n} } , \ldots, \frac{1}{ \lambda_{k} } \frac{ \mu_{nj_{J_{k}}} }{ \sigma_{n} } \right) for ( j_{1}, \ldots, j_{J_{k}} ) \in \mathsf{b}_{k}.
Moreover, the marginal choice probabilities q_{nk} = \frac{ \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{1}{ \lambda_{k'} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } can be computed for all nests at the same time by passing the somewhat awkward nest-wise terms \begin{align*} C_{k} &= \log \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k} } \\ &= \lambda_{k} \, \log \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{1}{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right). \end{align*} to another softmax function.
These inputs can themselves be efficiently computed with the use of a log-sum-exp function, \text{log-sum-exp}(x_{1}, \ldots, x_{L}) = \log \sum_{l = 1}^{L} e^{x_{l}}, which allows us to write C_{k} = \lambda_{k} \, \text{log-sum-exp} \left( \frac{1}{ \lambda_{k} } \frac{ \mu_{nj_{1}} }{ \sigma_{n} }, \ldots, \frac{1}{ \lambda_{k} } \frac{ \mu_{nj_{J_{k}}} }{ \sigma_{n} } \right) for ( j_{1}, \ldots, j_{J_{k}} ) \in \mathsf{b}_{k}.
1.2.3 Redundancies
Like the independent Gumbel model, the nested Gumbel model suffers from structural redundancies. Fortunately, these redundancies are straightforward to study by investigating the conditional and marginal choice probabilities.
Up to the scaling by \lambda_{k}, the conditional choice probabilities within each nest are identical to the choice probabilities that would be given by an independent Gumbel model defined over just that nest. Consequently, the baseline utilities within each nest are subject to the same translation redundancy.
Independently translating the baseline utilities locally within each nest, however, does not result in the same marginal probabilities. Instead, there is only a global translation redundancy across all of the baseline utilities. Writing \nu_{nj} = \frac{ \mu_{nj} }{ \sigma_{n} }, we have \begin{align*} q_{ nk(j) } ( \nu_{n1}, \ldots, \nu_{nJ} ) &= \frac{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj'} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &= \frac{ e^{\alpha} }{ e^{\alpha} } \frac{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj'} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &= \frac{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \alpha }{ \lambda_{k(j)} } } e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } }{ \sum_{k' = 1}^{K} e^{ \alpha } \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{ \alpha }{ \lambda_{k'} } } e^{ \frac{ \nu_{nj'} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &= \frac{ \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj'} + \alpha }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } }{ \sum_{k' = 1}^{K} e^{ \alpha } \left( \sum_{j' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj'} + \alpha }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &= q_{nk(j)} ( \nu_{n1} + \alpha, \ldots, \nu_{nJ} + \alpha) \end{align*}
In other words, we don’t have to anchor an alternative in each nest to avoid redundancy. Anchoring any one alternative ensures that all of the other alternatives are well-behaved, no matter their nesting. This means that, conveniently, the same relative baseline utility and relative scale approach that we considered in Section 3.2 of Part I is sufficient for the nested Gumbel model as well!
1.3 Substitution Patterns
The nested Gumbel model couples the alternative utilities within each nest together, making alternatives within a nest better substitutes for each other than alternatives in other nests. These substitution patterns can, as always, be quantified in various ways.
1.3.1 Ratios of Choice Probabilities
In a nested Gumbel model, the ratios of choice probabilities for any two alternatives is given by \begin{align*} \frac{ q_{nj_{1}} }{ q_{nj_{2}} } &= \frac{ q_{ nj_{1} \mid k(j_{1}) } \, q_{ nk(j_{1}) } }{ q_{ nj_{2} \mid k(j_{2}) } \, q_{ nk(j_{2}) } } \\ &= \frac{ q_{ nj_{1} \mid k(j_{1}) } }{ q_{ nj_{2} \mid k(j_{2}) } } \, \frac{ q_{ nk(j_{1}) } }{ q_{ nk(j_{2}) } }. \end{align*}
If two alternatives are in the same nest, k(j_{1}) = k(j_{2}) = k, then the marginal probabilities cancel, \begin{align*} \frac{ q_{nj_{1}} }{ q_{nj_{2}} } &= \frac{ q_{ nj_{1} \mid k(j_{1}) } \, q_{ nk(j_{1}) } } { q_{ nj_{2} \mid k(j_{2}) } \, q_{ nk(j_{2}) } } \\ &= \frac{ q_{ nj_{1} \mid k } \, q_{ nk } } { q_{ nj_{2} \mid k } \, q_{ nk } } \\ &= \frac{ q_{ nj_{1} \mid k } } { q_{ nj_{2} \mid k } }, \end{align*} leaving only the ratio of conditonal probabilities.
This ratio, however, depends on only the two corresponding baseline utilities, \begin{align*} \frac{ q_{nj_{1}} }{ q_{nj_{2}} } &= \frac{ q_{ nj_{1} \mid k } }{ q_{ nj_{2} \mid k } } \\ &= \frac{ e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj_{1}} }{ \sigma_{n} } } \, }{ \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj'} }{ \sigma_{n} } } } \frac{ \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } }{ e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj_{2}} }{ \sigma_{n} } } \, } \\ &= \exp \left( \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj_{1}} - \mu_{nj_{2}} }{ \sigma_{n} } \right). \end{align*} In other words, the nested Gumbel model exhibits independent of irrelevant alternatives within each nest.
When comparing alternatives across nests, however, the marginal probabilities no longer cancel. Instead we have \begin{align*} \frac{ q_{nj_{1}} }{ q_{nj_{2}} } &= \frac{ q_{ nj_{1} \mid k(j_{1}) } }{ q_{ nj_{2} \mid k(j_{2}) } } \, \frac{ q_{ nk(j_{1}) } }{ q_{ nk(j_{2}) } } \\ &= \frac{ e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj_{1}} }{ \sigma_{n} } } \, }{ e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj_{2}} }{ \sigma_{n} } } \, } \frac{ \left( \sum_{j' \in \mathsf{b}_{k(j_{1})}} e^{ \frac{ 1 }{ \lambda_{k(j_{1})} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j_{1})} } }{ \left( \sum_{j' \in \mathsf{b}_{k(j_{2})}} e^{ \frac{ 1 }{ \lambda_{k(j_{2})} } \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{ \lambda_{k(j_{2})} } }. \end{align*} The ratio of the choice probabilities now depends on all of the baseline utilities across the two nests. Whenever the behavior of any alternatives in either of the nests change, so too will the substitution pattern between j_{1} and j_{2}.
Why does the coupling of the nested Gumbel model allow alternatives in one nest to be better substitutes for each other than any alternative in another nest? If both \lambda_{k(j_{1})} and \lambda_{k(j_{2})} are strictly less than one, and if \sum_{j' \in \mathsf{b}_{k(j_{1})}} e^{ \frac{ 1 }{ \lambda_{k(j_{1})} } \frac{ \mu_{nj'} }{ \sigma_{n} } } decreases relative \sum_{j' \in \mathsf{b}_{k(j_{2})}} e^{ \frac{ 1 }{ \lambda_{k(j_{2})} } \frac{ \mu_{nj'} }{ \sigma_{n} }, } then the choice probability q_{nj_{1}} will increase relative to q_{nj_{2}}, and vice versa.
Choice probability removed from one alternative within a nest is preferentially transferred to the choice probabilities of the other alternatives in that same nest. Taking this to the limit, removing an item from \mathsf{b}_{k(j_{1})}, but keeping everything else fixed, decreases \sum_{j' \in \mathsf{b}_{k(j_{1})}} e^{ \frac{ 1 }{ \lambda_{k(j_{1})} } \frac{ \mu_{nj'} }{ \sigma_{n} } } relative to the corresponding sum for the k(j_{2}) nest. This in turn increases q_{nj_{1}} relative to q_{nj_{2}} (Figure 2).
1.3.2 Elasticities
These substitution patterns are also mirrored in the behavior of the elasticities. That said, the analytic form of the elasticities become substantially more ungainly. After a long calculation Appendix A.1.2, we have \begin{align*} \epsilon_{njj'} &= \frac{ \partial q_{nj} }{ \partial \mu_{nj'} } \\ &= \frac{ \mu_{nj'} }{ q_{nj} } \frac{ \partial }{ \partial \mu_{nj'} } \left[ \frac{ e^{ \frac{ 1 }{ \lambda_{k(j)} } \frac{ \mu_{nj} } { \sigma_{n} } } \, \left( \sum_{j'' \in \mathsf{b}_{k(j)} } e^{ \frac{ 1 }{ \lambda_{k(j)} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ 1 }{ \lambda_{k'} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right)^{ \lambda_{k'} } } \right] \\ &= \Bigg( \quad \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \\ &\hspace{7mm} + \delta_{k(j') k(j) } \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \\ &\hspace{7mm} - q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ 1 }{ \lambda_{k(j')} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \hspace{28mm} \Bigg) \frac{ \mu_{nj'} }{ \sigma_{n} }, \end{align*} where \delta denotes the Kronecker delta function, \delta_{ii'} = \left\{ \begin{array}{ll} 0, & i \ne i' \\ 1, & i = i' \end{array} \right. .
These sensitivities allow us to identify when independence from irrelevant alternatives arises by comparing the elasticities defined by three distinct alternatives, j_{1}, j_{2}, and j', in different nesting scenarios.
For instance, if j_{1} and j' are in different nests, k(j_{1}) \ne k(j'), then only the third term of the elasticity remains, \epsilon_{nj_{1}j'} = \frac{ \mu_{nj'} \, q_{nj'} }{ \sigma_{n} } \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ 1 }{ \lambda_{k(j')} } \frac{ \mu_{nj''} }{ \sigma_{n} } }. This remaining term, however, is independent of j_{1} and its nest. Consequently, if j_{2} is also in a different nest than j', but not necessarily in a different nest than j_{1}, k(j_{1}) \ne k(j') \text{ and } k(j_{2}) \ne k(j'), then the elasticities are equal, \epsilon_{nj_{1}j'} = \epsilon_{nj_{2}j'}. In this case, the ratio of q_{nj_{1}} / q_{nj_{2}} will be invariant to changes in the behavior of the j'th alternative.
At the same time, if all three alternatives are in the same nest, k( j_{1} ) = k( j_{2} ) = k( j' ) \equiv k then the elasticities become \begin{align*} \epsilon_{nj_{1}j'} &= \frac{ \mu_{nj'} }{ \sigma_{n} } \left( q_{ nj' \mid k } \, \frac{\lambda_{k} - 1}{ \lambda_{k} } + q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k}} e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right) \\ \epsilon_{nj_{2}j'} &= \frac{ \mu_{nj'} \, q_{nj'} }{ \sigma_{n} } \left( q_{ nj' \mid k } \, \frac{\lambda_{k} - 1}{ \lambda_{k} } + q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ 1 }{ \lambda_{k} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right). \end{align*} The two elasticities are exactly equal once again, and the ratio of q_{nj_{1}} / q_{nj_{2}} will be invariant to changes in the behavior of the j'th alternative.
This behavior changes, however, when j' is in the same nest as j_{1} or j_{2}. If, for instance, k( j' ) = k( j_{1} ) \ne k( j_{2} ), then \epsilon_{nj_{1}j'} = \frac{ \mu_{nj'} }{ \sigma_{n} } \left( q_{ nj' \mid k(j_{1}) } \, \frac{\lambda_{k(j_{1})} - 1}{ \lambda_{k(j_{1})} } + q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ 1 }{ \lambda_{k(j')} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right) but \epsilon_{nj_{2}j'} = \frac{ \mu_{nj'} }{ \sigma_{n} } \left( q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ 1 }{ \lambda_{k(j')} } \frac{ \mu_{nj''} }{ \sigma_{n} } } \right) Here, the elasticities are not equal, and changes in the behavior of the j' alternative will influence q_{nj_{1}} / q_{nj_{2}}.
Altogether, these results imply that the nested Gumbel model exhibits independence from irrelevant alternatives within each individual nest. Any changes to alternatives outside of the nests containing any alternatives being considered do not change the corresponding substitution patterns.
2 Beyond The Nested Gumbel Model
The nested Gumbel model is not the only coupled utility model that can be readily implemented in practice. Indeed, the nested Gumbel model can be generalized in a variety of ways without sacrificing practical feasibility (Train 2009).
2.1 Generalized Nested Gumbel Model
One way to generalize the nested Gumbel model, without compromising analytic tractability, is to remove the constraint that the nests must be disjoint. In particular, we can allow each alternative to have only a partial association with each nest.
The partial association of an alternative to each nest is quantified by a simplex of parameters ( \alpha_{j1}, \ldots, \alpha_{jK} ) that satisfies the constraints 0 \le \alpha_{jk} \le 1, \quad \sum_{k = 1}^{K} \alpha_{jk} = 1. If \alpha_{jk} is zero, then the jth alternative is completely excluded from the kth nest, \mathsf{b}_{k}. Any structural exclusions can be encoded into a model by fixing the corresponding \alpha_{jk} to zero.
Partial associations allow us to generalize the nested Gumbel model with the joint cumulative distribution function \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j \in \mathsf{b}_{k}} \left( \alpha_{jk} \, e^{ -\frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right). If for each alternative j we have \alpha_{jk} = 1 for one, and only one, k, then the nests become disjoint and this model reduces to the nested Gumbel model.
Deriving the choice probabilities becomes more straightforward if we first define the subset of nests that contain a given alternative, \mathsf{a}_{j}. In other words, k \in \mathsf{a}_{j} if and only if j \in \mathsf{b}_{k}.
After a long calculation Appendix A.2, this organization allows us to compactly write the choice probabilities as \begin{align*} q_{nj} &= \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, \frac{ \partial \Pi }{ \partial x_{j} } ( u_{nj}, \ldots, u_{nj} ) \\ &= \frac{ \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \, }{ \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } }. \end{align*}
Similar to the choice probabilities in the nested Gumbel model, the choice probabilities in the generalized nested Gumbel model admit a convenient conditional decomposition, \begin{align*} q_{nj} &= \frac{ \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \, }{ \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } } \\ &= \sum_{k \in \mathsf{a}_{j} } \frac{ \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \, }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k'} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k'}}} \right)^{ \lambda_{k'} } } \\ &= \sum_{k \in \mathsf{a}_{j} } \frac{ \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} }{ \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} } \frac{ \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k'} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k'}}} \right)^{ \lambda_{k'} } } \\ &\equiv \sum_{k \in \mathsf{a}_{j} } q_{nj \mid k } \, q_{nk}. \end{align*}
Here q_{nj \mid \mathsf{b}_{k} } = \frac{ \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} }{ \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} } quantifies the conditional probabilities of the alternatives within each nest, while q_{nk} = \frac{ \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } }{ \sum_{k' = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k'} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k'}}} \right)^{ \lambda_{k'} } } quantifies the marginal probability of each nest.
With some careful manipulation, we can also write these probabilities as the output of softmax functions, facilitating practical implementations. For instance, we have \begin{align*} q_{nj \mid \mathsf{b}_{k} } &= \frac{ \left( \alpha_{jk} \, e^{ \frac{ \mu_{nj} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} }{ \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \frac{ \mu_{nj'} }{ \sigma_{n} } } \right)^{\frac{1}{\lambda_{k}}} } \\ &= \frac{ \exp \left( \frac{1}{\lambda_{k}} \, \left( \log \alpha_{jk} + \frac{ \mu_{nj} }{ \sigma_{n} } \right) \right) }{ \sum_{j' \in \mathsf{b}_{k}} \exp \left( \frac{1}{\lambda_{k}} \, \left( \log \alpha_{j'k} + \frac{ \mu_{nj} }{ \sigma_{n} } \right) \right) } \end{align*} with (q_{n1 \mid k}, \ldots, q_{nJ \mid k} ) = \mathrm{softmax} \left( \frac{1}{\lambda_{k}} \, \left( \log \alpha_{1k} + \frac{ \mu_{n1} }{ \sigma_{n} } \right), \ldots, \frac{1}{\lambda_{k}} \, \left( \log \alpha_{Jk} + \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \right).
The substitution patterns exhibited by these generalized choice probabilities are, unsurprisingly, more complicated than those of the nested Gumbel model. Specifically, the summation over all nests that contain a given alternative immediately obstructs the cancellation needed for the ratios of choice probabilities in the same nest to be independent of the behavior of alternatives in other nests. I will leave the exploration of these substitution patterns as an exercise for the enthusiastic reader…
2.2 Hierarchical Nested Gumbel Model
Another way to generalize the nested Gumbel model is to organize the available alternatives into a hierarchy.
Formally, hierarchical organizations are defined by a sequence of compatible partitions (Figure 3 (a)) Consider a sequence of L partitions, each of which is comprised of K_{l} disjoint subsets, \{ \mathsf{b}_{l, k_{1}}, \ldots, \mathsf{b}_{l, K_{l}} \} \subset \mathbf{c}. These partitions are nested within each other if every subset of the lth partition is contained in one, and only one, subset of the (l + 1)st partition, \mathbf{b}_{l, k_{l}} \subseteq \mathbf{b}_{l + 1, k_{l + 1}} for some unique k_{l + 1}. In this case, we say that the lth partition is a refinement of the (l + 1)st partition.
Intuitively, each partition in a compatible sequence defines a layer in a hierarchy (Figure 3 (b)). Each alternative is an element of a unique subset in the first partition, and that subset is itself a subset of a unique subset in the second partition. Tracing out all of these relationships recovers a hierarchical organization.
Effectively communicating all of the relationships defined by a sequence of compatible partitions requires some careful notation. Here, I will denote the subset in the (l + 1)st partition that contains the k_{l}st subset of the lth partition as k_{l + 1}(k_{l}). Moreover, I will denote all of the subsets of the lth partition that are contained in the subset \mathbf{b}_{l + 1, k_{l + 1}} by \mathsf{a}_{l + 1, k_{l + 1}}.
Recall that the nested Gumbel model introduces coupling parameters for every nest, and then defines a cumulative distribution function by summing over the alternatives in each nest. The hierarchical nested Gumbel model expands upon this construction by introducing coupling parameters \lambda_{l, k_{l}} for every subset in every partition. We can then build up a cumulative distribution function by summing over alternatives in each subset for the first partition, C_{1, k_{1}} = \left( \sum_{j \in \mathsf{b}_{1, k_{1}}} e^{ - \frac{1}{\lambda_{1, k_{1}}} \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right)^{ \lambda_{1, k_{1}} } summing these contributions over each subset in the second partition, C_{2, k_{2}} = \left( \sum_{k \in \mathsf{a}_{2, k_{2}}} C_{1, k}^{ \frac{1}{\lambda_{2, k_{2}}} } \right)^{ \lambda_{2, k_{2}} }, and then iterating, C_{l, k_{l}} = \left( \sum_{k \in \mathsf{a}_{l, k_{l}}} C_{l - 1, k}^{ \frac{1}{\lambda_{l, k_{l}}} } \right)^{ \lambda_{l, k_{l}} }, until \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K_{L}} \left( \sum_{k \in \mathsf{a}_{L, k_{L}}} C_{L - 1, k}^{ \frac{1}{\lambda_{L, k_{L}}} } \right)^{ \lambda_{L, k_{L}} } \right).
The resulting choice probabilities are easiest to digest in terms of their conditional decomposition over the hierarchy. At the top, we have alternative conditional probabilities that are exactly the same as in the nested Gumbel model, q_{ nj \mid k_{1}(j) } = \frac{ \exp \left( \frac{ \nu_{nj} }{ \lambda_{1, k_{1}(j) } } \right) }{ \sum_{j' \in \mathsf{b}_{1, k_{1}(j) } } \exp \left( \frac{ \nu_{nj'} }{ \lambda_{1, k_{1}(j) } } \right) }.
For the hierarchical nested Gumbel model, however, we then iterate through the partitions, deriving the conditional probability of selecting a subset in the lth partition from a subset in the (l + 1)st partition. We begin by computing the intermediate terms I_{ n k_{1} } = \lambda_{1, k_{1}} \sum_{ j' \in \mathsf{b}_{ 1, k_{1} } } \exp \left( \frac{1}{ \lambda_{1, k_{1} } } \frac{ \mu_{nj'} }{ \sigma_{n} } \right). Then we iterate, computing I_{ n k_{l} } = \lambda_{l, k_{l}} \sum_{ k_{l - 1}' \in \mathsf{a}_{ l, k_{l} } } \exp \left( \frac{ I_{ n k_{l - 1} } }{ \lambda_{l, k_{l} } } \right). and then the corresponding conditional probabilities, q_{ n k_{l} \mid k_{l + 1}( k_{l} ) } = \frac{ \exp \left( \frac{ I_{ n k_{l} } }{ \lambda_{l + 1, k_{l + 1}(k_{l}) } } \right) }{ \sum_{ k_{l}' \in \mathsf{b}_{l + 1, k_{l + 1}(k_{l}') } } \exp \left( \frac{ I_{ n k_{l}' } }{ \lambda_{l + 1, k_{l + 1}(k_{l}) } } \right) }. Once we reach the bottom of the hierarchy, the marginal probabilities are given by summing over I_{ n k_{L - 1} } for each of the terminating subsets.
In practice, the conditional and marginal probabilities can be computed from the \log I_{ n k_{l} } with softmax functions. The \log I_{ n k_{l} } can, in turn, be efficiently computed with log-sum-exp functions.
2.3 Generalized Extreme Value Models
Perhaps surprisingly, all of the discrete choice models that we have considered in this chapter so far are special cases of an even larger family of models. Conveniently, this larger family is also relatively straightforward to implement in practice.
In extreme value theory (de Carvalho et al. 2026), the multivariate extreme value family of probability distributions is defined with a copula-like construction that binds together multiple univariate extreme value distributions. Given the univariate extreme value cumulative distribution functions \Pi_{1}(x_{1}), \ldots, \Pi_{j}(x_{j}), \ldots, \Pi_{J}(x_{J}) and a sufficiently well-behaved function l : \mathbb{R}^{J} \rightarrow \mathbb{R}^{+}, the corresponding multivariate extreme value distribution is defined by the joint cumulative distribution function \Pi( x_{1}, \ldots, x_{J} ) = \exp \bigg( - l \big( -\log \Pi_{1}(x_{1}), \ldots, -\log \Pi_{J}(x_{J}) \big) \bigg).
In its full generality, the multivariate extreme value family applied to alternative utilities, \Pi( u_{n1}, \ldots, u_{nJ} ), is too complex to admit straightforward, closed-form discrete choice calculations. To achieve that, we have to impose some simplifying, but not too simplifying, assumptions.
First, we restrict the input cumulative distribution functions to the Gumbel family with varying baselines and common scales, \begin{align*} \Pi_{j}(u_{nj}) &= \Pi_{\mathrm{Gumbel}}(u_{nj} \mid \mu_{nj}, \sigma_{n}) \\ &= \exp \left( - e^{ \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right). \end{align*} In this case, the multivariate extreme value family reduces to \begin{align*} \Pi( u_{n1}, \ldots, u_{nJ} ) &= \exp \bigg( - l \big( -\log \Pi_{\mathrm{Gumbel}}(u_{n1} \mid \mu_{n1}, \sigma_{n}), \\ & \hspace{21mm} \ldots, \\ & \hspace{21mm} -\log \Pi_{\mathrm{Gumbel}}(u_{nj} \mid \mu_{nJ}, \sigma_{n}) \; \big) \; \bigg) \\ &= \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} - \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( \frac{ u_{nJ} - \mu_{nJ} }{ \sigma_{n} } \right) \Bigg) \Bigg). \end{align*}
Second, we require that the function l be a homogeneous function of degree one, \begin{align*} l( \rho \, z_{1}, \ldots, \rho \, z_{J} ) &= \rho \, l( z_{1}, \ldots, z_{J} ) \\ \frac{ \partial l}{ \partial z_{j} } ( \rho \, z_{1}, \ldots, \rho \, z_{J} ) &= \frac{ \partial l}{ \partial z_{j} }( z_{1}, \ldots, z_{J} ). \end{align*}
Under these assumptions, the choice probabilities can be computed entirely in terms of the coupling function l and its partial derivatives, q_{nj} = \frac{ \frac{ \partial l }{ \partial z_{j} } \, \Big( \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( - \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \Big) \, \exp \left( - \frac{ \mu_{nj} }{ \sigma_{n} } \right) }{ l \Big( \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( - \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \Big) }; see Appendix A.3 for details. In particular, if l satisfies all of the necessary conditions, then we can implement the corresponding discrete choice model without having to implement a single integral.
For example, taking l(z_{1}, \ldots, z_{J}) = \sum_{j = 1}^{J} x_{j} with \frac{ \partial l }{ \partial z_{j} }(z_{1}, \ldots, z_{J}) = 1 gives the independent Gumbel model, \begin{align*} q_{nj} &= \frac{ 1 \, \exp \left( - \frac{ \mu_{nj} }{ \sigma_{n} } \right) }{ \sum_{j' = 1}^{J} \exp \left( - \frac{ \mu_{nj'} }{ \sigma_{n} } \right) } \\ &= \frac{ \exp \left( - \frac{ \mu_{nj} }{ \sigma_{n} } \right) }{ \sum_{j' = 1}^{J} \exp \left( - \frac{ \mu_{nj'} }{ \sigma_{n} } \right). } \end{align*}
In the discrete choice literature, this construction defines what are known as generalized extreme value models (McFadden 1978, 1981).
3 The Probit Model
Another popular discrete choice modeling technique with coupled utilities is the probit model (Hausman and Wise 1978; Daganzo 1979). The probit model assumes a multivariate normal utility model for each agent, p( u_{n1}, \ldots, u_{nJ} ) = \text{multinormal} \left( u_{n1}, \ldots, u_{nJ} \mid \boldsymbol{\mu}_{n}, \boldsymbol{\Sigma}_{n} \right). In order to calculate the choice probabilities in this model, q_{nj} = \pi \left( \, \bigcap_{j' \ne j} \{ u_{nj} > u_{nj'} \} \, \right), we have to be able to integrate the multivariate normal probability density function over these non-rectangular subsets.
Unfortunately, these integrals do not result in any convenient special functions that we can evaluate directly. At the same time, numerical integration is either too inaccurate or too expensive to be productive when we are working with more than a few alternatives.
Because of these challenges, the probit model is typically implemented not by calculating the choice probabilities directly, but rather by explicitly modeling the latent utilities. Specifically, the utilities for each agent u_{n1}, \ldots, u_{nJ} are elevated to parameters with the observational model p ( \tilde{y}_{m} \mid u_{n(m)1}, \ldots, u_{n(m)J} ) = \left\{ \begin{array}{ll} 1, & \bigcap_{j' \ne \tilde{y}_{m}} \{ u_{n(m)\tilde{y}_{m}} > u_{n(m)j'} \} \\ 0, & \text{else}. \end{array} \right.
This observational model, however, presents some immediate practical challenges. Some configurations of the utilities will be equally consistent with any observed data, while all of the others will be completely disallowed. In other words, the likelihood functions, and resulting posterior distributions, derived from this observational model will always be discontinuous.
Because of this discontinuous behavior, the gradients of the derived likelihood functions are not informative. This, in turn, obstructs not only optimization-based maximum likelihood estimators, but also scalable Markov chain Monte Carlo tools like Hamiltonian Monte Carlo.
Because the methodology required for efficient implementation and diagnostics of probit models is very distinct from the methodology of models amenable to Hamiltonian Monte Carlo, I will not consider probit models further in this chapter.
4 Demonstration
Let’s put all of this theory into context by analyzing data simulated from a model with substitution patterns that transcend independence from irrelevant alternatives.
4.1 Setup
As always, we begin by setting up our local computation environment.
import sys
sys.path.append('support')
import mcmc_analysis_tools_pystan3 as util
import mcmc_visualization_tools as putil4.2 Basics
As we did in Part I, let’s start with analyzing data arising from a single, fixed choice set.
4.2.1 Simulate Data
For this exercise, we’ll use a nested Gumbel model for our simulation truth. This requires configuring the nests and the intra-nest couplings within the given choice set.
N = 6
J = 10
M = 15000K = 3
nests = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
lambda_true = [0.65, 0.4, 0.8]The baseline utilities and agent scales will behave similar to before.
To implement the nested Gumbel model, we need to be able to sum over all of the alternatives in each nest. This, in, turn, requires knowing which alternatives are in each nest. There are a few different ways to do this, but here I’ll use the num_which_int and which_int functions to dynamically lookup the appropriate alternatives in the Stan program.
simu\_ng1.stan
functions {
// Return the number of elements in vals that equal i
int num_which_int(array[] int vals, int i) {
int N = num_elements(vals);
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
}
}
return NN;
}
// Return the elements in vals that equal i
array[] int which_int(array[] int vals, int i) {
int N = num_elements(vals);
array[N] int selected_idxs;
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
selected_idxs[NN] = n;
}
}
return selected_idxs[1:NN];
}
}
data {
int<lower=1> M; // Number of observations
int<lower=1> N; // Number of agents
int<lower=1> J; // Number of alternatives in choice set
// Nesting configuration
int<lower=1, upper=J> K;
array[J] int<lower=1, upper=K> nests;
// Intra-nest coupling parameters
vector<lower=0, upper=1>[K] lambda;
}
generated quantities {
vector[J] mu; // Baseline utilities
array[N] real<lower=0> sigma; // Scales
array[M] int<lower=1, upper=N> agent; // Observed agent
array[M] int<lower=1, upper=J> choice; // Observed choice
array[N] vector[J] log_probs;
// Sample true data generating process behavior
for (j in 1:J) mu[j] = normal_rng(0, 1);
for (n in 1:N) sigma[n] = gamma_rng(31.0, 15.5);
// Compute log choice probabilities
for (n in 1:N) {
vector[K] nest_lses;
vector[K] log_marg_probs;
// Compute marginal probability for each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_lses[k] = log_sum_exp( mu[nest_alts]
/ (sigma[n] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
// Compute joint probability for each alternative in each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
log_probs[n][nest_alts] // Joint Probability
= log_marg_probs[k] // Marginal Probability
+ mu[nest_alts] / (sigma[n] * lambda[k]) // Conditional
- nest_lses[k]; // Probability
}
}
// Simulate observations
for (m in 1:M) {
int n = categorical_rng(uniform_simplex(N));
agent[m] = n;
choice[m] = categorical_logit_rng(log_probs[n]);
}
}with open('stan_programs/simu_ng1.stan', 'r') as file:
stan_program = file.read()
simu_model = stan.build(stan_program, random_seed=8438338,
data={'N': N, 'J': J, 'M': M,
'K': K, 'nests': nests,
'lambda': lambda_true})
simu = simu_model.fixed_param(num_chains=1, num_samples=1)
samples = util.extract_expectand_vals(simu)Building...
Once the individual choices have been simulated, we can aggregate them across agent-alternative pairs again.
mu_true = [ samples[f'mu[{j + 1}]'][0][0]
for j in range(J) ]
sigma_true = [ samples[f'sigma[{n + 1}]'][0][0]
for n in range(N) ]
log_prob_true = \
[ numpy.array([ samples[f'log_probs[{n + 1},{j + 1}]'][0][0]
for j in range(J)])
for n in range(N) ]agent = [ samples[f'agent[{m + 1}]'][0][0].astype(numpy.int64)
for m in range(M) ]
choice = [ samples[f'choice[{m + 1}]'][0][0].astype(numpy.int64)
for m in range(M) ]
data = {'N': N, 'J': J, 'M': M,
'K': K, 'nests': nests,
'agent': agent, 'choice': choice}
choice_counts = numpy.array(
[ numpy.histogram([ c for c, a in
zip(data['choice'], data['agent'])
if a == n + 1 ],
bins=numpy.arange(0.5, data['J'] + 0.51, 1))[0]
for n in range(data['N']) ]
)
data['choice_counts'] = choice_counts4.2.2 Explore Data
The histogram of observed choices aggregated across all agents is somewhat informative.
agg_choice_counts = numpy.sum(data['choice_counts'], axis=0)
putil.plot_disc_vals(
plot.gca(), agg_choice_counts,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Choice Counts"
)
plot.show()Stratifying these histograms by nest, however, takes advantage of the known nesting structure to capture conditional behavior.
f, axarr = plot.subplots(3, 1, layout="constrained")
for k in range(1, data['K'] + 1):
alts = [ j for j in range(data['J']) if data['nests'][j] == k ]
nest_alt_choice_counts = agg_choice_counts[alts]
putil.plot_disc_vals(
axarr[k - 1], nest_alt_choice_counts,
xlabel="Chosen Alternative",
xticklabels=[ j + 1 for j in alts ],
ylabel="Choice Counts",
title=f'Nest {k}'
)
plot.show()At the same time, we can summarize the relative abundance of choices across nests by aggregating counts over all of the alternatives in each nest. Here, for example, we see that marginally the third nest contains the most attractive alternatives.
nest_choice_counts = [ sum([ agg_choice_counts[j]
for j in range(data['J'])
if data['nests'][j] == k ])
for k in range(1, data['K'] + 1) ]
putil.plot_disc_vals(
plot.gca(), nest_choice_counts,
xlabel="Chosen Nest",
xticklabels=[ k for k in range(1, data['K'] + 1) ],
ylabel="Choice Counts"
)
plot.show()4.2.3 Independent Gumbel Model
Although we know that the true data generating process couples the baseline utilities, let’s see what happens when we assume the simpler independent Gumbel model.
We’ll choose the anchor alternative based on the total number of observed choices.
agg_counts = numpy.sum(data['choice_counts'], axis=0)
for j in range(data['J']):
print(f'Alternative {j + 1}: '
f'{agg_counts[j]} total observations')Alternative 1: 1628 total observations
Alternative 2: 671 total observations
Alternative 3: 2697 total observations
Alternative 4: 791 total observations
Alternative 5: 482 total observations
Alternative 6: 2061 total observations
Alternative 7: 1838 total observations
Alternative 8: 2670 total observations
Alternative 9: 557 total observations
Alternative 10: 1605 total observations
data['anchor'] = 3with open('stan_programs/ig3.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program, random_seed=8438338, data=data)
fit = model.sample(num_samples=1024, refresh=0)Building...
All of the diagnostics are clean, indicting that our posterior computation is reasonably accurate.
diagnostics = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics)
samples = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples,
['omega_free',
'tau_free'],
True)
util.check_all_expectand_diagnostics(base_samples)All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Moreover, the aggregate posterior retrodictive behavior is great despite the oversimplified assumptions.
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples, names,
baseline_values=agg_choice_counts,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Choice Counts"
)
plot.show()The inferred log choice probabilities are also reasonably well-behaved, similar to the true behaviors from the simulation.
names = [ f'log_probs[1,{j + 1}]' for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples, names,
baseline_values=log_prob_true[0],
baseline_color=putil.mid_teal,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Log Probability"
)
plot.show()Inferences for the baseline utilities, however, are less satisfying, as they concentrate pretty far away from the true values.
omega_true = (mu_true - mu_true[data['anchor'] - 1]) / sigma_true[0]
names = [ f'omega[{j + 1}]' for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples, names,
baseline_values=omega_true,
baseline_color=putil.mid_teal,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Relative Baseline Utility"
)
plot.show()Regardless of the behavior of the true data generating process, the independent Gumbel model is perfectly adequate for capturing the choice probabilities within a fixed choice set. The limitations of the independent Gumbel model manifest only when we consider observations across multiple choice subsets at the same time. We’ll investigate this in Section 4.3.
4.2.4 Nested Gumbel Model
Before trying to jointly fit data from multiple choice subsets, let’s spend some time implementing the nested Gumbel model for this single choice set. We’ll use the same anchoring strategy that we used in the independent Gumbel model to avoid structural redundancies. This also allows us to use the same prior model for the relative baseline utilities and relative scales.
To query the relevant nests and make the choice probability calculations a bit cleaner, we’ll make extensive use of the num_which_int and which_int user-defined functions.
ng1.stan
functions {
// Insert a zero into x_free at index anchor
vector add_anchor(vector x_free, int anchor) {
int J = size(x_free) + 1;
if (anchor == 1)
return append_row(0, x_free);
else if (anchor == J)
return append_row(x_free, 0);
else
return append_row(x_free[1:(anchor - 1)],
append_row(0, x_free[anchor:(J - 1)]));
}
// Return the number of elements in vals that equal i
int num_which_int(array[] int vals, int i) {
int N = num_elements(vals);
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
}
}
return NN;
}
// Return the elements in vals that equal i
array[] int which_int(array[] int vals, int i) {
int N = num_elements(vals);
array[N] int selected_idxs;
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
selected_idxs[NN] = n;
}
}
return selected_idxs[1:NN];
}
}
data {
int<lower=1> N; // Number of agents
int<lower=1> J; // Number of alternatives in choice set
// Aggregated observations
array[N, J] int<lower=0> choice_counts;
// Nesting configuration
int<lower=1, upper=J> K;
array[J] int<lower=1, upper=K> nests;
// Anchor configuration
int<lower=1, upper=J> anchor;
}
parameters {
// Relative baseline utilities for non-anchor alternatives
vector[J - 1] omega_free;
// Relative scales for non-anchor agents
array[N - 1] real<lower=0> tau_free;
// Intra-nest coupling parameters
vector<lower=0, upper=1>[K] lambda;
}
transformed parameters {
// Relative baseline utilities for all alternatives
vector[J] omega = add_anchor(omega_free, anchor);
// Relative scales for all agents
array[N] real<lower=0> tau = append_array({1}, tau_free);
// Alternative log probabilities
array[N] vector[J] log_probs;
for (n in 1:N) {
vector[K] nest_lses;
vector[K] log_marg_probs;
// Compute marginal probability for each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_lses[k] = log_sum_exp( omega[nest_alts]
/ (tau[n] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
// Compute joint probability for each alternative in each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
log_probs[n][nest_alts] // Joint Probability
= log_marg_probs[k] // Marginal Probability
+ omega[nest_alts] / (tau[n] * lambda[k]) // Conditional
- nest_lses[k]; // Probability
}
}
}
model {
// Prior model
target += normal_lpdf(omega_free | 0, 2);
target += lognormal_lpdf(tau_free | 0, 1);
target += beta_lpdf(lambda | 1, 1);
// Observational model
for (n in 1:N)
target += multinomial_logit_lpmf(choice_counts[n] | log_probs[n]);
}
generated quantities {
array[N, J] int<lower=0> choice_counts_pred;
array[J] int<lower=0> agg_choice_counts_pred = rep_array(0, J);
array[K] int<lower=0> nest_choice_counts_pred = rep_array(0, K);
for (n in 1:N) {
int MM = sum(choice_counts[n]);
choice_counts_pred[n]
= multinomial_logit_rng(log_probs[n], MM);
for (j in 1:J) {
agg_choice_counts_pred[j] += choice_counts_pred[n, j];
}
}
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_choice_counts_pred[k]
= sum(agg_choice_counts_pred[nest_alts]);
}
}
with open('stan_programs/ng1.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program, random_seed=8438338, data=data)
fit = model.sample(num_samples=1024, refresh=0)Building...
Despite the fact that we’re now using the correct model, the posterior computation has faltered. Specifically, we now see divergences that indicate stunted exploration.
diagnostics1 = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics1)
samples1 = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples1,
['omega_free',
'tau_free',
'lambda'],
True)
util.check_all_expectand_diagnostics(base_samples) Chain 1: 1 of 1024 transitions (0.10%) diverged.
Chain 4: 1 of 1024 transitions (0.10%) diverged.
Divergent Hamiltonian transitions result from unstable numerical
trajectories. These instabilities are often due to degenerate target
geometry, especially "pinches". If there are only a small number of
divergences then running with adept_delta larger than 0.801 may reduce
the instabilities at the cost of more expensive Hamiltonian transitions.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
To root out the underlying problem, we’ll need to find where the divergences are concentrating; this means examining scatter plots of pairs of parameters. We could try to brute force our way through all of the possible scatter plots, but let’s take a second to consider the structure of the model and guide a more targeted investigation.
Going through the mathematical structure of the nested Gumbel model, we see that the relative baseline utilities within each nest always appear with the corresponding intra-nest coupling parameter, \exp \left( \frac{ 1 }{ \lambda_{k(j')} } \frac{ \omega_{nj''} }{ \tau_{n} } \right). If the data mostly inform only the ratio \frac{ \omega_{nj''} }{ \lambda_{k(j')} } and not \omega_{nj''} and \lambda_{k(j')} individually, then any inferences for these parameters would exhibit a problematic degeneracy. This hypothesis suggests examining these particular pairs plots.
lambda_names = [ f'lambda[{k + 1}]' for k in range(data['K']) ]
transforms = { f'lambda[{k + 1}]':2 for k in range(data['K']) }
for k in range(1, data['K'] + 1):
alt_names = [ f'omega[{j + 1}]'
for j in range(data['J'])
if data['nests'][j] == k ]
util.plot_div_pairs(
alt_names, [ lambda_names[k - 1] ],
samples1, diagnostics1,
transforms=transforms
)/Users/Betancourt/Documents/Research/Code/betanalpha/quarto_private/discrete_choice_modeling2/support/mcmc_analysis_tools_pystan3.py:788: UserWarning: Attempting to set identical low and high xlims makes transformation singular; automatically expanding.
axarr[idx1, idx2].set_xlim(local_xlim)
Indeed, it looks like our suspicions were spot on. As each \omega_{nj''} increases, the corresponding \lambda_{k} decreases to keep the ratio between the two approximately constant. That is, however, until we see the divergences start to concentrate, suggesting that the Hamiltonian Monte Carlo sampler has prematurely stalled.
Now that we’ve identified a likely culprit, we can consider possible improvements to our model implementation. For example, we might reparameterize our model using the ratios that the data seem to be directly informing, \zeta_{nj''} = \frac{ \omega_{nj''} }{ \lambda_{k(j')} }. We can then recover inferences for \omega_{nj''} by multiplying \zeta_{nj''} and \lambda_{k(j')}.
Once immediate difficulty with this reparameterization is updating the prior model appropriately. An analytic transformation of the prior model is technically possible, but it is not particularly straightforward to implement in practice. Fortunately, however, a Cauchy prior model approximates the exact transformation reasonably well.
S = 10000
zetas = stats.norm.rvs(0, 2, S) / stats.uniform.rvs(0, 1, S)
putil.plot_line_hist(plot.gca(), zetas, -40, 40, 1,
xlabel="zeta[j]", prob=True,
col=putil.dark)
xs = numpy.arange(-40, 40, 0.1)
pds = [ stats.cauchy.pdf(x, 0, 2) for x in xs]
plot.plot(xs, pds, linewidth=2, color=putil.light)
plot.show()185 values (1.85%) fell below the histogram binning.
207 values (2.07%) fell above the histogram binning.
This gives us everything that we need.
ng2.stan
functions {
// Insert a zero into x_free at index anchor
vector add_anchor(vector x_free, int anchor) {
int J = size(x_free) + 1;
if (anchor == 1)
return append_row(0, x_free);
else if (anchor == J)
return append_row(x_free, 0);
else
return append_row(x_free[1:(anchor - 1)],
append_row(0, x_free[anchor:(J - 1)]));
}
// Return the number of elements in vals that equal i
int num_which_int(array[] int vals, int i) {
int N = num_elements(vals);
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
}
}
return NN;
}
// Return the elements in vals that equal i
array[] int which_int(array[] int vals, int i) {
int N = num_elements(vals);
array[N] int selected_idxs;
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
selected_idxs[NN] = n;
}
}
return selected_idxs[1:NN];
}
}
data {
int<lower=1> N; // Number of agents
int<lower=1> J; // Number of alternatives in choice set
// Aggregated observations
array[N, J] int<lower=0> choice_counts;
// Nesting configuration
int<lower=1, upper=J> K;
array[J] int<lower=1, upper=K> nests;
// Anchor configuration
int<lower=1, upper=J> anchor;
}
parameters {
// Scaled relative baseline utilities for non-anchor alternatives
vector[J - 1] zeta_free;
// Relative scales for non-anchor agents
array[N - 1] real<lower=0> tau_free;
// Intra-nest coupling parameters
vector<lower=0, upper=1>[K] lambda;
}
transformed parameters {
// Scaled relative baseline utilities for all alternatives
vector[J] zeta = add_anchor(zeta_free, anchor);
vector[J] omega = zeta .* lambda[nests];
// Relative scales for all agents
array[N] real<lower=0> tau = append_array({1}, tau_free);
// Alternative log probabilities
array[N] vector[J] log_probs;
for (n in 1:N) {
vector[K] nest_lses;
vector[K] log_marg_probs;
// Compute marginal probability for each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_lses[k] = log_sum_exp( omega[nest_alts]
/ (tau[n] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
// Compute joint probability for each alternative in each nest
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
log_probs[n][nest_alts] // Joint Probability
= log_marg_probs[k] // Marginal Probability
+ omega[nest_alts] / (tau[n] * lambda[k]) // Conditional
- nest_lses[k]; // Probability
}
}
}
model {
// Prior model
target += cauchy_lpdf(zeta_free | 0, 2);
target += lognormal_lpdf(tau_free | 0, 1);
target += beta_lpdf(lambda | 1, 1);
// Observational model
for (n in 1:N)
target += multinomial_logit_lpmf(choice_counts[n] | log_probs[n]);
}
generated quantities {
array[N, J] int<lower=0> choice_counts_pred;
array[J] int<lower=0> agg_choice_counts_pred = rep_array(0, J);
array[K] int<lower=0> nest_choice_counts_pred = rep_array(0, K);
for (n in 1:N) {
int MM = sum(choice_counts[n]);
choice_counts_pred[n]
= multinomial_logit_rng(log_probs[n], MM);
for (j in 1:J) {
agg_choice_counts_pred[j] += choice_counts_pred[n, j];
}
}
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_choice_counts_pred[k]
= sum(agg_choice_counts_pred[nest_alts]);
}
}
with open('stan_programs/ng2.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program, random_seed=8438338, data=data)
fit = model.sample(num_samples=1024, refresh=0)Building...
The diagnostics are a little bit better – the divergences are gone – but that are some new warnings to parse. Specifically, the realized Markov chains exhibit heavier tails and slightly stronger autocorrelations than ideal. Because these warnings indicate only mildly inefficient, and not unreliable, posterior quantification, however, we’ll press on.
diagnostics2 = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics2)
samples2 = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples2,
['zeta_free',
'tau_free',
'lambda'],
True)
util.check_all_expectand_diagnostics(base_samples)All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
zeta_free[3]:
Chain 1: hat{ESS} (27.9) is smaller than desired (100).
Chain 4: hat{ESS} (92.1) is smaller than desired (100).
zeta_free[4]:
Chain 1: hat{ESS} (28.1) is smaller than desired (100).
Chain 4: hat{ESS} (92.6) is smaller than desired (100).
zeta_free[5]:
Chain 1: Right tail hat{xi} (0.312) exceeds 0.25.
Chain 1: hat{ESS} (27.4) is smaller than desired (100).
Chain 4: hat{ESS} (90.2) is smaller than desired (100).
zeta_free[6]:
Chain 4: Right tail hat{xi} (0.288) exceeds 0.25.
Chain 1: hat{ESS} (27.6) is smaller than desired (100).
Chain 4: hat{ESS} (90.3) is smaller than desired (100).
Large tail hat{xi}s suggest that the expectand might not be sufficiently
integrable.
Small empirical effective sample sizes result in imprecise Markov chain
Monte Carlo estimators.
Comparing the inferences for \omega_{nj''} and \lambda_{k(j')} between ng1.stan and ng2.stan, we see more expansive exploration in the latter. The divergences really were indicating a serious problem!
def logit(p):
return numpy.log(p / (1 - p))k = 1
alt_names = [ f'omega[{j + 1}]'
for j in range(data['J'])
if data['nests'][j] == k ]
f, axarr = plot.subplots(1, 3, layout="constrained")
for i, name in enumerate(alt_names):
axarr[i].scatter(samples2[name],
logit(samples2[lambda_names[k - 1]]),
s=2, color=putil.dark)
axarr[i].scatter(samples1[name],
logit(samples1[lambda_names[k - 1]]),
s=2, color=putil.light)
axarr[i].set_xlim([-2, 1])
axarr[i].set_xlabel(name),
axarr[i].set_ylim([-10, 10])
axarr[i].set_ylabel(f'logit(lambda[{k}])')
plot.show()With reasonably accurate posterior quantification, we can examine the posterior retrodictive behavior for the nested Gumbel model. Unsurprisingly, the aggregate behavior doesn’t show any signs of retrodictive tension.
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples2, names,
baseline_values=agg_choice_counts,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Choice Counts"
)
plot.show()Moreover, the retrodictive performance within each nest is also excellent.
names = [ f'nest_choice_counts_pred[{k + 1}]'
for k in range(data['K']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples2, names,
baseline_values=nest_choice_counts,
xlabel="Nest",
xticklabels=[ k + 1 for k in range(data['K']) ],
ylabel="Choice Counts"
)
plot.show()As with those inferred from the independent Gumbel model, the inferred choice probabilities inferred from the nested Gumbel model are consistent with the true choice probabilities.
names = [ f'log_probs[1,{j + 1}]' for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples2, names,
baseline_values=log_prob_true[0],
baseline_color=putil.mid_teal,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Log Probability"
)
plot.show()Now, however, we also recover the true baseline utilities and agent scales, albeit with some heavy-tailed uncertainties.
names = [ f'omega[{j + 1}]' for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples2, names,
baseline_values=omega_true,
baseline_color=putil.mid_teal,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Relative Baseline Utility"
)
plot.show()tau_true = sigma_true / sigma_true[0]
names = [ f'tau[{n + 1}]' for n in range(data['N']) ]
putil.plot_disc_pushforward_quantiles(
plot.gca(), samples2, names,
baseline_values=tau_true,
baseline_color=putil.mid_teal,
xlabel="Agent",
xticklabels=range(1, data['N'] + 1),
ylabel="Relative Scale"
)
plot.show()The posterior inferences for the intra-nest coupling parameters are also consistent with the simulation data generating process, although the uncertainties are massive. These large uncertainties amplify the degeneracy between the relative baseline utilities and the intra-nest coupling parameters, which is why the reparameterization was so critical here.
f, axarr = plot.subplots(1, 3, layout='constrained')
for k in range(1, data['K'] + 1):
util.plot_expectand_pushforward(
axarr[k - 1],
samples2[f'lambda[{k}]'],
25, flim=[0, 1],
display_name=f'lambda[{k}]',
baseline=lambda_true[k - 1]
)
plot.show()In hindsight, these large uncertainties should not have been surprising. The configuration of the \lambda_{k} determine substitution patterns. With observations spanning only a single choice subset, however, we can’t learn much about those substitution patterns. Consequently, these observations poorly inform the \lambda_{k}.
4.3 Nested Gumbel Substitution Patterns
In order to evaluate how well the independent and nested Gumbel models capture the true substitution patterns, and generalize from one choice subset to another, we need to observe different choice subsets. The more the nest occupancies vary across the choice subsets, the better we’ll be able to resolve the substitution patterns.
4.3.1 Simulate Data
For this exercise, let’s use the same nesting structure that we did above.
N = 6
J = 10
M = 15000K = 3
nests = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
lambda_true = [0.65, 0.4, 0.8]Because the size of the choice subsets can vary, we’ll need to concatenate all of the choice subsets together like we did previously. Here we’ll generate seven choice subsets randomly and append two choice subsets with very specific alternatives.
with open("data/subset_alts.json","r") as infile:
subset_alts = json.load(infile)['subset_alts']S = len(subset_alts)
for s in range(S):
print(f'Choice Subset {s + 1}: {subset_alts[s]}')Choice Subset 1: [1, 10, 8, 6, 9, 3, 7]
Choice Subset 2: [7, 4, 2, 5, 6, 9, 10]
Choice Subset 3: [10, 2, 7, 1, 8, 6]
Choice Subset 4: [5, 8, 6, 9, 10, 2, 4, 3]
Choice Subset 5: [9, 2, 4, 10, 5, 1, 3, 7]
Choice Subset 6: [1, 3, 8, 7, 2, 10, 4, 5, 6]
Choice Subset 7: [2, 4, 5, 10, 3, 6]
Choice Subset 8: [1, 2, 3, 4, 6, 7, 8, 10]
Choice Subset 9: [1, 2, 3, 4, 5, 6, 7, 8, 10]
to_subset_idx = [ [ subset_alt.index(j) + 1
if j in subset_alt else None
for j in range(1, data['J'] + 1) ]
for subset_alt in subset_alts ]SJ = 0
alts = []
subset_start = []
subset_end = []
for s in range(S):
subset_start.append(SJ + 1)
SJ += len(subset_alts[s])
subset_end.append(SJ)
alts += subset_alts[s]To avoid being overwhelmed by all of the possibilities, we’ll investigate the substitution patterns of only three alternatives.
j1 = 4
j2 = 7
j3 = 2The first two of these alternatives are in the same nest, while the third is not.
for j in [j1, j2, j3]:
print(f'Alternative {j}: Nest {nests[j - 1]}')Alternative 4: Nest 2
Alternative 7: Nest 2
Alternative 2: Nest 1
Because the j_{1}th and j_{2}th alternatives are in the same nest, the ratios \frac{ q_{nj_{1}} }{ q_{nj_{2}} } should be consistent across all of the choice subsets that include both alternatives. On the other hand, because j_{1} and j_{3} are in the different nests, the ratios \frac{ q_{nj_{1}} }{ q_{nj_{3}} } will generally vary from choice subset to the next.
When moving from the eight choice subset to the ninth choice subset, for example, we add an alternative to the second nest. This should preferentially leech probability from the other alternatives in that nest relative to the alternatives in other nests. Specifically, this should result in \frac{ q_{nj_{1}} }{ q_{nj_{3}} } decreasing in the ninth choice subset relative to the eighth choice subset.
With all of the details set, we can simulate data.
simu\_ng2.stan
functions {
// Return the number of elements in vals that equal i
int num_which_int(array[] int vals, int i) {
int N = num_elements(vals);
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
}
}
return NN;
}
// Return the elements in vals that equal i
array[] int which_int(array[] int vals, int i) {
int N = num_elements(vals);
array[N] int selected_idxs;
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
selected_idxs[NN] = n;
}
}
return selected_idxs[1:NN];
}
}
data {
int<lower=1> M; // Number of observations
int<lower=1> N; // Number of agents
int<lower=1> J; // Total number of alternatives
// Nesting configuration
int<lower=1, upper=J> K;
array[J] int<lower=1, upper=K> nests;
// Choice subset configurations
int<lower=1> S;
int<lower=1> SJ;
array[S] int<lower=1, upper=SJ> subset_start;
array[S] int<lower=1, upper=SJ> subset_end;
array[SJ] int<lower=1, upper=J> alts;
// Intra-nest coupling parameters
vector<lower=0, upper=1>[K] lambda;
}
generated quantities {
vector[J] mu; // Baseline utilities
array[N] real<lower=0> sigma; // Scales
array[M] int<lower=1, upper=S> subset; // Observed choice subset
array[M] int<lower=1, upper=N> agent; // Observed agent
array[M] int<lower=1, upper=J> choice; // Observed choice
array[N] vector[SJ] log_probs;
// Sample true data generating process behavior
for (j in 1:J) mu[j] = normal_rng(0, 1);
for (n in 1:N) sigma[n] = gamma_rng(31.0, 15.5);
// Compute log choice probabilities
for (s in 1:S) {
// Extract choice subset configuration
int JJ = subset_end[s] - subset_start[s] + 1;
array[JJ] int subset_alts
= alts[subset_start[s]:subset_end[s]];
array[JJ] int subset_nests = nests[subset_alts];
for (n in 1:N) {
vector[JJ] subset_log_probs;
vector[K] nest_lses;
vector[K] log_marg_probs;
// Compute marginal probability for each nest
for (k in 1:K) {
int JK = num_which_int(nests[subset_alts], k);
array[JK] int nest_alts = which_int(nests[subset_alts], k);
nest_lses[k] = log_sum_exp( mu[subset_alts[nest_alts]]
/ (sigma[n] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
// Compute joint probability for each alternative in each nest
for (k in 1:K) {
int JK = num_which_int(nests[subset_alts], k);
array[JK] int nest_alts = which_int(nests[subset_alts], k);
subset_log_probs[nest_alts] // Joint Probability
= log_marg_probs[k] // Marginal Probability
+ mu[subset_alts[nest_alts]] // Conditional
/ (sigma[n] * lambda[k]) // Prob-
- nest_lses[k]; // ability
}
log_probs[n][subset_start[s]:subset_end[s]]
= subset_log_probs;
}
}
// Simulate observations
for (m in 1:M) {
int n = categorical_rng(uniform_simplex(N));
int s = categorical_rng(uniform_simplex(S));
int JJ = subset_end[s] - subset_start[s] + 1;
vector[JJ] subset_log_probs
= log_probs[n][subset_start[s]:subset_end[s]];
agent[m] = n;
subset[m] = s;
choice[m] = categorical_logit_rng(subset_log_probs);
}
}with open('stan_programs/simu_ng2.stan', 'r') as file:
stan_program = file.read()
simu_model = stan.build(stan_program, random_seed=8438338,
data={'N': N, 'J': J, 'M': M,
'K': K, 'nests': nests,
'S': S, 'SJ': SJ,
'subset_start': subset_start,
'subset_end': subset_end,
'alts': alts,
'lambda': lambda_true})
simu = simu_model.fixed_param(num_chains=1, num_samples=1)
samples = util.extract_expectand_vals(simu)Building...
subset = [ samples[f'subset[{m + 1}]'][0][0].astype(numpy.int64)
for m in range(M) ]
agent = [ samples[f'agent[{m + 1}]'][0][0].astype(numpy.int64)
for m in range(M) ]
choice = [ samples[f'choice[{m + 1}]'][0][0].astype(numpy.int64)
for m in range(M) ]
choice_counts = numpy.full([N, SJ], 0)
for s in range(S):
JJ = subset_end[s] - subset_start[s] + 1
for n in range(N):
choice_counts[n,(subset_start[s] - 1):subset_end[s]] = \
numpy.histogram([ c for c, aa, ss in
zip(choice, agent, subset)
if aa == n + 1 and ss == s + 1 ],
bins=numpy.arange(0.5, JJ + 0.51, 1) )[0]In addition to saving the true configuration of the data generating process, we can also calculate the true choice probabilities across the full choice set for any particular agent. Here, we’ll look at the fifth agent.
mu_true = [ samples[f'mu[{j + 1}]'][0][0]
for j in range(J) ]
sigma_true = [ samples[f'sigma[{n + 1}]'][0][0]
for n in range(N) ]n_pred = 5
log_probs_true = numpy.empty([J])
nest_lses = numpy.empty([K])
log_marg_probs = numpy.empty([K])
for k in range(1, K + 1):
nest_alts = [ j for j in range(data['J'])
if nests[j] == k ]
nest_lses[k - 1] = \
numpy.log(sum(numpy.exp(
numpy.array([ mu_true[j] for j in nest_alts ]) /
(sigma_true[n_pred - 1] * lambda_true[k - 1])
) ) )
x = lambda_true * nest_lses
log_marg_probs = x - numpy.log(sum(numpy.exp(x)))
for k in range(1, K + 1):
nest_alts = [ j for j in range(data['J'])
if nests[j] == k ]
for j in nest_alts:
log_probs_true[j] = \
log_marg_probs[k - 1] \
+ mu_true[j] / (sigma_true[n_pred] * lambda_true[k - 1]) \
- nest_lses[k - 1]4.3.2 Explore Data
By aggregating over all of the choice subsets, we can use the same histogram summaries that we used previously. The only subtlety is making sure to aggregate using the global alternative indices.
agg_choice_counts = numpy.zeros(J)
for s in range(S):
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
subset_alts = [ j - 1 for j in
alts[(subset_start[s] - 1):subset_end[s]] ]
agg_choice_counts[subset_alts] = \
agg_choice_counts[subset_alts] + subset_agg_choice_counts
putil.plot_disc_vals(plot.gca(), agg_choice_counts,
xlabel='Chosen Alternative')
plot.show()nest_choice_counts = [ sum([ agg_choice_counts[j]
for j in range(data['J'])
if data['nests'][j] == k ])
for k in range(1, data['K'] + 1) ]
putil.plot_disc_vals(
plot.gca(), nest_choice_counts,
xlabel="Chosen Nest",
xticklabels=range(1, data['K'] + 1),
ylabel="Choice Counts"
)
plot.show()Now, however, we can also stratify these summaries by the individual choice subsets. Here, it’s a bit more convenient to use the local alternative indexing within each choice subset to avoid too many gaps in the histograms.
f, axarr = plot.subplots(3, 3, layout="constrained")
for s in range(S):
i1 = s // 3
i2 = s % 3
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
putil.plot_disc_vals(
axarr[i1, i2], subset_agg_choice_counts,
xlabel="Chosen Alternativen\n(Local Indexing)",
xticklabels=alts[(subset_start[s] - 1):subset_end[s]],
ylabel="Choice\nCounts",
title=f'Choice Subset {s + 1}'
)
plot.show()4.3.3 Independent Gumbel Model
The independent Gumbel model fared well when analyzing data from only a single choice set, even when that data was simulated from a more sophisticated model. Will that performance persist to data across multiple choice subsets?
If we want to be able to compare parameters across fits, then we’ll need to choose a consistent anchor. The ideal anchor is the most observed alternative that also appears in all of the choice subsets. We don’t have many options here, so we’ll anchor the only alternative that appears in all nine of the choice subsets.
common_alts = set.intersection(
*map(set, [ alts[(subset_start[s] - 1):subset_end[s]]
for s in range(S) ])
)
for alt in common_alts:
print(f'Alternative {alt}:')
for s in range(S):
local_idx = to_subset_idx[s][alt - 1] - 1
MM = int(sum(choice_counts[:, local_idx]))
print(f' {MM} observations in choice subset {s + 1}')Alternative 10:
194 observations in choice subset 1
262 observations in choice subset 2
206 observations in choice subset 3
42 observations in choice subset 4
275 observations in choice subset 5
360 observations in choice subset 6
275 observations in choice subset 7
334 observations in choice subset 8
119 observations in choice subset 9
anchor = 10At this point, we have the option of fitting the data from each choice subset independently or jointly. Let’s start by fitting them independently.
Note that we have to convert the anchor from global alternative indexing to local alternative indexing in each fit.
samples = [ None ] * S
for s in range(S):
JJ = subset_end[s] - subset_start[s] + 1
subset_choice_counts = \
choice_counts[:,(subset_start[s] - 1):subset_end[s]]
data = {'N': N, 'J': JJ,
'choice_counts': subset_choice_counts,
'anchor': to_subset_idx[s][anchor - 1]}
with open('stan_programs/ig3.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program,
random_seed=8438338,
data=data)
fit = model.sample(num_samples=1024, refresh=0)
print(f'Analyzing choice subset {s + 1}\n')
diagnostics = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics)
samples[s] = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples[s],
['omega_free',
'tau_free'],
True)
util.check_all_expectand_diagnostics(base_samples)
print('\n\n')Building...
Analyzing choice subset 1
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 2
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 3
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 4
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 5
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 6
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 7
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 8
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Building...
Analyzing choice subset 9
All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Each fit exhibits excellent retrodictive performance individually.
f, axarr = plot.subplots(3, 3, layout="constrained")
for s in range(S):
i1 = s // 3
i2 = s % 3
JJ = subset_end[s] - subset_start[s] + 1
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(JJ) ]
putil.plot_disc_pushforward_quantiles(
axarr[i1, i2], samples[s], names,
baseline_values=subset_agg_choice_counts,
xlabel="Alternative\n(Local Indexing)",
xticklabels=range(1, JJ + 1),
title=f'Subset {s + 1}'
)
plot.show()That said, the relative baseline utilities needed to achieve these retrodictive performances are all over the place! The inferred behaviors are not only inconsistent across fits, but also largely inconsistent with the true configuration of the simulation data generating process.
omega_true = (mu_true - mu_true[anchor - 1]) / sigma_true[0]
js = [j1, j2, j3]
Bs = [75, 75, 50]
flims = [ [-2.5, 0.25], [0, 1.25], [-0.5, 1.5] ]
ylims = [ [0, 6], [0, 6], [0, 6] ]
f, axarr = plot.subplots(1, 3, layout="constrained")
for i, j in enumerate(js):
valid_subsets = [ s for s in range(S)
if to_subset_idx[s][j - 1] is not None ]
sv = valid_subsets[0]
name = f'omega[{to_subset_idx[sv][j - 1]}]'
display_name = f'omega[{j}]'
util.plot_expectand_pushforward(
axarr[i], samples[sv][name], Bs[i],
flim=flims[i], ylim=ylims[i],
display_name=display_name,
color=putil.mid,
baseline=omega_true[j - 1],
baseline_color=putil.mid_teal)
for sv in valid_subsets[1:]:
name = f'omega[{to_subset_idx[sv][j - 1]}]'
util.plot_expectand_pushforward(
axarr[i], samples[sv][name], Bs[i],
flim=flims[i], ylim=ylims[i],
color=putil.mid,
border="#BBBBBB88",
add=True)
plot.show()1 value (0.02%) fell below the histogram binning.
2041 values (49.83%) fell below the histogram binning.
23 values (0.56%) fell below the histogram binning.
1 value (0.02%) fell below the histogram binning.
626 values (15.28%) fell below the histogram binning.
53 values (1.29%) fell below the histogram binning.
180 values (4.39%) fell below the histogram binning.
4008 values (97.85%) fell below the histogram binning.
3679 values (89.82%) fell below the histogram binning.
4094 values (99.95%) fell below the histogram binning.
3909 values (95.43%) fell below the histogram binning.
4093 values (99.93%) fell below the histogram binning.
3921 values (95.73%) fell below the histogram binning.
4094 values (99.95%) fell below the histogram binning.
While the independent Gumbel model can reproduce the observed behaviors within each choice subset individually, it cannot reproduce those all of those behaviors at the same time. This inconsistency across choice subsets then results in poor inferences and predictions for new choice subsets.
We can force the configuration of the independent Gumbel model to be consistent across choice subsets by analyzing all of the data jointly.
data = {'N': N, 'J': J,
'S': S, 'SJ': SJ,
'subset_start': subset_start,
'subset_end': subset_end,
'alts': alts,
'choice_counts': choice_counts,
'anchor': anchor }To study particular substitution patterns, we construct predictions for the choice behavior in particular choice subsets. Here, we’ll consider predictions for the full choice set, for which we have no observations.
data['n_pred'] = n_pred
data['J_pred'] = J
data['pred_alts'] = list(range(1, J + 1))How does the joint model fare?
with open('stan_programs/ig_joint.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program,
random_seed=8438338,
data=data)
fit = model.sample(num_samples=1024, refresh=0)Building...
Well, there don’t appear to be any computational issues.
diagnostics = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics)
samples_ig = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples_ig,
['omega_free',
'tau_free'],
True)
util.check_all_expectand_diagnostics(base_samples)All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
Unfortunately, the retrodictive performance is not so well behaved. Forcing a consistent configuration of the independent Gumbel model compromises the retrodictive performance in many of the choice subsets.
f, axarr = plot.subplots(3, 3, layout="constrained")
for s in range(S):
i1 = s // 3
i2 = s % 3
JJ = subset_end[s] - subset_start[s] + 1
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(subset_start[s] - 1, subset_end[s]) ]
putil.plot_disc_pushforward_quantiles(
axarr[i1, i2], samples_ig, names,
baseline_values=subset_agg_choice_counts,
xlabel="Alternative\n(Local Indexing)",
xticklabels=range(1, JJ + 1),
title=f'Subset {s + 1}'
)
plot.show()This is a little bit easier to see if we zoom in on the most compromised choice subsets.
f, axarr = plot.subplots(2, 2, layout="constrained")
i = 0
for s in [1, 2, 3, 4]:
i1 = i // 2
i2 = i % 2
JJ = subset_end[s] - subset_start[s] + 1
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(subset_start[s] - 1, subset_end[s]) ]
putil.plot_disc_pushforward_quantiles(
axarr[i1, i2], samples_ig, names,
baseline_values=subset_agg_choice_counts,
xlabel="Alternative\n(Local Indexing)",
xticklabels=range(1, JJ + 1),
title=f'Subset {s + 1}'
)
i += 1
plot.show()Lacking the sophistication to accommodate the substitution patterns of the true data generating process, the independent Gumbel model performs poorly in one way or another when analyzing data from multiple choice subsets. The only way to ensure consistent inferences and performant predictions is to use a sufficiently flexible model.
4.3.4 Nested Gumbel Model
Because we simulated data from a nested Gumbel model, analyzing the that data with a nested Gumbel model should should yield better results. Fortunately, we don’t have to speculate: we can just study this directly.
ng\_joint.stan
functions {
// Insert a zero into x_free at index anchor
vector add_anchor(vector x_free, int anchor) {
int J = size(x_free) + 1;
if (anchor == 1)
return append_row(0, x_free);
else if (anchor == J)
return append_row(x_free, 0);
else
return append_row(x_free[1:(anchor - 1)],
append_row(0, x_free[anchor:(J - 1)]));
}
// Return the number of elements in vals that equal i
int num_which_int(array[] int vals, int i) {
int N = num_elements(vals);
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
}
}
return NN;
}
// Return the elements in vals that equal i
array[] int which_int(array[] int vals, int i) {
int N = num_elements(vals);
array[N] int selected_idxs;
int NN = 0;
for (n in 1:N) {
if (vals[n] == i) {
NN += 1;
selected_idxs[NN] = n;
}
}
return selected_idxs[1:NN];
}
}
data {
int<lower=1> N; // Number of agents
int<lower=1> J; // Number of alternatives in choice set
// Nesting configuration
int<lower=1, upper=J> K;
array[J] int<lower=1, upper=K> nests;
// Choice subset configurations
int<lower=1> S;
int<lower=1> SJ;
array[S] int<lower=1, upper=SJ> subset_start;
array[S] int<lower=1, upper=SJ> subset_end;
array[SJ] int<lower=1, upper=J> alts;
// Aggregated observations
array[N, SJ] int<lower=0> choice_counts;
// Anchoring configuration
int<lower=1, upper=J> anchor;
// Prediction configuration
int<lower=1, upper=N> n_pred;
int<lower=1> J_pred;
array[J_pred] int<lower=1, upper=J> pred_alts;
}
parameters {
// Relative baseline utilities for non-anchor alternatives
vector[J - 1] omega_free;
// Relative scales for non-anchor agents
array[N - 1] real<lower=0> tau_free;
// Intra-nest coupling parameters
vector<lower=0, upper=1>[K] lambda;
}
transformed parameters {
// Relative baseline utilities for all alternatives
vector[J] omega = add_anchor(omega_free, anchor);
// Relative scales for all agents
array[N] real<lower=0> tau = append_array({1}, tau_free);
// Alternative log probabilities
array[N] vector[SJ] log_probs;
for (s in 1:S) {
int JJ = subset_end[s] - subset_start[s] + 1;
array[JJ] int subset_alts
= alts[subset_start[s]:subset_end[s]];
array[JJ] int subset_nests = nests[subset_alts];
for (n in 1:N) {
vector[JJ] subset_log_probs;
vector[K] nest_lses;
vector[K] log_marg_probs;
// Compute marginal probability for each nest
for (k in 1:K) {
int JK = num_which_int(nests[subset_alts], k);
array[JK] int nest_alts
= which_int(nests[subset_alts], k);
nest_lses[k] = log_sum_exp( omega[subset_alts[nest_alts]]
/ (tau[n] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
// Compute joint probability for each alternative in each nest
for (k in 1:K) {
int JK = num_which_int(nests[subset_alts], k);
array[JK] int nest_alts
= which_int(nests[subset_alts], k);
// Joint Probability
subset_log_probs[nest_alts] =
// Marginal Probability
log_marg_probs[k]
// Conditional Probability
+ omega[subset_alts[nest_alts]]
/ (tau[n] * lambda[k])
- nest_lses[k];
}
log_probs[n][subset_start[s]:subset_end[s]]
= subset_log_probs;
}
}
}
model {
// Prior model
target += normal_lpdf(omega_free | 0, 2);
target += lognormal_lpdf(tau_free | 0, 1);
target += beta_lpdf(lambda | 1, 1);
// Observational model
for (n in 1:N) {
for (s in 1:S) {
int JJ = subset_end[s] - subset_start[s] + 1;
vector[JJ] subset_log_probs
= log_probs[n][subset_start[s]:subset_end[s]];
array[JJ] int subset_choice_counts
= choice_counts[n, subset_start[s]:subset_end[s]];
target += multinomial_logit_lpmf(subset_choice_counts |
subset_log_probs);
}
}
}
generated quantities {
array[N, SJ] int<lower=0> choice_counts_pred;
array[SJ] int<lower=0> agg_choice_counts_pred = rep_array(0, SJ);
array[K] int<lower=0> nest_choice_counts_pred = rep_array(0, K);
vector[J_pred] probs_pred;
for (s in 1:S) {
int JJ = subset_end[s] - subset_start[s] + 1;
for (n in 1:N) {
vector[JJ] subset_log_probs
= log_probs[n][subset_start[s]:subset_end[s]];
int MM
= sum(choice_counts[n, subset_start[s]:subset_end[s]]);
choice_counts_pred[n, subset_start[s]:subset_end[s]]
= multinomial_logit_rng(subset_log_probs, MM);
for (j in subset_start[s]:subset_end[s])
agg_choice_counts_pred[j] += choice_counts_pred[n, j];
}
}
for (k in 1:K) {
int JK = num_which_int(nests, k);
array[JK] int nest_alts = which_int(nests, k);
nest_choice_counts_pred[k]
= sum(agg_choice_counts_pred[nest_alts]);
}
{
vector[K] nest_lses;
vector[K] log_marg_probs;
for (k in 1:K) {
int JK = num_which_int(nests[pred_alts], k);
array[JK] int nest_alts
= which_int(nests[pred_alts], k);
nest_lses[k] = log_sum_exp( omega[pred_alts[nest_alts]]
/ (tau[n_pred] * lambda[k]) );
}
log_marg_probs = log_softmax( lambda .* nest_lses );
for (k in 1:K) {
int JK = num_which_int(nests[pred_alts], k);
array[JK] int nest_alts
= which_int(nests[pred_alts], k);
probs_pred[nest_alts]
= exp( log_marg_probs[k]
+ omega[nest_alts] / (tau[n_pred] * lambda[k])
- nest_lses[k]);
}
}
}data['K'] = K
data['nests'] = nests
with open('stan_programs/ng_joint.stan', 'r') as file:
stan_program = file.read()
model = stan.build(stan_program,
random_seed=8438338,
data=data)
fit = model.sample(num_samples=1024, refresh=0)Building...
We don’t see any signs of computational problems, even though we’re using the nominal parameterization of the nested Gumbel model that exhibited problems before. This suggests that analyzing data from multiple choice subsets is indeed able to inform the intra-nest coupling parameters more strongly. That said, we’ll have to examine those posterior inferences to confirm.
diagnostics = util.extract_hmc_diagnostics(fit)
util.check_all_hmc_diagnostics(diagnostics)
samples_ng = util.extract_expectand_vals(fit)
base_samples = util.filter_expectands(samples_ng,
['omega_free',
'tau_free',
'lambda'],
True)
util.check_all_expectand_diagnostics(base_samples)All Hamiltonian Monte Carlo diagnostics are consistent with accurate
Markov chain Monte Carlo.
All expectands checked appear to be behaving well enough for reliable
Markov chain Monte Carlo estimation.
More good news: the retrodictive performance is good across all of the choice subsets.
f, axarr = plot.subplots(3, 3, layout="constrained")
for s in range(S):
i1 = s // 3
i2 = s % 3
JJ = subset_end[s] - subset_start[s] + 1
counts = choice_counts[:,(subset_start[s] - 1):subset_end[s]]
subset_agg_choice_counts = numpy.sum(counts, axis=0)
names = [ f'agg_choice_counts_pred[{j + 1}]'
for j in range(subset_start[s] - 1, subset_end[s]) ]
putil.plot_disc_pushforward_quantiles(
axarr[i1, i2], samples_ng, names,
baseline_values=subset_agg_choice_counts,
xlabel="Alternative\n(Local Indexing)",
xticklabels=range(1, JJ + 1),
title=f'Subset {s + 1}'
)
plot.show()This broad retrodictive performance is matched by inferences that concentrate around the true configuration of the simulation data generating process.
f, axarr = plot.subplots(2, 1, layout="constrained")
omega_true = (mu_true - mu_true[data['anchor'] - 1]) / sigma_true[0]
names = [ f'omega[{j + 1}]' for j in range(data['J']) ]
putil.plot_disc_pushforward_quantiles(
axarr[0], samples_ng, names,
baseline_values=omega_true,
baseline_color=putil.mid_teal,
xlabel="Alternative",
xticklabels=range(1, data['J'] + 1),
ylabel="Relative Baseline Utility"
)
tau_true = sigma_true / sigma_true[0]
names = [ f'tau[{n + 1}]' for n in range(data['N']) ]
putil.plot_disc_pushforward_quantiles(
axarr[1], samples_ng, names,
baseline_values=tau_true,
baseline_color=putil.mid_teal,
xlabel="Agent",
xticklabels=range(1, data['N'] + 1),
ylabel="Relative Scale"
)
plot.show()Confident that our model is learning the behavior of the true data generating process, let’s investigate some particular substitution patterns for the fifth agent. We begin by examining the inferred choice probabilities for our three distinguished alternatives in the eight choice subset.
Bs = [50, 50, 50]
flims = [ [-3.1, -2.65], [-2.2, -1.9], [-3.25, -2.8] ]
ylims = [ [0, 12], [0, 20], [0, 10] ]
s1_txs = [-2.73, -1.973, -2.9]
s1_tys = [8, 16, 8]
f, axarr = plot.subplots(3, 1, layout="constrained")
for i, j in enumerate(js):
s = 8
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j - 1]
name = f'log_probs[{n_pred},{idx}]'
display_name = f'Log Probability\nof Alternative {j}'
util.plot_expectand_pushforward(
axarr[i], samples_ng[name],
Bs[i], flim=flims[i], ylim=ylims[i],
display_name=display_name,
color=putil.dark,
title=f'Agent {n_pred}'
)
axarr[i].text(s1_txs[i], s1_tys[i],
'Choice\nSubset 8', color=putil.dark,
ha='center', va='center')
plot.show()1 value (0.02%) fell above the histogram binning.
The ninth choice subset is given by adding a new alternative to the eight choice subset. Any choice probability allocated to this new alternative has to taken from the existing alternatives. As expected, we see that the choice probabilities for all three distinguished alternatives decrease.
f, axarr = plot.subplots(3, 1, layout="constrained")
s2_txs = [-3, -2.15, -3.12]
s2_tys = [8, 16, 7]
for i, j in enumerate(js):
s = 8
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j - 1]
name = f'log_probs[{n_pred},{idx}]'
display_name = f'Log Probability\nof Alternative {j}'
util.plot_expectand_pushforward(
axarr[i], samples_ng[name],
Bs[i], flim=flims[i], ylim=ylims[i],
display_name=display_name, color=putil.light,
title=f'Agent {n_pred}'
)
axarr[i].text(s1_txs[i], s1_tys[i],
'Choice\nSubset 8', color=putil.light,
ha='center', va='center')
s = 9
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j - 1]
name = f'log_probs[{n_pred},{idx}]'
util.plot_expectand_pushforward(
axarr[i], samples_ng[name],
Bs[i], flim=flims[i],
color=putil.dark, border="#BBBBBB88",
add=True
)
axarr[i].text(s2_txs[i], s2_tys[i],
'Choice\nSubset 9', color=putil.dark,
ha='center', va='center')
plot.show()1 value (0.02%) fell above the histogram binning.
1 value (0.02%) fell above the histogram binning.
That said, the choice probabilities don’t all decrease by the same amount. In particular, the choice probabilities allocated to j_{1} and j_{2}, which are in the same nest as the new alternative, decrease far more than the choice probability allocated to j_{3}.
As we have seen over and over again at this point, these specific substitution patterns are a bit more evident in ratios of choice probabilities.
ratios = {}
for s in [8, 9]:
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j1 - 1]
name1 = f'log_probs[{n_pred},{idx}]'
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j2 - 1]
name2 = f'log_probs[{n_pred},{idx}]'
idx = subset_start[s - 1] - 1 + to_subset_idx[s - 1][j3 - 1]
name3 = f'log_probs[{n_pred},{idx}]'
ratios[f'{s}_{12}'] = \
util.eval_expectand_pushforward(
samples_ng,
lambda x1, x2: math.exp(x1 - x2),
{'x1': name1, 'x2': name2 }
)
ratios[f'{s}_{13}'] = \
util.eval_expectand_pushforward(
samples_ng,
lambda x1, x2: math.exp(x1 - x2),
{'x1': name1, 'x2': name3 }
)Because alternatives j_{1} and j_{2} are in the same nest, the ratio of their choice probabilities are consistent across choice subsets. In fact, the corresponding marginal posterior distributions perfectly overlap, suggesting that the ratios are equivalent.
B = 30
flim = [0.35, 0.55]
display_name = (f' Probability of Alternative {j1}'
f'\n/ Probability of Alternative {j2}')
title = f'Intra-Nest (Agent {n_pred})'
util.plot_expectand_pushforward(
plot.gca(), ratios['8_12'], B,
flim=flim, ylim=[0, 20],
display_name=display_name,
color=putil.light,
title=title
)
plot.gca().text(0.5, 10, 'Choice\nSubset 8',
color=putil.light,
ha='center', va='center')
util.plot_expectand_pushforward(
plot.gca(), ratios['9_12'], B,
flim=flim,
color=putil.dark,
border="#DDDDDD88",
add=True
)
plot.gca().text(0.39, 10, 'Choice\nSubset 9',
color=putil.dark,
ha='center', va='center')
plot.show()The introduction of the new alternative should siphon more choice probability from alternatives in the same nest, including j_{1}, than alternatives in other nests, including j_{3}. Consequently \frac{ q_{5j_{1}} }{ q_{5j_{3}} } should decrease when we move to the larger choice subset. Indeed, this is exactly the behavior that the nested Gumbel model infers.
B = 30
flim = [0.85, 1.5]
display_name = (f' Probability of Alternative {j1}'
f'\n/ Probability of Alternative {j3}')
title = f'Intra-Nest (Agent {n_pred})'
util.plot_expectand_pushforward(
plot.gca(), ratios['8_13'], B,
flim=flim, ylim=[0, 8],
display_name=display_name, color=putil.light,
title=title
)
plot.gca().text(1.35, 4, 'Choice\nSubset 8',
color=putil.light,
ha='center', va='center')
util.plot_expectand_pushforward(
plot.gca(), ratios['9_13'], B,
flim=flim,
color=putil.dark, border="#DDDDDD88",
add=True
)
plot.gca().text(1, 4, 'Choice\nSubset 9',
color=putil.dark,
ha='center', va='center')
plot.show()5 Conclusion
The nested Gumbel model is reasonably straightforward to implement but allows for substantially more sophisticated substitution patterns than the independent Gumbel model. Moreover, the nested Gumbel model is readily generalized to other generalized extreme value models that are only slightly more difficult to implement efficiently in practice.
In Part III we’ll consider another general technique for elaborating an initial probabilistic utility model, and its implied substitution patterns, with slightly different computational considerations.
Appendix: Onerous Calculations
This appendix is dedicated to the more onerous calculations needed to derive some of the main results in this chapter. Those interested in only the results themselves can safely skip these sections.
A.1 Nested Gumbel Model Calculations
Calculations for the nested Gumbel model are relatively straightforward. They just require very careful organization to avoid being overwhelmed by all of the possible terms.
A.1.1 Choice Probabilities
The first step in calculation the choice probabilities is, as always, differentiating the joint cumulative distribution function, \begin{align*} \frac{ \partial \Pi }{ \partial u_{nj} } &( u_{n1}, \ldots, u_{nJ} ) \\ &= \frac{ \partial }{ \partial u_{nj} } \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \\ &= - \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \frac{ \partial }{ \partial u_{nj} } \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \\ &= - \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \frac{ \partial }{ \partial u_{nj} } \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } \\ &= - \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \\ &\quad\quad \lambda_{k(j)} \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \frac{-1}{ \lambda_{k(j)} } e^{ - \frac{ u_{nj} - \mu_{nj} }{ \lambda_{k(j)} } } \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ - \frac{ u_{nj'} - \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } e^{ - \frac{ u_{nj} - \mu_{nj} }{ \lambda_{k(j)} } }. \end{align*}
Next, we evaluate each argument on the target utility and simplify, \begin{align*} \frac{ \partial \Pi }{ \partial u_{nj} } &( u_{nj}, \ldots, u_{nj} ) \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj} - \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ - \frac{ u_{nj} - \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } e^{ - \frac{ u_{nj} - \mu_{nj} }{ \lambda_{k(j)} } } \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ - \frac{ u_{nj} }{ \lambda_{k} } } \, e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \\ &\quad\quad \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ - \frac{ u_{nj} }{ \lambda_{k(j)} } } \, e^{ \frac{ \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } e^{ - \frac{ u_{nj} }{ \lambda_{k(j)} } } \, e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \\ &= \exp \left( - e^{ - u_{nj} } \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \\ &\quad\quad \left( e^{ - \frac{ u_{nj} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } e^{ - \frac{ u_{nj} }{ \lambda_{k(j)} } } \, e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, \left( e^{ - \frac{ u_{nj} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} } \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \\ &\quad\quad \exp \left( - e^{ - u_{nj} } \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } \right) \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{2} \right). \end{align*} where C_{1} = \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k(j)} } } and C_{2} = \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} }.
At this point, we integrate, \begin{align*} q_{nj} &= \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, \frac{ \partial \Pi }{ \partial x_{j} } ( u_{nj}, \ldots, u_{nj} ) \\ &= \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{2} \right) \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{2} \right) \end{align*}
To proceed further, we make the substitution z = \phi( u_{nj} ) = - e^{ - u_{nj} } \, C_{2} with \mathrm{d} z = \mathrm{d} u_{nj} \, e^{ - u_{nj} } \, C_{2} and \begin{align*} \phi( - \infty ) &= - \infty \\ \phi( + \infty ) &= 0. \end{align*}
This gives \begin{align*} q_{nj} &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{2} \right) \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, \int_{-\infty}^{0} \mathrm{d} z \, \frac{1}{C_{2}} \, \exp( - z ) \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, \frac{1}{C_{2}} \bigg[ \exp( 0 ) - \exp( - \infty) \bigg] \\ &= e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, C_{1}^{ \lambda_{k(j)} - 1 } \, \frac{1}{C_{2}} \\ &= \frac{ e^{ \frac{ \mu_{nj} }{ \lambda_{k(j)} } } \, \left( \sum_{j' \in \mathsf{b}_{k(j)}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} e^{ \frac{ \mu_{nj'} }{ \lambda_{k} } } \right)^{ \lambda_{k} } }. \end{align*}
A.1.2 Sensitivities
Calculating sensitivities, and then elasticities, requires even more careful organization. First, we have \begin{align*} \frac{ \partial q_{nj} }{ \partial \nu_{nj'} } &= \frac{ \partial }{ \partial \nu_{nj'} } \left[ \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \, \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \right] \\ &= C_{1} + C_{2} + C_{3} \end{align*}
These terms are \begin{align*} C_{1} &= \frac{ \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \frac{ \partial }{ \partial \nu_{nj'} } \left[ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \right] \\ &= \frac{ \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \, e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \\ &= \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \, \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &= \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \, q_{nj} \end{align*} and \begin{align*} C_{2} &=\quad \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &\quad \cdot \frac{ \partial }{ \partial \nu_{nj'} } \left[ \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \right] \\ &=\quad \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &\quad \cdot \delta_{k(j')k(j)} \, ( \lambda_{k(j)} - 1 ) \, \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 2 } \, \frac{1}{ \lambda_{k(j)} } \, e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j)} } } \\ &=\quad \delta_{k(j')k(j)} \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } \frac{ e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j)} } } }{ \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } } \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &=\quad \delta_{k(j')k(j)} \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \, q_{nj} \end{align*} and then \begin{align*} C_{3} &=\quad e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \\ &\quad \cdot \frac{ \partial }{ \partial \nu_{nj'} } \left[ \frac{ 1 }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \right] \\ &=\quad e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } \\ &\quad \cdot (-1) \frac{ 1 }{ \left( \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } \right)^{2} } \, \lambda_{k(j')} \, \left( \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \right)^{ \lambda_{k(j')} } \, \frac{1}{ \lambda_{k(j')} } \, e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j')} } } \\ &=-\quad \frac{ e^{ \frac{ \nu_{nj} }{ \lambda_{k(j)} } } \left( \sum_{j'' \in \mathsf{b}_{k(j)}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j)} } } \right)^{ \lambda_{k(j)} - 1 } }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \\ &\hphantom{-}\quad \cdot \frac{ e^{ \frac{ \nu_{nj'} }{ \lambda_{k(j')} } } \left( \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \right)^{ \lambda_{k(j')} - 1} }{ \sum_{k' = 1}^{K} \left( \sum_{j'' \in \mathsf{b}_{k'}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k'} } } \right)^{ \lambda_{k'} } } \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \\ &= - q_{nj} \, q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } }. \end{align*}
Substituting these intermediate results back in then gives \begin{align*} \frac{ \partial q_{nj} }{ \partial \nu_{nj'} } &=\quad C_{1} + C_{2} + C_{3} \\ &=\quad \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \, q_{nj} \\ &\quad+ \delta_{k(j')k(j)} \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \, q_{nj} \\ &\quad- q_{nj} \, q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \\ &= \Bigg( \quad \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \\ &\hspace{7mm} + \delta_{k(j')k(j)} \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \\ &\hspace{7mm} - q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \hspace{28mm} \Bigg) q_{nj}. \end{align*}
The elasticities immediately follow, \begin{align*} \epsilon_{njj'} &= \frac{ \mu_{nj'} }{ q_{nj} } \, \frac{ \partial q_{nj_{1}} }{ \partial \nu_{nj'} } \\ &= \frac{ \mu_{nj'} }{ q_{nj} } \, \Bigg( \quad \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \\ &\hspace{7mm} + \delta_{k(j')k(j)} \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \\ &\hspace{7mm} - q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \hspace{28mm} \Bigg) q_{nj} \\ &= \Bigg( \quad \delta_{j'j} \, \frac{1}{ \lambda_{k(j)} } \\ &\hspace{7mm} + \delta_{k(j')k(j)} \, \frac{\lambda_{k(j)} - 1}{ \lambda_{k(j)} } q_{ nj' \mid k(j) } \\ &\hspace{7mm} - q_{nj'} \, \sum_{j'' \in \mathsf{b}_{k(j')}} e^{ \frac{ \nu_{nj''} }{ \lambda_{k(j')} } } \hspace{28mm} \Bigg) \mu_{nj'}. \end{align*}
A.2 Generalized Nested Gumbel Model Calculations
Recall that the generalized nested Gumbel model is defined by the joint cumulative distribution function \Pi( u_{n1}, \ldots, u_{nJ} ) = \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j \in \mathsf{b}_{k}} \left( \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right).
Differentiating this joint cumulative distribution function is a little bit more straightforward if we first define the subset of nests that contain each alternative, \mathsf{a}_{j}, so that k \in \mathsf{a}_{j} if and only if j \in \mathsf{b}_{k}.
Now we can turn return to the chain rule mines, \begin{align*} \frac{ \partial \Pi }{ \partial u_{nj} } &( u_{n1}, \ldots, u_{nJ} ) \\ &= \frac{ \partial }{ \partial u_{nj} } \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj'} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj'} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \lambda_{k} \, \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj'} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \frac{1}{\lambda_{k}} \, \left( \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, \alpha_{jk} \, e^{ - u_{nj'} + \mu_{nj'} } \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj'} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj'} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} }. \end{align*}
Next we evaluate each argument on the active utility and simplify as much as possible, \begin{align*} \frac{ \partial \Pi }{ \partial u_{nj} } &( u_{nj}, \ldots, u_{nj} ) \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj} + \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, \alpha_{jk} \, e^{ - u_{nj} + \mu_{nj} } \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj} } e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ - u_{nj} } e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( \alpha_{jk} \, e^{ - u_{nj} } e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, \alpha_{jk} \, e^{ - u_{nj} } e^{ \mu_{nj} } \\ &= \exp \left( - \sum_{k = 1}^{K} \left( \left( e^{ - u_{nj} } \right)^{\frac{1}{\lambda_{k}}} \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( \left( e^{ - u_{nj} } \right)^{\frac{1}{\lambda_{k}}} \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( e^{ -u_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, e^{ - u_{nj} } \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} - 1} \, \alpha_{jk} \, e^{ \mu_{nj} } \\ &= \exp \left( - e^{ - u_{nj} } \, \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( e^{ - u_{nj} } \right)^{\frac{\lambda_{k} - 1}{\lambda_{k}}} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( e^{ -u_{nj} } \right)^{\frac{1}{\lambda_{k}}} \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}}}. \end{align*}
Pushing forward with the simplifications, \begin{align*} \frac{ \partial \Pi }{ \partial u_{nj} } &( u_{nj}, \ldots, u_{nj} ) \\ &= \exp \left( - e^{ - u_{nj} } \, \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( e^{ - u_{nj} } \right)^{\frac{\lambda_{k} - 1}{\lambda_{k}}} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( e^{ - u_{nj} } \right)^{\frac{1}{\lambda_{k}}} \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} } \\ &= \exp \left( - e^{ - u_{nj} } \, \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } \left( e^{ - u_{nj} } \right)^{\frac{\lambda_{k} - 1 + 1}{\lambda_{k}}} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \\ &\quad\quad\quad\;\; \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} } \\ &= \exp \left( - e^{ - u_{nj} } \, \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad \sum_{k \in \mathsf{a}_{j} } e^{ - u_{nj} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} } \\ &= \exp \left( - e^{ - u_{nj} } \, \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } \right) \\ &\quad e^{ - u_{nj} } \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}} } \\ &\equiv \exp \left( - e^{ - u_{nj} } \, C_{1} \right) \, e^{ - u_{nj} } \, C_{2} \end{align*}
At this point, the choice probabilities are given by integration, \begin{align*} q_{nj} &= \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, \frac{ \partial \Pi }{ \partial x_{j} } ( u_{nj}, \ldots, u_{nj} ) \\ &= \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, \exp \left( - e^{ - u_{nj} } \, C_{1} \right) \, e^{ - u_{nj} } \, C_{2} \\ &= C_{2} \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{1} \right) \end{align*}
To evaluate this integral, we employ the substitution z = \phi( u_{nj} ) = - e^{ - u_{nj} } \, C_{1} with \mathrm{d} z = \mathrm{d} u_{nj} e^{ - u_{nj} } \, C_{1} and \begin{align*} \phi( - \infty ) &= -\infty \\ \phi( + \infty ) &= 0. \end{align*}
This gives \begin{align*} q_{nj} &= C_{2} \int_{-\infty}^{+\infty} \mathrm{d} u_{nj} \, e^{ - u_{nj} } \, \exp \left( - e^{ - u_{nj} } \, C_{1} \right) \\ &= \frac{ C_{2} }{ C_{1} } \, \int_{-\infty}^{0} \mathrm{d} z \, \exp \left( z \right) \\ &= \frac{ C_{2} }{ C_{1} } \, \bigg[ \exp \left( 0 \right) - \exp( - \infty ) \bigg] \\ &= \frac{ C_{2} }{ C_{1} } \\ &= \frac{ \sum_{k \in \mathsf{a}_{j} } \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} - 1 } \, \left( \alpha_{jk} \, e^{ \mu_{nj} } \right)^{\frac{1}{\lambda_{k}}} \, }{ \sum_{k = 1}^{K} \left( \sum_{j' \in \mathsf{b}_{k}} \left( \alpha_{j'k} \, e^{ \mu_{nj'} } \right)^{\frac{1}{\lambda_{k}}} \right)^{ \lambda_{k} } }. \end{align*}
A.3 Generalized Extreme Value Model Calculations
Applying the generalized extreme value family to alternative utilities gives the joint cumulative distribution functions \begin{align*} \Pi( u_{n1}&, \ldots, y_{nJ} ) \\ &= \exp \bigg( - l \big( -\log \Pi_{1}(u_{n1}), \ldots, -\log \Pi_{J}(y_{nJ}) \big) \bigg). \end{align*}
If we restrict the input cumulative distribution functions to the Gumbel family with varying baselines and common scales, \begin{align*} \Pi_{j}(u_{nj}) &= \Pi_{\mathrm{Gumbel}}(u_{nj} \mid \mu_{nj}, \sigma_{n}) \\ &= \exp \left( - e^{ \frac{ u_{nj} - \mu_{nj} }{ \sigma_{n} } } \right). \end{align*} then the multivariate extreme value family reduces to \begin{align*} \Pi( u_{n1}, \ldots, u_{nJ} ) &= \exp \bigg( - l \big( -\log \Pi_{\mathrm{Gumbel}}(u_{n1} \mid \mu_{n1}, \sigma_{n}), \\ & \hspace{21mm} \ldots, \\ & \hspace{21mm} -\log \Pi_{\mathrm{Gumbel}}(u_{nj} \mid \mu_{nJ}, \sigma_{n}) \; \big) \; \bigg) \\ &= \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} - \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( \frac{ u_{nJ} - \mu_{nJ} }{ \sigma_{n} } \right) \Bigg) \Bigg). \end{align*}
To keep the equations a more notationally manageable, let’s define the intermediate terms \eta_{nj} = \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right) so that we can write the joint cumulative distribution functions a bit more compactly, \begin{align*} \Pi( u_{n1}&, \ldots, u_{nJ} ) \\ &= \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} - \mu_{n1} }{ \sigma_{n} } \right), \\ & \hspace{21mm} \ldots, \\ & \hspace{21mm} \exp \left( \frac{ u_{nJ} - \mu_{nJ} }{ \sigma_{n} } \right) \Bigg) \Bigg) \\ &= \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} }{ \sigma_{n} } \right) \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right), \\ & \hspace{21mm} \ldots, \\ & \hspace{21mm} \exp \left( \frac{ u_{nJ} }{ \sigma_{n} } \right) \exp \left( - \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \Bigg) \Bigg) \\ &= \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} }{ \sigma_{n} } \right) \, \eta_{n1}, \\ & \hspace{21mm} \ldots, \\ & \hspace{21mm} \exp \left( \frac{ u_{nJ} }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \Bigg). \end{align*}
To compute choice probabilities we follow the standard procedure. First, we differentiate the joint cumulative distribution function, \begin{align*} \frac{ \partial \Pi }{ \partial x_{j} } ( u_{n1}&, \ldots, u_{nJ} ) \\ &= \frac{ \partial }{ \partial u_{nj} } \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u_{nJ} }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \Bigg) \\ &=\;\; \exp \Bigg( - l \Bigg( \exp \left( \frac{ u_{n1} }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u_{nJ} }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \Bigg) \\ &\quad\cdot \frac{ \partial l }{ \partial z_{j} } \Bigg( \exp \left( \frac{ u_{n1} }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u_{nJ} }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \\ &\quad\cdot \exp \left( \frac{ u_{nj} }{ \sigma_{n} } \right) \frac{\eta_{nj} }{\sigma_{n}}. \end{align*}
Second, we set all of the component utilities to the same value, \begin{align*} \frac{ \partial \Pi }{ \partial x_{j} } ( &u, \ldots, u ) \\ &=\;\; \exp \Bigg( - l \Bigg( \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \Bigg) \\ &\quad\cdot \frac{ \partial l }{ \partial z_{j} } \Bigg( \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \\ &\quad\cdot \exp \left( \frac{ u }{ \sigma_{n} } \right) \frac{\eta_{nj} }{\sigma_{n}}. \end{align*}
Lastly, we derive the choice probabilities by integrating this derivative, q_{nj} = \int_{-\infty}^{+\infty} \mathrm{d} u \, \frac{ \partial \Pi }{ \partial x_{j} } ( u, \ldots, u ). Unfortunately, evaluating this integral analytically will not generally be feasible.
If, however, l is a homogeneous function of degree one, \begin{align*} l ( \rho \, z_{1}, \ldots, \rho \, z_{J} ) &= \rho \, l( z_{1}, \ldots, z_{J} ) \\ \frac{ \partial l}{ \partial z_{j} } ( \rho \, z_{1}, \ldots, \rho \, z_{J} ) &= \frac{ \partial l}{ \partial z_{j} } ( z_{1}, \ldots, z_{J} ), \end{align*} then the integral becomes much more manageable.
In this case, the integrand reduces to \begin{align*} \frac{ \partial \Pi }{ \partial x_{j} } ( &u, \ldots, u ) \\ &=\;\; \exp \Bigg( - l \Bigg( \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \Bigg) \\ &\quad\cdot \frac{ \partial l }{ \partial z_{j} } \Bigg( \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{n1}, \ldots, \exp \left( \frac{ u }{ \sigma_{n} } \right) \, \eta_{nJ} \Bigg) \\ &\quad\cdot \exp \left( \frac{ u }{ \sigma_{n} } \right) \frac{\eta_{nj} }{\sigma_{n}} \\ &=\;\; \exp \Bigg( -\exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \Bigg) \\ &\quad\cdot \frac{ \partial l }{ \partial z_{j} } \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \\ &\quad\cdot \exp \left( \frac{ u }{ \sigma_{n} } \right) \frac{\eta_{nj} }{\sigma_{n}}, \end{align*} and the choice probabilities become \begin{align*} q_{nj} &= \int_{-\infty}^{+\infty} \mathrm{d} u \, \frac{ \partial \Pi }{ \partial x_{j} } ( u, \ldots, u ) \\ &= \int_{-\infty}^{+\infty} \mathrm{d} u \, \;\; \exp \Bigg( -\exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \Bigg) \\ &\hspace{19mm} \cdot \frac{ \partial l }{ \partial z_{j} } \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \exp \left( \frac{ u }{ \sigma_{n} } \right) \frac{\eta_{nj} }{\sigma_{n}} \\ &= \frac{ \partial l }{ \partial z_{j} } \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \frac{\eta_{nj} }{\sigma_{n}} \\ &\quad \int_{-\infty}^{+\infty} \mathrm{d} u \, \exp \Bigg( -\exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \Bigg) \exp \left( \frac{ u }{ \sigma_{n} } \right). \end{align*}
At this point, we make the substitution w = - \exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) with \mathrm{d} w = \mathrm{d} u \, \frac{1}{\sigma_{n}} \exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big). Because l is a positive function, the new limits become \begin{align*} w(-\infty) &= 0 \\ w(+\infty) &= -\infty. \end{align*}
Under this substitution, the choice probabilities can be written as \begin{align*} q_{nj} &= \frac{ \partial l }{ \partial z_{j} } \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \frac{\eta_{nj} }{\sigma_{n}} \\ &\quad \int_{-\infty}^{+\infty} \mathrm{d} u \, \exp \Bigg( -\exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \Bigg) \exp \left( \frac{ u }{ \sigma_{n} } \right) \\ &= \frac{ \frac{ \partial l }{ \partial z_{j} } \, \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \, \eta_{nj} }{ l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) } \\ &\quad \int_{-\infty}^{+\infty} \left[ \mathrm{d} u \, \frac{1}{\sigma_{n}} \exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \right] \\ & \hspace{14mm} \exp \Bigg( -\exp \left( \frac{ u }{ \sigma_{n} } \right) l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \Bigg) \\ &= \frac{ \frac{ \partial l }{ \partial z_{j} } \, \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \, \eta_{nj} }{ l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) } \int_{0}^{-\infty} \mathrm{d} w \, \exp (w) \end{align*}
Conveniently, this integral can be evaluated immediately, \int_{0}^{-\infty} \mathrm{d} w \, \exp (w) = 1, to give the choice probabilities \begin{align*} q_{nj} &= \frac{ \frac{ \partial l }{ \partial z_{j} } \, \big( \eta_{n1}, \ldots, \eta_{nJ} \big) \, \eta_{nj} }{ l \big( \eta_{n1}, \ldots, \eta_{nJ} \big) } \\ &= \frac{ \frac{ \partial l }{ \partial z_{j} } \, \Big( \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( - \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \Big) \, \exp \left( - \frac{ \mu_{nj} }{ \sigma_{n} } \right) }{ l \Big( \exp \left( - \frac{ \mu_{n1} }{ \sigma_{n} } \right), \ldots, \exp \left( - \frac{ \mu_{nJ} }{ \sigma_{n} } \right) \Big) }. \end{align*}
Instead of starting with a cumulative distribution function and following the standard operations each time, we can derive the choice probabilities directly from the function l. Moreover, because positive, homogeneous functions of degree one are reasonably straightforward to engineer, this generalized extreme value construction facilitates a diversity of discrete choice models.
Acknowledgements
I thank Andrés Castro Araújo, jd, and Fred Feinberg for helpful comments.
A very special thanks to everyone supporting me on Patreon: Alessandro Varacca, Alex D, Alexander Noll, Amit, Andrea Serafino, Andrew Mascioli, Andrew Rouillard, Ara Winter, Ari Holtzman, Austin Rochford, Aviv Keshet, Avraham Adler, Ben Matthews, Ben Swallow, Benoit Essiambre, boot, Brendan Galdo, Bryan Chang, Cameron Smith, Canaan Breiss, Cat Shark, Cathy Oliveri, Charles Naylor, Chase Dwelle, Chris Jones, Christina Van Heer, Christopher Mehrvarzi, Colin Carroll, Colin McAuliffe, Damien Mannion, dan mackinlay, Dan W Joyce, Dan Waxman, Dan Weitzenfeld, Daniel Edward Marthaler, Daniel Hammarström, Danny Van Nest, David Burdelski, Doug Rivers, Dr. Jobo, Dr. Omri Har Shemesh, Dylan Maher, Dylan Spielman, Ebriand, Ed Cashin, Edgar Merkle, Edoardo Marcora, Eric LaMotte, Erik Banek, Eugene O’Friel, Felipe Vaca, Fergus Chadwick, Francesco Corona, Geoff Rollins, Glenn Williams, Granville Matheson, Gregor Gorjanc, Guilherme Marthe, Håkan Johansson, Hamed Bastan-Hagh, haubur, Hector Munoz, hs, Hugo Botha, Ian Costley, idontgetoutmuch, Ignacio Vera, Ilaria Prosdocimi, Isaac Vock, Isidor Belic, jacob pine, Jair Andrade, James C, James Hodgson, James Wade, Janek Berger, Jarrett Byrnes, Jason Martin, Jason Pekos, Jason Wong, jd, Jeff Burnett, Jeff Dotson, Jeff Helzner, Jeffrey Erlich, Jerry Lin , Jessica Graves, Joe Sloan, John Flournoy, Jonathan H. Morgan, Jonathon Vallejo, Josh Knecht, JU, Julian Lee, Justin Bois, Kádár András, Karim Naguib, Karim Osman, Konstantin Shakhbazov, Kristian Gårdhus Wichmann, Lars Barquist, lizzie , LOU ODETTE, Mads Christian Hansen, Marek Kwiatkowski, Mariana Carmona, Mark Donoghoe, Markus P., Márton Vaitkus, Matěj, Matthew, Matthieu LEROY, Mattia Arsendi, Maurits van der Meer, Max, Michael Colaresi, Michael DeJesus, Michael DeWitt, Michael Dillon, Michael Lerner, Mick Cooney, MisterMentat , N Sanders, N.S. , Name, Nathaniel Burbank, Nicholas Cowie, Nick S, Nikita Karetnikov, Octavio Medina, Ole Rogeberg, Olivier Ma, Patrick Kelley, Patrick Boehnke, Pau Pereira Batlle, Pieter van den Berg , ptr, quasar, Ramiro Barrantes Reynolds, Raúl Peralta Lozada, Rex, Riccardo Fusaroli, Richard Nerland, Rob Davies, Robert Frost, Robert Goldman, Robert kohn, Robin Taylor, Ryan Gan, Ryan Grossman, Ryan Kelly, S Hong, Sean Wilson, Sergiy Protsiv, Seth Axen, shira, Simon Duane, Simon Lilburn, Simon Steiger, Simone, Spencer Boucher, sssz, Stefan Lorenz, Stephen Lienhard, Steve Harris, Steven Forrest, Stew Watts, Stone Chen, Susan Holmes, Svilup, Tate Tunstall, Tatsuo Okubo, Teresa Ortiz`, Theodore Dasher, Thomas Siegert, Thomas Vladeck, Tobychev , Tomas Capretto, Tony Wuersch, Virgile Andreani, Virginia Fisher, Vitalie Spinu, Vladimir Markov, VO2 Maximus Decius, Will Farr, Will Lowe, Will Wen, Xianda Sun, yolhaj , yureq , Zach A, and Zhengchen Cai.
References
License
The code in this case study is copyrighted by Michael Betancourt and licensed under the new BSD (3-clause) license:
https://opensource.org/licenses/BSD-3-Clause
The text and figures in this chapter are copyrighted by Michael Betancourt and licensed under the CC BY-NC 4.0 license:
Original Computing Environment
from watermark import watermark
print(watermark())Last updated: 2026-12-02T17:35:01.071391-05:00
Python implementation: CPython
Python version : 3.12.10
IPython version : 9.6.0
Compiler : Clang 13.0.0 (clang-1300.0.29.30)
OS : Darwin
Release : 24.6.0
Machine : x86_64
Processor : i386
CPU cores : 16
Architecture: 64bit
print(watermark(packages="matplotlib,numpy,stan,scipy"))matplotlib: 3.10.7
numpy : 2.3.4
stan : 3.10.0
scipy : 1.16.3