.. 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-34 .. code-block:: Python import numpy as np import pandas as pd import biogeme.biogeme_logging as blog import biogeme.database as db import biogeme.exceptions as excep import biogeme.expressions as ex import biogeme.tools.derivatives from biogeme import models from biogeme.expressions import IdManager, TypeOfElementaryExpression, LinearTermTuple from biogeme.function_output import ( BiogemeDisaggregateFunctionOutput, BiogemeFunctionOutput, NamedBiogemeFunctionOutput, NamedBiogemeDisaggregateFunctionOutput, ) from biogeme.version import get_text .. GENERATED FROM PYTHON SOURCE LINES 35-36 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python print(get_text()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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) .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python logger = blog.get_screen_logger(level=blog.DEBUG) .. GENERATED FROM PYTHON SOURCE LINES 42-47 Simple expressions ------------------ Simple expressions can be evaluated both with the functions `get_value`(implemented in Python) and the `get_value_c` (implemented in C++). They do not require a database. .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python x = ex.Beta('x', 2, None, None, 1) x .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('x', 2, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python x.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python x.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.0 .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python y = ex.Beta('y', 3, None, None, 1) y .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('y', 3, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python y.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python y.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 70-71 Note that if the parameter has to be estimated, its value cannot be obtained. .. GENERATED FROM PYTHON SOURCE LINES 71-78 .. code-block:: Python unknown_parameter = ex.Beta('x', 2, None, None, 0) try: unknown_parameter.get_value() except excep.BiogemeError as e: print(e) .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python one = ex.Numeric(1) one .. rst-class:: sphx-glr-script-out .. code-block:: none `1.0` .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python one.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: Python one.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 89-90 Addition .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python z = x + y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 5.0 .. GENERATED FROM PYTHON SOURCE LINES 97-98 Substraction .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python z = x - y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none -1 .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -1.0 .. GENERATED FROM PYTHON SOURCE LINES 105-106 Multiplication .. GENERATED FROM PYTHON SOURCE LINES 106-109 .. code-block:: Python z = x * y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 6 .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 6.0 .. GENERATED FROM PYTHON SOURCE LINES 113-114 Division .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python z = x / y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6666666666666666 .. GENERATED FROM PYTHON SOURCE LINES 118-120 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6666666666666666 .. GENERATED FROM PYTHON SOURCE LINES 121-122 Power .. GENERATED FROM PYTHON SOURCE LINES 122-125 .. code-block:: Python z = x**y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 8 .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 8.0 .. GENERATED FROM PYTHON SOURCE LINES 129-130 Exponential .. GENERATED FROM PYTHON SOURCE LINES 130-133 .. code-block:: Python z = ex.exp(x) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(7.38905609893065) .. GENERATED FROM PYTHON SOURCE LINES 134-136 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 7.38905609893065 .. GENERATED FROM PYTHON SOURCE LINES 137-138 Logarithm .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: Python z = ex.log(x) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.6931471805599453) .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6931471805599453 .. GENERATED FROM PYTHON SOURCE LINES 145-146 Minimum .. GENERATED FROM PYTHON SOURCE LINES 146-149 .. code-block:: Python z = ex.bioMin(x, y) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.0 .. GENERATED FROM PYTHON SOURCE LINES 153-154 Maximum .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: Python z = ex.bioMax(x, y) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 158-160 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 161-162 And .. GENERATED FROM PYTHON SOURCE LINES 162-165 .. code-block:: Python z = x & y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 166-168 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 169-172 .. code-block:: Python z = x & 0 z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 173-175 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 176-177 Or .. GENERATED FROM PYTHON SOURCE LINES 179-182 .. code-block:: Python z = x | y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 183-185 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: Python z = ex.Numeric(0) | ex.Numeric(0) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 190-192 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 193-194 Equal .. GENERATED FROM PYTHON SOURCE LINES 194-197 .. code-block:: Python z = x == y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 198-200 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 201-204 .. code-block:: Python z = (x + 1) == y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 205-207 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 208-209 Not equal .. GENERATED FROM PYTHON SOURCE LINES 209-212 .. code-block:: Python z = x != y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 213-215 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 216-219 .. code-block:: Python z = (x + 1) != y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 220-222 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 223-224 Lesser or equal .. GENERATED FROM PYTHON SOURCE LINES 224-227 .. code-block:: Python z = x <= y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 228-230 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 231-232 Greater or equal .. GENERATED FROM PYTHON SOURCE LINES 232-235 .. code-block:: Python z = x >= y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 236-238 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 239-240 Lesser than .. GENERATED FROM PYTHON SOURCE LINES 240-243 .. code-block:: Python z = x < y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 244-246 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 247-248 Greater than .. GENERATED FROM PYTHON SOURCE LINES 248-251 .. code-block:: Python z = x > y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 252-254 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 255-256 Opposite .. GENERATED FROM PYTHON SOURCE LINES 256-259 .. code-block:: Python z = -x z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none -2 .. GENERATED FROM PYTHON SOURCE LINES 260-262 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -2.0 .. GENERATED FROM PYTHON SOURCE LINES 263-264 Sum of multiples expressions .. GENERATED FROM PYTHON SOURCE LINES 264-268 .. code-block:: Python listOfExpressions = [x, y, 1 + x, 1 + y] z = ex.bioMultSum(listOfExpressions) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 269-271 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 272-274 The result is the same as the following, but it implements the sum in a more efficient way. .. GENERATED FROM PYTHON SOURCE LINES 274-277 .. code-block:: Python z = x + y + 1 + x + 1 + y z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 278-280 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 12.0 .. GENERATED FROM PYTHON SOURCE LINES 281-284 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 284-286 .. code-block:: Python my_dict = {1: ex.exp(-1), 2: ex.log(1.2), 3: 1234} .. GENERATED FROM PYTHON SOURCE LINES 287-290 .. code-block:: Python index = x index.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 291-294 .. code-block:: Python z = ex.Elem(my_dict, index) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.1823215567939546) .. GENERATED FROM PYTHON SOURCE LINES 295-297 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.1823215567939546 .. GENERATED FROM PYTHON SOURCE LINES 298-301 .. code-block:: Python index = x - 1 index.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 302-305 .. code-block:: Python z = ex.Elem(my_dict, index) z.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.36787944117144233) .. GENERATED FROM PYTHON SOURCE LINES 306-308 .. code-block:: Python z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.36787944117144233 .. GENERATED FROM PYTHON SOURCE LINES 309-312 .. code-block:: Python index = x - 2 index.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 313-315 If the value returned as index does not corresponds to an entry in the dictionary, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 317-323 .. code-block:: Python z = ex.Elem(my_dict, index) try: z.get_value() 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 324-330 .. code-block:: Python z = ex.Elem(my_dict, index) try: z.get_value_c(prepare_ids=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 331-337 Complex expressions ------------------- When an expression is deemed complex in Biogeme, the `get_value` function is not available. Only the `get_value_c` function must be used. It calculates the expressions using a C++ implementation of the expression. .. GENERATED FROM PYTHON SOURCE LINES 339-343 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 343-346 .. code-block:: Python z = ex.bioNormalCdf(x) z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9772498680518218 .. GENERATED FROM PYTHON SOURCE LINES 347-350 .. code-block:: Python z = ex.bioNormalCdf(0) z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5 .. GENERATED FROM PYTHON SOURCE LINES 351-352 Derivative .. GENERATED FROM PYTHON SOURCE LINES 352-354 .. code-block:: Python z = 30 * x + 20 * y .. GENERATED FROM PYTHON SOURCE LINES 355-358 .. code-block:: Python zx = ex.Derive(z, 'x') zx.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 30.0 .. GENERATED FROM PYTHON SOURCE LINES 359-362 .. code-block:: Python zx = ex.Derive(z, 'y') zx.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 20.0 .. GENERATED FROM PYTHON SOURCE LINES 363-367 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 367-372 .. code-block:: Python omega = ex.RandomVariable('omega') pdf = ex.exp(-omega * omega / 2) z = ex.Integrate(pdf, 'omega') / np.sqrt(2 * np.pi) z.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 373-386 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 386-394 .. code-block:: Python 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.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.3333323120662822 .. GENERATED FROM PYTHON SOURCE LINES 395-396 Expressions using a database .. GENERATED FROM PYTHON SOURCE LINES 396-410 .. code-block:: Python 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 411-412 Linear utility: it defines a linear conbinations of parameters are variables. .. GENERATED FROM PYTHON SOURCE LINES 412-417 .. code-block:: Python 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 418-425 .. code-block:: Python list_of_terms = [ LinearTermTuple(beta=beta1, x=v1), LinearTermTuple(beta=beta2, x=v2), ] z = ex.bioLinearUtility(list_of_terms) z.get_value_c(database=my_data, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 2100., 4200., 6300., 8400., 10500.]) .. GENERATED FROM PYTHON SOURCE LINES 426-427 It is equivalent to the following, but implemented in a more efficient way. .. GENERATED FROM PYTHON SOURCE LINES 427-430 .. code-block:: Python z = beta1 * v1 + beta2 * v2 z.get_value_c(database=my_data, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 2100., 4200., 6300., 8400., 10500.]) .. GENERATED FROM PYTHON SOURCE LINES 431-437 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 437-441 .. code-block:: Python draws = ex.bioDraws('draws', 'UNIFORM') z = ex.MonteCarlo(draws * draws) z.get_value_c(database=my_data, prepare_ids=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 442-443 Panel Trajectory: we first calculate a quantity for each entry in the database. .. GENERATED FROM PYTHON SOURCE LINES 443-448 .. code-block:: Python v1 = ex.Variable('Variable1') v2 = ex.Variable('Variable2') p = v1 / (v1 + v2) p.get_value_c(database=my_data, prepare_ids=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 449-455 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 455-457 .. code-block:: Python my_data.panel('Person') .. GENERATED FROM PYTHON SOURCE LINES 458-459 In this case, we expect the following for individual 1: .. GENERATED FROM PYTHON SOURCE LINES 459-461 .. code-block:: Python 0.09090909**3 .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0007513147783621339 .. GENERATED FROM PYTHON SOURCE LINES 462-463 And the following for individual 2: .. GENERATED FROM PYTHON SOURCE LINES 463-465 .. code-block:: Python 0.09090909**2 .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0082644626446281 .. GENERATED FROM PYTHON SOURCE LINES 466-467 We verify that it is indeed the case: .. GENERATED FROM PYTHON SOURCE LINES 467-471 .. code-block:: Python z = ex.PanelLikelihoodTrajectory(p) z.get_value_c(database=my_data, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.00075131, 0.00826446]) .. GENERATED FROM PYTHON SOURCE LINES 472-478 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 480-482 .. code-block:: Python NUMBER_OF_DRAWS = 100 .. GENERATED FROM PYTHON SOURCE LINES 483-484 We first create a small database .. GENERATED FROM PYTHON SOURCE LINES 484-498 .. code-block:: Python 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 499-501 .. code-block:: Python my_data = db.Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 502-504 The following type of expression is a literal called Variable that corresponds to an entry in the database. .. GENERATED FROM PYTHON SOURCE LINES 504-512 .. code-block:: Python 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 513-515 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 515-518 .. code-block:: Python newvar_b = my_data.define_variable('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 519-520 It is equivalent to the following Pandas statement. .. GENERATED FROM PYTHON SOURCE LINES 520-523 .. code-block:: Python 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 524-527 **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 527-533 .. code-block:: Python try: my_expression = 200 <= Variable2 <= 400 except excep.BiogemeError as e: print(e) .. rst-class:: sphx-glr-script-out .. code-block:: none Expression (Variable2 >= `200.0`) cannot be used in a boolean expression. Use & for "and" and | for "or" .. GENERATED FROM PYTHON SOURCE LINES 534-539 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 539-542 .. code-block:: Python 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 543-546 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 546-550 .. code-block:: Python beta1 = ex.Beta('beta1', 0.2, None, None, 0) beta2 = ex.Beta('beta2', 0.4, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 551-553 The last argument allows to fix the value of the parameter to the value. .. GENERATED FROM PYTHON SOURCE LINES 553-556 .. code-block:: Python beta3 = ex.Beta('beta3', 1, None, None, 1) beta4 = ex.Beta('beta4', 0, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 557-559 Arithmetic operators are overloaded to allow standard manipulations of expressions. .. GENERATED FROM PYTHON SOURCE LINES 559-562 .. code-block:: Python expr0 = beta3 + beta4 print(expr0) .. rst-class:: sphx-glr-script-out .. code-block:: none (Beta('beta3', 1, None, None, 1) + Beta('beta4', 0, None, None, 1)) .. GENERATED FROM PYTHON SOURCE LINES 563-566 The evaluation of expressions can be done in two ways. For simple expressions, the fonction `get_value`, implemented in Python, returns the value of the expression. .. GENERATED FROM PYTHON SOURCE LINES 566-568 .. code-block:: Python expr0.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 569-570 It is possible to modify the values of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 570-574 .. code-block:: Python newvalues = {'beta1': 1, 'beta2': 2, 'beta3': 3, 'beta4': 2} expr0.change_init_values(newvalues) expr0.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none [WARNING] 2024-08-05 19:59:53,816 Parameter beta3 is fixed, but its value is changed from 1 to 3. [WARNING] 2024-08-05 19:59:53,817 Parameter beta4 is fixed, but its value is changed from 0 to 2. 5 .. GENERATED FROM PYTHON SOURCE LINES 575-582 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 582-588 .. code-block:: Python 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, None, None, 0)) - (exp((-Beta('beta2', 0.4, None, None, 0))) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1)))))) .. GENERATED FROM PYTHON SOURCE LINES 589-592 The function `get_value_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 592-594 .. code-block:: Python expr1.get_value_c(prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none -1.275800115089098 .. GENERATED FROM PYTHON SOURCE LINES 595-597 It actually calls the function `get_value_and_derivatives`, and returns its first output (without calculating the derivatives). .. GENERATED FROM PYTHON SOURCE LINES 597-599 .. code-block:: Python the_output = expr1.get_value_and_derivatives(prepare_ids=True) .. GENERATED FROM PYTHON SOURCE LINES 600-602 .. code-block:: Python the_output.function .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(-1.275800115089098) .. GENERATED FROM PYTHON SOURCE LINES 603-604 We create a pandas DataFrame just to have a nicer display of the results. .. GENERATED FROM PYTHON SOURCE LINES 604-606 .. code-block:: Python pd.DataFrame(the_output.gradient) .. raw:: html
0
0 2.0000
1 5.8653


.. GENERATED FROM PYTHON SOURCE LINES 607-609 .. code-block:: Python pd.DataFrame(the_output.hessian) .. raw:: html
0 1
0 0.0 0.000000
1 0.0 -31.002302


.. GENERATED FROM PYTHON SOURCE LINES 610-612 .. code-block:: Python pd.DataFrame(the_output.bhhh) .. raw:: html
0 1
0 4.000000 11.730601
1 11.730601 34.401749


.. GENERATED FROM PYTHON SOURCE LINES 613-614 Note that the BHHH matrix is the outer product of the gradient with itself. .. GENERATED FROM PYTHON SOURCE LINES 614-616 .. code-block:: Python pd.DataFrame(np.outer(the_output.gradient, the_output.gradient)) .. raw:: html
0 1
0 4.000000 11.730601
1 11.730601 34.401749


.. GENERATED FROM PYTHON SOURCE LINES 617-619 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 619-623 .. code-block:: Python expr1.get_value_and_derivatives( gradient=True, hessian=False, bhhh=False, prepare_ids=True ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 624-628 .. code-block:: Python the_output: NamedBiogemeFunctionOutput = expr1.get_value_and_derivatives( prepare_ids=True, named_results=True ) .. GENERATED FROM PYTHON SOURCE LINES 629-631 .. code-block:: Python print(the_output.gradient) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': np.float64(2.0), 'beta2': np.float64(5.865300402811843)} .. GENERATED FROM PYTHON SOURCE LINES 632-634 .. code-block:: Python print(the_output.hessian) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-31.002302129148312)}} .. GENERATED FROM PYTHON SOURCE LINES 635-637 .. code-block:: Python print(the_output.bhhh) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(11.730600805623686)}, 'beta2': {'beta1': np.float64(11.730600805623686), 'beta2': np.float64(34.401748815224764)}} .. GENERATED FROM PYTHON SOURCE LINES 638-642 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 642-644 .. code-block:: Python the_function = expr1.create_function() .. GENERATED FROM PYTHON SOURCE LINES 645-646 We evaluate it at one point... .. GENERATED FROM PYTHON SOURCE LINES 646-648 .. code-block:: Python the_function([1, 2]) .. GENERATED FROM PYTHON SOURCE LINES 649-650 ... and at another point. .. GENERATED FROM PYTHON SOURCE LINES 650-652 .. code-block:: Python the_function([10, -2]) .. GENERATED FROM PYTHON SOURCE LINES 653-654 We can use it to check the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 654-656 .. code-block:: Python biogeme.tools.derivatives.check_derivatives(the_function, np.array([1, 2]), logg=True) .. rst-class:: sphx-glr-script-out .. code-block:: none [INFO] 2024-08-05 19:59:53,821 x Gradient FinDiff Difference [INFO] 2024-08-05 19:59:53,821 x[0] +2.000000E+00 +2.000000E+00 -1.167734E-09 [INFO] 2024-08-05 19:59:53,822 x[1] +1.015015E-01 +1.015014E-01 +1.629049E-08 [INFO] 2024-08-05 19:59:53,822 Row Col Hessian FinDiff Difference [INFO] 2024-08-05 19:59:53,822 x[0] x[0] +0.000000E+00 +0.000000E+00 +0.000000E+00 [INFO] 2024-08-05 19:59:53,823 x[0] x[1] +0.000000E+00 +0.000000E+00 +0.000000E+00 [INFO] 2024-08-05 19:59:53,823 x[1] x[0] +0.000000E+00 +0.000000E+00 +0.000000E+00 [INFO] 2024-08-05 19:59:53,823 x[1] x[1] -1.691691E-01 -1.691691E-01 -3.203118E-08 (np.float64(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 657-658 And it is possible to also obtain the BHHH matrix. .. GENERATED FROM PYTHON SOURCE LINES 658-661 .. code-block:: Python the_function = expr1.create_function(bhhh=True) the_function([1, 2]) .. GENERATED FROM PYTHON SOURCE LINES 662-666 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 666-671 .. code-block:: Python results: BiogemeDisaggregateFunctionOutput = expr1.get_value_and_derivatives( database=my_data, aggregation=False ) print(len(results.functions)) .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 672-684 .. code-block:: Python f_array = results.functions g_array = results.gradients h_array = results.hessians bhhh_array = results.bhhhs 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=np.float64(1.9323323583816936) g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=np.float64(1.9323323583816936) g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=np.float64(1.9323323583816936) g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=np.float64(1.9323323583816936) g=array([2. , 0.10150146]) h=array([[ 0. , 0. ], [ 0. , -0.1691691]]) bhhh=array([[4. , 0.20300292], [0.20300292, 0.01030255]]) ****** f=np.float64(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 685-691 .. code-block:: Python results: NamedBiogemeDisaggregateFunctionOutput = expr1.get_value_and_derivatives( database=my_data, aggregation=False, named_results=True ) print(len(results.functions)) .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 692-704 .. code-block:: Python f_array = results.functions g_array = results.gradients h_array = results.hessians bhhh_array = results.bhhhs 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=np.float64(1.9323323583816936) g={'beta1': np.float64(2.0), 'beta2': np.float64(0.10150146242745953)} h={'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-0.16916910404576588)}} bhhh={'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(0.20300292485491905)}, 'beta2': {'beta1': np.float64(0.20300292485491905), 'beta2': np.float64(0.010302546874912978)}} ****** f=np.float64(1.9323323583816936) g={'beta1': np.float64(2.0), 'beta2': np.float64(0.10150146242745953)} h={'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-0.16916910404576588)}} bhhh={'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(0.20300292485491905)}, 'beta2': {'beta1': np.float64(0.20300292485491905), 'beta2': np.float64(0.010302546874912978)}} ****** f=np.float64(1.9323323583816936) g={'beta1': np.float64(2.0), 'beta2': np.float64(0.10150146242745953)} h={'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-0.16916910404576588)}} bhhh={'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(0.20300292485491905)}, 'beta2': {'beta1': np.float64(0.20300292485491905), 'beta2': np.float64(0.010302546874912978)}} ****** f=np.float64(1.9323323583816936) g={'beta1': np.float64(2.0), 'beta2': np.float64(0.10150146242745953)} h={'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-0.16916910404576588)}} bhhh={'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(0.20300292485491905)}, 'beta2': {'beta1': np.float64(0.20300292485491905), 'beta2': np.float64(0.010302546874912978)}} ****** f=np.float64(1.9323323583816936) g={'beta1': np.float64(2.0), 'beta2': np.float64(0.10150146242745953)} h={'beta1': {'beta1': np.float64(0.0), 'beta2': np.float64(0.0)}, 'beta2': {'beta1': np.float64(0.0), 'beta2': np.float64(-0.16916910404576588)}} bhhh={'beta1': {'beta1': np.float64(4.0), 'beta2': np.float64(0.20300292485491905)}, 'beta2': {'beta1': np.float64(0.20300292485491905), 'beta2': np.float64(0.010302546874912978)}} .. GENERATED FROM PYTHON SOURCE LINES 705-706 If `aggregation` is set to `True`, the results are accumulated as a sum. .. GENERATED FROM PYTHON SOURCE LINES 706-715 .. code-block:: Python the_output: BiogemeFunctionOutput = expr1.get_value_and_derivatives( database=my_data, aggregation=True ) print(f'{the_output.function=}') print(f'{the_output.gradient=}') print(f'{the_output.hessian=}') print(f'{the_output.bhhh=}') print('B3') .. rst-class:: sphx-glr-script-out .. code-block:: none the_output.function=np.float64(9.661661791908468) the_output.gradient=array([10. , 0.50750731]) the_output.hessian=array([[ 0. , 0. ], [ 0. , -0.84584552]]) the_output.bhhh=array([[20. , 1.01501462], [ 1.01501462, 0.05151273]]) B3 .. GENERATED FROM PYTHON SOURCE LINES 716-718 The following function scans the expression and extracts a dict with all free parameters. .. GENERATED FROM PYTHON SOURCE LINES 718-720 .. code-block:: Python expr1.set_of_elementary_expression(TypeOfElementaryExpression.FREE_BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1', 'beta2'} .. GENERATED FROM PYTHON SOURCE LINES 721-722 Options can be set to extract free parameters, fixed parameters, or both. .. GENERATED FROM PYTHON SOURCE LINES 722-724 .. code-block:: Python expr1.set_of_elementary_expression(TypeOfElementaryExpression.FIXED_BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta4', 'beta3'} .. GENERATED FROM PYTHON SOURCE LINES 725-727 .. code-block:: Python expr1.set_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta4', 'beta3', 'beta1', 'beta2'} .. GENERATED FROM PYTHON SOURCE LINES 728-729 It is possible also to extract an elementary expression from its name. .. GENERATED FROM PYTHON SOURCE LINES 729-731 .. code-block:: Python expr1.get_elementary_expression('beta2') .. rst-class:: sphx-glr-script-out .. code-block:: none Beta('beta2', 0.4, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 732-741 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 741-748 .. code-block:: Python expr2 = 2 * beta1 * Variable1 - ex.exp(-beta2 * Variable2) / ( beta2 * (beta3 >= beta4) + beta1 * (beta3 < beta4) ) print(expr2) print('B4') .. rst-class:: sphx-glr-script-out .. code-block:: none (((`2.0` * Beta('beta1', 0.2, None, None, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, None, None, 0)) * Variable2)) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1)))))) B4 .. GENERATED FROM PYTHON SOURCE LINES 749-752 It is not a simple expression anymore, and only the function `get_value_c` can be invoked. If we try the `get_value` function, it raises an exception. .. GENERATED FROM PYTHON SOURCE LINES 752-757 .. code-block:: Python try: expr2.get_value() except excep.BiogemeError as e: print(f'Exception raised: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: getValue method undefined at this level: . Each expression must implement it. .. GENERATED FROM PYTHON SOURCE LINES 758-763 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 763-769 .. code-block:: Python try: expr2.get_value_c(prepare_ids=True) except excep.BiogemeError as e: print(f'Exception raised: {e}') print('B5') .. rst-class:: sphx-glr-script-out .. code-block:: none Exception raised: No database is provided and an expression contains variables: {'Variable2', 'Variable1'} B5 .. GENERATED FROM PYTHON SOURCE LINES 770-772 .. code-block:: Python expr2.get_value_c(database=my_data, aggregation=False, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 4., 8., 12., 16., 20.]) .. GENERATED FROM PYTHON SOURCE LINES 773-775 The following function extracts the names of the parameters apprearing in the expression. .. GENERATED FROM PYTHON SOURCE LINES 775-777 .. code-block:: Python expr2.set_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta4', 'beta3', 'beta1', 'beta2'} .. GENERATED FROM PYTHON SOURCE LINES 778-779 The list of parameters can also be obtained in the form of a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 779-781 .. code-block:: Python expr2.dict_of_elementary_expression(TypeOfElementaryExpression.BETA) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': Beta('beta1', 0.2, None, None, 0), 'beta2': Beta('beta2', 0.4, None, None, 0), 'beta3': Beta('beta3', 3, None, None, 1), 'beta4': Beta('beta4', 2, None, None, 1)} .. GENERATED FROM PYTHON SOURCE LINES 782-783 The list of variables can also be obtained in the form of a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 783-785 .. code-block:: Python 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 786-787 or a set... .. GENERATED FROM PYTHON SOURCE LINES 787-789 .. code-block:: Python expr2.set_of_elementary_expression(TypeOfElementaryExpression.VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Variable2', 'Variable1'} .. GENERATED FROM PYTHON SOURCE LINES 790-793 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 793-795 .. code-block:: Python expr2.get_class_name() .. rst-class:: sphx-glr-script-out .. code-block:: none 'Minus' .. GENERATED FROM PYTHON SOURCE LINES 796-800 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 802-804 As the expression is used out of a specific context, it must be prepared before using it. .. GENERATED FROM PYTHON SOURCE LINES 804-808 .. code-block:: Python expr2.prepare(database=my_data, number_of_draws=0) expr2.get_status_id_manager() print(expr2) .. rst-class:: sphx-glr-script-out .. code-block:: none (((`2.0` * Beta('beta1', 0.2, None, None, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, None, None, 0)) * Variable2)) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1)))))) .. GENERATED FROM PYTHON SOURCE LINES 809-811 .. code-block:: Python expr2.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4907792336},2.0', b'{4852715824}"beta1"[0],0,0', b'{4907791952}(2),4907792336,4852715824', b'{5158109008}"Variable1",6,2', b'{4907784416}(2),4907791952,5158109008', b'{4852723072}"beta2"[0],1,1', b'{4907790896}(1),4852723072', b'{5158099264}"Variable2",7,3', b'{4907783984}(2),4907790896,5158099264', b'{4907786432}(1),4907783984', b'{4852723072}"beta2"[0],1,1', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4907790608}(2),4852723504,4852716160', b'{4907791520}(2),4852723072,4907790608', b'{4852715824}"beta1"[0],0,0', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4907791616}(2),4852723504,4852716160', b'{4907794208}(2),4852715824,4907791616', b'{4907782832}(2),4907791520,4907794208', b'{4907792912}(2),4907786432,4907782832', b'{4907780432}(2),4907784416,4907792912'] .. GENERATED FROM PYTHON SOURCE LINES 812-824 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 824-828 .. code-block:: Python collection_of_formulas = [expr1, expr2] formulas = IdManager(collection_of_formulas, my_data, None) .. GENERATED FROM PYTHON SOURCE LINES 829-830 Unique numbering for all elementary expressions. .. GENERATED FROM PYTHON SOURCE LINES 830-832 .. code-block:: Python 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 833-835 .. code-block:: Python formulas.free_betas .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={'beta1': Beta('beta1', 0.2, None, None, 0), 'beta2': Beta('beta2', 0.4, None, None, 0)}, indices={'beta1': 0, 'beta2': 1}, names=['beta1', 'beta2']) .. GENERATED FROM PYTHON SOURCE LINES 836-838 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 838-840 .. code-block:: Python [(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 841-843 .. code-block:: Python formulas.free_betas.names .. rst-class:: sphx-glr-script-out .. code-block:: none ['beta1', 'beta2'] .. GENERATED FROM PYTHON SOURCE LINES 844-846 .. code-block:: Python formulas.fixed_betas .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={'beta3': Beta('beta3', 3, None, None, 1), 'beta4': Beta('beta4', 2, None, None, 1)}, indices={'beta3': 0, 'beta4': 1}, names=['beta3', 'beta4']) .. GENERATED FROM PYTHON SOURCE LINES 847-849 .. code-block:: Python [(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 850-852 .. code-block:: Python formulas.fixed_betas.names .. rst-class:: sphx-glr-script-out .. code-block:: none ['beta3', 'beta4'] .. GENERATED FROM PYTHON SOURCE LINES 853-855 .. code-block:: Python formulas.random_variables .. rst-class:: sphx-glr-script-out .. code-block:: none ElementsTuple(expressions={}, indices={}, names=[]) .. GENERATED FROM PYTHON SOURCE LINES 856-857 Monte Carlo integration is based on draws. .. GENERATED FROM PYTHON SOURCE LINES 857-861 .. code-block:: Python 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 862-864 Note that draws are different from random variables, used for numerical integration. .. GENERATED FROM PYTHON SOURCE LINES 864-866 .. code-block:: Python expr3.set_of_elementary_expression(TypeOfElementaryExpression.RANDOM_VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 867-868 The following function reports the draws involved in an expression. .. GENERATED FROM PYTHON SOURCE LINES 868-870 .. code-block:: Python expr3.set_of_elementary_expression(TypeOfElementaryExpression.DRAWS) .. rst-class:: sphx-glr-script-out .. code-block:: none {'my_draws'} .. GENERATED FROM PYTHON SOURCE LINES 871-873 The following function checks if draws are defined outside MonteCarlo, and return their names. .. GENERATED FROM PYTHON SOURCE LINES 873-876 .. code-block:: Python 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 877-878 Checking the correct expression returns an empty set. .. GENERATED FROM PYTHON SOURCE LINES 878-880 .. code-block:: Python expr3.check_draws() .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 881-882 The expression is a Monte-Carlo integration. .. GENERATED FROM PYTHON SOURCE LINES 882-884 .. code-block:: Python expr3.get_class_name() .. rst-class:: sphx-glr-script-out .. code-block:: none 'MonteCarlo' .. GENERATED FROM PYTHON SOURCE LINES 885-888 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 888-893 .. code-block:: Python try: expr3.get_value_c(number_of_draws=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 894-897 Here is its value. It is an approximation of .. math:: \int_0^1 x^2 dx=\frac{1}{3}. .. GENERATED FROM PYTHON SOURCE LINES 897-899 .. code-block:: Python expr3.get_value_c(database=my_data, number_of_draws=NUMBER_OF_DRAWS, prepare_ids=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 900-901 Here is its signature. .. GENERATED FROM PYTHON SOURCE LINES 901-904 .. code-block:: Python expr3.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) expr3.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4907793248}"my_draws",0,0', b'{4907793248}"my_draws",0,0', b'{4907793872}(2),4907793248,4907793248', b'{4907779760}(1),4907793872'] .. GENERATED FROM PYTHON SOURCE LINES 905-907 The same integral can be calculated using numerical integration, declaring a random variable. .. GENERATED FROM PYTHON SOURCE LINES 907-909 .. code-block:: Python omega = ex.RandomVariable('omega') .. GENERATED FROM PYTHON SOURCE LINES 910-913 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 913-920 .. code-block:: Python 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 921-922 In this case, omega is a random variable. .. GENERATED FROM PYTHON SOURCE LINES 922-924 .. code-block:: Python expr4.dict_of_elementary_expression(TypeOfElementaryExpression.RANDOM_VARIABLE) .. rst-class:: sphx-glr-script-out .. code-block:: none {'omega': omega} .. GENERATED FROM PYTHON SOURCE LINES 925-927 .. code-block:: Python 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 928-930 The following function checks if random variables are defined outside an Integrate statement. .. GENERATED FROM PYTHON SOURCE LINES 930-933 .. code-block:: Python wrong_expression = x * x wrong_expression.check_rv() .. rst-class:: sphx-glr-script-out .. code-block:: none {'omega'} .. GENERATED FROM PYTHON SOURCE LINES 934-936 The same function called from the correct expression returns an empty set. .. GENERATED FROM PYTHON SOURCE LINES 936-938 .. code-block:: Python expr4.check_rv() .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 939-940 Calculating its value requires the C++ implementation. .. GENERATED FROM PYTHON SOURCE LINES 940-942 .. code-block:: Python expr4.get_value_c(my_data, prepare_ids=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 943-958 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 958-962 .. code-block:: Python 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, None, None, 0)) - (exp((-Beta('beta2', 0.4, None, None, 0))) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1)))))), 2:(((`2.0` * Beta('beta1', 0.2, None, None, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, None, None, 0)) * Variable2)) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1))))))}[Person] / `10.0`) .. GENERATED FROM PYTHON SOURCE LINES 963-965 .. code-block:: Python 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 966-970 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 970-975 .. code-block:: Python try: expr5.get_value_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 prepare_ids to True. .. GENERATED FROM PYTHON SOURCE LINES 976-978 .. code-block:: Python expr5.get_value_c(database=my_data, prepare_ids=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 979-981 .. code-block:: Python testElem = ex.MonteCarlo(ex.Elem({1: my_draws * my_draws}, 1)) .. GENERATED FROM PYTHON SOURCE LINES 982-984 .. code-block:: Python testElem.audit() .. rst-class:: sphx-glr-script-out .. code-block:: none ([], []) .. GENERATED FROM PYTHON SOURCE LINES 985-987 The next expression is simply the sum of multiple expressions. The argument is a list of expressions. .. GENERATED FROM PYTHON SOURCE LINES 987-990 .. code-block:: Python expr6 = ex.bioMultSum([expr1, expr2, expr4]) print(expr6) .. rst-class:: sphx-glr-script-out .. code-block:: none bioMultSum(((`2.0` * Beta('beta1', 0.2, None, None, 0)) - (exp((-Beta('beta2', 0.4, None, None, 0))) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 1)))))), (((`2.0` * Beta('beta1', 0.2, None, None, 0)) * Variable1) - (exp(((-Beta('beta2', 0.4, None, None, 0)) * Variable2)) / ((Beta('beta2', 0.4, None, None, 0) * (Beta('beta3', 3, None, None, 1) >= Beta('beta4', 2, None, None, 1))) + (Beta('beta1', 0.2, None, None, 0) * (Beta('beta3', 3, None, None, 1) < Beta('beta4', 2, None, None, 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 991-993 .. code-block:: Python expr6.get_value_c(database=my_data, number_of_draws=NUMBER_OF_DRAWS, prepare_ids=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 994-1000 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 1000-1004 .. code-block:: Python 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 1005-1007 .. code-block:: Python expr7.get_value() .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(-1.2362866960692134) .. GENERATED FROM PYTHON SOURCE LINES 1008-1009 If the alternative is not in the choice set, an exception is raised. .. GENERATED FROM PYTHON SOURCE LINES 1009-1015 .. code-block:: Python expr7_wrong = ex.LogLogit(V, av, 3) try: expr7_wrong.get_value() 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 1016-1018 It is actually better to use the C++ implementation, available in the module models. .. GENERATED FROM PYTHON SOURCE LINES 1020-1023 .. code-block:: Python expr8 = models.loglogit(V, av, 1) expr8.get_value_c(database=my_data, prepare_ids=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 1024-1026 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 1026-1029 .. code-block:: Python for v in V.values(): print(v.get_value_c(database=my_data, prepare_ids=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 1030-1038 .. code-block:: Python logsum = np.log( np.sum( [np.exp(v.get_value_c(database=my_data, prepare_ids=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 1039-1043 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 1043-1046 .. code-block:: Python expr9 = ex.Derive(expr8, 'beta2') expr9.get_value_c(database=my_data, prepare_ids=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 1047-1049 .. code-block:: Python expr9.elementaryName .. rst-class:: sphx-glr-script-out .. code-block:: none 'beta2' .. GENERATED FROM PYTHON SOURCE LINES 1050-1057 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 1057-1060 .. code-block:: Python expr10 = ex.bioNormalCdf(Variable1 / 10 - 1) expr10.get_value_c(database=my_data, prepare_ids=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 1061-1063 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 1063-1066 .. code-block:: Python expr11 = ex.bioMin(expr5, expr10) expr11.get_value_c(database=my_data, prepare_ids=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 1067-1070 .. code-block:: Python expr12 = ex.bioMax(expr5, expr10) expr12.get_value_c(database=my_data, prepare_ids=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 1071-1074 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 1074-1082 .. code-block:: Python terms = [ LinearTermTuple(beta=beta1, x=ex.Variable('Variable1')), LinearTermTuple(beta=beta2, x=ex.Variable('Variable2')), LinearTermTuple(beta=beta3, x=ex.Variable('newvar_b')), ] expr13 = ex.bioLinearUtility(terms) expr13.get_value_c(database=my_data, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 372., 744., 1116., 1488., 1860.]) .. GENERATED FROM PYTHON SOURCE LINES 1083-1086 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 1086-1089 .. code-block:: Python expr13bis = beta1 * Variable1 + beta2 * Variable2 + beta3 * newvar_b expr13bis.get_value_c(database=my_data, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 372., 744., 1116., 1488., 1860.]) .. GENERATED FROM PYTHON SOURCE LINES 1090-1091 A Pythonic way to write a linear utility function. .. GENERATED FROM PYTHON SOURCE LINES 1091-1100 .. code-block:: Python variables = ['v1', 'v2', 'v3', 'cost', 'time', 'headway'] coefficients = { f'{v}': biogeme.expressions.beta_parameters.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, None, None, 0) * v1)) + (Beta('beta_v2', 0, None, None, 0) * v2)) + (Beta('beta_v3', 0, None, None, 0) * v3)) + (Beta('beta_cost', 0, None, None, 0) * cost)) + (Beta('beta_time', 0, None, None, 0) * time)) + (Beta('beta_headway', 0, None, None, 0) * headway)) .. GENERATED FROM PYTHON SOURCE LINES 1101-1106 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 1108-1109 Our database contains 5 observations. .. GENERATED FROM PYTHON SOURCE LINES 1109-1111 .. code-block:: Python my_data.get_sample_size() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 1112-1114 .. code-block:: Python my_data.panel('Person') .. GENERATED FROM PYTHON SOURCE LINES 1115-1119 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 1121-1123 .. code-block:: Python my_data.get_sample_size() .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 1124-1126 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 1126-1131 .. code-block:: Python try: expr3.get_value_c(database=my_data) except excep.BiogemeError as e: print(f'Exception: {e}') .. rst-class:: sphx-glr-script-out .. code-block:: none [WARNING] 2024-08-05 19:59:53,843 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 1132-1134 This is detected by the `audit` function, called before the expression is evaluated. .. GENERATED FROM PYTHON SOURCE LINES 1134-1136 .. code-block:: Python 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 1137-1138 We now evaluate an expression for panel data. .. GENERATED FROM PYTHON SOURCE LINES 1138-1155 .. code-block:: Python c1 = ex.bioDraws('draws1', 'NORMAL_HALTON2') c2 = ex.bioDraws('draws2', 'NORMAL_HALTON2') U1 = ( biogeme.expressions.beta_parameters.Beta('beta1', 0, None, None, 0) * Variable1 + 10 * c1 ) U2 = ( biogeme.expressions.beta_parameters.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 1156-1159 .. code-block:: Python expr14.prepare(database=my_data, number_of_draws=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, None, None, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0, None, None, 0) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1160-1162 .. code-block:: Python expr14.get_value_c(database=my_data, number_of_draws=NUMBER_OF_DRAWS, prepare_ids=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-3.91914292, -2.11209896]) .. GENERATED FROM PYTHON SOURCE LINES 1163-1171 .. code-block:: Python expr14.get_value_and_derivatives( database=my_data, number_of_draws=NUMBER_OF_DRAWS, gradient=True, hessian=True, aggregation=False, ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 1172-1180 .. code-block:: Python expr14.get_value_and_derivatives( database=my_data, number_of_draws=NUMBER_OF_DRAWS, gradient=True, hessian=True, aggregation=True, ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 1181-1184 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 1186-1190 .. code-block:: Python the_function = expr14.create_function( database=my_data, number_of_draws=NUMBER_OF_DRAWS, gradient=True, hessian=True ) .. GENERATED FROM PYTHON SOURCE LINES 1191-1193 .. code-block:: Python the_function([0, 0]) .. GENERATED FROM PYTHON SOURCE LINES 1194-1196 .. code-block:: Python the_function([0.1, 0.1]) .. GENERATED FROM PYTHON SOURCE LINES 1197-1198 It is possible to fix the value of some (or all) Beta parameters .. GENERATED FROM PYTHON SOURCE LINES 1198-1200 .. code-block:: Python print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, None, None, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0, None, None, 0) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1201-1203 .. code-block:: Python expr14.fix_betas({'beta2': 0.123}) .. GENERATED FROM PYTHON SOURCE LINES 1204-1206 .. code-block:: Python print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, None, None, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('beta2', 0.123, None, None, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1207-1208 The name of the parameter can also be changed while fixing its value. .. GENERATED FROM PYTHON SOURCE LINES 1210-1212 .. code-block:: Python expr14.fix_betas({'beta2': 123}, prefix='prefix_', suffix='_suffix') .. GENERATED FROM PYTHON SOURCE LINES 1213-1215 .. code-block:: Python print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, None, None, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('prefix_beta2_suffix', 123, None, None, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1216-1217 It can also be renamed using the following function. .. GENERATED FROM PYTHON SOURCE LINES 1219-1221 .. code-block:: Python expr14.rename_elementary(['prefix_beta2_suffix'], prefix='PREFIX_', suffix='_SUFFIX') .. GENERATED FROM PYTHON SOURCE LINES 1222-1224 .. code-block:: Python print(expr14) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(PanelLikelihoodTrajectory(exp(_bioLogLogit[choice=Choice]U=(1:((Beta('beta1', 0, None, None, 0) * Variable1) + (`10.0` * bioDraws("draws1", "NORMAL_HALTON2"))), 2:((Beta('PREFIX_prefix_beta2_suffix_SUFFIX', 123, None, None, 1) * Variable2) + (`10.0` * bioDraws("draws2", "NORMAL_HALTON2"))), 3:`0.0`)av=(1:Av1, 2:Av2, 3:Av3))))) .. GENERATED FROM PYTHON SOURCE LINES 1225-1227 Signatures ---------- .. GENERATED FROM PYTHON SOURCE LINES 1229-1233 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 1235-1237 .. code-block:: Python id(expr1) .. rst-class:: sphx-glr-script-out .. code-block:: none 4852727728 .. GENERATED FROM PYTHON SOURCE LINES 1238-1240 Numerical expression ++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1242-1243 {identifier},0.0 .. GENERATED FROM PYTHON SOURCE LINES 1243-1245 .. code-block:: Python ex.Numeric(0).get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5158946768},0.0'] .. GENERATED FROM PYTHON SOURCE LINES 1246-1248 Beta parameters +++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1250-1259 {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 1261-1263 As the signature requires an Id, we need to prepare the expression first. .. GENERATED FROM PYTHON SOURCE LINES 1265-1268 .. code-block:: Python beta1.prepare(database=my_data, number_of_draws=0) beta1.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4852715824}"beta1"[0],0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1269-1272 .. code-block:: Python beta3.prepare(database=my_data, number_of_draws=0) beta3.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4852723504}"beta3"[1],0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1273-1275 Variables +++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1277-1283 {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 1285-1287 .. code-block:: Python Variable1.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5158109008}"Variable1",6,2'] .. GENERATED FROM PYTHON SOURCE LINES 1288-1290 Random variables ++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 1292-1299 {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 1301-1304 .. code-block:: Python omega.prepare(database=my_data, number_of_draws=0) omega.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4907788688}"omega",0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1305-1307 Draws +++++ .. GENERATED FROM PYTHON SOURCE LINES 1309-1315 {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 1317-1320 .. code-block:: Python my_draws.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) my_draws.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4907793248}"my_draws",0,0'] .. GENERATED FROM PYTHON SOURCE LINES 1321-1330 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 1332-1334 Binary operator /////////////// .. GENERATED FROM PYTHON SOURCE LINES 1336-1354 `{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 1356-1358 .. code-block:: Python the_sum = beta1 + Variable1 .. GENERATED FROM PYTHON SOURCE LINES 1359-1361 .. code-block:: Python the_sum.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4852715824}"beta1"[0],0,0', b'{5158109008}"Variable1",6,2', b'{5158954976}(2),4852715824,5158109008'] .. GENERATED FROM PYTHON SOURCE LINES 1362-1364 Unary operator ////////////// .. GENERATED FROM PYTHON SOURCE LINES 1366-1375 `{identifier}(1),idChild,` where operator is one of: - `UnaryMinus` - `MonteCarlo` - `bioNormalCdf` - `PanelLikelihoodTrajectory` - `exp` - `log` .. GENERATED FROM PYTHON SOURCE LINES 1377-1379 .. code-block:: Python m = -beta1 .. GENERATED FROM PYTHON SOURCE LINES 1380-1382 .. code-block:: Python m.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4852715824}"beta1"[0],0,0', b'{5158952480}(1),4852715824'] .. GENERATED FROM PYTHON SOURCE LINES 1383-1385 LogLogit //////// .. GENERATED FROM PYTHON SOURCE LINES 1387-1389 {identifier}(nbrOfAlternatives), chosenAlt,altNumber,utility,availability,altNumber,utility,availability, etc. .. GENERATED FROM PYTHON SOURCE LINES 1391-1394 .. code-block:: Python expr7.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) expr7.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5158250704},1.0', b'{4852715824}"beta1"[0],0,0', b'{5158253968}(1),4852715824', b'{4852723072}"beta2"[0],1,1', b'{5158249600}(1),4852723072', b'{4852715824}"beta1"[0],0,0', b'{5158248736}(1),4852715824', b'{5158256224},1.0', b'{5158255120},1.0', b'{5158244800},1.0', b'<_bioLogLogit>{5158254496}(3),5158250704,0,5158253968,5158256224,1,5158249600,5158255120,2,5158248736,5158244800'] .. GENERATED FROM PYTHON SOURCE LINES 1395-1397 Derive ////// .. GENERATED FROM PYTHON SOURCE LINES 1399-1400 {identifier},id of expression to derive,unique index of elementary expression .. GENERATED FROM PYTHON SOURCE LINES 1402-1405 .. code-block:: Python expr9.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) expr9.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5158253584},1.0', b'{4852715824}"beta1"[0],0,0', b'{5158253968}(1),4852715824', b'{4852723072}"beta2"[0],1,1', b'{5158249600}(1),4852723072', b'{4852715824}"beta1"[0],0,0', b'{5158248736}(1),4852715824', b'{5158254928},1.0', b'{5158244848},1.0', b'{5158255024},1.0', b'<_bioLogLogit>{5158253728}(3),5158253584,0,5158253968,5158254928,1,5158249600,5158244848,2,5158248736,5158255024', b'{5158788352},5158253728,1'] .. GENERATED FROM PYTHON SOURCE LINES 1406-1408 Integrate ///////// .. GENERATED FROM PYTHON SOURCE LINES 1410-1411 {identifier},id of expression to derive,index of random variable .. GENERATED FROM PYTHON SOURCE LINES 1413-1416 .. code-block:: Python expr4.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) expr4.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{5158245184},0.0', b'{5158249072},1.0', b'{5158246864},1.0', b'{4907788688}"omega",0,0', b'{5158246240}(1),4907788688', b'{5158243840}(1),5158246240', b'{5158250992}(2),5158246864,5158243840', b'{5158251232}(2),5158249072,5158250992', b'{5158249552}(2),5158245184,5158251232', b'{5158245184},0.0', b'{5158249072},1.0', b'{5158246864},1.0', b'{4907788688}"omega",0,0', b'{5158246240}(1),4907788688', b'{5158243840}(1),5158246240', b'{5158250992}(2),5158246864,5158243840', b'{5158251232}(2),5158249072,5158250992', b'{5158249552}(2),5158245184,5158251232', b'{5158253008}(2),5158249552,5158249552', b'{5158254304},1.0', b'{4907788688}"omega",0,0', b'{5158250368}(1),4907788688', b'{5158255552}(1),5158250368', b'{5158253872}(2),5158254304,5158255552', b'{5158245760},1.0', b'{4907788688}"omega",0,0', b'{5158252672}(1),4907788688', b'{5158251472}(1),5158252672', b'{5158247392}(2),5158245760,5158251472', b'{5158248256},5158247392,-2.0', b'{5158256176}(2),5158253872,5158248256', b'{5159599616}(2),5158253008,5158256176', b'{5158245232},1.0', b'{5158245328}(2),5159599616,5158245232', b'{5158250752},5158245328,0'] .. GENERATED FROM PYTHON SOURCE LINES 1417-1419 Elem //// .. GENERATED FROM PYTHON SOURCE LINES 1421-1428 {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 1430-1433 .. code-block:: Python elemExpr.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) elemExpr.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4907717680}"Person",4,0', b'{4852712224},2.0', b'{4852715824}"beta1"[0],0,0', b'{4852724368}(2),4852712224,4852715824', b'{4852723072}"beta2"[0],1,1', b'{4852724272}(1),4852723072', b'{4852720432}(1),4852724272', b'{4852723072}"beta2"[0],1,1', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4852724464}(2),4852723504,4852716160', b'{4852714528}(2),4852723072,4852724464', b'{4852715824}"beta1"[0],0,0', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4852727152}(2),4852723504,4852716160', b'{4852713520}(2),4852715824,4852727152', b'{4852720192}(2),4852714528,4852713520', b'{4852714816}(2),4852720432,4852720192', b'{4852727728}(2),4852724368,4852714816', b'{4907792336},2.0', b'{4852715824}"beta1"[0],0,0', b'{4907791952}(2),4907792336,4852715824', b'{5158109008}"Variable1",6,2', b'{4907784416}(2),4907791952,5158109008', b'{4852723072}"beta2"[0],1,1', b'{4907790896}(1),4852723072', b'{5158099264}"Variable2",7,3', b'{4907783984}(2),4907790896,5158099264', b'{4907786432}(1),4907783984', b'{4852723072}"beta2"[0],1,1', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4907790608}(2),4852723504,4852716160', b'{4907791520}(2),4852723072,4907790608', b'{4852715824}"beta1"[0],0,0', b'{4852723504}"beta3"[1],2,0', b'{4852716160}"beta4"[1],3,1', b'{4907791616}(2),4852723504,4852716160', b'{4907794208}(2),4852715824,4907791616', b'{4907782832}(2),4907791520,4907794208', b'{4907792912}(2),4907786432,4907782832', b'{4907780432}(2),4907784416,4907792912', b'{4907788640}(2),4907717680,1,4852727728,2,4907780432'] .. GENERATED FROM PYTHON SOURCE LINES 1434-1436 bioLinearUtility //////////////// .. GENERATED FROM PYTHON SOURCE LINES 1438-1449 {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 1451-1453 .. code-block:: Python expr13.prepare(database=my_data, number_of_draws=NUMBER_OF_DRAWS) expr13.get_signature() .. rst-class:: sphx-glr-script-out .. code-block:: none [b'{4852715824}"beta1"[0],0,0', b'{4852723072}"beta2"[0],1,1', b'{4852723504}"beta3"[1],2,0', b'{5158243504}"Variable1",5,2', b'{5158243168}"Variable2",6,3', b'{5158244176}"newvar_b",11,8', b'{5158247008}(3),4852715824,0,beta1,5158243504,5,Variable1,4852723072,1,beta2,5158243168,6,Variable2,4852723504,2,beta3,5158244176,11,newvar_b'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.064 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-jupyter :download:`Download Jupyter notebook: plot_expressions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_expressions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_expressions.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_