Discrete Choice and Random Utility Models#

Attention

This notebook uses libraries that are not PyMC dependencies and therefore need to be installed specifically to run this notebook. Open the dropdown below for extra guidance.

Extra dependencies install instructions

In order to run this notebook (either locally or on binder) you won’t only need a working PyMC installation with all optional dependencies, but also to install some extra dependencies. For advise on installing PyMC itself, please refer to Installation

You can install these dependencies with your preferred package manager, we provide as an example the pip and conda commands below.

$ pip install jax, jaxlib, numpyro

Note that if you want (or need) to install the packages from inside the notebook instead of the command line, you can install the packages by running a variation of the pip command:

import sys

!{sys.executable} -m pip install jax, jaxlib, numpyro

You should not run !pip install as it might install the package in a different environment and not be available from the Jupyter notebook even if installed.

Another alternative is using conda instead:

$ conda install jax, jaxlib, numpyro

when installing scientific python packages with conda, we recommend using conda forge

import arviz as az
import numpy as np
import pandas as pd
import pymc as pm
import xarray as xr

from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
%config InlineBackend.figure_format = 'retina'  # high resolution figures
az.style.use("arviz-variat")
rng = np.random.default_rng(42)

Discrete Choice Modelling: The Idea#

Discrete choice modelling is related to the idea of a latent utility scale as discussed in Regression Models with Ordered Categorical Outcomes, but it generalises the idea to decision making. It posits that human decision making can be modelled as a function of latent/subjective utility measurements over a set of mutually exclusive alternatives. The theory states that any decision maker will go with the option that maximises their subjective utility, and that utility can be modelled as a latent linear function of observable features of the world.

The idea is perhaps most famously applied by Daniel McFadden in the 1970s to predict the market share accruing to transportation choices (i.e. car, rail, walking etc..) in California after the proposed introduction of BART light rail system. It’s worth pausing on that point. The theory is one of micro level human decision making that has, in real applications, been scaled up to make broadly accurate macro level predictions. For more details we recommend

We don’t need to be too credulous either. This is merely a statistical model and success here is entirely dependent on the skill of modeller and the available measurements coupled with plausible theory. But it’s worth noting the scale of the ambition underlying these models. The structure of the model encourages you to articulate your theory of the decision makers and the environment they inhabit.

The Data#

In this example, we’ll examine the technique of discrete choice modelling using a (i) heating system data set from the R mlogit package and (ii) repeat choice data set over cracker brands. We’ll be pursuing a Bayesian approach to estimating the models rather than the MLE methodology reported in their vigenette. The first data set shows household choices over offers of heating systems in California. The observations consist of single-family houses in California that were newly built and had central air-conditioning. Five types of systems are considered to have been possible:

  • gas central (gc),

  • gas room (gr),

  • electric central (ec),

  • electric room (er),

  • heat pump (hp).

The data set reports the installation ic.alt and operating costs oc.alt each household was faced with for each of the five alternatives with some broad demographic information about the household and crucially the choice depvar. This is what one choice scenario over the five alternative looks like in the data:

try:
    wide_heating_df = pd.read_csv("../data/heating_data_r.csv")
except:
    wide_heating_df = pd.read_csv(pm.get_data("heating_data_r.csv"))

wide_heating_df[wide_heating_df["idcase"] == 1]
idcase depvar ic.gc ic.gr ic.ec ic.er ic.hp oc.gc oc.gr oc.ec oc.er oc.hp income agehed rooms region
0 1 gc 866.0 962.64 859.9 995.76 1135.5 199.69 151.72 553.34 505.6 237.88 7 25 6 ncostl

The core idea of these kinds of models is to conceive of this scenario as a choice over exhaustive options with attached latent utility. The utility ascribed to each option is viewed as a linear combination of the attributes for each option. The utility ascribed to each alternative drives the probability of choosing amongst each option. For each \(j\) in all the alternatives \(Alt = \{ gc, gr, ec, er, hp \}\) which is assumed to take a Gumbel distribution because this has a particularly nice mathematical property.

\[ \mathbf{U} \sim Gumbel \]
\[\begin{split} \begin{pmatrix} u_{gc} \\ u_{gr} \\ u_{ec} \\ u_{er} \\ u_{hp} \\ \end{pmatrix} = \begin{pmatrix} gc_{ic} & gc_{oc} \\ gr_{ic} & gr_{oc} \\ ec_{ic} & ec_{oc} \\ er_{ic} & er_{oc} \\ hp_{ic} & hp_{oc} \\ \end{pmatrix} \begin{pmatrix} \beta_{ic} \\ \beta_{oc} \\ \end{pmatrix} \end{split}\]

This assumption proves to be mathematically convenient because the difference between two Gumbel distributions can be modelled as a logistic function, meaning we can model a contrast difference among multiple alternatives with the softmax function. Details of the derivation can be found in

\[ \text{softmax}(u)_{j} = \frac{\exp(u_{j})}{\sum_{q=1}^{J}\exp(u_{q})} \]

The model then assumes that decision maker chooses the option that maximises their subjective utility. The individual utility functions can be richly parameterised. The model is identified just when the utility measures of the alternatives are benchmarked against the fixed utility of the “outside good.” The last quantity is often fixed at 0 to aid parameter identification on alternative-specific parameters as we’ll see below.

\[\begin{split}\begin{pmatrix} u_{gc} \\ u_{gr} \\ u_{ec} \\ u_{er} \\ 0 \\ \end{pmatrix} \end{split}\]

With all these constraints applied we can build out conditional random utility model and it’s hierarchical variants. Like nearly all subjects in statistics the precise vocabulary for the model specification is overloaded. The conditional logit parameters \(\beta\) may be fixed at the level of the individual, but can vary across individuals and the alternatives gc, gr, ec, er too. In this manner we can compose an elaborate theory of how we expect drivers of subjective utility to change the market share amongst a set of competing goods.

Digression on Data Formats#

Discrete choice models are often estimated using a long-data format where each choice scenario is represented with a row per alternative ID and a binary flag denoting the chosen option in each scenario. This data format is recommended for estimating these kinds of models in stan and in pylogit. The reason for doing this is that once the columns installation_costs and operating_costs have been pivoted in this fashion it’s easier to include them in matrix calculations.

try:
    long_heating_df = pd.read_csv("../data/long_heating_data.csv")
except:
    long_heating_df = pd.read_csv(pm.get_data("long_heating_data.csv"))

columns = [c for c in long_heating_df.columns if c != "Unnamed: 0"]
long_heating_df[long_heating_df["idcase"] == 1][columns]
idcase alt_id choice depvar income agehed rooms region installation_costs operating_costs
0 1 1 1 gc 7 25 6 ncostl 866.00 199.69
1 1 2 0 gc 7 25 6 ncostl 962.64 151.72
2 1 3 0 gc 7 25 6 ncostl 859.90 553.34
3 1 4 0 gc 7 25 6 ncostl 995.76 505.60
4 1 5 0 gc 7 25 6 ncostl 1135.50 237.88

The Basic Model#

We will show here how to incorporate the utility specifications in PyMC. PyMC is a nice interface for this kind of modelling because it can express the model quite cleanly following the natural mathematical expression for this system of equations. You can see in this simple model how we go about constructing equations for the utility measure of each alternative seperately, and then stacking them together to create the input matrix for our softmax transform.

N = wide_heating_df.shape[0]
observed = pd.Categorical(wide_heating_df["depvar"]).codes
coords = {
    "alts_probs": ["ec", "er", "gc", "gr", "hp"],
    "obs": range(N),
}

with pm.Model(coords=coords) as model_1:
    beta_ic = pm.Normal("beta_ic", 0, 1)
    beta_oc = pm.Normal("beta_oc", 0, 1)

    ## Construct Utility matrix and Pivot
    u0 = beta_ic * wide_heating_df["ic.ec"] + beta_oc * wide_heating_df["oc.ec"]
    u1 = beta_ic * wide_heating_df["ic.er"] + beta_oc * wide_heating_df["oc.er"]
    u2 = beta_ic * wide_heating_df["ic.gc"] + beta_oc * wide_heating_df["oc.gc"]
    u3 = beta_ic * wide_heating_df["ic.gr"] + beta_oc * wide_heating_df["oc.gr"]
    u4 = beta_ic * wide_heating_df["ic.hp"] + beta_oc * wide_heating_df["oc.hp"]
    s = pm.math.stack([u0, u1, u2, u3, u4]).T

    ## Apply Softmax Transform
    p_ = pm.Deterministic("p", pm.math.softmax(s, axis=1), dims=("obs", "alts_probs"))

    ## Likelihood
    choice_obs = pm.Categorical("y_cat", p=p_, observed=observed, dims="obs")

    idata_m1 = pm.sample_prior_predictive()
    idata_m1.update(pm.sample(nuts_sampler="numpyro", random_seed=101))
    pm.compute_log_likelihood(idata_m1, extend_inferencedata=True)
    pm.sample_posterior_predictive(idata_m1, extend_inferencedata=True)

pm.model_to_graphviz(model_1)
Sampling: [beta_ic, beta_oc, y_cat]
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/pymc/sampling/mcmc.py:832: FutureWarning: The arguments to `from_dict` have changed with the release of arviz 1.0. Please refer to the arviz documentation for more details
  return _sample_external_nuts(

Sampling: [y_cat]

../_images/ab3eac7d2797841f6672f4ecd9cdb43230dc8d769573a0f33477066f53b5a7eb.svg

Note that you need to be careful with the encoding of the outcome variable. The categorical ordering should reflect the ordering of the utilities as they are stacked into the softmax transform and then fed into the likelihood term.

idata_m1
<xarray.DataTree>
Group: /
├── Group: /prior
│       Dimensions:     (chain: 1, draw: 500, obs: 900, alts_probs: 5)
│       Coordinates:
│         * chain       (chain) int64 8B 0
│         * draw        (draw) int64 4kB 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499
│         * obs         (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 893 894 895 896 897 898 899
│         * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
│       Data variables:
│           beta_ic     (chain, draw) float64 4kB 0.3753 0.6784 -1.288 ... 1.664 0.3335
│           p           (chain, draw, obs, alts_probs) float64 18MB 2.351e-100 ... 0....
│           beta_oc     (chain, draw) float64 4kB -0.3994 -1.02 1.18 ... 0.5171 0.4634
│       Attributes:
│           created_at:                 2026-04-26T09:23:49.190834+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           inference_library:          pymc
│           inference_library_version:  5.28.0+58.gf58491a3
│           sample_dims:                ['chain', 'draw']
├── Group: /prior_predictive
│       Dimensions:  (chain: 1, draw: 500, obs: 900)
│       Coordinates:
│         * chain    (chain) int64 8B 0
│         * draw     (draw) int64 4kB 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499
│         * obs      (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 892 893 894 895 896 897 898 899
│       Data variables:
│           y_cat    (chain, draw, obs) int64 4MB 4 4 4 4 3 3 4 3 3 ... 1 1 1 1 1 1 1 4
│       Attributes:
│           created_at:                 2026-04-26T09:23:49.192446+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           inference_library:          pymc
│           inference_library_version:  5.28.0+58.gf58491a3
│           sample_dims:                ['chain', 'draw']
├── Group: /observed_data
│       Dimensions:  (obs: 900)
│       Coordinates:
│         * obs      (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 892 893 894 895 896 897 898 899
│       Data variables:
│           y_cat    (obs) int64 7kB 2 2 2 1 1 2 2 2 2 2 2 2 ... 2 2 2 2 2 2 1 2 2 2 2 2
│       Attributes:
│           created_at:                 2026-04-26T09:23:54.263110+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           inference_library:          pymc
│           inference_library_version:  5.28.0+58.gf58491a3
│           sample_dims:                []
├── Group: /posterior
│       Dimensions:     (chain: 4, draw: 1000, obs: 900, alts_probs: 5)
│       Coordinates:
│         * chain       (chain) int64 32B 0 1 2 3
│         * draw        (draw) int64 8kB 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
│         * obs         (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 893 894 895 896 897 898 899
│         * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
│       Data variables:
│           beta_ic     (chain, draw) float64 32kB ...
│           beta_oc     (chain, draw) float64 32kB ...
│           p           (chain, draw, obs, alts_probs) float64 144MB ...
│       Attributes:
│           created_at:                 2026-04-26T09:23:52.977924+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           sample_dims:                ['chain', 'draw']
│           inference_library:          numpyro
│           inference_library_version:  0.20.1
│           sampling_time:              1.773861
│           tuning_steps:               1000
├── Group: /sample_stats
│       Dimensions:          (chain: 4, draw: 1000)
│       Coordinates:
│         * chain            (chain) int64 32B 0 1 2 3
│         * draw             (draw) int64 8kB 0 1 2 3 4 5 6 ... 994 995 996 997 998 999
│       Data variables:
│           acceptance_rate  (chain, draw) float64 32kB ...
│           step_size        (chain, draw) float64 32kB ...
│           diverging        (chain, draw) bool 4kB ...
│           energy           (chain, draw) float64 32kB ...
│           n_steps          (chain, draw) int64 32kB ...
│           tree_depth       (chain, draw) int64 32kB 2 2 2 2 3 3 2 3 ... 2 2 3 3 2 2 2
│           lp               (chain, draw) float64 32kB ...
│       Attributes:
│           created_at:                 2026-04-26T09:23:52.987417+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           sample_dims:                ['chain', 'draw']
├── Group: /constant_data
│       Attributes:
│           created_at:                 2026-04-26T09:23:52.989238+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           sample_dims:                []
├── Group: /log_likelihood
│       Dimensions:  (chain: 4, draw: 1000, obs: 900)
│       Coordinates:
│         * chain    (chain) int64 32B 0 1 2 3
│         * draw     (draw) int64 8kB 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
│         * obs      (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 892 893 894 895 896 897 898 899
│       Data variables:
│           y_cat    (chain, draw, obs) float64 29MB -0.7954 -0.8251 ... -0.6345 -0.3862
│       Attributes:
│           created_at:                 2026-04-26T09:23:53.726598+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           inference_library:          pymc
│           inference_library_version:  5.28.0+58.gf58491a3
│           sample_dims:                ['chain', 'draw']
└── Group: /posterior_predictive
        Dimensions:  (chain: 4, draw: 1000, obs: 900)
        Coordinates:
          * chain    (chain) int64 32B 0 1 2 3
          * draw     (draw) int64 8kB 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
          * obs      (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 892 893 894 895 896 897 898 899
        Data variables:
            y_cat    (chain, draw, obs) int64 29MB 3 3 2 0 3 2 2 2 2 ... 2 2 2 2 2 2 2 2
        Attributes:
            created_at:                 2026-04-26T09:23:54.261803+00:00
            creation_library:           ArviZ
            creation_library_version:   1.1.1dev0
            creation_library_language:  Python
            inference_library:          pymc
            inference_library_version:  5.28.0+58.gf58491a3
            sample_dims:                ['chain', 'draw']
summaries = az.summary(idata_m1, var_names=["beta_ic", "beta_oc"])
summaries
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
beta_ic -0.00624 0.000362 -0.0068 -0.0057 3113 2576 1.00 6.5e-06 4.8e-06
beta_oc -0.0046 0.000336 -0.0051 -0.0041 3712 2368 1.00 5.6e-06 4.1e-06

In the mlogit vignette they report how the above model specification leads to inadequate parameter estimates. They note for instance that while the utility scale itself is hard to interpret the value of the ratio of the coefficients is often meaningful because when:

\[ U = \beta_{oc}oc + \beta_{ic}ic \]

then the marginal rate of substitution is just the ratio of the two beta coefficients. The relative importance of one component of the utility equation to another is an economically meaningful quantity even if the notion of subjective utility is itself unobservable.

\[ dU = \beta_{ic} dic + \beta_{oc} doc = 0 \Rightarrow -\frac{dic}{doc}\mid_{dU=0}=\frac{\beta_{oc}}{\beta_{ic}}\]

Our parameter estimates differ slightly from the reported estimates, but we agree the model is inadequate. We will show a number of Bayesian model checks to demonstrate this fact, but the main call out is that the parameter values for installation costs should probably be negative. It’s counter-intuitive that a \(\beta_{ic}\) increase in price would increase the utility of generated by the installation even marginally as here. Although we might imagine that some kind of quality assurance comes with price which drives satisfaction with higher installation costs. The coefficient for repeat operating costs is negative as expected. Putting this issue aside for now, we’ll show below how we can incorporate prior knowledge to adjust for this kind of conflicts with theory.

But in any case, once we have a fitted model we can calculate the marginal rate of substitution as follows:

## marginal rate of substitution for a reduction in installation costs
post = az.extract(idata_m1)
substitution_rate = post["beta_oc"] / post["beta_ic"]
substitution_rate.mean().item()
0.7403800240288226

This statistic gives a view of the relative importance of the attributes which drive our utility measures. But being good Bayesians we actually want to calculate the posterior distribution for this statistic.

fig, ax = plt.subplots(figsize=(12, 4))

ax.hist(
    substitution_rate,
    bins="auto",
    ec="black",
)
ax.set_title("Uncertainty in Marginal Rate of Substitution \n Operating Costs / Installation Costs");

which suggests that there is around .7 of the value accorded to the a unit reduction in recurring operating costs over the one-off installation costs. Whether this is remotely plausible is almost beside the point since the model does not even closely capture the data generating process. But it’s worth repeating that the native scale of utility is not straightforwardly meaningful, but the ratio of the coefficients in the utility equations can be directly interpreted.

To assess overall model adequacy we can rely on the posterior predictive checks to see if the model can recover an approximation to the data generating process.

# Forest plot with observed proportions
counts = wide_heating_df.groupby("depvar")["idcase"].count()
obs_ds = xr.Dataset({"p": xr.DataArray(counts / counts.sum(), dims=["obs_dim_0"])})


pc = az.plot_forest(
    idata_m1,
    var_names=["p"],
    combined=True,
    sample_dims=["chain", "draw", "obs"],
)

pc.map(
    az.visuals.scatter_x,
    "observations",
    data=obs_ds,
    coords={"column": "forest"},
    color="k",
)
# Calibration plot for categorical data
pc = az.plot_ppc_pava(idata_m1, data_type="categorical");

We can see here that the model is fairly inadequate, and fails quite dramatically to recapture the posterior predictive distribution.

Improved Model: Adding Alternative Specific Intercepts#

We can address some of the issues with the prior model specification by adding intercept terms for each of the unique alternatives gr, gc, ec, er. These terms will absorb some of the error seen in the last model by allowing us to control some of the heterogenity of utility measures across products.

N = wide_heating_df.shape[0]
observed = pd.Categorical(wide_heating_df["depvar"]).codes

coords = {
    "alts_intercepts": ["ec", "er", "gc", "gr"],
    "alts_probs": ["ec", "er", "gc", "gr", "hp"],
    "obs": range(N),
}
with pm.Model(coords=coords) as model_2:
    beta_ic = pm.Normal("beta_ic", 0, 1)
    beta_oc = pm.Normal("beta_oc", 0, 1)
    alphas = pm.Normal("alpha", 0, 1, dims="alts_intercepts")

    ## Construct Utility matrix and Pivot using an intercept per alternative
    u0 = alphas[0] + beta_ic * wide_heating_df["ic.ec"] + beta_oc * wide_heating_df["oc.ec"]
    u1 = alphas[1] + beta_ic * wide_heating_df["ic.er"] + beta_oc * wide_heating_df["oc.er"]
    u2 = alphas[2] + beta_ic * wide_heating_df["ic.gc"] + beta_oc * wide_heating_df["oc.gc"]
    u3 = alphas[3] + beta_ic * wide_heating_df["ic.gr"] + beta_oc * wide_heating_df["oc.gr"]
    u4 = 0 + beta_ic * wide_heating_df["ic.hp"] + beta_oc * wide_heating_df["oc.hp"]
    s = pm.math.stack([u0, u1, u2, u3, u4]).T

    ## Apply Softmax Transform
    p_ = pm.Deterministic("p", pm.math.softmax(s, axis=1), dims=("obs", "alts_probs"))

    ## Likelihood
    choice_obs = pm.Categorical("y_cat", p=p_, observed=observed, dims="obs")

    idata_m2 = pm.sample_prior_predictive()
    idata_m2.update(pm.sample(nuts_sampler="numpyro", random_seed=103))
    pm.compute_log_likelihood(idata_m2, extend_inferencedata=True)
    pm.sample_posterior_predictive(idata_m2, extend_inferencedata=True)


pm.model_to_graphviz(model_2)
Sampling: [alpha, beta_ic, beta_oc, y_cat]
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/pymc/sampling/mcmc.py:832: FutureWarning: The arguments to `from_dict` have changed with the release of arviz 1.0. Please refer to the arviz documentation for more details
  return _sample_external_nuts(

Sampling: [y_cat]

../_images/728b44ec74ddf796bb63c955b5eab4c6e6e7eef69c019a43cd868cd9db5557d8.svg

We have deliberately 0’d out the intercept parameter for the hp alternative to ensure parameter identification is feasible.

az.summary(idata_m2, var_names=["beta_ic", "beta_oc", "alpha"])
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
beta_ic -0.00185 0.00061 -0.0028 -0.00085 2445 2647 1.00 1.2e-05 9.3e-06
beta_oc -0.00579 0.00138 -0.008 -0.0036 1243 1371 1.00 3.9e-05 2.7e-05
alpha[ec] 1.19 0.38 0.58 1.8 1235 1165 1.00 0.011 0.0076
alpha[er] 1.49 0.32 0.98 2 1124 1139 1.00 0.0094 0.0065
alpha[gc] 1.6 0.213 1.3 1.9 1556 1680 1.00 0.0054 0.0041
alpha[gr] 0.26 0.194 -0.043 0.57 1496 1675 1.00 0.005 0.0036

We can see now how this model performs much better in capturing aspects of the data generating process.

# Forest plot with observed proportions
pc = az.plot_forest(
    idata_m2,
    var_names=["p"],
    combined=True,
    sample_dims=["chain", "draw", "obs"],
)

pc.map(
    az.visuals.scatter_x,
    "observations",
    data=obs_ds,
    coords={"column": "forest"},
    color="k",
)
# Calibration plot for categorical data
pc = az.plot_ppc_pava(idata_m2, data_type="categorical");

This model represents a substantial improvement.

Experimental Model: Adding Correlation Structure#

We might think that there is a correlation among the alternative goods that we should capture too. We can capture those effects in so far as they exist by placing a multvariate normal prior on the intercepts, (or alternatively the beta parameters). In addition we add information about how the effect of income influences the utility accorded to each alternative.

coords = {
    "alts_intercepts": ["ec", "er", "gc", "gr"],
    "alts_probs": ["ec", "er", "gc", "gr", "hp"],
    "obs": range(N),
}
with pm.Model(coords=coords) as model_3:
    ## Add data to experiment with changes later.
    ic_ec = pm.Data("ic_ec", wide_heating_df["ic.ec"])
    oc_ec = pm.Data("oc_ec", wide_heating_df["oc.ec"])
    ic_er = pm.Data("ic_er", wide_heating_df["ic.er"])
    oc_er = pm.Data("oc_er", wide_heating_df["oc.er"])

    beta_ic = pm.Normal("beta_ic", 0, 1)
    beta_oc = pm.Normal("beta_oc", 0, 1)
    chol, corr, stds = pm.LKJCholeskyCov(
        "chol", n=5, eta=2.0, sd_dist=pm.Exponential.dist(1.0, shape=5)
    )
    alphas = pm.MvNormal("alpha", mu=0, chol=chol, dims="alts_probs")

    u0 = alphas[0] + beta_ic * ic_ec + beta_oc * oc_ec
    u1 = alphas[1] + beta_ic * ic_er + beta_oc * oc_er
    u2 = alphas[2] + beta_ic * wide_heating_df["ic.gc"] + beta_oc * wide_heating_df["oc.gc"]
    u3 = alphas[3] + beta_ic * wide_heating_df["ic.gr"] + beta_oc * wide_heating_df["oc.gr"]
    u4 = (
        alphas[4] + beta_ic * wide_heating_df["ic.hp"] + beta_oc * wide_heating_df["oc.hp"]
    )  # pivot)
    s = pm.math.stack([u0, u1, u2, u3, u4]).T

    p_ = pm.Deterministic("p", pm.math.softmax(s, axis=1), dims=("obs", "alts_probs"))
    choice_obs = pm.Categorical("y_cat", p=p_, observed=observed, dims="obs")

    idata_m3 = pm.sample_prior_predictive()
    idata_m3.update(pm.sample(nuts_sampler="numpyro", random_seed=100))
    pm.compute_log_likelihood(idata_m3, extend_inferencedata=True)
    pm.sample_posterior_predictive(idata_m3, extend_inferencedata=True)


pm.model_to_graphviz(model_3)
Sampling: [alpha, beta_ic, beta_oc, chol, y_cat]
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/pymc/sampling/mcmc.py:832: FutureWarning: The arguments to `from_dict` have changed with the release of arviz 1.0. Please refer to the arviz documentation for more details
  return _sample_external_nuts(
There were 324 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.01 for some parameters. This indicates problems during sampling. See https://arxiv.org/abs/1903.08008 for details
The effective sample size per chain is smaller than 100 for some parameters.  A higher number is needed for reliable rhat and ess computation. See https://arxiv.org/abs/1903.08008 for details

Sampling: [y_cat]

../_images/35234d0984445b53e6713bcec5f3bfe704a0777b16818bdb233d10dab6ba2714.svg

Plotting the model fit we see a similar story.The model predictive performance is not drastically improved and we have added some complexity to the model. This extra complexity ought to be penalised in model assessment metrics such as AIC and WAIC. But often the correlation amongst products are some of the features of interest, independent of issues of historic predictions.

# Forest plot with observed proportions
pc = az.plot_forest(
    idata_m2,
    var_names=["p"],
    combined=True,
    sample_dims=["chain", "draw", "obs"],
)

pc.map(
    az.visuals.scatter_x,
    "observations",
    data=obs_ds,
    coords={"column": "forest"},
    color="k",
)
# Calibration plot for categorical data
pc = az.plot_ppc_pava(idata_m2, data_type="categorical");

That extra complexity can be informative, and the degree of relationship amongst the alternative products will inform the substitution patterns under policy changes. Also, note how under this model specification the parameter for beta_ic has a expected value of 0. Suggestive perhaps of a resignation towards the reality of installation costs that doesn’t change the utility metric one way or other after a decision to purchase.

az.summary(idata_m3, var_names=["beta_ic", "beta_oc", "alpha", "chol_corr"])
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/arviz_stats/base/diagnostics.py:90: RuntimeWarning: invalid value encountered in scalar divide
  (between_chain_variance / within_chain_variance + num_samples - 1) / (num_samples)
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/arviz_stats/base/diagnostics.py:313: RuntimeWarning: invalid value encountered in scalar divide
  varsd = varvar / evar / 4
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
beta_ic -0.00165 0.00062 -0.0026 -0.00067 2263 2561 1.00 1.3e-05 9.3e-06
beta_oc -0.0062 0.00143 -0.0083 -0.0038 802 648 1.01 5.2e-05 4e-05
alpha[ec] 0.17 0.53 -0.43 1.3 222 200 1.01 0.04 0.041
alpha[er] 0.4 0.53 -0.15 1.6 200 175 1.02 0.043 0.039
alpha[gc] 0.45 0.58 -0.26 1.5 179 174 1.01 0.049 0.031
alpha[gr] -0.9 0.6 -1.7 0.16 177 182 1.01 0.05 0.03
alpha[hp] -1.2 0.6 -2 -0.088 164 161 1.02 0.051 0.033
chol_corr[0, 0] 1 2.08167e-17 1 1 4000 4000 NaN 0 NaN
chol_corr[0, 1] 0.04 0.36 -0.53 0.61 2140 2246 1.00 0.008 0.0046
chol_corr[0, 2] 0.03 0.36 -0.56 0.61 1362 1331 1.01 0.0098 0.0057
chol_corr[0, 3] 0.01 0.34 -0.55 0.57 1442 1073 1.00 0.0091 0.0054
chol_corr[0, 4] 0.01 0.35 -0.55 0.59 1237 787 1.00 0.01 0.0059
chol_corr[1, 0] 0.04 0.36 -0.53 0.61 2140 2246 1.00 0.008 0.0046
chol_corr[1, 1] 1 1.38e-16 1 1 3369 3115 1.00 2.2e-18 1.3e-18
chol_corr[1, 2] 0.06 0.35 -0.52 0.61 1483 1663 1.00 0.009 0.0055
chol_corr[1, 3] -0.03 0.35 -0.59 0.53 725 454 1.00 0.013 0.0076
chol_corr[1, 4] -0.07 0.347 -0.62 0.5 2157 1854 1.00 0.0075 0.0045
chol_corr[2, 0] 0.03 0.36 -0.56 0.61 1362 1331 1.01 0.0098 0.0057
chol_corr[2, 1] 0.06 0.35 -0.52 0.61 1483 1663 1.00 0.009 0.0055
chol_corr[2, 2] 1 1.35e-16 1 1 3407 3157 1.00 2.1e-18 1.3e-18
chol_corr[2, 3] -0.02 0.346 -0.59 0.53 1806 2675 1.01 0.0083 0.0048
chol_corr[2, 4] -0.06 0.35 -0.61 0.51 1509 1950 1.00 0.0091 0.0054
chol_corr[3, 0] 0.01 0.34 -0.55 0.57 1442 1073 1.00 0.0091 0.0054
chol_corr[3, 1] -0.03 0.35 -0.59 0.53 725 454 1.00 0.013 0.0076
chol_corr[3, 2] -0.02 0.346 -0.59 0.53 1806 2675 1.01 0.0083 0.0048
chol_corr[3, 3] 1 1.34e-16 1 1 3067 3573 1.00 2.1e-18 1.3e-18
chol_corr[3, 4] 0.14 0.34 -0.43 0.66 1430 1253 1.00 0.009 0.0053
chol_corr[4, 0] 0.01 0.35 -0.55 0.59 1237 787 1.00 0.01 0.0059
chol_corr[4, 1] -0.07 0.347 -0.62 0.5 2157 1854 1.00 0.0075 0.0045
chol_corr[4, 2] -0.06 0.35 -0.61 0.51 1509 1950 1.00 0.0091 0.0054
chol_corr[4, 3] 0.14 0.34 -0.43 0.66 1430 1253 1.00 0.009 0.0053
chol_corr[4, 4] 1 1.36e-16 1 1 3163 2958 1.00 2.2e-18 1.3e-18

In this model we see that the marginal rate of substitution shows that an increase of one dollar for the operating costs is almost 17 times more impactful on the utility calculus than a similar increase in installation costs. Which makes sense in so far as we can expect the installation costs to be a one-off expense we’re pretty resigned to.

post = az.extract(idata_m3)
substitution_rate = post["beta_oc"] / post["beta_ic"]
substitution_rate.mean().item()
4.529644358254206

Market Inteventions and Predicting Market Share#

We can additionally use these kinds of models to predict market share under interventions where we change the price offering.

with model_3:
    # update values of predictors with new 20% price increase in operating costs for electrical options
    pm.set_data({"oc_ec": wide_heating_df["oc.ec"] * 1.2, "oc_er": wide_heating_df["oc.er"] * 1.2})
    # use the updated values and predict outcomes and probabilities:
    idata_new_policy = pm.sample_posterior_predictive(
        idata_m3,
        var_names=["p", "y_cat"],
        return_inferencedata=True,
        predictions=True,
        extend_inferencedata=False,
        random_seed=100,
    )

idata_new_policy
Sampling: [y_cat]

<xarray.DataTree>
Group: /
├── Group: /predictions
│       Dimensions:     (chain: 4, draw: 1000, obs: 900, alts_probs: 5)
│       Coordinates:
│         * chain       (chain) int64 32B 0 1 2 3
│         * draw        (draw) int64 8kB 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
│         * obs         (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 893 894 895 896 897 898 899
│         * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
│       Data variables:
│           p           (chain, draw, obs, alts_probs) float64 144MB 0.03556 ... 0.03786
│           y_cat       (chain, draw, obs) int64 29MB 2 4 4 2 1 2 2 2 ... 3 3 2 2 2 4 2
│       Attributes:
│           created_at:                 2026-04-26T09:24:38.491366+00:00
│           creation_library:           ArviZ
│           creation_library_version:   1.1.1dev0
│           creation_library_language:  Python
│           inference_library:          pymc
│           inference_library_version:  5.28.0+58.gf58491a3
│           sample_dims:                ['chain', 'draw']
└── Group: /predictions_constant_data
        Dimensions:      (ic_ec_dim_0: 900, oc_ec_dim_0: 900, ic_er_dim_0: 900,
                          oc_er_dim_0: 900)
        Coordinates:
          * ic_ec_dim_0  (ic_ec_dim_0) int64 7kB 0 1 2 3 4 5 ... 894 895 896 897 898 899
          * oc_ec_dim_0  (oc_ec_dim_0) int64 7kB 0 1 2 3 4 5 ... 894 895 896 897 898 899
          * ic_er_dim_0  (ic_er_dim_0) int64 7kB 0 1 2 3 4 5 ... 894 895 896 897 898 899
          * oc_er_dim_0  (oc_er_dim_0) int64 7kB 0 1 2 3 4 5 ... 894 895 896 897 898 899
        Data variables:
            ic_ec        (ic_ec_dim_0) float64 7kB 859.9 796.8 719.9 ... 799.8 967.9
            oc_ec        (oc_ec_dim_0) float64 7kB 664.0 624.3 526.9 ... 594.2 622.4
            ic_er        (ic_er_dim_0) float64 7kB 995.8 894.7 ... 1.123e+03 1.092e+03
            oc_er        (oc_er_dim_0) float64 7kB 606.7 583.8 485.7 ... 481.9 550.2
        Attributes:
            created_at:                 2026-04-26T09:24:38.494454+00:00
            creation_library:           ArviZ
            creation_library_version:   1.1.1dev0
            creation_library_language:  Python
            inference_library:          pymc
            inference_library_version:  5.28.0+58.gf58491a3
            sample_dims:                []
# Old Policy Expectations
old = idata_m3["posterior"]["p"].mean(dim=["chain", "draw", "obs"])
old
<xarray.DataArray 'p' (alts_probs: 5)> Size: 40B
Array([0.07076665, 0.0917175 , 0.63578922, 0.14468565, 0.05704098],      dtype=float64)
Coordinates:
  * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
# New Policy Expectations
new = idata_new_policy["predictions"]["p"].mean(dim=["chain", "draw", "obs"])
new
<xarray.DataArray 'p' (alts_probs: 5)> Size: 40B
array([0.04377364, 0.05973199, 0.68042523, 0.15494971, 0.06111943])
Coordinates:
  * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
new - old
<xarray.DataArray 'p' (alts_probs: 5)> Size: 40B
Array([-0.02699301, -0.03198551,  0.04463601,  0.01026406,  0.00407845],      dtype=float64)
Coordinates:
  * alts_probs  (alts_probs) <U2 40B 'ec' 'er' 'gc' 'gr' 'hp'
# Forest plot with observed proportions
pc = az.plot_forest(
    idata_m3,
    var_names=["p"],
    combined=True,
    sample_dims=["chain", "draw", "obs"],
)

pc.map(
    az.visuals.scatter_x,
    "observations",
    data=obs_ds,
    coords={"column": "forest"},
    color="k",
    label="Observed share",
)

new_predictions = xr.Dataset(
    {
        "p": xr.DataArray(
            idata_new_policy["predictions"]["p"].mean(dim=["chain", "draw", "obs"]).values,
            coords={"obs_dim_0": obs_ds["obs_dim_0"].values},
        )
    }
)

pc.map(
    az.visuals.scatter_x,
    "new_predictions",
    data=new_predictions,
    coords={"column": "forest"},
    color="C1",
    label="New Policy Predicted Share",
)
plt.legend();

Here we can, as expected, see that a rise in the operating costs of the electrical options has a negative impact on their predicted market share.

Compare Models#

We’ll now evaluate all three model fits on their predictive performance. Predictive performance on the original data is a good benchmark that the model has appropriately captured the data generating process. But it is not (as we’ve seen) the only feature of interest in these models. These models are sensetive to our theoretical beliefs about the agents making the decisions, the view of the decision process and the elements of the choice scenario.

compare = az.compare({"m1": idata_m1, "m2": idata_m2, "m3": idata_m3})
compare
rank elpd p elpd_diff weight se dse warning
m3 0 -1010.0 5.7 0.0 0.93 28.0 0.00 False
m2 1 -1010.0 5.4 -0.0 0.07 28.0 0.64 False
m1 2 -1100.0 2.1 -80.0 0.00 26.0 12.00 False
az.plot_compare(compare);

Choosing Crackers over Repeated Choices: Mixed Logit Model#

Moving to another example, we see a choice scenario where the same individual has been repeatedly polled on their choice of crackers among alternatives. This affords us the opportunity to evaluate the preferences of individuals by adding in coefficients for individuals for each product.

try:
    c_df = pd.read_csv("../data/cracker_choice_short.csv")
except:
    c_df = pd.read_csv(pm.get_data("cracker_choice_short.csv"))
columns = [c for c in c_df.columns if c != "Unnamed: 0"]
c_df[columns]
personId disp.sunshine disp.keebler disp.nabisco disp.private feat.sunshine feat.keebler feat.nabisco feat.private price.sunshine price.keebler price.nabisco price.private choice lastChoice personChoiceId choiceId
0 1 0 0 0 0 0 0 0 0 0.99 1.09 0.99 0.71 nabisco nabisco 1 1
1 1 1 0 0 0 0 0 0 0 0.49 1.09 1.09 0.78 sunshine nabisco 2 2
2 1 0 0 0 0 0 0 0 0 1.03 1.09 0.89 0.78 nabisco sunshine 3 3
3 1 0 0 0 0 0 0 0 0 1.09 1.09 1.19 0.64 nabisco nabisco 4 4
4 1 0 0 0 0 0 0 0 0 0.89 1.09 1.19 0.84 nabisco nabisco 5 5
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
3151 136 0 0 0 0 0 0 0 0 1.09 1.19 0.99 0.55 private private 9 3152
3152 136 0 0 0 1 0 0 0 0 0.78 1.35 1.04 0.65 private private 10 3153
3153 136 0 0 0 0 0 0 0 0 1.09 1.17 1.29 0.59 private private 11 3154
3154 136 0 0 0 0 0 0 0 0 1.09 1.22 1.29 0.59 private private 12 3155
3155 136 0 0 0 0 0 0 0 0 1.29 1.04 1.23 0.59 private private 13 3156

3156 rows × 17 columns

c_df.groupby("personId")[["choiceId"]].count().T
personId 1 2 3 4 5 6 7 8 9 10 ... 127 128 129 130 131 132 133 134 135 136
choiceId 15 15 13 28 13 27 16 25 18 40 ... 17 25 31 31 29 21 26 13 14 13

1 rows × 136 columns

The presence of repeated choice over time complicates the issue. We now have to contend with issues of personal taste and the evolving or dynamic effects of pricing in a competitive environment. Plotting the simple linear and polynomial fits for each person’s successive exposure to the brand price, seems to suggest that (a) pricing differentiates the product offering and (b) pricing evolves over time.

fig, axs = plt.subplots(1, 2, figsize=(20, 10))
axs = axs.flatten()

for i in c_df["personId"].unique():
    temp = c_df[c_df["personId"] == i].copy(deep=True)
    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.sunshine"], deg=1))
    axs[0].plot(predict(range(25)), color="C1", label="Sunshine", alpha=0.4)
    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.keebler"], deg=1))
    axs[0].plot(predict(range(25)), color="C0", label="Keebler", alpha=0.4)
    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.nabisco"], deg=1))
    axs[0].plot(predict(range(25)), color="C2", label="Nabisco", alpha=0.4)

    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.sunshine"], deg=2))
    axs[1].plot(predict(range(25)), color="C1", label="Sunshine", alpha=0.4)
    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.keebler"], deg=2))
    axs[1].plot(predict(range(25)), color="C0", label="Keebler", alpha=0.4)
    predict = np.poly1d(np.polyfit(temp["personChoiceId"], temp["price.nabisco"], deg=2))
    axs[1].plot(predict(range(25)), color="C2", label="Nabisco", alpha=0.4)

axs[0].set_title("Linear Regression Fit \n Customer Price Exposure over Time", fontsize=20)
axs[1].set_title("Polynomial^(2) Regression Fit \n Customer Price Exposure over Time", fontsize=20)
axs[0].set_xlabel("Nth Decision/Time point")
axs[1].set_xlabel("Nth Decision/Time point")
axs[0].set_ylabel("Product Price Offered")
axs[1].set_ylim(0, 2)
axs[0].set_ylim(0, 2)

colors = ["C1", "C0", "C2"]
lines = [Line2D([0], [0], color=c, linewidth=3, linestyle="-") for c in colors]
labels = ["Sunshine", "Keebler", "Nabisco"]
axs[0].legend(lines, labels)
axs[1].legend(lines, labels);

We’ll model now how individual taste enters into discrete choice problems, but ignore the complexities of the time-dimension or the endogenity of price in the system. There are adaptions of the basic discrete choice model that are designed to address each of these complications. We’ll leave the temporal dynamics as a suggested exercise for the reader.

N = c_df.shape[0]
map = {"sunshine": 0, "keebler": 1, "nabisco": 2, "private": 3}
c_df["choice_encoded"] = c_df["choice"].map(map)
observed = c_df["choice_encoded"].values
person_indx, uniques = pd.factorize(c_df["personId"])

coords = {
    "alts_intercepts": ["sunshine", "keebler", "nabisco", "private"],
    "alts_probs": ["sunshine", "keebler", "nabisco", "private"],
    "individuals": uniques,
    "obs": range(N),
}
with pm.Model(coords=coords) as model_4:
    beta_feat = pm.Normal("beta_feat", 0, 1)
    beta_disp = pm.Normal("beta_disp", 0, 1)
    beta_price = pm.Normal("beta_price", 0, 1)
    alphas = pm.Normal("alpha", 0, 1, dims="alts_intercepts")
    ## Hierarchical parameters for individual taste
    beta_individual = pm.Normal("beta_individual", 0, 0.1, dims=("individuals", "alts_intercepts"))

    u0 = (
        (alphas[0] + beta_individual[person_indx, 0])
        + beta_disp * c_df["disp.sunshine"]
        + beta_feat * c_df["feat.sunshine"]
        + beta_price * c_df["price.sunshine"]
    )
    u1 = (
        (alphas[1] + beta_individual[person_indx, 1])
        + beta_disp * c_df["disp.keebler"]
        + beta_feat * c_df["feat.keebler"]
        + beta_price * c_df["price.keebler"]
    )
    u2 = (
        (alphas[2] + beta_individual[person_indx, 2])
        + beta_disp * c_df["disp.nabisco"]
        + beta_feat * c_df["feat.nabisco"]
        + beta_price * c_df["price.nabisco"]
    )
    u3 = (
        (0 + beta_individual[person_indx, 2])
        + beta_disp * c_df["disp.private"]
        + beta_feat * c_df["feat.private"]
        + beta_price * c_df["price.private"]
    )
    s = pm.math.stack([u0, u1, u2, u3]).T

    ## Apply Softmax Transform
    p_ = pm.Deterministic("p", pm.math.softmax(s, axis=1), dims=("obs", "alts_probs"))

    ## Likelihood
    choice_obs = pm.Categorical("y_cat", p=p_, observed=observed, dims="obs")

    idata_m4 = pm.sample_prior_predictive()
    idata_m4.update(pm.sample(nuts_sampler="numpyro", random_seed=103))
    pm.compute_log_likelihood(idata_m4, extend_inferencedata=True)
    pm.sample_posterior_predictive(idata_m4, extend_inferencedata=True)


pm.model_to_graphviz(model_4)
Sampling: [alpha, beta_disp, beta_feat, beta_individual, beta_price, y_cat]
/home/osvaldo/anaconda3/envs/arviz_1/lib/python3.14/site-packages/pymc/sampling/mcmc.py:832: FutureWarning: The arguments to `from_dict` have changed with the release of arviz 1.0. Please refer to the arviz documentation for more details
  return _sample_external_nuts(

Sampling: [y_cat]

../_images/b35cf14315445670fe64fc1e771585e4f4773aa64ebf3ca36ce4fa8a0ec014c4.svg
az.summary(idata_m4, var_names=["beta_disp", "beta_feat", "beta_price", "alpha"]).head(6)
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
beta_disp 0.105 0.064 -0.00057 0.21 6031 3268 1.00 0.00083 0.00056
beta_feat 0.508 0.097 0.36 0.66 5101 3340 1.00 0.0014 0.00099
beta_price -2.98 0.208 -3.3 -2.7 1702 2112 1.00 0.005 0.0037
alpha[sunshine] -0.708 0.093 -0.86 -0.57 2547 2714 1.00 0.0018 0.0013
alpha[keebler] -0.234 0.121 -0.43 -0.039 1858 2323 1.00 0.0028 0.002
alpha[nabisco] 1.722 0.101 1.6 1.9 1680 2253 1.00 0.0025 0.0018

What have we learned? We’ve imposed a negative slope on the price coefficient but given it a wide prior. We can see that the data is sufficient to have narrowed the likely range of the coefficient considerably.

az.plot_prior_posterior(idata_m4, var_names=["beta_price"], figure_kwargs={"figsize": (10, 2)});

We have recovered a strongly negative estimate on the price effect in line with the basic theory of rational choice. The effect of price should have a negative impact on utility. The flexibility of priors here is key for incorporating theoretical knowledge about the process involved in choice. Priors are important for building a better picture of the decision making process and we’d be foolish to ignore their value in this setting.

posterior_coords = idata_m4["posterior"]["p"].coords["alts_probs"].values

counts = c_df.groupby("choice")["choiceId"].count()
counts /= counts.sum()
counts = counts.reindex(posterior_coords)

obs_ds = xr.Dataset(
    {"p": xr.DataArray(counts.values, dims=["obs_dim_0"], coords={"obs_dim_0": posterior_coords})}
)

new_predictions_ds = xr.Dataset(
    {
        "p": xr.DataArray(
            idata_m4["posterior"]["p"]
            .mean(dim=["chain", "draw", "obs"])
            .sel(alts_probs=posterior_coords)
            .values,
            dims=["obs_dim_0"],
            coords={"obs_dim_0": posterior_coords},
        )
    }
)

pc = az.plot_forest(
    idata_m4,
    var_names=["p"],
    combined=True,
    sample_dims=["chain", "draw", "obs"],
)

pc.map(
    az.visuals.scatter_x,
    "observations",
    data=obs_ds,
    coords={"column": "forest"},
    color="k",
    label="Observed share",
)

pc.map(
    az.visuals.scatter_x,
    "new_predictions",
    data=new_predictions_ds,
    coords={"column": "forest"},
    color="C1",
    label="Predicted mean",
)
plt.legend()

az.plot_ppc_pava(idata_m4, data_type="categorical");

We can now also recover the differences among individuals estimated by the model for particular cracker choices. More precisely we’ll plot how the individual specific contribution to the intercept drives preferences among the cracker choices.

beta_individual = idata_m4["posterior"]["beta_individual"]
predicted = beta_individual.mean(("chain", "draw"))
predicted = predicted.sortby(predicted.sel(alts_intercepts="nabisco"))
ci_lb = beta_individual.quantile(0.025, ("chain", "draw")).sortby(
    predicted.sel(alts_intercepts="nabisco")
)
ci_ub = beta_individual.quantile(0.975, ("chain", "draw")).sortby(
    predicted.sel(alts_intercepts="nabisco")
)
fig = plt.figure(figsize=(10, 9))
gs = fig.add_gridspec(
    2,
    3,
    width_ratios=(4, 4, 4),
    height_ratios=(1, 7),
    left=0.1,
    right=0.9,
    bottom=0.1,
    top=0.9,
    wspace=0.05,
    hspace=0.05,
)
# Create the Axes.
ax = fig.add_subplot(gs[1, 0])
ax.set_yticklabels([])
ax_histx = fig.add_subplot(gs[0, 0], sharex=ax)
ax_histx.set_title("Expected Modifications \n to Nabisco Baseline", fontsize=10)
ax_histx.hist(predicted.sel(alts_intercepts="nabisco"), bins=30, ec="black", color="C1")
ax_histx.set_yticklabels([])
ax_histx.tick_params(labelsize=8)
ax.set_ylabel("Individuals", fontsize=10)
ax.tick_params(labelsize=8)
ax.hlines(
    range(len(predicted)),
    ci_lb.sel(alts_intercepts="nabisco"),
    ci_ub.sel(alts_intercepts="nabisco"),
    color="black",
    alpha=0.3,
)
ax.scatter(predicted.sel(alts_intercepts="nabisco"), range(len(predicted)), color="C1", ec="white")
ax.fill_betweenx(range(139), -0.03, 0.03, alpha=0.2, color="C1")

ax1 = fig.add_subplot(gs[1, 1])
ax1.set_yticklabels([])
ax_histx = fig.add_subplot(gs[0, 1], sharex=ax1)
ax_histx.set_title("Expected Modifications \n to Keebler Baseline", fontsize=10)
ax_histx.set_yticklabels([])
ax_histx.tick_params(labelsize=8)
ax_histx.hist(predicted.sel(alts_intercepts="keebler"), bins="auto", ec="black", color="C1")
ax1.hlines(
    range(len(predicted)),
    ci_lb.sel(alts_intercepts="keebler"),
    ci_ub.sel(alts_intercepts="keebler"),
    color="black",
    alpha=0.3,
)
ax1.scatter(predicted.sel(alts_intercepts="keebler"), range(len(predicted)), color="C1", ec="white")
ax1.set_xlabel("Individual Modifications to the Product Intercept", fontsize=10)
ax1.fill_betweenx(range(139), -0.03, 0.03, alpha=0.2, color="C1", label="Negligible \n Region")
ax1.tick_params(labelsize=8)
ax1.legend(fontsize=10)

ax2 = fig.add_subplot(gs[1, 2])
ax2.set_yticklabels([])
ax_histx = fig.add_subplot(gs[0, 2], sharex=ax2)
ax_histx.set_title("Expected Modifications \n to Sunshine Baseline", fontsize=10)
ax_histx.set_yticklabels([])
ax_histx.hist(predicted.sel(alts_intercepts="sunshine"), bins=30, ec="black", color="C1")
ax2.hlines(
    range(len(predicted)),
    ci_lb.sel(alts_intercepts="sunshine"),
    ci_ub.sel(alts_intercepts="sunshine"),
    color="black",
    alpha=0.3,
)
ax2.fill_betweenx(range(139), -0.03, 0.03, alpha=0.2, color="C1")
ax2.scatter(
    predicted.sel(alts_intercepts="sunshine"), range(len(predicted)), color="C1", ec="white"
)
ax2.tick_params(labelsize=8)
ax_histx.tick_params(labelsize=8)
plt.suptitle("Individual Differences by Product", fontsize=20);

This type of plot is often useful for identifying loyal customers. Similarly it can be used to identify cohorts of customers that ought to be better incentivised if we hope them to switch to our product.

Conclusion#

We can see here the flexibility and richly parameterised possibilities for modelling individual choice of discrete options. These techniques are useful in a wide variety of domains from microeconomics, to marketing and product development. The notions of utility, probability and their interaction lie at the heart of Savage’s Representation theorem and justification(s) for Bayesian approaches to statistical inference. So discrete modelling is a natural fit for the Bayesian, but Bayesian statistics is also a natural fit for discrete choice modelling. The traditional estimation techniques are often brittle and very dependent on starting values of the MLE process. The Bayesian setting trades this brittleness for a framework which allows us to incorporate our beliefs about what drives human utility calculations. We’ve only scratched the surface in this example notebook, but encourage you to further explore the technique.

Authors#

References#

Watermark#

%load_ext watermark
%watermark -n -u -v -iv -w -p pytensor
Last updated: Sun, 26 Apr 2026

Python implementation: CPython
Python version       : 3.14.4
IPython version      : 9.12.0

pytensor: 2.38.0+133.g80cc113b5

arviz     : 1.1.0
matplotlib: 3.10.8
numpy     : 2.4.4
pandas    : 3.0.2
pymc      : 5.28.0+58.gf58491a3
xarray    : 2026.4.0

Watermark: 2.6.0

License notice#

All the notebooks in this example gallery are provided under the MIT License which allows modification, and redistribution for any use provided the copyright and license notices are preserved.

Citing PyMC examples#

To cite this notebook, use the DOI provided by Zenodo for the pymc-examples repository.

Important

Many notebooks are adapted from other sources: blogs, books… In such cases you should cite the original source as well.

Also remember to cite the relevant libraries used by your code.

Here is an citation template in bibtex:

@incollection{citekey,
  author    = "<notebook authors, see above>",
  title     = "<notebook title>",
  editor    = "PyMC Team",
  booktitle = "PyMC examples",
  doi       = "10.5281/zenodo.5654871"
}

which once rendered could look like:

Nathaniel Forde . "Discrete Choice and Random Utility Models". In: PyMC Examples. Ed. by PyMC Team. DOI: 10.5281/zenodo.5654871