.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/hybrid_choice_models/plot_h02_lv_mimic_gauss.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_hybrid_choice_models_plot_h02_lv_mimic_gauss.py: Gaussian MIMIC model: maximum likelihood estimation =================================================== This example estimates a Gaussian MIMIC model with one latent variable and no choice component. The observed Likert indicators are treated as continuous responses and modeled with Gaussian measurement equations. The example introduces the latent-variable-only setting used later in the hybrid-choice tutorial. The latent-variable structure itself is imported from a separate semantic specification file. This script adds the Gaussian measurement configuration, the normalization constraints required for identification, and the maximum-likelihood estimation setup. The script performs the following steps: - load the latent-variable and indicator specifications, - define a Gaussian measurement configuration for all indicators, - define the normalization constraints used for identification, - resolve the semantic specification into an estimable model, - build the Biogeme expressions from the resolved model, - integrate over the latent variable using Monte Carlo draws, - construct the corresponding log-likelihood, - estimate the model, or reload previously saved estimation results, - display the estimated parameters as pandas and LaTeX tables. Michel Bierlaire Mon Jun 15 2026, 09:54:37 .. GENERATED FROM PYTHON SOURCE LINES 30-64 .. code-block:: Python from __future__ import annotations from likert_spec import likert_indicators, likert_types from number_of_draws import NUMBER_OF_DRAWS from one_latent_variable_spec import DEFAULT_SIGMA_START, latent_variables from optima import read_data import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.expressions import log from biogeme.latent_variables import ( BuildContext, EstimationMode, Fixing, IndicatorMeasurementSpec, MeasurementConfiguration, MeasurementIntercept, MeasurementLoading, MeasurementModel, NormalizationPlan, PositiveParameterSpec, PositivityMode, build_biogeme_model, resolve_model, ) from biogeme.results_processing import ( get_latex_estimated_parameters, get_latex_general_statistics, get_pandas_estimated_parameters, ) logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 65-66 Gaussian measurement configuration for all Likert indicators. .. GENERATED FROM PYTHON SOURCE LINES 66-77 .. code-block:: Python measurement_configuration = MeasurementConfiguration( specifications=[ IndicatorMeasurementSpec( indicator_name=indicator.name, measurement_model=MeasurementModel.GAUSSIAN, measurement_sigma=PositiveParameterSpec(start=DEFAULT_SIGMA_START), ) for indicator in likert_indicators ] ) .. GENERATED FROM PYTHON SOURCE LINES 78-80 Load the Optima data. --------------------- .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 83-89 Define the build context for maximum likelihood estimation. ----------------------------------------------------------- The build context specifies how the semantic latent-variable specification is translated into estimable Biogeme expressions. We start from the default maximum-likelihood context and explicitly select the log-exp parameterization for positive parameters. .. GENERATED FROM PYTHON SOURCE LINES 89-99 .. code-block:: Python default_context = BuildContext.default(EstimationMode.MAXIMUM_LIKELIHOOD) context = BuildContext( estimation_mode=default_context.estimation_mode, draw_type=default_context.draw_type, positivity_mode=PositivityMode.LOG_EXP, naming=default_context.naming, ordinal_eps=default_context.ordinal_eps, ordinal_enforce_order=default_context.ordinal_enforce_order, ) .. GENERATED FROM PYTHON SOURCE LINES 100-118 Identification constraints for the latent variable -------------------------------------------------- A latent variable is not directly observed, so its location and scale are not identified unless we impose normalization constraints. We use a reference-indicator strategy based on indicator ``Envir01``: 1. The intercept of the reference indicator is fixed to 0. This anchors the location of the latent-variable scale. 2. The loading of the reference indicator on the latent variable is fixed to -1. This anchors the scale of the latent variable and fixes its orientation. The negative sign means that higher values of the latent variable imply lower expected values for ``Envir01``. With these two constraints in place, the remaining intercepts and loadings are interpreted relative to the reference indicator. .. GENERATED FROM PYTHON SOURCE LINES 118-131 .. code-block:: Python normalization_plan = NormalizationPlan() normalization_plan.add( Fixing(MeasurementIntercept('Envir01'), 0.0, note='reference indicator: location') ) normalization_plan.add( Fixing( MeasurementLoading('car_centric_attitude', 'Envir01'), -1.0, note='reference indicator: scale and orientation', ) ) .. GENERATED FROM PYTHON SOURCE LINES 132-137 Resolve the semantic specification. ----------------------------------- The resolver combines the latent-variable specification, the indicator definitions, the Gaussian measurement configuration, and the normalization plan into an internal resolved model. .. GENERATED FROM PYTHON SOURCE LINES 137-146 .. code-block:: Python resolved_model = resolve_model( latent_variables=latent_variables, likert_indicators=likert_indicators, likert_types=likert_types, measurement_configuration=measurement_configuration, context=context, normalization_plan=normalization_plan, ) .. GENERATED FROM PYTHON SOURCE LINES 147-151 Build the Biogeme expressions. ------------------------------ The builder translates the resolved model into expressions that can be used by Biogeme for maximum-likelihood estimation. .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python built_model = build_biogeme_model(resolved_model) .. GENERATED FROM PYTHON SOURCE LINES 154-159 Log-likelihood -------------- The builder provides the Monte-Carlo integrated measurement likelihood. Taking its logarithm yields the log-likelihood used for maximum-likelihood estimation. .. GENERATED FROM PYTHON SOURCE LINES 159-162 .. code-block:: Python integrated_likelihood = built_model.integrated_likelihood log_likelihood = log(integrated_likelihood) .. GENERATED FROM PYTHON SOURCE LINES 163-166 Estimate the model with Biogeme. -------------------------------- Existing results are reloaded from the YAML file when available. .. GENERATED FROM PYTHON SOURCE LINES 166-180 .. code-block:: Python biogeme = BIOGEME( database, log_likelihood, number_of_draws=NUMBER_OF_DRAWS, calculating_second_derivatives='never', numerically_safe=False, max_iterations=5_000, group_of_parameters=built_model.parameter_groups, ) biogeme.model_name = 'plot_h02_lv_mimic_gauss' yaml_file_name = f'saved_results/{biogeme.model_name}.yaml' results = biogeme.estimate_or_load(yaml_file_name=yaml_file_name) .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. Estimation results are read from saved_results/plot_h02_lv_mimic_gauss.yaml. No estimation is performed. .. GENERATED FROM PYTHON SOURCE LINES 181-182 Display a compact summary and the estimated parameters. .. GENERATED FROM PYTHON SOURCE LINES 182-201 .. code-block:: Python print(results.short_summary()) pandas_results = get_pandas_estimated_parameters( estimation_results=results, group_of_parameters=built_model.parameter_groups, ) for group_name, pandas_table in pandas_results.items(): print(group_name if group_name else 'Estimated parameters') print(pandas_table) general_statistics = get_latex_general_statistics(estimation_results=results) print(general_statistics) estimated_parameters = get_latex_estimated_parameters( estimation_results=results, group_of_parameters=built_model.parameter_groups, ) for group_name, latex_table in estimated_parameters.items(): print(group_name if group_name else 'Estimated parameters') print(latex_table) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model plot_h02_lv_mimic_gauss Nbr of parameters: 32 Sample size: 889 Excluded data: 0 Final log likelihood: -13029.98 Akaike Information Criterion: 26123.95 Bayesian Information Criterion: 26277.24 Structural equation Name ... BHHH p-value 0 struct_car_centric_attitude_intercept ... 0.000000 1 struct_car_centric_attitude_top_manager ... 0.523173 2 struct_car_centric_attitude_car_oriented_parents ... 0.020318 3 struct_car_centric_attitude_high_education ... 0.000033 4 struct_car_centric_attitude_low_education ... 0.325572 5 struct_car_centric_attitude_used_to_go_to_scho... ... 0.858527 6 struct_car_centric_attitude_sigma_log ... 0.970816 [7 rows x 5 columns] Measurement equation: Envir01 Name Value ... BHHH t-stat. BHHH p-value 7 measurement_Envir01_sigma_log 0.009691 ... 0.215424 0.829437 [1 rows x 5 columns] Measurement equation: Envir02 Name ... BHHH p-value 8 measurement_intercept_Envir02 ... 0.000000 9 measurement_coefficient_car_centric_attitude_E... ... 0.000000 10 measurement_Envir02_sigma_log ... 0.363014 [3 rows x 5 columns] Measurement equation: Envir06 Name ... BHHH p-value 11 measurement_intercept_Envir06 ... 0.0 12 measurement_coefficient_car_centric_attitude_E... ... 0.0 13 measurement_Envir06_sigma_log ... 0.0 [3 rows x 5 columns] Measurement equation: Mobil03 Name ... BHHH p-value 14 measurement_intercept_Mobil03 ... 0.000000 15 measurement_coefficient_car_centric_attitude_M... ... 0.006289 16 measurement_Mobil03_sigma_log ... 0.000000 [3 rows x 5 columns] Measurement equation: Mobil05 Name ... BHHH p-value 17 measurement_intercept_Mobil05 ... 0.000000e+00 18 measurement_coefficient_car_centric_attitude_M... ... 2.253014e-08 19 measurement_Mobil05_sigma_log ... 0.000000e+00 [3 rows x 5 columns] Measurement equation: Mobil08 Name ... BHHH p-value 20 measurement_intercept_Mobil08 ... 0.000000e+00 21 measurement_coefficient_car_centric_attitude_M... ... 9.726642e-10 22 measurement_Mobil08_sigma_log ... 3.364021e-08 [3 rows x 5 columns] Measurement equation: Mobil09 Name ... BHHH p-value 23 measurement_intercept_Mobil09 ... 0.000000e+00 24 measurement_coefficient_car_centric_attitude_M... ... 1.798117e-12 25 measurement_Mobil09_sigma_log ... 3.289191e-03 [3 rows x 5 columns] Measurement equation: Mobil10 Name ... BHHH p-value 26 measurement_intercept_Mobil10 ... 0.000000e+00 27 measurement_coefficient_car_centric_attitude_M... ... 2.731662e-09 28 measurement_Mobil10_sigma_log ... 0.000000e+00 [3 rows x 5 columns] Measurement equation: LifSty07 Name ... BHHH p-value 29 measurement_intercept_LifSty07 ... 0.000000 30 measurement_coefficient_car_centric_attitude_L... ... 0.076143 31 measurement_LifSty07_sigma_log ... 0.000000 [3 rows x 5 columns] %% General statistics \section{General statistics} \begin{tabular}{ll} Number of estimated parameters & 32 \\ Sample size & 889 \\ Excluded observations & 0 \\ Init log likelihood & -26577.53 \\ Final log likelihood & -13029.98 \\ Likelihood ratio test for the init. model & 27095.1 \\ Rho-square for the init. model & 0.51 \\ Rho-square-bar for the init. model & 0.509 \\ Akaike Information Criterion & 26123.95 \\ Bayesian Information Criterion & 26277.24 \\ Final gradient norm & 2.5847E-01 \\ Number of draws & 50000 \\ Draws generation time & 0:00:13.248030 \\ Types of draws & struct\_car\_centric\_attitude\_draws: NORMAL\_MLHS\_ANTI \\ Bootstrapping time & None \\ Algorithm & \verb$BFGS with trust region for simple bound constraints$ \\ Cause of termination & \verb$Relative gradient = 5.4e-06 <= 6.1e-06$ \\ Number of function evaluations & \verb$807$ \\ Number of gradient evaluations & \verb$459$ \\ Number of hessian evaluations & \verb$0$ \\ Number of iterations & \verb$348$ \\ Optimization time & \verb$0:25:49.527588$ \\ Proportion of Hessian calculation & \verb$0/229 = 0.0%$ \\ Relative gradient & \verb$5.356e-06$ \\ \end{tabular} Structural equation \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 0 & struct\_car\_centric\_attitude\_intercept & -2&64 & 0&115 & -23&0 & 0&00 \\ 1 & struct\_car\_centric\_attitude\_top\_manager & 0&0914 & 0&143 & 0&638 & 0&523 \\ 2 & struct\_car\_centric\_attitude\_car\_oriented\_parents & 0&237 & 0&102 & 2&32 & 0&0203 \\ 3 & struct\_car\_centric\_attitude\_high\_education & -0&541 & 0&130 & -4&15 & 3&27e-05 \\ 4 & struct\_car\_centric\_attitude\_low\_education & 0&123 & 0&125 & 0&983 & 0&326 \\ 5 & struct\_car\_centric\_attitude\_used\_to\_go\_to\_school\_by\_car & 0&0998 & 0&560 & 0&178 & 0&859 \\ 6 & struct\_car\_centric\_attitude\_sigma\_log & -0&00271 & 0&0740 & -0&0366 & 0&971 \\ \end{tabular} Measurement equation: Envir01 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 7 & measurement\_Envir01\_sigma\_log & 0&00969 & 0&0450 & 0&215 & 0&829 \\ \end{tabular} Measurement equation: Envir02 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 8 & measurement\_intercept\_Envir02 & 1&76 & 0&160 & 11&0 & 0&00 \\ 9 & measurement\_coefficient\_car\_centric\_attitude\_Envir02 & -0&597 & 0&0574 & -10&4 & 0&00 \\ 10 & measurement\_Envir02\_sigma\_log & 0&0247 & 0&0271 & 0&910 & 0&363 \\ \end{tabular} Measurement equation: Envir06 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 11 & measurement\_intercept\_Envir06 & 3&30 & 0&101 & 32&8 & 0&00 \\ 12 & measurement\_coefficient\_car\_centric\_attitude\_Envir06 & -0&376 & 0&0385 & -9&77 & 0&00 \\ 13 & measurement\_Envir06\_sigma\_log & -0&303 & 0&0183 & -16&5 & 0&00 \\ \end{tabular} Measurement equation: Mobil03 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 14 & measurement\_intercept\_Mobil03 & 2&86 & 0&156 & 18&3 & 0&00 \\ 15 & measurement\_coefficient\_car\_centric\_attitude\_Mobil03 & -0&150 & 0&0550 & -2&73 & 0&00629 \\ 16 & measurement\_Mobil03\_sigma\_log & 0&289 & 0&0286 & 10&1 & 0&00 \\ \end{tabular} Measurement equation: Mobil05 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 17 & measurement\_intercept\_Mobil05 & 2&42 & 0&172 & 14&0 & 0&00 \\ 18 & measurement\_coefficient\_car\_centric\_attitude\_Mobil05 & -0&339 & 0&0607 & -5&59 & 2&25e-08 \\ 19 & measurement\_Mobil05\_sigma\_log & 0&320 & 0&0288 & 11&1 & 0&00 \\ \end{tabular} Measurement equation: Mobil08 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 20 & measurement\_intercept\_Mobil08 & 3&22 & 0&150 & 21&4 & 0&00 \\ 21 & measurement\_coefficient\_car\_centric\_attitude\_Mobil08 & 0&336 & 0&0550 & 6&11 & 9&73e-10 \\ 22 & measurement\_Mobil08\_sigma\_log & 0&141 & 0&0255 & 5&52 & 3&36e-08 \\ \end{tabular} Measurement equation: Mobil09 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 23 & measurement\_intercept\_Mobil09 & 2&95 & 0&133 & 22&1 & 0&00 \\ 24 & measurement\_coefficient\_car\_centric\_attitude\_Mobil09 & -0&351 & 0&0498 & -7&05 & 1&80e-12 \\ 25 & measurement\_Mobil09\_sigma\_log & 0&0575 & 0&0196 & 2&94 & 0&00329 \\ \end{tabular} Measurement equation: Mobil10 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 26 & measurement\_intercept\_Mobil10 & 5&30 & 0&266 & 19&9 & 0&00 \\ 27 & measurement\_coefficient\_car\_centric\_attitude\_Mobil10 & 0&533 & 0&0896 & 5&95 & 2&73e-09 \\ 28 & measurement\_Mobil10\_sigma\_log & 0&594 & 0&0428 & 13&9 & 0&00 \\ \end{tabular} Measurement equation: LifSty07 \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{BHHH} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 29 & measurement\_intercept\_LifSty07 & 2&49 & 0&141 & 17&7 & 0&00 \\ 30 & measurement\_coefficient\_car\_centric\_attitude\_LifSty07 & 0&0866 & 0&0488 & 1&77 & 0&0761 \\ 31 & measurement\_LifSty07\_sigma\_log & 0&217 & 0&0247 & 8&76 & 0&00 \\ \end{tabular} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.083 seconds) .. _sphx_glr_download_auto_examples_hybrid_choice_models_plot_h02_lv_mimic_gauss.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_h02_lv_mimic_gauss.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_h02_lv_mimic_gauss.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_h02_lv_mimic_gauss.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_