Blog Topics...
3D plotting
(1)
Academic Life
(2)
ACE
(18)
Adaptive Behavior
(2)
Agglomeration
(1)
Aggregation Problems
(1)
Asset Pricing
(1)
Asymmetric Information
(2)
Behavioral Economics
(1)
Breakfast
(4)
Business Cycles
(8)
Business Theory
(4)
China
(1)
Cities
(2)
Clustering
(1)
Collective Intelligence
(1)
Community Structure
(1)
Complex Systems
(42)
Computational Complexity
(1)
Consumption
(1)
Contracting
(1)
Credit constraints
(1)
Credit Cycles
(6)
Daydreaming
(2)
Decision Making
(1)
Deflation
(1)
Diffusion
(2)
Disequilibrium Dynamics
(6)
DSGE
(3)
Dynamic Programming
(6)
Dynamical Systems
(9)
Econometrics
(2)
Economic Growth
(5)
Economic Policy
(5)
Economic Theory
(1)
Education
(4)
Emacs
(1)
Ergodic Theory
(6)
Euro Zone
(1)
Evolutionary Biology
(1)
EVT
(1)
Externalities
(1)
Finance
(29)
Fitness
(6)
Game Theory
(3)
General Equilibrium
(8)
Geopolitics
(1)
GitHub
(1)
Graph of the Day
(11)
Greatest Hits
(1)
Healthcare Economics
(1)
Heterogenous Agent Models
(2)
Heteroskedasticity
(1)
HFT
(1)
Housing Market
(2)
Income Inequality
(2)
Inflation
(2)
Institutions
(2)
Interesting reading material
(2)
IPython
(1)
IS-LM
(1)
Jerusalem
(7)
Keynes
(1)
Kronecker Graphs
(3)
Krussel-Smith
(1)
Labor Economics
(1)
Leverage
(2)
Liquidity
(11)
Logistics
(6)
Lucas Critique
(2)
Machine Learning
(2)
Macroeconomics
(45)
Macroprudential Regulation
(1)
Mathematics
(23)
matplotlib
(10)
Mayavi
(1)
Micro-foundations
(10)
Microeconomic of Banking
(1)
Modeling
(8)
Monetary Policy
(4)
Mountaineering
(9)
MSD
(1)
My Daily Show
(3)
NASA
(1)
Networks
(46)
Non-parametric Estimation
(5)
NumPy
(2)
Old Jaffa
(9)
Online Gaming
(1)
Optimal Growth
(1)
Oxford
(4)
Pakistan
(1)
Pandas
(8)
Penn World Tables
(1)
Physics
(2)
Pigouvian taxes
(1)
Politics
(6)
Power Laws
(10)
Prediction Markets
(1)
Prices
(3)
Prisoner's Dilemma
(2)
Producer Theory
(2)
Python
(29)
Quant
(4)
Quote of the Day
(21)
Ramsey model
(1)
Rational Expectations
(1)
RBC Models
(2)
Research Agenda
(36)
Santa Fe
(6)
SciPy
(1)
Shakshuka
(1)
Shiller
(1)
Social Dynamics
(1)
St. Andrews
(1)
Statistics
(1)
Stocks
(2)
Sugarscape
(2)
Summer Plans
(2)
Systemic Risk
(13)
Teaching
(16)
Theory of the Firm
(4)
Trade
(4)
Travel
(3)
Unemployment
(9)
Value iteration
(2)
Visualizations
(1)
wbdata
(2)
Web 2.0
(1)
Yale
(1)
Sunday, February 17, 2013
Dynamic programming with credit constraints
I am looking for simple examples of economic models with occasionally binding credit constraints. I would like to find the most straightforward example possible, and then bludgeon it into submission with my various numerical algorithms...suggestions are much appreciated!
Wednesday, February 13, 2013
Solving a deterministic RBC model
Taking a short break from marking undergraduate economic essays and decided to write a bit of Python code to solve a deterministic RBC model using value function iteration. Code to replicate the result can be found here. Below are plots of the optimal policy functions (I included some of the iterates of the policy functions as well).
Again the code is mind-numbingly slow (possibly due to the interpolation scheme I am currently using) and takes roughly 8-10 minutes to finish. Any suggestions for speeding up the code (perhaps by using fancy indexing to avoid the for loop!) would greatly appreciated!
Again the code is mind-numbingly slow (possibly due to the interpolation scheme I am currently using) and takes roughly 8-10 minutes to finish. Any suggestions for speeding up the code (perhaps by using fancy indexing to avoid the for loop!) would greatly appreciated!
Labels:
Dynamic Programming,
Python,
RBC Models,
Value iteration
Assaulting the Ramsey model (numerically!)
Everything (and then some!) that you would ever want to know about using dynamic programming techniques to solve deterministic and stochastic versions of the Ramsey optimal growth model can be found in this paper.
I wrote up a quick implementation of the most basic version of the value function iteration described in the paper (vanilla value iteration with a good initial guess and cubic spline interpolation). Below is a graphic I produced of the optimal value and policy functions as well as every 50th iterate (to give a sense of the convergence properties).
The Python code is slowish (takes several minutes to compute the above functions). Suggestions on ways to speed up the code are definitely welcome!
Back to the grind of marking essays...enjoy!
Labels:
Dynamic Programming,
Python,
Ramsey model,
Value iteration
Saturday, January 26, 2013
Technology and the Solow residual...
I recently wrote some Python code to compute paths of technology and the implied Solow residuals using data from the Penn World Tables. Combining the results with some country metadata (i.e., income groupings) from the World Bank API yields this pretty interesting graphic...
If you don't already have the Penn World Tables data...no worries! The script will download the PWT data, compute the Solow residuals based on a method used by Hall and Jones (1999). Based on this decomposition, high income (i.e., red) countries had higher levels of technology in 1960 and higher subsequent growth rates of technology. In fact, the low income (i.e., purple) countries have had effectively zero technological progress since 1960!
Enjoy!
If you don't already have the Penn World Tables data...no worries! The script will download the PWT data, compute the Solow residuals based on a method used by Hall and Jones (1999). Based on this decomposition, high income (i.e., red) countries had higher levels of technology in 1960 and higher subsequent growth rates of technology. In fact, the low income (i.e., purple) countries have had effectively zero technological progress since 1960!
Enjoy!
Thursday, January 24, 2013
Solving models with heterogenous agents...
This material was previously part of another post on welfare costs of business cycles. I decided that the links on solving Krusell-Smith deserved its own post!
Place to start is definitely Wouter den Haan's website, specifically his introductory slides on heterogenous agent models. Krusell and Smith (2006) is an excellent literature review on heterogenous agent models.
Various solution algorithms: Key difficulty in solving Krusell-Smith type models is to find a way to summarize the cross-sectional distribution of capital and employment status (a potentially infinite dimensional object) with a limited set of its moments.
- Original KS algorithm: The KS algorithm specifies a law of motion for these moments and then finds the approximating function to this law of motion using a simulation procedure. Specifically, for a given a set of individual policy rules (which can be solved for via value function iterations, etc), a time series of cross-sectional moments is generated and new laws of motion for the aggregate moments are estimated using this simulated data.
- Xpa algorithm
- Other algorithms
Den Haan (2010):
This paper compares numerical solutions to the model of Krusell and Smith [1998. Income and wealth heterogeneity in the macroeconomy. Journal of Political Economy 106, 867–896] generated by different algorithms. The algorithms have very similar implications for the correlations between different variables. Larger differences are observed for (i) the unconditional means and standard deviations of individual variables, (ii) the behavior of individual agents during particularly bad times, (iii) the volatility of the per capita capital stock, and (iv) the behavior of the higher-order moments of the cross-sectional distribution. For example, the two algorithms that differ the most from each other generate individual consumption series that have an average (maximum) difference of 1.63% (11.4%).Concise description of Krusell and Smith (1998) model can be found in the introduction to the JEDC Krusell-Smith comparison project. It would seem the JEDC Krusell-Smith comparison project is a good place to start thinking about how to implement the Krusell-Smith algorithm in Python. The above paper suggests that implementation details matter and can substantially impact the accuracy of solution. Wouter den Haan provides code for JECD comparison project. Maybe start by implementing the den Haan and Rendahl (2009) algorithm in Python? Followed by Reiter (2009). Given that different algorithms can arrive at different solutions, checking the accuracy of a given solution method against alternatives is important. Slides on the checking accuracy of the above algorithms.
Other Krusell-Smith related links:
- Anthony Smith's original Fortran implementation of Krusell-Smith. Code looks completely impenetrable! Not much in the way of comments or documentation. Probably not worth working out how the code works.
- Sergei Mailar's MatLab code for Krusell-Smith (Mailar provides code for other interesting projects as well!).
- Fortran code for Krusell and Smith (2009).
- Wouter den Haan's slides on additional applications of the Krusell-Smith approach (includes monetary models with consumer heterogeneity, models with entrepreneurs, turning KS into a matching model, portfolio problem).
Thursday, January 17, 2013
Python code to grab Penn World Tables data
For any interested parties, I wrote a small python script to download the Penn World Tables dataset and convert it into a Pandas Panel. Code passed all of my tests, but can't claim that it is industrial strength.
To test out the code, I put together a quick set of layered histograms for global real GDP per capita growth rates from 1951 to 2010. Result...
The number of countries for which data is available varies from around 50 in 1951 to roughly 190 in 2010. Perhaps this means I should have normalized the above histogram?
To test out the code, I put together a quick set of layered histograms for global real GDP per capita growth rates from 1951 to 2010. Result...
The number of countries for which data is available varies from around 50 in 1951 to roughly 190 in 2010. Perhaps this means I should have normalized the above histogram?
Saturday, January 12, 2013
Welfare costs of business cycles and models with heterogenous agents...
Start of quasi literature review for a heterogenous agents project I am starting in the near future. I will continue to update this post as I come across/finish reading additional papers...comments or links to important paper are welcome!
Literature:
Lucas (2003): Robert Lucas' Presidential Address given at the 2003 AEA conference in which he summarizes and defends his back-of-the-envelope calculation of the welfare costs of business cycles. Good, gentle introduction to the literature. Includes a good reference list and brief discussion of the the Krusell and Smith (2002) working paper.
Barlevy (2004):
Krusell and Smith (1998):
Literature:
Lucas (2003): Robert Lucas' Presidential Address given at the 2003 AEA conference in which he summarizes and defends his back-of-the-envelope calculation of the welfare costs of business cycles. Good, gentle introduction to the literature. Includes a good reference list and brief discussion of the the Krusell and Smith (2002) working paper.
Barlevy (2004):
This article reviews the literature on the cost of U.S. post-War business cycle fluctuations. I argue that recent work has established this cost is considerably larger than initial work found. However, despite the large cost of macroeconomic volatility, it is not obvious that policymakers should have pursued a more aggressive stabilization policy than they did. Still, the fact that volatility is so costly suggests stable growth is a desirable goal that ought to be maintained to the extent possible, just as policymakers are currently required to do under the Balanced Growth and Full Employment Act of 1978. This survey was prepared for the Economic Perspectives, a publication of the Federal Reserve Bank of Chicago.As boring an abstract as you will ever come across. Includes a nice table summarizing various estimates of the cost of business cycles.
Krusell and Smith (1998):
How do movements in the distribution of income and wealth affect the macroeconomy? We analyze this question using a calibrated version of the stochastic growth model with partially uninsurable idiosyncratic risk and movements in aggregate productivity. Our main finding is that, in the stationary stochastic equilibrium, the behavior of the macroeconomic aggregates can be almost perfectly described using only the mean of the wealth distribution. This result is robust to substantial changes in both parameter values and model specification. Our benchmark model, whose only difference from the representative-agent framework is the existence of uninsurable idiosyncratic risk, displays far less cross-sectional dispersion and skewness in wealth than U.S. data. However, an extension that relies on a small amount of heterogeneity in thrift does succeed in replicating the key features of the wealth data. Furthermore, this extension features aggregate time series that depart significantly from permanent income behavior.Krusell and Smith (1999):
We investigate the welfare effects of eliminating business cycles in a model with substantial consumer heterogeneity. The heterogeneity arises from uninsurable and idiosyncratic uncertainty in preferences and employment, where, regarding employment, we distinguish among employment and short- and long-term unemployment. We calibrate the model to match the distribution of wealth in U.S. data and features of transitions between employment and unemployment. Unlike previous studies, we study how business cycles affect different groups of consumers. We conclude that the cost of cycles is small for almost all groups and, indeed, is negative for some.Krebs (2004):
This paper analyzes the welfare costs of business cycles when workers face uninsurable idiosyncratic labor income risk. In accordance with the previous literature, this paper decomposes labor income risk into an aggregate and an idiosyncratic component, but in contrast to the previous literature, this paper allows for multiple sources of idiosyncratic labor income risk. Using the multi-dimensional approach to idiosyncratic risk, this paper provides a general characterization of the welfare cost of business cycles when preferences and the (marginal) process of individual labor income in the economy with business cycles are given. The general analysis shows that the introduction of multiple sources of idiosyncratic risk never decreases the welfare cost of business cycles, and strictly increases it if there are cyclical fluctuations across the different sources of risk. Finally, this paper also provides a quantitative analysis of multi-dimensional labor income risk based on a version of the model that is calibrated to match U.S. labor market data. The quantitative analysis suggests that realistic variations across two particular dimensions of idiosyncratic labor income risk increase the welfare cost of business cycles by a substantial amount.
I study the welfare cost of business cycles in a complete-markets economy where some people are more risk averse than others. Relatively more risk-averse people buy insurance against aggregate risk, and relatively less risk-averse people sell insurance. These trades reduce the welfare cost of business cycles for everyone. Indeed, the least risk-averse people benefit from business cycles. Moreover, even infinitely risk-averse people suffer only finite and, in my empirical estimates, very small welfare losses. In other words, when there are complete insurance markets, aggregate fluctuations in consumption are essentially irrelevant not just for the average person—the surprising finding of Lucas [Lucas, Jr., R.E., 1987. Models of Business Cycles. Basil Blackwell, New York] but for everyone in the economy, no matter how risk averse they are. If business cycles matter, it is because they affect productivity or interact with uninsured idiosyncratic risk, not because aggregate risk per se reduces welfare.Krusell et al. (2009):
We investigate the welfare effects of eliminating business cycles in a model with substantial consumer heterogeneity. The heterogeneity arises from uninsurable and idiosyncratic uncertainty in preferences and employment status. We calibrate the model to match the distribution of wealth in U.S. data and features of transitions between employment and unemployment. In comparison with much of the literature, we find rather large effects. For our benchmark model, we find welfare effects that, on average across all consumers, are of a bit more than one order of magnitude larger than those computed by Lucas [Lucas Jr., R.E., 1987. Models of Business Cycles. Basil Blackwell, New York]. When we distinguish long- from short-term unemployment, long-term unemployment being distinguished by poor (and highly procyclical) employment prospects and low unemployment compensation, the average gain from eliminating cycles is as much as 1% in consumption equivalents. In addition, in both models, there are large differences across groups: very poor consumers gain a lot when cycles are removed (the long-term unemployed as much as around 30%), as do very rich consumers, whereas the majority of consumers—the “middle class”—sees much smaller gains from removing cycles. Inequality also rises substantially upon removing cycles.The above paper has a 2002 working paper that seems to come to different conclusions about the welfare costs of business cycles. Technical appendices are also provided.
Thursday, January 3, 2013
How well do you know your utility function?
This is an excerpt from my teaching notes for an upcoming computational economics lab on the RBC model that I am teaching at the University of Edinburgh that I thought might be of more general interest (mostly because of the cool graphics!)...
In the basic RBC model from Chapter 5 of David Romer's Advanced Macroeconomics, the representative household has the following single period utility function: $$u(C_{t}, l_{t}) = ln(C_{t}) + b\ ln(1 - l_{t})$$ where $C_{t}$ is per capita consumption, $l_{t}$ is labor (note that labor endowment has been normalized to 1!), and $b$ is a parameter (just a weight that the household places on utility from leisure relative to utility from consumption).
First a 3D plot of the utility surface...
Followed by a nice contour plot showing the indifference curves for the agent...
Suppose that the representative household lives for two periods and that there is no uncertainty about future prices. Because of logarithmic preferences, the household will follow the decision rule 'consume a fraction fixed fraction of the PDV of lifetime net worth.' We can derive this decision rule formally as follows. First, note that the household budget constraint is $$C_{0} + \frac{1}{1 + r_{1}}C_{1} = w_{0}l_{0} + \frac{1}{1 + r_{1}}w_{1}l_{1}$$ where $r_{1}$ is the real interest rate. The Lagrangian for the household's two period optimization problem is $$\max_{\{C_{t}\}, \{l_{t}\}} ln(C_{0}) + b\ ln(1-l_{0}) + \beta[ln(C_{1}) + b\ ln(1-l_{1})] + \lambda\left[w_{0}l_{0} + \frac{1}{1 + r_{1}}w_{1}l_{1} - C_{0} - \frac{1}{1 + r_{1}}C_{1}\right]$$ The household now chooses sequences of consumption and labor (i.e., representative household chooses $C_{0}, C_{1}, l_{0}, l_{1}$). The FOC along with the budget constraint imply a system of 5 equations in the 5 unknowns $C_{0}, C_{1}, l_{0}, l_{1}, \lambda$ as follows: $$\begin{align}\frac{1}{C_{0}} - \lambda =& 0 \\ \beta\frac{1}{C_{1}} - \lambda \frac{1}{1+r_{1}}=& 0 \\-\frac{b}{1 - l_{0}} + \lambda w_{0} =& 0 \\-\beta\frac{b}{1 - l_{1}} + \lambda \frac{1}{1 + r_{1}}w_{1} =& 0 \\C_{0} + \frac{1}{1 + r_{1}}C_{1} =& w_{0}l_{0} + \frac{1}{1 + r_{1}}w_{1}l_{1}\end{align}$$ This 5 equation system can be reduced (by eliminating the Lagrange multiplier $\lambda$) to a linear system of 4 equations in 4 unknowns: $$\begin{vmatrix} b & \ 0 & \ w_{0} & 0 \\\ \beta(1 + r_{1}) & -1 & 0 & 0 \\\ 0 & \ b & 0 & w_{1} \\\ 1 & \frac{1}{1 + r_{1}} & -w_{0} & -\frac{1}{1+r_{1}}w_{1} \end{vmatrix} \begin{vmatrix}C_{0} \\\ C_{1} \\\ l_{0} \\\ l_{1}\end{vmatrix} = \begin{vmatrix} w_{0} \\\ 0 \\\ w_{1} \\\ 0\end{vmatrix}$$ The above system can be solved in closde form using some method like Cramer's rule/substitution etc to yield the following optimal sequences/policies for consumption and labor supply: $$\begin{align}C_{0} =& \frac{1}{(1 + b)(1 + \beta)}\left(w_{0} + \frac{1}{1 + r_{1}}w_{1}\right) \\ C_{1} =& \left(\frac{1 + r_{1}}{1 + b}\right)\left(\frac{\beta}{1 + \beta}\right)\left(w_{0} + \frac{1}{1 + r_{1}}w_{1}\right) \\ l_{0} =& 1 - \left(\frac{b}{w_{0}}\right)\left(\frac{1}{(1 + b)(1 + \beta)}\right)\left(w_{0} + \frac{1}{1 + r_{1}}w_{1}\right) \\ l_{1} =& 1 - \left(\frac{b\beta(1+r_{1})}{w_{1}}\right) \left(\frac{1}{(1 + b)(1 + \beta)}\right)\left(w_{0} + \frac{1}{1 + r_{1}}w_{1}\right) \end{align}$$
Several important points to note about the above optimal consumption and labor supply policies:
- Household's lifetime net worth, $w_{0} + \frac{1}{1 + r_{1}}w_{1}$, is the present discounted value of its labor endowment.
- Household's lifetime net worth depends on the wages in BOTH periods and future interest rate. It hints at the more general result that, if the household has an infinite time horizon, lifetime net worth depends on the entire future path of wages and interest rates.
- In each period, household's consume a fraction of their lifetime net worth. Although the fraction changes in this simple two period model, if the household has an infinite horizon, the fraction of lifetime net worth consumed each period will be fixed and equal to $$\frac{1}{(1 + b)(1 + \beta + \beta^2 + \dots)}=\frac{1 - \beta}{1 + b}$$
- From the policy function for $l_{0}$, one can show that in order for the labor supply in period $t=0$ to be non-negative (which it must!), the following inequality must hold: $$\left(\frac{1}{1 + r_{1}}\right)\left(\frac{w_{1}}{w_{0}}\right) \lt \frac{(1 + b)(1 + \beta)}{b} - 1$$
- From the policy function for $l_{1}$, in order for the labor supply in period $t=1$ to be non-negative (which it must!), the following inequality must hold: $$(1 + r_{1})\left(\frac{w_{0}}{w_{1}}\right) \lt \left(\frac{1 + b}{b}\right)\left(\frac{1 + \beta}{\beta}\right) - 1$$
If we specify some prices (i.e., wages in period $t=0, 1$, $w_{0}=5,w_{1}=9$ and the interest rate $r_{1}=0.025$), then we can graphically represent the optimal choices of consumption and labor supply in period $t=0$ and $t=1$ as follows.
Note that with the wage in $t=1$ being almost twice as high as the wage in period $t=0$, the agent pushes his labor supply in period $t=0$, $l_{0}$, almost all the way to zero (i.e., he chooses not to work very much). The agent can still consume because, absent any frictions (things like borrowing constraints, incomplete markets, imperfect contracts, etc), he can easily consume some of his future labor earnings in period $t=1$ in the current period.
In period $t=1$, although the higher wage causes the agent to significantly increases his labor supply, there is no much change in his level of consumption (i.e., there is consumption smoothing!).
As always, code is available on GitHub.
Labels:
Graph of the Day,
matplotlib,
NumPy,
Python,
RBC Models
Python, IPython, and Emacs
For a long time now I have been meaning to move to Emacs fulltime. Today I decided to bite the bullet and dive into setting up Python, IPython, and Emacs on my MacBook. Process was surprisingly painless. Hat tip to Jess Hamrick for this very detailed post that helped get me up and running.
Wednesday, January 2, 2013
Graph of the Day
A busy day (actually trying to do a bit of my own research!)...so I just threw together a plot of the historical civilian unemployment rate using FRED data (similar to figure 1-3 from Mankiw's intermediate macroeconomics textbook). Very boring I know, but tomorrow I promise something a bit more interesting!
If anyone can point me in the direction of the actual data that Mankiw uses to generate the graphs from his textbook I would be very grateful. I can't seem to find it! Code for the above is available on GitHub.
If anyone can point me in the direction of the actual data that Mankiw uses to generate the graphs from his textbook I would be very grateful. I can't seem to find it! Code for the above is available on GitHub.
Labels:
Graph of the Day,
Macroeconomics,
matplotlib,
Pandas,
Python,
Unemployment
Tuesday, January 1, 2013
Graph of the Day
A New Year and a new graph of the day! This graphic actually uses a new Python library, wbdata, for grabbing World Bank data via the World Bank's API. Here is a plot of global inflation over the last 50 odd years for all available countries. I have color-coded the countries according to income group: Low, Lower-Middle, Upper-Middle, or High.
I am not entirely thrilled with this graph. It turned out to be hard to scale the y-axis to capture the full range of the data: Democratic Republic of Congo had an annual inflation rate over 23,000% in 1994! Zimbabwe would have had even higher annual inflation rates but they stopped reporting inflation statistics in 2006 (just prior to the onset of its recent bought of hyperinflation).
As always, code is available on GitHub.
I am not entirely thrilled with this graph. It turned out to be hard to scale the y-axis to capture the full range of the data: Democratic Republic of Congo had an annual inflation rate over 23,000% in 1994! Zimbabwe would have had even higher annual inflation rates but they stopped reporting inflation statistics in 2006 (just prior to the onset of its recent bought of hyperinflation).
As always, code is available on GitHub.
Labels:
Graph of the Day,
Inflation,
matplotlib,
Pandas,
Python,
wbdata
Monday, December 31, 2012
Graph of the Day
Today's graphic is motivated by recent posts by Paul Krugman on implications of capital-biased technological change. In both posts Krugman uses the share of employee compensation (COE) to nominal GDP as his measure of labor's share of income. Although the data for both series go back to 1947, Krugman chooses to drop the data prior to 1973 arguing that 1973 marked the end of the post-WWII economic boom. Put another way, Krugman is saying that there is a structural break in the data generating process for labor's share which makes data prior to 1973 useless (or perhaps actively misleading) if one is interested in thinking about future trends in labor share.
If you are wondering what a plot of the entire time series looks like here is the ratio of COE / GDP from 1947 forward.
It looks like the employee compensation ratio is roughly the same today as it was in 1950 (although obviously heading in different directions!).
In his first post Krugman argues that this measure "fluctuates over the business cycle." Note that the vertical scale ranges only from 0.52 to 0.60. Such a small range will exacerbate fluctuations in the series. Plotting the same data on its natural scale (i.e., 0 to 1), yields the following.
Based on this plot, the measure appears to have been remarkably constant over the past 60 odd years.
Which of these plots gives the more "correct" view of the data? Or does it depend on the point you are trying to make?
As always, code is available.
If you are wondering what a plot of the entire time series looks like here is the ratio of COE / GDP from 1947 forward.
It looks like the employee compensation ratio is roughly the same today as it was in 1950 (although obviously heading in different directions!).
In his first post Krugman argues that this measure "fluctuates over the business cycle." Note that the vertical scale ranges only from 0.52 to 0.60. Such a small range will exacerbate fluctuations in the series. Plotting the same data on its natural scale (i.e., 0 to 1), yields the following.
Based on this plot, the measure appears to have been remarkably constant over the past 60 odd years.
Which of these plots gives the more "correct" view of the data? Or does it depend on the point you are trying to make?
As always, code is available.
Labels:
Graph of the Day,
Labor Economics,
Macroeconomics,
matplotlib,
Pandas,
Python
Friday, December 28, 2012
Graph of the Day
Took a few days off blogging for Christmas and Boxing Day, but am now back at it! Here is a quick plot of historical measures of inflation in the U.S.. I used Pandas to grab the three price indices, and then used a nice built-in Pandas method pct_change(periods)to convert the monthly price indices (i.e., CPIAUCNS and CPIAUCSL) and the quarterly GDP deflator to measures of percentage change in prices from a year ago (which is a standard measure of inflation).
After combining the three series into a single DataFrame object, you can plot all three series with a single line of code!
Unsurprisingly the three measures track one another very closely. Perhaps I should have thrown in some measures of producer prices? Code is available here.
After combining the three series into a single DataFrame object, you can plot all three series with a single line of code!
Unsurprisingly the three measures track one another very closely. Perhaps I should have thrown in some measures of producer prices? Code is available here.
Labels:
Graph of the Day,
Inflation,
Macroeconomics,
matplotlib,
Pandas,
Python
Monday, December 24, 2012
Graph(s) of the Day!
Today's graphic(s) attempt to dispel a common misunderstanding of basic probability theory. We all know that flipping a fair coin will result in heads exactly 50% of the time. Given this, many people seem to think that the Law of Large Numbers (LLN) tells us that the observed number of heads should more or less equal the expected number of heads. This intuition is wrong!
A South African mathematician named John Kerrich was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich spent the next five years in an interment camp where, to pass the time, he carried out a series of experiments in probability theory...including an experiment where he flipped a coin by hand 10,000 times! He apparently also used ping-pong balls to demonstrate Bayes theorem.
After the war Kerrich was released and published the results of many of his experiments. I have copied the table of the coin flipping results reported by Kerrich below (and included a csv file on GitHub). The first two collumns are self explanatory, the third column, Difference, is the difference between the observed number of heads and the expected number of heads.
Below I plot the data in the third column: the difference between the observed number of heads and the expected number of heads is diverging (which is the exact opposite of most peoples' intuition)!
Perhaps Kerrich made a mistake (he didn't), but we can check his results via simulation! First, a single replication of T = 10,000 flips of a fair coin...
Again, we observe divergence (but this time in the opposite direction!). For good measure, I ran N=100 replications of the same experiment (i.e., flipping a coin T=10,000 times). The result is the following nice graphic...
Our simulations suggest that Kerrich's result was indeed typical. The LLN does not say that as T increases the observed number of heads will be close to the expected number of heads! What the LLN says instead is that, as T increases, the average number of heads will get closer and closer to the true population average (which in this case, with our fair coin, is 0.5).
A South African mathematician named John Kerrich was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich spent the next five years in an interment camp where, to pass the time, he carried out a series of experiments in probability theory...including an experiment where he flipped a coin by hand 10,000 times! He apparently also used ping-pong balls to demonstrate Bayes theorem.
After the war Kerrich was released and published the results of many of his experiments. I have copied the table of the coin flipping results reported by Kerrich below (and included a csv file on GitHub). The first two collumns are self explanatory, the third column, Difference, is the difference between the observed number of heads and the expected number of heads.
| Tosses | Heads | Difference |
| 10 | 4 | -1 |
| 20 | 10 | 0 |
| 30 | 17 | 2 |
| 40 | 21 | 1 |
| 50 | 25 | 0 |
| 60 | 29 | -1 |
| 70 | 32 | -3 |
| 80 | 35 | -5 |
| 90 | 40 | -5 |
| 100 | 44 | -6 |
| 200 | 98 | -2 |
| 300 | 146 | -4 |
| 400 | 199 | -1 |
| 500 | 255 | 5 |
| 600 | 312 | 12 |
| 700 | 368 | 18 |
| 800 | 413 | 13 |
| 900 | 458 | 8 |
| 1000 | 502 | 2 |
| 2000 | 1013 | 13 |
| 3000 | 1510 | 10 |
| 4000 | 2029 | 29 |
| 5000 | 2533 | 33 |
| 6000 | 3009 | 9 |
| 7000 | 3516 | 16 |
| 8000 | 4034 | 34 |
| 9000 | 4538 | 38 |
| 10000 | 5067 | 67 |
Perhaps Kerrich made a mistake (he didn't), but we can check his results via simulation! First, a single replication of T = 10,000 flips of a fair coin...
Again, we observe divergence (but this time in the opposite direction!). For good measure, I ran N=100 replications of the same experiment (i.e., flipping a coin T=10,000 times). The result is the following nice graphic...
Our simulations suggest that Kerrich's result was indeed typical. The LLN does not say that as T increases the observed number of heads will be close to the expected number of heads! What the LLN says instead is that, as T increases, the average number of heads will get closer and closer to the true population average (which in this case, with our fair coin, is 0.5).
Let's run another simulation to verify that the LLN actually holds. In the experiment I conduct N=100 runs of T=10,000 coin flips. For each of the runs I re-compute the sample average after each successive flip.
As always code and data are available! Enjoy.
Labels:
Graph of the Day,
matplotlib,
NumPy,
Pandas,
SciPy,
Statistics
Sunday, December 23, 2012
Graph of the Day
Earlier this week I used Pandas to grab some historical data on the S&P 500 from Yahoo!Finance and generate a simple time series plot. Today, I am going to re-examine this data set in order to show the importance of scaling and adjusting for inflation when plotting economic data.
I again use the functions from the pandas.io.data to grab the data. Specifically, I use get_data_yahoo('^GSPC') to get the S&P 500 time series, and get_data_fred('CPIAUCSL')to grab the consumer price index (CPI). Here is a naive plot of historical S&P 500 returns from 1950 through 2012 (as usual, includes grey NBER recession bands).
Note that, because the CPI data are monthly frequency, I resample the daily S&P 500 data by taking monthly averages. This plot might make you conclude that there was a massive structural break/regime change around the year 2000 in whatever underlying process is generating the S&P 500. However, as the level of the S&P 500 increases, the linear scale on the vertical axis makes changes from month to month seem more dramatic. To control for this, I simply make the vertical scale logarithmic (now equal distances on the vertical axis represent equal percentage changes in the S&P 500).
Now the "obvious" structural break in the year 2000 no longer seems so obvious. Indeed there was a period of roughly 10-15 years during the late 1960's through 1970's during which the S&P 500 basically moved sideways in a similar manner to what we have experienced during the last 10+ years.
This brings us to another, more significant, problem with these graphs: neither or them adjusts for inflation! When plotting long economic time series it is always a good idea to adjust for inflation. The 1970's was a period of fairly high inflation in the U.S., thus the fact that the nominal value of the S&P 500 didn't change all that much over this period tells us that, in real terms, the value of the S&P 500 fell considerably.
Using the CPI data from FRED, it is straight-forward to convert the nominal value of the S&P 500 index to a real value for some base month/year. Below is a plot of the real S&P 500 (in Nov. 2012 Dollars), with a logarithmic scale on the vertical axis. As expected, the real S&P 500 declined significantly during the 1970's.
Code is available on GitHub. Enjoy!
I again use the functions from the pandas.io.data to grab the data. Specifically, I use get_data_yahoo('^GSPC') to get the S&P 500 time series, and get_data_fred('CPIAUCSL')to grab the consumer price index (CPI). Here is a naive plot of historical S&P 500 returns from 1950 through 2012 (as usual, includes grey NBER recession bands).
Note that, because the CPI data are monthly frequency, I resample the daily S&P 500 data by taking monthly averages. This plot might make you conclude that there was a massive structural break/regime change around the year 2000 in whatever underlying process is generating the S&P 500. However, as the level of the S&P 500 increases, the linear scale on the vertical axis makes changes from month to month seem more dramatic. To control for this, I simply make the vertical scale logarithmic (now equal distances on the vertical axis represent equal percentage changes in the S&P 500).
Now the "obvious" structural break in the year 2000 no longer seems so obvious. Indeed there was a period of roughly 10-15 years during the late 1960's through 1970's during which the S&P 500 basically moved sideways in a similar manner to what we have experienced during the last 10+ years.
This brings us to another, more significant, problem with these graphs: neither or them adjusts for inflation! When plotting long economic time series it is always a good idea to adjust for inflation. The 1970's was a period of fairly high inflation in the U.S., thus the fact that the nominal value of the S&P 500 didn't change all that much over this period tells us that, in real terms, the value of the S&P 500 fell considerably.
Using the CPI data from FRED, it is straight-forward to convert the nominal value of the S&P 500 index to a real value for some base month/year. Below is a plot of the real S&P 500 (in Nov. 2012 Dollars), with a logarithmic scale on the vertical axis. As expected, the real S&P 500 declined significantly during the 1970's.
Code is available on GitHub. Enjoy!
Saturday, December 22, 2012
Graph(s) of the Day
Suppose large number of identical firms in a perfectly competitive industry with constant returns to scale (CRTS) Cobb-Douglas production functions: \[Y = F(K, L) = K^{\alpha}(AL)^{1 - \alpha}\] Output, Y, is a homogenous of degree one function of capital, K, labor, L, and technology, A, is labor augmenting.
Typically, we economists model firms as choosing demands for capital and labor in order to maximize profits while taking prices as given (i.e., unaffected by the decisions of the individual firm):\[\max_{K,L} \Pi = K^{\alpha}(AL)^{1 - \alpha} - (wL + rK)\] where the prices are $1, w, r$. Note that I am following convention in assuming that the price of the output good is the numeraire (i.e., normalized to 1) and thus the real wage, $w$, and the return to capital, $r$, are both relative prices expressed in terms of units of the output good.
The first order conditions (FOCs) of a typical firms maximization problem are \[\begin{align}\frac{\partial \Pi}{\partial K}=&0 \implies r = \alpha K^{\alpha-1}(AL)^{1 - \alpha} \label{MPK}\\
\frac{\partial \Pi}{\partial L}=&0 \implies w = (1 - \alpha) K^{\alpha}(AL)^{-\alpha}A \label{MPL}\end{align}\] Dividing $\ref{MPK}$ by $\ref{MPL}$ (and a bit of algebra) yields the following equation for the optimal capital/labor ratio: \[\frac{K}{L} = \left(\frac{\alpha}{1 - \alpha}\right)\left(\frac{w}{r}\right)\] The fact that, for a given set of prices $w$, $r$, the optimal choices of $K$ and $L$ are indeterminate (any ratio of $K$ and $L$ satisfying the above condition will do) implies that the optimal scale of the firm is also indeterminate.
How can I create a graphic that clearly demonstrates this property of the CRTS production function? I can start by fixing values for the wage and return to capital and then creating contour plots of the production frontier and the cost surface.
The above contour plots are drawn for $w\approx0.84$ and $r\approx0.21$ (which implies an optimal capital/labor ratio of 2:1). You should recognize the contour plot for the production surface (left) from a previous post. The contour plot of the cost surface (right) is a simple plane (which is why the isocost lines are lines and not curves!). Combining the contour plots allows one to see the set of tangency points between isoquants and isocosts.
A firm manager is indifferent between each of these points of tangency, and thus the size/scale of the firm is indeterminate. Indeed, with CRTS a firm will earn zero profits at each of the tangency points in the above contour plot.
As usual, the code is available on GitHub.
Update: Installing MathJax on my blog to render mathematical equations was easy (just a quick cut and paste job).
Typically, we economists model firms as choosing demands for capital and labor in order to maximize profits while taking prices as given (i.e., unaffected by the decisions of the individual firm):\[\max_{K,L} \Pi = K^{\alpha}(AL)^{1 - \alpha} - (wL + rK)\] where the prices are $1, w, r$. Note that I am following convention in assuming that the price of the output good is the numeraire (i.e., normalized to 1) and thus the real wage, $w$, and the return to capital, $r$, are both relative prices expressed in terms of units of the output good.
The first order conditions (FOCs) of a typical firms maximization problem are \[\begin{align}\frac{\partial \Pi}{\partial K}=&0 \implies r = \alpha K^{\alpha-1}(AL)^{1 - \alpha} \label{MPK}\\
\frac{\partial \Pi}{\partial L}=&0 \implies w = (1 - \alpha) K^{\alpha}(AL)^{-\alpha}A \label{MPL}\end{align}\] Dividing $\ref{MPK}$ by $\ref{MPL}$ (and a bit of algebra) yields the following equation for the optimal capital/labor ratio: \[\frac{K}{L} = \left(\frac{\alpha}{1 - \alpha}\right)\left(\frac{w}{r}\right)\] The fact that, for a given set of prices $w$, $r$, the optimal choices of $K$ and $L$ are indeterminate (any ratio of $K$ and $L$ satisfying the above condition will do) implies that the optimal scale of the firm is also indeterminate.
How can I create a graphic that clearly demonstrates this property of the CRTS production function? I can start by fixing values for the wage and return to capital and then creating contour plots of the production frontier and the cost surface.
The above contour plots are drawn for $w\approx0.84$ and $r\approx0.21$ (which implies an optimal capital/labor ratio of 2:1). You should recognize the contour plot for the production surface (left) from a previous post. The contour plot of the cost surface (right) is a simple plane (which is why the isocost lines are lines and not curves!). Combining the contour plots allows one to see the set of tangency points between isoquants and isocosts.
A firm manager is indifferent between each of these points of tangency, and thus the size/scale of the firm is indeterminate. Indeed, with CRTS a firm will earn zero profits at each of the tangency points in the above contour plot.
As usual, the code is available on GitHub.
Update: Installing MathJax on my blog to render mathematical equations was easy (just a quick cut and paste job).
Friday, December 21, 2012
Gun control...
Via Mark Thoma, Steve Williamson has an excellent post about the economics of gun control:
What's the problem here? People buy guns for three reasons: (i) they want to shoot animals with them; (ii) they want to shoot people with them; (iii) they want to threaten people with them. There are externalities. Gun manufacturers and retailers profit from the sale of guns. The people who buy the guns and use them seem to enjoy having them. But there are third parties who suffer. People shooting at animals can hit people. People who buy guns intending to protect themselves may shoot people who in fact intend no harm. People may temporarily feel compelled to harm others, and want an efficient instrument to do it with.
There are also information problems. It may be difficult to determine who is a hunter, who is temporarily not in their right mind, and who wants to put a loaded weapon in the bedside table.
What do economists know? We know something about information problems, and we know something about mitigating externalities. Let's think first about the information problems. Here, we know that we can make some headway by regulating the market so that it becomes segmented, with these different types of people self-selecting. This one is pretty obvious, and is a standard part of the conversation. Guns for hunting do not need to be automatic or semi-automatic, they do not need to have large magazines, and they do not have to be small. If hunting weapons do not have these properties, who would want to buy them for other purposes?
On the externality problem, we can be more inventive. A standard tool for dealing with externalities is the Pigouvian tax. Tax the source of the bad externality, and you get less of it. How big should the tax be? An unusual problem here is that the size of the externality is random - every gun is not going to injure or kill someone. There's also an inherent moral hazard problem, in that the size of the externality depends on the care taken by the gunowner. Did he or she properly train himself or herself? Did they store their weapon to decrease the chance of an accident?
What's the value of a life? I think when economists ask that question, lay people are offended. I'm thinking about it now, and I'm offended too. If someone offered me \$5 million for my cat, let alone another human being, I wouldn't take it.
In any case, the Pigouvian tax we would need to correct the externality should be a large one, and it could generate a lot of revenue. If there are 300 million guns in the United States, and we impose a tax of \$3600 per gun on the current stock, we would eliminate the federal government deficit. But \$3600 is coming nowhere close to the potential damage that a single weapon could cause. A potential solution would be to have a gun-purchaser post collateral - several million dollars in assets - that could be confiscated in the event that the gun resulted in injury or loss of life. This has the added benefit of mitigating the moral hazard problem - the collateral is lost whether the damage is "accidental" or caused by, for example, someone who steals the gun.
Of course, once we start thinking about the size of the tax (or collateral) needed to correct the inefficiency that exists here, we'll probably come to the conclusion that it is more efficient just to ban particular weapons and ammunition at the point of manufacture. I think our legislators should take that as far as it goes.
Graph of the Day
Today's graphic demonstrates the use of Pandas to grab data from Yahoo!Finance. The code I wrote uses pandas.io.get_data_yahoo() to grab historical daily data on the S&P 500 index and then generates a simple time series plot. I went ahead and added the NBER recession bars for good measure. Note the use of a logarithmic scale on the vertical axis. Enjoy!
Thursday, December 20, 2012
Graph of the Day
Today's graph is a combined 3D plot of the production frontier associated with the constant returns to scale Cobb-Douglas production function and a contour plot showing the isoquants of the production frontier. This static snapshot was written up using matplotlib (the code also includes an interactive version of the 3D production frontier implemented in Mayavi).
At some point I will figure out how to embed the interactive Mayavi plot into a blog post so that readers can manipulate the plot and change parameter values. If anyone knows how to do this already, a pointer would be much appreciated!
At some point I will figure out how to embed the interactive Mayavi plot into a blog post so that readers can manipulate the plot and change parameter values. If anyone knows how to do this already, a pointer would be much appreciated!
Wednesday, December 19, 2012
Blogging to resume again!
It has been far too long since my last post. Life (becoming a father), travel (summer research trip to SFI), teaching (am teaching a course on Computational Economics), and research (also trying to finish my PhD!) have a way of getting in the way of my blogging. As a mechanism to slowly move back into the blog world, I have decided to start a 'Graphic of the Day' series. Each day I will create a new economic graphic using my favorite Python libraries (mostly Pandas, matplotlib, NumPy/Scipy).
The inaugural 'Graph of the Day' is Figure 1-1 from Mankiw's intermediate undergraduate textbook Macroeconomics.
Real GDP measures the total income of everyone in the economy, and real GDP per person measures the income of the average person in the economy. The figure shows that real GDP per person tends to grow over time and that this normal growth is sometimes interrupted by period of declining income (i.e., the grey NBER bars!), called recessions or depressions.
Note that Real GDP per person is plotted on a logarithmic scale. On such a scale equal distances on the vertical axis represent equal percentage changes. This is why the distance between \$8,000 and \$16,000 (a 100% increase) is the same as the distance between \$32,000 and \$64,000 (also a 100% increase).
The Python code is available on GitHub for download (I used pandas.io.data.get_data_fred() to grab the data). The graphic is a bit boring. I was a bit depressed to find that the longest time series for U.S. per capita real GDP only goes back to 1960! This seems a bit scandalous...but perhaps I was just using the wrong data tags!
The inaugural 'Graph of the Day' is Figure 1-1 from Mankiw's intermediate undergraduate textbook Macroeconomics.
Real GDP measures the total income of everyone in the economy, and real GDP per person measures the income of the average person in the economy. The figure shows that real GDP per person tends to grow over time and that this normal growth is sometimes interrupted by period of declining income (i.e., the grey NBER bars!), called recessions or depressions.
Note that Real GDP per person is plotted on a logarithmic scale. On such a scale equal distances on the vertical axis represent equal percentage changes. This is why the distance between \$8,000 and \$16,000 (a 100% increase) is the same as the distance between \$32,000 and \$64,000 (also a 100% increase).
The Python code is available on GitHub for download (I used pandas.io.data.get_data_fred() to grab the data). The graphic is a bit boring. I was a bit depressed to find that the longest time series for U.S. per capita real GDP only goes back to 1960! This seems a bit scandalous...but perhaps I was just using the wrong data tags!
Subscribe to:
Posts (Atom)


















.png)
.png)
.png)





