Skip to content

Krusell and Smith (1998) using f2Py

David R. Pugh edited this page Jun 6, 2013 · 2 revisions

Where to get the original FORTRAN code:

The original FORTRAN code (and required auxiliary files) for computing the equilibrium of the stochastic-β economy be downloaded from Anthony Smith's website. Unclear whether or not FORTRAN code for the benchmark version is available.

  • wealth.f: FORTRAN code that will be compiled into a UNIX executable file that will solve the model.
  • cross.in: A 30,000 x 4 array of data defining the initial cross section distribution of capital/wealth for the economy. Footnote 20 from the paper says "when we simulate the behavior of the stochastic-β economy, we use 30,000 agents, so that the invariant distribution across discount factors has 3,000 agents at each of the extreme values of β and 24,000 agents at the middle value of β." Given this, the first column is effectively an agent ID number (i.e., 1-30,000); second column takes values in 1, 2, 3 and is likely that these map to the low, middle, and high values of β discussed below; third column takes values in 0, 1 and presumably denotes whether or not the agent is employed; the fourth column is the agent's initial endowment of wealth/capital.
  • points.kgd:
  • heter40w.in:
  • betas.dat: Assumed distribution for discount factors in the extended version of the model. More precisely, they assume that β can take on three values, 0.9858, 0.9894, and 0.9930, and that the transition probabilities are such that (i) the invariant distribution for β’s has 80 percent of the population at the middle β and 10 percent at each of the other β's, (ii) immediate transitions between the extreme values of β occur with probability zero, and (iii) the average duration of the highest and lowest β’s is 50 years. Not sure why the array is 2 x 3 instead of 3 x 3!
  • agglaw.in: Presumably an initial guess of the coefficients for the law of motion for aggregate capital. Note that the coefficient array is 2 x 2 because aggregate shock has only two states: "good" or "bad."

Download and save two copies of all the files. One copy goes into your working directory, the second copy should be saved in a separate directory in case the working files get corrupted/over-written in the process of building the Python module.

Compiling wealth.f directly via the gfortran compiler:

First, I made sure that the code from Anthony Smith's website works as advertised by compiling and executing his code directly. At the command line type:

> gfortran wealth.f -o replicate-krusell-smith

The above will compile the wealth.f file containing the solution routines used in the paper and create a UNIX executable called replicate-krusell-smith in your working directory. The code did compile, but not without a number of warnings messages (never a good sign!).

To execute the program replicate-krusell-smith type the following at the prompt:

> ./replicate-krusell-smith

If the above doesn't work, then you may need to chmod +x replicate-krusell-smith in order to give yourself permissions to execute the file. The program should now begin to run! The entire program takes a little less than an hour to run to completion during which time you should see arrays being printed directly in the terminal window as well as written to a number of output files.

Making sense of the output files:

  • agglaw.out: This file contains the coefficients of the equilibrium aggregate law of motion computed by replicate-krusell-smith. Note that these match those reported in Section B of Part IV of Krusell and Smith (1998) that the wealth.f.
  • cross.out: Equilibrium cross-section distribution of capital/wealth. Columns are same as those of cross.in.

Initial and equilibrium wealth distributions are almost identical

I compared some descriptive statistics of the equilibrium wealth distribution to those of the initial wealth distribution and found that the two distributions are basically identical:

Descriptive statistics for initial distribution of wealth:

  • count 30000.000000
  • mean 11.724722
  • std 42.602848
  • min -2.400000
  • 25% 0.835919
  • 50% 2.234808
  • 75% 4.427163
  • max 3359.841151

Descriptive statistics for equilibrium distribution of wealth:

  • count 30000.000000
  • mean 11.718953
  • std 41.642649
  • min -2.400000
  • 25% 0.843116
  • 50% 2.256479
  • 75% 4.405612
  • max 2781.580034

Why is this? Is the initial wealth distribution "cooked" to be close to equilibrium in order to reduce computation time? Or does the model need a very unequal initial distribution of wealth in order to generate an equilibrium distribution of wealth that is also unequal? Why did Krusell and Smith not document their code!

Building a Python module using f2Py

Ideally, I would like to make sure that the f2Py constructed Python module can toggle back and forth between the benchmark and extended versions of the model. However, would need to get my hands on the code for the basic model.

Attempt to run f2Py on the wealth.f file directly via the command line...

> f2py -c wealth.f -m 'krusell-smith-1998'

The -c command is short for 'compile'; the The -m flag gives the name that the python module should take. This attempt failed with the following error message:

TypeError: All blocks must be python module blocks but got 'program'

Not at all surprised that this failed...but no real sense of what the error message means! Found a tutorial on f2Py by Pierre Schnizer and set about reading. Found the f2Py user guide/reference manual

Next tried typing the following at the command prompt:

`> f2py -m 'krusell-smith-1998' -h wealth.pyf wealth.f'

The -h flag tells f2py in which file it should write the signature. After that all the FORTRAN files are listed which f2py should parse. In the case here it is only the file wealth.f.

Second attempt at compiling via the command line...

f2py -c wealth.pyf wealth.f

Failed with the following (unhelpful!) error message:

error: f2py target file '.../krusellmodule.c' not generated