NestedLogit#

class pymc_marketing.customer_choice.nested_logit.NestedLogit(choice_df=None, utility_equations=None, depvar=None, covariates=None, nesting_structure=None, model_config=None, sampler_config=None, alphas_nests=False)[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_df (DataFrame | None) – Deprecated. Pass the choice data to fit() instead. 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 (list[str] | None) –

    Formulas specifying how to model the utility of each product alternative. The formulas use Wilkinson style notation. The target product is specified as a function of the alternative-specific attributes. Individual-specific attributes may be added after a |:

    target_product ~ target_attribute1 + target_attribute2 | individual_attribute
    

  • depvar (str | None) – The name of the dependent variable in the choice_df.

  • covariates (list[str] | None) – Covariate names (e.g., [‘X1’, ‘X2’]).

  • nesting_structure (dict | None) – Dictionary to specify how to nest the choices between products. Single-layer nesting only. For more complex substitution patterns, consider using MixedLogit instead.

  • model_config (dict | None) – Model configuration. If None, the default config is used.

  • sampler_config (dict | None) – Sampler configuration. If None, the default config is used.

Examples

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 (single-layer only):

nesting_structure = {
    "Land": ["alt_1", "alt_2"],
    "Air": ["alt_3"],
}

Create the model with its settings and pass the choice data to fit:

nstL = NestedLogit(
    utility_equations=utility_equations,
    depvar="depvar",
    covariates=["X1", "X2"],
    nesting_structure=nesting_structure,
)
nstL.fit(choice_df)

Methods

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

Initialize the model.

NestedLogit.apply_intervention(new_choice_df)

Apply one of two types of intervention.

NestedLogit.attrs_to_init_kwargs(attrs)

Rehydrate the __init__ kwargs from the serialised attrs.

NestedLogit.build_from_idata(idata)

Build the model from a loaded DataTree.

NestedLogit.build_model(**kwargs)

Build the model from the stored choice data and utility equations.

NestedLogit.calc_conditional_prob(U, ...)

Calculate conditional probability within a nest.

NestedLogit.calculate_share_change(idata, ...)

Calculate difference in market share due to market intervention.

NestedLogit.create_fit_data_group()

Build the fit_data group storing the choice data.

NestedLogit.create_idata_attrs()

Create the attributes for the DataTree.

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

Fit the discrete choice model.

NestedLogit.graphviz(**kwargs)

Get the graphviz representation of the model.

NestedLogit.idata_to_init_kwargs(idata)

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

NestedLogit.load(fname[, check])

Create a ModelBuilder instance from a file.

NestedLogit.load_from_idata(idata[, check])

Create a ModelBuilder instance from a DataTree object.

NestedLogit.make_alt_coefs()

Create coefficients for alternative-specific covariates.

NestedLogit.make_fixed_coefs(X_fixed, n_obs, ...)

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

NestedLogit.make_intercepts()

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

NestedLogit.make_lambdas()

Create nest-specific lambda (scale) parameters.

NestedLogit.make_model(X, W, y, *[, coords, ...])

Build nested logit model.

NestedLogit.make_nest_probs(U, lambdas, ...)

Calculate nest selection probabilities and conditional probabilities.

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.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 from posterior predictive distribution.

NestedLogit.sample_prior_predictive([...])

Sample from prior predictive distribution.

NestedLogit.save(fname, **kwargs)

Save the model's inference data to a file.

NestedLogit.set_idata_attrs([idata])

Set attributes on a DataTree object.

NestedLogit.table(**model_table_kwargs)

Get the summary table of the model.

Attributes

NestedLogit.default_model_config

Default model configuration.

NestedLogit.default_sampler_config

Default sampler configuration.

NestedLogit.fit_result

Get the posterior fit_result.

NestedLogit.id

Generate a unique hash value for the model.

NestedLogit.output_var

The output variable of the model.

NestedLogit.posterior

Access the 'posterior' group of the DataTree.

NestedLogit.posterior_predictive

Access the 'posterior_predictive' group of the DataTree.

NestedLogit.predictions

Access the 'predictions' group of the DataTree.

NestedLogit.prior

Access the 'prior' group of the DataTree.

NestedLogit.prior_predictive

Access the 'prior_predictive' group of the DataTree.

NestedLogit.version

NestedLogit.choice_df

NestedLogit.utility_equations

NestedLogit.idata

NestedLogit.sampler_config

NestedLogit.model_config

NestedLogit.model

NestedLogit.is_fitted_