MixedLogit#

class pymc_marketing.customer_choice.mixed_logit.MixedLogit(choice_df, utility_equations, depvar, covariates, model_config=None, sampler_config=None, group_id=None, instrumental_vars=None, non_centered=True)[source]#

Mixed Logit (Random Parameters Logit) class.

Class to perform a mixed logit analysis with random coefficients to capture heterogeneity in consumer preferences. Random coefficients can vary across individuals (or groups in panel data).

Parameters:
choice_dfpd.DataFrame

A wide DataFrame where each row is a choice scenario. Product-specific attributes are stored in columns, and the dependent variable identifies the chosen product.

utility_equationslist of formula strings

A list of formulas specifying how to model the utility of each product alternative. The formulas should be in Wilkinson style notation with three parts separated by |: target_product ~ alt_specific_covariates | fixed_covariates | random_covariates

depvarstr

The name of the dependent variable in the choice_df.

covariateslist of str

Covariate names (e.g., [‘price’, ‘time’, ‘comfort’])

model_configdict, optional

Model configuration. If None, the default config is used.

sampler_configdict, optional

Sampler configuration. If None, the default config is used.

group_idstr, optional

Column name for group identifier (for panel data). If None, each observation is treated as a unique individual.

instrumental_varsdict, optional

Dictionary specifying instrumental variables for endogenous price: {‘X_instruments’: np.ndarray, ‘y_price’: np.ndarray, ‘diagonal’: bool} If None, no control function is used.

non_centeredbool, optional

Whether to use non-centered parameterization for random coefficients.

Notes

Example:#

The format of choice_df:

choice

bus_price

bus_time

car_price

car_time

bus

2.4

45

5.4

30

car

3.5

50

2.3

25

Example utility_equations list:

utility_equations = [
    "bus ~ bus_price + bus_time | income | bus_price",
    "car ~ car_price + car_time | income | car_price",
    "train ~ train_price + train_time | income | train_price",
]

This specifies: - Alternative-specific: price and time for each mode - Fixed across alternatives: income (with alternative-specific coefficients) - Random coefficients: price varies across individuals

Methods

MixedLogit.__init__(choice_df, ...[, ...])

Initialize model configuration and sampler configuration for the model.

MixedLogit.apply_intervention(new_choice_df)

Apply intervention by changing observable attributes or market structure.

MixedLogit.attrs_to_init_kwargs(attrs)

Convert the model configuration and sampler configuration from the attributes to keyword arguments.

MixedLogit.build_from_idata(idata)

Build model from loaded InferenceData.

MixedLogit.build_model(**kwargs)

Build model using stored choice_df and utility_equations.

MixedLogit.calculate_share_change(idata, ...)

Calculate difference in market share due to intervention.

MixedLogit.create_idata_attrs()

Create the attributes for the InferenceData object.

MixedLogit.fit([choice_df, ...])

Fit the discrete choice model.

MixedLogit.graphviz(**kwargs)

Get the graphviz representation of the model.

MixedLogit.idata_to_init_kwargs(idata)

Create the model configuration and sampler configuration from the InferenceData to keyword arguments.

MixedLogit.load(fname[, check])

Create a ModelBuilder instance from a file.

MixedLogit.load_from_idata(idata[, check])

Create a ModelBuilder instance from an InferenceData object.

MixedLogit.make_beta_matrix(...)

Combine random and non-random coefficients into full coefficient matrix.

MixedLogit.make_choice_prob(U)

Compute choice probabilities via softmax transformation.

MixedLogit.make_control_function(n_obs, n_alts)

Create control function for price endogeneity correction.

MixedLogit.make_fixed_coefs(X_fixed, n_obs, ...)

Create alternative-varying coefficients for fixed (non-varying) covariates.

MixedLogit.make_intercepts()

Create alternative-specific intercepts with reference alternative set to zero.

MixedLogit.make_model(X, F, y[, observed])

Build mixed logit model with random coefficients.

MixedLogit.make_non_random_coefs()

Create coefficients for non-random alternative-specific covariates.

MixedLogit.make_random_coefs(n_obs[, ...])

Create random coefficients that vary across individuals.

MixedLogit.make_utility(X_data, B_full, ...)

Compute total systematic utility for each alternative.

MixedLogit.parse_formula(df, formula, depvar)

Parse the three-part structure of a mixed logit formula specification.

MixedLogit.plot_change(change_df[, title, ...])

Plot change induced by a market intervention.

MixedLogit.prepare_X_matrix(df, ...)

Prepare the design matrices for the utility equations.

MixedLogit.preprocess_model_data(choice_df, ...)

Pre-process the model initiation inputs into PyMC-ready format.

MixedLogit.sample([...])

Sample all the things.

MixedLogit.sample_posterior_predictive([...])

Sample from posterior predictive distribution.

MixedLogit.sample_prior_predictive([...])

Sample from prior predictive distribution.

MixedLogit.save(fname, **kwargs)

Save the model's inference data to a file.

MixedLogit.set_idata_attrs([idata])

Set attributes on an InferenceData object.

MixedLogit.table(**model_table_kwargs)

Get the summary table of the model.

Attributes

default_model_config

Default model configuration.

default_sampler_config

Default sampler configuration.

fit_result

Get the posterior fit_result.

id

Generate a unique hash value for the model.

output_var

The output variable of the model.

posterior

posterior_predictive

predictions

prior

prior_predictive

version

idata

sampler_config

model_config