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,171 @@
|
|
1
|
+
---
|
2
|
+
title: "ScannerData_Beer"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 2
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec02/ScannerData_Beer.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
```python
|
9
|
+
import pandas as pd
|
10
|
+
import matplotlib.pyplot as plt
|
11
|
+
import numpy as np
|
12
|
+
import plotly.express as px
|
13
|
+
%matplotlib inline
|
14
|
+
```
|
15
|
+
|
16
|
+
```python
|
17
|
+
try:
|
18
|
+
import gdown
|
19
|
+
except:
|
20
|
+
!pip install gdown
|
21
|
+
import gdown
|
22
|
+
```
|
23
|
+
|
24
|
+
```python
|
25
|
+
#This file is too big and appears to crash the Datahub - could potentially work on a local machine with more RAM
|
26
|
+
#! gdown 1z0XeoE6PYPOUqzzKLagvmiFohdOF3CLJ
|
27
|
+
# n_wber.csv = 140M
|
28
|
+
```
|
29
|
+
|
30
|
+
This is a smaller version of the dataset with half of the data ( approx 2m rows)
|
31
|
+
|
32
|
+
The following command pulls the dataset over from Google Drive
|
33
|
+
|
34
|
+
```python
|
35
|
+
#https://drive.google.com/file/d/1qZpdbEpvvWQJlZKpzu8rwd53JrrufzdW/view?usp=sharing
|
36
|
+
! gdown 1qZpdbEpvvWQJlZKpzu8rwd53JrrufzdW
|
37
|
+
# s_wber.csv = 70M
|
38
|
+
```
|
39
|
+
|
40
|
+
# A Quick Look on the Inverse Demand Curves for Beer
|
41
|
+
|
42
|
+
Start off by pulling in open source data on beer sales from [The University of Chicago Booth School of Business](https://www.chicagobooth.edu/research/kilts/datasets/dominicks).
|
43
|
+
|
44
|
+
```python
|
45
|
+
df = pd.read_csv('s_wber.csv')
|
46
|
+
df
|
47
|
+
```
|
48
|
+
|
49
|
+
Can you tell what each instance (row) and the features (columns) represent?
|
50
|
+
|
51
|
+
```python
|
52
|
+
df.describe()
|
53
|
+
```
|
54
|
+
|
55
|
+
One way that I like to approach a dataset where I don't know the specific details of it is to first summarize the whole table, then delve deeper into each feature. Here's a quick example of my approach:
|
56
|
+
|
57
|
+
```python
|
58
|
+
len(df['UPC'].unique()) # Number of Unique beers?
|
59
|
+
```
|
60
|
+
|
61
|
+
```python
|
62
|
+
len(df['STORE'].unique()) # Number of unique stores
|
63
|
+
```
|
64
|
+
|
65
|
+
```python
|
66
|
+
len(df['WEEK'].unique()) # Number of unique weeks
|
67
|
+
```
|
68
|
+
|
69
|
+
```python
|
70
|
+
df['PRICE'].mean() # Average beer price
|
71
|
+
```
|
72
|
+
|
73
|
+
```python
|
74
|
+
df['PRICE'].max() # most expensive beer
|
75
|
+
```
|
76
|
+
|
77
|
+
```python
|
78
|
+
df['PRICE'].min() # FREE BEER?!
|
79
|
+
```
|
80
|
+
|
81
|
+
```python
|
82
|
+
df['QTY'].mean() # avg beers bought ?
|
83
|
+
```
|
84
|
+
|
85
|
+
```python
|
86
|
+
df['MOVE'].mean() # avg beers bought ?
|
87
|
+
```
|
88
|
+
|
89
|
+
Can you tell what this plot is showing below? I'm not sure if I can!
|
90
|
+
|
91
|
+
```python
|
92
|
+
df[['MOVE','PRICE']].plot();
|
93
|
+
```
|
94
|
+
|
95
|
+
```python
|
96
|
+
df['QUANTITY'] = df['MOVE'] # Interestingly enough, quantity is not denoted as QTY, by 'MOVE'.
|
97
|
+
```
|
98
|
+
|
99
|
+
By now, we should be aware that we're looking at a dataset of beer sales, where the respective price and quantities for each transaction is represented. Let's filter out all the free beer - although it would be very nice to keep that!
|
100
|
+
|
101
|
+
```python
|
102
|
+
g = df[['QUANTITY','PRICE']]
|
103
|
+
g = g[g['PRICE']>0]
|
104
|
+
g
|
105
|
+
```
|
106
|
+
|
107
|
+
To create the demand curve itself, we need to remember that we're looking for quantity demanded at each given price. Hence, we group by price and 'ask' for the sum at the given price. Then, flip that around (just trick), and do the cumulative sum (since a demand curve is cumulative), and then flip it around one last time (since we're looking at the inverse demand curve).
|
108
|
+
|
109
|
+
```python
|
110
|
+
demand = np.flip(np.cumsum(np.flip(g.groupby('PRICE')['QUANTITY'].sum())))
|
111
|
+
demand = demand.to_frame().reset_index()
|
112
|
+
demand
|
113
|
+
```
|
114
|
+
|
115
|
+
Now, let's use Plotly Express' [Scatterplot function](https://plotly.com/python-api-reference/generated/plotly.express.scatter.html) to visualize the inverse demand curve for all beers!
|
116
|
+
|
117
|
+
```python
|
118
|
+
px.scatter(demand,x='QUANTITY', y='PRICE', trendline='ols', title='Inverse Demand Curve for all Beers')
|
119
|
+
```
|
120
|
+
|
121
|
+
This is pretty cool! Hover over the line to see the Ordinary Least Squares approximation of the inverse demand curve. What does it tell you?
|
122
|
+
|
123
|
+
Now, do you expect the price elasticity of demand to differ for different prices? Yes! Usually, a more expensive good (luxury beers?) tend to have a higher PED. Could we visualize this?
|
124
|
+
|
125
|
+
```python
|
126
|
+
demand.describe()
|
127
|
+
```
|
128
|
+
|
129
|
+
Let's plot multiple demand curves for different price segments of beer. We could start with all beers above the mean. Let's call them expensive.
|
130
|
+
|
131
|
+
```python
|
132
|
+
demand['EXPENSIVE'] = demand['PRICE']>demand['PRICE'].mean()
|
133
|
+
demand.head(5)
|
134
|
+
```
|
135
|
+
|
136
|
+
Before you plot, think about how this curve might differ from the previous one. Then, check if your intuition was right!
|
137
|
+
|
138
|
+
```python
|
139
|
+
px.scatter(demand,x='QUANTITY', y='PRICE', trendline='ols', color='EXPENSIVE',title='Inverse Demand Curve for Expensive and Cheaper Beer')
|
140
|
+
```
|
141
|
+
|
142
|
+
Did your economic intuition help you? Now, what's happening with the really expensive beer?
|
143
|
+
|
144
|
+
```python
|
145
|
+
demand['REALLY EXPENSIVE'] = demand['PRICE']>10.5 #75th Percentile of Price
|
146
|
+
demand.head(5)
|
147
|
+
```
|
148
|
+
|
149
|
+
```python
|
150
|
+
px.scatter(demand,x='QUANTITY', y='PRICE', trendline='ols', color='REALLY EXPENSIVE',title='Inverse Demand Curve for Really Expensive Beer')
|
151
|
+
```
|
152
|
+
|
153
|
+
And the really, really expensive beer?
|
154
|
+
|
155
|
+
```python
|
156
|
+
demand['REALLY, REALLY EXPENSIVE'] = demand['PRICE']>15
|
157
|
+
demand.head(5)
|
158
|
+
```
|
159
|
+
|
160
|
+
```python
|
161
|
+
px.scatter(demand,x='QUANTITY', y='PRICE', trendline='ols', color='REALLY, REALLY EXPENSIVE',title='Inverse Demand Curve for Really, Really Expensive Beer is almost Vertical!')
|
162
|
+
```
|
163
|
+
|
164
|
+
This notebook should have given you the data science skills to plot up simple, but powerful inverse demand curves. It should have also gotten you thinking about how demand curves differ for different price segments for the same goods.
|
165
|
+
|
166
|
+
Made by Peter F. Grinde-Hollevik.
|
167
|
+
|
168
|
+
```python
|
169
|
+
|
170
|
+
```
|
171
|
+
|
@@ -0,0 +1,213 @@
|
|
1
|
+
---
|
2
|
+
title: "demand-curve-Fa24"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 2
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec02/demand-curve-Fa24.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
# Creating a Demand Curve
|
9
|
+
|
10
|
+
```python
|
11
|
+
import pandas as pd
|
12
|
+
import os
|
13
|
+
import json
|
14
|
+
import numpy as np
|
15
|
+
from datascience import *
|
16
|
+
from ipywidgets import interact, interactive, fixed, interact_manual
|
17
|
+
import ipywidgets as widgets
|
18
|
+
%matplotlib inline
|
19
|
+
import matplotlib.pyplot as plt
|
20
|
+
import warnings
|
21
|
+
warnings.filterwarnings("ignore")
|
22
|
+
```
|
23
|
+
|
24
|
+
We will create a few demand curves based on the class. Start by filling in the form at https://forms.gle/KhCjC4nwwhtRDjSJ9 or https://tinyurl.com/data-88e-demand!
|
25
|
+
|
26
|
+
```python
|
27
|
+
sheet_id = "1jzgX74fgWo91Dyv7SbD4AmSFvUN5APc79BaOENpsyv8"
|
28
|
+
sheet_name = "Form1"
|
29
|
+
url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
|
30
|
+
```
|
31
|
+
|
32
|
+
```python
|
33
|
+
pd.read_csv(url)
|
34
|
+
```
|
35
|
+
|
36
|
+
```python
|
37
|
+
df_demand=pd.read_csv(url)
|
38
|
+
```
|
39
|
+
|
40
|
+
```python
|
41
|
+
demand_table = Table.from_df(df_demand)
|
42
|
+
demand_table = demand_table.drop('Timestamp')
|
43
|
+
demand_table
|
44
|
+
```
|
45
|
+
|
46
|
+
Let's try graphing all our different responses!
|
47
|
+
|
48
|
+
```python
|
49
|
+
for i in demand_table.labels:
|
50
|
+
demand_table.hist(i);
|
51
|
+
```
|
52
|
+
|
53
|
+
Let's start by looking at the demand for masks. How many people would buy masks at a given price? Let's assume that a person would be willing to buy the good at a price less than their bid price.
|
54
|
+
|
55
|
+
```python
|
56
|
+
# This is a column of bid values for masks that you've all inputted.
|
57
|
+
masks = demand_table.select('Masks')
|
58
|
+
masks
|
59
|
+
```
|
60
|
+
|
61
|
+
```python
|
62
|
+
# This cell does some python magic. You do not need to worry about what's going on.
|
63
|
+
prices = pd.DataFrame({'price':[0.25, 0.5, 0.75, 1.00, 1.25, 1.5,1.75,2]})
|
64
|
+
MasksByPrice = masks.group("Masks")
|
65
|
+
mbp = MasksByPrice.to_df()
|
66
|
+
mask = (
|
67
|
+
prices
|
68
|
+
.merge(mbp, left_on='price', how='left', right_on='Masks')
|
69
|
+
.fillna(0).drop('Masks', axis=1)
|
70
|
+
)
|
71
|
+
masks_table = Table.from_df(mask)
|
72
|
+
Q_demand = np.flip(np.cumsum(np.flip(masks_table.group("price", sum).column(1))))
|
73
|
+
masks_demand = Table().with_columns(
|
74
|
+
'price', prices.price,
|
75
|
+
'quantity', Q_demand
|
76
|
+
)
|
77
|
+
masks_demand
|
78
|
+
```
|
79
|
+
|
80
|
+
```python
|
81
|
+
# Let's graph our results
|
82
|
+
masks_demand.scatter("quantity", "price")
|
83
|
+
plt.xlabel('Quantity')
|
84
|
+
plt.ylabel('Price')
|
85
|
+
plt.title('Demand for a pack of surgical masks');
|
86
|
+
```
|
87
|
+
|
88
|
+
Now let's find the slope and intercept of the line of best fit. The cell below defines some functions that you'll learn about in the later portions of Data 8.
|
89
|
+
|
90
|
+
```python
|
91
|
+
std_units = lambda a: (a - np.mean(a)) / np.std(a)
|
92
|
+
corr = lambda x, y: np.mean(std_units(x) * std_units(y))
|
93
|
+
slope = lambda x, y: corr(x, y) * np.std(y) / np.std(x)
|
94
|
+
intercept = lambda x, y: np.mean(y) - slope(x, y) * np.mean(x)
|
95
|
+
```
|
96
|
+
|
97
|
+
```python
|
98
|
+
slope(masks_demand["quantity"], masks_demand["price"])
|
99
|
+
```
|
100
|
+
|
101
|
+
```python
|
102
|
+
intercept(masks_demand["quantity"], masks_demand["price"])
|
103
|
+
```
|
104
|
+
|
105
|
+
We can use the same code as above to create demand curves for our other products as well!
|
106
|
+
|
107
|
+
```python
|
108
|
+
#Gourmet Burrito
|
109
|
+
prices_burrito = pd.DataFrame({'price':[2.50, 5, 7.50, 10, 12.5, 15,17.5,20]})
|
110
|
+
|
111
|
+
burritos = demand_table.select('Burrito')
|
112
|
+
burritosByPrice = burritos.group("Burrito")
|
113
|
+
bbp = burritosByPrice.to_df()
|
114
|
+
gb = (
|
115
|
+
prices_burrito
|
116
|
+
.merge(bbp, left_on='price', how='left', right_on='Burrito')
|
117
|
+
.fillna(0).drop('Burrito', axis=1)
|
118
|
+
)
|
119
|
+
|
120
|
+
burritos_table = Table.from_df(gb)
|
121
|
+
Q_demand_burrito = np.flip(np.cumsum(np.flip(burritos_table.group("price", sum).column(1))))
|
122
|
+
|
123
|
+
gb_demand = Table().with_columns(
|
124
|
+
'price', prices_burrito.price,
|
125
|
+
'quantity', Q_demand_burrito
|
126
|
+
)
|
127
|
+
|
128
|
+
burrito_slope = slope(gb_demand["quantity"], gb_demand["price"])
|
129
|
+
burrito_intercept = intercept(gb_demand["quantity"], gb_demand["price"])
|
130
|
+
print("Slope: " + str(burrito_slope))
|
131
|
+
print("Intercept: " + str(burrito_intercept))
|
132
|
+
```
|
133
|
+
|
134
|
+
```python
|
135
|
+
#Greek Theatre Tickets
|
136
|
+
prices_tickets = pd.DataFrame({'price':[25, 50, 75, 100, 125, 150,175,200]})
|
137
|
+
|
138
|
+
tickets = demand_table.select('GreekTix')
|
139
|
+
ticketsByPrice = tickets.group("GreekTix")
|
140
|
+
tbp = ticketsByPrice.to_df()
|
141
|
+
gt = (
|
142
|
+
prices_tickets
|
143
|
+
.merge(tbp, left_on='price', how='left', right_on='GreekTix')
|
144
|
+
.fillna(0).drop('GreekTix', axis=1)
|
145
|
+
)
|
146
|
+
|
147
|
+
tickets_table = Table.from_df(gt)
|
148
|
+
Q_demand_tickets = np.flip(np.cumsum(np.flip(tickets_table.group("price", sum).column(1))))
|
149
|
+
|
150
|
+
gt_demand = Table().with_columns(
|
151
|
+
'price', prices_tickets.price,
|
152
|
+
'quantity', Q_demand_tickets
|
153
|
+
)
|
154
|
+
|
155
|
+
tickets_slope = slope(gt_demand["quantity"], gt_demand["price"])
|
156
|
+
tickets_intercept = intercept(gt_demand["quantity"], gt_demand["price"])
|
157
|
+
print("Slope: " + str(tickets_slope))
|
158
|
+
print("Intercept: " + str(tickets_intercept))
|
159
|
+
```
|
160
|
+
|
161
|
+
```python
|
162
|
+
#Iphone 14
|
163
|
+
prices_iphone = pd.DataFrame({'price':[250, 500, 750, 1000, 1250, 1500,1750,2000, 2250, 2500, 2750, 3000]})
|
164
|
+
|
165
|
+
iphones = demand_table.select('iPhone')
|
166
|
+
iphonesByPrice = iphones.group("iPhone")
|
167
|
+
ibp = iphonesByPrice.to_df()
|
168
|
+
iphone14 = (
|
169
|
+
prices_iphone
|
170
|
+
.merge(ibp, left_on='price', how='left', right_on="iPhone")
|
171
|
+
.fillna(0).drop("iPhone", axis=1)
|
172
|
+
)
|
173
|
+
|
174
|
+
iphones_table = Table.from_df(iphone14)
|
175
|
+
Q_demand_iphones = np.flip(np.cumsum(np.flip(iphones_table.group("price", sum).column(1))))
|
176
|
+
|
177
|
+
iphone14_demand = Table().with_columns(
|
178
|
+
'price', prices_iphone.price,
|
179
|
+
'quantity', Q_demand_iphones
|
180
|
+
)
|
181
|
+
|
182
|
+
iphones_slope = slope(iphone14_demand["quantity"], iphone14_demand["price"])
|
183
|
+
iphones_intercept = intercept(iphone14_demand["quantity"], iphone14_demand["price"])
|
184
|
+
print("Slope: " + str(iphones_slope))
|
185
|
+
print("Intercept: " + str(iphones_intercept))
|
186
|
+
```
|
187
|
+
|
188
|
+
Comparing the demand curves for our four products, what similarities or differences do you notice? In particular, think about what the slopes of the curves might reveal to us about consumer preferences.
|
189
|
+
|
190
|
+
```python
|
191
|
+
masks_demand.scatter("quantity", "price")
|
192
|
+
plt.xlabel('Quantity')
|
193
|
+
plt.ylabel('Price')
|
194
|
+
plt.title('Demand for a pack of surgical masks');
|
195
|
+
|
196
|
+
gb_demand.scatter("quantity", "price")
|
197
|
+
plt.xlabel('Quantity')
|
198
|
+
plt.ylabel('Price')
|
199
|
+
plt.title('Demand for Gourmet Burritos');
|
200
|
+
|
201
|
+
gt_demand.scatter("quantity", "price")
|
202
|
+
plt.xlabel('Quantity')
|
203
|
+
plt.ylabel('Price')
|
204
|
+
plt.title('Demand for Greek Theatre Tickets');
|
205
|
+
|
206
|
+
iphone14_demand.scatter("quantity", "price")
|
207
|
+
plt.xlabel('Quantity')
|
208
|
+
plt.ylabel('Price')
|
209
|
+
plt.title('Demand for iPhone14');
|
210
|
+
```
|
211
|
+
|
212
|
+
|
213
|
+
|
@@ -0,0 +1,239 @@
|
|
1
|
+
---
|
2
|
+
title: "3.0-CubicCostCurve"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 3
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec03/3.0-CubicCostCurve.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
|
+
Shashank Dalmia, Ergun Acikoz</p></td></tr>
|
15
|
+
</table>
|
16
|
+
|
17
|
+
```python
|
18
|
+
try:
|
19
|
+
from csaps import csaps
|
20
|
+
except:
|
21
|
+
!pip install csaps
|
22
|
+
from csaps import csaps
|
23
|
+
```
|
24
|
+
|
25
|
+
```python
|
26
|
+
import sympy as sp
|
27
|
+
import matplotlib.pyplot as plt
|
28
|
+
import numpy as np
|
29
|
+
from ipywidgets import interact, widgets
|
30
|
+
from datascience import *
|
31
|
+
%matplotlib inline
|
32
|
+
from csaps import csaps
|
33
|
+
```
|
34
|
+
|
35
|
+
# Cubic Cost Functions
|
36
|
+
|
37
|
+
The cubic cost function is one that is commonly used to model a cost function within a firm.
|
38
|
+
|
39
|
+
This notebook uses parameters from a recent article with some parameters for the cubic cost function, in a journal on Teaching Applied Economics
|
40
|
+
|
41
|
+
>"Tractable Cubic Cost Functions for Teaching Microeconomics"
|
42
|
+
>June 21,2023; Applied Economics Teaching Resources (AETR);
|
43
|
+
>Scott M. Swinton and Hanzhe Zhang
|
44
|
+
|
45
|
+
https://www.aaea.org/UserFiles/file/AETR_2021_003RProofFinal.pdf
|
46
|
+
|
47
|
+
- d=3600
|
48
|
+
- c=177
|
49
|
+
- b=-15
|
50
|
+
- a=0.5
|
51
|
+
|
52
|
+
Let's use these as starting values but then use widgets to allow us to play with the parameters!
|
53
|
+
|
54
|
+
```python
|
55
|
+
# Define the variables and parameters
|
56
|
+
q = sp.symbols('q') # Quantity of goods produced
|
57
|
+
|
58
|
+
# Define the cubic cost function
|
59
|
+
a = widgets.FloatSlider(value=0.5, min=0, max=1, step=0.01, description='a')
|
60
|
+
b = widgets.FloatSlider(value=-15, min=-30, max=30, step=1, description='b')
|
61
|
+
c = widgets.FloatSlider(value=177.0, min=0, max=250, step=5, description='c')
|
62
|
+
d = widgets.FloatSlider(value=3600, min=0, max=10000, step=100, description='d')
|
63
|
+
|
64
|
+
def plot_cubic_cost_function(a, b, c, d):
|
65
|
+
cost_function = a * q**3 + b * q**2 + c * q + d
|
66
|
+
|
67
|
+
cost_function_np = sp.lambdify(q, cost_function, 'numpy')
|
68
|
+
|
69
|
+
quantity_values = np.linspace(0, 25, 100)
|
70
|
+
cost_values = cost_function_np(quantity_values)
|
71
|
+
|
72
|
+
plt.figure(figsize=(8, 6))
|
73
|
+
plt.plot(quantity_values, cost_values, label='Cost Function', color='blue')
|
74
|
+
plt.xlabel('Quantity Produced')
|
75
|
+
plt.ylabel('Cost')
|
76
|
+
plt.title('Cubic Cost Function')
|
77
|
+
plt.legend()
|
78
|
+
plt.grid(True)
|
79
|
+
plt.show()
|
80
|
+
|
81
|
+
interact(plot_cubic_cost_function, a=a, b=b, c=c, d=d)
|
82
|
+
```
|
83
|
+
|
84
|
+
## Great - we can make a cost function that displays the model characteristics that we want
|
85
|
+
**Now let's go ahead and make an table from the Function**
|
86
|
+
|
87
|
+
We can use Sympy and get a Numpy version of the equation as well
|
88
|
+
|
89
|
+
```python
|
90
|
+
q = sp.symbols('q')
|
91
|
+
cost_function = a * q**3 + b * q**2 + c * q + d
|
92
|
+
|
93
|
+
|
94
|
+
# You dont know how to do this but we can also convert the SymPy expression to a numpy function
|
95
|
+
cost_function_np = sp.lambdify(q, cost_function, 'numpy')
|
96
|
+
```
|
97
|
+
|
98
|
+
Le'ts make the model by specifying those cubic parameters that we did in the last section
|
99
|
+
|
100
|
+
```python
|
101
|
+
d=2000
|
102
|
+
c=177
|
103
|
+
b=-15
|
104
|
+
a=0.5
|
105
|
+
```
|
106
|
+
|
107
|
+
```python
|
108
|
+
quantity_values = np.linspace(0, 30, 16) # Levels of Q from 0 to 20
|
109
|
+
quantity_values
|
110
|
+
```
|
111
|
+
|
112
|
+
```python
|
113
|
+
total_costs = cost_function_np(quantity_values)
|
114
|
+
```
|
115
|
+
|
116
|
+
```python
|
117
|
+
|
118
|
+
cost_table = Table().with_columns(
|
119
|
+
'Quantity-Q', quantity_values,
|
120
|
+
'Fixed Costs-FC', d,
|
121
|
+
'Total Costs-TC', total_costs
|
122
|
+
)
|
123
|
+
cost_table
|
124
|
+
```
|
125
|
+
|
126
|
+
```python
|
127
|
+
total_variable_cost = cost_table.column("Total Costs-TC") - cost_table.column('Fixed Costs-FC')
|
128
|
+
total_variable_cost
|
129
|
+
```
|
130
|
+
|
131
|
+
```python
|
132
|
+
cost_table = cost_table.with_column("Total Variable Cost-TVC", total_variable_cost)
|
133
|
+
cost_table
|
134
|
+
```
|
135
|
+
|
136
|
+
```python
|
137
|
+
average_total_cost = cost_table.column("Total Costs-TC") / cost_table.column("Quantity-Q")
|
138
|
+
average_total_cost[0] = 0
|
139
|
+
average_total_cost
|
140
|
+
```
|
141
|
+
|
142
|
+
```python
|
143
|
+
cost_table = cost_table.with_column("Average Total Cost-ATC", average_total_cost)
|
144
|
+
cost_table
|
145
|
+
```
|
146
|
+
|
147
|
+
```python
|
148
|
+
average_variable_cost = cost_table.column("Total Variable Cost-TVC") / cost_table.column("Quantity-Q")
|
149
|
+
average_variable_cost[0] = 0
|
150
|
+
|
151
|
+
cost_table = cost_table.with_column("Average Variable Cost-AVC", average_variable_cost)
|
152
|
+
cost_table
|
153
|
+
```
|
154
|
+
|
155
|
+
```python
|
156
|
+
average_fixed_cost = cost_table.column("Fixed Costs-FC") / cost_table.column("Quantity-Q")
|
157
|
+
average_fixed_cost[0] = 0
|
158
|
+
|
159
|
+
cost_table = cost_table.with_column("Average Fixed Cost-AFC", average_fixed_cost)
|
160
|
+
cost_table
|
161
|
+
```
|
162
|
+
|
163
|
+
## And Finally Marginal Costs
|
164
|
+
|
165
|
+
```python
|
166
|
+
marginal_cost = np.diff(total_costs)
|
167
|
+
marginal_cost = np.append(make_array(0), marginal_cost)
|
168
|
+
marginal_cost
|
169
|
+
```
|
170
|
+
|
171
|
+
```python
|
172
|
+
cost_table = cost_table.with_column("Marginal Cost", marginal_cost)
|
173
|
+
cost_table
|
174
|
+
```
|
175
|
+
|
176
|
+
```python
|
177
|
+
|
178
|
+
```
|
179
|
+
|
180
|
+
```python
|
181
|
+
plt.plot(cost_table.column("Quantity-Q"), cost_table.column("Fixed Costs-FC"), marker='o')
|
182
|
+
plt.plot(cost_table.column("Quantity-Q"), cost_table.column("Total Variable Cost-TVC"), marker='o')
|
183
|
+
plt.plot(cost_table.column("Quantity-Q"), cost_table.column("Total Costs-TC"), marker='o')
|
184
|
+
plt.xlabel('Quantity')
|
185
|
+
plt.ylabel('Cost')
|
186
|
+
plt.title('TFC, TVC and TC')
|
187
|
+
plt.legend(make_array("Total Fixed Cost","Total Variable Cost","Total Cost"))
|
188
|
+
|
189
|
+
plt.show()
|
190
|
+
```
|
191
|
+
|
192
|
+
```python
|
193
|
+
plt.plot(cost_table.column("Quantity-Q")[1:], cost_table.column("Average Fixed Cost-AFC")[1:], marker='o')
|
194
|
+
plt.plot(cost_table.column("Quantity-Q")[1:], cost_table.column("Average Variable Cost-AVC")[1:], marker='o')
|
195
|
+
plt.plot(cost_table.column("Quantity-Q")[1:], cost_table.column("Average Total Cost-ATC")[1:], marker='o')
|
196
|
+
plt.xlabel('Quantity')
|
197
|
+
plt.ylabel('Cost')
|
198
|
+
plt.title('AFC, AVC and ATC')
|
199
|
+
plt.legend(make_array("Average Fixed Cost","Average Variable Cost","Average Total Cost"))
|
200
|
+
|
201
|
+
plt.show()
|
202
|
+
```
|
203
|
+
|
204
|
+
```python
|
205
|
+
# You do not need to understand what the code below is doing.
|
206
|
+
output = cost_table.column("Quantity-Q")[1:]
|
207
|
+
mc = cost_table.column("Marginal Cost")[1:]
|
208
|
+
avc = cost_table.column("Average Variable Cost-AVC")[1:]
|
209
|
+
atc = cost_table.column("Average Total Cost-ATC")[1:]
|
210
|
+
|
211
|
+
sp_mc = csaps(output, mc, smooth=0.85)
|
212
|
+
sp_avc = csaps(output, avc, smooth=0.85)
|
213
|
+
sp_atc = csaps(output, atc, smooth=0.85)
|
214
|
+
|
215
|
+
output_s = np.linspace(output.min(), output.max(), 150)
|
216
|
+
mc_s = sp_mc(output_s)
|
217
|
+
avc_s = sp_avc(output_s)
|
218
|
+
atc_s = sp_atc(output_s)
|
219
|
+
|
220
|
+
plt.plot(output, mc, marker = 'o', color = 'tab:blue')
|
221
|
+
plt.plot(output_s, mc_s, alpha=0.7, lw = 2, label='_nolegend_', color = 'tab:blue')
|
222
|
+
plt.plot(output, avc, marker = 'o', color = 'tab:green')
|
223
|
+
plt.plot(output_s, avc_s, alpha=0.7, lw = 2, label='_nolegend_', color = 'tab:green')
|
224
|
+
plt.plot(output, atc, marker = 'o', color = 'tab:orange')
|
225
|
+
plt.plot(output_s, atc_s, alpha=0.7, lw = 2, label='_nolegend_', color = 'tab:orange')
|
226
|
+
plt.hlines(y=min(avc), xmin = 11.5, xmax = 13, lw=3, color='r', zorder = 10)
|
227
|
+
plt.hlines(y=min(atc), xmin = 16, xmax = 18, lw=3, color='r', zorder = 10)
|
228
|
+
plt.xlabel('Quantity')
|
229
|
+
plt.ylabel('Cost')
|
230
|
+
plt.title('MC, AVC and ATC')
|
231
|
+
plt.legend(make_array("Marginal Cost","Average Variable Cost","Average Total Cost"))
|
232
|
+
|
233
|
+
plt.show()
|
234
|
+
```
|
235
|
+
|
236
|
+
```python
|
237
|
+
|
238
|
+
```
|
239
|
+
|