.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_expressions.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_programmers_plot_expressions.py: biogeme.expressions =================== 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: Tue Nov 21 14:53:49 2023 .. GENERATED FROM PYTHON SOURCE LINES 15-27 .. code-block:: default from biogeme.version import getText import numpy as np import pandas as pd import biogeme.expressions as ex import biogeme.database as db import biogeme.exceptions as excep from biogeme import models from biogeme import tools from biogeme.expressions import IdManager, TypeOfElementaryExpression import biogeme.biogeme_logging as blog .. GENERATED FROM PYTHON SOURCE LINES 28-29 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: default print(getText()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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) .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 35-40 Simple expressions ------------------ Simple expressions can be evaluated both with the functions `getValue`(implemented in Python) and the `getValue_c` (implemented in C++). They do not require a database. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default x = ex.Beta('x', 2, None, None, 1) x .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('x', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: default x.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: default x.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.0 .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default y = ex.Beta('y', 3, None, None, 1) y .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('y', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: default y.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: default y.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 63-64 Note that if the parameter has to be estimated, its value cannot be obtained. .. GENERATED FROM PYTHON SOURCE LINES 64-70 .. code-block:: default unknown_parameter = ex.Beta('x', 2, None, None, 0) try: unknown_parameter.getValue() except excep.BiogemeError as e: print(e) .. rst-class:: sphx-glr-script-out .. code-block:: none Parameter x must be estimated from data. .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: default one = ex.Numeric(1) one .. rst-class:: sphx-glr-script-out .. code-block:: none `1.0` .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default one.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: default one.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 81-82 Addition .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default z = x + y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 5.0 .. GENERATED FROM PYTHON SOURCE LINES 89-90 Substraction .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: default z = x - y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none -1 .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -1.0 .. GENERATED FROM PYTHON SOURCE LINES 97-98 Multiplication .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: default z = x * y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 6 .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 6.0 .. GENERATED FROM PYTHON SOURCE LINES 105-106 Division .. GENERATED FROM PYTHON SOURCE LINES 106-109 .. code-block:: default z = x / y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6666666666666666 .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6666666666666666 .. GENERATED FROM PYTHON SOURCE LINES 113-114 Power .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: default z = x**y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 8 .. GENERATED FROM PYTHON SOURCE LINES 118-120 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 8.0 .. GENERATED FROM PYTHON SOURCE LINES 121-122 Exponential .. GENERATED FROM PYTHON SOURCE LINES 122-125 .. code-block:: default z = ex.exp(x) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 7.38905609893065 .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 7.38905609893065 .. GENERATED FROM PYTHON SOURCE LINES 129-130 Logarithm .. GENERATED FROM PYTHON SOURCE LINES 130-133 .. code-block:: default z = ex.log(x) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6931471805599453 .. GENERATED FROM PYTHON SOURCE LINES 134-136 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6931471805599453 .. GENERATED FROM PYTHON SOURCE LINES 137-138 Minimum .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: default z = ex.bioMin(x, y) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.0 .. GENERATED FROM PYTHON SOURCE LINES 145-146 Maximum .. GENERATED FROM PYTHON SOURCE LINES 146-149 .. code-block:: default z = ex.bioMax(x, y) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 153-154 And .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: default z = x & y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 158-160 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 161-164 .. code-block:: default z = x & 0 z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 168-169 Or .. GENERATED FROM PYTHON SOURCE LINES 171-174 .. code-block:: default z = x | y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 175-177 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 178-181 .. code-block:: default z = ex.Numeric(0) | ex.Numeric(0) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 182-184 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 185-186 Equal .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: default z = x == y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 190-192 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 193-196 .. code-block:: default z = (x + 1) == y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 197-199 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 200-201 Not equal .. GENERATED FROM PYTHON SOURCE LINES 201-204 .. code-block:: default z = x != y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 205-207 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 208-211 .. code-block:: default z = (x + 1) != y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 212-214 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 215-216 Lesser or equal .. GENERATED FROM PYTHON SOURCE LINES 216-219 .. code-block:: default z = x <= y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 220-222 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 223-224 Greater or equal .. GENERATED FROM PYTHON SOURCE LINES 224-227 .. code-block:: default z = x >= y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 228-230 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 231-232 Lesser than .. GENERATED FROM PYTHON SOURCE LINES 232-235 .. code-block:: default z = x < y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 236-238 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 239-240 Greater than .. GENERATED FROM PYTHON SOURCE LINES 240-243 .. code-block:: default z = x > y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 244-246 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 247-248 Opposite .. GENERATED FROM PYTHON SOURCE LINES 248-251 .. code-block:: default z = -x z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none -2 .. GENERATED FROM PYTHON SOURCE LINES 252-254 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -2.0 .. GENERATED FROM PYTHON SOURCE LINES 255-256 Sum of multiples expressions .. GENERATED FROM PYTHON SOURCE LINES 256-260 .. code-block:: default listOfExpressions = [x, y, 1 + x, 1 + y] z = ex.bioMultSum(listOfExpressions) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 261-263 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 264-266 The result is the same as the following, but it implements the sum in a more efficient way. .. GENERATED FROM PYTHON SOURCE LINES 266-269 .. code-block:: default z = x + y + 1 + x + 1 + y z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 270-272 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 273-276 Element: this expression considers a dictionary of expressions, and an expression for the index. The index is evaluated, and the value of the corresponding expression in the dictionary is returned. .. GENERATED FROM PYTHON SOURCE LINES 276-278 .. code-block:: default my_dict = {1: ex.exp(-1), 2: ex.log(1.2), 3: 1234} .. GENERATED FROM PYTHON SOURCE LINES 279-282 .. code-block:: default index = x index.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 283-286 .. code-block:: default z = ex.Elem(my_dict, index) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.1823215567939546 .. GENERATED FROM PYTHON SOURCE LINES 287-289 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.1823215567939546 .. GENERATED FROM PYTHON SOURCE LINES 290-293 .. code-block:: default index = x - 1 index.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 294-297 .. code-block:: default z = ex.Elem(my_dict, index) z.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.36787944117144233 .. GENERATED FROM PYTHON SOURCE LINES 298-300 .. code-block:: default z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.36787944117144233 .. GENERATED FROM PYTHON SOURCE LINES 301-304 .. code-block:: default index = x - 2 index.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 305-307 If the value returned as index does not corresponds to an entry in the dictionary, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 309-315 .. code-block:: default z = ex.Elem(my_dict, index) try: z.getValue() except excep.BiogemeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: Key 0 is not present in the dictionary. Available keys: dict_keys([1, 2, 3]) .. GENERATED FROM PYTHON SOURCE LINES 316-322 .. code-block:: default z = ex.Elem(my_dict, index) try: z.getValue_c(prepareIds=True) except RuntimeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: src/cythonbiogeme/cpp/bioExprElem.cc:58: Biogeme exception: Key (-(x lit[0],fixed[0](2),2)=0) is not present in dictionary: 1: exp(-1) 2: log(1.2) 3: 1234 .. GENERATED FROM PYTHON SOURCE LINES 323-329 Complex expressions ------------------- When an expression is deemed complex in Biogeme, the `getValue` function is not available. Only the `getValue_c` function must be used. It calculates the expressions using a C++ implementation of the expression. .. GENERATED FROM PYTHON SOURCE LINES 331-335 Normal CDF: it calculates .. math:: \Phi(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x} e^{-\frac{1}{2}\omega^2}d\omega. .. GENERATED FROM PYTHON SOURCE LINES 335-338 .. code-block:: default z = ex.bioNormalCdf(x) z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9772498680518218 .. GENERATED FROM PYTHON SOURCE LINES 339-342 .. code-block:: default z = ex.bioNormalCdf(0) z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5 .. GENERATED FROM PYTHON SOURCE LINES 343-344 Derivative .. GENERATED FROM PYTHON SOURCE LINES 344-346 .. code-block:: default z = 30 * x + 20 * y .. GENERATED FROM PYTHON SOURCE LINES 347-350 .. code-block:: default zx = ex.Derive(z, 'x') zx.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 30.0 .. GENERATED FROM PYTHON SOURCE LINES 351-354 .. code-block:: default zx = ex.Derive(z, 'y') zx.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 20.0 .. GENERATED FROM PYTHON SOURCE LINES 355-359 Integral: let's calculate the integral of the pdf of a normal distribution: .. math:: \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{+\infty} e^{-\frac{1}{2}\omega^2}d\omega = 1. .. GENERATED FROM PYTHON SOURCE LINES 359-364 .. code-block:: default omega = ex.RandomVariable('omega') pdf = ex.exp(-omega * omega / 2) z = ex.Integrate(pdf, 'omega') / np.sqrt(2 * np.pi) z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 365-378 In order to change the bounds of integration, a change of variables must be performed. Let's calculate .. math:: \int_0^1 x^2 dx=\frac{1}{3}. If :math:`a` is the lower bound of integration, and :math:`b` is the upper bound, the change of variable is .. math:: x = a + \frac{b-a}{1+e^{-\omega}}, and .. math:: dx = \frac{(b-a)e^{-\omega}}{(1+e^{-\omega})^2}d\omega. .. GENERATED FROM PYTHON SOURCE LINES 378-386 .. code-block:: default a = 0 b = 1 t = a + (b - a) / (1 + ex.exp(-omega)) dt = (b - a) * ex.exp(-omega) * (1 + ex.exp(-omega)) ** -2 integrand = t * t z = ex.Integrate(integrand * dt / (b - a), 'omega') z.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.3333323120662822 .. GENERATED FROM PYTHON SOURCE LINES 387-388 Expressions using a database .. GENERATED FROM PYTHON SOURCE LINES 388-402 .. code-block:: default df = pd.DataFrame( { 'Person': [1, 1, 1, 2, 2], 'Exclude': [0, 0, 1, 0, 1], 'Variable1': [10, 20, 30, 40, 50], 'Variable2': [100, 200, 300, 400, 500], 'Choice': [2, 2, 3, 1, 2], 'Av1': [0, 1, 1, 1, 1], 'Av2': [1, 1, 1, 1, 1], 'Av3': [0, 1, 1, 1, 1], } ) my_data = db.Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 403-404 Linear utility: it defines a linear conbinations of parameters are variables. .. GENERATED FROM PYTHON SOURCE LINES 404-409 .. code-block:: default beta1 = ex.Beta('beta1', 10, None, None, 0) beta2 = ex.Beta('beta2', 20, None, None, 0) v1 = ex.Variable('Variable1') v2 = ex.Variable('Variable2') .. GENERATED FROM PYTHON SOURCE LINES 410-417 .. code-block:: default listOfTerms = [ (beta1, v1), (beta2, v2), ] z = ex.bioLinearUtility(listOfTerms) z.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 2100., 4200., 6300., 8400., 10500.]) .. GENERATED FROM PYTHON SOURCE LINES 418-419 It is equivalent to the following, but implemented in a more efficient way. .. GENERATED FROM PYTHON SOURCE LINES 419-422 .. code-block:: default z = beta1 * v1 + beta2 * v2 z.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 2100., 4200., 6300., 8400., 10500.]) .. GENERATED FROM PYTHON SOURCE LINES 423-429 Monte Carlo: we approximate the integral .. math:: \int_0^1 x^2 dx=\frac{1}{3} using Monte-Carlo integration. As draws require a database, it is calculated for each entry in the database. .. GENERATED FROM PYTHON SOURCE LINES 429-433 .. code-block:: default draws = ex.bioDraws('draws', 'UNIFORM') z = ex.MonteCarlo(draws * draws) z.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.32299649, 0.33650473, 0.33501673, 0.35513668, 0.33056744]) .. GENERATED FROM PYTHON SOURCE LINES 434-435 Panel Trajectory: we first calculate a quantity for each entry in the database. .. GENERATED FROM PYTHON SOURCE LINES 435-440 .. code-block:: default v1 = ex.Variable('Variable1') v2 = ex.Variable('Variable2') p = v1 / (v1 + v2) p.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909]) .. GENERATED FROM PYTHON SOURCE LINES 441-447 We now declare the data as "panel", based on the identified `Person`. It means that the first three rows correspond to a sequence of three observations for individual 1, and the last two, a sequence of two observations for individual 2. The panel trajectory calculates the expression for each row associated with an individual, and calculate the product. .. GENERATED FROM PYTHON SOURCE LINES 447-449 .. code-block:: default my_data.panel('Person') .. GENERATED FROM PYTHON SOURCE LINES 450-451 In this case, we expect the following for individual 1: .. GENERATED FROM PYTHON SOURCE LINES 451-453 .. code-block:: default 0.09090909**3 .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0007513147783621339 .. GENERATED FROM PYTHON SOURCE LINES 454-455 And the following for individual 2: .. GENERATED FROM PYTHON SOURCE LINES 455-457 .. code-block:: default 0.09090909**2 .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0082644626446281 .. GENERATED FROM PYTHON SOURCE LINES 458-459 We verify that it is indeed the case: .. GENERATED FROM PYTHON SOURCE LINES 459-463 .. code-block:: default z = ex.PanelLikelihoodTrajectory(p) z.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.00075131, 0.00826446]) .. GENERATED FROM PYTHON SOURCE LINES 464-470 More complex expressions ------------------------ We set the number of draws for Monte-Carlo integration. It should be a large number. For the sake of computational efficiency, as this notebook is designed to illustrate the various function, we use a low value. .. GENERATED FROM PYTHON SOURCE LINES 472-474 .. code-block:: default NUMBER_OF_DRAWS = 100 .. GENERATED FROM PYTHON SOURCE LINES 475-476 We first create a small database .. GENERATED FROM PYTHON SOURCE LINES 476-490 .. code-block:: default df = pd.DataFrame( { 'Person': [1, 1, 1, 2, 2], 'Exclude': [0, 0, 1, 0, 1], 'Variable1': [10, 20, 30, 40, 50], 'Variable2': [100, 200, 300, 400, 500], 'Choice': [2, 2, 3, 1, 2], 'Av1': [0, 1, 1, 1, 1], 'Av2': [1, 1, 1, 1, 1], 'Av3': [0, 1, 1, 1, 1], } ) df .. raw:: html
Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3
0 1 0 10 100 2 0 1 0
1 1 0 20 200 2 1 1 1
2 1 1 30 300 3 1 1 1
3 2 0 40 400 1 1 1 1
4 2 1 50 500 2 1 1 1


.. GENERATED FROM PYTHON SOURCE LINES 491-493 .. code-block:: default my_data = db.Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 494-496 The following type of expression is a literal called Variable that corresponds to an entry in the database. .. GENERATED FROM PYTHON SOURCE LINES 496-504 .. code-block:: default Person = ex.Variable('Person') Variable1 = ex.Variable('Variable1') Variable2 = ex.Variable('Variable2') Choice = ex.Variable('Choice') Av1 = ex.Variable('Av1') Av2 = ex.Variable('Av2') Av3 = ex.Variable('Av3') .. GENERATED FROM PYTHON SOURCE LINES 505-507 It is possible to add a new column to the database, that creates a new variable that can be used in expressions. .. GENERATED FROM PYTHON SOURCE LINES 507-510 .. code-block:: default newvar_b = my_data.DefineVariable('newvar_b', Variable1 + Variable2) my_data.data .. raw:: html
Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3 newvar_b
0 1 0 10 100 2 0 1 0 110.0
1 1 0 20 200 2 1 1 1 220.0
2 1 1 30 300 3 1 1 1 330.0
3 2 0 40 400 1 1 1 1 440.0
4 2 1 50 500 2 1 1 1 550.0


.. GENERATED FROM PYTHON SOURCE LINES 511-512 It is equivalent to the following Pandas statement. .. GENERATED FROM PYTHON SOURCE LINES 512-515 .. code-block:: default my_data.data['newvar_p'] = my_data.data['Variable1'] + my_data.data['Variable2'] my_data.data .. raw:: html
Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3 newvar_b newvar_p
0 1 0 10 100 2 0 1 0 110.0 110
1 1 0 20 200 2 1 1 1 220.0 220
2 1 1 30 300 3 1 1 1 330.0 330
3 2 0 40 400 1 1 1 1 440.0 440
4 2 1 50 500 2 1 1 1 550.0 550


.. GENERATED FROM PYTHON SOURCE LINES 516-519 **Do not use chaining comparison expressions with Biogeme. Not only it does not provide the expected expression, but it does not trigger a warning or an exception.** .. GENERATED FROM PYTHON SOURCE LINES 519-522 .. code-block:: default my_expression = 200 <= Variable2 <= 400 print(my_expression) .. rst-class:: sphx-glr-script-out .. code-block:: none (Variable2 <= `400.0`) .. GENERATED FROM PYTHON SOURCE LINES 523-528 The reason is that Python executes `200 <= Variable2 <= 400` as `(200 <= Variable2) and (Variable2 <= 400)`. The `and` operator cannot be overloaded in Python. Therefore, it does not return a Biogeme expression. Note that Pandas does not allow chaining either, and has implemented a `between` function instead. .. GENERATED FROM PYTHON SOURCE LINES 528-531 .. code-block:: default my_data.data['chaining_p'] = my_data.data['Variable2'].between(200, 400) my_data.data .. raw:: html
Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3 newvar_b newvar_p chaining_p
0 1 0 10 100 2 0 1 0 110.0 110 False
1 1 0 20 200 2 1 1 1 220.0 220 True
2 1 1 30 300 3 1 1 1 330.0 330 True
3 2 0 40 400 1 1 1 1 440.0 440 True
4 2 1 50 500 2 1 1 1 550.0 550 False


.. GENERATED FROM PYTHON SOURCE LINES 532-535 The following type of expression is another literal, corresponding to an unknown parameter. Note that the value is just a starting value for the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 535-539 .. code-block:: default beta1 = ex.Beta('beta1', 0.2, None, None, 0) beta2 = ex.Beta('beta2', 0.4, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 540-542 The last argument allows to fix the value of the parameter to the value. .. GENERATED FROM PYTHON SOURCE LINES 542-545 .. code-block:: default beta3 = ex.Beta('beta3', 1, None, None, 1) beta4 = ex.Beta('beta4', 0, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 546-548 Arithmetic operators are overloaded to allow standard manipulations of expressions. .. GENERATED FROM PYTHON SOURCE LINES 548-551 .. code-block:: default expr0 = beta3 + beta4 print(expr0) .. rst-class:: sphx-glr-script-out .. code-block:: none (Beta('beta3', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) + Beta('beta4', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)) .. GENERATED FROM PYTHON SOURCE LINES 552-555 The evaluation of expressions can be done in two ways. For simple expressions, the fonction `getValue`, implemented in Python, returns the value of the expression. .. GENERATED FROM PYTHON SOURCE LINES 555-557 .. code-block:: default expr0.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 558-559 It is possible to modify the values of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 559-563 .. code-block:: default newvalues = {'beta1': 1, 'beta2': 2, 'beta3': 3, 'beta4': 2} expr0.change_init_values(newvalues) expr0.getValue() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 564-571 Consider another expression: .. math:: e_1 = 2 \beta_1 - \frac{\exp(-\beta_2)}{\beta_2 (\beta_3 \geq \beta_4) + \beta_1 (\beta_3 < \beta_4)}, where :math:`(\beta_2 \geq \beta_1)` equals 1 if :math:`\beta_2 \geq \beta_1` and 0 otherwise. .. GENERATED FROM PYTHON SOURCE LINES 571-577 .. code-block:: default expr1 = 2 * beta1 - ex.exp(-beta2) / ( beta2 * (beta3 >= beta4) + beta1 * (beta3 < beta4) ) print(expr1) .. rst-class:: sphx-glr-script-out .. code-block:: none ((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) - (exp((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0))) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))) .. GENERATED FROM PYTHON SOURCE LINES 578-581 The function `getValue_c` is implemented in C++, and works for any expression. When use outside a specific context, the IDs must be explicitly prepared. .. GENERATED FROM PYTHON SOURCE LINES 581-583 .. code-block:: default expr1.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -1.275800115089098 .. GENERATED FROM PYTHON SOURCE LINES 584-586 It actually calls the function `getValueAndDerivates`, and returns its first output (without calculating the derivatives). .. GENERATED FROM PYTHON SOURCE LINES 586-588 .. code-block:: default f, g, h, bhhh = expr1.getValueAndDerivatives(prepareIds=True) .. GENERATED FROM PYTHON SOURCE LINES 589-591 .. code-block:: default f .. rst-class:: sphx-glr-script-out .. code-block:: none -1.275800115089098 .. GENERATED FROM PYTHON SOURCE LINES 592-593 We create a pandas DataFrame just to have a nicer display of the results. .. GENERATED FROM PYTHON SOURCE LINES 593-595 .. code-block:: default pd.DataFrame(g) .. raw:: html
0
0 2.0000
1 5.8653


.. GENERATED FROM PYTHON SOURCE LINES 596-598 .. code-block:: default pd.DataFrame(h) .. raw:: html
0 1
0 0.0 0.000000
1 0.0 -31.002302


.. GENERATED FROM PYTHON SOURCE LINES 599-601 .. code-block:: default pd.DataFrame(bhhh) .. raw:: html
0 1
0 4.000000 11.730601
1 11.730601 34.401749


.. GENERATED FROM PYTHON SOURCE LINES 602-603 Note that the BHHH matrix is the outer product of the gradient with itself. .. GENERATED FROM PYTHON SOURCE LINES 603-605 .. code-block:: default pd.DataFrame(np.outer(g, g)) .. raw:: html
0 1
0 4.000000 11.730601
1 11.730601 34.401749


.. GENERATED FROM PYTHON SOURCE LINES 606-608 If the derivatives are not needed, their calculation can be skipped. Here, we calculate the gradient, but not the hessian. .. GENERATED FROM PYTHON SOURCE LINES 608-610 .. code-block:: default expr1.getValueAndDerivatives(gradient=True, hessian=False, bhhh=False, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none (-1.275800115089098, array([2. , 5.8653004]), None, None) .. GENERATED FROM PYTHON SOURCE LINES 611-615 It can also generate a function that takes the value of the parameters as argument, and provides a tuple with the value of the expression and its derivatives. By default, it returns the value of the function, its gradient and its hessian. .. GENERATED FROM PYTHON SOURCE LINES 615-617 .. code-block:: default the_function = expr1.createFunction() .. GENERATED FROM PYTHON SOURCE LINES 618-619 We evaluate it at one point... .. GENERATED FROM PYTHON SOURCE LINES 619-621 .. code-block:: default the_function([1, 2]) .. rst-class:: sphx-glr-script-out .. code-block:: none (1.9323323583816936, array([2. , 0.10150146]), array([[ 0. , 0. ], [ 0. , -0.1691691]])) .. GENERATED FROM PYTHON SOURCE LINES 622-623 ... and at another point. .. GENERATED FROM PYTHON SOURCE LINES 623-625 .. code-block:: default the_function([10, -2]) .. rst-class:: sphx-glr-script-out .. code-block:: none (23.694528049465326, array([ 2. , -1.84726402]), array([[0. , 0. ], [0. , 1.84726402]])) .. GENERATED FROM PYTHON SOURCE LINES 626-627 We can use it to check the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 627-629 .. code-block:: default tools.checkDerivatives(the_function, [1, 2], logg=True) .. rst-class:: sphx-glr-script-out .. code-block:: none x Gradient FinDiff Difference x[0] +2.000000E+00 +2.000000E+00 -1.167734E-09 x[1] +1.015015E-01 +1.015014E-01 +1.629049E-08 Row Col Hessian FinDiff Difference x[0] x[0] +0.000000E+00 +0.000000E+00 +0.000000E+00 x[0] x[1] +0.000000E+00 +0.000000E+00 +0.000000E+00 x[1] x[0] +0.000000E+00 +0.000000E+00 +0.000000E+00 x[1] x[1] -1.691691E-01 -1.691691E-01 -3.203118E-08 (1.9323323583816936, array([2. , 0.10150146]), array([[ 0. , 0. ], [ 0. , -0.1691691]]), array([-1.16773435e-09, 1.62904950e-08]), array([[ 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, -3.20311803e-08]])) .. GENERATED FROM PYTHON SOURCE LINES 630-631 And it is possible to also obtain the BHHH matrix. .. GENERATED FROM PYTHON SOURCE LINES 631-634 .. code-block:: default the_function = expr1.createFunction(bhhh=True) the_function([1, 2]) .. rst-class:: sphx-glr-script-out .. code-block:: none (1.9323323583816936, array([2. , 0.10150146]), array([[ 0. , 0. ], [ 0. , -0.1691691]]), array([[4. , 0.20300292], [0.20300292, 0.01030255]])) .. GENERATED FROM PYTHON SOURCE LINES 635-639 It can take a database as input, and evaluate the expression and its derivatives for each entry in the database. In the following example, as no variable of the database is involved in the expression, the output of the expression is the same for each entry. .. GENERATED FROM PYTHON SOURCE LINES 639-642 .. code-block:: default results = expr1.getValueAndDerivatives(database=my_data, aggregation=False) print(len(results)) .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 643-652 .. code-block:: default f_array, g_array, h_array, bhhh_array = results for f, g, h, bhhh in zip(f_array, g_array, h_array, bhhh_array): print('******') print(f'{f=}') print(f'{g=}') print(f'{h=}') print(f'{bhhh=}') .. rst-class:: sphx-glr-script-out .. code-block:: none ****** f=1.9323323583816936 g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=1.9323323583816936 g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=1.9323323583816936 g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=1.9323323583816936 g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=1.9323323583816936 g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) .. GENERATED FROM PYTHON SOURCE LINES 653-654 If `aggregation` is set to `True`, the results are accumulated as a sum. .. GENERATED FROM PYTHON SOURCE LINES 654-660 .. code-block:: default f, g, h, bhhh = expr1.getValueAndDerivatives(database=my_data, aggregation=True) print(f'{f=}') print(f'{g=}') print(f'{h=}') print(f'{bhhh=}') .. rst-class:: sphx-glr-script-out .. code-block:: none f=9.661661791908468 g=array([10. , 0.50750731]) h=array([[ 0. , 0. ], [ 0. , -0.84584552]]) bhhh=array([[20. , 1.01501462], [ 1.01501462, 0.05151273]]) .. GENERATED FROM PYTHON SOURCE LINES 661-663 The following function scans the expression and extracts a dict with all free parameters. .. GENERATED FROM PYTHON SOURCE LINES 663-665 .. code-block:: default expr1.set_of_elementary_expression(TypeOfElementaryExpression.FREE_BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta2', 'beta1'} .. GENERATED FROM PYTHON SOURCE LINES 666-667 Options can be set to extract free parameters, fixed parameters, or both. .. GENERATED FROM PYTHON SOURCE LINES 667-669 .. code-block:: default expr1.set_of_elementary_expression(TypeOfElementaryExpression.FIXED_BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta4', 'beta3'} .. GENERATED FROM PYTHON SOURCE LINES 670-672 .. code-block:: default expr1.set_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta2', 'beta4', 'beta1', 'beta3'} .. GENERATED FROM PYTHON SOURCE LINES 673-674 It is possible also to extract an elementary expression from its name. .. GENERATED FROM PYTHON SOURCE LINES 674-676 .. code-block:: default expr1.getElementaryExpression('beta2') .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) .. GENERATED FROM PYTHON SOURCE LINES 677-686 Let's consider an expression involving two variables :math:`V_1` and :math:`V_2`: .. math:: e_2 = 2 \beta_1 V_1 - \frac{\exp(-\beta_2 V_2)}{\beta_2 (\beta_3 \geq \beta_4) + \beta_1 (\beta_3 < \beta_4)}, where :math:`(\beta_2 \geq \beta_1)` equals 1 if :math:`\beta_2 \geq \beta_1` and 0 otherwise. Note that, in our example, the second term is numerically negligible with respect to the first one. .. GENERATED FROM PYTHON SOURCE LINES 686-692 .. code-block:: default expr2 = 2 * beta1 * Variable1 - ex.exp(-beta2 * Variable2) / ( beta2 * (beta3 >= beta4) + beta1 * (beta3 < beta4) ) print(expr2) .. rst-class:: sphx-glr-script-out .. code-block:: none (((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable2)) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))) .. GENERATED FROM PYTHON SOURCE LINES 693-696 It is not a simple expression anymore, and only the function `getValue_c` can be invoked. If we try the `getValue` function, it raises an exception. .. GENERATED FROM PYTHON SOURCE LINES 696-701 .. code-block:: default try: expr2.getValue() except excep.BiogemeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: Parameter beta1 must be estimated from data. .. GENERATED FROM PYTHON SOURCE LINES 702-707 As the expression is called out of a specific context, it should be instructed to prepare its IDs. Note that if no database is provided, an exception is raised when the formula contains variables. Indeed, the values of these variables cannot be found anywhere. .. GENERATED FROM PYTHON SOURCE LINES 707-712 .. code-block:: default try: expr2.getValue_c(prepareIds=True) except excep.BiogemeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: No database is provided and an expression contains variables: {'Variable1', 'Variable2'} .. GENERATED FROM PYTHON SOURCE LINES 713-715 .. code-block:: default expr2.getValue_c(database=my_data, aggregation=False, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 4., 8., 12., 16., 20.]) .. GENERATED FROM PYTHON SOURCE LINES 716-718 The following function extracts the names of the parameters apprearing in the expression. .. GENERATED FROM PYTHON SOURCE LINES 718-720 .. code-block:: default expr2.set_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta2', 'beta4', 'beta1', 'beta3'} .. GENERATED FROM PYTHON SOURCE LINES 721-722 The list of parameters can also be obtained in the form of a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 722-724 .. code-block:: default expr2.dict_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0), 'beta2': Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0), 'beta3': Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1), 'beta4': Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)} .. GENERATED FROM PYTHON SOURCE LINES 725-726 The list of variables can also be obtained in the form of a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 726-728 .. code-block:: default expr2.dict_of_elementary_expression(TypeOfElementaryExpression.VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Variable1': Variable1, 'Variable2': Variable2} .. GENERATED FROM PYTHON SOURCE LINES 729-730 or a set... .. GENERATED FROM PYTHON SOURCE LINES 730-732 .. code-block:: default expr2.set_of_elementary_expression(TypeOfElementaryExpression.VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Variable1', 'Variable2'} .. GENERATED FROM PYTHON SOURCE LINES 733-736 Expressions are defined recursively, using a tree representation. The following function describes the type of the upper most node of the tree. .. GENERATED FROM PYTHON SOURCE LINES 736-738 .. code-block:: default expr2.getClassName() .. rst-class:: sphx-glr-script-out .. code-block:: none 'Minus' .. GENERATED FROM PYTHON SOURCE LINES 739-743 The signature is a formal representation of the expression, assigning identifiers to each node of the tree, and representing them starting from the leaves. It is easy to parse, and is passed to the C++ implementation. .. GENERATED FROM PYTHON SOURCE LINES 745-747 As the expression is used out of a specific context, it must be prepared before using it. .. GENERATED FROM PYTHON SOURCE LINES 747-751 .. code-block:: default expr2.prepare(database=my_data, numberOfDraws=0) expr2.getStatusIdManager() print(expr2) .. rst-class:: sphx-glr-script-out .. code-block:: none (((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable2)) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))) .. GENERATED FROM PYTHON SOURCE LINES 752-754 .. code-block:: default expr2.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5751161296},2.0', b'{5750675920}"beta1"[0],0,0', b'{5750670544}(2),5751161296,5750675920', b'{5682743680}"Variable1",6,2', b'{5751161344}(2),5750670544,5682743680', b'{5750672224}"beta2"[0],1,1', b'{5751165568}(1),5750672224', b'{5682752224}"Variable2",7,3', b'{5751165808}(2),5751165568,5682752224', b'{5751165040}(1),5751165808', b'{5750672224}"beta2"[0],1,1', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5752127712}(2),5750672848,5750670736', b'{5752133232}(2),5750672224,5752127712', b'{5750675920}"beta1"[0],0,0', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5752129776}(2),5750672848,5750670736', b'{5752128096}(2),5750675920,5752129776', b'{5752143840}(2),5752133232,5752128096', b'{5752143312}(2),5751165040,5752143840', b'{5752140912}(2),5751161344,5752143312'] .. GENERATED FROM PYTHON SOURCE LINES 755-767 The elementary expressions are - free parameters, - fixed parameters, - random variables (for numerical integration), - draws (for Monte-Carlo integration), and - variables from the database. The following function extracts all elementary expressions from a list of formulas, give them a unique numbering, and return them organized by group, as defined above (with the exception of the variables, that are directly available in the database). .. GENERATED FROM PYTHON SOURCE LINES 767-771 .. code-block:: default collection_of_formulas = [expr1, expr2] formulas = IdManager(collection_of_formulas, my_data, None) .. GENERATED FROM PYTHON SOURCE LINES 772-773 Unique numbering for all elementary expressions. .. GENERATED FROM PYTHON SOURCE LINES 773-775 .. code-block:: default formulas.elementary_expressions.indices .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': 0, 'beta2': 1, 'beta3': 2, 'beta4': 3, 'Person': 4, 'Exclude': 5, 'Variable1': 6, 'Variable2': 7, 'Choice': 8, 'Av1': 9, 'Av2': 10, 'Av3': 11, 'newvar_b': 12, 'newvar_p': 13, 'chaining_p': 14} .. GENERATED FROM PYTHON SOURCE LINES 776-778 .. code-block:: default formulas.free_betas .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={'beta1': Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0), 'beta2': Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)}, indices={'beta1': 0, 'beta2': 1}, names=['beta1', 'beta2']) .. GENERATED FROM PYTHON SOURCE LINES 779-781 Each elementary expression has two ids. One unique index across all elementary expressions, and one unique within each specific group. .. GENERATED FROM PYTHON SOURCE LINES 781-783 .. code-block:: default [(i.elementaryIndex, i.betaId) for k, i in formulas.free_betas.expressions.items()] .. rst-class:: sphx-glr-script-out .. code-block:: none [(0, 0), (1, 1)] .. GENERATED FROM PYTHON SOURCE LINES 784-786 .. code-block:: default formulas.free_betas.names .. rst-class:: sphx-glr-script-out .. code-block:: none ['beta1', 'beta2'] .. GENERATED FROM PYTHON SOURCE LINES 787-789 .. code-block:: default formulas.fixed_betas .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={'beta3': Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1), 'beta4': Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)}, indices={'beta3': 0, 'beta4': 1}, names=['beta3', 'beta4']) .. GENERATED FROM PYTHON SOURCE LINES 790-792 .. code-block:: default [(i.elementaryIndex, i.betaId) for k, i in formulas.fixed_betas.expressions.items()] .. rst-class:: sphx-glr-script-out .. code-block:: none [(2, 0), (3, 1)] .. GENERATED FROM PYTHON SOURCE LINES 793-795 .. code-block:: default formulas.fixed_betas.names .. rst-class:: sphx-glr-script-out .. code-block:: none ['beta3', 'beta4'] .. GENERATED FROM PYTHON SOURCE LINES 796-798 .. code-block:: default formulas.random_variables .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={}, indices={}, names=[]) .. GENERATED FROM PYTHON SOURCE LINES 799-800 Monte Carlo integration is based on draws. .. GENERATED FROM PYTHON SOURCE LINES 800-804 .. code-block:: default my_draws = ex.bioDraws('my_draws', 'UNIFORM') expr3 = ex.MonteCarlo(my_draws * my_draws) print(expr3) .. rst-class:: sphx-glr-script-out .. code-block:: none MonteCarlo((bioDraws("my_draws", "UNIFORM") * bioDraws("my_draws", "UNIFORM"))) .. GENERATED FROM PYTHON SOURCE LINES 805-807 Note that draws are different from random variables, used for numerical integration. .. GENERATED FROM PYTHON SOURCE LINES 807-809 .. code-block:: default expr3.set_of_elementary_expression(TypeOfElementaryExpression.RANDOM_VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 810-811 The following function reports the draws involved in an expression. .. GENERATED FROM PYTHON SOURCE LINES 811-813 .. code-block:: default expr3.set_of_elementary_expression(TypeOfElementaryExpression.DRAWS) .. rst-class:: sphx-glr-script-out .. code-block:: none {'my_draws'} .. GENERATED FROM PYTHON SOURCE LINES 814-816 The following function checks if draws are defined outside MonteCarlo, and return their names. .. GENERATED FROM PYTHON SOURCE LINES 816-819 .. code-block:: default wrong_expression = my_draws + ex.MonteCarlo(my_draws * my_draws) wrong_expression.check_draws() .. rst-class:: sphx-glr-script-out .. code-block:: none {'my_draws'} .. GENERATED FROM PYTHON SOURCE LINES 820-821 Checking the correct expression returns an empty set. .. GENERATED FROM PYTHON SOURCE LINES 821-823 .. code-block:: default expr3.check_draws() .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 824-825 The expression is a Monte-Carlo integration. .. GENERATED FROM PYTHON SOURCE LINES 825-827 .. code-block:: default expr3.getClassName() .. rst-class:: sphx-glr-script-out .. code-block:: none 'MonteCarlo' .. GENERATED FROM PYTHON SOURCE LINES 828-831 Note that the draws are associated with a database. Therefore, the evaluation of expressions involving Monte Carlo integration can only be done on a database. If none is provided, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 831-836 .. code-block:: default try: expr3.getValue_c(numberOfDraws=NUMBER_OF_DRAWS) except excep.BiogemeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: An expression involving MonteCarlo integration must be associated with a database. .. GENERATED FROM PYTHON SOURCE LINES 837-840 Here is its value. It is an approximation of .. math:: \int_0^1 x^2 dx=\frac{1}{3}. .. GENERATED FROM PYTHON SOURCE LINES 840-842 .. code-block:: default expr3.getValue_c(database=my_data, numberOfDraws=NUMBER_OF_DRAWS, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.34528706, 0.32486975, 0.32311505, 0.31370743, 0.40247583]) .. GENERATED FROM PYTHON SOURCE LINES 843-844 Here is its signature. .. GENERATED FROM PYTHON SOURCE LINES 844-847 .. code-block:: default expr3.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr3.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5752139904}"my_draws",0,0', b'{5752139904}"my_draws",0,0', b'{5752137408}(2),5752139904,5752139904', b'{5752129728}(1),5752137408'] .. GENERATED FROM PYTHON SOURCE LINES 848-850 The same integral can be calculated using numerical integration, declaring a random variable. .. GENERATED FROM PYTHON SOURCE LINES 850-852 .. code-block:: default omega = ex.RandomVariable('omega') .. GENERATED FROM PYTHON SOURCE LINES 853-856 Numerical integration calculates integrals between :math:`-\infty` and :math:`+\infty`. Here, the interval being :math:`[0,1]`, a change of variables is required. .. GENERATED FROM PYTHON SOURCE LINES 856-863 .. code-block:: default a = 0 b = 1 x = a + (b - a) / (1 + ex.exp(-omega)) dx = (b - a) * ex.exp(-omega) * (1 + ex.exp(-omega)) ** (-2) integrand = x * x expr4 = ex.Integrate(integrand * dx / (b - a), 'omega') .. GENERATED FROM PYTHON SOURCE LINES 864-865 In this case, omega is a random variable. .. GENERATED FROM PYTHON SOURCE LINES 865-867 .. code-block:: default expr4.dict_of_elementary_expression(TypeOfElementaryExpression.RANDOM_VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'omega': omega} .. GENERATED FROM PYTHON SOURCE LINES 868-870 .. code-block:: default print(expr4) .. rst-class:: sphx-glr-script-out .. code-block:: none Integrate(((((`0.0` + (`1.0` / (`1.0` + exp((-omega))))) * (`0.0` + (`1.0` / (`1.0` + exp((-omega)))))) * ((`1.0` * exp((-omega))) * ((`1.0` + exp((-omega))) ** `-2.0`))) / `1.0`), "omega") .. GENERATED FROM PYTHON SOURCE LINES 871-873 The folllowing function checks if random variables are defined outside an Integrate statement. .. GENERATED FROM PYTHON SOURCE LINES 873-876 .. code-block:: default wrong_expression = x * x wrong_expression.check_rv() .. rst-class:: sphx-glr-script-out .. code-block:: none {'omega'} .. GENERATED FROM PYTHON SOURCE LINES 877-879 The same function called from the correct expression returns an empty set. .. GENERATED FROM PYTHON SOURCE LINES 879-881 .. code-block:: default expr4.check_rv() .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 882-883 Calculating its value requires the C++ implementation. .. GENERATED FROM PYTHON SOURCE LINES 883-885 .. code-block:: default expr4.getValue_c(my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.33333231, 0.33333231, 0.33333231, 0.33333231, 0.33333231]) .. GENERATED FROM PYTHON SOURCE LINES 886-901 We illustrate now the Elem function. It takes two arguments: a dictionary, and a formula for the key. For each entry in the database, the formula is evaluated, and its result identifies which formula in the dictionary should be evaluated. Here is 'Person' is 1, the expression is .. math:: e_1=2 \beta_1 - \frac{\exp(-\beta_2)}{\beta_3 (\beta_2 \geq \beta_1)}, and if 'Person' is 2, the expression is .. math:: e_2=2 \beta_1 V_1 - \frac{\exp(-\beta_2 V_2) }{ \beta_3 (\beta_2 \geq \beta_1)}. As it is a regular expression, it can be included in any formula. Here, we illustrate it by dividing the result by 10. .. GENERATED FROM PYTHON SOURCE LINES 901-905 .. code-block:: default elemExpr = ex.Elem({1: expr1, 2: expr2}, Person) expr5 = elemExpr / 10 print(expr5) .. rst-class:: sphx-glr-script-out .. code-block:: none ({{1:((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) - (exp((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0))) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))), 2:(((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable2)) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))))))}[Person] / `10.0`) .. GENERATED FROM PYTHON SOURCE LINES 906-908 .. code-block:: default expr5.dict_of_elementary_expression(TypeOfElementaryExpression.VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Person': Person, 'Variable1': Variable1, 'Variable2': Variable2} .. GENERATED FROM PYTHON SOURCE LINES 909-913 Note that `Variable1` and `Variable2` have previously been involved in another formula. Therefore, they have been numbered according to this formula, and this numbering is invalid for the new expression `expr5`. An error is triggered .. GENERATED FROM PYTHON SOURCE LINES 913-918 .. code-block:: default try: expr5.getValue_c(database=my_data) except excep.BiogemeError as e: print(e) .. rst-class:: sphx-glr-script-out .. code-block:: none Expression evaluated out of context. Set prepareIds to True. .. GENERATED FROM PYTHON SOURCE LINES 919-921 .. code-block:: default expr5.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-0.12758001, -0.12758001, -0.12758001, 1.6 , 2. ]) .. GENERATED FROM PYTHON SOURCE LINES 922-924 .. code-block:: default testElem = ex.MonteCarlo(ex.Elem({1: my_draws * my_draws}, 1)) .. GENERATED FROM PYTHON SOURCE LINES 925-927 .. code-block:: default testElem.audit() .. rst-class:: sphx-glr-script-out .. code-block:: none ([], []) .. GENERATED FROM PYTHON SOURCE LINES 928-930 The next expression is simply the sum of multiple expressions. The argument is a list of expressions. .. GENERATED FROM PYTHON SOURCE LINES 930-933 .. code-block:: default expr6 = ex.bioMultSum([expr1, expr2, expr4]) print(expr6) .. rst-class:: sphx-glr-script-out .. code-block:: none bioMultSum(((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) - (exp((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0))) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))), (((`2.0` * Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) * Variable2)) / ((Beta('beta2', 0.4, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) >= Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1))) + (Beta('beta1', 0.2, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * (Beta('beta3', 3, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) < Beta('beta4', 2, -1.3407807929942596e+154, 1.3407807929942596e+154, 1)))))), Integrate(((((`0.0` + (`1.0` / (`1.0` + exp((-omega))))) * (`0.0` + (`1.0` / (`1.0` + exp((-omega)))))) * ((`1.0` * exp((-omega))) * ((`1.0` + exp((-omega))) ** `-2.0`))) / `1.0`), "omega")) .. GENERATED FROM PYTHON SOURCE LINES 934-936 .. code-block:: default expr6.getValue_c(database=my_data, numberOfDraws=NUMBER_OF_DRAWS, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 3.0575322, 7.0575322, 11.0575322, 15.0575322, 19.0575322]) .. GENERATED FROM PYTHON SOURCE LINES 937-943 We now illustrate how to calculate a logit model, that is .. math:: \frac{y_1 e^{V_1}}{y_0 e^{V_0}+y_1 e^{V_1}+y_2 e^{V_2}} where :math:`V_0=-\beta_1`, :math:`V_1=-\beta_2` and :math:`V_2=-\beta_1`, and :math:`y_i = 1`, :math:`i=1,2,3`. .. GENERATED FROM PYTHON SOURCE LINES 943-947 .. code-block:: default V = {0: -beta1, 1: -beta2, 2: -beta1} av = {0: 1, 1: 1, 2: 1} expr7 = ex._bioLogLogit(V, av, 1) .. GENERATED FROM PYTHON SOURCE LINES 948-950 .. code-block:: default expr7.getValue_c(prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -1.2362866960692136 .. GENERATED FROM PYTHON SOURCE LINES 951-952 If the alternative is not in the choice set, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 952-958 .. code-block:: default expr7_wrong = ex.LogLogit(V, av, 3) try: expr7_wrong.getValue() except excep.BiogemeError as e: print(f'Exception: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception: Alternative 3 does not appear in the list of utility functions: dict_keys([0, 1, 2]) .. GENERATED FROM PYTHON SOURCE LINES 959-961 It is actually better to use the C++ implementation, available in the module models. .. GENERATED FROM PYTHON SOURCE LINES 963-966 .. code-block:: default expr8 = models.loglogit(V, av, 1) expr8.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-1.2362867, -1.2362867, -1.2362867, -1.2362867, -1.2362867]) .. GENERATED FROM PYTHON SOURCE LINES 967-969 As the result is a numpy array, it can be used for any calculation. Here, we show how to calculate the logsum. .. GENERATED FROM PYTHON SOURCE LINES 969-972 .. code-block:: default for v in V.values(): print(v.getValue_c(database=my_data, prepareIds=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none [-0.2 -0.2 -0.2 -0.2 -0.2] [-0.4 -0.4 -0.4 -0.4 -0.4] [-0.2 -0.2 -0.2 -0.2 -0.2] .. GENERATED FROM PYTHON SOURCE LINES 973-981 .. code-block:: default logsum = np.log( np.sum( [np.exp(v.getValue_c(database=my_data, prepareIds=True)) for v in V.values()], axis=1, ) ) logsum .. rst-class:: sphx-glr-script-out .. code-block:: none array([1.40943791, 1.20943791, 1.40943791]) .. GENERATED FROM PYTHON SOURCE LINES 982-986 It is possible to calculate the derivative of a formula with respect to a literal: .. math:: e_9=\frac{\partial e_8}{\partial \beta_2}. .. GENERATED FROM PYTHON SOURCE LINES 986-989 .. code-block:: default expr9 = ex.Derive(expr8, 'beta2') expr9.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-0.70953921, -0.70953921, -0.70953921, -0.70953921, -0.70953921]) .. GENERATED FROM PYTHON SOURCE LINES 990-992 .. code-block:: default expr9.elementaryName .. rst-class:: sphx-glr-script-out .. code-block:: none 'beta2' .. GENERATED FROM PYTHON SOURCE LINES 993-1000 Biogeme also provides an approximation of the CDF of the normal distribution: .. math:: e_{10}= \frac{1}{{\sigma \sqrt {2\pi } }}\int_{-\infty}^t e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. } {2\sigma ^2 }}}dx. .. GENERATED FROM PYTHON SOURCE LINES 1000-1003 .. code-block:: default expr10 = ex.bioNormalCdf(Variable1 / 10 - 1) expr10.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.5 , 0.84134475, 0.97724987, 0.9986501 , 0.99996833]) .. GENERATED FROM PYTHON SOURCE LINES 1004-1006 Min and max operators are also available. To avoid any ambiguity with the Python operator, they are called bioMin and bioMax. .. GENERATED FROM PYTHON SOURCE LINES 1006-1009 .. code-block:: default expr11 = ex.bioMin(expr5, expr10) expr11.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-0.12758001, -0.12758001, -0.12758001, 0.9986501 , 0.99996833]) .. GENERATED FROM PYTHON SOURCE LINES 1010-1013 .. code-block:: default expr12 = ex.bioMax(expr5, expr10) expr12.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.5 , 0.84134475, 0.97724987, 1.6 , 2. ]) .. GENERATED FROM PYTHON SOURCE LINES 1014-1017 For the sake of efficiency, it is possible to specify explicitly a linear function, where each term is the product of a parameter and a variable. .. GENERATED FROM PYTHON SOURCE LINES 1017-1025 .. code-block:: default terms = [ (beta1, ex.Variable('Variable1')), (beta2, ex.Variable('Variable2')), (beta3, ex.Variable('newvar_b')), ] expr13 = ex.bioLinearUtility(terms) expr13.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 372., 744., 1116., 1488., 1860.]) .. GENERATED FROM PYTHON SOURCE LINES 1026-1029 In terms of specification, it is equivalent to the expression below. But the calculation of the derivatives is more efficient, as the linear structure of the specification is exploited. .. GENERATED FROM PYTHON SOURCE LINES 1029-1032 .. code-block:: default expr13bis = beta1 * Variable1 + beta2 * Variable2 + beta3 * newvar_b expr13bis.getValue_c(database=my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 372., 744., 1116., 1488., 1860.]) .. GENERATED FROM PYTHON SOURCE LINES 1033-1034 A Pythonic way to write a linear utility function. .. GENERATED FROM PYTHON SOURCE LINES 1034-1040 .. code-block:: default variables = ['v1', 'v2', 'v3', 'cost', 'time', 'headway'] coefficients = {f'{v}': ex.Beta(f'beta_{v}', 0, None, None, 0) for v in variables} terms = [coefficients[v] * ex.Variable(v) for v in variables] util = sum(terms) print(util) .. rst-class:: sphx-glr-script-out .. code-block:: none ((((((`0.0` + (Beta('beta_v1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * v1)) + (Beta('beta_v2', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * v2)) + (Beta('beta_v3', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * v3)) + (Beta('beta_cost', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * cost)) + (Beta('beta_time', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * time)) + (Beta('beta_headway', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * headway)) .. GENERATED FROM PYTHON SOURCE LINES 1041-1046 If the data is organized a panel data, it means that several rows correspond to the same individual. The expression `PanelLikelihoodTrajectory` calculates the product of the expression evaluated for each row. If Monte Carlo integration is involved, the same draws are used for each them. .. GENERATED FROM PYTHON SOURCE LINES 1048-1049 Our database contains 5 observations. .. GENERATED FROM PYTHON SOURCE LINES 1049-1051 .. code-block:: default my_data.getSampleSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 1052-1054 .. code-block:: default my_data.panel('Person') .. GENERATED FROM PYTHON SOURCE LINES 1055-1059 Once the data has been labeled as "panel", it is considered that there are only two series of observations, corresponding to each person. Each of these observations is associated with several rows of observations. .. GENERATED FROM PYTHON SOURCE LINES 1061-1063 .. code-block:: default my_data.getSampleSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 1064-1066 If we try to evaluate again the integral :math:`\int_0^1 x^2 dx=\frac{1}{3}`, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 1066-1071 .. code-block:: default try: expr3.getValue_c(database=my_data) except excep.BiogemeError as e: print(f'Exception: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none As the database is panel, the argument of MonteCarlo must contain a PanelLikelihoodTrajectory: MonteCarlo((bioDraws("my_draws", "UNIFORM") * bioDraws("my_draws", "UNIFORM"))) Exception: As the database is panel, the argument of MonteCarlo must contain a PanelLikelihoodTrajectory: MonteCarlo((bioDraws("my_draws", "UNIFORM") * bioDraws("my_draws", "UNIFORM"))) .. GENERATED FROM PYTHON SOURCE LINES 1072-1074 This is detected by the `audit` function, called before the expression is evaluated. .. GENERATED FROM PYTHON SOURCE LINES 1074-1076 .. code-block:: default expr3.audit(database=my_data) .. rst-class:: sphx-glr-script-out .. code-block:: none (['As the database is panel, the argument of MonteCarlo must contain a PanelLikelihoodTrajectory: MonteCarlo((bioDraws("my_draws", "UNIFORM") * bioDraws("my_draws", "UNIFORM")))'], []) .. GENERATED FROM PYTHON SOURCE LINES 1077-1078 We now evaluate an expression for panel data. .. GENERATED FROM PYTHON SOURCE LINES 1078-1089 .. code-block:: default c1 = ex.bioDraws('draws1', 'NORMAL_HALTON2') c2 = ex.bioDraws('draws2', 'NORMAL_HALTON2') U1 = ex.Beta('beta1', 0, None, None, 0) * Variable1 + 10 * c1 U2 = ex.Beta('beta2', 0, None, None, 0) * Variable2 + 10 * c2 U3 = 0 U = {1: U1, 2: U2, 3: U3} av = {1: Av1, 2: Av2, 3: Av3} expr14 = ex.log( ex.MonteCarlo(ex.PanelLikelihoodTrajectory(models.logit(U, av, Choice))) ) .. GENERATED FROM PYTHON SOURCE LINES 1090-1093 .. code-block:: default expr14.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr14 .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1094-1096 .. code-block:: default expr14.getValue_c(database=my_data, numberOfDraws=NUMBER_OF_DRAWS, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-3.91914292, -2.11209896]) .. GENERATED FROM PYTHON SOURCE LINES 1097-1105 .. code-block:: default expr14.getValueAndDerivatives( database=my_data, numberOfDraws=NUMBER_OF_DRAWS, gradient=True, hessian=True, aggregation=False, ) .. rst-class:: sphx-glr-script-out .. code-block:: none (array([-3.91914292, -2.11209896]), array([[-12.31921998, 76.80780015], [ -3.14130423, 68.58695767]]), array([[[ -165.65755306, 1546.42166536], [ 1546.42166536, -16565.75530623]], [[ -987.62129533, 9777.04786414], [ 9777.04786414, -98762.12953279]]]), array([[[ 151.76318103, -946.21218663], [-946.21218663, 5899.4381646 ]], [[ 9.86779229, -215.45250047], [-215.45250047, 4704.17076195]]])) .. GENERATED FROM PYTHON SOURCE LINES 1106-1114 .. code-block:: default expr14.getValueAndDerivatives( database=my_data, numberOfDraws=NUMBER_OF_DRAWS, gradient=True, hessian=True, aggregation=True, ) .. rst-class:: sphx-glr-script-out .. code-block:: none (-6.0312418791725335, array([-15.46052422, 145.39475782]), array([[ -1153.27884839, 11323.46952949], [ 11323.46952949, -115327.88483902]]), array([[ 161.63097331, -1161.6646871 ], [-1161.6646871 , 10603.60892655]])) .. GENERATED FROM PYTHON SOURCE LINES 1115-1118 A Python function can also be obtained for this expression. Note that it is available only for the aggregated version, summing over the database. .. GENERATED FROM PYTHON SOURCE LINES 1120-1124 .. code-block:: default the_function = expr14.createFunction( database=my_data, numberOfDraws=NUMBER_OF_DRAWS, gradient=True, hessian=True ) .. GENERATED FROM PYTHON SOURCE LINES 1125-1127 .. code-block:: default the_function([0, 0]) .. rst-class:: sphx-glr-script-out .. code-block:: none (-6.0312418791725335, array([-15.46052422, 145.39475782]), array([[ -1153.27884839, 11323.46952949], [ 11323.46952949, -115327.88483902]])) .. GENERATED FROM PYTHON SOURCE LINES 1128-1130 .. code-block:: default the_function([0.1, 0.1]) .. rst-class:: sphx-glr-script-out .. code-block:: none (-49.645666583910895, array([ 39.99999992, -553.04056325]), array([[-1.62802916e-06, 1.46098013e-05], [ 1.46098013e-05, -1.18518603e+03]])) .. GENERATED FROM PYTHON SOURCE LINES 1131-1132 It is possible to fix the value of some (or all) beta parameters .. GENERATED FROM PYTHON SOURCE LINES 1132-1134 .. code-block:: default print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1135-1137 .. code-block:: default expr14.fix_betas({'beta2': 0.123}) .. GENERATED FROM PYTHON SOURCE LINES 1138-1140 .. code-block:: default print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0.123, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1141-1142 The name of the parameter can also be changed while fixing its value. .. GENERATED FROM PYTHON SOURCE LINES 1144-1146 .. code-block:: default expr14.fix_betas({'beta2': 123}, prefix='prefix_', suffix='_suffix') .. GENERATED FROM PYTHON SOURCE LINES 1147-1149 .. code-block:: default print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('prefix_beta2_suffix', 123, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1150-1151 It can also be renamed using the following function. .. GENERATED FROM PYTHON SOURCE LINES 1153-1155 .. code-block:: default expr14.rename_elementary(['prefix_beta2_suffix'], prefix='PREFIX_', suffix='_SUFFIX') .. GENERATED FROM PYTHON SOURCE LINES 1156-1158 .. code-block:: default print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('PREFIX_prefix_beta2_suffix_SUFFIX', 123, -1.3407807929942596e+154, 1.3407807929942596e+154, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1159-1161 Signatures ---------- .. GENERATED FROM PYTHON SOURCE LINES 1163-1167 The Python library communicates the expressions to the C++ library using a syntax called a "signature". We describe and illustrate now the signature for each expression. Each expression is identified by an identifier provided by Python using the function 'id'. .. GENERATED FROM PYTHON SOURCE LINES 1169-1171 .. code-block:: default id(expr1) .. rst-class:: sphx-glr-script-out .. code-block:: none 5751164800 .. GENERATED FROM PYTHON SOURCE LINES 1172-1174 Numerical expression ++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1176-1177 {identifier},0.0 .. GENERATED FROM PYTHON SOURCE LINES 1177-1179 .. code-block:: default ex.Numeric(0).getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{11840887536},0.0'] .. GENERATED FROM PYTHON SOURCE LINES 1180-1182 Beta parameters +++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1184-1193 {identifier}"name"[status],uniqueId,betaId' where - status is 0 for free parameters, and non zero for fixed parameters, - uniqueId is a unique index given by Biogeme to all elementary expressions, - betaId is a unique index given by Biogeme to all free parameters, and to all fixed parameters. .. GENERATED FROM PYTHON SOURCE LINES 1195-1197 As the signature requires an Id, we need to prepare the expression first. .. GENERATED FROM PYTHON SOURCE LINES 1199-1202 .. code-block:: default beta1.prepare(database=my_data, numberOfDraws=0) beta1.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5750675920}"beta1"[0],0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1203-1206 .. code-block:: default beta3.prepare(database=my_data, numberOfDraws=0) beta3.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5750672848}"beta3"[1],0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1207-1209 Variables +++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1211-1217 {identifier}"name",uniqueId,variableId where - uniqueId is a unique index given by Biogeme to all elementary expressions, - variableId is a unique index given by Biogeme to all variables. .. GENERATED FROM PYTHON SOURCE LINES 1219-1221 .. code-block:: default Variable1.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5682743680}"Variable1",6,2'] .. GENERATED FROM PYTHON SOURCE LINES 1222-1224 Random variables ++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1226-1233 {identifier}"name",uniqueId,randomVariableId where - uniqueId is a unique index given by Biogeme to all elementary expressions, - randomVariableId is a unique index given by Biogeme to all random variables. .. GENERATED FROM PYTHON SOURCE LINES 1235-1238 .. code-block:: default omega.prepare(database=my_data, numberOfDraws=0) omega.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5752133328}"omega",0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1239-1241 Draws +++++ .. GENERATED FROM PYTHON SOURCE LINES 1243-1249 {identifier}"name",uniqueId,drawId where - uniqueId is a unique index given by Biogeme to all elementary expressions, - drawId is a unique index given by Biogeme to all draws. .. GENERATED FROM PYTHON SOURCE LINES 1251-1254 .. code-block:: default my_draws.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) my_draws.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5752139904}"my_draws",0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1255-1264 General expression ++++++++++++++++++ `{identifier}(numberOfChildren),idFirstChild,idSecondChild,idThirdChild,` etc... where the number of identifiers given after the comma matches the reported number of children. Specific examples are reported below. .. GENERATED FROM PYTHON SOURCE LINES 1266-1268 Binary operator /////////////// .. GENERATED FROM PYTHON SOURCE LINES 1270-1288 `{identifier}(2),idFirstChild,idSecondChild ` where operator is one of: - `Plus` - `Minus` - `Times` - `Divide` - `Power` - `bioMin` - `bioMax` - `And` - `Or` - `Equal` - `NotEqual` - `LessOrEqual` - `GreaterOrEqual` - `Less` - `Greater` .. GENERATED FROM PYTHON SOURCE LINES 1290-1292 .. code-block:: default the_sum = beta1 + Variable1 .. GENERATED FROM PYTHON SOURCE LINES 1293-1295 .. code-block:: default the_sum.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5750675920}"beta1"[0],0,0', b'{5682743680}"Variable1",6,2', b'{11840884560}(2),5750675920,5682743680'] .. GENERATED FROM PYTHON SOURCE LINES 1296-1298 Unary operator ////////////// .. GENERATED FROM PYTHON SOURCE LINES 1300-1309 `{identifier}(1),idChild,` where operator is one of: - `UnaryMinus` - `MonteCarlo` - `bioNormalCdf` - `PanelLikelihoodTrajectory` - `exp` - `log` .. GENERATED FROM PYTHON SOURCE LINES 1311-1313 .. code-block:: default m = -beta1 .. GENERATED FROM PYTHON SOURCE LINES 1314-1316 .. code-block:: default m.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5750675920}"beta1"[0],0,0', b'{11840895024}(1),5750675920'] .. GENERATED FROM PYTHON SOURCE LINES 1317-1319 LogLogit //////// .. GENERATED FROM PYTHON SOURCE LINES 1321-1322 {identifier}(nbrOfAlternatives),chosenAlt,altNumber,utility,availability,altNumber,utility,availability, etc. .. GENERATED FROM PYTHON SOURCE LINES 1324-1327 .. code-block:: default expr7.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr7.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5751137264},1.0', b'{5750675920}"beta1"[0],0,0', b'{5752137600}(1),5750675920', b'{5750672224}"beta2"[0],1,1', b'{5752131120}(1),5750672224', b'{5750675920}"beta1"[0],0,0', b'{5752140144}(1),5750675920', b'{5752140240},1.0', b'{5752129008},1.0', b'{5751129728},1.0', b'<_bioLogLogit>{5752138128}(3),5751137264,0,5752137600,5752140240,1,5752131120,5752129008,2,5752140144,5751129728'] .. GENERATED FROM PYTHON SOURCE LINES 1328-1330 Derive ////// .. GENERATED FROM PYTHON SOURCE LINES 1332-1333 {identifier},id of expression to derive,unique index of elementary expression .. GENERATED FROM PYTHON SOURCE LINES 1335-1338 .. code-block:: default expr9.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr9.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5751144416},1.0', b'{5750675920}"beta1"[0],0,0', b'{5752137600}(1),5750675920', b'{5750672224}"beta2"[0],1,1', b'{5752131120}(1),5750672224', b'{5750675920}"beta1"[0],0,0', b'{5752140144}(1),5750675920', b'{5751133568},1.0', b'{5751137504},1.0', b'{5751129632},1.0', b'<_bioLogLogit>{5751129776}(3),5751144416,0,5752137600,5751133568,1,5752131120,5751137504,2,5752140144,5751129632', b'{5751133664},5751129776,1'] .. GENERATED FROM PYTHON SOURCE LINES 1339-1341 Integrate ///////// .. GENERATED FROM PYTHON SOURCE LINES 1343-1344 {identifier},id of expression to derive,index of random variable .. GENERATED FROM PYTHON SOURCE LINES 1346-1349 .. code-block:: default expr4.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr4.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5752140336},0.0', b'{5752134816},1.0', b'{5752140048},1.0', b'{5752133328}"omega",0,0', b'{5752142016}(1),5752133328', b'{5752137456}(1),5752142016', b'{5752140192}(2),5752140048,5752137456', b'{5752135248}(2),5752134816,5752140192', b'{5752134096}(2),5752140336,5752135248', b'{5752140336},0.0', b'{5752134816},1.0', b'{5752140048},1.0', b'{5752133328}"omega",0,0', b'{5752142016}(1),5752133328', b'{5752137456}(1),5752142016', b'{5752140192}(2),5752140048,5752137456', b'{5752135248}(2),5752134816,5752140192', b'{5752134096}(2),5752140336,5752135248', b'{5752130928}(2),5752134096,5752134096', b'{5752129488},1.0', b'{5752133328}"omega",0,0', b'{5752141200}(1),5752133328', b'{5752129248}(1),5752141200', b'{5752141632}(2),5752129488,5752129248', b'{5752129536},1.0', b'{5752133328}"omega",0,0', b'{5752141392}(1),5752133328', b'{5752142976}(1),5752141392', b'{5752130736}(2),5752129536,5752142976', b'{5752141920},-2.0', b'{5752130016}(2),5752130736,5752141920', b'{5752131792}(2),5752141632,5752130016', b'{4926460880}(2),5752130928,5752131792', b'{5752143024},1.0', b'{5752141872}(2),4926460880,5752143024', b'{5752131264},5752141872,0'] .. GENERATED FROM PYTHON SOURCE LINES 1350-1352 Elem //// .. GENERATED FROM PYTHON SOURCE LINES 1354-1361 {identifier}(number_of_expressions),keyId,value1,expression1,value2,expression2, etc... where - keyId is the identifier of the expression calculating the key, - the number of pairs valuex,expressionx must correspond to the value of number_of_expressions .. GENERATED FROM PYTHON SOURCE LINES 1363-1366 .. code-block:: default elemExpr.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) elemExpr.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5682741376}"Person",4,0', b'{5751165904},2.0', b'{5750675920}"beta1"[0],0,0', b'{5751162352}(2),5751165904,5750675920', b'{5750672224}"beta2"[0],1,1', b'{5751177088}(1),5750672224', b'{5751176992}(1),5751177088', b'{5750672224}"beta2"[0],1,1', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5751175984}(2),5750672848,5750670736', b'{5751163744}(2),5750672224,5751175984', b'{5750675920}"beta1"[0],0,0', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5751164896}(2),5750672848,5750670736', b'{5751161200}(2),5750675920,5751164896', b'{5751162928}(2),5751163744,5751161200', b'{5751175936}(2),5751176992,5751162928', b'{5751164800}(2),5751162352,5751175936', b'{5751161296},2.0', b'{5750675920}"beta1"[0],0,0', b'{5750670544}(2),5751161296,5750675920', b'{5682743680}"Variable1",6,2', b'{5751161344}(2),5750670544,5682743680', b'{5750672224}"beta2"[0],1,1', b'{5751165568}(1),5750672224', b'{5682752224}"Variable2",7,3', b'{5751165808}(2),5751165568,5682752224', b'{5751165040}(1),5751165808', b'{5750672224}"beta2"[0],1,1', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5752127712}(2),5750672848,5750670736', b'{5752133232}(2),5750672224,5752127712', b'{5750675920}"beta1"[0],0,0', b'{5750672848}"beta3"[1],2,0', b'{5750670736}"beta4"[1],3,1', b'{5752129776}(2),5750672848,5750670736', b'{5752128096}(2),5750675920,5752129776', b'{5752143840}(2),5752133232,5752128096', b'{5752143312}(2),5751165040,5752143840', b'{5752140912}(2),5751161344,5752143312', b'{5752132320}(2),5682741376,1,5751164800,2,5752140912'] .. GENERATED FROM PYTHON SOURCE LINES 1367-1369 bioLinearUtility //////////////// .. GENERATED FROM PYTHON SOURCE LINES 1371-1381 {identifier}(numberOfTerms), beta1_exprId, beta1_uniqueId, beta1_name, variable1_exprId, variable1_uniqueId, variable1_name, etc... where 6 entries are provided for each term: - beta1_exprId is the expression id of the beta parameter - beta1_uniqueId is the unique id of the beta parameter - beta1_name is the name of the parameter - variable1_exprId is the expression id of the variable - variable1_uniqueId is the unique id of the variable - variable1_name is the name of the variable .. GENERATED FROM PYTHON SOURCE LINES 1383-1385 .. code-block:: default expr13.prepare(database=my_data, numberOfDraws=NUMBER_OF_DRAWS) expr13.getSignature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5750675920}"beta1"[0],0,0', b'{5750672224}"beta2"[0],1,1', b'{5750672848}"beta3"[1],2,0', b'{11840880816}"Variable1",5,2', b'{11840884224}"Variable2",6,3', b'{11840890752}"newvar_b",11,8', b'{11840885328}(3),5750675920,0,beta1,11840880816,5,Variable1,5750672224,1,beta2,11840884224,6,Variable2,5750672848,2,beta3,11840890752,11,newvar_b'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.303 seconds) .. _sphx_glr_download_auto_examples_programmers_plot_expressions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_expressions.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_expressions.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_