my-markdown-library 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/F24LS_md/ Lecture 4 - Public.md +347 -0
- data/F24LS_md/Lecture 1 - Introduction and Overview.md +327 -0
- data/F24LS_md/Lecture 10 - Development_.md +631 -0
- data/F24LS_md/Lecture 11 - Econometrics.md +345 -0
- data/F24LS_md/Lecture 12 - Finance.md +692 -0
- data/F24LS_md/Lecture 13 - Environmental Economics.md +299 -0
- data/F24LS_md/Lecture 15 - Conclusion.md +272 -0
- data/F24LS_md/Lecture 2 - Demand.md +349 -0
- data/F24LS_md/Lecture 3 - Supply.md +329 -0
- data/F24LS_md/Lecture 5 - Production C-D.md +291 -0
- data/F24LS_md/Lecture 6 - Utility and Latex.md +440 -0
- data/F24LS_md/Lecture 7 - Inequality.md +607 -0
- data/F24LS_md/Lecture 8 - Macroeconomics.md +704 -0
- data/F24LS_md/Lecture 8 - Macro.md +700 -0
- data/F24LS_md/Lecture 9 - Game Theory_.md +436 -0
- data/F24LS_md/summary.yaml +105 -0
- data/F24Lec_MD/LecNB_summary.yaml +206 -0
- data/F24Lec_MD/lec01/lec01.md +267 -0
- data/F24Lec_MD/lec02/Avocados_demand.md +425 -0
- data/F24Lec_MD/lec02/Demand_Steps_24.md +126 -0
- data/F24Lec_MD/lec02/PriceElasticity.md +83 -0
- data/F24Lec_MD/lec02/ScannerData_Beer.md +171 -0
- data/F24Lec_MD/lec02/demand-curve-Fa24.md +213 -0
- data/F24Lec_MD/lec03/3.0-CubicCostCurve.md +239 -0
- data/F24Lec_MD/lec03/3.1-Supply.md +274 -0
- data/F24Lec_MD/lec03/3.2-sympy.md +332 -0
- data/F24Lec_MD/lec03/3.3a-california-energy.md +120 -0
- data/F24Lec_MD/lec03/3.3b-a-really-hot-tuesday.md +121 -0
- data/F24Lec_MD/lec04/lec04-CSfromSurvey-closed.md +335 -0
- data/F24Lec_MD/lec04/lec04-CSfromSurvey.md +331 -0
- data/F24Lec_MD/lec04/lec04-Supply-Demand-closed.md +519 -0
- data/F24Lec_MD/lec04/lec04-Supply-Demand.md +514 -0
- data/F24Lec_MD/lec04/lec04-four-plot-24.md +34 -0
- data/F24Lec_MD/lec04/lec04-four-plot.md +34 -0
- data/F24Lec_MD/lec05/Lec5-Cobb-Douglas.md +131 -0
- data/F24Lec_MD/lec05/Lec5-CobbD-AER1928.md +283 -0
- data/F24Lec_MD/lec06/6.1-Sympy-Differentiation.md +253 -0
- data/F24Lec_MD/lec06/6.2-3D-utility.md +287 -0
- data/F24Lec_MD/lec06/6.3-QuantEcon-Optimization.md +399 -0
- data/F24Lec_MD/lec06/6.4-latex.md +138 -0
- data/F24Lec_MD/lec06/6.5-Edgeworth.md +269 -0
- data/F24Lec_MD/lec07/7.1-inequality.md +283 -0
- data/F24Lec_MD/lec07/7.2-historical-inequality.md +237 -0
- data/F24Lec_MD/lec08/macro-fred-api.md +313 -0
- data/F24Lec_MD/lec09/lecNB-prisoners-dilemma.md +88 -0
- data/F24Lec_MD/lec10/Lec10.2-waterguard.md +401 -0
- data/F24Lec_MD/lec10/lec10.1-mapping.md +199 -0
- data/F24Lec_MD/lec11/11.1-slr.md +305 -0
- data/F24Lec_MD/lec11/11.2-mlr.md +171 -0
- data/F24Lec_MD/lec12/Lec12-4-PersonalFinance.md +590 -0
- data/F24Lec_MD/lec12/lec12-1_Interest_Payments.md +267 -0
- data/F24Lec_MD/lec12/lec12-2-stocks-options.md +235 -0
- data/F24Lec_MD/lec13/Co2_ClimateChange.md +139 -0
- data/F24Lec_MD/lec13/ConstructingMAC.md +213 -0
- data/F24Lec_MD/lec13/EmissionsTracker.md +170 -0
- data/F24Lec_MD/lec13/KuznetsHypothesis.md +219 -0
- data/F24Lec_MD/lec13/RoslingPlots.md +217 -0
- data/F24Lec_MD/lec15/vibecession.md +485 -0
- data/F24Textbook_MD/00-intro/index.md +292 -0
- data/F24Textbook_MD/01-demand/01-demand.md +152 -0
- data/F24Textbook_MD/01-demand/02-example.md +131 -0
- data/F24Textbook_MD/01-demand/03-log-log.md +284 -0
- data/F24Textbook_MD/01-demand/04-elasticity.md +248 -0
- data/F24Textbook_MD/01-demand/index.md +15 -0
- data/F24Textbook_MD/02-supply/01-supply.md +203 -0
- data/F24Textbook_MD/02-supply/02-eep147-example.md +86 -0
- data/F24Textbook_MD/02-supply/03-sympy.md +138 -0
- data/F24Textbook_MD/02-supply/04-market-equilibria.md +204 -0
- data/F24Textbook_MD/02-supply/index.md +16 -0
- data/F24Textbook_MD/03-public/govt-intervention.md +73 -0
- data/F24Textbook_MD/03-public/index.md +10 -0
- data/F24Textbook_MD/03-public/surplus.md +351 -0
- data/F24Textbook_MD/03-public/taxes-subsidies.md +282 -0
- data/F24Textbook_MD/04-production/index.md +15 -0
- data/F24Textbook_MD/04-production/production.md +178 -0
- data/F24Textbook_MD/04-production/shifts.md +296 -0
- data/F24Textbook_MD/05-utility/budget-constraints.md +166 -0
- data/F24Textbook_MD/05-utility/index.md +15 -0
- data/F24Textbook_MD/05-utility/utility.md +136 -0
- data/F24Textbook_MD/06-inequality/historical-inequality.md +253 -0
- data/F24Textbook_MD/06-inequality/index.md +15 -0
- data/F24Textbook_MD/06-inequality/inequality.md +226 -0
- data/F24Textbook_MD/07-game-theory/bertrand.md +257 -0
- data/F24Textbook_MD/07-game-theory/cournot.md +333 -0
- data/F24Textbook_MD/07-game-theory/equilibria-oligopolies.md +96 -0
- data/F24Textbook_MD/07-game-theory/expected-utility.md +61 -0
- data/F24Textbook_MD/07-game-theory/index.md +19 -0
- data/F24Textbook_MD/07-game-theory/python-classes.md +340 -0
- data/F24Textbook_MD/08-development/index.md +35 -0
- data/F24Textbook_MD/09-macro/CentralBanks.md +101 -0
- data/F24Textbook_MD/09-macro/Indicators.md +77 -0
- data/F24Textbook_MD/09-macro/fiscal_policy.md +36 -0
- data/F24Textbook_MD/09-macro/index.md +14 -0
- data/F24Textbook_MD/09-macro/is_curve.md +76 -0
- data/F24Textbook_MD/09-macro/phillips_curve.md +70 -0
- data/F24Textbook_MD/10-finance/index.md +10 -0
- data/F24Textbook_MD/10-finance/options.md +178 -0
- data/F24Textbook_MD/10-finance/value-interest.md +60 -0
- data/F24Textbook_MD/11-econometrics/index.md +16 -0
- data/F24Textbook_MD/11-econometrics/multivariable.md +218 -0
- data/F24Textbook_MD/11-econometrics/reading-econ-papers.md +25 -0
- data/F24Textbook_MD/11-econometrics/single-variable.md +483 -0
- data/F24Textbook_MD/11-econometrics/statsmodels.md +58 -0
- data/F24Textbook_MD/12-environmental/KuznetsHypothesis-Copy1.md +187 -0
- data/F24Textbook_MD/12-environmental/KuznetsHypothesis.md +187 -0
- data/F24Textbook_MD/12-environmental/MAC.md +254 -0
- data/F24Textbook_MD/12-environmental/index.md +36 -0
- data/F24Textbook_MD/LICENSE.md +11 -0
- data/F24Textbook_MD/intro.md +26 -0
- data/F24Textbook_MD/references.md +25 -0
- data/F24Textbook_MD/summary.yaml +414 -0
- metadata +155 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
---
|
2
|
+
title: "Lec5-Cobb-Douglas"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 5
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec05/Lec5-Cobb-Douglas.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
<table style="width: 100%;">
|
9
|
+
<tr style="background-color: transparent;"><td>
|
10
|
+
<img src="https://data-88e.github.io/assets/images/blue_text.png" width="250px" style="margin-left: 0;" />
|
11
|
+
</td><td>
|
12
|
+
<p style="text-align: right; font-size: 10pt;"><strong>Economic Models</strong>, Fall 2024<br>
|
13
|
+
Dr. Eric Van Dusen
|
14
|
+
</p></td></tr>
|
15
|
+
</table>
|
16
|
+
|
17
|
+
# Cobb-Douglas Regression and Penn World Table
|
18
|
+
|
19
|
+
The following code is in Pandas - a more advanced data science library you are not required to know.
|
20
|
+
Just understanding the outputs of the following cells should be good!
|
21
|
+
|
22
|
+
```python
|
23
|
+
import pandas as pd
|
24
|
+
import numpy as np
|
25
|
+
import matplotlib.pyplot as plt
|
26
|
+
import scipy.stats
|
27
|
+
```
|
28
|
+
|
29
|
+
```python
|
30
|
+
pwt = pd.read_excel('pwt1001.xlsx', sheet_name='Data')
|
31
|
+
pwt
|
32
|
+
```
|
33
|
+
|
34
|
+
```python
|
35
|
+
pwt['countrycode'].unique()
|
36
|
+
```
|
37
|
+
|
38
|
+
```python
|
39
|
+
#Graphs of ln(Y/L) vs ln(K/L)
|
40
|
+
def graph_cobbs(ccodes, begin_date, end_date):
|
41
|
+
for ccode in ccodes:
|
42
|
+
first = pwt.loc[pwt["countrycode"] == ccode]
|
43
|
+
second = first[ (first['year'] >= begin_date) & (first['year'] <= end_date) ]
|
44
|
+
third = {}
|
45
|
+
third['Y'] = second['cgdpe'] / second['cgdpe'].iloc[0]
|
46
|
+
third['K'] = second['cn'] / second['cn'].iloc[0]
|
47
|
+
third['L'] = second['emp'] / second['emp'].iloc[0]
|
48
|
+
third['YL'] = third['Y'] /third['L']
|
49
|
+
third['KL'] = third['K'] /third['L']
|
50
|
+
third['lnYL'] = np.log(third['YL'])
|
51
|
+
third['lnKL'] = np.log(third['KL'])
|
52
|
+
third = pd.DataFrame(third)
|
53
|
+
third = third.dropna()
|
54
|
+
if(len(third)>0):
|
55
|
+
f = plt.figure()
|
56
|
+
ax = f.add_subplot(111)
|
57
|
+
ax.scatter(third['lnKL'], third['lnYL'], label='')
|
58
|
+
m, b, r_value, p_value, std_err = scipy.stats.linregress(third['lnKL'], third['lnYL'])
|
59
|
+
ax.plot(third['lnKL'], m*third['lnKL'] + b, label='y = %.4f x + %.4f \n$R^2$ = %.4f' %(m, b, r_value**2))
|
60
|
+
ax.legend()
|
61
|
+
ax.set_xlabel('ln(K/L)')
|
62
|
+
ax.set_ylabel('ln(Y/L)')
|
63
|
+
plt.grid()
|
64
|
+
ax.set_title(second['country'].iloc[0] + ' (' + ccode + '): '+ str(begin_date) + ' to '+ str(end_date))
|
65
|
+
ax.text(0.0, 0.0, "Data Source: Penn World Tables", color='blue', fontstyle='italic', transform=f.transFigure)
|
66
|
+
# plt.savefig('Cobb-Douglas-' + ccode + '.png')
|
67
|
+
plt.show()
|
68
|
+
```
|
69
|
+
|
70
|
+
```python
|
71
|
+
ccodes =['NER','SDN', 'IND', 'CHN', 'NOR', 'USA']
|
72
|
+
graph_cobbs(ccodes, 1994, 2017)
|
73
|
+
```
|
74
|
+
|
75
|
+
The coefficient of $x$ represents the relative impact of capital ($K$) on GDP ($Y$). Here is a brief refresher on $\alpha$ and the log form of the Cobb Douglas equation (seen above):
|
76
|
+
$$\ln{\frac{Y}{L}} = \alpha \ln{\frac{K}{L}} + A$$
|
77
|
+
$$e^{\ln{\frac{Y}{L}}} = e^{\alpha \ln{\frac{K}{L}} + A}$$
|
78
|
+
$$\frac{Y}{L} = (\frac{K}{L})^{\alpha} * e^{A}$$
|
79
|
+
$$ Y = A K^{\alpha} L^{1 - \alpha}$$
|
80
|
+
|
81
|
+
```python
|
82
|
+
graph_cobbs(['RUS'], 1994, 2017)
|
83
|
+
graph_cobbs(['RUS'], 2003, 2017)
|
84
|
+
```
|
85
|
+
|
86
|
+
```python
|
87
|
+
#Graphs of Y and K vs time
|
88
|
+
begin_date = 1994
|
89
|
+
end_date = 2017
|
90
|
+
ccodes = pwt.countrycode.unique().tolist()
|
91
|
+
ccodes =['ZWE', 'RUS', 'CHN', 'VEN']
|
92
|
+
for ccode in ccodes:
|
93
|
+
first = pwt.loc[pwt["countrycode"] == ccode]
|
94
|
+
second = first[ (first['year'] >= begin_date) & (first['year'] <= end_date) ]
|
95
|
+
third = {}
|
96
|
+
third['year'] = second['year']
|
97
|
+
third['Y'] = second['cgdpe'] / second['cgdpe'].iloc[0]
|
98
|
+
third['K'] = second['cn'] / second['cn'].iloc[0]
|
99
|
+
third['L'] = second['emp'] / second['emp'].iloc[0]
|
100
|
+
third['YL'] = third['Y'] /third['L']
|
101
|
+
third['KL'] = third['K'] /third['L']
|
102
|
+
third['lnYL'] = np.log(third['YL'])
|
103
|
+
third['lnKL'] = np.log(third['KL'])
|
104
|
+
third = pd.DataFrame(third)
|
105
|
+
third = third.dropna()
|
106
|
+
if(len(third)>0):
|
107
|
+
f = plt.figure(figsize=(10,6))
|
108
|
+
ax = f.add_subplot(111)
|
109
|
+
ax.scatter(third['year'], third['Y'], label='')
|
110
|
+
m_y, b_y, r_value_y, p_value_y, std_err_y = scipy.stats.linregress(third['year'], third['Y'])
|
111
|
+
ax.plot(third['year'], m_y*third['year'] + b_y, label='Y = %.4f x + %.4f \n$R^2$ = %.4f' %(m_y, b_y, r_value_y**2))
|
112
|
+
ax.scatter(third['year'], third['K'], label='')
|
113
|
+
m_k, b_k, r_value_k, p_value_k, std_err_k = scipy.stats.linregress(third['year'], third['K'])
|
114
|
+
ax.plot(third['year'], m_k*third['year'] + b_k, label='K = %.4f x + %.4f \n$R^2$ = %.4f' %(m_k, b_k, r_value_k**2))
|
115
|
+
ax.scatter(third['year'], third['L'], label='')
|
116
|
+
m_l, b_l, r_value_l, p_value_l, std_err_l = scipy.stats.linregress(third['year'], third['L'])
|
117
|
+
ax.plot(third['year'], m_l*third['year'] + b_l, label='L = %.4f x + %.4f \n$R^2$ = %.4f' %(m_l, b_l, r_value_l**2))
|
118
|
+
ax.legend()
|
119
|
+
ax.set_xlabel('Year')
|
120
|
+
ax.set_ylabel('Y/K')
|
121
|
+
plt.grid()
|
122
|
+
ax.set_title(second['country'].iloc[0] + ' (' + ccode + '): '+ str(begin_date) + ' to '+ str(end_date))
|
123
|
+
ax.text(0.0, 0.0, "Data Source: Penn World Tables", color='blue', fontstyle='italic', transform=f.transFigure)
|
124
|
+
# plt.savefig('Cobb-Douglas-' + ccode + '.png')
|
125
|
+
plt.show()
|
126
|
+
```
|
127
|
+
|
128
|
+
```python
|
129
|
+
|
130
|
+
```
|
131
|
+
|
@@ -0,0 +1,283 @@
|
|
1
|
+
---
|
2
|
+
title: "Lec5-CobbD-AER1928"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 5
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec05/Lec5-CobbD-AER1928.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
<table style="width: 100%;">
|
9
|
+
<tr style="background-color: transparent;"><td>
|
10
|
+
<img src="https://data-88e.github.io/assets/images/blue_text.png" width="250px" style="margin-left: 0;" />
|
11
|
+
</td><td>
|
12
|
+
<p style="text-align: right; font-size: 10pt;"><strong>Economic Models</strong>, Fall 2024<br>
|
13
|
+
Dr. Eric Van Dusen <br>
|
14
|
+
</p></td></tr>
|
15
|
+
</table>
|
16
|
+
|
17
|
+
## Recreating Key Parts of the Cobb-Douglas Seminal Journal Article from 1928
|
18
|
+
|
19
|
+
This Jupyter Notebook aims to recreate and analyze key parts of the seminal journal article "A Theory of Production" by Charles W. Cobb and Paul H. Douglas, published in the American Economic Review in 1928. The article introduced the Cobb-Douglas production function, which has become a fundamental concept in economics.
|
20
|
+
|
21
|
+
### Overview
|
22
|
+
|
23
|
+
The Cobb-Douglas production function is a mathematical representation of the relationship between inputs (capital and labor) and output in the production process. The function is typically expressed as:
|
24
|
+
$$
|
25
|
+
Y = A \cdot K^\alpha \cdot L^\beta
|
26
|
+
$$
|
27
|
+
|
28
|
+
Where:
|
29
|
+
|
30
|
+
- \( Y \) is the total production (output),
|
31
|
+
- \( A \) is the total factor productivity,
|
32
|
+
- \( K \) is the capital input,
|
33
|
+
- \( L \) is the labor input,
|
34
|
+
- \( $\alpha$\) and \( $\beta$ \) are the output elasticities of capital and labor, respectively.
|
35
|
+
- In the simplified CD formulation \( $\alpha$\) + \( $\beta$ \) = 1
|
36
|
+
|
37
|
+
### Data and Analysis
|
38
|
+
|
39
|
+
**Dataset was manually compiled from the original article and is available in a csv file . The dataset contains historical data on labor, capital, and output for the United States from 1909 to 1929.**
|
40
|
+
|
41
|
+
|
42
|
+
In this notebook, we will:
|
43
|
+
1. Load and explore the historical data used by Cobb and Douglas.
|
44
|
+
2. Visualize the data to understand the trends in labor, capital, and output over time.
|
45
|
+
3. Estimate the parameters \( $\alpha$ \) and \( $A$ \) of the Cobb-Douglas production function using regression techniques.
|
46
|
+
4. Compare the predicted output values with the actual values to assess the model's accuracy.
|
47
|
+
5. Visualize the differences between the actual and predicted values.
|
48
|
+
|
49
|
+
|
50
|
+
### Steps
|
51
|
+
|
52
|
+
1. **Data Loading and Exploration**: We start by loading the historical data from a CSV file and exploring its structure.
|
53
|
+
2. **Data Visualization**: We create various plots to visualize the trends in labor, capital, and output over the years, seeking to make a plot that is reminiscent of the hand-drawn figure in the original article.
|
54
|
+
3. **Parameter Estimation**: Using regression techniques, we estimate the parameters \( \alpha \) and \( A \) of the Cobb-Douglas production function.
|
55
|
+
4. **Model Comparison**: We compare the predicted output values with the actual values and visualize the differences.
|
56
|
+
|
57
|
+
By following these steps, we aim to gain a deeper understanding of the Cobb-Douglas production function and its application to historical economic data.
|
58
|
+
|
59
|
+
**Please take a look at the original article for a more detailed explanation of the theory and methodology.**
|
60
|
+
From the 1928 Journal Article
|
61
|
+
https://www.jstor.org/stable/1811556
|
62
|
+
|
63
|
+
|
64
|
+
A Theory of Production
|
65
|
+
Charles W. Cobb, Paul H. Douglas
|
66
|
+
The American Economic Review, Vol. 18, No. 1, Supplement, Papers and Proceedings of the Fortieth Annual Meeting of the American Economic Association (Mar., 1928), pp. 139-165 (27 pages)
|
67
|
+
|
68
|
+
```python
|
69
|
+
from datascience import *
|
70
|
+
import numpy as np
|
71
|
+
import matplotlib.pyplot as plt
|
72
|
+
import matplotlib.ticker as mticker
|
73
|
+
|
74
|
+
import pandas as pd
|
75
|
+
%matplotlib inline
|
76
|
+
```
|
77
|
+
|
78
|
+
## Cobb Douglas graph of US Economic Output 1899-1922
|
79
|
+
|
80
|
+

|
81
|
+
|
82
|
+
```python
|
83
|
+
cobbd = Table.read_table('Cobb.csv')
|
84
|
+
cobbd
|
85
|
+
```
|
86
|
+
|
87
|
+
Step 1 - A simple plot over time using the datascience commands
|
88
|
+
|
89
|
+
```python
|
90
|
+
cobbd.select('Year', 'EmplIndex', 'CapitalIndex', 'PhProdIndex').plot('Year')
|
91
|
+
```
|
92
|
+
|
93
|
+
Step 2- Making the Y-Axis a log-scale
|
94
|
+
|
95
|
+
```python
|
96
|
+
cobbd.select('Year', 'EmplIndex', 'CapitalIndex', 'PhProdIndex').plot('Year')
|
97
|
+
|
98
|
+
plt.yscale('log')
|
99
|
+
|
100
|
+
plt.show()
|
101
|
+
```
|
102
|
+
|
103
|
+
Step 3 - Changing properties for th Y-Axis
|
104
|
+
|
105
|
+
```python
|
106
|
+
cobbd.select('Year', 'EmplIndex', 'CapitalIndex', 'PhProdIndex').plot('Year')
|
107
|
+
|
108
|
+
|
109
|
+
plt.yscale('log')
|
110
|
+
|
111
|
+
plt.gca().set_yticks([100, 200, 300, 400, 500]) # Set specific y-tick values
|
112
|
+
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter()) # Format as scalar
|
113
|
+
|
114
|
+
plt.show()
|
115
|
+
```
|
116
|
+
|
117
|
+
Step 4 - *Try to make the plot look like the original !*
|
118
|
+
- Change the properties of the lines
|
119
|
+
- add Gridlines
|
120
|
+
- Title and Legend
|
121
|
+
|
122
|
+
```python
|
123
|
+
cobbd.select('Year', 'EmplIndex', 'CapitalIndex', 'PhProdIndex').plot('Year')
|
124
|
+
|
125
|
+
plt.yscale('log')
|
126
|
+
|
127
|
+
# Customize line styles
|
128
|
+
empl_line, = plt.plot(cobbd.column('Year'), cobbd.column('EmplIndex'), label='Labor Index', color='black', marker='o', markerfacecolor='white', linewidth=2)
|
129
|
+
capital_line, = plt.plot(cobbd.column('Year'), cobbd.column('CapitalIndex'), label='Capital Index', color='black', marker='o', linewidth=2)
|
130
|
+
phprod_line, = plt.plot(cobbd.column('Year'), cobbd.column('PhProdIndex'), label='Physical Product Index', color='black', linewidth=2)
|
131
|
+
|
132
|
+
plt.gca().set_yticks([75, 100, 200, 300, 400, 500])
|
133
|
+
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter())
|
134
|
+
|
135
|
+
# Add gridlines in black and white
|
136
|
+
plt.grid(True, which='both', linestyle='-', linewidth=0.5, color='gray')
|
137
|
+
|
138
|
+
# Customize title and labels
|
139
|
+
plt.title("PROGRESS IN MANUFACTURING \n 1899-1922 (1899=100)", fontsize=16)
|
140
|
+
plt.xlabel("Year", fontsize=14)
|
141
|
+
|
142
|
+
plt.legend([empl_line, capital_line, phprod_line],
|
143
|
+
['LABOR FORCE', 'FIXED CAPITAL', 'PHYSICAL PRODUCT'], # Legend labels
|
144
|
+
loc='upper center', # Move to center
|
145
|
+
frameon=True) # Optional: removes the legend box frame
|
146
|
+
|
147
|
+
plt.show()
|
148
|
+
```
|
149
|
+
|
150
|
+
## Part 2 - Estimate Alpha and A
|
151
|
+
### Adding Columns for Ratios
|
152
|
+
|
153
|
+
1. **Add a Column for the Ratio of Capital to Labor:**
|
154
|
+
- Use the variable names from the `cobbd` table.
|
155
|
+
- Take the natural logarithms of the ratios
|
156
|
+
- Add this as a new column to the `cobbd` table.
|
157
|
+
|
158
|
+
2. **Add a Column for the Ratio of Output to Labor:**
|
159
|
+
- Use the variable names from the `cobbd` table.
|
160
|
+
- Take the natural logarithms of the ratios
|
161
|
+
- Add this as a new column to the `cobbd` table.
|
162
|
+
|
163
|
+
```python
|
164
|
+
|
165
|
+
cobbd = cobbd.with_column("ln(K/L)", np.log(cobbd.column("CapitalIndex")/cobbd.column("EmplIndex")))
|
166
|
+
|
167
|
+
cobbd = cobbd.with_column("ln(Y/L)", np.log(cobbd.column("PhProdIndex")/cobbd.column("EmplIndex")))
|
168
|
+
cobbd
|
169
|
+
```
|
170
|
+
|
171
|
+
### Estimating the Coefficients of the Cobb-Douglas Production Function
|
172
|
+
|
173
|
+
To estimate the coefficients of the Cobb-Douglas production function, we will use the `np.polyfit` function from the NumPy library. This function will help us perform a linear regression on the logarithmic ratios of capital to labor (\( \ln(K/L) \)) and output to labor (\( \ln(Y/L) \)).
|
174
|
+
|
175
|
+
The `log_ratios` table contains the necessary data for this estimation. By fitting a linear model to these logarithmic ratios, we can derive the coefficients \( \alpha \) and \( A \) of the Cobb-Douglas production function.
|
176
|
+
|
177
|
+
The steps involved are as follows:
|
178
|
+
1. Use `np.polyfit` to perform a linear regression on the `log_ratios` table.
|
179
|
+
2. Extract the coefficients from the regression output.
|
180
|
+
3. Interpret these coefficients in the context of the Cobb-Douglas production function.
|
181
|
+
|
182
|
+
This approach allows us to quantify the relationship between capital, labor, and output in the production process, providing valuable insights into the underlying economic dynamics.
|
183
|
+
|
184
|
+
```python
|
185
|
+
CDestimates = np.polyfit(cobbd.column("ln(K/L)"), cobbd.column("ln(Y/L)"), 1)
|
186
|
+
```
|
187
|
+
|
188
|
+
```python
|
189
|
+
alpha = CDestimates.item(0)
|
190
|
+
beta = 1-alpha
|
191
|
+
A = np.exp(CDestimates.item(1))
|
192
|
+
```
|
193
|
+
|
194
|
+
```python
|
195
|
+
print("alpha: ", alpha)
|
196
|
+
print("beta: ", beta)
|
197
|
+
print("A: ", A)
|
198
|
+
```
|
199
|
+
|
200
|
+
### Regression Analysis Using Statsmodels
|
201
|
+
|
202
|
+
In addition to using NumPy's `polyfit` function, we can also perform the regression analysis using the `statsmodels` library. This allows us to regress $ \ln(Y/L) $ on $\ln(K/L)$ and obtain detailed statistical information about the regression model.
|
203
|
+
|
204
|
+
The steps involved are as follows:
|
205
|
+
1. Use `statsmodels` to perform an Ordinary Least Squares (OLS) regression.
|
206
|
+
2. Compare the results with those obtained using `np.polyfit`.
|
207
|
+
|
208
|
+
This approach is beyond the scope of Data 8 but would be familiar to those who have taken an econometrics class
|
209
|
+
|
210
|
+
```python
|
211
|
+
import statsmodels.api as sm
|
212
|
+
|
213
|
+
sm.OLS(cobbd.column("ln(Y/L)"), sm.add_constant(cobbd.column("ln(K/L)"))).fit().summary()
|
214
|
+
```
|
215
|
+
|
216
|
+
```python
|
217
|
+
# get the coefficients for alpha from the statsmodels output
|
218
|
+
model = sm.OLS(cobbd.column("ln(Y/L)"), sm.add_constant(cobbd.column("ln(K/L)"))).fit()
|
219
|
+
alpha2 = model.params[1]
|
220
|
+
A2= np.exp(model.params[0])
|
221
|
+
print("alpha2: ", alpha2)
|
222
|
+
print("A2: ", A2)
|
223
|
+
```
|
224
|
+
|
225
|
+
What have we learned?
|
226
|
+
- Np.polyfit returns the same output as statsmodels
|
227
|
+
- Cobb-Douglas got the right result before computers had been invented
|
228
|
+
|
229
|
+
Now lets set this up to make predictions for Y and add that column to our table
|
230
|
+
$$
|
231
|
+
A \cdot K^{\alpha} \cdot L^{1 - \alpha}
|
232
|
+
$$
|
233
|
+
|
234
|
+
or
|
235
|
+
$$
|
236
|
+
1.01 \cdot K^{0.25} \cdot L^{0.75}
|
237
|
+
$$
|
238
|
+
|
239
|
+
or
|
240
|
+
$$
|
241
|
+
1.01 \cdot K^{\frac{1}{4}} \cdot L^{\frac{1}{4}}
|
242
|
+
$$
|
243
|
+
|
244
|
+
```python
|
245
|
+
|
246
|
+
cobbd = cobbd.with_column("PredictedY", A*cobbd.column("CapitalIndex")**alpha*cobbd.column("EmplIndex")**(1-alpha))
|
247
|
+
cobbd
|
248
|
+
```
|
249
|
+
|
250
|
+
```python
|
251
|
+
# now we need a line plot the actual values of Y vs year and a pplot of predicted Y vs year
|
252
|
+
cobbd.select('Year', 'PhProdIndex', 'PredictedY').plot('Year')
|
253
|
+
```
|
254
|
+
|
255
|
+
```python
|
256
|
+
# Let's add the difference between the actual and predicted values of Y to the cobbd table
|
257
|
+
cobbd = cobbd.with_column("Difference", cobbd.column("PredictedY") - cobbd.column("PhProdIndex") )
|
258
|
+
# lets add the percentage difference to the table which is actual - predicted / actual * 100
|
259
|
+
cobbd = cobbd.with_column("Percent Difference", cobbd.column("Difference")/cobbd.column("PhProdIndex")*100)
|
260
|
+
cobbd.show(24)
|
261
|
+
```
|
262
|
+
|
263
|
+
And if we plot this we can compare it to 'Chart IV' in the original paper ( page 155)
|
264
|
+
|
265
|
+
```python
|
266
|
+
cobbd.select('Year', 'Difference').plot('Year')
|
267
|
+
```
|
268
|
+
|
269
|
+
```python
|
270
|
+
cobbd.select('Year', 'Difference').plot('Year')
|
271
|
+
|
272
|
+
# Add gridlines in black and white
|
273
|
+
plt.grid(True, which='both', linestyle='-', linewidth=0.5, color='gray')
|
274
|
+
|
275
|
+
# Customize title and labels
|
276
|
+
plt.title("Percentage Deviations \n of Computed from Actual Product\n 1899-1922", fontsize=16)
|
277
|
+
plt.xlabel("Year", fontsize=14)
|
278
|
+
```
|
279
|
+
|
280
|
+
Not quite matching the graph in the article?
|
281
|
+
|
282
|
+
|
283
|
+
|
@@ -0,0 +1,253 @@
|
|
1
|
+
---
|
2
|
+
title: "6.1-Sympy-Differentiation"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 6
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec06/6.1-Sympy-Differentiation.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
<table style="width: 100%;">
|
9
|
+
<tr style="background-color: transparent;"><td>
|
10
|
+
<img src="https://data-88e.github.io/assets/images/blue_text.png" width="250px" style="margin-left: 0;" />
|
11
|
+
</td><td>
|
12
|
+
<p style="text-align: right; font-size: 10pt;"><strong>Economic Models</strong>, Fall 2024<br>
|
13
|
+
Dr. Eric Van Dusen
|
14
|
+
</p></td></tr>
|
15
|
+
</table>
|
16
|
+
|
17
|
+
# Using Sympy to take Derivatives
|
18
|
+
|
19
|
+
```python
|
20
|
+
import sympy as sp
|
21
|
+
import numpy as np
|
22
|
+
import matplotlib.pyplot as plt
|
23
|
+
```
|
24
|
+
|
25
|
+
## Let's start with the Cobb - Douglas Production Function
|
26
|
+
|
27
|
+
$Q = A \cdot L^{\alpha} \cdot K^\beta$
|
28
|
+
|
29
|
+
$\frac{dQ}{dL} = A \cdot \alpha \cdot L^{\alpha - 1} \cdot K^\beta$
|
30
|
+
|
31
|
+
$\frac{d^2Q}{dL^2} = A \cdot \alpha (\alpha - 1) \cdot L^{\alpha - 2} \cdot K^\beta$
|
32
|
+
|
33
|
+
Define the symbols and variables
|
34
|
+
- which symbols will we need for the Cobb Douglas formulation?
|
35
|
+
- Declare them as symbols
|
36
|
+
|
37
|
+
```python
|
38
|
+
Q, A, L, K, alpha, beta = sp.symbols('Q A L K alpha beta')
|
39
|
+
```
|
40
|
+
|
41
|
+
Define the Cobb-Douglas production function
|
42
|
+
- writing this equation in its Python formulation
|
43
|
+
- using the symbols we have already defined
|
44
|
+
|
45
|
+
```python
|
46
|
+
cobb_douglas = A * L**alpha * K**beta
|
47
|
+
```
|
48
|
+
|
49
|
+
Calculate the **first derivative** with respect to **L**
|
50
|
+
|
51
|
+
- This is a *partial derivative*
|
52
|
+
|
53
|
+
- sympy.diff ( original equation, thing you are taking the derivative over )
|
54
|
+
|
55
|
+
- Save that as an equation for future use!
|
56
|
+
|
57
|
+
```python
|
58
|
+
first_derivative = sp.diff(cobb_douglas, L)
|
59
|
+
```
|
60
|
+
|
61
|
+
Print this result
|
62
|
+
- you can just ask sympy to print - spprint ()
|
63
|
+
- you can add some text "First Derivative is.."
|
64
|
+
|
65
|
+
```python
|
66
|
+
print("First Derivative:")
|
67
|
+
sp.pprint(first_derivative)
|
68
|
+
```
|
69
|
+
|
70
|
+
This doesnt Look so great
|
71
|
+
- lets pass in our function and get sympy to simplify it
|
72
|
+
- what form you want an equaiton to be in can take many forms
|
73
|
+
|
74
|
+
```python
|
75
|
+
simplified_first_derivative = sp.simplify(first_derivative)
|
76
|
+
print(simplified_first_derivative)
|
77
|
+
```
|
78
|
+
|
79
|
+
Print Result - Variation
|
80
|
+
- you can also save as Latex !
|
81
|
+
- It won't render yet - but you can copy paste this into Markdown cell
|
82
|
+
|
83
|
+
```python
|
84
|
+
first_derivative_latex = sp.latex(first_derivative)
|
85
|
+
print("First Derivative (LaTeX):")
|
86
|
+
print(first_derivative_latex)
|
87
|
+
```
|
88
|
+
|
89
|
+
$\frac{A K^{\beta} L^{\alpha} \alpha}{L}$
|
90
|
+
|
91
|
+
Calculate the **second derivative** with respect to **L**
|
92
|
+
- use the first derivative that you saved
|
93
|
+
- take the derivatie again with respect to variable of interes, L
|
94
|
+
- save that equation
|
95
|
+
|
96
|
+
```python
|
97
|
+
second_derivative = sp.diff(first_derivative, L)
|
98
|
+
```
|
99
|
+
|
100
|
+
```python
|
101
|
+
sp.pprint(second_derivative)
|
102
|
+
```
|
103
|
+
|
104
|
+
```python
|
105
|
+
second_derivative_latex = sp.latex(second_derivative)
|
106
|
+
print(second_derivative_latex)
|
107
|
+
```
|
108
|
+
|
109
|
+
Pasting this into a Markdown cell and adding "dollar signs"
|
110
|
+
|
111
|
+
$\frac{A K^{\beta} L^{\alpha} \alpha^{2}}{L^{2}} - \frac{A K^{\beta} L^{\alpha} \alpha}{L^{2}}$
|
112
|
+
|
113
|
+
First Derivative (LaTeX):
|
114
|
+
$\frac{A K^{\beta} L^{\alpha} \alpha}{L}$
|
115
|
+
|
116
|
+
Second Derivative (LaTeX):
|
117
|
+
$\frac{A K^{\beta} L^{\alpha} \alpha^{2}}{L^{2}} - \frac{A K^{\beta} L^{\alpha} \alpha}{L^{2}}$
|
118
|
+
|
119
|
+
Lets simplify both of these and generat the Latex
|
120
|
+
|
121
|
+
```python
|
122
|
+
first_div_simp_latex = sp.latex(sp.simplify(first_derivative))
|
123
|
+
print(first_div_simp_latex)
|
124
|
+
```
|
125
|
+
|
126
|
+
```python
|
127
|
+
second_div_simp_latex = sp.latex(sp.simplify(sp.diff(first_derivative, L)))
|
128
|
+
print(second_div_simp_latex)
|
129
|
+
```
|
130
|
+
|
131
|
+
$$A K^{\beta} L^{\alpha - 1} \alpha$$
|
132
|
+
$$A K^{\beta} L^{\alpha - 2} \alpha \left(\alpha - 1\right)$$
|
133
|
+
|
134
|
+
## Now Let's consider the simpler case of a Utility function
|
135
|
+
- that has a Cobb-Douglas type formulation
|
136
|
+
- consists of consuming two goods $X1$ and $X2$
|
137
|
+
|
138
|
+
$ U = \cdot X1^{\alpha} \cdot X2^{1-\alpha}$
|
139
|
+
|
140
|
+
```python
|
141
|
+
U, X1, X2, alpha = sp.symbols('U, X1, X2, alpha ')
|
142
|
+
```
|
143
|
+
|
144
|
+
```python
|
145
|
+
U = X1**alpha * X2**(1-alpha)
|
146
|
+
```
|
147
|
+
|
148
|
+
```python
|
149
|
+
first_deriv_x1 = sp.diff(U,X1)
|
150
|
+
```
|
151
|
+
|
152
|
+
```python
|
153
|
+
second_deriv_x1 = sp.diff(first_deriv_x1, X1)
|
154
|
+
```
|
155
|
+
|
156
|
+
```python
|
157
|
+
sp.pprint(first_deriv_x1)
|
158
|
+
```
|
159
|
+
|
160
|
+
```python
|
161
|
+
sp.pprint(second_deriv_x1)
|
162
|
+
```
|
163
|
+
|
164
|
+
```python
|
165
|
+
sp.simplify(first_deriv_x1)
|
166
|
+
```
|
167
|
+
|
168
|
+
```python
|
169
|
+
sp.simplify( second_deriv_x1)
|
170
|
+
```
|
171
|
+
|
172
|
+
```python
|
173
|
+
first_deriv_x1_latex = sp.latex(sp.simplify(first_deriv_x1))
|
174
|
+
print(first_deriv_x1_latex)
|
175
|
+
second_deriv_x1_latex = sp.latex(sp.simplify(second_deriv_x1))
|
176
|
+
print(second_deriv_x1_latex)
|
177
|
+
```
|
178
|
+
|
179
|
+
First Derivative (LaTeX):
|
180
|
+
$X_{1}^{\alpha - 1} X_{2}^{1 - \alpha} \alpha$
|
181
|
+
|
182
|
+
Second Derivative (LaTeX):
|
183
|
+
$X_{1}^{\alpha - 2} X_{2}^{1 - \alpha} \alpha \left(\alpha - 1\right)
|
184
|
+
$
|
185
|
+
|
186
|
+
Lets get Numeric!
|
187
|
+
- Lets declare some variables as constants ( alpha, x2)
|
188
|
+
- and pass in a range for X1
|
189
|
+
|
190
|
+
```python
|
191
|
+
alpha = 0.5 # You can change the value of alpha as needed
|
192
|
+
X1_values = np.linspace(0.1, 2.0, 100) # Range of X1 values
|
193
|
+
X2 = 1.0 # Fixed value for X2
|
194
|
+
```
|
195
|
+
|
196
|
+
```python
|
197
|
+
X1_values
|
198
|
+
```
|
199
|
+
|
200
|
+
```python
|
201
|
+
U_values = X1_values**alpha * X2**(1 - alpha)
|
202
|
+
U_values
|
203
|
+
```
|
204
|
+
|
205
|
+
```python
|
206
|
+
first_derivative_values = alpha * X1_values**(alpha - 1) * X2**(1 - alpha)
|
207
|
+
first_derivative_values
|
208
|
+
```
|
209
|
+
|
210
|
+
```python
|
211
|
+
second_derivative_values = alpha * (alpha - 1) * X1_values**(alpha - 2) * X2**(1 - alpha)
|
212
|
+
second_derivative_values
|
213
|
+
```
|
214
|
+
|
215
|
+
```python
|
216
|
+
plt.figure(figsize=(8, 6))
|
217
|
+
plt.plot(X1_values, U_values, label='U')
|
218
|
+
plt.plot(X1_values, first_derivative_values, label='First Derivative')
|
219
|
+
plt.plot(X1_values, second_derivative_values, label='Second Derivative')
|
220
|
+
plt.xlabel('X1')
|
221
|
+
plt.ylabel('First Derivative')
|
222
|
+
plt.title('First Derivative and Second Derivative of U')
|
223
|
+
plt.legend()
|
224
|
+
plt.grid(True)
|
225
|
+
plt.show()
|
226
|
+
```
|
227
|
+
|
228
|
+
```python
|
229
|
+
|
230
|
+
|
231
|
+
# Create a figure with two subplots (one for U and one for the first derivative)
|
232
|
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
|
233
|
+
|
234
|
+
# Plot the original function U on the left subplot
|
235
|
+
ax1.plot(X1_values, U_values, label='U')
|
236
|
+
ax1.set_xlabel('X1')
|
237
|
+
ax1.set_ylabel('U')
|
238
|
+
ax1.set_title('Utility Function ')
|
239
|
+
|
240
|
+
# Plot the first derivative on the right subplot
|
241
|
+
ax2.plot(X1_values, first_derivative_values, label='First Derivative', color='orange')
|
242
|
+
ax2.set_xlabel('X1')
|
243
|
+
ax2.set_ylabel('First Derivative')
|
244
|
+
ax2.set_title('First Derivative')
|
245
|
+
|
246
|
+
# Show the plot
|
247
|
+
plt.show()
|
248
|
+
```
|
249
|
+
|
250
|
+
```python
|
251
|
+
|
252
|
+
```
|
253
|
+
|