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 tofit()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_equationslist: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.
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.
Calculate conditional probability within a nest.
NestedLogit.calculate_share_change(idata, ...)Calculate difference in market share due to market intervention.
Build the
fit_datagroup storing the choice data.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.
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.
Create coefficients for alternative-specific covariates.
NestedLogit.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.
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.
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.
Sample from posterior predictive distribution.
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
Default model configuration.
Default sampler configuration.
Get the posterior fit_result.
Generate a unique hash value for the model.
The output variable of the model.
Access the 'posterior' group of the DataTree.
Access the 'posterior_predictive' group of the DataTree.
Access the 'predictions' group of the DataTree.
Access the 'prior' group of the DataTree.
Access the 'prior_predictive' group of the DataTree.