Note
Go to the end to download the full example code.
biogeme.segmentation
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:
Thu Dec 7 15:18:45 2023
from biogeme.version import get_text
import biogeme.segmentation as seg
from biogeme.expressions import Variable, Beta
Version of Biogeme.
print(get_text())
biogeme 3.2.14 [2024-08-05]
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)
socio_eco_1 = Variable('socio_eco_1')
segmentation_1 = seg.DiscreteSegmentationTuple(
variable=socio_eco_1,
mapping={
1: 'high',
2: 'medium',
3: 'low',
},
)
socio_eco_2 = Variable('socio_eco_2')
segmentation_2 = seg.DiscreteSegmentationTuple(
variable=socio_eco_2,
mapping={
1: 'level_1',
2: 'level_2',
3: 'level_3',
4: 'level_4',
},
)
beta_x = Beta('beta_x', 0, None, None, 0)
segmented_parameter = seg.Segmentation(beta_x, (segmentation_1,))
The expressions for the segmented parameter is obtained as follows:
segmented_parameter.segmented_beta()
bioMultSum(Beta('beta_x', 0, None, None, 0), (Beta('beta_x_medium', 0, None, None, 0) * (socio_eco_1 == `2.0`)), (Beta('beta_x_low', 0, None, None, 0) * (socio_eco_1 == `3.0`)))
The corresponding Python code can be obtained as follows.
print(segmented_parameter.segmented_code())
beta_x_medium = Beta('beta_x_medium', 0, None, None, 0)
beta_x_low = Beta('beta_x_low', 0, None, None, 0)
segmented_beta_x = bioMultSum([Beta('beta_x', 0, None, None, 0), beta_x_medium * (Variable('socio_eco_1') == 2), beta_x_low * (Variable('socio_eco_1') == 3)])
The code of the original Beta is also available:
print(segmented_parameter.beta_code())
Beta('beta_x', 0, None, None, 0)
Same with the second segmentation
segmented_parameter = seg.Segmentation(beta_x, (segmentation_2,))
segmented_parameter.segmented_beta()
bioMultSum(Beta('beta_x', 0, None, None, 0), (Beta('beta_x_level_2', 0, None, None, 0) * (socio_eco_2 == `2.0`)), (Beta('beta_x_level_3', 0, None, None, 0) * (socio_eco_2 == `3.0`)), (Beta('beta_x_level_4', 0, None, None, 0) * (socio_eco_2 == `4.0`)))
print(segmented_parameter.segmented_code())
beta_x_level_2 = Beta('beta_x_level_2', 0, None, None, 0)
beta_x_level_3 = Beta('beta_x_level_3', 0, None, None, 0)
beta_x_level_4 = Beta('beta_x_level_4', 0, None, None, 0)
segmented_beta_x = bioMultSum([Beta('beta_x', 0, None, None, 0), beta_x_level_2 * (Variable('socio_eco_2') == 2), beta_x_level_3 * (Variable('socio_eco_2') == 3), beta_x_level_4 * (Variable('socio_eco_2') == 4)])
The two segmentations can be combined.
segmented_parameter = seg.Segmentation(
beta_x,
(
segmentation_1,
segmentation_2,
),
)
segmented_parameter.segmented_beta()
bioMultSum(Beta('beta_x', 0, None, None, 0), (Beta('beta_x_medium', 0, None, None, 0) * (socio_eco_1 == `2.0`)), (Beta('beta_x_low', 0, None, None, 0) * (socio_eco_1 == `3.0`)), (Beta('beta_x_level_2', 0, None, None, 0) * (socio_eco_2 == `2.0`)), (Beta('beta_x_level_3', 0, None, None, 0) * (socio_eco_2 == `3.0`)), (Beta('beta_x_level_4', 0, None, None, 0) * (socio_eco_2 == `4.0`)))
print(segmented_parameter.segmented_code())
beta_x_medium = Beta('beta_x_medium', 0, None, None, 0)
beta_x_low = Beta('beta_x_low', 0, None, None, 0)
beta_x_level_2 = Beta('beta_x_level_2', 0, None, None, 0)
beta_x_level_3 = Beta('beta_x_level_3', 0, None, None, 0)
beta_x_level_4 = Beta('beta_x_level_4', 0, None, None, 0)
segmented_beta_x = bioMultSum([Beta('beta_x', 0, None, None, 0), beta_x_medium * (Variable('socio_eco_1') == 2), beta_x_low * (Variable('socio_eco_1') == 3), beta_x_level_2 * (Variable('socio_eco_2') == 2), beta_x_level_3 * (Variable('socio_eco_2') == 3), beta_x_level_4 * (Variable('socio_eco_2') == 4)])
Total running time of the script: (0 minutes 0.002 seconds)