PUBLIC ECONOMICS (2019) by Burkhard Heer: PYTHON CODE

In the following, you will find the Python Code to my book on Public Economics (2019). You can also download the code from the download page

https://www.uni-augsburg.de/de/fakultaet/wiwi/prof/vwl/heer/pubec-buch/computer-code/

If you have not installed PYTHON yet or have little prior experience in PYTHON programming, an ideal starting point is provided by the web page of THOMAS J. SARGENT and JOHN STACHURSKI on 'Python Programming for Economics and Finance':

https://python-programming.quantecon.org/intro.html

The order of the following computer codes is chosen with intent. The programs get more sophisticated in each step. We start with applications from the Chapter 3 on overlapping generations model and continue to cover material from Chapter 6 on pensions. We cover the following numerical tools in these exercises:

- functions 
- loops
- non-linear equations solver in one variable
- searching for a good initial value as input into the non-linear eqs solver
- non-linear equations solver in multiple variables
- solution of a two-point boundary problem for a first-order non-linear difference equation: Reverse Shooting
- nested application of the non-linear eqs solver fsolve

The numerical and economic problems are described at length in Chapters 3 and 6 of the book Public Economics. Slides, youtube tutorials and solutions to the problem section are also available from the web page of the book (together with MATLAB and Gauss code):

https://www.uni-augsburg.de/de/fakultaet/wiwi/prof/vwl/heer/pubec-buch/

If you have questions or comments, please do not hesitate to contact me: Burkhard.Heer@wiwi.uni-augsburg.de

Chapter 3: Overlapping Generations Model --- Python Code

Application 3.1: Transition Dynamics in a Simple 2-Period OLG Model

The computer program Ch3_olg_dyn.py is a good starting point if you have little programming experience. It computes the transition in a simple 2-period OLG model as described in Section 3.2 of my book. The economic problem is described in more detail in the first 5 minutes of my youtube video:

https://www.youtube.com/watch?v=hZCpyQFk5o4&list=PL-anmMgSYtuEHGjWmUUPl8PCwalcjHvpn&index=3

In the application Ch3_olg_dyn.py, we learn the use of 'functions' in Python.

In order to run the program, you need to install Python and the libraries numpy and matlibplot (as described on the pages by Sargent and Stachurski). You can also download the code of the Python code from my webpage:

https://www.uni-augsburg.de/de/fakultaet/wiwi/prof/vwl/heer/pubec-buch/computer-code/

The figure displays the transition dynamics in the OLG model and is equivalent to Fig. 3.6 on page 76 in my book on 'Public Economics'.

Application 3.2: Turnpike Behavior in a Simple 2-Period OLG Model

In the next application, we look at the so-called turnpike behavior as described in Chapter 3.3.4 on 'Dynamics in the Command Optimum' in my book on 'Public Economics'. The economic problem is also described in the following youtube video:

https://www.youtube.com/watch?v=a73hFit4OUQ&list=PL-anmMgSYtuEHGjWmUUPl8PCwalcjHvpn&index=5

The basic new element in this code is the non-linear equations solver 'broyden1' which is provided by the library 'optimize'. The code of Ch3_turnpike.py which you can also download from my homepage is the following:

Chapter 6: Pensions --- Python Code

Application 6.1: Pay-As-You-Go-Pensions with Inelastic Labor Supply

In the first Python application of this chapter, we compute the welfare effects and the transition dynamics following the abolition of a pay-as-you-go pension system. The dynamic problem is described in more detail in Chapter 6.3 of my book on 'Public Economics'. In addition, you can find the description of the economic and computational problem in the following youtube video:

https://www.youtube.com/watch?v=SGIxL6IRtHE&list=PL-anmMgSYtuEHGjWmUUPl8PCwalcjHvpn&index=9

The Python code 'Ch6_social_security1.py' can also be downloaded from my homepage:

https://www.uni-augsburg.de/de/fakultaet/wiwi/prof/vwl/heer/pubec-buch/computer-code/

In this program, we, again, have to solve a non-linear equation problem. Therefore, we will make use of the command 'fsolve' from the library optimise. The command 'fsolve' is very convenient. You can use it for one- and multi-dimensional non-linear equations problems.

As an additional new element, we have to search for a good initial value to this non-linear equation problem. First, we have to reformulate the non-linear equation so that the non-linear equations solver does not get stuck at the trivial steady state with zero capital stock (therefore, we divide the non-linear equation by the capital stock). Second, we have to search for a good initial value. For this reason, we specifiy an equispaced grid over the capital stock, compute the respective values of the non-linear equation and allocate the minimum of its absolute values.

Note: The code is not written to be elegant and slim, but rather intuitive for the beginner. Therefore, I did not avoid repitition of code or so-called 'magic numbers'. To improve the code and apply best practises, please consult Chapter 15 of Sargent's and Starchurski's tutorial referenced above.

The output of the program is as follows: In the top figure, the non-linear steady-state equation (6.19) is pictured over a range of the steady-state capital stock. The absolute minimum of this function provides us with a guess for the solution of the non-linear equation.

Next, the steady state welfare effect of an introduction of a public pay-as-you-go pension system is computed. In steady state, welfare falls by 38% of total consumption.

In the middle figure, the transition of the capital stock is illustrated for the case that pensions are abolished in period 1.

In the bottom figure, the generational welfare during the transition is illustrated. Notice that welfare drops for the generation born in period 1. They still have to finance the pensions of the old generation but do not receive a pension themselves.

Application 6.2: Pay-As-You-Go Pensions and Elastic Labor Supply

In the following example, we look at a 2-period OLG model with elastic labor and study the effects of the abolition of public PAYG pensions. We find that steady-state welfare effects are 38% of consumption. We also compute the transition that follows an unexpected abolition of the PAYG pension system. This amounts to the solution of a non-linear first-order difference equation in two variables, the capital stock k_t and labor l_t. The boundary conditions are provided by the initial and final values of the capital stock, k_0 and k_T. In order to solve this numerical problem, we have to use the method of REVERSE SHOOTING described in Appendix 4.1 of my book on 'Public Economics' (2019).

We also have to use a nested version of the non-linear equations solver 'fsolve'. In particular, in the outer loop we compute the value of the capital stock in the-second-to-last period, k_T-1, that implies a value of k_0 (by backward iteration) equal to the initial steady state value of the capital stock. Within this function 'findkt()', we solve another set of non-linear equations 'kdyn()' in each period t during the backwards iteration that solves for k_t-1 and l_t-1 given k_t and l_t.

The Python command 'fsolve' is very convenient. It allows us to economize on the use of globals in the functions, which you should always avoid to comply with good programming practices. The command 'fsolve(f,x,args(y))' allows you to find the root of a function f(x,y) with respect to the variable x. In addition, you can pass on the global variables y to this function and avoid that the global variables y get overwritten.

The following Python code is stored in the file 'Ch6_social_security2.py' and can be downloaded from my web page as well:

https://www.uni-augsburg.de/de/fakultaet/wiwi/prof/vwl/heer/pubec-buch/computer-code/

The output of the Python code provides the steady-state values of the capital stock and labor and the welfare effects (equal to 38% of total consumption) associated with a long-run abolition of the public pension system.

The two figures illustrate the transition dynamics of labor and capital if pensions are abolished in period 1 (unexpectedly).

If you enjoyed this introductory tutorial on Python code and would like to find out more about the economics you might find it helpful to watch my youtube graduate course on 'Social Security in Dynamic General Equilibrium':

https://www.youtube.com/watch?v=0Lu2KAWLQHE&list=PL-anmMgSYtuFJu4A1biSqpKk-Oh5woKQn

Last update: Dec 11, 2020