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,425 @@
|
|
1
|
+
---
|
2
|
+
title: "Avocados_demand"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 2
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec02/Avocados_demand.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
```python
|
9
|
+
# HIDDEN
|
10
|
+
%pip install datascience ipywidgets
|
11
|
+
import numpy as np
|
12
|
+
import pandas as pd
|
13
|
+
import matplotlib.pyplot as plt
|
14
|
+
import warnings
|
15
|
+
warnings.simplefilter("ignore")
|
16
|
+
from matplotlib import patches
|
17
|
+
from datascience import *
|
18
|
+
%matplotlib inline
|
19
|
+
from ipywidgets import interact, interactive, fixed
|
20
|
+
import ipywidgets as widgets
|
21
|
+
```
|
22
|
+
|
23
|
+
# Empirical Demand Curves
|
24
|
+
|
25
|
+
*This deck largely mirrors parts 2-3 of the [demand chapter](https://data-88e.github.io/textbook/content/01-demand/index.html):*
|
26
|
+
- *An Empirical Demand Curve*
|
27
|
+
- *Log-log and Semi-log Demand Curves*
|
28
|
+
|
29
|
+
## Avocados
|
30
|
+
|
31
|
+
Let's examine some historical data on non-organic avocado prices and sales volumes in San Francisco from 2015 to 2018. The original dataset is taken from Kaggle and can be found [here](https://www.kaggle.com/neuromusic/avocado-prices).
|
32
|
+
|
33
|
+
```python
|
34
|
+
avocados = Table.read_table("avocados.csv") # is it avocados or avocadoes?
|
35
|
+
avocados
|
36
|
+
```
|
37
|
+
|
38
|
+
## Visualizing the Relationship between Price and Quantity
|
39
|
+
|
40
|
+
We would expect to see a downward-sloping line between price and quantity; if a product's price increases, consumers will purchase less, and if a product's price decreases, then consumers will purchase more.
|
41
|
+
|
42
|
+
```python
|
43
|
+
avocados.scatter("Total Volume", "Average Price", width = 7, height = 7)
|
44
|
+
plt.title("Demand Curve for Avocados", fontsize = 16);
|
45
|
+
```
|
46
|
+
|
47
|
+
## Linear Demand Curves
|
48
|
+
|
49
|
+
### Demand with Price as a Function of Quantity
|
50
|
+
|
51
|
+
First, we will fit a demand curve expressed in terms of price as a function of quantity. This aligns with the axes of supply and demand curves, in which the quantity is on the x-axis and price is on the y-axis:
|
52
|
+
|
53
|
+
$$P(Q) = m\cdot Q + b$$
|
54
|
+
|
55
|
+
We will now quantify our demand curve using NumPy's [`np.polyfit` function](https://numpy.org/doc/stable/reference/generated/numpy.polyfit.html).
|
56
|
+
|
57
|
+
It takes 3 parameters:
|
58
|
+
- array of x-coordinates
|
59
|
+
- array of y-coordinates
|
60
|
+
- degree of polynomial
|
61
|
+
|
62
|
+
With degree 1, `np.polyfit` returns an array of size 2, where the first element is the slope and the second is the $y$-intercept.
|
63
|
+
|
64
|
+
```python
|
65
|
+
slope, intercept = np.polyfit(avocados.column("Total Volume"), avocados.column("Average Price"), 1)
|
66
|
+
print("The slope is:", slope)
|
67
|
+
print("The intercept is:", intercept)
|
68
|
+
```
|
69
|
+
|
70
|
+
Our demand curve is $P(Q) = -0.00000109Q+ 2.2495$:
|
71
|
+
- The slope is -0.00000109
|
72
|
+
- The $y$-intercept is 2.2495
|
73
|
+
|
74
|
+
This means that as the quantity demanded increases by 1 unit (in this case, 1 avocado), we would expect to see price to decrease by 0.00000109 units (in this case, \$0.000214).
|
75
|
+
|
76
|
+
#### Plotting our demand curve
|
77
|
+
|
78
|
+
```python
|
79
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
80
|
+
quantities = np.arange(400000, 1600000, 1000)
|
81
|
+
predicted_prices = slope * quantities + intercept
|
82
|
+
plt.plot(quantities, predicted_prices, color = 'red', label = "Demand curve")
|
83
|
+
plt.xlabel("Quantity")
|
84
|
+
plt.ylabel("Price")
|
85
|
+
plt.legend();
|
86
|
+
```
|
87
|
+
|
88
|
+
### Demand with Quantity as a Function of Price
|
89
|
+
|
90
|
+
Our interpretation of the demand curve and its slope above was probably not quite intuitive: changes in quantity demanded likely do not trigger changes in price, but instead it is the other way around. In addition, the slope was tiny: the marginal increase of one additional avocado sold had very little effect from the change in price.
|
91
|
+
|
92
|
+
Thus, it is more intuitive to think the effect a one dollar change in price has on the quantity demanded, and to flip our axes:
|
93
|
+
|
94
|
+
$$D(P) = Q(P) = m\cdot P + b$$
|
95
|
+
|
96
|
+
One key thing to remember: our axes are flipped for this demand curve!
|
97
|
+
|
98
|
+
#### Fitting our data using this function, we get:
|
99
|
+
|
100
|
+
```python
|
101
|
+
slope, intercept = np.polyfit(avocados.column("Average Price"), avocados.column("Total Volume"), 1)
|
102
|
+
print("The slope is:", slope)
|
103
|
+
print("The intercept is:", intercept)
|
104
|
+
```
|
105
|
+
|
106
|
+
Our demand curve is roughly $Q(P) = -476413P+ 1446952$:
|
107
|
+
- The slope is -476413
|
108
|
+
- The $y$-intercept is 1446952
|
109
|
+
|
110
|
+
This means that as the price increases by 1 unit (in this case, \$1), we would expect to see quantity demanded to decrease by 476413 units (in this case, 476413 avocados).
|
111
|
+
|
112
|
+
*Note that this demand curve is not the same as the previous demand curve! It is not simply the inverse of the previous demand curve.*
|
113
|
+
|
114
|
+
Plotting this line on a graph, we see a slightly different demand curve.
|
115
|
+
|
116
|
+
```python
|
117
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
118
|
+
prices = np.arange(0.2, 2.3, 0.01)
|
119
|
+
predicted_quantities = slope * prices + intercept
|
120
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Demand curve")
|
121
|
+
|
122
|
+
plt.xlabel("Quantity")
|
123
|
+
plt.ylabel("Price")
|
124
|
+
plt.legend();
|
125
|
+
```
|
126
|
+
|
127
|
+
### A Large Caveat
|
128
|
+
So far, we have examined demand curves assuming that they were linear: specifically, we've assumed that the relationship between quantity demanded and price was linear: for a \$1 change in price, we can expect a fixed change in units demanded at any price level.
|
129
|
+
|
130
|
+
Is this intuitively true?
|
131
|
+
|
132
|
+
As humans, we think about changes as proportions. What this implies is that these curves should be exponential in nature: at higher prices, a larger change in price will yield the same change quantity as compared to that in lower prices.
|
133
|
+
|
134
|
+
Perhaps a better model for demand, then, is that a 1\% change in price will lead to a fixed absolute change in units demanded, or a fixed percentage change in units demanded.
|
135
|
+
|
136
|
+
To model this, we turn to log-log and semi-log demand curves, respectively.
|
137
|
+
|
138
|
+
## An Important Aside: Using Logarithms for Proportional Changes
|
139
|
+
|
140
|
+
Let's consider how the variable GDP behaves.
|
141
|
+
|
142
|
+
GDP tends to grow by a certain percent each year; no one is particularly interested in how *much* GDP changes from one year to the next, or from one country to another, but rather by *what percent* it changes.
|
143
|
+
|
144
|
+
If you were to plot GDP over time for a country, it might look something like this:
|
145
|
+
|
146
|
+
```python
|
147
|
+
GDPs = make_array(100)
|
148
|
+
for _ in np.arange(99):
|
149
|
+
GDPs = np.append(GDPs, GDPs.item(-1) * 1.05)
|
150
|
+
plt.figure(figsize=(8,6))
|
151
|
+
plt.plot(np.arange(100), GDPs)
|
152
|
+
plt.xlabel('Years')
|
153
|
+
plt.ylabel('GDP')
|
154
|
+
plt.title('GDP Over Time');
|
155
|
+
```
|
156
|
+
|
157
|
+
This relationship is fundamentally non-linear.
|
158
|
+
|
159
|
+
However, for variables that change by proportions we can apply a log transformation to make the relationship linear.
|
160
|
+
|
161
|
+
```python
|
162
|
+
ln_GDPs = np.log(GDPs)
|
163
|
+
|
164
|
+
plt.figure(figsize=(8,6))
|
165
|
+
plt.plot(np.arange(100), ln_GDPs)
|
166
|
+
plt.xlabel('Years')
|
167
|
+
plt.ylabel('log GDP')
|
168
|
+
plt.title('GDP Over Time');
|
169
|
+
```
|
170
|
+
|
171
|
+
We've now uncovered a linear relationship between years and GDP! You can interpret the slope of this line as the approximate *percent change* in GDP for an increase in one year (you will later see why the slope is not exactly 0.05). To verify:
|
172
|
+
|
173
|
+
```python
|
174
|
+
print('Slope between years 0 and 1: ', ln_GDPs[1] - ln_GDPs[0])
|
175
|
+
```
|
176
|
+
|
177
|
+
To generalize our results, taking the natural log of a variable allows us to interpret its change as a percentage change instead of an absolute change:
|
178
|
+
|
179
|
+
$$\text{slope} = \frac{\text{Change in GDP %}}{\text{Change in year}} \approx \frac{\text{Change in log-GDP} \times 100}{\text{Change in year}} $$
|
180
|
+
|
181
|
+
## Semi-log Demand Curves
|
182
|
+
|
183
|
+
Suppose that a change in price by \$1 leads to a m\% change in quantity demanded.
|
184
|
+
|
185
|
+
This means that our slope would be:
|
186
|
+
|
187
|
+
$$\text{slope} = \frac{m \text{% change in quantity}}{\text{\$1 change in price}}\approx \frac{m \times 0.01 \text{ change in log-quantity}}{\text{\$1 change in price}} $$
|
188
|
+
|
189
|
+
We have to log transform our quantity-demanded variable to capture the above relationship. This is known as the semi-log demand curve, in which the price and log-quantity are linearly related:
|
190
|
+
|
191
|
+
$$\ln{D(P)} = m\cdot P + b$$
|
192
|
+
|
193
|
+
Let's gain some more intuition of this relationship. By exponentiating both sides, this is equivalent to:
|
194
|
+
|
195
|
+
$$\begin{align*}
|
196
|
+
\ln{D(P)} &= m\cdot P + b \\
|
197
|
+
D(P) &= e^{m\cdot P + b}\\
|
198
|
+
&= e^be^{m\cdot P } \\
|
199
|
+
\end{align*}$$
|
200
|
+
|
201
|
+
What do the slope and intercept represent?
|
202
|
+
|
203
|
+
$b$ (specifically $e^b$) corresponds to the 'baseline' quantity demanded when price is 0, since $e^{m \cdot P} = e^0 = 1$.
|
204
|
+
|
205
|
+
$m$ corresponds roughly to how much a one dollar change in price will lead to a percentage change in quantity demanded.
|
206
|
+
|
207
|
+
To see this, imagine that P goes up by one dollar such that we have:
|
208
|
+
|
209
|
+
$$
|
210
|
+
\begin{align*}
|
211
|
+
D(P+1) &= e^be^{m \cdot (P+1) } \\
|
212
|
+
&= e^be^{m + m \cdot P }\\
|
213
|
+
&= e^be^me^{m \cdot P }\\
|
214
|
+
&= e^mD(P) \\
|
215
|
+
&\approx (1+m)D(P)
|
216
|
+
\end{align*}$$
|
217
|
+
|
218
|
+
The last line relies on the fact $e^{x} \approx (1+x)$ when $x$ is small.
|
219
|
+
|
220
|
+
Our results leads to the caveat that our transformation is only approximate and only valid when our $m$ is small.
|
221
|
+
|
222
|
+
### Visualizing the Semi-log Relationship
|
223
|
+
Plotting $D(P) = e^be^{m\cdot P}$, we get:
|
224
|
+
|
225
|
+
```python
|
226
|
+
m = -0.05
|
227
|
+
b = 5
|
228
|
+
price = np.arange(0,100)
|
229
|
+
quantity = (np.e ** (price * m)) * (np.e ** b)
|
230
|
+
plt.figure(figsize=(7,5))
|
231
|
+
plt.plot(quantity, price)
|
232
|
+
plt.xlabel('Quantity')
|
233
|
+
plt.ylabel('Price')
|
234
|
+
plt.title('Semi-log Demand Curve');
|
235
|
+
```
|
236
|
+
|
237
|
+
### Fitting Semi-log to Avocados
|
238
|
+
|
239
|
+
```python
|
240
|
+
log_quantity = np.log(avocados.column("Total Volume"))
|
241
|
+
slope, intercept = np.polyfit(avocados.column("Average Price"), log_quantity, 1)
|
242
|
+
print("The slope is: ", slope)
|
243
|
+
print("The intercept is: ", intercept)
|
244
|
+
```
|
245
|
+
|
246
|
+
For every one dollar change in price of avocados, we would expect the change in quantity demanded to decrease by 57%.
|
247
|
+
|
248
|
+
*Take this result with a grain of salt – recall that our approximation typically is valid for small values of $m$, and here our $m=-0.57$*.
|
249
|
+
|
250
|
+
#### Plotting on log quantity
|
251
|
+
|
252
|
+
```python
|
253
|
+
plt.scatter(log_quantity, avocados.column("Average Price"))
|
254
|
+
prices = np.arange(0.5, 2.3, 0.01)
|
255
|
+
predicted_quantities = slope * prices + intercept
|
256
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Semi-log demand curve")
|
257
|
+
plt.xlabel("Log Quantity")
|
258
|
+
plt.ylabel("Price")
|
259
|
+
plt.legend();
|
260
|
+
```
|
261
|
+
|
262
|
+
#### Plotting without axes transformations
|
263
|
+
|
264
|
+
```python
|
265
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
266
|
+
prices = np.arange(0.5, 2.3, 0.01)
|
267
|
+
predicted_quantities = np.e ** (slope * prices + intercept)
|
268
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Semi-log demand curve")
|
269
|
+
plt.xlabel("Quantity")
|
270
|
+
plt.ylabel("Price")
|
271
|
+
plt.legend();
|
272
|
+
```
|
273
|
+
|
274
|
+
## Log-log Demand Curves
|
275
|
+
|
276
|
+
Now suppose that a 1% change in price leads to a m% change in quantity demanded.
|
277
|
+
|
278
|
+
This means that our slope would be:
|
279
|
+
|
280
|
+
$$\text{slope} = \frac{m \text{% change in quantity}}{1 \text{% change in price}} \approx \frac{m \times 0.01 \text{ change in log-quantity}}{ 0.01 \text{ change in log-price}} = \frac{\text{change by } m \text{ in log-quantity}}{\text{ change by 1 in log-price}}$$
|
281
|
+
|
282
|
+
In this case, we have to log transform both our quantity-demanded variable as well as price variable to capture the above relationship. This is known as the log-log demand curve, in which the log-price and log-quantity are linearly related:
|
283
|
+
|
284
|
+
$$\ln{D(P)} = m\cdot\ln{P} + b$$
|
285
|
+
|
286
|
+
Let's gain some more intuition of this relationship. By exponentiating both sides, this is equivalent to:
|
287
|
+
|
288
|
+
$$\begin{align*}
|
289
|
+
D(P) &= e^{m\cdot\ln{P} + b}\\
|
290
|
+
&= e^be^{m\cdot\ln{P}} \\
|
291
|
+
&= e^b(e^{\ln{P}})^m \\
|
292
|
+
&= e^bP^m \\
|
293
|
+
\end{align*}$$
|
294
|
+
|
295
|
+
In this setup, $b$ does not have as clear a meaning. For $m$, we can once again suppose that P goes up by one dollar:
|
296
|
+
|
297
|
+
$$\begin{align*}
|
298
|
+
D(P+1) &= e^b(P+1)^m \\
|
299
|
+
&\approx e^b (1+m)P^m\\
|
300
|
+
&\approx (1+m) D(P)
|
301
|
+
\end{align*}$$
|
302
|
+
|
303
|
+
Where we utilize the approximation that $(P+1)^m \approx P^m \times (1+m)$. Our caveat from the previous section about $m$ being small continues to be in place here: typically, we do not want our $m$ to be larger than $0.2$, or else the approxmation will fall apart.
|
304
|
+
|
305
|
+
### Visualizing the Log-log Relationship
|
306
|
+
Plotting $D(P) = e^bP^m$, we get:
|
307
|
+
|
308
|
+
```python
|
309
|
+
m = -0.05
|
310
|
+
b = 5
|
311
|
+
price = np.arange(0,100)
|
312
|
+
quantity = (price ** m) * (np.e ** b)
|
313
|
+
plt.figure(figsize=(7,5))
|
314
|
+
plt.plot(quantity, price)
|
315
|
+
plt.xlabel('Quantity')
|
316
|
+
plt.ylabel('Price')
|
317
|
+
plt.title('Log-log Demand Curve');
|
318
|
+
```
|
319
|
+
|
320
|
+
#### A caveat about our log-log model
|
321
|
+
Since our model is ultimately linear between log-price and log-quantity, the slope of log-price to log-quantity is always the same.
|
322
|
+
|
323
|
+
This means that at any price level, we assume a 1% change in price will yield the same percentage change in quantity.
|
324
|
+
|
325
|
+
This is also known as fixed elasticities.
|
326
|
+
|
327
|
+
### Elasticities
|
328
|
+
|
329
|
+
Elasticity is defined:
|
330
|
+
$$\varepsilon = \frac{\Delta \% \text{Quantity}}{\Delta \% \text{Price}}$$
|
331
|
+
|
332
|
+
Similarly, in point-slope form:
|
333
|
+
$$\varepsilon = \frac{\frac{\Delta Q}{Q}}{\frac{\Delta P}{P}} = \frac{\Delta Q}{\Delta P} \frac{P}{Q}$$
|
334
|
+
|
335
|
+
Elasticity behaves like the slope in calculus; thus when approximating using the point-slope formula it is typically only valid in small % changes of quantity or price.
|
336
|
+
|
337
|
+
Elastic demand: $\varepsilon > 1$
|
338
|
+
- % change in price leads to a greater % change in quantity
|
339
|
+
- Profit increases from a decrease in price
|
340
|
+
- Examples: McDonalds, Toyota Prius, electronic devices
|
341
|
+
|
342
|
+
Inelastic demand: $\varepsilon < 1$
|
343
|
+
- % change in price leads to a smaller % change in quantity
|
344
|
+
- Profit decreases from a decrease in price
|
345
|
+
- Examples: insulin, gasoline, cigarettes
|
346
|
+
|
347
|
+
Consider the log-log demand curve, which assumes constant elasticity:
|
348
|
+
|
349
|
+
```python
|
350
|
+
m = -0.05
|
351
|
+
b = 5
|
352
|
+
price = np.arange(0,100)
|
353
|
+
quantity = (price ** m) * (np.e ** b)
|
354
|
+
plt.figure(figsize=(7,5))
|
355
|
+
plt.plot(quantity, price)
|
356
|
+
plt.xlabel('Quantity')
|
357
|
+
plt.ylabel('Price')
|
358
|
+
plt.title('Log-log Demand Curve');
|
359
|
+
```
|
360
|
+
|
361
|
+
Now consider the linear demand curve; which segments are elastic and which are inelastic?
|
362
|
+
|
363
|
+
```python
|
364
|
+
price = np.arange(0,100)
|
365
|
+
quantity = -1 * price + 100
|
366
|
+
plt.figure(figsize=(7,5))
|
367
|
+
plt.plot(quantity, price)
|
368
|
+
plt.xlabel('Quantity')
|
369
|
+
plt.ylabel('Price')
|
370
|
+
plt.title('Linear Demand Curve');
|
371
|
+
```
|
372
|
+
|
373
|
+
### Fitting Log-log to Avocados
|
374
|
+
|
375
|
+
```python
|
376
|
+
log_quantity = np.log(avocados.column("Total Volume"))
|
377
|
+
log_price = np.log(avocados.column("Average Price"))
|
378
|
+
slope, intercept = np.polyfit(log_price, log_quantity, 1)
|
379
|
+
print("The slope is: ", slope)
|
380
|
+
print("The intercept is: ", intercept)
|
381
|
+
```
|
382
|
+
|
383
|
+
For every 1% change in price of avocados, we would expect the change in quantity demanded to decrease by $-0.816\%$.
|
384
|
+
|
385
|
+
Is demand elastic or inelastic?
|
386
|
+
|
387
|
+
#### Plotting log-log demand curve with both axes log-transformed
|
388
|
+
|
389
|
+
```python
|
390
|
+
plt.scatter(log_quantity, log_price)
|
391
|
+
prices = np.arange(-0.2, 0.8, 0.01)
|
392
|
+
predicted_quantities = slope * prices + intercept
|
393
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Log-log demand curve")
|
394
|
+
|
395
|
+
plt.xlabel("Log Quantity")
|
396
|
+
plt.ylabel("Log Price")
|
397
|
+
plt.legend();
|
398
|
+
```
|
399
|
+
|
400
|
+
#### Plotting without axes transformations
|
401
|
+
|
402
|
+
```python
|
403
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
404
|
+
prices = np.arange(0.6, 2.5, 0.01)
|
405
|
+
predicted_quantities = (np.e ** intercept) * (prices ** slope)
|
406
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Log-log demand curve")
|
407
|
+
plt.xlabel("Quantity")
|
408
|
+
plt.ylabel("Price")
|
409
|
+
plt.legend();
|
410
|
+
```
|
411
|
+
|
412
|
+
## Which Model is Better: Linear, Semi-log, or Log-log?
|
413
|
+
|
414
|
+
There is no correct answer here, in fact justifying one approach over another is surprisingly profound.
|
415
|
+
|
416
|
+
- One way to approach this is to look at the graphs produced above and which red line goes through our data points "best" (but what does "best" mean? We'll save this for another day...)
|
417
|
+
- Another approach is to utilize our real-world knowledge to conclude which relationship is more accurate: do consumers react similarly to price changes that are in a proportion manner or in a absolute manner? This may also depend on the price, the promotion around it, the product itself, and many other factors.
|
418
|
+
|
419
|
+
### An Afterword
|
420
|
+
This example highlights how ambiguity is a big part of doing data science. We can approach ambiguity with statistical methods and with domain knowledge. Either way, as long as you can ultimately justify your approach, that is what is key in conducting robust data science.
|
421
|
+
|
422
|
+
```python
|
423
|
+
|
424
|
+
```
|
425
|
+
|
@@ -0,0 +1,126 @@
|
|
1
|
+
---
|
2
|
+
title: "Demand_Steps_24"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 2
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec02/Demand_Steps_24.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
## Demand Curve step by step
|
9
|
+
|
10
|
+
|
11
|
+
We will create a few demand curves based on the class. Start by filling in the form at
|
12
|
+
- https://forms.gle/uxgsxtnedqeANshCA or
|
13
|
+
- https://tinyurl.com/data88fa24demand
|
14
|
+
|
15
|
+
```python
|
16
|
+
import pandas as pd
|
17
|
+
from datascience import *
|
18
|
+
import numpy as np
|
19
|
+
%matplotlib inline
|
20
|
+
```
|
21
|
+
|
22
|
+
### Find the Sheet ID in the URL of the Google Sheet!
|
23
|
+
|
24
|
+
Take a look at the data
|
25
|
+
https://docs.google.com/spreadsheets/d/1jp-XrFPk0eUNDUVWGa7Rmw9b0P8_jobTG0oLpvcHB9s/edit?resourcekey=&gid=418675525#gid=418675525
|
26
|
+
|
27
|
+
```python
|
28
|
+
sheet_id = "1jp-XrFPk0eUNDUVWGa7Rmw9b0P8_jobTG0oLpvcHB9s"
|
29
|
+
sheet_name = "Form1"
|
30
|
+
url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
|
31
|
+
```
|
32
|
+
|
33
|
+
Read it into a datascience table
|
34
|
+
|
35
|
+
```python
|
36
|
+
demand_table = Table.read_table(url)
|
37
|
+
demand_table
|
38
|
+
```
|
39
|
+
|
40
|
+
```python
|
41
|
+
demand_table.ihist("Coffee",bins=7)
|
42
|
+
```
|
43
|
+
|
44
|
+
```python
|
45
|
+
demand_table.ihist("Burrito",bins=7)
|
46
|
+
```
|
47
|
+
|
48
|
+
## Gonna roll with Burrito for this example
|
49
|
+
|
50
|
+
Step 1 - Lets pull out just Burritos
|
51
|
+
|
52
|
+
This is a table with just Burrito prices that people are willing to pay ( bids)
|
53
|
+
|
54
|
+
```python
|
55
|
+
Burrito = demand_table.select("Burrito")
|
56
|
+
Burrito
|
57
|
+
```
|
58
|
+
|
59
|
+
Step 2 - Let's count the number at each price
|
60
|
+
|
61
|
+
And sort the table so that it is descending from high to low price
|
62
|
+
|
63
|
+
```python
|
64
|
+
# count the number at each price
|
65
|
+
Burrito_counts = demand_table.group("Burrito")
|
66
|
+
Burrito_counts = Burrito_counts.sort('Burrito', descending=True)
|
67
|
+
|
68
|
+
Burrito_counts
|
69
|
+
```
|
70
|
+
|
71
|
+
Step 3 - Let's pull out those counts
|
72
|
+
|
73
|
+
```python
|
74
|
+
counts = Burrito_counts.column("count")
|
75
|
+
print(counts)
|
76
|
+
```
|
77
|
+
|
78
|
+
Step 4 - use a numpy command called cumulative sum to get the number of people who will buy at each price
|
79
|
+
|
80
|
+
```python
|
81
|
+
cumulative_counts = counts.cumsum()
|
82
|
+
cumulative_counts
|
83
|
+
```
|
84
|
+
|
85
|
+
Step 5 - make an array of the prices of the burritos in descending order
|
86
|
+
|
87
|
+
```python
|
88
|
+
prices = make_array(20,17.5,15,12.5,10,7.5,5,2.5)
|
89
|
+
prices
|
90
|
+
```
|
91
|
+
|
92
|
+
Step 6 - make a table with the prices and the cumulative counts
|
93
|
+
|
94
|
+
```python
|
95
|
+
demand_curve = Table().with_columns("Price", prices, "Cumulative Count", cumulative_counts)
|
96
|
+
demand_curve
|
97
|
+
```
|
98
|
+
|
99
|
+
```python
|
100
|
+
demand_curve.iscatter("Cumulative Count","Price")
|
101
|
+
```
|
102
|
+
|
103
|
+
```python
|
104
|
+
demand_curve.iscatter("Cumulative Count","Price", fit_line=True)
|
105
|
+
```
|
106
|
+
|
107
|
+
```python
|
108
|
+
# fit a line to the data using numpy
|
109
|
+
m, b = np.polyfit(cumulative_counts,prices, 1)
|
110
|
+
print(m, b)
|
111
|
+
```
|
112
|
+
|
113
|
+
```python
|
114
|
+
# add a new column to the table with the log of price
|
115
|
+
demand_curve = demand_curve.with_column("Log Price", np.log(prices))
|
116
|
+
demand_curve
|
117
|
+
```
|
118
|
+
|
119
|
+
```python
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
```python
|
124
|
+
|
125
|
+
```
|
126
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
---
|
2
|
+
title: "PriceElasticity"
|
3
|
+
type: lecture-notebook
|
4
|
+
week: 2
|
5
|
+
source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec02/PriceElasticity.ipynb"
|
6
|
+
---
|
7
|
+
|
8
|
+
## Price Elasticity of Demand
|
9
|
+
|
10
|
+
```python
|
11
|
+
def calculate_ped(initial_quantity, new_quantity, initial_price, new_price):
|
12
|
+
# Calculate the percentage change in quantity demanded
|
13
|
+
percent_change_in_quantity = ((new_quantity - initial_quantity) / initial_quantity) * 100
|
14
|
+
|
15
|
+
# Calculate the percentage change in price
|
16
|
+
percent_change_in_price = ((new_price - initial_price) / initial_price) * 100
|
17
|
+
|
18
|
+
# Calculate the price elasticity of demand (PED)
|
19
|
+
ped = percent_change_in_quantity / percent_change_in_price
|
20
|
+
|
21
|
+
return ped
|
22
|
+
```
|
23
|
+
|
24
|
+
## Let's start with the Textbook Example
|
25
|
+
https://data88e.org/textbook/content/01-demand/04-elasticity.html
|
26
|
+
|
27
|
+

|
28
|
+
|
29
|
+
### Example 1
|
30
|
+
|
31
|
+
```python
|
32
|
+
|
33
|
+
initial_quantity = 5
|
34
|
+
new_quantity = 4
|
35
|
+
initial_price = 5
|
36
|
+
new_price = 6
|
37
|
+
|
38
|
+
ped = calculate_ped(initial_quantity, new_quantity, initial_price, new_price)
|
39
|
+
print(f"The price elasticity of demand is {ped:.2f}")
|
40
|
+
```
|
41
|
+
|
42
|
+
### Example 2
|
43
|
+
|
44
|
+
```python
|
45
|
+
initial_quantity = 4
|
46
|
+
new_quantity = 5
|
47
|
+
initial_price = 6
|
48
|
+
new_price = 5
|
49
|
+
|
50
|
+
ped = calculate_ped(initial_quantity, new_quantity, initial_price, new_price)
|
51
|
+
print(f"The price elasticity of demand is {ped:.2f}")
|
52
|
+
```
|
53
|
+
|
54
|
+
## How about the point slope formula for eslasticity of demand?
|
55
|
+
|
56
|
+
Slope = -1 along the line
|
57
|
+
|
58
|
+
```python
|
59
|
+
slope=-1
|
60
|
+
```
|
61
|
+
|
62
|
+
### Example 1 - point slope
|
63
|
+
|
64
|
+
```python
|
65
|
+
initial_quantity = 5
|
66
|
+
initial_price = 5
|
67
|
+
ped = slope * (initial_price/initial_quantity)
|
68
|
+
print(f"The price elasticity of demand is {ped:.2f}")
|
69
|
+
```
|
70
|
+
|
71
|
+
### Example 2 - point slope
|
72
|
+
|
73
|
+
```python
|
74
|
+
initial_quantity = 4
|
75
|
+
initial_price = 6
|
76
|
+
ped = slope * (initial_price/initial_quantity)
|
77
|
+
print(f"The price elasticity of demand is {ped:.2f}")
|
78
|
+
```
|
79
|
+
|
80
|
+
```python
|
81
|
+
|
82
|
+
```
|
83
|
+
|