Skip to content

Edits to intro supply demand lecture #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 116 additions & 12 deletions lectures/intro_supply_demand.md
Original file line number Diff line number Diff line change
@@ -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 <supply_demand_multiple_goods>` 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.
Expand All @@ -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
Copy link
Contributor

@mmcky mmcky Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jstac these need to be code-cell to be executable. Is that your intention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mmcky . Pushed changes.

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$.
Expand All @@ -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$:

Expand Down Expand Up @@ -83,15 +95,20 @@ $$ (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:

$$
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`.
Expand All @@ -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**.
<!-- #endregion -->

+++

## 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

```