Tools¶
Generic convenient tools.
biogeme.tools module¶
Implements some useful functions
- author
Michel Bierlaire
- date
Sun Apr 14 10:46:10 2019
-
biogeme.tools.
calculatePrimeNumbers
(upperBound)[source]¶ Calculate prime numbers
- Parameters
upperBound (int) – prime numbers up to this value will be computed
- Returns
array with prime numbers
- Return type
list(int)
>>> tools.calculatePrimeNumbers(10) [2, 3, 5, 7]
-
biogeme.tools.
checkDerivatives
(theFunction, x, names=None, logg=False)[source]¶ Verifies the analytical derivatives of a function by comparing them with finite difference approximations.
- Parameters
theFunction (function) –
A function object that takes a vector as an argument, and returns a tuple:
The first element of the tuple is the value of the function \(f\),
the second is the gradient of the function,
the third is the hessian.
x (numpy.array) – arguments of the function
names (list(string)) – the names of the entries of x (for reporting).
logg (bool) – if True, messages will be displayed.
- Returns
tuple f, g, h, gdiff, hdiff where
f is the value of the function at x,
g is the analytical gradient,
h is the analytical hessian,
gdiff is the difference between the analytical gradient and the finite difference approximation
hdiff is the difference between the analytical hessian and the finite difference approximation
- Return type
float, numpy.array,numpy.array, numpy.array,numpy.array
-
biogeme.tools.
countNumberOfGroups
(df, column)[source]¶ This function counts the number of groups of same value in a column. For instance: 1,2,2,3,3,3,4,1,1 would give 5.
Example:
>>>df = pd.DataFrame({'ID': [1, 1, 2, 3, 3, 1, 2, 3], 'value':[1000, 2000, 3000, 4000, 5000, 5000, 10000, 20000]}) >>>tools.countNumberOfGroups(df,'ID') 6 >>>tools.countNumberOfGroups(df,'value') 7
-
biogeme.tools.
findiff_H
(theFunction, x)[source]¶ Calculates the hessian of a function \(f\) using finite differences
- Parameters
theFunction (function) – A function object that takes a vector as an argument, and returns a tuple. The first element of the tuple is the value of the function \(f\), and the second is the gradient of the function. The other elements are not used.
x (numpy.array) – argument of the function
- Returns
numpy matrix containing the hessian calculated by finite differences.
- Return type
numpy.array
-
biogeme.tools.
findiff_g
(theFunction, x)[source]¶ Calculates the gradient of a function :math`f` using finite differences
- Parameters
theFunction (function) – A function object that takes a vector as an argument, and returns a tuple. The first element of the tuple is the value of the function \(f\). The other elements are not used.
x (numpy.array) – argument of the function
- Returns
numpy vector, same dimension as x, containing the gradient calculated by finite differences.
- Return type
numpy.array
-
biogeme.tools.
getPrimeNumbers
(n)[source]¶ Get a given number of prime numbers
- Parameters
n (int) – number of primes that are requested
- Returns
array with prime numbers
- Return type
list(int)
-
biogeme.tools.
likelihood_ratio_test
(model1, model2, significance_level=0.95)[source]¶ This function performs a likelihood ratio test between a restricted and an unrestricted model.
- Parameters
model1 (tuple(float, int)) – the final loglikelood of one model, and the number of estimated parameters.
model2 (tuple(float, int)) – the final loglikelood of the other model, and the number of estimated parameters.
significance_level (float) – level of significance of the test. Default: 0.95
- Returns
a tuple containing:
a message with the outcome of the test
the statistic, that is minus two times the difference between the loglikelihood of the two models
the threshold of the chi square distribution.
- Return type
(str, float, float)
- Raises
biogemeError – if the unrestricted model has a lower log likelihood than the restricted model.