Skip to content

Fix supply demand #176

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 10 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 12 additions & 38 deletions lectures/intro_supply_demand.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ 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$.
We study a market for a single good in which buyers and sellers exchange a quantity $q$ for a price $p$.

Quantity $q$ and price $p$ are both scalars.
Quantity $q$ and price $p$ are both scalars.

We assume that inverse demand and supply curves for the good are:

Expand Down Expand Up @@ -177,7 +177,7 @@ plt.show()

Consumer surplus gives a measure of total consumer welfare at quantity $q$.

The idea is that the inverse demand curve $d_0 - d_1 q$ shows willingness to
The idea is that the inverse demand curve $d_0 - d_1 q$ shows willingness to
pay at a given quantity $q$.

The difference between willingness to pay and the actual price is the surplus.
Expand Down Expand Up @@ -312,16 +312,15 @@ $$
q = \frac{ d_0 - s_0}{s_1 + d_1}
$$ (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.
Let's remember the quantity $q$ given by equation {eq}`eq:old1` that a social planner would choose to maximize consumer surplus 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 that equates supply to demand.



### Competitive Equilibrium

Instead of equating quantities supplied and demanded, we'll can accomplish the
Instead of equating quantities supplied and demanded, we can accomplish the
same thing by equating demand price to supply price:

$$
Expand All @@ -347,29 +346,24 @@ supply to demand brings us a **key finding:**

This is a version of the [first fundamental welfare theorem](https://en.wikipedia.org/wiki/Fundamental_theorems_of_welfare_economics),

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

## Generalizations

In a {doc}`later lecture <supply_demand_multiple_goods>`, we'll derive
In a {doc}`later lecture <supply_demand_multiple_goods>`, 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**.
taker and maximizes his profits minus total costs that are described by a **cost function**.

## Exercises

Expand All @@ -387,21 +381,18 @@ $$
All parameters are positive, as before.




```{exercise}
:label: isd_ex1

Define a new `Market` class that holds the same parameter values as before by
changes the `inverse_demand` and `inverse_supply` methods to
changing the `inverse_demand` and `inverse_supply` methods to
match these new definitions.

Using the class, plot the inverse demand and supply curves $i_d$ and $i_s$

```



```{solution-start} isd_ex1
:class: dropdown
```
Expand Down Expand Up @@ -432,7 +423,6 @@ Let's create an instance.
market = Market()
```


Here is a plot of inverse supply and demand.

```{code-cell} ipython3
Expand All @@ -455,16 +445,13 @@ ax.set_ylabel('price')
plt.show()
```


```{solution-end}
```



```{exercise}
:label: isd_ex2


As before, consumer surplus at $q$ is the area under the demand curve minus
price times quantity:

Expand Down Expand Up @@ -500,7 +487,6 @@ Plot welfare as a function of $q$.
```



```{solution-start} isd_ex2
:class: dropdown
```
Expand All @@ -515,7 +501,6 @@ $$

Here's a Python function that computes this value:


```{code-cell} ipython3
def W(q, market):
# Unpack
Expand All @@ -528,7 +513,6 @@ def W(q, market):

The next figure plots welfare as a function of $q$.


```{code-cell} ipython3
fig, ax = plt.subplots()
ax.plot(q_vals, W(q_vals, market), label='welfare')
Expand All @@ -541,11 +525,9 @@ plt.show()
```



```{exercise}
:label: isd_ex3


Due to nonlinearities, the new welfare function is not easy to maximize with
pencil and paper.

Expand All @@ -554,7 +536,6 @@ Maximize it using `scipy.optimize.minimize_scalar` instead.
```



```{solution-start} isd_ex3
:class: dropdown
```
Expand All @@ -569,8 +550,6 @@ result = minimize_scalar(objective, bounds=(0, 10))
print(result.message)
```



```{code-cell} ipython3
maximizing_q = result.x
print(f"{maximizing_q: .5f}")
Expand All @@ -580,7 +559,6 @@ print(f"{maximizing_q: .5f}")
```



```{exercise}
:label: isd_ex4

Expand All @@ -605,12 +583,10 @@ price, in line with the first fundamental welfare theorem.
```



```{solution-start} isd_ex3
:class: dropdown
```


```{code-cell} ipython3
from scipy.optimize import newton

Expand All @@ -621,7 +597,5 @@ equilibrium_q = newton(excess_demand, 0.1)
print(f"{equilibrium_q: .5f}")
```


```{solution-end}
```

Loading