.. 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_mc.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_mc.py: Choice model with a latent variable: sequential estimation (Monte-Carlo) ======================================================================== Mixture of logit, with Monte-Carlo integration Measurement equation for the indicators. Sequential estimation. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 18:00:05 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-55 .. code-block:: Python import sys import biogeme.biogeme as bio import biogeme.biogeme_logging as blog from biogeme.exceptions import BiogemeError from biogeme import models 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, ) from biogeme.expressions import ( Beta, bioDraws, MonteCarlo, exp, log, ) from biogeme.results import bioResults from read_or_estimate import read_or_estimate logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b04latent_choice_seq_mc.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b04latent_choice_seq_mc.py .. GENERATED FROM PYTHON SOURCE LINES 56-57 Read the estimates from the structural equation estimation. .. GENERATED FROM PYTHON SOURCE LINES 57-68 .. code-block:: Python MODELNAME = 'b02one_latent_ordered' try: struct_results = 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 69-70 Coefficients. .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. 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 81-82 Latent variable: structural equation. .. GENERATED FROM PYTHON SOURCE LINES 84-86 Define a random parameter, normally distributed, designed to be used for numerical integration .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python sigma_s = Beta('sigma_s', 1, None, None, 0) error_component = sigma_s * bioDraws('EC', 'NORMAL_MLHS') .. GENERATED FROM PYTHON SOURCE LINES 91-92 Piecewise linear specification for income. .. GENERATED FROM PYTHON SOURCE LINES 92-120 .. code-block:: Python 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 + error_component ) .. GENERATED FROM PYTHON SOURCE LINES 121-122 Choice model. .. GENERATED FROM PYTHON SOURCE LINES 122-132 .. 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 133-136 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 136-139 .. code-block:: Python BETA_TIME_PT_CL = Beta('BETA_TIME_PT_CL', -0.01, None, None, 0) BETA_TIME_CAR_CL = Beta('BETA_TIME_CAR_CL', -0.01, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 140-141 Definition of utility functions:. .. GENERATED FROM PYTHON SOURCE LINES 141-162 .. code-block:: Python BETA_TIME_PT = BETA_TIME_PT_REF * exp(BETA_TIME_PT_CL * CARLOVERS) V0 = ( ASC_PT + BETA_TIME_PT * TimePT_scaled + BETA_WAITING_TIME * WaitingTimePT + BETA_COST_HWH * MarginalCostPT_scaled * PurpHWH + BETA_COST_OTHER * MarginalCostPT_scaled * PurpOther ) BETA_TIME_CAR = BETA_TIME_CAR_REF * exp(BETA_TIME_CAR_CL * CARLOVERS) 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 163-164 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 164-166 .. code-block:: Python V = {0: V0, 1: V1, 2: V2} .. GENERATED FROM PYTHON SOURCE LINES 167-168 Conditional on omega, we have a logit model (called the kernel). .. GENERATED FROM PYTHON SOURCE LINES 168-170 .. code-block:: Python condprob = models.logit(V, None, Choice) .. GENERATED FROM PYTHON SOURCE LINES 171-172 We integrate over omega using numerical integration .. GENERATED FROM PYTHON SOURCE LINES 172-174 .. code-block:: Python loglike = log(MonteCarlo(condprob)) .. GENERATED FROM PYTHON SOURCE LINES 175-176 Read the data .. GENERATED FROM PYTHON SOURCE LINES 176-178 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 179-182 As the objective is to illustrate the syntax, we calculate the Monte-Carlo approximation with a small number of draws. Therefore, we first define the parameters. .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: Python the_biogeme = bio.BIOGEME(database, loglike, number_of_draws=100, seed=1223) the_biogeme.modelName = 'b04latent_choice_seq_mc' .. 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-197 .. 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}') results.write_latex() print(f'LaTeX file: {results.data.latexFileName}') .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated betas: 11 Final log likelihood: -1103.506 Output file: b04latent_choice_seq_mc~01.html Results saved in file b04latent_choice_seq_mc.tex LaTeX file: b04latent_choice_seq_mc.tex .. GENERATED FROM PYTHON SOURCE LINES 198-199 .. code-block:: Python results.get_estimated_parameters() .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.860275 0.113300 7.592904 3.130829e-14
ASC_SM 1.937109 0.231571 8.365062 0.000000e+00
BETA_COST_HWH -1.720510 0.376961 -4.564162 5.014930e-06
BETA_COST_OTHER -0.933392 0.231425 -4.033233 5.501473e-05
BETA_DIST -5.457147 0.635376 -8.588845 0.000000e+00
BETA_TIME_CAR_CL -0.133062 0.007496 -17.751639 0.000000e+00
BETA_TIME_CAR_REF -11.890599 1.724086 -6.896754 5.320411e-12
BETA_TIME_PT_CL -0.144913 0.004588 -31.586011 0.000000e+00
BETA_TIME_PT_REF -2.924784 0.566019 -5.167286 2.375175e-07
BETA_WAITING_TIME -0.040270 0.012112 -3.324803 8.848109e-04
sigma_s 11.679959 0.603737 19.346106 0.000000e+00


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.654 seconds) .. _sphx_glr_download_auto_examples_latent_plot_b04latent_choice_seq_mc.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_mc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b04latent_choice_seq_mc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b04latent_choice_seq_mc.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_