biogeme.nests

Examples of use of several functions.

This is designed for programmers who need examples of use of the functions of the module. The examples are designed to illustrate the syntax. They do not correspond to any meaningful model.

author:

Michel Bierlaire

date:

Wed Nov 29 18:35:06 2023

import numpy as np
from biogeme.version import getText
from biogeme.nests import (
    OneNestForNestedLogit,
    OneNestForCrossNestedLogit,
    NestsForCrossNestedLogit,
    NestsForNestedLogit,
)

Version of Biogeme.

print(getText())
biogeme 3.2.13 [2023-12-23]
Home page: http://biogeme.epfl.ch
Submit questions to https://groups.google.com/d/forum/biogeme
Michel Bierlaire, Transport and Mobility Laboratory, Ecole Polytechnique Fédérale de Lausanne (EPFL)

Covariance and correlation between two alternatives of a cross-nested logit model. Here, we test a logit model by setting the nest parameters to 1.0. We expect the identify matrix as correlation.

choice_set = ['i', 'j', 'k', 'm']
mu_nest_1 = 1.0
alphas_1 = {'i': 1, 'j': 1}
nest_1 = OneNestForCrossNestedLogit(
    nest_param=mu_nest_1, dict_of_alpha=alphas_1, name='Nest 1'
)
mu_nest_2 = 1.0
alphas_2 = {'j': 0.0, 'k': 1, 'm': 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))
nests.correlation()
i j k m
i 1.000000e+00 8.237105e-12 8.237105e-12 8.237105e-12
j 8.237105e-12 1.000000e+00 8.237105e-12 8.237105e-12
k 8.237105e-12 8.237105e-12 1.000000e+00 8.237105e-12
m 8.237105e-12 8.237105e-12 8.237105e-12 1.000000e+00


Entries of the covariance matrix can also be obtained. Here, we report the variance for alternative i.

nests.covariance('i', 'i')
1.6449340668482264

It is \(\pi^2/6\).

np.pi**2 / 6
1.6449340668482264

Second, a nested logit model

mu_nest_1 = 1.5
alphas_1 = {'i': 1, 'j': 1}
nest_1 = OneNestForNestedLogit(
    nest_param=mu_nest_1, list_of_alternatives=['i', 'j'], name='Nest 1'
)
mu_nest_2 = 2.0
alphas_2 = {'k': 1, 'm': 1}
nest_2 = OneNestForNestedLogit(
    nest_param=mu_nest_2, list_of_alternatives=['k', 'm'], name='Nest 2'
)
nests = NestsForNestedLogit(choice_set=choice_set, tuple_of_nests=(nest_1, nest_2))
nests.correlation()
i j k m
i 1.000000 0.555556 0.00 0.00
j 0.555556 1.000000 0.00 0.00
k 0.000000 0.000000 1.00 0.75
m 0.000000 0.000000 0.75 1.00


Theoretical value for the correlation

correl_nest_1 = 1 - (1 / mu_nest_1**2)
correl_nest_1
0.5555555555555556
correl_nest_2 = 1 - (1 / mu_nest_2**2)
correl_nest_2
0.75

The same nested logit model, coded as a cross-nested logit

mu_nest_1 = 1.5
alphas_1 = {'i': 1, 'j': 1}
nest_1 = OneNestForCrossNestedLogit(
    nest_param=mu_nest_1, dict_of_alpha=alphas_1, name='Nest 1'
)
mu_nest_2 = 2.0
alphas_2 = {'j': 0.0, 'k': 1, 'm': 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))
nests.correlation()
/Users/bierlair/venv312/lib/python3.12/site-packages/scipy/integrate/_quadpack_py.py:1233: IntegrationWarning: The integral is probably divergent, or slowly convergent.
  quad_r = quad(f, low, high, args=args, full_output=self.full_output,
/Users/bierlair/venv312/lib/python3.12/site-packages/scipy/integrate/_quadpack_py.py:1233: IntegrationWarning: The algorithm does not converge.  Roundoff error is detected
  in the extrapolation table.  It is assumed that the requested tolerance
  cannot be achieved, and that the returned result (if full_output = 1) is
  the best which can be obtained.
  quad_r = quad(f, low, high, args=args, full_output=self.full_output,
i j k m
i 1.000000e+00 5.555556e-01 8.248444e-12 8.248444e-12
j 5.555556e-01 1.000000e+00 8.248444e-12 8.248444e-12
k 8.248444e-12 8.248444e-12 1.000000e+00 7.500000e-01
m 8.248444e-12 8.248444e-12 7.500000e-01 1.000000e+00


Finally, a cross-nested logit model, where alternative j is correlated with all the other alternatives, and belong to two different nests.

mu_nest_1 = 1.5
alphas_1 = {'i': 1, 'j': 0.5}
nest_1 = OneNestForCrossNestedLogit(
    nest_param=mu_nest_1, dict_of_alpha=alphas_1, name='Nest 1'
)
mu_nest_2 = 2.0
alphas_2 = {'j': 0.5, 'k': 1, 'm': 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))
nests.correlation()
i j k m
i 1.000000e+00 0.37618 8.248444e-12 8.248444e-12
j 3.761799e-01 1.00000 5.000000e-01 5.000000e-01
k 8.248444e-12 0.50000 1.000000e+00 7.500000e-01
m 8.248444e-12 0.50000 7.500000e-01 1.000000e+00


Total running time of the script: (1 minutes 37.085 seconds)

Gallery generated by Sphinx-Gallery