.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/latentbis/plot_m02_sequential_estimation.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_latentbis_plot_m02_sequential_estimation.py: Choice model with latent variable: sequential estimation ======================================================== Mixture of logit. Measurement equation for the indicators. Sequential estimation. :author: Michel Bierlaire, EPFL :date: Fri Apr 14 09:47:53 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-55 .. code-block:: default import sys import biogeme.biogeme_logging as blog import biogeme.exceptions as excep import biogeme.biogeme as bio import biogeme.distributions as dist import biogeme.results as res from biogeme import models from biogeme.expressions import ( Beta, RandomVariable, exp, log, Integrate, ) from read_or_estimate import read_or_estimate from optima import ( database, male, age, haveChildren, highEducation, childCenter, childSuburb, SocioProfCat, WaitingTimePT, Choice, TimePT_scaled, TimeCar_scaled, MarginalCostPT_scaled, CostCarCHF_scaled, distance_km_scaled, PurpHWH, PurpOther, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example m02_sequential_estimation.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example m02_sequential_estimation.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:: default MODELNAME = 'm01_latent_variable' try: struct_results = res.bioResults(pickleFile=f'saved_results/{MODELNAME}.pickle') except excep.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.getBetaValues() .. GENERATED FROM PYTHON SOURCE LINES 69-70 Coefficients .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: default coef_intercept = struct_betas['coef_intercept'] coef_age_30_less = struct_betas['coef_age_30_less'] coef_male = struct_betas['coef_male'] coef_haveChildren = struct_betas['coef_haveChildren'] coef_highEducation = struct_betas['coef_highEducation'] coef_artisans = struct_betas['coef_artisans'] coef_employees = struct_betas['coef_employees'] coef_child_center = struct_betas['coef_child_center'] coef_child_suburb = struct_betas['coef_child_suburb'] .. 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:: default omega = RandomVariable('omega') density = dist.normalpdf(omega) sigma_s = Beta('sigma_s', 1, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 91-104 .. code-block:: default ACTIVELIFE = ( coef_intercept + coef_child_center * childCenter + coef_child_suburb * childSuburb + coef_highEducation * highEducation + coef_artisans * (SocioProfCat == 5) + coef_employees * (SocioProfCat == 6) + coef_age_30_less * (age <= 30) + coef_male * male + coef_haveChildren * haveChildren + sigma_s * omega ) .. GENERATED FROM PYTHON SOURCE LINES 105-106 Choice model .. GENERATED FROM PYTHON SOURCE LINES 106-116 .. code-block:: default ASC_CAR = Beta('ASC_CAR', 0.94, None, None, 0) ASC_PT = Beta('ASC_PT', 0, None, None, 1) ASC_SM = Beta('ASC_SM', 0.35, None, None, 0) BETA_COST_HWH = Beta('BETA_COST_HWH', -2.3, None, None, 0) BETA_COST_OTHER = Beta('BETA_COST_OTHER', -1.9, None, None, 0) BETA_DIST = Beta('BETA_DIST', -1.3, None, None, 0) BETA_TIME_CAR_REF = Beta('BETA_TIME_CAR_REF', -6.1, None, 0, 0) BETA_TIME_PT_REF = Beta('BETA_TIME_PT_REF', 0, None, 0, 0) BETA_WAITING_TIME = Beta('BETA_WAITING_TIME', -0.075, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 117-120 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 120-125 .. code-block:: default BETA_TIME_PT_AL = Beta('BETA_TIME_PT_AL', 1.5, None, None, 0) BETA_TIME_PT = BETA_TIME_PT_REF * exp(BETA_TIME_PT_AL * ACTIVELIFE) BETA_TIME_CAR_AL = Beta('BETA_TIME_CAR_AL', -0.11, None, None, 0) BETA_TIME_CAR = BETA_TIME_CAR_REF * exp(BETA_TIME_CAR_AL * ACTIVELIFE) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Definition of utility functions: .. GENERATED FROM PYTHON SOURCE LINES 127-144 .. code-block:: default 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 145-146 Associate utility functions with the numbering of alternatives .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: default V = {0: V0, 1: V1, 2: V2} .. GENERATED FROM PYTHON SOURCE LINES 149-150 Conditional on omega, we have a logit model (called the kernel) .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: default condprob = models.logit(V, None, Choice) .. GENERATED FROM PYTHON SOURCE LINES 153-154 We integrate over omega using numerical integration .. GENERATED FROM PYTHON SOURCE LINES 154-156 .. code-block:: default loglike = log(Integrate(condprob * density, 'omega')) .. GENERATED FROM PYTHON SOURCE LINES 157-158 Create the Biogeme object .. GENERATED FROM PYTHON SOURCE LINES 158-162 .. code-block:: default the_biogeme = bio.BIOGEME(database, loglike) the_biogeme.modelName = 'm02_sequential_estimation' the_biogeme.maxiter = 1000 .. rst-class:: sphx-glr-script-out .. code-block:: none File biogeme.toml has been parsed. .. GENERATED FROM PYTHON SOURCE LINES 163-165 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 165-167 .. code-block:: default results = read_or_estimate(the_biogeme=the_biogeme, directory='saved_results') .. GENERATED FROM PYTHON SOURCE LINES 168-170 .. code-block:: default print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model m02_sequential_estimation Nbr of parameters: 11 Sample size: 1906 Excluded data: 359 Final log likelihood: -1121.273 Akaike Information Criterion: 2264.546 Bayesian Information Criterion: 2325.626 .. GENERATED FROM PYTHON SOURCE LINES 171-174 .. code-block:: default 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 Final log likelihood: -1121.273 Output file: m02_sequential_estimation.html .. GENERATED FROM PYTHON SOURCE LINES 175-176 .. code-block:: default results.getEstimatedParameters() .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.622583 0.116322 5.352241 8.687180e-08
ASC_SM 1.654831 0.225762 7.329973 2.302603e-13
BETA_COST_HWH -1.822974 0.386925 -4.711438 2.459747e-06
BETA_COST_OTHER -0.929877 0.213730 -4.350703 1.357015e-05
BETA_DIST -4.920546 0.532287 -9.244152 0.000000e+00
BETA_TIME_CAR_AL -0.038697 0.022999 -1.682579 9.245669e-02
BETA_TIME_CAR_REF -9.057901 1.304489 -6.943639 3.821166e-12
BETA_TIME_PT_AL -0.036915 0.021627 -1.706888 8.784284e-02
BETA_TIME_PT_REF -3.054210 0.517935 -5.896901 3.703930e-09
BETA_WAITING_TIME -0.027764 0.011142 -2.491826 1.270885e-02
sigma_s 33.539526 20.078869 1.670389 9.484239e-02


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