.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_cnl.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_programmers_plot_cnl.py: biogeme.cnl =========== Example of usage of the cnl module. This is for programmers who need examples of use of the functions of the class. The examples are designed to illustrate the syntax. :author: Michel Bierlaire :date: Fri Nov 17 08:27:24 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-25 .. code-block:: default import numpy as np import pandas as pd import biogeme.cnl as cnl import biogeme.tools as tools import biogeme.biogeme_logging as blog from biogeme.nests import OneNestForCrossNestedLogit, NestsForCrossNestedLogit logger = blog.get_screen_logger(level=blog.INFO) logger.info('Logging on') .. rst-class:: sphx-glr-script-out .. code-block:: none Logging on .. GENERATED FROM PYTHON SOURCE LINES 26-27 Definition of the nests. .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: default choice_set = ['i', 'j', 'k', 'ell'] mu_nest_1 = 1.4 alphas_1 = {'i': 1, 'j': 0.5, 'k': 0.2} nest_1 = OneNestForCrossNestedLogit( nest_param=mu_nest_1, dict_of_alpha=alphas_1, name='Nest 1' ) mu_nest_2 = 1.2 alphas_2 = {'j': 0.5, 'k': 0.8, 'ell': 1} nest_2 = OneNestForCrossNestedLogit( nest_param=mu_nest_2, dict_of_alpha=alphas_2, name='Nest 2' ) nests = NestsForCrossNestedLogit(choice_set=choice_set, tuple_of_nests=(nest_1, nest_2)) .. GENERATED FROM PYTHON SOURCE LINES 41-43 We retrieve the G function of the cross-nested logit, and verify numerically the implementation of the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: default G = cnl.cnl_G(choice_set, nests) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Draw a random point where to evaluate the function. .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: default y = np.random.uniform(low=0.01, high=2, size=4) y .. rst-class:: sphx-glr-script-out .. code-block:: none array([1.85473919, 0.51318999, 0.5561585 , 1.36406925]) .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default f, g, h, gdiff, hdiff = tools.checkDerivatives(G, y, names=None, logg=True) f .. rst-class:: sphx-glr-script-out .. code-block:: none x Gradient FinDiff Difference x[0] +9.776899E-01 +9.776899E-01 -5.542010E-10 x[1] +5.602327E-01 +5.602327E-01 -1.488811E-08 x[2] +6.683210E-01 +6.683210E-01 -1.352010E-08 x[3] +9.459859E-01 +9.459859E-01 -2.156753E-09 Row Col Hessian FinDiff Difference x[0] x[0] +1.601040E-02 +1.601040E-02 +2.188207E-09 x[0] x[1] -4.416095E-02 -4.416095E-02 +1.228835E-09 x[0] x[2] -1.264418E-02 -1.264418E-02 +8.741659E-10 x[0] x[3] +0.000000E+00 +0.000000E+00 +0.000000E+00 x[1] x[0] -4.416095E-02 -4.416095E-02 -2.127112E-09 x[1] x[1] +2.819459E-01 +2.819459E-01 +2.296991E-08 x[1] x[2] -2.561778E-02 -2.561778E-02 +1.098153E-09 x[1] x[3] -3.558268E-02 -3.558268E-02 -1.176896E-09 x[2] x[0] -1.264418E-02 -1.264418E-02 -6.444130E-10 x[2] x[1] -2.561778E-02 -2.561778E-02 -1.207005E-11 x[2] x[2] +2.216909E-01 +2.216909E-01 +2.104954E-08 x[2] x[3] -6.355749E-02 -6.355749E-02 -2.485184E-09 x[3] x[0] +0.000000E+00 +0.000000E+00 +0.000000E+00 x[3] x[1] -3.558268E-02 -3.558268E-02 +8.337737E-10 x[3] x[2] -6.355749E-02 -6.355749E-02 +5.604779E-10 x[3] x[3] +3.930058E-02 +3.930058E-02 +3.785419E-09 3.7629483742533636 .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: default pd.DataFrame(g) .. raw:: html
0
0 0.977690
1 0.560233
2 0.668321
3 0.945986


.. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: default pd.DataFrame(h) .. raw:: html
0 1 2 3
0 0.016010 -0.044161 -0.012644 0.000000
1 -0.044161 0.281946 -0.025618 -0.035583
2 -0.012644 -0.025618 0.221691 -0.063557
3 0.000000 -0.035583 -0.063557 0.039301


.. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: default pd.DataFrame(gdiff) .. raw:: html
0
0 -5.542010e-10
1 -1.488811e-08
2 -1.352010e-08
3 -2.156753e-09


.. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default pd.DataFrame(hdiff) .. raw:: html
0 1 2 3
0 2.188207e-09 1.228835e-09 8.741659e-10 0.000000e+00
1 -2.127112e-09 2.296991e-08 1.098153e-09 -1.176896e-09
2 -6.444130e-10 -1.207005e-11 2.104954e-08 -2.485184e-09
3 0.000000e+00 8.337737e-10 5.604779e-10 3.785419e-09


.. GENERATED FROM PYTHON SOURCE LINES 70-71 We do the same for the CDF. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: default xi = np.random.uniform(low=-10, high=10, size=4) xi .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 9.9789834 , 1.02172755, 2.53060465, -6.92151866]) .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: default F = cnl.cnl_CDF(choice_set, nests) .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default f, g, h, gdiff, hdiff = tools.checkDerivatives(F, y, names=None, logg=True) f .. rst-class:: sphx-glr-script-out .. code-block:: none x Gradient FinDiff Difference x[0] +2.901734E-02 +2.901734E-02 +3.696049E-09 x[1] +1.394584E-01 +1.394584E-01 +5.265964E-09 x[2] +1.315120E-01 +1.315120E-01 +4.664963E-09 x[3] +5.588572E-02 +5.588572E-02 +3.847727E-09 Row Col Hessian FinDiff Difference x[0] x[0] -3.478215E-02 -3.478215E-02 -3.157892E-09 x[0] x[1] +2.153048E-02 +2.153048E-02 +2.995897E-10 x[0] x[2] +1.554827E-02 +1.554827E-02 +5.218916E-10 x[0] x[3] +5.833581E-03 +5.833581E-03 +4.022392E-10 x[1] x[0] +2.153048E-02 +2.153048E-02 +2.265892E-09 x[1] x[1] -9.062555E-02 -9.062555E-02 +2.166100E-09 x[1] x[2] +7.693634E-02 +7.693634E-02 +1.922201E-09 x[1] x[3] +3.123268E-02 +3.123268E-02 +1.911724E-09 x[2] x[0] +1.554827E-02 +1.554827E-02 +1.884139E-09 x[2] x[1] +7.693634E-02 +7.693634E-02 +2.199757E-09 x[2] x[2] -8.741174E-02 -8.741174E-02 +1.227572E-09 x[2] x[3] +3.177463E-02 +3.177462E-02 +1.951594E-09 x[3] x[0] +5.833581E-03 +5.833580E-03 +7.555838E-10 x[3] x[1] +3.123268E-02 +3.123268E-02 +8.846930E-10 x[3] x[2] +3.177463E-02 +3.177463E-02 +7.653369E-10 x[3] x[3] -5.318263E-02 -5.318262E-02 -2.425383E-09 0.27798626196217313 .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: default pd.DataFrame(g) .. raw:: html
0
0 0.029017
1 0.139458
2 0.131512
3 0.055886


.. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default pd.DataFrame(h) .. raw:: html
0 1 2 3
0 -0.034782 0.021530 0.015548 0.005834
1 0.021530 -0.090626 0.076936 0.031233
2 0.015548 0.076936 -0.087412 0.031775
3 0.005834 0.031233 0.031775 -0.053183


.. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: default pd.DataFrame(gdiff) .. raw:: html
0
0 3.696049e-09
1 5.265964e-09
2 4.664963e-09
3 3.847727e-09


.. GENERATED FROM PYTHON SOURCE LINES 93-94 .. code-block:: default pd.DataFrame(hdiff) .. raw:: html
0 1 2 3
0 -3.157892e-09 2.995897e-10 5.218916e-10 4.022392e-10
1 2.265892e-09 2.166100e-09 1.922201e-09 1.911724e-09
2 1.884139e-09 2.199757e-09 1.227572e-09 1.951594e-09
3 7.555838e-10 8.846930e-10 7.653369e-10 -2.425383e-09


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