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.
import datetime
print(datetime.datetime.now())
import biogeme.version as ver
print(ver.getText())
import numpy as np
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).$$
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).$$
x = np.array([1.1,1.1])
f,g,H = myFunction(x)
f
g
H
Calculates an approximation of the gradient by finite differences.
g_fd = tools.findiff_g(myFunction,x)
g_fd
Check the precision of the approximation
g-g_fd
Calculates an approximation of the Hessian by finite differences.
H_fd = tools.findiff_H(myFunction,x)
H_fd
Check the precision of the approximation
H-H_fd
There is a function that checks the analytical derivatives by comparing them to their finite difference approximation.
logger = msg.bioMessage(screenLevel=3)
f,g,h,gdiff,hdiff = tools.checkDerivatives(myFunction,x,names=None,logg=True)
To help reading the reporting, it is possible to give names to variables.
f,g,h,gdiff,hdiff = tools.checkDerivatives(myFunction,x,names=['First','Second'],logg=True)
gdiff
hdiff
Calculate prime numbers lesser or equal to an upper bound
myprimes = tools.calculatePrimeNumbers(10)
myprimes
myprimes = tools.calculatePrimeNumbers(100)
myprimes
Calculate a given number of prime numbers
myprimes = tools.getPrimeNumbers(7)
myprimes