From 38bf614a415a1c3c4b812b864e2f6aeefa32fb4f Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Thu, 23 Mar 2023 20:56:28 +1100 Subject: [PATCH 1/3] misc --- lectures/intro_supply_demand.md | 128 +++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 12 deletions(-) diff --git a/lectures/intro_supply_demand.md b/lectures/intro_supply_demand.md index f7caf74e4..f1826ad2b 100644 --- a/lectures/intro_supply_demand.md +++ b/lectures/intro_supply_demand.md @@ -1,12 +1,17 @@ # Introduction to Supply and Demand -This lecture is about some linear models of equilibrium prices and -quantities, one of the main topics of elementary microeconomics. - -Our approach is first to offer a scalar version with one good and one price. ++++ ## Outline +This lecture is about some models of equilibrium prices and quantities, one of +the main topics of elementary microeconomics. + +Throughout the lecture, we focus on models with one good and one price. + +({doc}`Later ` we will investigate settings with +many goods.) + We shall describe two classic welfare theorems: * **first welfare theorem:** for a given a distribution of wealth among consumers, a competitive equilibrium allocation of goods solves a social planning problem. @@ -23,6 +28,13 @@ Key infrastructure concepts that we'll encounter in this lecture are * social welfare as a sum of consumer and producer surpluses * competitive equilibrium +We will use the following imports. + +```python +import numpy as np +import matplotlib.pyplot as plt +``` + ## Supply and Demand We study a market for a single good in which buyers and sellers exchange a quantity $q$ for a price $p$. @@ -41,7 +53,7 @@ $$ We call them inverse demand and supply curves because price is on the left side of the equation rather than on the right side as it would be in a direct demand or supply function. - +### Surpluses and Welfare We define **consumer surplus** as the area under an inverse demand curve minus $p q$: @@ -83,8 +95,12 @@ $$ (eq:old1) Let's remember the quantity $q$ given by equation {eq}`eq:old1` that a social planner would choose to maximize consumer plus producer surplus. -We'll compare it to the quantity that emerges in a competitive equilibrium equilibrium that equates -supply to demand. +We'll compare it to the quantity that emerges in a competitive equilibrium +equilibrium that equates supply to demand. + ++++ + +### Competitive Equilibrium Instead of equating quantities supplied and demanded, we'll can accomplish the same thing by equating demand price to supply price: @@ -92,6 +108,7 @@ $$ p = d_0 - d_1 q = s_0 + s_1 q , $$ ++++ It we solve the equation defined by the second equality in the above line for $q$, we obtain the competitive equilibrium quantity; it equals the same $q$ given by equation {eq}`eq:old1`. @@ -105,15 +122,102 @@ It also brings a useful **competitive equilibrium computation strategy:** * after solving the welfare problem for an optimal quantity, we can read a competitive equilibrium price from either supply price or demand price at the competitive equilibrium quantity -Soon we'll derive generalizations of the above demand and supply -curves from other objects. +### Generalizations + +In later lectures, we'll derive generalizations of the above demand and +supply curves from other objects. -Our generalizations will extend the preceding analysis of a market for a single good to the analysis -of $n$ simultaneous markets in $n$ goods. +Our generalizations will extend the preceding analysis of a market for a +single good to the analysis of $n$ simultaneous markets in $n$ goods. In addition * we'll derive **demand curves** from a consumer problem that maximizes a **utility function** subject to a **budget constraint**. * we'll derive **supply curves** from the problem of a producer who is price taker and maximizes his profits minus total costs that are described by a **cost function**. - + ++++ + +## Code + ++++ + +```python +class SingleGoodMarket: + + def __init__(self, + d_0=1.0, # demand intercept + d_1=0.5, # demand slope + s_0=0.1, # supply intercept + s_1=0.4): # supply slope + + self.d_0, self.d_1 = d_0, d_1 + self.s_0, self.s_1 = s_0, s_1 + + def inverse_demand(self, q): + return self.d_0 - self.d_1 * q + + def inverse_supply(self, q): + return self.s_0 + self.s_1 * q + + def equilibrium_quantity(self): + return (self.d_0 - self.s_0) / (self.d_1 + self.s_1) + + def equilibrium_price(self): + q = self.equilibrium_quantity() + return self.s_0 + self.s_1 * q +``` + +```python +def plot_supply_demand(market): + + # Unpack + d_0, d_1 = market.d_0, market.d_1 + s_0, s_1 = market.s_0, market.s_1 + q = market.equilibrium_quantity() + p = market.equilibrium_price() + grid_size = 200 + x_grid = np.linspace(0, 2 * q, grid_size) + ps = np.ones_like(x_grid) * p + supply_curve = market.inverse_supply(x_grid) + demand_curve = market.inverse_demand(x_grid) + + fig, ax = plt.subplots() + + ax.plot(x_grid, supply_curve, label='Supply', color='#020060') + ax.plot(x_grid, demand_curve, label='Demand', color='#600001') + + ax.fill_between(x_grid[x_grid <= q], + demand_curve[x_grid<=q], + ps[x_grid <= q], + label='Consumer surplus', + color='#EED1CF') + ax.fill_between(x_grid[x_grid <= q], + supply_curve[x_grid <= q], + ps[x_grid <= q], + label='Producer surplus', + color='#E6E6F5') + + ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7) + ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7) + + ax.legend(loc='upper center', frameon=False) + ax.margins(x=0, y=0) + ax.set_ylim(0) + ax.set_xlabel('Quantity') + ax.set_ylabel('Price') + + plt.show() +``` + +```python +market = SingleGoodMarket() +``` + +```python +plot_supply_demand(market) +``` + +```python + +``` From 05e1c48b38a6df0eba3fa01b5bba5d2d056332f1 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Thu, 23 Mar 2023 21:14:52 +1100 Subject: [PATCH 2/3] misc --- lectures/intro_supply_demand.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lectures/intro_supply_demand.md b/lectures/intro_supply_demand.md index f1826ad2b..e40fcf1bb 100644 --- a/lectures/intro_supply_demand.md +++ b/lectures/intro_supply_demand.md @@ -30,7 +30,7 @@ Key infrastructure concepts that we'll encounter in this lecture are We will use the following imports. -```python +```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt ``` @@ -142,7 +142,7 @@ In addition +++ -```python +```{code-cell} ipython3 class SingleGoodMarket: def __init__(self, @@ -168,7 +168,7 @@ class SingleGoodMarket: return self.s_0 + self.s_1 * q ``` -```python +```{code-cell} ipython3 def plot_supply_demand(market): # Unpack @@ -210,14 +210,11 @@ def plot_supply_demand(market): plt.show() ``` -```python +```{code-cell} ipython3 market = SingleGoodMarket() ``` -```python +```{code-cell} ipython3 plot_supply_demand(market) ``` -```python - -``` From 59ea931fe54c87a3d11a125feb04c2c71fb54e0b Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 23 Mar 2023 21:27:10 +1100 Subject: [PATCH 3/3] fix header for execution --- lectures/intro_supply_demand.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lectures/intro_supply_demand.md b/lectures/intro_supply_demand.md index e40fcf1bb..160906152 100644 --- a/lectures/intro_supply_demand.md +++ b/lectures/intro_supply_demand.md @@ -1,6 +1,15 @@ -# Introduction to Supply and Demand +--- +jupytext: + text_representation: + extension: .md + format_name: myst +kernelspec: + display_name: Python 3 (ipykernel) + language: python + name: python3 +--- -+++ +# Introduction to Supply and Demand ## Outline @@ -98,8 +107,6 @@ Let's remember the quantity $q$ given by equation {eq}`eq:old1` that a social pl We'll compare it to the quantity that emerges in a competitive equilibrium equilibrium that equates supply to demand. -+++ - ### Competitive Equilibrium Instead of equating quantities supplied and demanded, we'll can accomplish the same thing by equating demand price to supply price: @@ -136,12 +143,8 @@ In addition * we'll derive **supply curves** from the problem of a producer who is price taker and maximizes his profits minus total costs that are described by a **cost function**. -+++ - ## Code -+++ - ```{code-cell} ipython3 class SingleGoodMarket: