MNLogit#

class pymc_marketing.customer_choice.mnl_logit.MNLogit(choice_df, utility_equations, depvar, covariates, model_config=None, sampler_config=None)[source]#

Multinomial Logit class.

Class to perform a multinomial logit analysis with the specific intent of determining the product attribute effects on consumer preference.

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’]).

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",
]

Methods

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

Initialize model configuration and sampler configuration for the model.

MNLogit.apply_intervention(new_choice_df[, ...])

Apply one of two types of intervention.

MNLogit.attrs_to_init_kwargs(attrs)

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

MNLogit.build_from_idata(idata)

Build model from loaded InferenceData.

MNLogit.build_model(**kwargs)

Build model using stored choice_df and utility_equations.

MNLogit.calculate_share_change(idata, new_idata)

Calculate difference in market share due to market intervention.

MNLogit.create_idata_attrs()

Create attributes for InferenceData.

MNLogit.fit([choice_df, utility_equations, ...])

Fit the discrete choice model.

MNLogit.graphviz(**kwargs)

Get the graphviz representation of the model.

MNLogit.idata_to_init_kwargs(idata)

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

MNLogit.load(fname[, check])

Create a ModelBuilder instance from a file.

MNLogit.load_from_idata(idata[, check])

Create a ModelBuilder instance from an InferenceData object.

MNLogit.make_alt_coefs()

Create coefficients for alternative-specific covariates.

MNLogit.make_choice_prob(U)

Compute choice probabilities via softmax transformation.

MNLogit.make_fixed_coefs(X_fixed, n_obs, n_alts)

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

MNLogit.make_intercepts()

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

MNLogit.make_model(X, F, y)

Build Model.

MNLogit.make_utility(X_data, alphas, betas, F)

Compute systematic utility for each alternative.

MNLogit.parse_formula(df, formula, depvar)

Parse the three-part structure of a formula specification.

MNLogit.plot_change(change_df[, title, figsize])

Plot change induced by a market intervention.

MNLogit.prepare_X_matrix(df, ...)

Prepare the X matrix for the utility equations.

MNLogit.preprocess_model_data(choice_df, ...)

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

MNLogit.sample([...])

Sample all the things.

MNLogit.sample_posterior_predictive([...])

Sample from posterior predictive distribution.

MNLogit.sample_prior_predictive([choice_df, ...])

Sample from prior predictive distribution.

MNLogit.save(fname, **kwargs)

Save the model's inference data to a file.

MNLogit.set_idata_attrs([idata])

Set attributes on an InferenceData object.

MNLogit.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