Cross-validation
Blocked K-fold cross-validation for choosing the number of latent classes. Folds are drawn at the decision-maker level so that an individual's entire choice history sits in exactly one fold.
Experimental
The cross-validation utility is functional but still under active refinement. See the model-selection tutorial for a worked example. And please feel free to suggest improvements or extensions; I've never done any scholarship on hyperparameter tuning, so I'd be excited to hear about recent developments in this area!
lcl._cross_validation.cv_optimal_classes(data, alts_col=None, cases_col=None, panels_col=None, num_classes_list=None, formula=None, utility_formula=None, membership_formula=None, choice_col=None, case_varnames=None, dem_varnames=None, dems_data=None, numeraire=None, folds=5, seed=42, *, spec=None, fit_options=None, optimization_options=None, em_alg_config=None, mle_config=None)
Perform blocked K-Fold Cross Validation to determine the optimal number of latent classes.
Splits the data safely at the panel (decision-maker) level to ensure that the same decision-maker does not appear in both the training and test folds.
The recommended high-level call mirrors :func:lcl.fit: pass the same
:class:~lcl.spec.LCLSpec you fit with, then sweep num_classes_list::
cv_optimal_classes(data, spec, num_classes_list=[2, 3, 4, 5])
The lower-level keyword form (alts_col=..., case_varnames=...,
em_alg_config=...) remains supported for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The main dataset containing choice situations and alternatives. |
required |
alts_col
|
str | LCLSpec | None
|
Either the alternative-identifier column name or, in the high-level form,
the :class: |
None
|
cases_col
|
str | None
|
Name of the column grouping observations into distinct choice situations.
Optional when an |
None
|
panels_col
|
str | None
|
Name of the column mapping observations to specific decision-makers.
Optional when an |
None
|
num_classes_list
|
Sequence[int]
|
A sequence of integers specifying the numbers of latent classes to evaluate (e.g., [2, 3, 4, 5, 10, 15, 20]). |
None
|
formula
|
str | None
|
Backward-compatible combined Formulaic string, for example
|
None
|
utility_formula
|
str | None
|
Formulaic string for the utility design, such as
|
None
|
membership_formula
|
str | None
|
Right-hand-side Formulaic string for class-membership demographics, such
as |
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
|
dem_varnames
|
Sequence[str] | None
|
List of demographic variables. |
None
|
dems_data
|
Any | None
|
A separate dataset containing panel-level demographics. |
None
|
numeraire
|
str | None
|
The variable to be constrained as strictly negative (e.g., "price") via softplus. |
None
|
folds
|
int
|
Number of cross-validation folds. |
5
|
seed
|
int
|
Random seed for replicable panel splitting. |
42
|
spec
|
:class:`~lcl.spec.LCLSpec` | None
|
Declarative specification supplying the identifier columns, utility and membership variables, formula, and numeraire constraint. Explicit keyword arguments override the corresponding spec fields. |
None
|
fit_options
|
:class:`~lcl._struct.FitOptions` | None
|
High-level EM fit options applied to every fold. Translated internally to
an :class: |
None
|
optimization_options
|
:class:`~lcl._struct.OptimizationOptions` | None
|
High-level M-step optimizer options applied to every fold. |
None
|
em_alg_config
|
:class:`~lcl._struct.EMAlgConfig`
|
Lower-level configuration for the EM algorithm loop. Ignored when
|
None
|
mle_config
|
:class:`~lcl._struct.MleConfig`
|
Lower-level configuration for the inner optimization routines. Ignored
when |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame containing the Average Out-of-Sample Log-Likelihood for each specified number of classes. |
Source code in src/lcl/_cross_validation.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 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 | |