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,73 @@
1
+ ---
2
+ title: govt-intervention
3
+ type: textbook
4
+ source_path: content/03-public/govt-intervention.ipynb
5
+ chapter: 3
6
+ ---
7
+
8
+ # Other Forms of Government Intervention
9
+
10
+ ## Price Controls
11
+
12
+ The last type of government intervention is far more forceful than taxes and subsidies. Instead of changing the per-unit cost of a good paid by consumers or producers, price controls directly fix the market price. It does not change the amount different consumers are willing and able to pay or how much producers are willing and able to produce.
13
+
14
+ When price controls are introduced to a market, the equilibrium price and quantity no longer exist. Consumer and producers are forced to sell or buy goods at the prescribed price.
15
+
16
+ [Following image is a hand drawn diagram of a price floor]
17
+
18
+ ```{figure} fig6-price-floor.png
19
+ ---
20
+ width: 500px
21
+ name: price-floor
22
+ ---
23
+ A price floor
24
+ ```
25
+
26
+ ![title](fig6-price-floor.png)
27
+
28
+ Let's take a price floor as the first example. This is when the government imposes a minimum price for which a good can be sold. For this to be effective, the price floor must be set above the equilibrium price, otherwise the market would just settle at equilibrium. This results in a surplus, as producers are willing to sell far more goods than consumers are willing to purchase. This results in a surplus as $Q_s > Q_d$. Normally, without government intervention, the invisible hand would push prices down until $Q_s = Q_d$. However, with the price floor, the market remains in a state of disequilibrium.
29
+
30
+ Minimum wages act as a price floor, keeping wages above a certain level. At this higher rate, more workers are willing to work than there is demand for, creating unemployment.
31
+
32
+ [Following image is a hand drawn diagram of a price ceiling ]
33
+
34
+ ```{figure} fig7-price-ceil.png
35
+ ---
36
+ width: 500px
37
+ name: price-ceil
38
+ ---
39
+ A price ceiling
40
+ ```
41
+
42
+ ![title](fig7-price-ceil.png)
43
+
44
+ The other control is a price ceiling. This is when the government imposes a maximum price for which a good can be sold. This results in a shortage as consumers are to purchase more goods than producer are willing to sell: $Q_d > Q_s$.
45
+
46
+ Rent control in Berkeley acts as a price ceiling, keeping rents from rising above a certain level.
47
+
48
+ ## Optional: World Trade vs. Autarky
49
+
50
+ Throughout the class so far, we have assumed that the economy is operating by itself, in isolation. Economists use the word "autarky" to represent the scenario when a country is economically self-sufficient. The key consequence of this is that the country's economy is unaffected by global events, allowing us to conclude that any changes to equilibrium price or quantity are purely a result of shifts in domestic demand or supply.
51
+
52
+ We will now assume that the country is no longer in autarky, and is thus subject to world prices, demand and supply.
53
+
54
+ We now label our demand and supply curves as domestic demand and supply, respectively. Their intersection represents where the market would be if we were in autarky. However, as we are now open to world trade, the price where the market operates is now determined by the world price, which may be totally different from the equilibrium price.
55
+
56
+ Where the world price line intersects with domestic demand represents the total quantity demanded within the market. Where it intersects with the domestic supply curve is the total quantity supplied.
57
+
58
+ [Following image is a hand drawn diagram of a market with trade]
59
+
60
+ ```{figure} fig8-autarky.png
61
+ ---
62
+ width: 500px
63
+ name: autarky
64
+ ---
65
+ Domestic supply and demand in the world market
66
+ ```
67
+
68
+ ![title](fig8-autarky.png)
69
+
70
+ Let's first imagine a scenario where world price is lower than domestic price. From our graph, we find that $Q_s < Q_d$. This is a shortage. Domestic producers are not willing to fully provide all goods demanded by consumers in the economy. The difference between what is produced domestically and the total quantity demanded will be fulfilled by international producers. Thus, $Q_d - Q_s =$ imports. As prices are lower than what some consumers were willing to pay, there is a significant consumer surplus. Unfortunately for producers, as world prices are lower than what they would have been able to charge, they lose producer surplus. There is a net gain in the change in consumer surplus is greater than the loss in producer surplus. This is represented by the triangle between the curves and above the world price line.
71
+
72
+ Now, let's imagine that world price is greater than domestic price. In this case, $Q_s > Q_d$. We now have a surplus where the quantity demanded by consumers is far less than what domestic producers are willing to provide at this higher price. Thus, domestic producers will willingly sell goods to domestic customers and sell the rest on the global market. $Q_s - Q_d =$ exports. As prices are higher than what some consumers were willing to pay, there is a significant decrease in consumer surplus. Producers however rejoice as world prices are higher than what they would have been able to charge. They gain producer surplus. There is a net gain in total surplus as the increase in producer surplus is greater than the loss in consumer surplus. This is represented by the triangle between the curves and above the world price line.
73
+
@@ -0,0 +1,10 @@
1
+ ---
2
+ title: index
3
+ type: textbook
4
+ source_path: content/03-public/index.md
5
+ chapter: 3
6
+ ---
7
+
8
+ # Public Economics
9
+
10
+ **Public economics** deals with the different methods government policy affect market equilibria, and studies these through the lens of economic efficiency and equity. This chapter begins with a graphical and mathematical overview of market equilibrium. It follows with a discussion of consumer and producer surplus - specifically how these concepts underscore the competing forces of supply and demand. Last, it covers the different ways in which governments intervene: taxes, subsidies and price controls. It discusses their effects on welfare and how intervention changes market participation
@@ -0,0 +1,351 @@
1
+ ---
2
+ title: surplus
3
+ type: textbook
4
+ source_path: content/03-public/surplus.ipynb
5
+ chapter: 3
6
+ ---
7
+
8
+ ```python
9
+ import matplotlib.pyplot as plt
10
+ import matplotlib.patches as patches
11
+ import sympy
12
+ import numpy as np
13
+ import warnings
14
+ from datascience import *
15
+ from ipywidgets import interact, interactive, fixed, interact_manual
16
+ import ipywidgets as widgets
17
+ import plotly.graph_objects as go
18
+ from plotly.offline import plot
19
+ from IPython.display import display, HTML
20
+ warnings.filterwarnings('ignore')
21
+ plt.style.use("seaborn-muted")
22
+ solve = lambda x,y: sympy.solve(x-y)[0] if len(sympy.solve(x-y)) == 1 else "Not Single Solution"
23
+ %matplotlib inline
24
+ ```
25
+
26
+ # Surplus
27
+
28
+ ## Consumer Surplus
29
+ Although all consumers face the same market price, consumers are different in how much they individually value a good. We say that consumers have a maximum price that they are willing to pay for a good, and any price marginally higher than this price will dissuade the consumer from participating in the market. This max willingness to pay (WTP) price varies among entities based on their desire for the good, which in turn can be based on how much of the good they already have.
30
+
31
+ Consider the market for electricity. Among consumers we have entities such as households, commercial buildings, factories, and so on. A factory would likely have a very high WTP for electricity because the opportunity costs for factories to not operate are very high. Capital is expensive, employees still have to be paid, and it is inconvenient to have to stop and start up machinery frequently. Thus, for a factory it is preferable to always have a reliable supply of electricity to continue operations and this need is reflected in the WTP. Contrast this with households, who certainly value having electricity, but should electricity become prohibitively expensive, probably would decide to cut back on usage as the drawbacks of not having electricity aren't as severe compared to the factory above.
32
+
33
+ ## Producer Surplus
34
+ Producers experience a similar characteristic. A producer has a minimum price at which it is willing to produce a good based on its costs. Any market price less than this price would dissuade a producer from supplying its good. Again, in the electricity example, we have several power plants that produce electricity, but each inherently does so at different costs. Imagine and contrast the operating costs of a solar farm with a coal plant, or a newer, more efficient natural gas plant versus an older one.
35
+
36
+ Putting all of these concepts together we arrive at the idea of economic welfare. Suppose electricity costs 10 cents per kWh. On the demand side, imagine a factory who's WTP is 30 cents/kWh. This factory enjoys a consumer surplus of 20 cents/kWh, in other words, it's paying 20 cents less per kWh than what it would be willing to pay. A household might have a WTP of 15 cents/kWh. Here the household's surplus is only 5 cents/kWh. We can also imagine a consumer whose WTP is less than the market price and thus doesn't participate in the market. Imagine for some reason that cryptocurrency prices have dropped to the point that they aren't worth the electricity it takes to mine them. In this case, we might have an idle or non-existent crypto-farm (a place with a lot of computing power) due to electricity being too expensive. On the producer side, maybe we have a solar plant which is operating at the market price, but a natural gas plant that is idling because the price of supplying electricity isn't sufficient to make up for operating costs.
37
+
38
+ Combining the surpluses of all individual consumers and producers yields the market consumer surplus and producer surplus. As the market price fluctuates, certain comsumers and producers enter and exit the market, and the total surplus varies. Note from the above examples that a consumer is not always an individual, it can be a firm buying from another firm. We now explore further.
39
+
40
+ ## Example
41
+
42
+ We create a consumer class with a WTP characteristic, and a list of consumers with WTP from 10 to 1. The binary function `demand` ```1``` if the consumer participates in the market at a given price and ```0``` if not.
43
+
44
+ ```python
45
+ class Consumer:
46
+ def __init__(self, WTP):
47
+ self.WTP = WTP
48
+
49
+ def demand(self, price):
50
+ if price <= self.WTP:
51
+ return 1
52
+ else:
53
+ return 0
54
+
55
+ def surplus(self, price):
56
+ if price <= self.WTP:
57
+ return self.WTP - price
58
+ else:
59
+ return 0
60
+
61
+ consumers = [Consumer(x) for x in range(10,0,-1)]
62
+
63
+ [x.demand(6) for x in consumers]
64
+ ```
65
+
66
+ For a market price of 6, we have 5 consumers who participate and 5 who don't. Now let's make a table of the lists of participants for each market price between 1 and 10.
67
+
68
+ ```python
69
+ per_consumer = np.array([[x.demand(i) for x in consumers] for i in range(10, 0, -1)]).T
70
+ Table().with_columns("Market Price", np.arange(10, 0, -1),
71
+ "Consumer with WTP: 10", per_consumer[0],
72
+ "Consumer with WTP: 9", per_consumer[1],
73
+ "Consumer with WTP: 8", per_consumer[2],
74
+ "Consumer with WTP: 7", per_consumer[3],
75
+ "Consumer with WTP: 6", per_consumer[4],
76
+ "Consumer with WTP: 5", per_consumer[5],
77
+ "Consumer with WTP: 4", per_consumer[6],
78
+ "Consumer with WTP: 3", per_consumer[7],
79
+ "Consumer with WTP: 2", per_consumer[8],
80
+ "Consumer with WTP: 1", per_consumer[9])
81
+ ```
82
+
83
+ You can draw a downward-sloping diagonal line sepearating ```1```s from ```0```s - a vague resemblance of a downward-sloping demand curve. The left-most consumer, with a WTP of 10, always participates for the market prices we have listed. The right-most consumer only participates at a market price of 1. Now lets try and find the number of consumers who participate for each price point, starting at 10.
84
+
85
+ ```python
86
+ market = Table().with_column("Market Price", np.arange(10, 0, -1))
87
+ market = market.with_column("Number of Participants",
88
+ market.apply(lambda price : sum([x.demand(price) for x in consumers]), "Market Price"))
89
+ market
90
+ ```
91
+
92
+ Instead of printing a binary 1 or 0 indicating market participation, we've displayed each participant's actual surplus value. Similarly, let's find total surplus per price point.
93
+
94
+ ```python
95
+ surplus = Table().with_column("Market Price", np.arange(10, 0, -1))
96
+ surplus = surplus.with_column("Total Surplus",
97
+ surplus.apply(lambda price : sum([x.surplus(price) for x in consumers]), "Market Price"))
98
+ surplus
99
+ ```
100
+
101
+ Clearly there must be an opposite "force" at play here, otherwise all prices would converge to 0 as consumers maximize their surplus (more on maximization later). Naturally, we must also consider the producers who sell their product to the consumers. We essentially repeat the exercise above, but now instead of a consumer class with individual willingness to pay, we have a producer class with some minimal market price at which production can occur.
102
+
103
+ ```python
104
+ class Producer:
105
+ def __init__(self, WTA):
106
+ self.WTA = WTA
107
+
108
+ def supply(self, price):
109
+ if price >= self.WTA:
110
+ return 1
111
+ else:
112
+ return 0
113
+
114
+ def surplus(self, price):
115
+ if price >= self.WTA:
116
+ return price - self.WTA
117
+ else:
118
+ return 0
119
+
120
+ producers = [Producer(x) for x in range(1,11)]
121
+ ```
122
+
123
+ ```python
124
+ per_producer = np.array([[x.surplus(i) for x in producers] for i in np.arange(10, 0, -1)]).T
125
+
126
+ Table().with_columns("Market Price", np.arange(10, 0, -1),
127
+ "Producer with WTP: 10", per_producer[0],
128
+ "Producer with WTP: 9", per_producer[1],
129
+ "Producer with WTP: 8", per_producer[2],
130
+ "Producer with WTP: 7", per_producer[3],
131
+ "Producer with WTP: 6", per_producer[4],
132
+ "Producer with WTP: 5", per_producer[5],
133
+ "Producer with WTP: 4", per_producer[6],
134
+ "Producer with WTP: 3", per_producer[7],
135
+ "Producer with WTP: 2", per_producer[8],
136
+ "Producer with WTP: 1", per_producer[9])
137
+ ```
138
+
139
+ Looks familiar, but with an opposite slope! Here we've captured the idea of producer surplus. At a market price of 10, the leftmost producer is very happy with a surplus of 9, as in this case that producer is actually able to produce and sell at a price of 1 but is able to operate at a price of 10.
140
+
141
+ Before we continue, let's take a moment to think about the meaning and significance of our findings. Firms that can produce at lower market prices than their peers seem to be better off in the sense that they enjoy higher surplus. This minimum production price is based on the costs of operation the firm experiences, so naturally it seems that firms that can operate at lower costs do better. Certainly, if market prices decrease, more inefficent firms would be the first to shut down while these low operating cost firms continue to do business. This idea is very important in economics: Firms that can reduce their costs are rewarded with higher surplus. This is pretty much how society advances, at least in an economics context. Production methods continually to improve, and less efficient firms must either follow suit or shut down as prices decrease, to the benefit of consumers.
142
+
143
+ However, what would the equivalent be for the consumer side of things? We've discussed the idea of willingness to pay, and initially it might seem that in our perfectly-competitive market environment, only the consumers who most need a good or service will be the first to get it, as their WTP is the highest. We might think that resources are efficiently allocated in this way. Most of the time this is likely the case, but we've made an assumption while reaching this conclusion; an assumption that doesn't necessarily hold. We have assumed that a person with high willingness to pay also has at least an equally high *ability* to pay. In reality, this might not be the case. A hungry person might have high WTP for a serving of food, but if this person lacks the means to pay for this food, his willingness to pay won't do him much good. In this scenario, our earlier exercise reflects willingness to pay with a simultaneous ability to pay as well. While this week isn't about the ethics of certain types of markets and whether they achieve their goals, it's important to keep in mind that in these ideal exercises, an efficient economy with rational pricing should reflect consumers' willingness to pay, whereas in reality this might not actually be the case.
144
+
145
+ ## Note on the Demand and Supply Curves
146
+
147
+ As pointed out above, the matrix we saw with rows of surpluses and columns of prices resembles the demand curve in the sense that we can see a diagonal line separating participants from non-participants. This is no coincidence. This idea is essentially what the demand and supply curves depict, except that due to there usually being many participants in a market, we simplify the concept to a continuous curve as opposed to a set of discrete values. This is helpful not only for visualization, but as we will soon see we can use these curves to find rates of change, which will prove to be useful as well.
148
+
149
+
150
+ Earlier we had a matrix of each individual's surplus at each price point, and the overall surplus at each price point. Notice how as the price decreased, surplus increased. Let's see this exact same concept illustrated on a familiar demand curve. Take a few moments to adjust the slider controlling the market price to see how consumer surplus behaves.
151
+
152
+ [Following image is an interactive graph of Consumer Surplus at different price points]
153
+
154
+ ```python
155
+ filename="fig1.html"
156
+
157
+ p = sympy.Symbol("p")
158
+ consumers = [Consumer(x) for x in range(0,11)]
159
+ demand_equation = 10 - p
160
+ prices = [x for x in range(0,11)]
161
+ demand_Q = [float(demand_equation.subs(p,x)) for x in prices]
162
+
163
+ fig = go.Figure()
164
+
165
+ active_idx = None
166
+ active_val = 5
167
+ vals = []
168
+ for i, val in enumerate(np.arange(1, 11)):
169
+ vals.append(val)
170
+ if val == active_val:
171
+ active_idx = 3 * i
172
+ step_idx = i
173
+
174
+ demand_subbed = float(demand_equation.subs(p, val))
175
+ fig.add_trace(go.Scatter(
176
+ x = demand_Q,
177
+ y = prices,
178
+ mode = "lines",
179
+ name = f"Demand",
180
+ visible = False
181
+ ))
182
+ fig.add_trace(go.Scatter(
183
+ x = [demand_subbed, 0, 0, demand_subbed],
184
+ y = [val, val, 10, val],
185
+ name = "Consumer Surplus",
186
+ fill = "toself",
187
+ visible = False,
188
+ marker_color = "green"
189
+ ))
190
+ fig.add_trace(go.Scatter(
191
+ x = [demand_subbed],
192
+ y = [val],
193
+ mode = "markers",
194
+ name = "Equilibrium",
195
+ visible = False,
196
+ marker = dict(color = "red", size = 10)
197
+ ))
198
+
199
+ fig.data[active_idx].visible = True
200
+ fig.data[active_idx + 1].visible = True
201
+ fig.data[active_idx + 2].visible = True
202
+
203
+ steps = []
204
+ for i in range(len(fig.data)):
205
+ if i % 3 != 0:
206
+ continue
207
+ val = vals[i // 3]
208
+ step = dict(
209
+ method = "update",
210
+ args = [
211
+ {"visible": [False] * len(fig.data) + [True]},
212
+ {"annotations": [
213
+ dict(xref="paper", yref="paper", x=-0.003, y=-0.2, showarrow=False, xanchor="left",
214
+ text=f"Consumer Surplus: {sum([person.surplus(val) for person in producers])}")
215
+ ]}
216
+ ],
217
+ label = f"{val}"
218
+ )
219
+ step["args"][0]["visible"][i] = True
220
+ step["args"][0]["visible"][i + 1] = True
221
+ step["args"][0]["visible"][i + 2] = True
222
+ steps.append(step)
223
+
224
+ sliders = [
225
+ dict(
226
+ active = step_idx,
227
+ currentvalue = {"prefix": "Market Price: $"},
228
+ pad = {"t": 75},
229
+ steps = steps
230
+ )
231
+ ]
232
+
233
+ fig.update_layout(
234
+ xaxis = dict(title = "Demand Quantity"),
235
+ yaxis = dict(title = "Price"),
236
+ title = "Demand Curve with Consumer Surplus Shaded",
237
+ sliders = sliders,
238
+ width = 800,
239
+ height = 600,
240
+ showlegend = False,
241
+ annotations = [dict(xref="paper", yref="paper", x=-0.003, y=-0.2, showarrow=False, xanchor="left",
242
+ text=f"Consumer Surplus: {sum([person.surplus(active_val) for person in consumers])}")]
243
+ )
244
+
245
+ plot(fig, filename=filename, auto_open=False, include_mathjax='cdn')
246
+
247
+ display(HTML(filename))
248
+ ```
249
+
250
+ Producer surplus with the supply curve works exactly the same way but mirrored to reflect the fact that producers gain surplus from higher prices instead of lower.
251
+
252
+ [Following image is an interactive graph of Producer Surplus at different price points]
253
+
254
+ ```python
255
+ filename="fig2.html"
256
+
257
+ p = sympy.Symbol("p")
258
+ producers = [Producer(x) for x in range(1,11)]
259
+ supply_equation = p
260
+ prices = [x for x in range(0,11)]
261
+ supply_Q = [float(supply_equation.subs(p, x)) for x in prices]
262
+
263
+ fig = go.Figure()
264
+
265
+ active_idx = None
266
+ active_val = 5
267
+ vals = []
268
+ for i, val in enumerate(np.arange(1, 11)):
269
+ vals.append(val)
270
+ if val == active_val:
271
+ active_idx = 3 * i
272
+ step_idx = i
273
+
274
+ supply_subbed = float(supply_equation.subs(p, val))
275
+ fig.add_trace(go.Scatter(
276
+ x = supply_Q,
277
+ y = prices,
278
+ mode = "lines",
279
+ name = f"Supply",
280
+ visible = False
281
+ ))
282
+ fig.add_trace(go.Scatter(
283
+ x = [supply_subbed, 0, 0, supply_subbed],
284
+ y = [val, val, 0, val],
285
+ name = "Producer Surplus",
286
+ fill = "toself",
287
+ visible = False,
288
+ marker_color = "green"
289
+ ))
290
+ fig.add_trace(go.Scatter(
291
+ x = [supply_subbed],
292
+ y = [val],
293
+ mode = "markers",
294
+ name = "Equilibrium",
295
+ visible = False,
296
+ marker = dict(color = "red", size = 10)
297
+ ))
298
+
299
+ fig.data[active_idx].visible = True
300
+ fig.data[active_idx + 1].visible = True
301
+ fig.data[active_idx + 2].visible = True
302
+
303
+ steps = []
304
+ for i in range(len(fig.data)):
305
+ if i % 3 != 0:
306
+ continue
307
+ val = vals[i // 3]
308
+ step = dict(
309
+ method = "update",
310
+ args = [
311
+ {"visible": [False] * len(fig.data) + [True]},
312
+ {"annotations": [
313
+ dict(xref="paper", yref="paper", x=-0.003, y=-0.2, showarrow=False, xanchor="left",
314
+ text=f"Producer Surplus: {sum([person.surplus(val) for person in producers])}")
315
+ ]}
316
+ ],
317
+ label = f"{val}"
318
+ )
319
+ step["args"][0]["visible"][i] = True
320
+ step["args"][0]["visible"][i + 1] = True
321
+ step["args"][0]["visible"][i + 2] = True
322
+ steps.append(step)
323
+
324
+ sliders = [
325
+ dict(
326
+ active = step_idx,
327
+ currentvalue = {"prefix": "Market Price: $"},
328
+ pad = {"t": 75},
329
+ steps = steps
330
+ )
331
+ ]
332
+
333
+ fig.update_layout(
334
+ xaxis = dict(title = "Supply Quantity"),
335
+ yaxis = dict(title = "Price"),
336
+ title = "Supply Curve with Producer Surplus Shaded",
337
+ sliders = sliders,
338
+ width = 800,
339
+ height = 600,
340
+ showlegend = False,
341
+ annotations = [dict(xref="paper", yref="paper", x=-0.003, y=-0.2, showarrow=False, xanchor="left",
342
+ text=f"Producer Surplus: {sum([person.surplus(active_val) for person in producers])}")]
343
+ )
344
+
345
+ plot(fig, filename=filename, auto_open=False, include_mathjax='cdn')
346
+
347
+ display(HTML(filename))
348
+ ```
349
+
350
+ Here we used a demand curve of $10-P$ and a supply curve of $P$.
351
+