THIS SITE IS UNDER CONSTRUCTION
This Fortran code provides a list of functions and subroutines that I typically use in my research. If you find any mistake, either in the codes or in this website, contact me.
Efficiency of the algorithms is not guaranteed.
All functions and subroutines assume double precision for real variables.
License:
The code is distributed under the MIT license. You are free to use and modify it as you wish, but please acknowledge the source. If you distributte a modified version ofthis code, please indicate in the preambule the changes made along with your name and date.
In the preambule, several parameters used across functions and subroutines are defined:
  integer  , parameter :: dp    = kind(1.0d00)             ! double-precision real variables
  real(dp) , parameter :: cero  = dble(0.00000000000000)   ! zero
  real(dp) , parameter :: medio = dble(0.50000000000000)   ! one half
  real(dp) , parameter :: uno   = dble(1.00000000000000)   ! one
  real(dp) , parameter :: dos   = dble(2.00000000000000)   ! two
  real(dp) , parameter :: cien  = dble(100.000000000000)   ! one hundred
  real(dp) , parameter :: mil   = dble(1000.00000000000)   ! one thousand
  real(dp) , parameter :: tolvl = dble(0.00000000010000)   ! tolerance level
crra: a constant relative risk aversion utilit function.ces: a constant elasticity of substitution aggregator.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.num2text: converts a real number or an integer into a string.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.shuffle_vect: this subroutine returns a vector with the shuffled values of a used-provided vector using the Fisher-Yates algorithm.fisheryates: this subroutine returns a vector filled with integers from 1 to $n$ and then shuffled using the Fisher-Yates algorithm.vect: transform a matrix of into a vector (similar to reshape).cumsum: returns the vector with cumulative sum of a vector (as Matlab’s cumsum function).diag: returns a vector with the main diagonal of a matrix.inverse: returns the inverse of a squared matrix.transmat: returns the transpose of a matrix.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 thenormalizeanddenormalizesubroutines.