.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b01expressions.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_indicators_plot_b01expressions.py: Examples of mathematical expressions ==================================== Example of manipulating mathematical expressions and calculation of derivatives. :author: Michel Bierlaire, EPFL :date: Wed Apr 12 21:06:21 2023 .. GENERATED FROM PYTHON SOURCE LINES 13-30 .. code-block:: default import numpy as np try: import matplotlib.pyplot as plt can_plot = True except ModuleNotFoundError: can_plot = False from biogeme.expressions import Beta, exp # ## # We create a simple expression: b = Beta('b', 1, None, None, 0) expression = exp(-b * b + 1) .. GENERATED FROM PYTHON SOURCE LINES 31-34 We can calculate its value. Note that, as the expression is calculated out of Biogeme, the IDs must be prepared. So the parameter 'prepareIds' is set to True .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default z = expression.getValue_c(prepareIds=True) print(f'exp(-b * b + 1) = {z}') .. rst-class:: sphx-glr-script-out .. code-block:: none exp(-b * b + 1) = 1.0 .. GENERATED FROM PYTHON SOURCE LINES 38-41 We can also calculate the value, the first derivative, the second derivative, and the BHHH, which in this case is the square of the first derivatives .. GENERATED FROM PYTHON SOURCE LINES 41-42 .. code-block:: default f, g, h, bhhh = expression.getValueAndDerivatives(prepareIds=True) .. GENERATED FROM PYTHON SOURCE LINES 43-44 .. code-block:: default print(f'f = {f}') .. rst-class:: sphx-glr-script-out .. code-block:: none f = 1.0 .. GENERATED FROM PYTHON SOURCE LINES 45-46 .. code-block:: default print(f'g = {g}') .. rst-class:: sphx-glr-script-out .. code-block:: none g = [-2.] .. GENERATED FROM PYTHON SOURCE LINES 47-48 .. code-block:: default print(f'h = {h}') .. rst-class:: sphx-glr-script-out .. code-block:: none h = [[2.]] .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default print(f'BHHH = {bhhh}') .. rst-class:: sphx-glr-script-out .. code-block:: none BHHH = [[4.]] .. GENERATED FROM PYTHON SOURCE LINES 52-55 From the expression, we can create a Python function that takes as argument the value of the free parameters, and returns the function, the first and second derivatives. .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: default fct = expression.createFunction() .. GENERATED FROM PYTHON SOURCE LINES 58-59 We can use the function for different values of the parameter .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: default beta = 2 f, g, h = fct(beta) print(f'f({beta}) = {f}') print(f'g({beta}) = {g}') print(f'h({beta}) = {h}') .. rst-class:: sphx-glr-script-out .. code-block:: none f(2) = 0.049787068367863944 g(2) = [-0.19914827] h(2) = [[0.69701896]] .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. code-block:: default beta = 3 f, g, h = fct(beta) print(f'f({beta}) = {f}') print(f'g({beta}) = {g}') print(f'h({beta}) = {h}') .. rst-class:: sphx-glr-script-out .. code-block:: none f(3) = 0.00033546262790251185 g(3) = [-0.00201278] h(3) = [[0.01140573]] .. GENERATED FROM PYTHON SOURCE LINES 73-95 .. code-block:: default if can_plot: # We can also use it to plot the function and its derivatives x = np.arange(-2, 2, 0.01) # The value of the function is element [0]. f = [fct(xx)[0] for xx in x] # The gradient is element [1]. As it contains only one entry [0], # we convert it into float. g = [float(fct([xx])[1][0]) for xx in x] # The hessian is element [2]. As it contains only one entry # [0][0], we convert it into float. h = [float(fct([xx])[2][0][0]) for xx in x] ax = plt.gca() ax.plot(x, f, label="f(x)") ax.plot(x, g, label="f'(x)") ax.plot(x, h, label='f"(x)') ax.legend() plt.show() .. image-sg:: /auto_examples/indicators/images/sphx_glr_plot_b01expressions_001.png :alt: plot b01expressions :srcset: /auto_examples/indicators/images/sphx_glr_plot_b01expressions_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.081 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b01expressions.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_b01expressions.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01expressions.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_