NestedLogit#

class pymc_marketing.customer_choice.nested_logit.NestedLogit(choice_df, utility_equations, depvar, covariates, nesting_structure, model_config=None, sampler_config=None)[source]#

Nested Logit class.

Class to perform a nested logit analysis with the specific intent of determining the product attribute effects on consumer preference. The implementation here is drawn from a discussion in Kenneth Train’s book “Discrete Choice Methods with Simulation” Second Edition from 2009. Useful discussion of the model can also be found in Paez & Boisjoly’s book “Discrete Choice Analysis with R” from 2022.

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 and allow the target product to be specified as as a function of the alternative specific attributes and the individual specific attributes: target_product ~ target_attribute1 + target_attribute2 | individual_attribute

depvarstr

The name of the dependent variable in the choice_df.

covariateslist of str

Covariate names (e.g., [‘X1’, ‘X2’])

nested_structure: dict

Dictionary to specify how to nest the choices between products

model_configdict, optional

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

sampler_configdict, optional

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

Notes

Example:#

The format of choice_df:

Depvar

alt_1_X1

alt_1_X2

alt_2_X1

alt_2_X2

alt_1

2.4

4.5

5.4

6.7

alt_2

3.5

6.7

2.3

8.9

Example utility_equations list:

>>> utility_equations = [
...     "alt_1 ~ X1_alt1 + X2_alt1 | income",
...     "alt_2 ~ X1_alt2 + X2_alt2 | income",
...     "alt_3 ~ X1_alt3 + X2_alt3 | income",
... ]

Example nesting structure:

>>> nesting_structure = {
...     "Nest1": ["alt1"],
...     "Nest2": {"Nest2_1": ["alt_2", "alt_3"], "Nest_2_2": ["alt_4", "alt_5"]},
... }

Methods

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

Initialize model configuration and sampler configuration for the model.

NestedLogit.apply_intervention(new_choice_df)

Apply one of two types of intervention.

NestedLogit.attrs_to_init_kwargs(attrs)

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

NestedLogit.build_from_idata(idata)

Build model from the InferenceData object.

NestedLogit.build_model(X, y, **kwargs)

Do not use, required by parent class.

NestedLogit.calculate_share_change(idata, ...)

Calculate difference in market share due to market intervention.

NestedLogit.create_fit_data(X, y)

Create the fit_data group based on the input data.

NestedLogit.create_idata_attrs()

Create the attributes for the InferenceData object.

NestedLogit.fit(extend_idata, kwargs)

Fit Nested Logit Model.

NestedLogit.graphviz(**kwargs)

Get the graphviz representation of the model.

NestedLogit.load(fname)

Create a ModelBuilder instance from a file.

NestedLogit.load_from_idata(idata)

Create a ModelBuilder instance from an InferenceData object.

NestedLogit.make_P_nest(U, W, betas_fixed, ...)

Calculate the probability of choosing a nest.

NestedLogit.make_exp_nest(U, W, betas_fixed, ...)

Calculate within-nest probabilities for nested logit models.

NestedLogit.make_model(X, W, y)

Build Model.

NestedLogit.parse_formula(df, formula, depvar)

Parse the three-part structure of a formula specification.

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

Plot change induced by a market intervention.

NestedLogit.post_sample_model_transformation()

Perform transformation on the model after sampling.

NestedLogit.predict([X, extend_idata])

Use a model to predict on unseen data and return point prediction of all the samples.

NestedLogit.predict_posterior([X, ...])

Generate posterior predictive samples on unseen data.

NestedLogit.predict_proba([X, extend_idata, ...])

Alias for predict_posterior, for consistency with scikit-learn probabilistic estimators.

NestedLogit.prepare_X_matrix(df, ...)

Prepare the X matrix for the utility equations.

NestedLogit.preprocess_model_data(choice_df, ...)

Pre-process the model initiation inputs into a format that can be used by the PyMC model.

NestedLogit.sample([...])

Sample all the things.

NestedLogit.sample_posterior_predictive(...)

Sample Posterior Predictive Distribution.

NestedLogit.sample_prior_predictive(...)

Sample Prior Predictive Distribution.

NestedLogit.save(fname)

Save the model's inference data to a file.

NestedLogit.set_idata_attrs([idata])

Set attributes on an InferenceData object.

Attributes

X

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

y