.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/latent/plot_b01one_latent_regression.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_b01one_latent_regression.py: Measurement equations: continuous indicators ============================================ It is actually a simle linear regression. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 16:42:02 2023 .. GENERATED FROM PYTHON SOURCE LINES 11-40 .. code-block:: default import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme.models import piecewiseFormula import biogeme.loglikelihood as ll from biogeme.expressions import Beta, Elem, bioMultSum from optima import ( database, age_65_more, ScaledIncome, moreThanOneCar, moreThanOneBike, individualHouse, male, haveChildren, haveGA, highEducation, Envir01, Envir02, Envir03, Mobil11, Mobil14, Mobil16, Mobil17, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b01one_latent_regression.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b01one_latent_regression.py .. GENERATED FROM PYTHON SOURCE LINES 41-42 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 42-55 .. code-block:: default coef_intercept = Beta('coef_intercept', 0.0, None, None, 0) 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) thresholds = [None, 4, 6, 8, 10, None] formula_income = piecewiseFormula(variable=ScaledIncome, thresholds=thresholds) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Latent variable: structural equation. .. GENERATED FROM PYTHON SOURCE LINES 57-71 .. code-block:: default 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 ) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Measurement equations. .. GENERATED FROM PYTHON SOURCE LINES 75-76 Intercepts. .. GENERATED FROM PYTHON SOURCE LINES 76-84 .. code-block:: default INTER_Envir01 = Beta('INTER_Envir01', 0, None, None, 1) INTER_Envir02 = Beta('INTER_Envir02', 0, None, None, 0) INTER_Envir03 = Beta('INTER_Envir03', 0, None, None, 0) INTER_Mobil11 = Beta('INTER_Mobil11', 0, None, None, 0) INTER_Mobil14 = Beta('INTER_Mobil14', 0, None, None, 0) INTER_Mobil16 = Beta('INTER_Mobil16', 0, None, None, 0) INTER_Mobil17 = Beta('INTER_Mobil17', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Coefficients. .. GENERATED FROM PYTHON SOURCE LINES 86-94 .. code-block:: default B_Envir01_F1 = Beta('B_Envir01_F1', -1, None, None, 1) B_Envir02_F1 = Beta('B_Envir02_F1', -1, None, None, 0) B_Envir03_F1 = Beta('B_Envir03_F1', 1, None, None, 0) B_Mobil11_F1 = Beta('B_Mobil11_F1', 1, None, None, 0) B_Mobil14_F1 = Beta('B_Mobil14_F1', 1, None, None, 0) B_Mobil16_F1 = Beta('B_Mobil16_F1', 1, None, None, 0) B_Mobil17_F1 = Beta('B_Mobil17_F1', 1, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Linear models. .. GENERATED FROM PYTHON SOURCE LINES 96-104 .. code-block:: default MODEL_Envir01 = INTER_Envir01 + B_Envir01_F1 * CARLOVERS MODEL_Envir02 = INTER_Envir02 + B_Envir02_F1 * CARLOVERS MODEL_Envir03 = INTER_Envir03 + B_Envir03_F1 * CARLOVERS MODEL_Mobil11 = INTER_Mobil11 + B_Mobil11_F1 * CARLOVERS MODEL_Mobil14 = INTER_Mobil14 + B_Mobil14_F1 * CARLOVERS MODEL_Mobil16 = INTER_Mobil16 + B_Mobil16_F1 * CARLOVERS MODEL_Mobil17 = INTER_Mobil17 + B_Mobil17_F1 * CARLOVERS .. GENERATED FROM PYTHON SOURCE LINES 105-106 Scale parameters. .. GENERATED FROM PYTHON SOURCE LINES 106-114 .. code-block:: default SIGMA_STAR_Envir01 = Beta('SIGMA_STAR_Envir01', 1, None, None, 0) SIGMA_STAR_Envir02 = Beta('SIGMA_STAR_Envir02', 1, None, None, 0) SIGMA_STAR_Envir03 = Beta('SIGMA_STAR_Envir03', 1, None, None, 0) SIGMA_STAR_Mobil11 = Beta('SIGMA_STAR_Mobil11', 1, None, None, 0) SIGMA_STAR_Mobil14 = Beta('SIGMA_STAR_Mobil14', 1, None, None, 0) SIGMA_STAR_Mobil16 = Beta('SIGMA_STAR_Mobil16', 1, None, None, 0) SIGMA_STAR_Mobil17 = Beta('SIGMA_STAR_Mobil17', 1, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 115-117 We build a dict with each contribution to the loglikelihood if (var > 0) and (var < 6). If not, 0 is returned. .. GENERATED FROM PYTHON SOURCE LINES 117-168 .. code-block:: default F = {} F['Envir01'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Envir01, MODEL_Envir01, SIGMA_STAR_Envir01), }, (Envir01 > 0) * (Envir01 < 6), ) F['Envir02'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Envir02, MODEL_Envir02, SIGMA_STAR_Envir02), }, (Envir02 > 0) * (Envir02 < 6), ) F['Envir03'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Envir03, MODEL_Envir03, SIGMA_STAR_Envir03), }, (Envir03 > 0) * (Envir03 < 6), ) F['Mobil11'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Mobil11, MODEL_Mobil11, SIGMA_STAR_Mobil11), }, (Mobil11 > 0) * (Mobil11 < 6), ) F['Mobil14'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Mobil14, MODEL_Mobil14, SIGMA_STAR_Mobil14), }, (Mobil14 > 0) * (Mobil14 < 6), ) F['Mobil16'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Mobil16, MODEL_Mobil16, SIGMA_STAR_Mobil16), }, (Mobil16 > 0) * (Mobil16 < 6), ) F['Mobil17'] = Elem( { 0: 0, 1: ll.loglikelihoodregression(Mobil17, MODEL_Mobil17, SIGMA_STAR_Mobil17), }, (Mobil17 > 0) * (Mobil17 < 6), ) .. GENERATED FROM PYTHON SOURCE LINES 169-170 The log likelihood is the sum of the elements of the above dict .. GENERATED FROM PYTHON SOURCE LINES 170-172 .. code-block:: default loglike = bioMultSum(F) .. GENERATED FROM PYTHON SOURCE LINES 173-174 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 174-177 .. code-block:: default the_biogeme = bio.BIOGEME(database, loglike) the_biogeme.modelName = 'b01one_latent_regression' .. rst-class:: sphx-glr-script-out .. code-block:: none File biogeme.toml has been created .. GENERATED FROM PYTHON SOURCE LINES 178-179 Estimate the parameters .. GENERATED FROM PYTHON SOURCE LINES 179-181 .. code-block:: default results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b01one_latent_regression.iter Cannot read file __b01one_latent_regression.iter. Statement is ignored. Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds Iter. Function Relgrad Radius Rho 0 3.4e+04 0.3 10 0.91 ++ 1 3.4e+04 0.3 5 -3.7e+03 - 2 3.4e+04 0.3 2.5 -1.9e+03 - 3 3.4e+04 0.3 1.2 -51 - 4 3.4e+04 0.3 0.62 -1.6 - 5 2.6e+04 0.15 6.2 0.95 ++ 6 2.6e+04 0.15 3.1 -1.1e+02 - 7 2.6e+04 0.15 1.6 -3.7e+05 - 8 2.6e+04 0.15 0.78 -0.19 - 9 2.3e+04 0.13 7.8 1 ++ 10 2.3e+04 0.13 3.9 -1.9e+02 - 11 2.3e+04 0.13 2 -8.4 - 12 2.3e+04 0.13 0.98 -6.5 - 13 2.1e+04 0.088 0.98 0.89 + 14 2.1e+04 0.017 0.98 0.57 + 15 2.1e+04 0.017 0.49 -4 - 16 2.1e+04 0.14 0.49 0.61 + 17 2.1e+04 0.0076 4.9 0.95 ++ 18 2.1e+04 0.0076 2.4 -3.1e+02 - 19 2.1e+04 0.0076 1.2 -40 - 20 2.1e+04 0.0076 0.61 -3.5 - 21 2.1e+04 0.13 0.61 0.2 + 22 2e+04 0.017 6.1 1.2 ++ 23 2e+04 0.017 3.1 -35 - 24 2e+04 0.017 1.5 -6.9 - 25 2e+04 0.11 1.5 0.28 + 26 2e+04 0.15 1.5 0.85 + 27 2e+04 0.22 15 1 ++ 28 2e+04 0.22 7.6 -2.6e+02 - 29 2e+04 0.22 3.8 -16 - 30 2e+04 0.22 1.9 -1.9 - 31 1.9e+04 0.17 1.9 0.67 + 32 1.9e+04 0.17 0.95 -10 - 33 1.9e+04 0.17 0.48 -0.47 - 34 1.9e+04 0.072 0.48 0.78 + 35 1.9e+04 0.13 0.48 0.85 + 36 1.9e+04 0.0094 4.8 1 ++ 37 1.9e+04 0.0094 2.4 -4.1 - 38 1.9e+04 0.0094 1.2 -0.02 - 39 1.9e+04 0.0046 12 1.1 ++ 40 1.9e+04 0.0046 6 -53 - 41 1.9e+04 0.0046 3 -8 - 42 1.9e+04 0.0046 1.5 -0.77 - 43 1.9e+04 0.013 1.5 0.75 + 44 1.9e+04 0.0047 1.5 0.52 + 45 1.9e+04 0.0065 1.5 0.58 + 46 1.9e+04 0.0011 1.5 0.22 + 47 1.9e+04 0.0012 1.5 0.13 + 48 1.9e+04 0.0012 0.51 0.00076 - 49 1.9e+04 0.00062 5.1 0.91 ++ 50 1.9e+04 9.1e-05 51 0.95 ++ 51 1.9e+04 9e-07 51 1 ++ .. GENERATED FROM PYTHON SOURCE LINES 182-188 .. code-block:: default 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.writeLaTeX() print(f'LaTeX file: {results.data.latexFileName}') .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated betas: 33 final log likelihood: -18658.152 Output file: None Results saved in file b01one_latent_regression.tex LaTeX file: b01one_latent_regression.tex .. GENERATED FROM PYTHON SOURCE LINES 189-190 .. code-block:: default results.getEstimatedParameters() .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
B_Envir02_F1 -0.496160 0.057780 -8.587026 0.000000e+00
B_Envir03_F1 0.670919 0.060137 11.156562 0.000000e+00
B_Mobil11_F1 0.562862 0.058911 9.554380 0.000000e+00
B_Mobil14_F1 0.705344 0.059649 11.824941 0.000000e+00
B_Mobil16_F1 0.539936 0.061248 8.815632 0.000000e+00
B_Mobil17_F1 0.431749 0.059963 7.200288 6.008527e-13
INTER_Envir02 2.008185 0.153261 13.103062 0.000000e+00
INTER_Envir03 4.565455 0.158304 28.839845 0.000000e+00
INTER_Mobil11 5.143851 0.151171 34.026714 0.000000e+00
INTER_Mobil14 4.914890 0.157521 31.201469 0.000000e+00
INTER_Mobil16 4.797498 0.158486 30.270822 0.000000e+00
INTER_Mobil17 4.500423 0.157149 28.637908 0.000000e+00
SIGMA_STAR_Envir01 1.248310 0.016139 77.345225 0.000000e+00
SIGMA_STAR_Envir02 1.120135 0.014927 75.043199 0.000000e+00
SIGMA_STAR_Envir03 1.069487 0.015518 68.919604 0.000000e+00
SIGMA_STAR_Mobil11 1.082910 0.016309 66.398524 0.000000e+00
SIGMA_STAR_Mobil14 1.050687 0.014081 74.618994 0.000000e+00
SIGMA_STAR_Mobil16 1.096088 0.015107 72.555763 0.000000e+00
SIGMA_STAR_Mobil17 1.109030 0.015460 71.737716 0.000000e+00
beta_ScaledIncome_10_inf 0.102971 0.036009 2.859596 4.241810e-03
beta_ScaledIncome_4_6 -0.252093 0.108158 -2.330778 1.976509e-02
beta_ScaledIncome_6_8 0.299881 0.129823 2.309914 2.089290e-02
beta_ScaledIncome_8_10 -0.620844 0.150375 -4.128652 3.648969e-05
beta_ScaledIncome_minus_inf_4 0.103050 0.063320 1.627460 1.036394e-01
coef_age_65_more 0.103132 0.073161 1.409656 1.586413e-01
coef_haveChildren -0.045353 0.054154 -0.837491 4.023168e-01
coef_haveGA -0.688798 0.086095 -8.000392 1.332268e-15
coef_highEducation -0.298037 0.061203 -4.869668 1.117861e-06
coef_individualHouse -0.109935 0.053967 -2.037088 4.164118e-02
coef_intercept -2.497252 0.182810 -13.660364 0.000000e+00
coef_male 0.071602 0.050626 1.414346 1.572603e-01
coef_moreThanOneBike -0.327629 0.062047 -5.280314 1.289627e-07
coef_moreThanOneCar 0.624407 0.058136 10.740379 0.000000e+00


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.524 seconds) .. _sphx_glr_download_auto_examples_latent_plot_b01one_latent_regression.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_b01one_latent_regression.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01one_latent_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_