Notes on using F2PY to call Fortran code from Python.

The application is the power series approximation of the Leontief inverse matrix, from input-output analysis in economics. The explanation and example below draw heavily on Miller and Blair (2009) and Almon (2017).

# Notation and accounting

In input-output analysis we define:

• an $\left( m \times m \right)$ matrix of inter-industry purchases (intermediate consumption): $\mathbf{Z}$
• an $\left( m \times 1 \right)$ vector of primary inputs (value added): $\mathbf{v}$
• an $\left( m \times 1 \right)$ vector of final demand: $\mathbf{f}$
• an $\left( m \times 1 \right)$ vector of gross output: $\mathbf{q}$

By the accounting that underpins an input-output table:

where $\mathbf{i}$ is a summation vector: an $\left( m \times 1 \right)$ vector of ones.

# The Leontief inverse matrix

Under the assumption of a Leontief production function, which exhibits constant returns to scale, we define $\mathbf{A}$, an $\left( m \times m \right)$ matrix of technical (or input-output) coefficients with elements:

where $a_{ij}$ is what industry (product) $j$ requires of industry (product) $i$ to produce one unit of output.

We can then express gross output, $\mathbf{q}$, as:

Solving for $\mathbf{q}$:

where $\mathbf{L}$ is the (here, Type I) Leontief inverse matrix:

and $\mathbf{I}$ is the identity matrix.

Equation 5 provides a way to answer the question, ‘How much output would be required from across the economy to support a certain amount of final demand?’. For a hypothetical vector of final demand, $\mathbf{f}^*$:

# Power series approximation

Historically, matrix inversion was computationally expensive and, even today, may not be a good idea. As an alternative, we can think of the Leontief inverse as the sum of a power series:

where $\mathbf{A}^2 = \mathbf{A} \mathbf{A}$, $\mathbf{A}^3 = \mathbf{A} \mathbf{A}^2$ etc.

With this, we can approximate a solution to Equation 5 as:

for some $n$ large enough for $\mathbf{A}^n \mathbf{f}$ to be not significantly different from zero. This is the power series approximation we want to implement. Miller and Blair (2009) provide more detail on the derivation and, as well as Almon (2017), the proof by which this series converges for an input-output matrix, $\mathbf{A}$.

The power series also has a convenient economic interpretation. Each term in the series represents a round-by-round output effect to support a given amount of final demand. To supply our dinner, the butcher and the brewer must buy from the farmer; the baker must buy from the miller, who must also buy from the farmer etc.

## Implementation

F2PY provides various ways to wrap Fortran code. Here, I do it the quick way but with a Python function to wrap the call to the compiled Fortran code. An abridged version of the Fortran subroutine is below:

Niceties the code above could do with:

• control over the maximum number of iterations, rather than a hardcoded limit of 25
• control over the convergence threshold, rather than a hardcoded value of 0.000001
• some indication of whether the procedure converged

The full code implements the above here.

We then compile the code from the command line with f2py (here, the code above is in a file with name ‘leontief.f95’):

The Python wrapper (again, abridged) is as follows:

We can now call power_method_fortran() just like any other Python function.

The full code uses the example of the Tiny model from Almon (2017). The ‘power_series_example.py’ script compares the speed of three approaches to calculate $\mathbf{q}$ from $\mathbf{f}$ and $\mathbf{A}$:

1. matrix inversion and then multiplication by Equations 5 and 6 above, in Python
2. the power method, implemented in Python
3. the power method, implemented in Fortran (as above)

All rely on NumPy to some degree.

Running the script on my machine, I get the following screen output:

This shows a reasonable speed gain from doing this. In a one-off piece of analysis, the time savings probably aren’t worth it, though. You’re unlikely to make the coding (and debugging) time back. However, if you have an application that involves repeatedly calculating gross output (say, in a large-scale macroeconometric input-output model with changing input-output coefficients), it could be worth it.

See here for the full code and example.

Almon, C. (2017) The craft of economic modeling, Third, enlarged edition, Inforum
http://www.inforum.umd.edu/papers/TheCraft.html

Miller, R. E., Blair, P. D. (2009) Input-output analysis: Foundations and extensions, Second edition, Cambridge University Press