.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/latent/plot_b03choice_only.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_b03choice_only.py: Mixture of logit ================ Choice model with latent variable. No measurement equation for the indicators. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 16:58:21 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-53 .. code-block:: Python import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme import models import biogeme.distributions as dist from biogeme.expressions import ( Beta, RandomVariable, Integrate, exp, log, ) from read_or_estimate import read_or_estimate from biogeme.data.optima import ( read_data, age_65_more, ScaledIncome, moreThanOneCar, moreThanOneBike, individualHouse, male, haveChildren, haveGA, highEducation, 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 b03choice_only.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b03choice_only.py .. GENERATED FROM PYTHON SOURCE LINES 54-55 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 55-65 .. code-block:: Python coef_intercept = Beta('coef_intercept', 0.0, None, None, 1) coef_age_65_more = Beta('coef_age_65_more', 0.0, None, None, 0) coef_haveGA = Beta('coef_haveGA', 0.0, None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', 0.0, None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', 0.0, None, None, 0) coef_individualHouse = Beta('coef_individualHouse', 0.0, None, None, 0) coef_male = Beta('coef_male', 0.0, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', 0.0, None, None, 0) coef_highEducation = Beta('coef_highEducation', 0.0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Latent variable: structural equation. .. GENERATED FROM PYTHON SOURCE LINES 69-72 Define a random parameter, normally distributed, designed to be used for numerical integration. .. GENERATED FROM PYTHON SOURCE LINES 72-93 .. code-block:: Python omega = RandomVariable('omega') density = dist.normalpdf(omega) sigma_s = Beta('sigma_s', 1, -1, 1, 0) thresholds = [None, 4, 6, 8, 10, None] formula_income = models.piecewise_formula(variable=ScaledIncome, thresholds=thresholds) 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 94-95 Choice model: parameters. .. GENERATED FROM PYTHON SOURCE LINES 95-107 .. code-block:: Python ASC_CAR = Beta('ASC_CAR', 0.0, None, None, 0) ASC_PT = Beta('ASC_PT', 0.0, None, None, 1) ASC_SM = Beta('ASC_SM', 0.0, None, None, 0) BETA_COST_HWH = Beta('BETA_COST_HWH', 0.0, None, None, 0) BETA_COST_OTHER = Beta('BETA_COST_OTHER', 0.0, None, None, 0) BETA_DIST = Beta('BETA_DIST', 0.0, None, None, 0) BETA_TIME_CAR_REF = Beta('BETA_TIME_CAR_REF', -0.0001, None, 0, 0) BETA_TIME_CAR_CL = Beta('BETA_TIME_CAR_CL', -1.0, -3, 3, 0) BETA_TIME_PT_REF = Beta('BETA_TIME_PT_REF', -0.0001, None, 0, 0) BETA_TIME_PT_CL = Beta('BETA_TIME_PT_CL', -1.0, -3, 3, 0) BETA_WAITING_TIME = Beta('BETA_WAITING_TIME', 0.0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Definition of utility functions. .. GENERATED FROM PYTHON SOURCE LINES 109-130 .. 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 131-132 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 132-134 .. code-block:: Python V = {0: V0, 1: V1, 2: V2} .. GENERATED FROM PYTHON SOURCE LINES 135-136 Conditional on omega, we have a logit model (called the kernel). .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python condprob = models.logit(V, None, Choice) .. GENERATED FROM PYTHON SOURCE LINES 139-140 We integrate over omega using numerical integration. .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python loglike = log(Integrate(condprob * density, 'omega')) .. GENERATED FROM PYTHON SOURCE LINES 143-144 Read the data .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 147-148 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 148-151 .. code-block:: Python the_biogeme = bio.BIOGEME(database, loglike) the_biogeme.modelName = 'b03choice_only' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 152-154 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 154-156 .. code-block:: Python results = read_or_estimate(the_biogeme=the_biogeme, directory='saved_results') .. GENERATED FROM PYTHON SOURCE LINES 157-161 .. 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: 24 Final log likelihood: -1068.884 Output file: b03choice_only.html .. GENERATED FROM PYTHON SOURCE LINES 162-163 .. code-block:: Python results.get_estimated_parameters() .. raw:: html
Value Active bound Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.931176 0.0 0.149727 6.219169 4.997935e-10
ASC_SM 2.014524 0.0 0.294670 6.836552 8.112178e-12
BETA_COST_HWH -1.766405 0.0 0.198007 -8.920916 0.000000e+00
BETA_COST_OTHER -0.828576 0.0 0.132385 -6.258836 3.878613e-10
BETA_DIST -6.174887 0.0 0.861775 -7.165311 7.760459e-13
BETA_TIME_CAR_CL -1.480684 0.0 2.973269 -0.497999 6.184849e-01
BETA_TIME_CAR_REF -15.285957 0.0 5.001630 -3.056195 2.241655e-03
BETA_TIME_PT_CL -1.069380 0.0 2.160096 -0.495061 6.205570e-01
BETA_TIME_PT_REF -5.426451 0.0 1.538572 -3.526941 4.203910e-04
BETA_WAITING_TIME -0.039497 0.0 0.009740 -4.054926 5.015009e-05
beta_ScaledIncome_10_inf 0.033239 0.0 0.109399 0.303836 7.612531e-01
beta_ScaledIncome_4_6 0.087575 0.0 0.281559 0.311036 7.557735e-01
beta_ScaledIncome_6_8 -0.178503 0.0 0.436064 -0.409350 6.822829e-01
beta_ScaledIncome_8_10 -0.094509 0.0 0.413156 -0.228749 8.190639e-01
beta_ScaledIncome_minus_inf_4 0.058319 0.0 0.191729 0.304173 7.609964e-01
coef_age_65_more -0.037623 0.0 0.177962 -0.211412 8.325657e-01
coef_haveChildren -0.021627 0.0 0.122668 -0.176307 8.600530e-01
coef_haveGA -0.824816 0.0 1.679053 -0.491239 6.232574e-01
coef_highEducation -0.017555 0.0 0.124911 -0.140543 8.882307e-01
coef_individualHouse -0.123526 0.0 0.275458 -0.448441 6.538351e-01
coef_male 0.155699 0.0 0.328803 0.473532 6.358334e-01
coef_moreThanOneBike -0.402377 0.0 0.820967 -0.490126 6.240447e-01
coef_moreThanOneCar 1.120108 0.0 2.255210 0.496676 6.194179e-01
sigma_s 1.000000 1.0 2.007941 0.498023 6.184681e-01


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