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_df
pd.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_equations
listofformulastrings 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
- depvar
str The name of the dependent variable in the choice_df.
- covariates
listofstr Covariate names (e.g., [‘price’, ‘time’, ‘comfort’])
- model_config
dict, optional Model configuration. If None, the default config is used.
- sampler_config
dict, optional Sampler configuration. If None, the default config is used.
- group_id
str, optional Column name for group identifier (for panel data). If None, each observation is treated as a unique individual.
- instrumental_vars
dict, 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.
- choice_df
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_equationslist: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.
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.
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.
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.
Combine random and non-random coefficients into full coefficient matrix.
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.
Create alternative-specific intercepts with reference alternative set to zero.
MixedLogit.make_model(X, F, y[, observed])Build mixed logit model with random coefficients.
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.
Sample from posterior predictive distribution.
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_configDefault model configuration.
default_sampler_configDefault sampler configuration.
fit_resultGet the posterior fit_result.
idGenerate a unique hash value for the model.
output_varThe output variable of the model.
posteriorposterior_predictivepredictionspriorprior_predictiveversionidatasampler_configmodel_config