Note
Go to the end to download the full example code
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
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 = ['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))
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.85473919, 0.51318999, 0.5561585 , 1.36406925])
f, g, h, gdiff, hdiff = tools.checkDerivatives(G, y, names=None, logg=True)
f
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
pd.DataFrame(g)
pd.DataFrame(h)
pd.DataFrame(gdiff)
pd.DataFrame(hdiff)
We do the same for the CDF.
xi = np.random.uniform(low=-10, high=10, size=4)
xi
array([ 9.9789834 , 1.02172755, 2.53060465, -6.92151866])
F = cnl.cnl_CDF(choice_set, nests)
f, g, h, gdiff, hdiff = tools.checkDerivatives(F, y, names=None, logg=True)
f
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
pd.DataFrame(g)
pd.DataFrame(h)
pd.DataFrame(gdiff)
pd.DataFrame(hdiff)
Total running time of the script: (0 minutes 0.009 seconds)