.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/latent/plot_b04latent_choice_seq.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_latent_plot_b04latent_choice_seq.py: Choice model with a latent variable: sequential estimation ========================================================== Mixture of logit. Measurement equation for the indicators. Sequential estimation. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 17:32:34 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-58 .. code-block:: Python import sys import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme import models from biogeme.exceptions import BiogemeError import biogeme.distributions as dist import biogeme.results as res from biogeme.expressions import ( Beta, RandomVariable, exp, log, Integrate, ) from read_or_estimate import read_or_estimate from biogeme.data.optima import ( read_data, age_65_more, moreThanOneCar, moreThanOneBike, individualHouse, male, haveChildren, haveGA, highEducation, WaitingTimePT, Choice, TimePT_scaled, TimeCar_scaled, MarginalCostPT_scaled, CostCarCHF_scaled, distance_km_scaled, PurpHWH, PurpOther, ScaledIncome, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b04latent_choice_seq.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b04latent_choice_seq.py .. GENERATED FROM PYTHON SOURCE LINES 59-60 Read the estimates from the structural equation estimation. .. GENERATED FROM PYTHON SOURCE LINES 60-71 .. code-block:: Python MODELNAME = 'b02one_latent_ordered' try: struct_results = res.bioResults(pickle_file=f'saved_results/{MODELNAME}.pickle') except BiogemeError: print( f'Run first the script {MODELNAME}.py in order to generate the ' f'file {MODELNAME}.pickle, and move it to the directory saved_results' ) sys.exit() struct_betas = struct_results.get_beta_values() .. GENERATED FROM PYTHON SOURCE LINES 72-73 Coefficients. .. GENERATED FROM PYTHON SOURCE LINES 73-83 .. code-block:: Python coef_intercept = struct_betas['coef_intercept'] coef_age_65_more = struct_betas['coef_age_65_more'] coef_haveGA = struct_betas['coef_haveGA'] coef_moreThanOneCar = struct_betas['coef_moreThanOneCar'] coef_moreThanOneBike = struct_betas['coef_moreThanOneBike'] coef_individualHouse = struct_betas['coef_individualHouse'] coef_male = struct_betas['coef_male'] coef_haveChildren = struct_betas['coef_haveChildren'] coef_highEducation = struct_betas['coef_highEducation'] .. GENERATED FROM PYTHON SOURCE LINES 84-85 Latent variable: structural equation. .. GENERATED FROM PYTHON SOURCE LINES 87-89 Define a random parameter, normally distributed, designed to be used for numerical integration .. GENERATED FROM PYTHON SOURCE LINES 89-122 .. code-block:: Python omega = RandomVariable('omega') density = dist.normalpdf(omega) sigma_s = Beta('sigma_s', 1, None, None, 0) thresholds = [None, 4, 6, 8, 10, None] formula_income = models.piecewise_formula( variable=ScaledIncome, thresholds=thresholds, betas=[ struct_betas['beta_ScaledIncome_minus_inf_4'], struct_betas['beta_ScaledIncome_4_6'], struct_betas['beta_ScaledIncome_6_8'], struct_betas['beta_ScaledIncome_8_10'], struct_betas['beta_ScaledIncome_10_inf'], ], ) CARLOVERS = ( coef_intercept + coef_age_65_more * age_65_more + formula_income + coef_moreThanOneCar * moreThanOneCar + coef_moreThanOneBike * moreThanOneBike + coef_individualHouse * individualHouse + coef_male * male + coef_haveChildren * haveChildren + coef_haveGA * haveGA + coef_highEducation * highEducation + sigma_s * omega ) .. GENERATED FROM PYTHON SOURCE LINES 123-124 Choice model. .. GENERATED FROM PYTHON SOURCE LINES 124-134 .. code-block:: Python ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_PT = Beta('ASC_PT', 0, None, None, 1) ASC_SM = Beta('ASC_SM', 0, None, None, 0) BETA_COST_HWH = Beta('BETA_COST_HWH', 0, None, None, 0) BETA_COST_OTHER = Beta('BETA_COST_OTHER', 0, None, None, 0) BETA_DIST = Beta('BETA_DIST', 0, None, None, 0) BETA_TIME_CAR_REF = Beta('BETA_TIME_CAR_REF', 0, None, 0, 0) BETA_TIME_PT_REF = Beta('BETA_TIME_PT_REF', 0, None, 0, 0) BETA_WAITING_TIME = Beta('BETA_WAITING_TIME', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 135-138 The coefficient of the latent variable should be initialized to something different from zero. If not, the algorithm may be trapped in a local optimum, and never change the value. .. GENERATED FROM PYTHON SOURCE LINES 138-143 .. code-block:: Python BETA_TIME_PT_CL = Beta('BETA_TIME_PT_CL', -0.01, None, None, 0) BETA_TIME_PT = BETA_TIME_PT_REF * exp(BETA_TIME_PT_CL * CARLOVERS) BETA_TIME_CAR_CL = Beta('BETA_TIME_CAR_CL', -0.01, None, None, 0) BETA_TIME_CAR = BETA_TIME_CAR_REF * exp(BETA_TIME_CAR_CL * CARLOVERS) .. GENERATED FROM PYTHON SOURCE LINES 144-145 Definition of utility functions:. .. GENERATED FROM PYTHON SOURCE LINES 145-164 .. code-block:: Python V0 = ( ASC_PT + BETA_TIME_PT * TimePT_scaled + BETA_WAITING_TIME * WaitingTimePT + BETA_COST_HWH * MarginalCostPT_scaled * PurpHWH + BETA_COST_OTHER * MarginalCostPT_scaled * PurpOther ) V1 = ( ASC_CAR + BETA_TIME_CAR * TimeCar_scaled + BETA_COST_HWH * CostCarCHF_scaled * PurpHWH + BETA_COST_OTHER * CostCarCHF_scaled * PurpOther ) V2 = ASC_SM + BETA_DIST * distance_km_scaled .. GENERATED FROM PYTHON SOURCE LINES 165-166 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 166-168 .. code-block:: Python V = {0: V0, 1: V1, 2: V2} .. GENERATED FROM PYTHON SOURCE LINES 169-170 Conditional on omega, we have a logit model (called the kernel). .. GENERATED FROM PYTHON SOURCE LINES 170-172 .. code-block:: Python condprob = models.logit(V, None, Choice) .. GENERATED FROM PYTHON SOURCE LINES 173-174 We integrate over omega using numerical integration. .. GENERATED FROM PYTHON SOURCE LINES 174-176 .. code-block:: Python loglike = log(Integrate(condprob * density, 'omega')) .. GENERATED FROM PYTHON SOURCE LINES 177-178 Read the data .. GENERATED FROM PYTHON SOURCE LINES 178-180 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 181-182 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: Python the_biogeme = bio.BIOGEME(database, loglike) the_biogeme.modelName = 'b04latent_choice_seq' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 186-188 If estimation results are saved on file, we read them to speed up the process. If not, we estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 188-190 .. code-block:: Python results = read_or_estimate(the_biogeme=the_biogeme, directory='saved_results') .. GENERATED FROM PYTHON SOURCE LINES 191-195 .. code-block:: Python print(f'Estimated betas: {len(results.data.betaValues)}') print(f'Final log likelihood: {results.data.logLike:.3f}') print(f'Output file: {results.data.htmlFileName}') .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated betas: 11 Final log likelihood: -1094.811 Output file: b04latent_choice_seq.html .. GENERATED FROM PYTHON SOURCE LINES 196-197 .. code-block:: Python results.get_estimated_parameters() .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.818249 0.123298 6.636358 3.215295e-11
ASC_SM 1.979021 0.267020 7.411508 1.247891e-13
BETA_COST_HWH -1.715963 0.185522 -9.249369 0.000000e+00
BETA_COST_OTHER -0.890702 0.110066 -8.092461 6.661338e-16
BETA_DIST -5.871872 0.736174 -7.976202 1.554312e-15
BETA_TIME_CAR_CL -0.151834 0.017993 -8.438370 0.000000e+00
BETA_TIME_CAR_REF -14.372652 1.008616 -14.249870 0.000000e+00
BETA_TIME_PT_CL -0.141156 0.017664 -7.991069 1.332268e-15
BETA_TIME_PT_REF -4.373281 0.504807 -8.663266 0.000000e+00
BETA_WAITING_TIME -0.035181 0.006745 -5.215922 1.829051e-07
sigma_s 9.737609 1.154931 8.431337 0.000000e+00


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.294 seconds) .. _sphx_glr_download_auto_examples_latent_plot_b04latent_choice_seq.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b04latent_choice_seq.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b04latent_choice_seq.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b04latent_choice_seq.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_