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,284 @@
|
|
1
|
+
---
|
2
|
+
title: 03-log-log
|
3
|
+
type: textbook
|
4
|
+
source_path: content/01-demand/03-log-log.ipynb
|
5
|
+
chapter: 1
|
6
|
+
---
|
7
|
+
|
8
|
+
# Log-log and Semi-log Demand Curves
|
9
|
+
|
10
|
+
```python
|
11
|
+
# HIDDEN
|
12
|
+
import numpy as np
|
13
|
+
import pandas as pd
|
14
|
+
import matplotlib.pyplot as plt
|
15
|
+
import warnings
|
16
|
+
warnings.simplefilter("ignore")
|
17
|
+
from matplotlib import patches
|
18
|
+
from datascience import *
|
19
|
+
%matplotlib inline
|
20
|
+
from ipywidgets import interact, interactive, fixed
|
21
|
+
import ipywidgets as widgets
|
22
|
+
```
|
23
|
+
|
24
|
+
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.
|
25
|
+
|
26
|
+
Is this intuitively true? Probably not. For example, a \$1 decrease from \$100 is trivial compared to a \$1 decrease from an original price of \$2. As humans, we think about changes as proportions, and this fact should be reflected in the supply and demand curves. 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.
|
27
|
+
|
28
|
+
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. To model this, we turn to log-log and semi-log demand curves, respectively.
|
29
|
+
|
30
|
+
## Using Logarithms for Proportional Changes
|
31
|
+
|
32
|
+
Before we jump into our new demand models, we must first go over measuring proportional changes.
|
33
|
+
To highlight this fact, let's actually consider how the variable GDP behaves. 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.
|
34
|
+
|
35
|
+
If you were to plot GDP over time for a country, it might look something like this:
|
36
|
+
|
37
|
+
[Following image is a line of increasing upward sloping growth of GDP over time]
|
38
|
+
|
39
|
+
```python
|
40
|
+
GDPs = make_array(100)
|
41
|
+
|
42
|
+
for _ in np.arange(99):
|
43
|
+
GDPs = np.append(GDPs, GDPs.item(-1) * 1.05)
|
44
|
+
|
45
|
+
plt.figure(figsize=(8,6))
|
46
|
+
plt.plot(np.arange(100), GDPs)
|
47
|
+
plt.xlabel('Years')
|
48
|
+
plt.ylabel('GDP')
|
49
|
+
plt.title('GDP Over Time');
|
50
|
+
```
|
51
|
+
|
52
|
+
The starting value for GDP is 100, and GDP grows by 5 percent each year. Look at how how much GDP grows in the later years!
|
53
|
+
|
54
|
+
|
55
|
+
While this phenomenon is impressive, it is misleading. At surface level, it seems to imply that something different happened at around year 50 or so that caused GDP to increase considerably in subsequent years. We know that this isn't true, and that this is just a consequence of exponential growth.
|
56
|
+
|
57
|
+
To counter this effect, **for variables that tend to vary from one observation to the next by proportions rather than absolute amounts, we take the natural log of these variables**. Let's do that for GDP:
|
58
|
+
|
59
|
+
[Following image is a line of constant upward sloping growth of GDP over time]
|
60
|
+
|
61
|
+
```python
|
62
|
+
ln_GDPs = np.log(GDPs)
|
63
|
+
|
64
|
+
plt.figure(figsize=(8,6))
|
65
|
+
plt.plot(np.arange(100), ln_GDPs)
|
66
|
+
plt.xlabel('Years')
|
67
|
+
plt.ylabel('log GDP')
|
68
|
+
plt.title('GDP Over Time');
|
69
|
+
```
|
70
|
+
|
71
|
+
We've now uncovered a linear relationship between years and GDP! You can interpret the slope of this line as the approximate factor that GDP increases by for an increase in one year. If we want the percentage change in GDP for an increase in one year, we can multiply by 100. (You will later see why the slope is not exactly 0.05). To verify:
|
72
|
+
|
73
|
+
```python
|
74
|
+
print('Slope between years 0 and 1: ', ln_GDPs[1] - ln_GDPs[0])
|
75
|
+
```
|
76
|
+
|
77
|
+
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. Using our GDP example from above, the slope after taking the natural log of GDP is now roughly equal to:
|
78
|
+
|
79
|
+
$$\text{slope} = \frac{\text{% change in GDP}}{\text{Change in time (1 year)}} \approx \frac{\text{Change in log-GDP} \times 100}{\text{Change in time (1 year)}} $$
|
80
|
+
|
81
|
+
## Semi-log Demand Curves
|
82
|
+
|
83
|
+
Revisiting demand curves, suppose that a change in price by $1 leads to a change in quantity demanded by a factor of $m$. This means that our slope would be:
|
84
|
+
|
85
|
+
$$\text{slope} = \frac{\text{change in quantity by a factor of } m}{\text{ \$1 change in price}} \approx \frac{m \text{ change in log-quantity}}{\text{\$1 change in price}}$$
|
86
|
+
|
87
|
+
Using our intuition from above, we simply 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:
|
88
|
+
|
89
|
+
$$\ln{D(P)} = m\cdot P + b$$
|
90
|
+
|
91
|
+
Let's gain some more intuition of this relationship. By exponentiating both sides, this is equivalent to:
|
92
|
+
|
93
|
+
$$\begin{align*}
|
94
|
+
D(P) &= e^{m\cdot P + b}\\
|
95
|
+
&= e^be^{m\cdot P } \\
|
96
|
+
\end{align*}$$
|
97
|
+
|
98
|
+
This rewriting provides us some intuition on the interpretation of $m$ and $b$, the slope and intercept terms in the semi-log relationship. $b$ (specifically $e^b$) corresponds to the 'baseline' quantity demanded when price is 0, since $e^{m \cdot P} = e^0 = 1$.
|
99
|
+
|
100
|
+
$m$ corresponds roughly to how much a one dollar change in price will lead to a percentage change in quantity demanded. To see this, imagine that P goes up by one dollar such that we have:
|
101
|
+
|
102
|
+
$$
|
103
|
+
\begin{align*}
|
104
|
+
D(P+1) &= e^be^{m \cdot (P+1) } \\
|
105
|
+
&= e^be^{m + m \cdot P }\\
|
106
|
+
&= e^be^me^{m \cdot P }\\
|
107
|
+
&= e^mD(P) \\
|
108
|
+
&\approx (1+m)D(P)
|
109
|
+
\end{align*}$$
|
110
|
+
|
111
|
+
The last line relies on the fact $e^{x} \approx (1+x)$ when $x$ is small. This is why in our GDP example, our slope was not exactly 0.05. Thus, our results leads to the caveat that our transformation is only approximate and only valid when our $m$ is small. Typically, we do not want $m$ to be larger than $0.2$, or else the approximation will start to diverge.
|
112
|
+
|
113
|
+
### Visualizing the semi-log relationship
|
114
|
+
From above, we can convert the linear semi-log relationship into non-log terms for price and quantity, ultimately getting $D(P) = e^be^{m\cdot P}$. Plotting this relationship, we get:
|
115
|
+
|
116
|
+
[Following image is a line of downward sloping semi-log Demand]
|
117
|
+
|
118
|
+
```python
|
119
|
+
m = -0.05
|
120
|
+
b = 5
|
121
|
+
|
122
|
+
x = np.arange(0,100)
|
123
|
+
y = (np.e ** (x * m)) * (np.e ** b)
|
124
|
+
|
125
|
+
plt.figure(figsize=(8,6))
|
126
|
+
plt.plot(x, y)
|
127
|
+
plt.xlabel('Quantity')
|
128
|
+
plt.ylabel('Price')
|
129
|
+
plt.title('Semi-log Demand Curve');
|
130
|
+
```
|
131
|
+
|
132
|
+
## Log-log Demand Curves
|
133
|
+
|
134
|
+
Now suppose that a 1% change in price leads to an $m$% change in quantity demanded. This means that our slope would be:
|
135
|
+
|
136
|
+
$$\text{slope} = \frac{m * \text{pct change in quantity}}{1 \text{pct change in price}} \approx \frac{0.01m \text{ change in log-quantity}}{0.01 \text{ change in log-price}}$$
|
137
|
+
|
138
|
+
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:
|
139
|
+
|
140
|
+
$$\ln{D(P)} = m\cdot\ln{P} + b$$
|
141
|
+
|
142
|
+
Let's gain some more intuition of this relationship. By exponentiating both sides, this is equivalent to:
|
143
|
+
|
144
|
+
$$\begin{align*}
|
145
|
+
D(P) &= e^{m\cdot\ln{P} + b}\\
|
146
|
+
&= e^be^{m\cdot\ln{P}} \\
|
147
|
+
&= e^b(e^{\ln{P}})^m \\
|
148
|
+
&= e^bP^m \\
|
149
|
+
\end{align*}$$
|
150
|
+
|
151
|
+
In this setup, $b$ does not have as clear a meaning. For $m$, we can suppose that $P$ goes up by one percent:
|
152
|
+
|
153
|
+
$$\begin{align*}
|
154
|
+
D(1.01P) &= e^b(1.01P)^m \\
|
155
|
+
&= e^b 1.01^m P^m\\
|
156
|
+
&= 1.01^m D(P) \\
|
157
|
+
&\approx (1 + 0.01m) D(P)
|
158
|
+
\end{align*}$$
|
159
|
+
|
160
|
+
Where we utilize the approximation that $1.01^m \approx 1 + 0.01m$. Our caveat from the previous section about $m$ not being large continues to be in place here: typically, we do not want our $m$ to be larger than 30 (where $1.01^{30}\approx 1.35$), or else the approximation will fall apart.
|
161
|
+
|
162
|
+
### Visualizing the log-log relationship
|
163
|
+
From above, we can convert the linear log-log relationship into non-log terms for price and quantity, ultimately getting $D(P) = e^bP^m$. Plotting this relationship, we can see a demand curve that looks somewhat like an exponential decay curve:
|
164
|
+
|
165
|
+
[Following image is a line of downward sloping log-log Demand]
|
166
|
+
|
167
|
+
```python
|
168
|
+
m = -0.05
|
169
|
+
b = 5
|
170
|
+
|
171
|
+
x = np.arange(0,100)
|
172
|
+
y = (x ** m) * (np.e ** b)
|
173
|
+
|
174
|
+
plt.figure(figsize=(8,6))
|
175
|
+
plt.plot(x, y)
|
176
|
+
plt.xlabel('Quantity')
|
177
|
+
plt.ylabel('Price')
|
178
|
+
plt.title('Log-log Demand Curve');
|
179
|
+
```
|
180
|
+
|
181
|
+
A caveat about our model: since our model is ultimately linear between log-price and log-quantity, the slope is always the same. This means that at any price level, we assume a 1% change in price will yield the same percentage change in quantity. This is also known as fixed elasticities -- a topic we will dive in on the next page.
|
182
|
+
|
183
|
+
## Revisiting Avocados
|
184
|
+
We will revisit the avocados dataset from the previous page, which features historical data on non-organic avocado prices and sales volumes in San Francisco from 2015 to 2018.
|
185
|
+
|
186
|
+
```python
|
187
|
+
avocados = Table.read_table("avocados.csv") # is it avocados or avocadoes?
|
188
|
+
avocados
|
189
|
+
```
|
190
|
+
|
191
|
+
Taking a look at the natural relationship between price and volume sold, we see a possible exponentially decaying relationship between volume and price. We will try using both semi-log and log-log transformations on the data.
|
192
|
+
|
193
|
+
[Following image is a scatter plot of Volume vs Price ]
|
194
|
+
|
195
|
+
```python
|
196
|
+
avocados.scatter("Total Volume", "Average Price")
|
197
|
+
```
|
198
|
+
|
199
|
+
### Semi-log
|
200
|
+
|
201
|
+
```python
|
202
|
+
log_quantity = np.log(avocados.column("Total Volume"))
|
203
|
+
|
204
|
+
slope, intercept = np.polyfit(avocados.column("Average Price"), log_quantity, 1)
|
205
|
+
print("The slope is: ", slope)
|
206
|
+
print("The intercept is: ", intercept)
|
207
|
+
```
|
208
|
+
|
209
|
+
Interpreting the slope, we get that for every one dollar change in price of avocados, we would expect the change in quantity demanded to decrease by 57%. 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$.
|
210
|
+
|
211
|
+
Now, let's plot our semi-log demand curve. First, we will plot it on an axes in which the quantity is log-transformed, exhibiting a linear demand curve:
|
212
|
+
|
213
|
+
[Following image is a straight line of downward sloping semi-log Demand]
|
214
|
+
|
215
|
+
```python
|
216
|
+
plt.scatter(log_quantity, avocados.column("Average Price"))
|
217
|
+
prices = np.arange(0.5, 2.3, 0.01)
|
218
|
+
predicted_quantities = slope * prices + intercept
|
219
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Semi-log demand curve")
|
220
|
+
plt.xlabel("Log Quantity")
|
221
|
+
plt.ylabel("Price")
|
222
|
+
plt.legend();
|
223
|
+
```
|
224
|
+
|
225
|
+
Next, let's plot our semi-log demand curve in which neither axis is log-transformed:
|
226
|
+
|
227
|
+
[Following image is a curved line of downward sloping semi-log Demand]
|
228
|
+
|
229
|
+
```python
|
230
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
231
|
+
prices = np.arange(0.5, 2.3, 0.01)
|
232
|
+
predicted_quantities = np.e ** (slope * prices + intercept)
|
233
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Semi-log demand curve")
|
234
|
+
plt.xlabel("Quantity")
|
235
|
+
plt.ylabel("Price")
|
236
|
+
plt.legend();
|
237
|
+
```
|
238
|
+
|
239
|
+
### Log-log
|
240
|
+
|
241
|
+
```python
|
242
|
+
log_quantity = np.log(avocados.column("Total Volume"))
|
243
|
+
log_price = np.log(avocados.column("Average Price"))
|
244
|
+
|
245
|
+
slope, intercept = np.polyfit(log_price, log_quantity, 1)
|
246
|
+
print("The slope is: ", slope)
|
247
|
+
print("The intercept is: ", intercept)
|
248
|
+
```
|
249
|
+
|
250
|
+
Interpreting the slope, we get that for every 1% change in price of avocados, we would expect the change in quantity demanded to decrease by $-0.816\%$. Once again, keep in mind that our approximation typically is valid for small values of $m$, and here our $m=-0.816$.
|
251
|
+
|
252
|
+
Plotting our log-log demand curve with both axes log-transformed, we observe a linear demand curve:
|
253
|
+
|
254
|
+
[Following image is a straight line of downward sloping log-log Demand]
|
255
|
+
|
256
|
+
```python
|
257
|
+
plt.scatter(log_quantity, log_price)
|
258
|
+
prices = np.arange(-0.2, 0.8, 0.01)
|
259
|
+
predicted_quantities = slope * prices + intercept
|
260
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Log-log demand curve")
|
261
|
+
|
262
|
+
plt.xlabel("Log Quantity")
|
263
|
+
plt.ylabel("Log Price")
|
264
|
+
plt.legend();
|
265
|
+
```
|
266
|
+
|
267
|
+
Finally, let's plot our log-log demand curve in which neither axis is log-transformed:
|
268
|
+
|
269
|
+
[Following image is a curved line of downward sloping log-log Demand]
|
270
|
+
|
271
|
+
```python
|
272
|
+
plt.scatter(avocados.column("Total Volume"), avocados.column("Average Price"))
|
273
|
+
prices = np.arange(0.6, 2.5, 0.01)
|
274
|
+
predicted_quantities = (np.e ** intercept) * (prices ** slope)
|
275
|
+
plt.plot(predicted_quantities, prices, color = 'red', label = "Log-log demand curve")
|
276
|
+
plt.xlabel("Quantity")
|
277
|
+
plt.ylabel("Price")
|
278
|
+
plt.legend();
|
279
|
+
```
|
280
|
+
|
281
|
+
Which model is better: semi-log or log-log? There is no correct answer here, in fact justifying one approach over another is surprisingly profound. 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...). 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.
|
282
|
+
|
283
|
+
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.
|
284
|
+
|
@@ -0,0 +1,248 @@
|
|
1
|
+
---
|
2
|
+
title: 04-elasticity
|
3
|
+
type: textbook
|
4
|
+
source_path: content/01-demand/04-elasticity.ipynb
|
5
|
+
chapter: 1
|
6
|
+
---
|
7
|
+
|
8
|
+
```python
|
9
|
+
from datascience import *
|
10
|
+
import matplotlib.pyplot as plt
|
11
|
+
%matplotlib inline
|
12
|
+
import numpy as np
|
13
|
+
from utils import *
|
14
|
+
plt.style.use('seaborn-muted')
|
15
|
+
import sympy
|
16
|
+
```
|
17
|
+
|
18
|
+
# Elasticity
|
19
|
+
|
20
|
+
Suppose you own a business but you find yourself struggling to make a profit lately. How would you resolve this? You might consider raising your prices. But surely if you were to raise your prices too much you might lose some of your customers. To relate to our discussions above regarding surplus, maybe some of your customers are already at their highest willingness to pay, and raising prices would push them away from your business. Depending on the situation, you might lose so many customers that the increased revenue from raising your prices are completely offset by a decline in customers. This is a problem. To answer the question of how much to raise prices without losing too many customers and therefore losing revenue, we would like a way to measure this concept of customers' responsiveness to prices. We call this concept *elasticity*.
|
21
|
+
|
22
|
+
How would we measure elasticity? In the above context, we want to see how much quantity demanded of your business's goods or services changes in response to a change in price. It seems like a good place to start would be to define elasticity as the percent change in quantity over percent change in price. Let's use the demand curve, assuming linear non-log demand curves, depicted below as an example.
|
23
|
+
|
24
|
+
[Following image is a line of downward sloping Demand]
|
25
|
+
|
26
|
+
```python
|
27
|
+
def plot_demand1():
|
28
|
+
p = sympy.Symbol("p")
|
29
|
+
demand_equation = 10 - p
|
30
|
+
prices = [x for x in range(0,11)]
|
31
|
+
demand_Q = [demand_equation.subs(p,x) for x in prices]
|
32
|
+
plt.figure(figsize = [9,6])
|
33
|
+
plt.plot(prices,demand_Q)
|
34
|
+
plt.xlabel("Demand Quantity")
|
35
|
+
plt.ylabel("Price")
|
36
|
+
plt.title("Demand Curve")
|
37
|
+
|
38
|
+
plot_demand1()
|
39
|
+
plt.show()
|
40
|
+
```
|
41
|
+
|
42
|
+
Now let's say our current price is 5, and we would like to increase the price to 6. We can see from the curve that this causes demand to drop from 5 to 4.
|
43
|
+
|
44
|
+
[Following image is a line of downward sloping Demand with movement from 5 to 6]
|
45
|
+
|
46
|
+
```python
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
```python
|
51
|
+
plot_demand1()
|
52
|
+
plt.plot(np.full(6,5), np.arange(0,6,1), 'r--')
|
53
|
+
plt.plot(np.full(7,4), np.arange(0,7,1), 'r--')
|
54
|
+
plt.plot(np.arange(0,6,1), np.full(6,5), 'r--')
|
55
|
+
plt.plot(np.arange(0,5,1), np.full(5,6), 'r--')
|
56
|
+
plt.plot(5,5,'ro')
|
57
|
+
plt.text(5.1,5.1,"(5,5)")
|
58
|
+
plt.plot(4,6,'ro')
|
59
|
+
plt.text(4.1,6.1,"(4,6)")
|
60
|
+
plt.arrow(4.9,5.1,-0.01,0.01,head_width=0.2)
|
61
|
+
plt.arrow(4.6,5.4,-0.01,0.01,head_width=0.2)
|
62
|
+
plt.arrow(4.3,5.7,-0.01,0.01,head_width=0.2)
|
63
|
+
plt.show()
|
64
|
+
```
|
65
|
+
|
66
|
+
Using our definition from above, we can calculate the elasticity.
|
67
|
+
|
68
|
+
$$
|
69
|
+
\begin{align}
|
70
|
+
\frac{\%\Delta Q}{\%\Delta P} &= \frac{\frac{4-5}{5}\times100\%}{\frac{6-5}{5}\times100\%} \\
|
71
|
+
&= \frac{-20\%}{20\%} \\
|
72
|
+
&= 1
|
73
|
+
\end{align}
|
74
|
+
$$
|
75
|
+
|
76
|
+
*Note that we take the absolute value for elasticity.*
|
77
|
+
|
78
|
+
This tells us that in this specific example, the percent change in price is met with an equal percent change in quantity. But what happens if we start at a price of 6 and go down to a price of 5? This movement is just the opposite of what we did above, so is the elasticity also 1? Let's see.
|
79
|
+
|
80
|
+
[Following image is a line of downward sloping Demand with movement from 6 to 5]
|
81
|
+
|
82
|
+
```python
|
83
|
+
plot_demand1()
|
84
|
+
plt.plot(np.full(6,5), np.arange(0,6,1), 'r--')
|
85
|
+
plt.plot(np.full(7,4), np.arange(0,7,1), 'r--')
|
86
|
+
plt.plot(np.arange(0,6,1), np.full(6,5), 'r--')
|
87
|
+
plt.plot(np.arange(0,5,1), np.full(5,6), 'r--')
|
88
|
+
plt.plot(5,5,'ro')
|
89
|
+
plt.text(5.1,5.1,"(5,5)")
|
90
|
+
plt.plot(4,6,'ro')
|
91
|
+
plt.text(4.1,6.1,"(4,6)")
|
92
|
+
plt.arrow(4.7,5.3,0.01,-0.01,head_width=0.2)
|
93
|
+
plt.arrow(4.4,5.6,0.01,-0.01,head_width=0.2)
|
94
|
+
plt.arrow(4.1,5.9,0.01,-0.01,head_width=0.2)
|
95
|
+
plt.show()
|
96
|
+
```
|
97
|
+
|
98
|
+
$$
|
99
|
+
\begin{align}
|
100
|
+
\frac{\%\Delta Q}{\%\Delta P} &= \frac{\frac{5-4}{4}\times100\%}{\frac{5-6}{6}\times100\%} \\
|
101
|
+
&= \frac{25\%}{-16.67\%} \\
|
102
|
+
&\approx 1.5
|
103
|
+
\end{align}
|
104
|
+
$$
|
105
|
+
|
106
|
+
We get a different number! This is of course a property of percentages. In both scenarios we moved price by 1, but we started at different prices, and increasing price by 1 starting from 5 is different percentage-wise from starting at 6 and decreasing by 1.
|
107
|
+
|
108
|
+
If we want to find a unique value for elasticity between the price points of 5 and 6, independent of price movement, we can use something called the **mid-point method**. It looks like this:
|
109
|
+
|
110
|
+
$$\frac{\frac{Q_2 - Q_1}{(Q_2 + Q_1)\times0.5}\times100\%}{\frac{P_2 - P_1}{(P_2 + P_1)\times0.5}\times100\%}$$
|
111
|
+
|
112
|
+
What this does is it essentially finds the elasticity of the point between the two given points.
|
113
|
+
|
114
|
+
It is also useful, however, to think of elasticity as a property of a single point. For example, we might want the price point of 5 in the example above to have a set elasticity of 1 irrespective of what other point we are going to or coming from. For this, we can use the **point-slope formula**.
|
115
|
+
|
116
|
+
$$\frac{\Delta Q}{\Delta P}\frac{P}{Q}$$
|
117
|
+
|
118
|
+
or
|
119
|
+
|
120
|
+
$$\frac{dQ}{dP}\Bigr|_{P,Q}\times\frac{P}{Q}$$
|
121
|
+
|
122
|
+
where the left fraction is the inverse slope at that particular point.
|
123
|
+
|
124
|
+
It turns out that in our example above, we can think of the very first formula for elasticity (percent change in quantity over percent change in price) as the point-slope elasticity of the starting point. To verify this, find the elasticity from a price of 5 to 6, from a price of 5 to 4, and at the price of 5 using the point-slope formula. You should get the same answer all three times. This does not hold in general for all curves, but using the base formula that was first introduced should be fine for our examples and other situations.
|
125
|
+
|
126
|
+
## Other Elasticities
|
127
|
+
|
128
|
+
We can measure elasticity in different contexts as well. Naturally, if we have an elasticity of demand, it follows that we should have an elasticity of supply. Just like consumers make adjustments to their quantity demanded based on changes in the market price, producers respond to market prices as well. Elasticity of supply can be calculated similarly to elasticity of demand.
|
129
|
+
|
130
|
+
Imagine that there are two fast food locations next to each other on campus competing for students' business. Let's call them Railway and Bear Express. Initially, their food is priced such that they get equal amounts of business from the students, and students on average don't prefer one over the other; they just want a good and affordable place to eat. Now, imagine that Bear Express raises the prices of their food, while Railway holds their prices steady. In this scenario, one can imagine that students would begin eating more frequently at Railway as opposed to Bear Express due to it being relatively cheaper out of the two options. Now, if Railway in turn increases their prices, the flow of students would shift back to Bear Express by a certain amount. These two restaurants are said to offer goods that are *substitutes* to each other. That is, a consumer will want one or the other, but usually not both. Furthermore, it would seem that just like the price of a good affects its quantity demanded, for substitutes the price of one good can affect the quantity demanded of the other. This is called *cross-price elasticity of demand* (as opposed to *own-price elasticity* that we just learned earlier). It can be defined as follows:
|
131
|
+
|
132
|
+
$$\frac{\%\Delta Q_{Good A}}{\%\Delta P_{Good B}}$$
|
133
|
+
|
134
|
+
Notice that here there is a notion of positive and negative elasticity. Substitutes will have positive cross-price elasticity, because we expect that an increase in the price of one good will lead to an increase in the quantity demanded of its substitute. But what about when cross-price elasticity is negative? In other words, are there goods where an increase in the price of one leads to a decrease in the quantity demanded of the other? Of course; these are called *complements*. Examples of complementary goods could be cars and gasoline, hot dogs and hot dog buns, or peanut butter and jelly.
|
135
|
+
|
136
|
+
There is one more type of elasticity that we will briefly discuss, and that is *income elasticity of demand*. For most goods, the more disposable income consumers have the more they will demand these goods. Almost all goods follow this pattern, and thus there are countless examples. These goods are called *normal goods*. More interestingly, there are some goods, called *inferior goods*, for which we see quantity demanded decrease as income increases. An example of this might be frozen, microwaveable food. As consumers grow wealthier and their palates grow more discerning, their consumption of frozen food might decrease in place of more cooked meals. To calculate this elasticity we would use the following:
|
137
|
+
|
138
|
+
$$\frac{\%\Delta Q}{\%\Delta Income}$$
|
139
|
+
|
140
|
+
## Elasticity and Revenue
|
141
|
+
|
142
|
+
Let's take a closer look at own-price elasticity of demand. Below we've defined a function using our first definition. Let's see what happens as we increase price. Note that is this percent change in quantity over % change in price. While this is negative quantity usually, we still denote it as a positive number.
|
143
|
+
|
144
|
+
```python
|
145
|
+
def elasticity(P1,P2,Q1,Q2):
|
146
|
+
return ((Q2-Q1)/Q1)/((P2-P1)/P1)
|
147
|
+
|
148
|
+
p = sympy.Symbol("p")
|
149
|
+
demand_equation = 10-p
|
150
|
+
prices = [i for i in range(1,10)]
|
151
|
+
|
152
|
+
print("Price: Elasticity")
|
153
|
+
print()
|
154
|
+
|
155
|
+
for x in prices:
|
156
|
+
print(str(x)+":", abs(elasticity(x,x+1,demand_equation.subs(p,x),demand_equation.subs(p,x+1))))
|
157
|
+
```
|
158
|
+
|
159
|
+
Notice how a price increase will increase revenue until a certain point, at which point revenue decreases again. Intuitively this makes sense. A firm can only increase its prices so much until it starts losing too many of its customers for the increase to be worthwhile. Let's try and find when this happens in general. First let's plot revenue as a function of price.
|
160
|
+
|
161
|
+
[Following image is a u-shaped curve of revenue at different prices]
|
162
|
+
|
163
|
+
```python
|
164
|
+
elast = [abs(elasticity(x,x+1,demand_equation.subs(p,x),demand_equation.subs(p,x+1))) for x in range(1,10)]
|
165
|
+
revenue = [demand_equation.subs(p,x)*x for x in range(1,10)]
|
166
|
+
prices = [x for x in range(1,10)]
|
167
|
+
plt.figure(figsize = [9,6])
|
168
|
+
plt.plot(prices,revenue)
|
169
|
+
plt.xlabel("Price")
|
170
|
+
plt.ylabel("Revenue")
|
171
|
+
plt.title("Revenue vs Price for Given Demand Curve")
|
172
|
+
plt.show()
|
173
|
+
```
|
174
|
+
|
175
|
+
We notice that revenue reaches a maximun at a price of 5. What elasticity does this price point have?
|
176
|
+
|
177
|
+
[Following image is an upward sloping line of elasticty at different prices ]
|
178
|
+
|
179
|
+
```python
|
180
|
+
plt.figure(figsize = [9,6])
|
181
|
+
plt.plot(prices,elast)
|
182
|
+
plt.plot(np.arange(1,6,1), np.full(5,1), 'r--')
|
183
|
+
plt.plot(np.full(2,5), np.arange(0,2,1), 'r--')
|
184
|
+
plt.plot(5,1,'ro')
|
185
|
+
plt.text(5,1.5,"(5,1)")
|
186
|
+
plt.xlabel("Price")
|
187
|
+
plt.ylabel("Elasticity")
|
188
|
+
plt.title("Elasticity vs Price for Given Demand Curve")
|
189
|
+
plt.show()
|
190
|
+
```
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
Revenue is maximized when elasticity is 1! To further reinforce this, let's plot revenue vs elasticity.
|
195
|
+
|
196
|
+
[Following image is a line revenue vs elasticity]
|
197
|
+
|
198
|
+
```python
|
199
|
+
plt.figure(figsize = [9,6])
|
200
|
+
plt.plot(elast,revenue)
|
201
|
+
plt.plot(np.full(17,1), np.arange(9,26,1), 'r--')
|
202
|
+
plt.plot(1,25,'ro')
|
203
|
+
plt.text(1.3,25,"(1,25)")
|
204
|
+
plt.xlabel("Elasticity")
|
205
|
+
plt.ylabel("Revenue")
|
206
|
+
plt.title("Revenue vs Elasticity for Given Demand Curve")
|
207
|
+
plt.show()
|
208
|
+
```
|
209
|
+
|
210
|
+
Before we continue to the final section let's make sure we have a solid intuitive grasp of the significance of our findings. Near low price points, a firm deciding how to raise prices will tend to face inelastic demand. You can imagine that if there were some market where let's say laptops cost only a few dollars, doubling the price of laptops would proabalby not decrease demand for laptops at all, since the price of laptops is still very low. Here, demand is inelastic, or in other words, it is less than 1, because a unit change in price is not met by the same change in quantity. Alternatively, if laptops were to cost as much as a house, and then the price for laptops doubled, the few people who were still in the market for laptops would probably be very discouraged from continuing to buy laptops. It wouldn't be unreasonable to assume that nearly all of the demand would be lost. Here, demand is elastic, or greater than 1, because a unit change in price is met by an even greater change in quantity. The point of the above exercise is to illustrate that there is some middle ground between these two extremes, where a firm with a good pulse on market demand should be pricing its products.
|
211
|
+
|
212
|
+
It's important to remember that while we have so far been discussing elasticity from a demand perspective, supply also has the property of elasticity.
|
213
|
+
|
214
|
+
## Relative Elasticity
|
215
|
+
|
216
|
+
Suppose that you are hiking in the mountains when a venomous snake bites you. Fortunately there is a hospital nearby. Unfortunately, it seems that their antivenom is very expensive. If the antivenom costs $1000, would you pay? What if the price doubled to $2000? What is your max willingness to pay for this antivenom? In the moment, you would likely be willing to pay anything and everything for the antivenom, as any money you refuse to pay won't be of much use to you if you're dead.
|
217
|
+
|
218
|
+
Contrast this with your demand for pizza. Imagine a pizza chain close to where you live doubles their prices from \\$3 a slice to \\$6 a slice. Most people would be disuaded from buying pizza by this price increase, assuming that there are many alternatives to pizza close to where you live. Since pizza is not a very essential good, and there are plenty of other places to eat close to where you live, most wouldn't see the point in getting pizza anymore.
|
219
|
+
|
220
|
+
We can easily see that any change in price is unlikely to change the demand for antivenom in our example, and thus we say the demand for antivenom is inelastic. But this is a different kind of elasticity. We are not measuring the marginal change in quantity demanded at a given price point or interval, we are saying that in general, on the demand curve for antivenom the elasticity at any price is more inelastic than some other typical demand curve. Similarly, in our example we can say that the demand for pizza is relatively elastic, as the pizza chain cannot change their prices too much without significantly altering the quantity demanded for their pizza. The keyword in this concept is *relative*. It doesn't really make much sense to say that the demand for antivenom is inelastic without there being at least one other kind of demand that is comparatively elastic.
|
221
|
+
|
222
|
+
More specifically, our example of antivenom likely exhibits *perfect inelasticity*, meaning that demand for antivenom is the same at absolutely any price; the hospital can charge any price it wants for antivenom. Contrast this with our nearby pizza chain. If we make some further assumptions and say that the market for food around your house or apartment is perfectly competitive, such that any marginal increase in price of one food chain will cause them to lose all of their customers to other food chains, then the demand for the specific food of that chain is *perfectly elastic*. The chain cannot increase its prices or else it will lose all demand. These two extreme scenarios are depicted below.
|
223
|
+
|
224
|
+
|
225
|
+
[Following image is a vertical line]
|
226
|
+
[Following image is a horizontal line]
|
227
|
+
|
228
|
+
```python
|
229
|
+
plt.figure(figsize = [9,6])
|
230
|
+
plt.plot(np.full(20,1), np.arange(0,20,1))
|
231
|
+
plt.xlabel("Quantity Demanded")
|
232
|
+
plt.ylabel("Price")
|
233
|
+
plt.title("Perfectly Inelastic Demand Curve")
|
234
|
+
plt.xticks([], [])
|
235
|
+
plt.yticks([], [])
|
236
|
+
plt.show()
|
237
|
+
```
|
238
|
+
|
239
|
+
```python
|
240
|
+
plt.figure(figsize = [9,6])
|
241
|
+
plt.plot(np.arange(0,20,1), np.full(20,1))
|
242
|
+
plt.xlabel("Quantity Demanded")
|
243
|
+
plt.ylabel("Price")
|
244
|
+
plt.title("Perfectly Elastic Demand Curve")
|
245
|
+
plt.xticks([], [])
|
246
|
+
plt.yticks([], [])
|
247
|
+
plt.show()
|
248
|
+
```
|
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
title: index
|
3
|
+
type: textbook
|
4
|
+
source_path: content/01-demand/index.md
|
5
|
+
chapter: 1
|
6
|
+
---
|
7
|
+
|
8
|
+
# Demand and Elasticities
|
9
|
+
|
10
|
+
**Student Learning Outcomes:**
|
11
|
+
|
12
|
+
* Understand basic properties of the demand curve, including movements along and shifts in the curve
|
13
|
+
* Examine different types of demand curves and their implications: non-log, semi-log, and log-log
|
14
|
+
* Use `np.polyfit` to create a demand curve from provided data
|
15
|
+
* Understand different types of elasticities and calculate price elasticities for demand
|