Conditional logit
The McFadden conditional logit estimates one taste vector for the full sample. It provides a useful homogeneous benchmark for latent-class specifications.
Use utility_formula for new formula-based specifications, and use the current
optimization and inference option objects:
from lcl import ConditionalLogit, InferenceOptions, OptimizationOptions, Options
results = ConditionalLogit(numeraire="price").fit(
data,
alts_col="alternative",
cases_col="choice_situation",
panels_col="respondent",
utility_formula="chosen ~ price + time + C(mode)",
weights="survey_weight",
options=Options(
optimization=OptimizationOptions(gradient_tol=1e-6),
inference=InferenceOptions(covariance="clustered"),
),
)
coefficient_table = results.summarize_betas(show=False)
Weights are case-level. Prefer a column name or case-keyed mapping because those
forms preserve identity when rows are reordered. If case IDs repeat across panels,
key a mapping by (panel_id, case_id). A sequence is interpreted in
first-case-appearance order and realigned after encoding.
With panels_col, BIC, CAIC, and adjusted BIC use the number of panels as their
sample size; otherwise they use the number of choice situations. A
softplus-constrained numeraire does not have an ordinary zero-null p-value, so its
reported p-value is NaN.
covariance="clustered" clusters at the panel level when panels_col is
provided. covariance="robust" always requests case-level Huber–White inference;
the two labels are not aliases. The result also reports the null log likelihood,
McFadden rho-squared, final score, and information diagnostics.
Prediction returns a CLPrediction rather than a bare
frame. Probabilities remain in prediction.predicted_probs, with WTP,
elasticities, market shares, aggregate elasticities, denominator diagnostics,
and surplus available through the same methods as latent-class prediction.
prediction = results.predict(counterfactual_data, panel_weights="survey_weight")
wtp = prediction.wtp("time", se="bootstrap", bootstrap_draws=1_000)
elasticities = prediction.elasticities(["price", "time"])
market_shares = prediction.market_shares()
Model
lcl.ConditionalLogit(numeraire=None, numeraire_min_abs=DEFAULT_NEGATIVE_MIN_ABS)
Bases: ChoiceModel
Specification and estimation for standard Multinomial Conditional Logit models.
Unlike the Latent Class variant, this model estimates a single vector of homogeneous taste parameters across the entire sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numeraire
|
str | None
|
The name of the variable (e.g., 'price') to use as the numeraire. If provided, its coefficient is bounded to be strictly negative to ensure logically consistent utility scaling and willingness-to-pay calculations. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
numeraire_idx |
int | None
|
The column index of the numeraire variable in the expanded design matrix. |
Create an unfitted conditional-logit model specification.
Source code in src/lcl/conditional_logit.py
fit(data, alts_col, cases_col, panels_col=None, utility_formula=None, choice_col=None, case_varnames=None, variable_labels=None, weights=None, weight_type='probability', init_beta=None, options=None, optimization_options=None, inference=None)
Fit the conditional logit model via Maximum Likelihood Estimation.
Supports both R-style formulas (via formulaic) and explicit lists of variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | DataFrame | ArrayLike
|
The main dataset containing choice situations and alternatives in long format. |
required |
alts_col
|
str
|
Name of the column containing alternative identifiers. |
required |
cases_col
|
str
|
Name of the column grouping observations into distinct choice situations. |
required |
panels_col
|
str | None
|
Name of the column mapping observations to specific decision-makers. If provided, the covariance matrix is automatically clustered at the panel level. If omitted, standard Huber-White robust standard errors are computed. |
None
|
utility_formula
|
str | None
|
Preferred Formulaic string for the alternative-specific utility
specification. If it includes a left-hand side, that outcome is used
as the choice indicator; otherwise |
None
|
choice_col
|
str | None
|
Name of the boolean/binary column indicating chosen alternatives. |
None
|
case_varnames
|
Sequence[str] | None
|
List of alternative-specific variables. |
None
|
variable_labels
|
Mapping[str, str] | None
|
Optional mapping from raw DataFrame/model variable names to human-readable labels used in printed coefficient tables. |
None
|
weights
|
str, Mapping, ArrayLike, or None
|
Case-level weights. A string names a data column that must be constant
within case; a mapping is keyed by case ID (or |
None
|
weight_type
|
(probability, frequency)
|
How |
"probability"
|
init_beta
|
ArrayLike | None
|
|
None
|
optimization_options
|
OptimizationOptions | None
|
Preferred safeguarded exact-Newton settings. |
None
|
inference
|
InferenceOptions | None
|
Preferred covariance and standard-error settings. |
None
|
Returns:
| Type | Description |
|---|---|
class:`~lcl.conditional_logit.CLResults`
|
Results container housing coefficients, robust standard errors, and fit statistics. |
Source code in src/lcl/conditional_logit.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
Results
lcl.results.CLResults(model_spec, optim_res, data_struct, inference, estim_time_sec, has_panels, case_weights, weight_type='probability', cluster_of_cases=None, num_clusters=None)
Post-estimation results and inference container for Conditional Logit.
Automatically handles the derivation of robust standard errors via the Delta Method if a softplus-constrained numeraire is specified in the model specification.
Compute inference summaries from a fitted conditional-logit model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_spec
|
:class:`~lcl.conditional_logit.ConditionalLogit`
|
Fitted model specification and variable metadata. |
required |
optim_res
|
:class:`~lcl._struct.OptimizeResult`
|
Optimizer output containing parameters, gradients, and Hessian inverse. |
required |
data_struct
|
:class:`~lcl._struct.Data`
|
Encoded estimation data. |
required |
inference
|
:class:`~lcl.options.InferenceOptions`
|
Covariance and standard-error configuration. |
required |
estim_time_sec
|
float
|
Wall-clock estimation time in seconds. |
required |
has_panels
|
bool
|
Whether robust covariance should cluster scores at the panel level. |
required |
case_weights
|
ArrayLike
|
Case weights aligned with encoded choice situations. |
required |
weight_type
|
(probability, frequency)
|
Interpretation of |
"probability"
|
cluster_of_cases
|
ArrayLike | None
|
Zero-indexed cluster identifier per case, for clustering coarser than the panel. |
None
|
num_clusters
|
int | None
|
Number of distinct clusters implied by |
None
|
Source code in src/lcl/conditional_logit.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
abic
property
Deprecated alias for :attr:adjusted_bic.
convergence
property
Deprecated alias for :attr:converged.
covariance
property
Deprecated alias for :attr:cov_matrix.
covariance_available
property
Report whether a usable covariance matrix was estimated.
flat_params
property
Latent parameter vector, aligned with :attr:latent_cov_matrix.
coefficient_table()
Return conditional-logit coefficients with presentation labels.
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per alternative-specific variable with raw variable names, display labels, estimates, standard errors, z-values, and p-values. |
Source code in src/lcl/conditional_logit.py
diagnostics()
Return convergence, fit, score, and information diagnostics.
Source code in src/lcl/conditional_logit.py
loglik(data, *, per_case=False)
Score observed choices with the fitted conditional-logit encoder.
Source code in src/lcl/conditional_logit.py
parameter_names()
predict(data, *, alts_col=None, cases_col=None, panels_col=None, panel_weights=None)
Predict conditional choice probabilities for a given set of alternatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | DataFrame
|
The counterfactual dataset. Must contain all variables specified in the original model (including expanded dummy columns if a formula was used). |
required |
alts_col
|
str
|
Name of the column containing alternative identifiers. |
None
|
cases_col
|
str
|
Name of the column grouping observations into distinct choice situations. |
None
|
panels_col
|
str | None
|
Name of the column mapping observations to specific decision-makers. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing the computed out-of-sample choice probabilities. |
Source code in src/lcl/conditional_logit.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 | |
summarize(num_decimals=3, *, show=True)
summarize_betas(header=('Variable', 'Estimate', 'Std. Error'), num_decimals=3, *, show=True)
Print and return a table of parameter estimates and standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
tuple[str, str, str]
|
Column labels used for printed LaTeX and terminal tables. |
("Variable", "Estimate", "Std. Error")
|
num_decimals
|
int
|
Number of decimal places used in printed tables. |
3
|
show
|
bool
|
Emit LaTeX and terminal renderings. Set to |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Tidy coefficient table. The |
Source code in src/lcl/conditional_logit.py
lcl.results.CLPrediction(predicted_probs_df, surplus_df, wtp_alt_vars_by_panel_df, predict_data, results, class_probs_by_panel=None, class_probabilities_source='prior', partition_data_df=None, original_alts=None, original_cases=None, original_panels=None, raw_prediction_data=None, panel_weights=None, past_diff_unchosen_chosen=None, past_data=None)
Bases: _PredictionBase
Conditional-logit prediction with WTP and elasticity diagnostics.
Source code in src/lcl/_prediction.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
compute_wtp(target=None, **kwargs)
denominator_diagnostics()
Report the homogeneous WTP denominator and configured floor.
Source code in src/lcl/_prediction.py
wtp(target=None, *, se='delta', bootstrap_draws=500, bootstrap_seed=0)
Return homogeneous WTP ratios with delta or parametric-bootstrap SEs.
Both methods work in the unconstrained parameterization and apply the softplus transform inside the target function. Drawing structural coefficients directly would put mass on a positive numeraire coefficient -- a region the constraint excludes -- and the resulting ratios have no finite variance to summarize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str | None
|
Restrict the table to one non-numeraire variable. |
None
|
se
|
(delta, bootstrap, none)
|
Standard-error method. |
"delta"
|
bootstrap_draws
|
int
|
Number of asymptotic parameter draws for |
500
|
bootstrap_seed
|
int
|
Reproducible seed for those draws. |
0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per variable with its tradeoff ratio and standard error. |
Source code in src/lcl/_prediction.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 | |