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.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/F24LS_md/ Lecture 4 - Public.md +347 -0
  3. data/F24LS_md/Lecture 1 - Introduction and Overview.md +327 -0
  4. data/F24LS_md/Lecture 10 - Development_.md +631 -0
  5. data/F24LS_md/Lecture 11 - Econometrics.md +345 -0
  6. data/F24LS_md/Lecture 12 - Finance.md +692 -0
  7. data/F24LS_md/Lecture 13 - Environmental Economics.md +299 -0
  8. data/F24LS_md/Lecture 15 - Conclusion.md +272 -0
  9. data/F24LS_md/Lecture 2 - Demand.md +349 -0
  10. data/F24LS_md/Lecture 3 - Supply.md +329 -0
  11. data/F24LS_md/Lecture 5 - Production C-D.md +291 -0
  12. data/F24LS_md/Lecture 6 - Utility and Latex.md +440 -0
  13. data/F24LS_md/Lecture 7 - Inequality.md +607 -0
  14. data/F24LS_md/Lecture 8 - Macroeconomics.md +704 -0
  15. data/F24LS_md/Lecture 8 - Macro.md +700 -0
  16. data/F24LS_md/Lecture 9 - Game Theory_.md +436 -0
  17. data/F24LS_md/summary.yaml +105 -0
  18. data/F24Lec_MD/LecNB_summary.yaml +206 -0
  19. data/F24Lec_MD/lec01/lec01.md +267 -0
  20. data/F24Lec_MD/lec02/Avocados_demand.md +425 -0
  21. data/F24Lec_MD/lec02/Demand_Steps_24.md +126 -0
  22. data/F24Lec_MD/lec02/PriceElasticity.md +83 -0
  23. data/F24Lec_MD/lec02/ScannerData_Beer.md +171 -0
  24. data/F24Lec_MD/lec02/demand-curve-Fa24.md +213 -0
  25. data/F24Lec_MD/lec03/3.0-CubicCostCurve.md +239 -0
  26. data/F24Lec_MD/lec03/3.1-Supply.md +274 -0
  27. data/F24Lec_MD/lec03/3.2-sympy.md +332 -0
  28. data/F24Lec_MD/lec03/3.3a-california-energy.md +120 -0
  29. data/F24Lec_MD/lec03/3.3b-a-really-hot-tuesday.md +121 -0
  30. data/F24Lec_MD/lec04/lec04-CSfromSurvey-closed.md +335 -0
  31. data/F24Lec_MD/lec04/lec04-CSfromSurvey.md +331 -0
  32. data/F24Lec_MD/lec04/lec04-Supply-Demand-closed.md +519 -0
  33. data/F24Lec_MD/lec04/lec04-Supply-Demand.md +514 -0
  34. data/F24Lec_MD/lec04/lec04-four-plot-24.md +34 -0
  35. data/F24Lec_MD/lec04/lec04-four-plot.md +34 -0
  36. data/F24Lec_MD/lec05/Lec5-Cobb-Douglas.md +131 -0
  37. data/F24Lec_MD/lec05/Lec5-CobbD-AER1928.md +283 -0
  38. data/F24Lec_MD/lec06/6.1-Sympy-Differentiation.md +253 -0
  39. data/F24Lec_MD/lec06/6.2-3D-utility.md +287 -0
  40. data/F24Lec_MD/lec06/6.3-QuantEcon-Optimization.md +399 -0
  41. data/F24Lec_MD/lec06/6.4-latex.md +138 -0
  42. data/F24Lec_MD/lec06/6.5-Edgeworth.md +269 -0
  43. data/F24Lec_MD/lec07/7.1-inequality.md +283 -0
  44. data/F24Lec_MD/lec07/7.2-historical-inequality.md +237 -0
  45. data/F24Lec_MD/lec08/macro-fred-api.md +313 -0
  46. data/F24Lec_MD/lec09/lecNB-prisoners-dilemma.md +88 -0
  47. data/F24Lec_MD/lec10/Lec10.2-waterguard.md +401 -0
  48. data/F24Lec_MD/lec10/lec10.1-mapping.md +199 -0
  49. data/F24Lec_MD/lec11/11.1-slr.md +305 -0
  50. data/F24Lec_MD/lec11/11.2-mlr.md +171 -0
  51. data/F24Lec_MD/lec12/Lec12-4-PersonalFinance.md +590 -0
  52. data/F24Lec_MD/lec12/lec12-1_Interest_Payments.md +267 -0
  53. data/F24Lec_MD/lec12/lec12-2-stocks-options.md +235 -0
  54. data/F24Lec_MD/lec13/Co2_ClimateChange.md +139 -0
  55. data/F24Lec_MD/lec13/ConstructingMAC.md +213 -0
  56. data/F24Lec_MD/lec13/EmissionsTracker.md +170 -0
  57. data/F24Lec_MD/lec13/KuznetsHypothesis.md +219 -0
  58. data/F24Lec_MD/lec13/RoslingPlots.md +217 -0
  59. data/F24Lec_MD/lec15/vibecession.md +485 -0
  60. data/F24Textbook_MD/00-intro/index.md +292 -0
  61. data/F24Textbook_MD/01-demand/01-demand.md +152 -0
  62. data/F24Textbook_MD/01-demand/02-example.md +131 -0
  63. data/F24Textbook_MD/01-demand/03-log-log.md +284 -0
  64. data/F24Textbook_MD/01-demand/04-elasticity.md +248 -0
  65. data/F24Textbook_MD/01-demand/index.md +15 -0
  66. data/F24Textbook_MD/02-supply/01-supply.md +203 -0
  67. data/F24Textbook_MD/02-supply/02-eep147-example.md +86 -0
  68. data/F24Textbook_MD/02-supply/03-sympy.md +138 -0
  69. data/F24Textbook_MD/02-supply/04-market-equilibria.md +204 -0
  70. data/F24Textbook_MD/02-supply/index.md +16 -0
  71. data/F24Textbook_MD/03-public/govt-intervention.md +73 -0
  72. data/F24Textbook_MD/03-public/index.md +10 -0
  73. data/F24Textbook_MD/03-public/surplus.md +351 -0
  74. data/F24Textbook_MD/03-public/taxes-subsidies.md +282 -0
  75. data/F24Textbook_MD/04-production/index.md +15 -0
  76. data/F24Textbook_MD/04-production/production.md +178 -0
  77. data/F24Textbook_MD/04-production/shifts.md +296 -0
  78. data/F24Textbook_MD/05-utility/budget-constraints.md +166 -0
  79. data/F24Textbook_MD/05-utility/index.md +15 -0
  80. data/F24Textbook_MD/05-utility/utility.md +136 -0
  81. data/F24Textbook_MD/06-inequality/historical-inequality.md +253 -0
  82. data/F24Textbook_MD/06-inequality/index.md +15 -0
  83. data/F24Textbook_MD/06-inequality/inequality.md +226 -0
  84. data/F24Textbook_MD/07-game-theory/bertrand.md +257 -0
  85. data/F24Textbook_MD/07-game-theory/cournot.md +333 -0
  86. data/F24Textbook_MD/07-game-theory/equilibria-oligopolies.md +96 -0
  87. data/F24Textbook_MD/07-game-theory/expected-utility.md +61 -0
  88. data/F24Textbook_MD/07-game-theory/index.md +19 -0
  89. data/F24Textbook_MD/07-game-theory/python-classes.md +340 -0
  90. data/F24Textbook_MD/08-development/index.md +35 -0
  91. data/F24Textbook_MD/09-macro/CentralBanks.md +101 -0
  92. data/F24Textbook_MD/09-macro/Indicators.md +77 -0
  93. data/F24Textbook_MD/09-macro/fiscal_policy.md +36 -0
  94. data/F24Textbook_MD/09-macro/index.md +14 -0
  95. data/F24Textbook_MD/09-macro/is_curve.md +76 -0
  96. data/F24Textbook_MD/09-macro/phillips_curve.md +70 -0
  97. data/F24Textbook_MD/10-finance/index.md +10 -0
  98. data/F24Textbook_MD/10-finance/options.md +178 -0
  99. data/F24Textbook_MD/10-finance/value-interest.md +60 -0
  100. data/F24Textbook_MD/11-econometrics/index.md +16 -0
  101. data/F24Textbook_MD/11-econometrics/multivariable.md +218 -0
  102. data/F24Textbook_MD/11-econometrics/reading-econ-papers.md +25 -0
  103. data/F24Textbook_MD/11-econometrics/single-variable.md +483 -0
  104. data/F24Textbook_MD/11-econometrics/statsmodels.md +58 -0
  105. data/F24Textbook_MD/12-environmental/KuznetsHypothesis-Copy1.md +187 -0
  106. data/F24Textbook_MD/12-environmental/KuznetsHypothesis.md +187 -0
  107. data/F24Textbook_MD/12-environmental/MAC.md +254 -0
  108. data/F24Textbook_MD/12-environmental/index.md +36 -0
  109. data/F24Textbook_MD/LICENSE.md +11 -0
  110. data/F24Textbook_MD/intro.md +26 -0
  111. data/F24Textbook_MD/references.md +25 -0
  112. data/F24Textbook_MD/summary.yaml +414 -0
  113. metadata +155 -0
@@ -0,0 +1,257 @@
1
+ ---
2
+ title: bertrand
3
+ type: textbook
4
+ source_path: content/07-game-theory/bertrand.ipynb
5
+ chapter: 7
6
+ ---
7
+
8
+ ```python
9
+ from datascience import *
10
+ import sympy
11
+ solve = lambda x,y: sympy.solve(x-y)[0] if len(sympy.solve(x-y))==1 else "Not Single Solution"
12
+ import matplotlib.pyplot as plt
13
+ import numpy as np
14
+ import pandas as pd
15
+ import datetime as dt
16
+ import warnings
17
+ warnings.simplefilter("ignore")
18
+ %matplotlib inline
19
+ ```
20
+
21
+ # Bertrand Competition
22
+
23
+ Another model we consider is **Bertrand competition**, named for Joseph Louis Francois Bertrand, that is similar to Cournot competition but that firms compete using _prices_ rather than quantity. Under the assumptions of this model, consumers want to buy everything at the lowest price, and if the price is the same then demand is evenly split between those producers. One fundamental assumption is that all firms have the same unit cost of production, which means that as long as the price the firm sets is above the unit cost, it is willing to supply any amount that is demanded.
24
+
25
+ An example of a Bertrand oligopoly comes form the soft drink industry: Coke and Pepsi (which form a **duopoly**, a market with only two participants). Both firms compete by changing their prices based on a function that takes into account the price charged by their competitor. This model predicts that even this small competition will result in prices being reduced to the marginal cost level, the same outcome as perfect competition.
26
+
27
+ ## Bertrand Equilibrium
28
+
29
+ To find the Bertrand equilibrium, let $c$ be the (constant) marginal cost, $p_1$ be firm 1's price level, $p_2$ be firm 2's price level, and $p_m$ be the monopoly price level. Firm 1's price depends on what it believes firm 2 will set its prices to be. Because consumers always buy at the lowest price and the firm will fulfill any level of demand, pricing just below firm 2 will obtain full market demand for firm 1. Why might this not be a good idea? If firm 2 is pricing below the level of marginal cost, then firm 1 will incur losses because they would need to sell at a price lower than the cost of production.
30
+
31
+ Let $p'_1(p_2)$ be firm 1's optimal price based on price $p_2$ set by firm 2. The graph below shows $p'_1(p_2)$. Note that when $p_2 < c$, $p'_1$ is equal to $c$, that $p'_1$ rises linearly along but _just below_ the line $p_1 = p_2$ with $p_2$ until $p_2$ reaches $p_m$ (the monopoly price level), and that it then levels off at $p_m$. In this way, firm 1's price stays below firm 2's price when it is not operating at a loss and does not exceed $p_m$ (because $p_m$ is the profit-maximizing amount for a monopoly and producing more actually results in less profit). This piecewise function has the formula
32
+
33
+ $$
34
+ p'_1(p_2) = \begin{cases}
35
+ c & \text{if } p_2 < c + h \\
36
+ p_2 - h & \text{if } c + h \le p_2 < p_m + h \\
37
+ p_m & \text{otherwise}
38
+ \end{cases}
39
+ $$
40
+
41
+ where $h$ is a small positive value and indicates the vertical distance between $p'_1$ and the line $p_1 = p_2$. We can think of $h$ as the amount by which firm 1 will undercut firm 2: as long as firm 1 will be operating at a profit and not exceeding $p_m$, they will sell at $h$ dollars below $p_2$.
42
+
43
+ [Following image has line graphs for price response functions between two firms]
44
+
45
+ ```python
46
+ p_m = 18
47
+ c = 6
48
+ dist = 0.4 # the distance from p_1 = p_2 to p'_1(p_2)
49
+
50
+ xs1 = np.linspace(0, c, 100)
51
+ ys1 = c * np.ones_like(xs1)
52
+
53
+ xs2 = np.linspace(c + dist, p_m + dist, 100)
54
+ ys2 = xs2.copy() - dist
55
+
56
+ xs3 = np.linspace(p_m + dist, 25, 100)
57
+ ys3 = p_m * np.ones_like(xs3)
58
+
59
+ xs = np.append(np.append(xs1, xs2), xs3)
60
+ ys = np.append(np.append(ys1, ys2), ys3)
61
+
62
+ plt.figure(figsize=[8,8])
63
+
64
+ plt.plot(xs, ys, label=r"$p'_1(p_2)$")
65
+
66
+ y_equals_x_xs = np.linspace(0, 25, 100)
67
+ y_equals_x_ys = y_equals_x_xs.copy()
68
+
69
+ plt.plot(y_equals_x_xs, y_equals_x_ys, label=r"$p_1 = p_2$")
70
+
71
+ plt.vlines(c, 0, c, linestyle="dashed")
72
+ plt.vlines(p_m, 0, p_m, linestyle="dashed")
73
+ plt.hlines(p_m, 0, p_m, linestyle="dashed")
74
+
75
+ plt.text(2, 6.5, r"$p'_1(p_2)$", size=16)
76
+ plt.text(17, 20, r"$p_1 = p_2$", size=16)
77
+
78
+ plt.xlim([0,22])
79
+ plt.ylim([0,22])
80
+ plt.xlabel(r"$p_2$", size=16)
81
+ plt.ylabel(r"$p_1$", size=16)
82
+ plt.xticks([c, p_m], [r"$c$", r"$p_m$"], size=14)
83
+ plt.yticks([c, p_m], [r"$c$", r"$p_m$"], size=14)
84
+ plt.legend();
85
+ ```
86
+
87
+ Because firm 2 has the same marginal cost $c$ as firm 1, its reaction function $p'_2(p_1)$ is symmetrical to firm 1's about the line $p_1 = p_2$:
88
+
89
+ [Following image has line graphs for price response functions between two firms]
90
+
91
+ ```python
92
+ p_m = 18
93
+ c = 6
94
+ dist = 0.4 # the distance from p_1 = p_2 to p'_1(p_2)
95
+
96
+ xs1 = np.linspace(0, c, 100)
97
+ ys1 = c * np.ones_like(xs1)
98
+
99
+ xs2 = np.linspace(c + dist, p_m + dist, 100)
100
+ ys2 = xs2.copy() - dist
101
+
102
+ xs3 = np.linspace(p_m + dist, 25, 100)
103
+ ys3 = p_m * np.ones_like(xs3)
104
+
105
+ xs_1 = np.append(np.append(xs1, xs2), xs3)
106
+ ys_1 = np.append(np.append(ys1, ys2), ys3)
107
+
108
+ ys1 = np.linspace(0, c, 100)
109
+ xs1 = c * np.ones_like(xs1)
110
+
111
+ ys2 = np.linspace(c + dist, p_m + dist, 100)
112
+ xs2 = xs2.copy() - dist
113
+
114
+ ys3 = np.linspace(p_m + dist, 25, 100)
115
+ xs3 = p_m * np.ones_like(xs3)
116
+
117
+ xs_2 = np.append(np.append(xs1, xs2), xs3)
118
+ ys_2 = np.append(np.append(ys1, ys2), ys3)
119
+
120
+ plt.figure(figsize=[8,8])
121
+
122
+ plt.plot(xs_1, ys_1, label=r"$p'_1(p_2)$")
123
+ plt.plot(xs_2, ys_2, label=r"$p'_2(p_1)$")
124
+
125
+ y_equals_x_xs = np.linspace(0, 25, 100)
126
+ y_equals_x_ys = y_equals_x_xs.copy()
127
+
128
+ plt.plot(y_equals_x_xs, y_equals_x_ys, label=r"$p_1 = p_2$")
129
+
130
+ plt.vlines(p_m, 0, p_m, linestyle="dashed")
131
+ plt.hlines(p_m, 0, p_m, linestyle="dashed")
132
+
133
+ plt.text(2, 6.5, r"$p'_1(p_2)$", size=16)
134
+ plt.text(6.5, 2, r"$p'_2(p_1)$", size=16)
135
+ plt.text(18.2, 21, r"$p_1 = p_2$", size=16)
136
+
137
+ plt.xlim([0,22])
138
+ plt.ylim([0,22])
139
+ plt.xlabel(r"$p_2$", size=16)
140
+ plt.ylabel(r"$p_1$", size=16)
141
+ plt.xticks([c, p_m], [r"$c$", r"$p_m$"], size=14)
142
+ plt.yticks([c, p_m], [r"$c$", r"$p_m$"], size=14)
143
+ plt.legend();
144
+ ```
145
+
146
+ These two strategies form a Nash equilibrium because neither firm can increase profits by changing their own strategy unilaterally. The equilibrium occurs where $p_1 = p'_1(p_2)$ and $p_2 = p'_2(p_1)$, at the intersection of the two reaction curves. Notably, this means that the Bertrand equilibrium occurs when both firms are producing _at marginal cost_.
147
+
148
+ This makes intuitive sense: say that the two firms both set equal prices at a price above $c$ where they split demand equally. Then both firms have incentive to reduce their price slightly and take the other half of the market share from their competitor. Thus, both firms are tempted to lower prices as much as possible, but lowering below the level of marginal cost makes no sense because then they're operating at a loss. Thus, both firms sell at the price level $c$.
149
+
150
+ ## Implications
151
+
152
+ The Bertrand model implies that even a duopoly in a market is enough to push prices down to the level of perfect competition. It does, however, rely on some serious assumptions. For example, there are many reasons why consumers might not buy the lowest-priced item (e.g. non-price competition, search costs). When these factors are included in the Bertrand model, the same result is no longer reached. It also ignores the fact that firms may not be able to supply the entire market demand; including these capacity constraints in the model can result in the system having no Nash equilibrium. Lastly, the Bertrand model demonstrates big incentives to cooperate and raise prices to the monopoly level; however, this state is not a Nash equilibrium, and in fact, the only Nash equilibrium of this model is the non-cooperative one with prices at marginal cost.
153
+
154
+ ## Applying Bertrand
155
+
156
+ Now that we have derived the Bertrand equilibrium, let's apply it to a problem. Consider the Coke-Pepsi duopoly we mentioned above. Suppose that the only product in the soft-drink market is the 12-oz. can, that the market demand for cans is given by $P = -0.05 Q + 5.05$, and that the marginal cost for Coke and Pepsi is constant at $c = 0.25$. To find the equilibrium price for Coke based in it's belief that Pepsi will sell at $p_2 = 1$, we need to start by finding the monopoly price level $p_m$; recall from Cournot that this occurs when the marginal revenue curve of the market demand intersects the marginal cost. The marginal revenue is $r(q) = -0.1 q + 5.05$:
157
+
158
+ [Following image has line graphs for Demand, Marginal Revenue and Cost]
159
+
160
+ ```python
161
+ P_fn = lambda x: -0.05 * x + 5.05
162
+ r_fn = lambda x: 2 * P_fn(x) - P_fn(0)
163
+ Qs = np.linspace(-10, 1000, 1000)
164
+ Ps = P_fn(Qs)
165
+ rs = r_fn(Qs)
166
+
167
+ plt.figure(figsize=[7,7])
168
+ plt.plot(Qs, Ps, label=r"$P(Q)$")
169
+ plt.plot(Qs, rs, label=r"$r(Q)$")
170
+ plt.hlines(.25, -10, 150, color="r", label=r"$c$")
171
+ plt.xlim([0, 110])
172
+ plt.ylim([0, 5.5])
173
+ plt.xlabel(r"Quantity $Q$", size=16)
174
+ plt.ylabel(r"Price $P$", size=16)
175
+ plt.legend();
176
+ ```
177
+
178
+ Using SymPy, we can find the quantity $q$ at which $r(q) = c$. This value, denoted $q_m$, is the monopoly quantity.
179
+
180
+ ```python
181
+ c = 0.25
182
+ q = sympy.Symbol("q")
183
+ r = -.1 * q + 5.05
184
+
185
+ q_m = solve(r, c)
186
+ q_m
187
+ ```
188
+
189
+ The monopoly price $p_m$ is the price from the market demand curve that this level of output:
190
+
191
+ ```python
192
+ Q = sympy.Symbol("Q")
193
+ P = -.05 * Q + 5.05
194
+
195
+ p_m = P.subs(Q, q_m)
196
+ p_m
197
+ ```
198
+
199
+ Now that we have found $p_m$, we can use this to construct Coke's reaction function $p'_1(p_2)$ to Pepsi's choice of price. Assuming Coke selects $h=0.1$ (that is, Coke will sell at \$0.10 below Pepsi as long as they operate at a profit), the formula for $p'_1$ is
200
+
201
+ $$
202
+ p'_1(p_2) = \begin{cases}
203
+ 0.25 & \text{if } p_2 < 0.25 + 0.1 \\
204
+ p_2 - 0.1 & \text{if } 0.25 + 0.1 \le p_2 < 2.65 + 0.1 \\
205
+ 2.65 & \text{otherwise}
206
+ \end{cases}
207
+ $$
208
+
209
+ [Following image has piecewise line graphs for how one firm reacts to anothers price setting]
210
+
211
+ ```python
212
+ c = 0.25
213
+ def p1_fn(p_2, h):
214
+ if p_2 < c + h:
215
+ return c
216
+ elif c + h <= p_2 < p_m + h:
217
+ return p_2 - h
218
+ else:
219
+ return p_m
220
+
221
+ p1_fn = np.vectorize(p1_fn)
222
+
223
+ xs = np.linspace(-1, 4, 1000)
224
+ ys = xs.copy()
225
+ p1s = p1_fn(xs, .1)
226
+
227
+ plt.figure(figsize=[7,7])
228
+ plt.plot(xs, p1s, label=r"$p'_1(p_2)$")
229
+ plt.plot(xs, ys, label=r"$p_1 = p_2$")
230
+ plt.hlines(c, 0, 5, color="r", label=r"$c$")
231
+ plt.vlines(c, 0, 5, color="r")
232
+ plt.hlines(p_m, 0, 5, color="g", label=r"$p_m$")
233
+ plt.vlines(p_m, 0, 5, color="g")
234
+ plt.xlim([0,3])
235
+ plt.ylim([0,3])
236
+ plt.xlabel(r"$p_2$", size=16)
237
+ plt.ylabel(r"$p_1$", size=16)
238
+ plt.legend();
239
+ ```
240
+
241
+ Finally, to find Coke's selling price, we find $p'_1(1)$, since Coke believes Pepsi will sell at \$1.
242
+
243
+ ```python
244
+ c = 0.25
245
+ h = 0.1
246
+ p_2 = sympy.Symbol("p_2")
247
+ p_1_prime = sympy.Piecewise(
248
+ (c, p_2 < c + h),
249
+ (p_2 - h, p_2 < p_m + h),
250
+ (p_m, True)
251
+ )
252
+
253
+ p_1_prime.subs(p_2, 1)
254
+ ```
255
+
256
+ Thus, if Coke believes that Pepsi will sell cans at \$1, it should sell at \$0.90. This should make intuitive sense: we showed that $p'_1(p_2)$ was below the line $p_1=p_2$ by a vertical distance of $h=0.1$, so it makes sense that Coke would sell at \$0.10 below Pepsi. If Coke had believed that Pepsi was going to sell at, say, \$2.70, then it would have been better for them to sell at the monopoly price level $p_m = 2.65$. If Pepsi was selling below margin cost, at \$0.20 maybe, then Coke's best bet would have been to sell at $c = 0.25$, although they would have sold 0 units of output because consumers buy from the lowest-priced vendor.
257
+
@@ -0,0 +1,333 @@
1
+ ---
2
+ title: cournot
3
+ type: textbook
4
+ source_path: content/07-game-theory/cournot.ipynb
5
+ chapter: 7
6
+ ---
7
+
8
+ ```python
9
+ from datascience import *
10
+ import sympy
11
+ solve = lambda x,y: sympy.solve(x-y)[0] if len(sympy.solve(x-y))==1 else "Not Single Solution"
12
+ import matplotlib.pyplot as plt
13
+ import numpy as np
14
+ import pandas as pd
15
+ import datetime as dt
16
+ import warnings
17
+ warnings.simplefilter("ignore")
18
+ %matplotlib inline
19
+ ```
20
+
21
+ # Cournot Competition
22
+
23
+ One model of understanding oligopolies comes in Cournot competition, named for Aontoine Cournot who first proposed it. Cournot competition is a model describing a market in which firms compete by changing their output. In Cournot competition, there are a fixed number of firms in a market that produce the same product. Firms do not collude but have market power (i.e. each firm’s output decisions affect the price of the good). Each firm knows the number of firms in the market and has its own cost function that it uses to determine its level of output.
24
+
25
+ OPEC is a good example of a Cournot oligopoly: its participants affect prices by changing their output. OPEC also demonstrates a flaw in the reasoning behind Cournot competition: the equilibrium state of a Cournot oligopoly suggests that collusion by market participants is the rational policy, but in reality game theory shows us this cannot be the “true” equilibrium because cartel members undercut one another in a bid for market share.
26
+
27
+ ## Cournot Profit Functions
28
+ For the purposes of this class, we will be analyzing the case of Cournot duopoly (i.e. when there are only 2 firms in a market). Throughout the course we will make these assumptions about the market: both firms have to follow a joint linear price function (meaning the market price is determined only by the quantity produced of both firms) and both firms have an equal constant marginal cost curve. To put this formally, $P=P(Q)=P(q_1+q_2)=m(q_1+q_2)+b$ (where $m$ and $b$ are parameters for the slope and intercept of the price functions). Costs for firm 1 can be represented as $C_1=cq_1$ and costs for firm 2 can be represented as $C_2=cq_2$. To find the equilibrium of this game we will need to set up the profit functions for both firms. Starting with firm 1 we can put the last two equations together to get:
29
+
30
+ $$
31
+ \begin{equation} \label{eq1}
32
+ \begin{split}
33
+ \pi_1 & =Pq_1-C_1 \\
34
+ & =(m(q_1+q_2)+b)q_1-cq_1 \\
35
+ & =mq_1^2+mq_1q_2+bq_1 -cq_1
36
+ \end{split}
37
+ \end{equation}
38
+ $$
39
+
40
+ Firm 2 follows a similar profit function:
41
+
42
+ $$
43
+ \begin{align*}
44
+ \pi_2 & =Pq_2-C_2\\
45
+ & =mq_2^2+mq_1q_2+bq_2 -cq_2
46
+ \end{align*}
47
+ $$
48
+
49
+ ## Best Response Functions
50
+ Similar to how in the utility unit individuals seek to maximize utility, in Cournot firms seek to maximize profit. So we will be using the same mathematical tool we learned last week (partial derivatives) to derive the equilibrium of this game. Specifically we will be solving for a $q_1^{*}$ that satisfies $ \underset{q_1}{\mathrm{argmax}}\ \pi_1 \ \textrm{or} \ \underset{q_1}{\mathrm{argmax}}\ (mq_1^2+mq_1q_2+bq_1 -cq_1)$. In english, this means we will be finding the value (or values) of $q_1$ that maximize the profit function for player 1. We will also do the same thing for player 2. Starting with player 1, let's take the partial derivative with respect to $q_1$ and set that equation equal to 0. We can then solve that equation to obtain $q_1^{*}$.
51
+
52
+ $$
53
+ \begin{align*}
54
+ \frac{\partial{\pi_1}}{\partial{q_1}}= 2mq_1 + mq_2+b - c \\
55
+ 2mq_1 + mq_2+b - c = 0\\
56
+ q_1^{*}= \frac{c-mq_2-b}{2m}
57
+ \end{align*}
58
+ \
59
+ $$
60
+ This equation for $q_1$ is called a best response function because it tells us player 1's optimal output for every possible output player 2 can choose. Now, in order to get the equilibrium, we need to find player 2's best response function. We will use the same approach to do this (i.e., we will solve for a $q_2^{*}$ that satisfies $\underset{q_2}{\mathrm{argmax}}\ \pi_2 \ \textrm{or} \ \underset{q_2}{\mathrm{argmax}}\ mq_2^2+mq_1q_2+bq_2 -cq_2$ ).
61
+
62
+ $$
63
+ \begin{align*}
64
+ \frac{\partial{\pi_2}}{\partial{q_2}}= 2mq_2 + mq_1+b - c \\
65
+ 2mq_2 + mq_1+b - c = 0\\
66
+ q_2^{*} = \frac{c-mq_1-b}{2m}
67
+ \end{align*}
68
+ $$
69
+
70
+ If $q_2$ was fixed at a certain price and we were to graph firm 1's best response equation, it would look something like this:
71
+
72
+ [Following image has line graphs for downward sloping market demand and marginal revenue functions]
73
+
74
+ ```python
75
+ m, b = -0.5, 9
76
+ q_2 = 4
77
+ c = 3
78
+ D_fn = lambda x: m * x + b + 2
79
+ P = lambda x: D_fn(x + q_2)
80
+ r_1_fn = lambda x: 2 * m * x + m * q_2 + b + 2
81
+ r_1_inv = lambda x: (x - m * q_2 - b - 2) / 2 / m
82
+
83
+ q_1 = np.linspace(0, 25, 1000)
84
+ D = D_fn(q_1)
85
+ d_1 = P(q_1)
86
+ r_1 = r_1_fn(q_1)
87
+
88
+ q_1_prime = r_1_inv(c)
89
+
90
+ plt.figure(figsize=[8,8])
91
+
92
+ # demand
93
+ plt.plot(q_1, D, label=r"Market Demand, $D$")
94
+ plt.text(5, b, r"$D$", size=16)
95
+
96
+
97
+
98
+ # marginal revenue
99
+ plt.plot(q_1, r_1, label=r"Marginal Revenue of Firm 1, $r_1$")
100
+ plt.text(5, b - 4.5, r"$r_1$", size=16)
101
+
102
+ # marginal cost
103
+ plt.plot(q_1, c * np.ones_like(q_1), label=r"Marginal Cost, $c$")
104
+ plt.text(20, 3.2, r"$c$", size=16)
105
+
106
+ # optimum quantity
107
+ plt.vlines(q_1_prime, 0, 3, linestyles="dashed")
108
+
109
+
110
+
111
+ plt.xlim([0,22])
112
+ plt.ylim([0,12])
113
+ plt.xticks([r_1_inv(c)], [r"$q_1^*$"], size=14)
114
+ plt.yticks([P(0)], [r"$P(q_2)$"], size=14)
115
+ plt.xlabel("Quantity", size=16)
116
+ plt.ylabel("Price", size=16)
117
+ plt.legend();
118
+ ```
119
+
120
+ This may make intuitive sense as if you recall from the demand and supply chapters, firms seek to produce where marginal revenue equals marginal cost. The partial derivative we took before did that automatically. We can verify this with the following calculation:
121
+
122
+ $$
123
+ \begin{align*}
124
+ MR=MC \\
125
+ MR-MC=0 \\
126
+ \frac{\partial}{\partial{q_1}} (\textrm{Revenue - Cost}) = 0 \\
127
+ \frac{\partial}{\partial{q_1}} ((m(q_1+q_2)+b)q_1-cq_1) = 0 \\
128
+ 2mq_1 + mq_2+b - c = 0
129
+ \end{align*}
130
+ $$
131
+
132
+ However, this graph assumes a fixed value for $q_2$. Let's try varying $q_2$ and see what happens.
133
+
134
+ [Following image has line graphs for downward sloping market demand and marginal revenue functions]
135
+
136
+ ```python
137
+ m, b = -0.5, 9
138
+ q_2 = 0
139
+ c = 3
140
+ D_fn = lambda x: m * x + b + 2
141
+ P = lambda x: D_fn(x + q_2)
142
+ r_1_fn = lambda x: 2 * m * x + m * q_2 + b + 2
143
+ r_1_inv = lambda x: (x - m * q_2 - b - 2) / 2 / m
144
+
145
+ q_1 = np.linspace(0, 25, 1000)
146
+ D = D_fn(q_1)
147
+ d_1 = P(q_1)
148
+ r_1 = r_1_fn(q_1)
149
+
150
+ q_1_prime = r_1_inv(c)
151
+
152
+ plt.figure(figsize=[8,8])
153
+
154
+ # demand
155
+ plt.plot(q_1, D, label=r"Market Demand, $D$")
156
+ plt.text(5, b, r"$D$", size=16)
157
+
158
+
159
+ # marginal revenue
160
+ plt.plot(q_1, r_1, label=r"Marginal Revenue of Firm 1 for $q_2=0$, $r_1$")
161
+ plt.text(5, b - 2.5, r"$r_1$", size=16)
162
+
163
+ # case when q_2 = q_c
164
+ m, b = -0.5, 9
165
+ q_2 = 16
166
+ c = 3
167
+ D_fn = lambda x: m * x + b + 2
168
+ P = lambda x: D_fn(x + q_2)
169
+ r_1_fn = lambda x: 2 * m * x + m * q_2 + b + 2
170
+ r_1_inv = lambda x: (x - m * q_2 - b - 2) / 2 / m
171
+
172
+ q_1 = np.linspace(0, 25, 1000)
173
+ D = D_fn(q_1)
174
+ d_1 = P(q_1)
175
+ r_1 = r_1_fn(q_1)
176
+
177
+
178
+ # marginal revenue
179
+ plt.plot(q_1, r_1, label=r"Marginal Revenue of Firm 1 for $q_2=q_m$, $r_1$")
180
+ plt.text(2.5, 0.8, r"$r_1$", size=16)
181
+
182
+ # marginal cost
183
+ plt.plot(q_1, c * np.ones_like(q_1), label=r"Marginal Cost, $c$")
184
+ plt.text(20, 3.2, r"$c$", size=16)
185
+
186
+
187
+ # q'_1(0)
188
+ plt.vlines(r_1_inv(-5), 0, 3, linestyles="dashed")
189
+
190
+ plt.xlim([0,22])
191
+ plt.ylim([0,12])
192
+ plt.xticks([r_1_inv(-5), 0], [r"$q_1^*(0) = q_m$", r"$q_1^*(q_m)$"], size=14)
193
+ plt.yticks([], [], size=14)
194
+ plt.xlabel("Quantity", size=16)
195
+ plt.ylabel("Price", size=16)
196
+ plt.legend();
197
+ ```
198
+
199
+ The orange marginal revenue curve represents a scenario where Firm 2 produces so little that Firm 1 can overtake the market and become a monopoly. The green marginal revenue curve is a scenario where Firm 2 produces so much that it is too costly for Firm 1 to remain in this market, causing Firm 1's optimal output to become 0.
200
+
201
+ Let's now try to solve for the Nash Equilibrium of this game.
202
+
203
+ ## Cournot Equilibrium
204
+ Recall that a Nash Equilibrium is where both the players are giving the best response to each other's actions. Graphically, this is where the best response curves intersect.
205
+
206
+ [Following image has line graphs for the intersecting lines of response functions between two firms]
207
+
208
+ ```python
209
+ q_m = r_1_inv(-5)
210
+ q_c = D_fn(-10)
211
+
212
+ p1 = (0, q_m)
213
+ p2 = (q_c, 0)
214
+
215
+ slope = (p2[1] - p1[1]) / (p2[0]- p1[0])
216
+ intercept = p1[1] - slope * p1[0]
217
+
218
+ xs = np.linspace(0, 25, 1000)
219
+ ys1 = slope * xs + intercept
220
+ ys2 = (xs - intercept) / slope
221
+
222
+ plt.figure(figsize=[8,8])
223
+
224
+ plt.plot(xs, ys1, label=r"$q'_1$")
225
+ plt.plot(xs, ys2, label=r"$q'_2$")
226
+ plt.text(13, intercept - 6.3, r"$q_1^*(q_2)$", size=16)
227
+ plt.text(3, intercept + 2.5, r"$q_2^*(q_1)$", size=16)
228
+
229
+ x_star = - (slope + 1) * intercept / (slope**2 - 1)
230
+ y_star = slope * x_star + intercept
231
+ plt.vlines(x_star, 0, y_star, linestyle="dashed")
232
+ plt.hlines(y_star, 0, x_star, linestyle="dashed")
233
+
234
+ plt.xlim([0,22])
235
+ plt.ylim([0,12])
236
+ plt.xticks([x_star], [r"$q_2^*$"], size=14)
237
+ plt.yticks([y_star], [r"$q_1^*$"], size=14);
238
+ ```
239
+
240
+ In this class, you will use Python to find that intersection. However, for future classes, it may be useful to see how you can find the intersection algebraically. Here's the equation we must solve:
241
+
242
+ $$
243
+ \begin{align*}
244
+ q_1=\frac{c-b-m(\frac{c-mq_1-b}{2m})}{2m}
245
+ \end{align*}
246
+ \
247
+ $$
248
+
249
+ To find player 1's equilibrium strategy, we solve for $q_1$.
250
+
251
+ $$
252
+ \begin{align*}
253
+ q_1=\frac{c-b-m(\frac{c-mq_1-b}{2m})}{2m} \\
254
+ 2mq_1 = c-b-m(\frac{c-mq_1-b}{2m}) \\
255
+ 2mq_1 = c-b-\frac{c-mq_1-b}{2}\\
256
+ 2mq_1 - \frac{mq_1}{2} = c - \frac{c}{2} -b +\frac{b}{2} \\
257
+ \frac{3mq_1}{2} = \frac{c-b}{2} \\
258
+ 3mq_1 = c-b \\
259
+ q_1^{*} = \frac{c-b}{3m}
260
+ \end{align*}
261
+ \
262
+ $$
263
+
264
+ Phew, that was a lot of math. However, we are not quite done yet - we still need to find player 2's optimal output. The process for doing this is fairly similar to the process for finding player 1's optimal output, as can be seen below.
265
+
266
+ $$
267
+ \begin{align*}
268
+ q_2 = \frac{c-mq_1^{*}-b}{2m} = \frac{c-b-m(\frac{c-b}{3m})}{2m} \\
269
+ 2mq_2 = c -\frac{c}{3} - b +\frac{b}{3} \\
270
+ 2mq_2 = \frac{2(c-b)}{3}\\
271
+ q_2^{*} = q_2 = \frac{c-b}{3m}
272
+ \end{align*}
273
+ \
274
+ $$
275
+ This shows that the unique Nash Equilibrium of Cournot Duopoly is $(q_1^{*},q_2^{*}) = (\frac{c-b}{3m},\frac{c-b}{3m})$
276
+
277
+ ## Implications
278
+ The Cournot model implies that output is greater in a Cournot duopoly than in a monopoly but still lower than perfect competition. Prices are also lower in a Cournot duopoly, but higher than perfect competition. Cournot equilibria are also a subset of Nash equilibria, and so the equilibrium we just derived is one from which neither player will likely deviate. As noted earlier, Cournot also indicates that members of a duopoly could form a cartel and raise profits by colluding.
279
+
280
+ ### Applying Cournot
281
+ Now that we have shown how to derive the Cournot equilibrium, let’s apply this to a problem. Consider the industry of airline manufacturing: there are two main competitors, Boeing and Airbus, and for this problem, we will think of this market as a Cournot duopoly. Suppose the market demand curve is given by $P=-1.89Q+148.89$ where the price is in millions of dollars and that the marginal cost of both firms is constant at $c=100$. Solve for the Nash Equilibrium.
282
+
283
+
284
+ First we will want to derive both firms profit functions:
285
+
286
+ $$
287
+ \begin{align*}
288
+ \pi_1 & = Pq_1 -cq_1 \\
289
+ \pi_1 & = (-1.89Q+148.89)q_1 - cq_1 \\
290
+ \pi_1 & = (-1.89(q_1+q_2)+148.89)q_1 -cq_1 \\
291
+ \pi_1 & = -1.89q_1^2-1.89q_1q_2+148.89q_1 - cq_1
292
+ \end{align*}
293
+ $$
294
+
295
+ For firm 2 the profit function similarly looks like:
296
+
297
+ $$
298
+ \begin{align*}
299
+ \pi_2 = -1.89q_2^2-1.89q_1q_2+148.89q_2 - cq_2
300
+ \end{align*}
301
+ $$
302
+
303
+ Now we will take the partial derivative to find the best response functions for both players. For firm 1 this looks like:
304
+
305
+ $$
306
+ \begin{align*}
307
+ \frac{\partial{\pi_1}}{\partial{q_1}}= -3.78q_1 - 1.89q_2+148.89 - 100 = 0\\
308
+ q_1 = \frac{48.89-1.89q_2}{3.78}
309
+ \end{align*}
310
+ \
311
+ $$
312
+
313
+ For firm 2 this looks like:
314
+
315
+ $$
316
+ \begin{align*}
317
+ \frac{\partial{\pi_2}}{\partial{q_2}}= -3.78q_2 - 1.89q_1+148.89 - 100 = 0\\
318
+ q_2 = \frac{48.89-1.89q_1}{3.78}
319
+ \end{align*}
320
+ $$
321
+
322
+ Now while you can solve this algebraically, it takes much longer and leaves a lot of room for error, so as data scientists we will use Python to solve this problem. The code for this looks like this:
323
+
324
+ ```python
325
+ q_1=sympy.Symbol("q_1") #Create a symbol for q1
326
+ q_2=sympy.Symbol("q_2")#Create a symbol for q2
327
+ Br_q_1 = (48.89 - 1.89*q_2)/3.78 #Create the best response function for firm 1
328
+ Br_q_2 = (48.89 - 1.89*q_1)/3.78 #Create the best response function for firm 2
329
+ q1_Br_2 = (48.89-3.78*q_2)/1.89 #Solve firm 2's best response function for q1
330
+ q_1_star = solve(Br_q_1,q1_Br_2) #Use the solve function to solve the system of equations
331
+ q_2_star = Br_q_2.subs(q_1,q_1_star ) #Substiute in the optimal q_1_star into firm 2's best response function
332
+ (q_1_star,q_2_star) #Nash Equilibrium
333
+ ```