Fortran toolkit
Borja Petit
THIS SITE IS UNDER CONSTRUCTION
This Fortran code provides a list of functions and subroutines that I typically use in my research. All functions and subroutines assume double precision for real variables.
Efficiency of the algorithms is not guaranteed, and the code is distributed under the MIT license.
You can download the code from this link.
If you find any mistake, or have any suggestion, contact me.
General purpose
grid
: generate a grid for a continuous variable.interpolation
: interpolate a value over a grid, returning position and distance.interpolate
: linearly interpolate a value over an n-dimensional grid (up to dimension 6).timing
: timing function robust to parallelization.multiplo
: check if an integer is a multiple of another user-provided integer.iseven
: check if a user-provided integer is even.error
: print error message and pause execution.
Statistics
varmean
: returns the average of a variable (allows for weights and mask).varvar
: returns the variance of a variable (allows for weights and mask).varstd
: returns the standard deviation of a variable (allows for weights and mask).correlation
: returns the correlation between two variables (allows for weights and mask).percentile
: returns the i-th percentile of a distribution (allows for weights and mask).olsreg
: returns the OLS coefficients of a linear regression with up to 8 explanatory variables (allows for weights and mask).tauchen
: returns the transition matrix for a discretized AR(1) process.normaldist
: returns the distribution for a normal random variable given some mean and variance.randomnormal
: returns a random draw (either a scalar or a vector) for a normal distribution.cdfn
: returns the cdf of a standard normal distribution.
Linear algebra
vect
: transform a matrix of into a vector (similar toreshape
).cumsum
: returns the vector with cumulative sum of a vector (as Matlab’scumsum
function).diag
: returns a vector with the main diagonal of a matrix.inverse
: returns the inverse of a squared matrix.
Optimization
Algorithms for single-valued univariate equations:
golden
: maximize a single-valued univariate equation using the Golden Search algorithm.brent
: find the root of a single-valued univariate equation using the Brent’s method.
Algorithm for single-valued multivariate equations:
simplex
: minimize a single-valued multivariate equation using the Nelder-Mead algorithm.
Algorithm for systems of equations:
-
lmmin
: minimize a multivariate system of equations using the Levenberg–Marquardt algorithm.The Levenberg–Marquardt algorithm uses the Jacobian of the system to find the minimum. When evaluating the objective function is time-costly, computing the Jacobian may take too long. In those cases, one potential way of speeding up the algorithm is to update the Jacobian matrix using the Broyden’s method, that does not require further function evaluations.
Other functions:
broyden
: updates a Jacobian matrix using the Broyden’s method.
Constrained optimization:
None of the algorithms in this toolkit is explicitly written to allow for constraints, but one can transform a constrained optimization problem into an unconstrained one using thenormalize
anddenormalize
subroutines.