{
"cells": [
{
"cell_type": "markdown",
"id": "2aff7c23",
"metadata": {},
"source": [
"# Introduction to Supply and Demand"
]
},
{
"cell_type": "markdown",
"id": "8ec2256d",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"This lecture is about some models of equilibrium prices and quantities, one of\n",
"the core topics of elementary microeconomics.\n",
"\n",
"Throughout the lecture, we focus on models with one good and one price.\n",
"\n",
"In a [subsequent lecture](https://intro.quantecon.org/supply_demand_multiple_goods.html) we will investigate settings with\n",
"many goods."
]
},
{
"cell_type": "markdown",
"id": "babfc03e",
"metadata": {},
"source": [
"### Why does this model matter?\n",
"\n",
"In the 15th, 16th, 17th and 18th centuries, mercantilist ideas held sway among most rulers of European countries.\n",
"\n",
"Exports were regarded as good because they brought in bullion (gold flowed into the country).\n",
"\n",
"Imports were regarded as bad because bullion was required to pay for them (gold flowed out).\n",
"\n",
"This [zero-sum](https://en.wikipedia.org/wiki/Zero-sum_game) view of economics was eventually overturned by the work of the classical economists such as [Adam Smith](https://en.wikipedia.org/wiki/Adam_Smith) and [David Ricado](https://en.wikipedia.org/wiki/David_Ricardo), who showed how freeing domestic and international trade can enhance welfare.\n",
"\n",
"There are many different expressions of this idea in economics.\n",
"\n",
"This lecture discusses one of the simplest: how free adjustment of prices can maximize a measure of social welfare in the market for a single good."
]
},
{
"cell_type": "markdown",
"id": "a7fe94af",
"metadata": {},
"source": [
"### Topics and infrastructure\n",
"\n",
"Key infrastructure concepts that we will encounter in this lecture are:\n",
"\n",
"- inverse demand curves \n",
"- inverse supply curves \n",
"- consumer surplus \n",
"- producer surplus \n",
"- integration \n",
"- social welfare as the sum of consumer and producer surpluses \n",
"- the relationship between equilibrium quantity and social welfare optimum \n",
"\n",
"\n",
"In our exposition we will use the following Python imports."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40bd4c72",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from collections import namedtuple"
]
},
{
"cell_type": "markdown",
"id": "f2cb8ee6",
"metadata": {},
"source": [
"## Consumer surplus\n",
"\n",
"Before we look at the model of supply and demand, it will be helpful to have some background on (a) consumer and producer surpluses and (b) integration.\n",
"\n",
"(If you are comfortable with both topics you can jump to the [next section](#integration).)"
]
},
{
"cell_type": "markdown",
"id": "8ea3f415",
"metadata": {},
"source": [
"### A discrete example\n",
"\n",
"Regarding consumer surplus, suppose that we have a single good and 10 consumers.\n",
"\n",
"These 10 consumers have different preferences; in particular, the amount they would be willing to pay for one unit of the good differs.\n",
"\n",
"Suppose that the willingness to pay for each of the 10 consumers is as follows:\n",
"\n",
"|consumer|1|2|3|4|5|6|7|8|9|10|\n",
"|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|\n",
"|willing to pay|98|72|41|38|29|21|17|12|11|10|\n",
"(We have ordered consumers by willingness to pay, in descending order.)\n",
"\n",
"If $ p $ is the price of the good and $ w_i $ is the amount that consumer $ i $ is willing to pay, then $ i $ buys when $ w_i \\geq p $.\n",
"\n",
">**Note**\n",
">\n",
">If $ p=w_i $ the consumer is indifferent between buying and not buying; we arbitrarily assume that they buy.\n",
"\n",
"The **consumer surplus** of the $ i $-th consumer is $ \\max\\{w_i - p, 0\\} $\n",
"\n",
"- if $ w_i \\geq p $, then the consumer buys and gets surplus $ w_i - p $ \n",
"- if $ w_i < p $, then the consumer does not buy and gets surplus $ 0 $ \n",
"\n",
"\n",
"For example, if the price is $ p=40 $, then consumer 1 gets surplus $ 98-40=58 $.\n",
"\n",
"The bar graph below shows the surplus of each consumer when $ p=25 $.\n",
"\n",
"The total height of each bar $ i $ is willingness to pay by consumer $ i $.\n",
"\n",
"The orange portion of some of the bars shows consumer surplus."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "041bff18",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"consumers = range(1, 11) # consumers 1,..., 10\n",
"# willingness to pay for each consumer\n",
"wtp = (98, 72, 41, 38, 29, 21, 17, 12, 11, 10)\n",
"price = 25\n",
"ax.bar(consumers, wtp, label=\"consumer surplus\", color=\"darkorange\", alpha=0.8)\n",
"ax.plot((0, 12), (price, price), lw=2, label=\"price $p$\")\n",
"ax.bar(consumers, [min(w, price) for w in wtp], color=\"black\", alpha=0.6)\n",
"ax.set_xlim(0, 12)\n",
"ax.set_xticks(consumers)\n",
"ax.set_ylabel(\"willingness to pay, price\")\n",
"ax.set_xlabel(\"consumer, quantity\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "7e1857cd",
"metadata": {},
"source": [
"The total consumer surplus in this market is\n",
"\n",
"$$\n",
"\\sum_{i=1}^{10} \\max\\{w_i - p, 0\\}\n",
"= \\sum_{w_i \\geq p} (w_i - p)\n",
"$$\n",
"\n",
"Since consumer surplus $ \\max\\{w_i-p,0\\} $ of consumer $ i $ is a measure of her gains from trade (i.e., extent to which the good is valued over and above the amount the consumer had to pay), it is reasonable to consider total consumer surplus as a measurement of consumer welfare.\n",
"\n",
"Later we will pursue this idea further, considering how different prices lead to different welfare outcomes for consumers and producers."
]
},
{
"cell_type": "markdown",
"id": "a9ab0858",
"metadata": {},
"source": [
"### A comment on quantity.\n",
"\n",
"Notice that in the figure, the horizontal axis is labeled “consumer, quantity”.\n",
"\n",
"We have added “quantity” here because we can read the number of units sold from this axis, assuming for now that there are sellers who are willing to sell as many units as the consumers demand, given the current market price $ p $.\n",
"\n",
"In this example, consumers 1 to 5 buy, and the quantity sold is 5.\n",
"\n",
"Below we drop the assumption that sellers will provide any amount at a given price and study how this changes outcomes."
]
},
{
"cell_type": "markdown",
"id": "b9c7c878",
"metadata": {},
"source": [
"### A continuous approximation\n",
"\n",
"It is often convenient to assume that there is a “very large number” of consumers, so that willingness to pay becomes a continuous curve.\n",
"\n",
"As before, the vertical axis measures willingness to pay, while the horizontal axis measures quantity.\n",
"\n",
"This kind of curve is called an **inverse demand curve**\n",
"\n",
"An example is provided below, showing both an inverse demand curve and a set price.\n",
"\n",
"The inverse demand curve is given by\n",
"\n",
"$$\n",
"p = 100 e^{-q}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f278aea1",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def inverse_demand(q):\n",
" return 100 * np.exp(- q)\n",
"\n",
"# build a grid to evaluate the function at different values of q\n",
"q_min, q_max = 0, 5\n",
"q_grid = np.linspace(q_min, q_max, 1000)\n",
"\n",
"# plot the inverse demand curve\n",
"fig, ax = plt.subplots()\n",
"ax.plot((q_min, q_max), (price, price), lw=2, label=\"price\")\n",
"ax.plot(q_grid, inverse_demand(q_grid), \n",
" color=\"orange\", label=\"inverse demand curve\")\n",
"ax.set_ylabel(\"willingness to pay, price\")\n",
"ax.set_xlabel(\"quantity\")\n",
"ax.set_xlim(q_min, q_max)\n",
"ax.set_ylim(0, 110)\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "2e8a9b34",
"metadata": {},
"source": [
"Reasoning by analogy with the discrete case, the area under the demand curve and above the price is called the **consumer surplus**, and is a measure of total gains from trade on the part of consumers.\n",
"\n",
"The consumer surplus is shaded in the figure below."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f952125",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"# solve for the value of q where demand meets price\n",
"q_star = np.log(100) - np.log(price)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot((q_min, q_max), (price, price), lw=2, label=\"price\")\n",
"ax.plot(q_grid, inverse_demand(q_grid), \n",
" color=\"orange\", label=\"inverse demand curve\")\n",
"small_grid = np.linspace(0, q_star, 500)\n",
"ax.fill_between(small_grid, np.full(len(small_grid), price),\n",
" inverse_demand(small_grid), color=\"orange\",\n",
" alpha=0.5, label=\"consumer surplus\")\n",
"ax.vlines(q_star, 0, price, ls=\"--\")\n",
"ax.set_ylabel(\"willingness to pay, price\")\n",
"ax.set_xlabel(\"quantity\")\n",
"ax.set_xlim(q_min, q_max)\n",
"ax.set_ylim(0, 110)\n",
"ax.text(q_star, -10, \"$q^*$\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "97935a5b",
"metadata": {},
"source": [
"The value $ q^* $ is where the inverse demand curve meets price."
]
},
{
"cell_type": "markdown",
"id": "a4df539f",
"metadata": {},
"source": [
"## Producer surplus\n",
"\n",
"Having discussed demand, let’s now switch over to the supply side of the market."
]
},
{
"cell_type": "markdown",
"id": "caf893d8",
"metadata": {},
"source": [
"### The discrete case\n",
"\n",
"The figure below shows the price at which a collection of producers, also numbered 1 to 10, are willing to sell one unit of the good in question"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00e6a017",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"producers = range(1, 11) # producers 1,..., 10\n",
"# willingness to sell for each producer\n",
"wts = (5, 8, 17, 22, 35, 39, 46, 57, 88, 91)\n",
"price = 25\n",
"ax.bar(producers, wts, label=\"willingness to sell\", color=\"green\", alpha=0.5)\n",
"ax.set_xlim(0, 12)\n",
"ax.set_xticks(producers)\n",
"ax.set_ylabel(\"willingness to sell\")\n",
"ax.set_xlabel(\"producer\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "25fd1855",
"metadata": {},
"source": [
"Let $ v_i $ be the price at which producer $ i $ is willing to sell the good.\n",
"\n",
"When the price is $ p $, producer surplus for producer $ i $ is $ \\max\\{p - v_i, 0\\} $.\n",
"\n",
"For example, a producer willing to sell at \\$10 and selling at price \\$20 makes a surplus of \\$10.\n",
"\n",
"Total producer surplus is given by\n",
"\n",
"$$\n",
"\\sum_{i=1}^{10} \\max\\{p - v_i, 0\\}\n",
"= \\sum_{p \\geq v_i} (p - v_i)\n",
"$$\n",
"\n",
"As for the consumer case, it can be helpful for analysis if we approximate producer willingness to sell into a continuous curve.\n",
"\n",
"This curve is called the **inverse supply curve**\n",
"\n",
"We show an example below where the inverse supply curve is\n",
"\n",
"$$\n",
"p = 2 q^2\n",
"$$\n",
"\n",
"The shaded area is the total producer surplus in this continuous model."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1acdf44",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def inverse_supply(q):\n",
" return 2 * q**2\n",
"\n",
"# solve for the value of q where supply meets price\n",
"q_star = (price / 2)**(1/2)\n",
"\n",
"# plot the inverse supply curve\n",
"fig, ax = plt.subplots()\n",
"ax.plot((q_min, q_max), (price, price), lw=2, label=\"price\")\n",
"ax.plot(q_grid, inverse_supply(q_grid), \n",
" color=\"green\", label=\"inverse supply curve\")\n",
"small_grid = np.linspace(0, q_star, 500)\n",
"ax.fill_between(small_grid, inverse_supply(small_grid), \n",
" np.full(len(small_grid), price), \n",
" color=\"green\",\n",
" alpha=0.5, label=\"producer surplus\")\n",
"ax.vlines(q_star, 0, price, ls=\"--\")\n",
"ax.set_ylabel(\"willingness to sell, price\")\n",
"ax.set_xlabel(\"quantity\")\n",
"ax.set_xlim(q_min, q_max)\n",
"ax.set_ylim(0, 60)\n",
"ax.text(q_star, -10, \"$q^*$\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "c5b4a67c",
"metadata": {},
"source": [
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "6fc29f67",
"metadata": {},
"source": [
"## Integration\n",
"\n",
"How can we calculate the consumer and producer surplus in the continuous case?\n",
"\n",
"The short answer is: by using [integration](https://en.wikipedia.org/wiki/Integral).\n",
"\n",
"Some readers will already be familiar with the basics of integration.\n",
"\n",
"For those who are not, here is a quick introduction.\n",
"\n",
"In general, for a function $ f $, the **integral** of $ f $ over the interval $ [a, b] $ is the area under the curve $ f $ between $ a $ and $ b $.\n",
"\n",
"This value is written as $ \\int_a^b f(x) \\mathrm{d} x $ and illustrated in the figure below when $ f(x) = \\cos(x/2) + 1 $."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee62401b",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def f(x):\n",
" return np.cos(x/2) + 1\n",
"\n",
"xmin, xmax = 0, 5\n",
"a, b = 1, 3\n",
"x_grid = np.linspace(xmin, xmax, 1000)\n",
"ab_grid = np.linspace(a, b, 400)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(x_grid, f(x_grid), label=\"$f$\", color=\"k\")\n",
"ax.fill_between(ab_grid, [0] * len(ab_grid), f(ab_grid), \n",
" label=\"$\\int_a^b f(x) dx$\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "090f5ce6",
"metadata": {},
"source": [
"There are many rules for calculating integrals, with different rules applying to different choices of $ f $.\n",
"\n",
"Many of these rules relate to one of the most beautiful and powerful results in all of mathematics: the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus).\n",
"\n",
"We will not try to cover these ideas here, partly because the subject is too big, and partly because you only need to know one rule for this lecture, stated below.\n",
"\n",
"If $ f(x) = c + \\mathrm{d} x $, then\n",
"\n",
"$$\n",
"\\int_a^b f(x) \\mathrm{d} x = c (b - a) + \\frac{d}{2}(b^2 - a^2)\n",
"$$\n",
"\n",
"In fact this rule is so simple that it can be calculated from elementary geometry – you might like to try by graphing $ f $ and calculating the area under the curve between $ a $ and $ b $.\n",
"\n",
"We use this rule repeatedly in what follows."
]
},
{
"cell_type": "markdown",
"id": "57933951",
"metadata": {},
"source": [
"## Supply and demand\n",
"\n",
"Let’s now put supply and demand together.\n",
"\n",
"This leads us to the all important notion of market equilibrium, and from there onto a discussion of equilibria and welfare.\n",
"\n",
"For most of this discussion, we’ll assume that inverse demand and supply curves are **affine** functions of quantity.\n",
"\n",
">**Note**\n",
">\n",
">“Affine” means “linear plus a constant” and [here](https://math.stackexchange.com/questions/275310/what-is-the-difference-between-linear-and-affine-function) is a nice discussion about it.\n",
"\n",
"We’ll also assume affine inverse supply and demand functions when we study models with multiple consumption goods in our [subsequent lecture](https://intro.quantecon.org/supply_demand_multiple_goods.html).\n",
"\n",
"We do this in order to simplify the exposition and enable us to use just a few tools from linear algebra, namely, matrix multiplication and matrix inversion.\n",
"\n",
"We study a market for a single good in which buyers and sellers exchange a quantity $ q $ for a price $ p $.\n",
"\n",
"Quantity $ q $ and price $ p $ are both scalars.\n",
"\n",
"We assume that inverse demand and supply curves for the good are:\n",
"\n",
"$$\n",
"p = d_0 - d_1 q, \\quad d_0, d_1 > 0\n",
"$$\n",
"\n",
"$$\n",
"p = s_0 + s_1 q , \\quad s_0, s_1 > 0\n",
"$$\n",
"\n",
"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.\n",
"\n",
"We can use a [namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) to store the parameters for our single good market."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76a9f69e",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"Market = namedtuple('Market', ['d_0', # demand intercept\n",
" 'd_1', # demand slope\n",
" 's_0', # supply intercept\n",
" 's_1'] # supply slope\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "e7426b23",
"metadata": {},
"source": [
"The function below creates an instance of a Market namedtuple with default values."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2eb41c2",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def create_market(d_0=1.0, d_1=0.6, s_0=0.1, s_1=0.4):\n",
" return Market(d_0=d_0, d_1=d_1, s_0=s_0, s_1=s_1)"
]
},
{
"cell_type": "markdown",
"id": "40734c2b",
"metadata": {},
"source": [
"This `market` can then be used by our `inverse_demand` and `inverse_supply` functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5059a65",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def inverse_demand(q, model):\n",
" return model.d_0 - model.d_1 * q\n",
"\n",
"def inverse_supply(q, model):\n",
" return model.s_0 + model.s_1 * q"
]
},
{
"cell_type": "markdown",
"id": "cc913374",
"metadata": {},
"source": [
"Here is a plot of these two functions using `market`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4abcd038",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"market = create_market()\n",
"\n",
"grid_min, grid_max, grid_size = 0, 1.5, 200\n",
"q_grid = np.linspace(grid_min, grid_max, grid_size)\n",
"supply_curve = inverse_supply(q_grid, market)\n",
"demand_curve = inverse_demand(q_grid, market)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(q_grid, supply_curve, label='supply', color='green')\n",
"ax.plot(q_grid, demand_curve, label='demand', color='orange')\n",
"ax.legend(loc='upper center', frameon=False)\n",
"ax.set_ylim(0, 1.2)\n",
"ax.set_xticks((0, 1))\n",
"ax.set_yticks((0, 1))\n",
"ax.set_xlabel('quantity')\n",
"ax.set_ylabel('price')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "dad71e23",
"metadata": {},
"source": [
"In the above graph, an **equilibrium** price-quantity pair occurs at the intersection of the supply and demand curves."
]
},
{
"cell_type": "markdown",
"id": "59083112",
"metadata": {},
"source": [
"### Consumer surplus\n",
"\n",
"Let a quantity $ q $ be given and let $ p := d_0 - d_1 q $ be the\n",
"corresponding price on the inverse demand curve.\n",
"\n",
"We define **consumer surplus** $ S_c(q) $ as the area under an inverse demand\n",
"curve minus $ p q $:\n",
"\n",
"\n",
"\n",
"$$\n",
"S_c(q) := \n",
"\\int_0^{q} (d_0 - d_1 x) \\mathrm{d} x - p q \\tag{7.1}\n",
"$$\n",
"\n",
"The next figure illustrates"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52c3f012",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"q = 1.25\n",
"p = inverse_demand(q, market)\n",
"ps = np.ones_like(q_grid) * p\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(q_grid, demand_curve, label='demand', color='orange')\n",
"ax.fill_between(q_grid[q_grid <= q],\n",
" demand_curve[q_grid <= q],\n",
" ps[q_grid <= q],\n",
" label='consumer surplus',\n",
" color=\"orange\", \n",
" alpha=0.5)\n",
"ax.vlines(q, 0, p, linestyle=\"dashed\", color='black', alpha=0.7)\n",
"ax.hlines(p, 0, q, linestyle=\"dashed\", color='black', alpha=0.7)\n",
"\n",
"ax.legend(loc='upper center', frameon=False)\n",
"ax.set_ylim(0, 1.2)\n",
"ax.set_xticks((q,))\n",
"ax.set_xticklabels((\"$q$\",))\n",
"ax.set_yticks((p,))\n",
"ax.set_yticklabels((\"$p$\",))\n",
"ax.set_xlabel('quantity')\n",
"ax.set_ylabel('price')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "1e3042a5",
"metadata": {},
"source": [
"Consumer surplus provides a measure of total consumer welfare at quantity $ q $.\n",
"\n",
"The idea is that the inverse demand curve $ d_0 - d_1 q $ shows a consumer’s willingness to\n",
"pay for an additional increment of the good at a given quantity $ q $.\n",
"\n",
"The difference between willingness to pay and the actual price is consumer surplus.\n",
"\n",
"The value $ S_c(q) $ is the “sum” (i.e., integral) of these surpluses when the total\n",
"quantity purchased is $ q $ and the purchase price is $ p $.\n",
"\n",
"Evaluating the integral in the definition of consumer surplus [(7.1)](#equation-eq-cstm-spls) gives\n",
"\n",
"$$\n",
"S_c(q) \n",
"= d_0 q - \\frac{1}{2} d_1 q^2 - p q\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "c7c850bf",
"metadata": {},
"source": [
"### Producer surplus\n",
"\n",
"Let a quantity $ q $ be given and let $ p := s_0 + s_1 q $ be the\n",
"corresponding price on the inverse supply curve.\n",
"\n",
"We define **producer surplus** as $ p q $ minus the area under an inverse supply curve\n",
"\n",
"\n",
"\n",
"$$\n",
"S_p(q) \n",
":= p q - \\int_0^q (s_0 + s_1 x) \\mathrm{d} x \\tag{7.2}\n",
"$$\n",
"\n",
"The next figure illustrates"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91d1e3ee",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"q = 0.75\n",
"p = inverse_supply(q, market)\n",
"ps = np.ones_like(q_grid) * p\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(q_grid, supply_curve, label='supply', color='green')\n",
"ax.fill_between(q_grid[q_grid <= q],\n",
" supply_curve[q_grid <= q],\n",
" ps[q_grid <= q],\n",
" label='producer surplus',\n",
" color=\"green\",\n",
" alpha=0.5)\n",
"ax.vlines(q, 0, p, linestyle=\"dashed\", color='black', alpha=0.7)\n",
"ax.hlines(p, 0, q, linestyle=\"dashed\", color='black', alpha=0.7)\n",
"\n",
"ax.legend(loc='upper center', frameon=False)\n",
"ax.set_ylim(0, 1.2)\n",
"ax.set_xticks((q,))\n",
"ax.set_xticklabels((\"$q$\",))\n",
"ax.set_yticks((p,))\n",
"ax.set_yticklabels((\"$p$\",))\n",
"ax.set_xlabel('quantity')\n",
"ax.set_ylabel('price')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "0c67dd87",
"metadata": {},
"source": [
"Producer surplus measures total producer welfare at quantity $ q $\n",
"\n",
"The idea is similar to that of consumer surplus.\n",
"\n",
"The inverse supply curve $ s_0 + s_1 q $ shows the price at which producers are\n",
"prepared to sell, given quantity $ q $.\n",
"\n",
"The difference between willingness to sell and the actual price is producer surplus.\n",
"\n",
"The value $ S_p(q) $ is the integral of these surpluses.\n",
"\n",
"Evaluating the integral in the definition of producer surplus [(7.2)](#equation-eq-pdcr-spls) gives\n",
"\n",
"$$\n",
"S_p(q) = pq - s_0 q - \\frac{1}{2} s_1 q^2\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "a105c62b",
"metadata": {},
"source": [
"### Social welfare\n",
"\n",
"Sometimes economists measure social welfare by a **welfare criterion** that\n",
"equals consumer surplus plus producer surplus, assuming that consumers and\n",
"producers pay the same price:\n",
"\n",
"$$\n",
"W(q)\n",
"= \\int_0^q (d_0 - d_1 x) dx - \\int_0^q (s_0 + s_1 x) \\mathrm{d} x\n",
"$$\n",
"\n",
"Evaluating the integrals gives\n",
"\n",
"$$\n",
"W(q) = (d_0 - s_0) q - \\frac{1}{2} (d_1 + s_1) q^2\n",
"$$\n",
"\n",
"Here is a Python function that evaluates this social welfare at a given\n",
"quantity $ q $ and a fixed set of parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ea947b3",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def W(q, market):\n",
" # Compute and return welfare\n",
" return (market.d_0 - market.s_0) * q - 0.5 * (market.d_1 + market.s_1) * q**2"
]
},
{
"cell_type": "markdown",
"id": "94cbf334",
"metadata": {},
"source": [
"The next figure plots welfare as a function of $ q $."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a4febf8",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"q_vals = np.linspace(0, 1.78, 200)\n",
"fig, ax = plt.subplots()\n",
"ax.plot(q_vals, W(q_vals, market), label='welfare', color='brown')\n",
"ax.legend(frameon=False)\n",
"ax.set_xlabel('quantity')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "c17cf8b3",
"metadata": {},
"source": [
"Let’s now give a social planner the task of maximizing social welfare.\n",
"\n",
"To compute a quantity that maximizes the welfare criterion, we differentiate\n",
"$ W $ with respect to $ q $ and then set the derivative to zero.\n",
"\n",
"$$\n",
"\\frac{\\mathrm{d} W(q)}{\\mathrm{d} q} = d_0 - s_0 - (d_1 + s_1) q = 0\n",
"$$\n",
"\n",
"Solving for $ q $ yields\n",
"\n",
"\n",
"\n",
"$$\n",
"q = \\frac{ d_0 - s_0}{s_1 + d_1} \\tag{7.3}\n",
"$$\n",
"\n",
"Let’s remember the quantity $ q $ given by equation [(7.3)](#equation-eq-old1) that a social planner would choose to maximize consumer surplus plus producer surplus.\n",
"\n",
"We’ll compare it to the quantity that emerges in a competitive equilibrium that equates supply to demand."
]
},
{
"cell_type": "markdown",
"id": "d8163ce2",
"metadata": {},
"source": [
"### Competitive equilibrium\n",
"\n",
"Instead of equating quantities supplied and demanded, we can accomplish the\n",
"same thing by equating demand price to supply price:\n",
"\n",
"$$\n",
"p = d_0 - d_1 q = s_0 + s_1 q\n",
"$$\n",
"\n",
"If we solve the equation defined by the second equality in the above line for\n",
"$ q $, we obtain\n",
"\n",
"\n",
"\n",
"$$\n",
"q = \\frac{ d_0 - s_0}{s_1 + d_1} \\tag{7.4}\n",
"$$\n",
"\n",
"This is the competitive equilibrium quantity.\n",
"\n",
"Observe that the equilibrium quantity equals the same $ q $ given by equation [(7.3)](#equation-eq-old1).\n",
"\n",
"The outcome that the quantity determined by equation [(7.3)](#equation-eq-old1) equates\n",
"supply to demand brings us a **key finding:**\n",
"\n",
"- a competitive equilibrium quantity maximizes our welfare criterion \n",
"\n",
"\n",
"This is a version of the [first fundamental welfare theorem](https://en.wikipedia.org/wiki/Fundamental_theorems_of_welfare_economics),\n",
"\n",
"It also brings a useful **competitive equilibrium computation strategy:**\n",
"\n",
"- 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 "
]
},
{
"cell_type": "markdown",
"id": "b029748f",
"metadata": {},
"source": [
"## Generalizations\n",
"\n",
"In a [later lecture](https://intro.quantecon.org/supply_demand_multiple_goods.html), we’ll derive\n",
"generalizations of the above demand and supply curves from other objects.\n",
"\n",
"Our generalizations will extend the preceding analysis of a market for a single good to the analysis of $ n $ simultaneous markets in $ n $ goods.\n",
"\n",
"In addition\n",
"\n",
"- we’ll derive **demand curves** from a consumer problem that maximizes a\n",
" **utility function** subject to a **budget constraint**. \n",
"- we’ll derive **supply curves** from the problem of a producer who is price\n",
" taker and maximizes his profits minus total costs that are described by a **cost function**. "
]
},
{
"cell_type": "markdown",
"id": "ea89efd8",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"Suppose now that the inverse demand and supply curves are modified to take the\n",
"form\n",
"\n",
"$$\n",
"p = i_d(q) := d_0 - d_1 q^{0.6}\n",
"$$\n",
"\n",
"$$\n",
"p = i_s(q) := s_0 + s_1 q^{1.8}\n",
"$$\n",
"\n",
"All parameters are positive, as before."
]
},
{
"cell_type": "markdown",
"id": "b0554fc8",
"metadata": {},
"source": [
"## Exercise 7.1\n",
"\n",
"Use the same `Market` namedtuple that holds the parameter values as before but\n",
"make new `inverse_demand` and `inverse_supply` functions to match these new definitions.\n",
"\n",
"Then plot the inverse demand and supply curves $ i_d $ and $ i_s $."
]
},
{
"cell_type": "markdown",
"id": "d7dc494c",
"metadata": {},
"source": [
"## Solution to[ Exercise 7.1](https://intro.quantecon.org/#isd_ex1)\n",
"\n",
"Let’s update the `inverse_demand` and `inverse_supply` functions, as defined above."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f5287c5",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def inverse_demand(q, model):\n",
" return model.d_0 - model.d_1 * q**0.6\n",
"\n",
"def inverse_supply(q, model):\n",
" return model.s_0 + model.s_1 * q**1.8"
]
},
{
"cell_type": "markdown",
"id": "620ff298",
"metadata": {},
"source": [
"Here is a plot of inverse supply and demand."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f73fdb3",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"grid_min, grid_max, grid_size = 0, 1.5, 200\n",
"q_grid = np.linspace(grid_min, grid_max, grid_size)\n",
"market = create_market()\n",
"supply_curve = inverse_supply(q_grid, market)\n",
"demand_curve = inverse_demand(q_grid, market)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(q_grid, supply_curve, label='supply', color='green')\n",
"ax.plot(q_grid, demand_curve, label='demand', color='orange')\n",
"ax.legend(loc='upper center', frameon=False)\n",
"ax.set_ylim(0, 1.2)\n",
"ax.set_xticks((0, 1))\n",
"ax.set_yticks((0, 1))\n",
"ax.set_xlabel('quantity')\n",
"ax.set_ylabel('price')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "d6858a2c",
"metadata": {},
"source": [
"## Exercise 7.2\n",
"\n",
"As before, consumer surplus at $ q $ is the area under the demand curve minus\n",
"price times quantity:\n",
"\n",
"$$\n",
"S_c(q) = \\int_0^{q} i_d(x) dx - p q\n",
"$$\n",
"\n",
"Here $ p $ is set to $ i_d(q) $\n",
"\n",
"Producer surplus is price times quantity minus the area under the inverse\n",
"supply curve:\n",
"\n",
"$$\n",
"S_p(q) \n",
"= p q - \\int_0^q i_s(x) \\mathrm{d} x\n",
"$$\n",
"\n",
"Here $ p $ is set to $ i_s(q) $.\n",
"\n",
"Social welfare is the sum of consumer and producer surplus under the\n",
"assumption that the price is the same for buyers and sellers:\n",
"\n",
"$$\n",
"W(q)\n",
"= \\int_0^q i_d(x) dx - \\int_0^q i_s(x) \\mathrm{d} x\n",
"$$\n",
"\n",
"Solve the integrals and write a function to compute this quantity numerically\n",
"at given $ q $.\n",
"\n",
"Plot welfare as a function of $ q $."
]
},
{
"cell_type": "markdown",
"id": "7618229b",
"metadata": {},
"source": [
"## Solution to[ Exercise 7.2](https://intro.quantecon.org/#isd_ex2)\n",
"\n",
"Solving the integrals gives\n",
"\n",
"$$\n",
"W(q) \n",
"= d_0 q - \\frac{d_1 q^{1.6}}{1.6}\n",
" - \\left( s_0 q + \\frac{s_1 q^{2.8}}{2.8} \\right)\n",
"$$\n",
"\n",
"Here’s a Python function that computes this value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a75bec50",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def W(q, market):\n",
" # Compute and return welfare\n",
" S_c = market.d_0 * q - market.d_1 * q**1.6 / 1.6\n",
" S_p = market.s_0 * q + market.s_1 * q**2.8 / 2.8\n",
" return S_c - S_p"
]
},
{
"cell_type": "markdown",
"id": "1a21b5e8",
"metadata": {},
"source": [
"The next figure plots welfare as a function of $ q $."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89f01a03",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"ax.plot(q_vals, W(q_vals, market), label='welfare', color='brown')\n",
"ax.legend(frameon=False)\n",
"ax.set_xlabel('quantity')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "c23ab4bb",
"metadata": {},
"source": [
"## Exercise 7.3\n",
"\n",
"Due to non-linearities, the new welfare function is not easy to maximize with\n",
"pencil and paper.\n",
"\n",
"Maximize it using `scipy.optimize.minimize_scalar` instead.\n",
"\n",
"Our [SciPy](https://python-programming.quantecon.org/scipy.html) lecture has\n",
"a section on [Optimization](https://python-programming.quantecon.org/scipy.html#optimization)\n",
"is a useful resource to find out more."
]
},
{
"cell_type": "markdown",
"id": "71b5f54b",
"metadata": {},
"source": [
"## Solution to[ Exercise 7.3](https://intro.quantecon.org/#isd_ex3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e4a3d33",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"from scipy.optimize import minimize_scalar\n",
"\n",
"def objective(q):\n",
" return -W(q, market)\n",
"\n",
"result = minimize_scalar(objective, bounds=(0, 10))\n",
"print(result.message)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0464afa2",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"maximizing_q = result.x\n",
"print(f\"{maximizing_q: .5f}\")"
]
},
{
"cell_type": "markdown",
"id": "3eafb1f3",
"metadata": {},
"source": [
"## Exercise 7.4\n",
"\n",
"Now compute the equilibrium quantity by finding the price that equates supply\n",
"and demand.\n",
"\n",
"You can do this numerically by finding the root of the excess demand function\n",
"\n",
"$$\n",
"e_d(q) := i_d(q) - i_s(q)\n",
"$$\n",
"\n",
"You can use `scipy.optimize.newton` to compute the root.\n",
"\n",
"Our [SciPy](https://python-programming.quantecon.org/scipy.html) lecture has\n",
"a section on [Roots and Fixed Points](https://python-programming.quantecon.org/scipy.html#roots-and-fixed-points)\n",
"is a useful resource to find out more.\n",
"\n",
"Initialize `newton` with a starting guess somewhere close to 1.0.\n",
"\n",
"(Similar initial conditions will give the same result.)\n",
"\n",
"You should find that the equilibrium price agrees with the welfare maximizing\n",
"price, in line with the first fundamental welfare theorem."
]
},
{
"cell_type": "markdown",
"id": "3587bae0",
"metadata": {},
"source": [
"## Solution to[ Exercise 7.4](https://intro.quantecon.org/#isd_ex4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01637e4d",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"from scipy.optimize import newton\n",
"\n",
"def excess_demand(q):\n",
" return inverse_demand(q, market) - inverse_supply(q, market)\n",
"\n",
"equilibrium_q = newton(excess_demand, 0.99)\n",
"print(f\"{equilibrium_q: .5f}\")"
]
}
],
"metadata": {
"date": 1722558194.6629465,
"filename": "intro_supply_demand.md",
"kernelspec": {
"display_name": "Python",
"language": "python3",
"name": "python3"
},
"title": "Introduction to Supply and Demand"
},
"nbformat": 4,
"nbformat_minor": 5
}