choice.BinaryLogit
Guide & worked example · Source
choice.BinaryLogit(optimizer=None, maxiter=5000, tol=0.0001, device=None)Binary Logit model for discrete choice analysis.
This model estimates utility-based binary choice using the logistic (sigmoid) link function. Commonly used in structural estimation for modeling binary decisions such as product purchase, participation, etc.
Examples: >>> model = BinaryLogit() >>> X = torch.randn(100, 3) # 100 observations, 3 features >>> y = torch.randint(0, 2, (100,)) >>> model.fit(X, y) >>> probs = model.predict_proba(X)
Attributes
| Name | Description |
|---|---|
device |
|
history |
|
maxiter |
|
optimizer_class |
|
params |
|
tol |
Methods
| Name | Description |
|---|---|
| counterfactual | Compute change in market share for a counterfactual scenario. |
| fit | Fit the model using maximum likelihood estimation. |
| predict_proba | Predict choice probabilities for binary logit model. |
| simulate | Simulate binary choices from the fitted model. |
| summary | Print a formatted summary of model estimation results. |
| to | Move model parameters and fitted data to device. |
counterfactual
choice.BinaryLogit.counterfactual(X_new)Compute change in market share for a counterfactual scenario.
Compares predicted market share (average choice probability) between the original fitted data and a counterfactual scenario with different covariates.
Args: X_new: Counterfactual design matrix of shape (n_samples, n_features).
Returns: Dictionary containing: - market_share_original: Original market share (scalar tensor). - market_share_counterfactual: Counterfactual market share. - change_in_market_share: Difference in market shares.
Raises: ValueError: If model has not been fitted yet.
fit
choice.BinaryLogit.fit(
X,
y,
init_params=None,
verbose=False,
fe=None,
fe_design=None,
hdfe_index=None,
offset=None,
batch_size=None,
stderr=True,
)Fit the model using maximum likelihood estimation.
Optimizes the model parameters to minimize the negative log-likelihood using the specified PyTorch optimizer. After convergence, computes standard errors via the Fisher information matrix.
Args: X: Design matrix of shape (n_samples, n_features). Assumes X includes an intercept column if desired. y: Target vector of shape (n_samples,). init_params: Initial parameter values. For FE-aware GLMs, may be a packed parameter vector or a dict with coef and fe_coef. verbose: If True, prints convergence information. fe: Optional list of 1D fixed-effect ID vectors. fe_design: Optional list of sparse fixed-effect incidence matrices. hdfe_index: Optional index of the FE block to use for fast inference. offset: Optional offset vector added to the linear predictor. batch_size: Optional mini-batch size for first-order optimizers. stderr: Whether to compute standard errors.
Returns: Self (fitted estimator) with populated params dictionary.
Examples: >>> model = LogisticRegression() >>> model.fit(X_train, y_train, verbose=True) >>> print(model.params[“coef”])
predict_proba
choice.BinaryLogit.predict_proba(X)Predict choice probabilities for binary logit model.
Args: X: Design matrix of shape (n_samples, n_features).
Returns: Predicted probabilities of choosing option 1, shape (n_samples,).
Raises: ValueError: If model has not been fitted yet.
simulate
choice.BinaryLogit.simulate(X)Simulate binary choices from the fitted model.
Args: X: Design matrix of shape (n_samples, n_features).
Returns: Simulated binary choices of shape (n_samples,).
summary
choice.BinaryLogit.summary(alpha=0.05)Print a formatted summary of model estimation results.
Displays a comprehensive table including parameter estimates, standard errors, t-statistics, p-values, and confidence intervals (if standard errors are available).
Args: alpha: Significance level for confidence intervals (default: 0.05 for 95% confidence intervals).
Examples: >>> model.fit(X, y) >>> model.summary(alpha=0.01) # 99% confidence intervals
to
choice.BinaryLogit.to(device)Move model parameters and fitted data to device.