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

import numpy as np
import pandas as pd
import biogeme.cnl as cnl
import biogeme.tools as tools
import biogeme.biogeme_logging as blog
import biogeme.tools.derivatives
from biogeme.nests import OneNestForCrossNestedLogit, NestsForCrossNestedLogit

logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Logging on')
Logging on

Definition of the nests.

choice_set = [1, 2, 3, 4]
mu_nest_1 = 1.4
alphas_1 = {1: 1, 2: 0.5, 3: 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 = {2: 0.5, 3: 0.8, 4: 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))

We retrieve the G function of the cross-nested logit, and verify numerically the implementation of the derivatives.

G = cnl.cnl_g(choice_set, nests)

Draw a random point where to evaluate the function.

y = np.random.uniform(low=0.01, high=2, size=4)
y
array([1.49769096, 1.49746887, 1.49131898, 1.09772283])
f, g, h, gdiff, hdiff = biogeme.tools.derivatives.check_derivatives(
    G, y, names=None, logg=True
)
f
x               Gradient        FinDiff         Difference
x[0]            +8.934671E-01   +8.934671E-01   -6.680707E-09
x[1]            +7.301560E-01   +7.301560E-01   -5.556037E-09
x[2]            +7.814834E-01   +7.814834E-01   -4.404730E-09
x[3]            +8.455176E-01   +8.455176E-01   -3.989405E-09
Row             Col             Hessian FinDiff         Difference
x[0]            x[0]            +7.774919E-02   +7.774919E-02   +5.896635E-09
x[0]            x[1]            -6.095700E-02   -6.095700E-02   -7.129155E-10
x[0]            x[2]            -1.687301E-02   -1.687301E-02   +5.225261E-12
x[0]            x[3]            +0.000000E+00   +0.000000E+00   +0.000000E+00
x[1]            x[0]            -6.095700E-02   -6.095700E-02   -2.599306E-09
x[1]            x[1]            +1.075622E-01   +1.075622E-01   +6.114073E-09
x[1]            x[2]            -2.759919E-02   -2.759919E-02   +4.535238E-10
x[1]            x[3]            -2.606951E-02   -2.606951E-02   -3.533775E-10
x[2]            x[0]            -1.687301E-02   -1.687301E-02   -1.027236E-09
x[2]            x[1]            -2.759919E-02   -2.759919E-02   -2.908989E-10
x[2]            x[2]            +7.835902E-02   +7.835901E-02   +4.599154E-09
x[2]            x[3]            -4.578460E-02   -4.578460E-02   -4.043081E-10
x[3]            x[0]            +0.000000E+00   +0.000000E+00   +0.000000E+00
x[3]            x[1]            -2.606951E-02   -2.606951E-02   -1.791665E-10
x[3]            x[2]            -4.578460E-02   -4.578460E-02   -9.050472E-10
x[3]            x[3]            +9.776396E-02   +9.776396E-02   +6.530907E-09

np.float64(4.525108507494385)
pd.DataFrame(g)
0
0 0.893467
1 0.730156
2 0.781483
3 0.845518


pd.DataFrame(h)
0 1 2 3
0 0.077749 -0.060957 -0.016873 0.000000
1 -0.060957 0.107562 -0.027599 -0.026070
2 -0.016873 -0.027599 0.078359 -0.045785
3 0.000000 -0.026070 -0.045785 0.097764


pd.DataFrame(gdiff)
0
0 -6.680707e-09
1 -5.556037e-09
2 -4.404730e-09
3 -3.989405e-09


pd.DataFrame(hdiff)
0 1 2 3
0 5.896635e-09 -7.129155e-10 5.225261e-12 0.000000e+00
1 -2.599306e-09 6.114073e-09 4.535238e-10 -3.533775e-10
2 -1.027236e-09 -2.908989e-10 4.599154e-09 -4.043081e-10
3 0.000000e+00 -1.791665e-10 -9.050472e-10 6.530907e-09


We do the same for the CDF.

xi = np.random.uniform(low=-10, high=10, size=4)
xi
array([-5.00497586,  8.22843199, -4.8539593 ,  5.87976015])
F = cnl.cnl_cdf(choice_set, nests)
f, g, h, gdiff, hdiff = biogeme.tools.derivatives.check_derivatives(
    F, y, names=None, logg=True
)
f
x               Gradient        FinDiff         Difference
x[0]            +8.731002E-02   +8.731002E-02   +6.150578E-09
x[1]            +6.889688E-02   +6.889687E-02   +5.218100E-09
x[2]            +7.265077E-02   +7.265076E-02   +5.738748E-09
x[3]            +1.328849E-01   +1.328849E-01   +5.477419E-09
Row             Col             Hessian FinDiff         Difference
x[0]            x[0]            -8.127657E-02   -8.127657E-02   -3.516370E-09
x[0]            x[1]            +2.267640E-02   +2.267640E-02   +1.325450E-09
x[0]            x[2]            +1.700504E-02   +1.700504E-02   +1.253258E-09
x[0]            x[3]            +2.654409E-02   +2.654409E-02   +1.018159E-09
x[1]            x[0]            +2.267640E-02   +2.267640E-02   +7.338953E-10
x[1]            x[1]            -7.395178E-02   -7.395177E-02   -4.783506E-09
x[1]            x[2]            +1.435251E-02   +1.435251E-02   +1.033318E-09
x[1]            x[3]            +2.504601E-02   +2.504601E-02   +8.835944E-10
x[2]            x[0]            +1.700504E-02   +1.700504E-02   +9.820123E-10
x[2]            x[1]            +1.435251E-02   +1.435251E-02   +1.045728E-09
x[2]            x[2]            -7.322866E-02   -7.322866E-02   -4.257790E-09
x[2]            x[3]            +2.934713E-02   +2.934713E-02   +8.319581E-10
x[3]            x[0]            +2.654409E-02   +2.654409E-02   +1.721685E-09
x[3]            x[1]            +2.504601E-02   +2.504601E-02   +1.710186E-09
x[3]            x[2]            +2.934713E-02   +2.934713E-02   +1.775611E-09
x[3]            x[3]            -1.038447E-01   -1.038447E-01   -1.941338E-09

np.float64(0.4370909968119332)
pd.DataFrame(g)
0
0 0.087310
1 0.068897
2 0.072651
3 0.132885


pd.DataFrame(h)
0 1 2 3
0 -0.081277 0.022676 0.017005 0.026544
1 0.022676 -0.073952 0.014353 0.025046
2 0.017005 0.014353 -0.073229 0.029347
3 0.026544 0.025046 0.029347 -0.103845


pd.DataFrame(gdiff)
0
0 6.150578e-09
1 5.218100e-09
2 5.738748e-09
3 5.477419e-09


pd.DataFrame(hdiff)
0 1 2 3
0 -3.516370e-09 1.325450e-09 1.253258e-09 1.018159e-09
1 7.338953e-10 -4.783506e-09 1.033318e-09 8.835944e-10
2 9.820123e-10 1.045728e-09 -4.257790e-09 8.319581e-10
3 1.721685e-09 1.710186e-09 1.775611e-09 -1.941338e-09


Total running time of the script: (0 minutes 0.021 seconds)

Gallery generated by Sphinx-Gallery