module tools: examples of use of each function

This webpage is for programmers who need examples of use of the functions of the class. The examples are designed to illustrate the syntax. They do not correspond to any meaningful model. For examples of models, visit biogeme.epfl.ch.

In [1]:
import datetime
print(datetime.datetime.now())
In [2]:
import biogeme.version as ver
print(ver.getText())
In [3]:
import numpy as np
import pandas as pd
import biogeme.tools as tools
import biogeme.messaging as msg

Define a function and its derivatives: $$f = \log(x_0) + \exp(x_1),$$ $$g = \left( \begin{array}{c} \frac{1}{x_0} \\ \exp(x_1)\end{array}\right),$$ $$h=\left( \begin{array}{cc} -\frac{1}{x_0^2} & 0 \\ 0 & \exp(x_1)\end{array}\right).$$

In [4]:
def myFunction(x):
    f = np.log(x[0]) + np.exp(x[1])
    g = np.empty(2)
    g[0] = 1.0 / x[0]
    g[1] = np.exp(x[1])
    H = np.empty((2,2))
    H[0,0] = - 1.0 / x[0]**2
    H[0,1] = 0.0
    H[1,0] = 0.0
    H[1,1] = np.exp(x[1])
    return f,g,H

Evaluate the function at the point $$x = \left( \begin{array}{c}1 \\ 1 \end{array}\right).$$

In [5]:
x = np.array([1.1,1.1])
f,g,H = myFunction(x)
In [6]:
f
Out[6]:
3.099476203750758
In [7]:
g
Out[7]:
array([0.90909091, 3.00416602])
In [8]:
H
Out[8]:
array([[-0.82644628,  0.        ],
       [ 0.        ,  3.00416602]])

Calculates an approximation of the gradient by finite differences.

In [9]:
g_fd = tools.findiff_g(myFunction,x)
In [10]:
g_fd
Out[10]:
array([0.90909087, 3.00416619])

Check the precision of the approximation

In [11]:
g-g_fd
Out[11]:
array([ 4.18595454e-08, -1.64594663e-07])

Calculates an approximation of the Hessian by finite differences.

In [12]:
H_fd = tools.findiff_H(myFunction,x)
In [13]:
H_fd
Out[13]:
array([[-0.8264462 ,  0.        ],
       [ 0.        ,  3.00416619]])

Check the precision of the approximation

In [14]:
H-H_fd
Out[14]:
array([[-8.26465610e-08,  0.00000000e+00],
       [ 0.00000000e+00, -1.64594663e-07]])

There is a function that checks the analytical derivatives by comparing them to their finite difference approximation.

In [15]:
logger = msg.bioMessage(screenLevel=3)
f,g,h,gdiff,hdiff = tools.checkDerivatives(myFunction,x,names=None,logg=True)
[19:09:26] < Detailed >  x		Gradient	FinDiff		Difference
[19:09:26] < Detailed >  x[0]           	+9.090909E-01	+9.090909E-01	+4.185955E-08
[19:09:26] < Detailed >  x[1]           	+3.004166E+00	+3.004166E+00	-1.645947E-07
[19:09:26] < Detailed >  Row		Col		Hessian	FinDiff		Difference
[19:09:26] < Detailed >  x[0]           	x[0]           	-8.264463E-01	-8.264462E-01	-8.264656E-08
[19:09:26] < Detailed >  x[0]           	x[1]           	+0.000000E+00	+0.000000E+00	+0.000000E+00
[19:09:26] < Detailed >  x[1]           	x[0]           	+0.000000E+00	+0.000000E+00	+0.000000E+00
[19:09:26] < Detailed >  x[1]           	x[1]           	+3.004166E+00	+3.004166E+00	-1.645947E-07

To help reading the reporting, it is possible to give names to variables.

In [16]:
f,g,h,gdiff,hdiff = tools.checkDerivatives(myFunction,x,names=['First','Second'],logg=True)
[19:09:26] < Detailed >  x		Gradient	FinDiff		Difference
[19:09:26] < Detailed >  First          	+9.090909E-01	+9.090909E-01	+4.185955E-08
[19:09:26] < Detailed >  Second         	+3.004166E+00	+3.004166E+00	-1.645947E-07
[19:09:26] < Detailed >  Row		Col		Hessian	FinDiff		Difference
[19:09:26] < Detailed >  First          	First          	-8.264463E-01	-8.264462E-01	-8.264656E-08
[19:09:26] < Detailed >  First          	Second         	+0.000000E+00	+0.000000E+00	+0.000000E+00
[19:09:26] < Detailed >  Second         	First          	+0.000000E+00	+0.000000E+00	+0.000000E+00
[19:09:26] < Detailed >  Second         	Second         	+3.004166E+00	+3.004166E+00	-1.645947E-07
In [17]:
gdiff
Out[17]:
array([ 4.18595454e-08, -1.64594663e-07])
In [18]:
hdiff
Out[18]:
array([[-8.26465610e-08,  0.00000000e+00],
       [ 0.00000000e+00, -1.64594663e-07]])

Prime numbers

Calculate prime numbers lesser or equal to an upper bound

In [19]:
myprimes = tools.calculatePrimeNumbers(10)
In [20]:
myprimes
Out[20]:
[2, 3, 5, 7]
In [21]:
myprimes = tools.calculatePrimeNumbers(100)
In [22]:
myprimes
Out[22]:
[2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
 23,
 29,
 31,
 37,
 41,
 43,
 47,
 53,
 59,
 61,
 67,
 71,
 73,
 79,
 83,
 89,
 97]

Calculate a given number of prime numbers

In [23]:
myprimes = tools.getPrimeNumbers(7)
myprimes
Out[23]:
[2, 3, 5, 7, 11, 13, 17]

Counting groups of data

In [24]:
df = pd.DataFrame({'ID':[1,1,2,3,3, 1,2,3], 'value':[1000,2000,3000,4000,5000,6000,10000,20000]})
In [25]:
tools.countNumberOfGroups(df,'ID')
Out[25]:
6