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
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)
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([-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)
pd.DataFrame(h)
pd.DataFrame(gdiff)
pd.DataFrame(hdiff)
Total running time of the script: (0 minutes 0.021 seconds)