{
"cells": [
{
"cell_type": "markdown",
"id": "4ef0de87",
"metadata": {},
"source": [
"# Commodity Prices"
]
},
{
"cell_type": "markdown",
"id": "f5d48ed7",
"metadata": {},
"source": [
"## Outline\n",
"\n",
"For more than half of all countries around the globe, [commodities](https://en.wikipedia.org/wiki/Commodity) account for [the majority of total exports](https://unctad.org/publication/commodities-and-development-report-2019).\n",
"\n",
"Examples of commodities include copper, diamonds, iron ore, lithium, cotton\n",
"and coffee beans.\n",
"\n",
"In this lecture we give an introduction to the theory of commodity prices.\n",
"\n",
"The lecture is quite advanced relative to other lectures in this series.\n",
"\n",
"We need to compute an equilibrium, and that equilibrium is described by a\n",
"price function.\n",
"\n",
"We will solve an equation where the price function is the unknown.\n",
"\n",
"This is harder than solving an equation for an unknown number, or vector.\n",
"\n",
"The lecture will discuss one way to solve a “functional equation” for an unknown\n",
"function\n",
"\n",
"For this lecture we need the `yfinance` library."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5534b5eb",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"!pip install yfinance"
]
},
{
"cell_type": "markdown",
"id": "b3f50b5f",
"metadata": {},
"source": [
"We will use the following imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a39658e",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import yfinance as yf\n",
"import matplotlib.pyplot as plt\n",
"from scipy.interpolate import interp1d\n",
"from scipy.optimize import brentq\n",
"from scipy.stats import beta"
]
},
{
"cell_type": "markdown",
"id": "5b901ba2",
"metadata": {},
"source": [
"## Data\n",
"\n",
"The figure below shows the price of cotton in USD since the start of 2016."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9676dda4",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"s = yf.download('CT=F', '2016-1-1', '2023-4-1')['Adj Close']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55997b32",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"\n",
"ax.plot(s, marker='o', alpha=0.5, ms=1)\n",
"ax.set_ylabel('price', fontsize=12)\n",
"ax.set_xlabel('date', fontsize=12)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "b53ecabf",
"metadata": {},
"source": [
"The figure shows surprisingly large movements in the price of cotton.\n",
"\n",
"What causes these movements?\n",
"\n",
"In general, prices depend on the choices and actions of\n",
"\n",
"1. suppliers, \n",
"1. consumers, and \n",
"1. speculators. \n",
"\n",
"\n",
"Our focus will be on the interaction between these parties.\n",
"\n",
"We will connect them together in a dynamic model of supply and demand, called\n",
"the *competitive storage model*.\n",
"\n",
"This model was developed by\n",
"[[Samuelson, 1971](https://intro.quantecon.org/zreferences.html#id17)],\n",
"[[Wright and Williams, 1982](https://intro.quantecon.org/zreferences.html#id16)], [[Scheinkman and Schechtman, 1983](https://intro.quantecon.org/zreferences.html#id15)],\n",
"[[Deaton and Laroque, 1992](https://intro.quantecon.org/zreferences.html#id14)], [[Deaton and Laroque, 1996](https://intro.quantecon.org/zreferences.html#id13)], and\n",
"[[Chambers and Bailey, 1996](https://intro.quantecon.org/zreferences.html#id12)]."
]
},
{
"cell_type": "markdown",
"id": "8d55e4fa",
"metadata": {},
"source": [
"## The competitive storage model\n",
"\n",
"In the competitive storage model, commodities are assets that\n",
"\n",
"1. can be traded by speculators and \n",
"1. have intrinsic value to consumers. \n",
"\n",
"\n",
"Total demand is the sum of consumer demand and demand by speculators.\n",
"\n",
"Supply is exogenous, depending on “harvests”.\n",
"\n",
">**Note**\n",
">\n",
">These days, goods such as basic computer chips and integrated circuits are\n",
"often treated as commodities in financial markets, being highly standardized,\n",
"and, for these kinds of commodities, the word “harvest” is not\n",
"appropriate.\n",
"\n",
"Nonetheless, we maintain it for simplicity.\n",
"\n",
"The equilibrium price is determined competitively.\n",
"\n",
"It is a function of the current state (which determines\n",
"current harvests and predicts future harvests)."
]
},
{
"cell_type": "markdown",
"id": "7a18adb1",
"metadata": {},
"source": [
"## The model\n",
"\n",
"Consider a market for a single commodity, whose price is given at $ t $ by\n",
"$ p_t $.\n",
"\n",
"The harvest of the commodity at time $ t $ is $ Z_t $.\n",
"\n",
"We assume that the sequence $ \\{ Z_t \\}_{t \\geq 1} $ is IID with common\n",
"density function $ \\phi $.\n",
"\n",
"Speculators can store the commodity between periods, with $ I_t $ units\n",
"purchased in the current period yielding $ \\alpha I_t $ units in the next.\n",
"\n",
"Here $ \\alpha \\in (0,1) $ is a depreciation rate for the commodity.\n",
"\n",
"For simplicity, the risk free interest rate is taken to be\n",
"zero, so expected profit on purchasing $ I_t $ units is\n",
"\n",
"$$\n",
"\\mathbb{E}_t \\, p_{t+1} \\cdot \\alpha I_t - p_t I_t\n",
" = (\\alpha \\mathbb{E}_t \\, p_{t+1} - p_t) I_t\n",
"$$\n",
"\n",
"Here $ \\mathbb{E}_t \\, p_{t+1} $ is the expectation of $ p_{t+1} $ taken at time\n",
"$ t $."
]
},
{
"cell_type": "markdown",
"id": "8000b475",
"metadata": {},
"source": [
"## Equilibrium\n",
"\n",
"In this section we define the equilibrium and discuss how to compute it."
]
},
{
"cell_type": "markdown",
"id": "3da30d94",
"metadata": {},
"source": [
"### Equilibrium conditions\n",
"\n",
"Speculators are assumed to be risk neutral, which means that they buy the\n",
"commodity whenever expected profits are positive.\n",
"\n",
"As a consequence, if expected profits are positive, then the market is not in\n",
"equilibrium.\n",
"\n",
"Hence, to be in equilibrium, prices must satisfy the “no-arbitrage”\n",
"condition\n",
"\n",
"\n",
"\n",
"$$\n",
"\\alpha \\mathbb{E}_t \\, p_{t+1} - p_t \\leq 0 \\tag{25.1}\n",
"$$\n",
"\n",
"Profit maximization gives the additional condition\n",
"\n",
"\n",
"\n",
"$$\n",
"\\alpha \\mathbb{E}_t \\, p_{t+1} - p_t < 0 \\text{ implies } I_t = 0 \\tag{25.2}\n",
"$$\n",
"\n",
"We also require that the market clears in each period.\n",
"\n",
"We assume that consumers generate demand quantity $ D(p) $ corresponding to\n",
"price $ p $.\n",
"\n",
"Let $ P := D^{-1} $ be the inverse demand function.\n",
"\n",
"Regarding quantities,\n",
"\n",
"- supply is the sum of carryover by speculators and the current harvest \n",
"- demand is the sum of purchases by consumers and purchases by speculators. \n",
"\n",
"\n",
"Mathematically,\n",
"\n",
"- supply $ = X_t = \\alpha I_{t-1} + Z_t $, which takes values in $ S := \\mathbb R_+ $, while \n",
"- demand $ = D(p_t) + I_t $ \n",
"\n",
"\n",
"Thus, the market equilibrium condition is\n",
"\n",
"\n",
"\n",
"$$\n",
"\\alpha I_{t-1} + Z_t = D(p_t) + I_t \\tag{25.3}\n",
"$$\n",
"\n",
"The initial condition $ X_0 \\in S $ is treated as given."
]
},
{
"cell_type": "markdown",
"id": "12f4cd5b",
"metadata": {},
"source": [
"### An equilibrium function\n",
"\n",
"How can we find an equilibrium?\n",
"\n",
"Our path of attack will be to seek a system of prices that depend only on the\n",
"current state.\n",
"\n",
"In other words, we take a function $ p $ on $ S $ and set $ p_t = p(X_t) $ for every $ t $.\n",
"\n",
"Prices and quantities then follow\n",
"\n",
"\n",
"\n",
"$$\n",
"p_t = p(X_t), \\quad I_t = X_t - D(p_t), \\quad X_{t+1} = \\alpha I_t + Z_{t+1} \\tag{25.4}\n",
"$$\n",
"\n",
"We choose $ p $ so that these prices and quantities satisfy the equilibrium\n",
"conditions above.\n",
"\n",
"More precisely, we seek a $ p $ such that [(25.1)](#equation-eq-arbi) and [(25.2)](#equation-eq-pmco) hold for\n",
"the corresponding system [(25.4)](#equation-eq-eosy).\n",
"\n",
"To this end, suppose that there exists a function $ p^* $ on $ S $\n",
"satisfying\n",
"\n",
"\n",
"\n",
"$$\n",
"p^*(x) = \\max\n",
" \\left\\{\n",
" \\alpha \\int_0^\\infty p^*(\\alpha I(x) + z) \\phi(z)dz, P(x)\n",
" \\right\\}\n",
" \\qquad (x \\in S) \\tag{25.5}\n",
"$$\n",
"\n",
"where\n",
"\n",
"\n",
"\n",
"$$\n",
"I(x) := x - D(p^*(x))\n",
" \\qquad (x \\in S) \\tag{25.6}\n",
"$$\n",
"\n",
"It turns out that such a $ p^* $ will suffice, in the sense that [(25.1)](#equation-eq-arbi)\n",
"and [(25.2)](#equation-eq-pmco) hold for the corresponding system [(25.4)](#equation-eq-eosy).\n",
"\n",
"To see this, observe first that\n",
"\n",
"$$\n",
"\\mathbb{E}_t \\, p_{t+1}\n",
" = \\mathbb{E}_t \\, p^*(X_{t+1})\n",
" = \\mathbb{E}_t \\, p^*(\\alpha I(X_t) + Z_{t+1})\n",
" = \\int_0^\\infty p^*(\\alpha I(X_t) + z) \\phi(z)dz\n",
"$$\n",
"\n",
"Thus [(25.1)](#equation-eq-arbi) requires that\n",
"\n",
"$$\n",
"\\alpha \\int_0^\\infty p^*(\\alpha I(X_t) + z) \\phi(z)dz \\leq p^*(X_t)\n",
"$$\n",
"\n",
"This inequality is immediate from [(25.5)](#equation-eq-dopf).\n",
"\n",
"Second, regarding [(25.2)](#equation-eq-pmco), suppose that\n",
"\n",
"$$\n",
"\\alpha \\int_0^\\infty p^*(\\alpha I(X_t) + z) \\phi(z)dz < p^*(X_t)\n",
"$$\n",
"\n",
"Then by [(25.5)](#equation-eq-dopf) we have $ p^*(X_t) = P(X_t) $\n",
"\n",
"But then $ D(p^*(X_t)) = X_t $ and $ I_t = I(X_t) = 0 $.\n",
"\n",
"As a consequence, both [(25.1)](#equation-eq-arbi) and [(25.2)](#equation-eq-pmco) hold.\n",
"\n",
"We have found an equilibrium."
]
},
{
"cell_type": "markdown",
"id": "ba9b1a2b",
"metadata": {},
"source": [
"### Computing the equilibrium\n",
"\n",
"We now know that an equilibrium can be obtained by finding a function $ p^* $\n",
"that satisfies [(25.5)](#equation-eq-dopf).\n",
"\n",
"It can be shown that, under mild conditions there is exactly one function on\n",
"$ S $ satisfying [(25.5)](#equation-eq-dopf).\n",
"\n",
"Moreover, we can compute this function using successive approximation.\n",
"\n",
"This means that we start with a guess of the function and then update it using\n",
"[(25.5)](#equation-eq-dopf).\n",
"\n",
"This generates a sequence of functions $ p_1, p_2, \\ldots $\n",
"\n",
"We continue until this process converges, in the sense that $ p_k $ and\n",
"$ p_{k+1} $ are very close together.\n",
"\n",
"Then we take the final $ p_k $ that we computed as our approximation of $ p^* $.\n",
"\n",
"To implement our update step, it is helpful if we put [(25.5)](#equation-eq-dopf) and\n",
"[(25.6)](#equation-eq-einvf) together.\n",
"\n",
"This leads us to the update rule\n",
"\n",
"\n",
"\n",
"$$\n",
"p_{k+1}(x) = \\max\n",
" \\left\\{\n",
" \\alpha \\int_0^\\infty p_k(\\alpha ( x - D(p_{k+1}(x))) + z) \\phi(z)dz, P(x)\n",
" \\right\\} \\tag{25.7}\n",
"$$\n",
"\n",
"In other words, we take $ p_k $ as given and, at each $ x $, solve for $ q $ in\n",
"\n",
"\n",
"\n",
"$$\n",
"q = \\max\n",
" \\left\\{\n",
" \\alpha \\int_0^\\infty p_k(\\alpha ( x - D(q)) + z) \\phi(z)dz, P(x)\n",
" \\right\\} \\tag{25.8}\n",
"$$\n",
"\n",
"Actually we can’t do this at every $ x $, so instead we do it on a grid of\n",
"points $ x_1, \\ldots, x_n $.\n",
"\n",
"Then we get the corresponding values $ q_1, \\ldots, q_n $.\n",
"\n",
"Then we compute $ p_{k+1} $ as the linear interpolation of\n",
"the values $ q_1, \\ldots, q_n $ over the grid $ x_1, \\ldots, x_n $.\n",
"\n",
"Then we repeat, seeking convergence."
]
},
{
"cell_type": "markdown",
"id": "decb9d5d",
"metadata": {},
"source": [
"## Code\n",
"\n",
"The code below implements this iterative process, starting from $ p_0 = P $.\n",
"\n",
"The distribution $ \\phi $ is set to a shifted Beta distribution (although many\n",
"other choices are possible).\n",
"\n",
"The integral in [(25.8)](#equation-eq-dopf3) is computed via Monte Carlo."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16155522",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"α, a, c = 0.8, 1.0, 2.0\n",
"beta_a, beta_b = 5, 5\n",
"mc_draw_size = 250\n",
"gridsize = 150\n",
"grid_max = 35\n",
"grid = np.linspace(a, grid_max, gridsize)\n",
"\n",
"beta_dist = beta(5, 5)\n",
"Z = a + beta_dist.rvs(mc_draw_size) * c # Shock observations\n",
"D = P = lambda x: 1.0 / x\n",
"tol = 1e-4\n",
"\n",
"\n",
"def T(p_array):\n",
"\n",
" new_p = np.empty_like(p_array)\n",
"\n",
" # Interpolate to obtain p as a function.\n",
" p = interp1d(grid,\n",
" p_array,\n",
" fill_value=(p_array[0], p_array[-1]),\n",
" bounds_error=False)\n",
"\n",
" # Update\n",
" for i, x in enumerate(grid):\n",
"\n",
" h = lambda q: q - max(α * np.mean(p(α * (x - D(q)) + Z)), P(x))\n",
" new_p[i] = brentq(h, 1e-8, 100)\n",
"\n",
" return new_p\n",
"\n",
"\n",
"fig, ax = plt.subplots()\n",
"\n",
"price = P(grid)\n",
"ax.plot(grid, price, alpha=0.5, lw=1, label=\"inverse demand curve\")\n",
"error = tol + 1\n",
"while error > tol:\n",
" new_price = T(price)\n",
" error = max(np.abs(new_price - price))\n",
" price = new_price\n",
"\n",
"ax.plot(grid, price, 'k-', alpha=0.5, lw=2, label=r'$p^*$')\n",
"ax.legend()\n",
"ax.set_xlabel('$x$', fontsize=12)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "7839c232",
"metadata": {},
"source": [
"The figure above shows the inverse demand curve $ P $, which is also $ p_0 $, as\n",
"well as our approximation of $ p^* $.\n",
"\n",
"Once we have an approximation of $ p^* $, we can simulate a time series of\n",
"prices."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9e3798e",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"# Turn the price array into a price function\n",
"p_star = interp1d(grid,\n",
" price,\n",
" fill_value=(price[0], price[-1]),\n",
" bounds_error=False)\n",
"\n",
"def carry_over(x):\n",
" return α * (x - D(p_star(x)))\n",
"\n",
"def generate_cp_ts(init=1, n=50):\n",
" X = np.empty(n)\n",
" X[0] = init\n",
" for t in range(n-1):\n",
" Z = a + c * beta_dist.rvs()\n",
" X[t+1] = carry_over(X[t]) + Z\n",
" return p_star(X)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(generate_cp_ts(), label=\"price\")\n",
"ax.set_xlabel(\"time\")\n",
"ax.legend()\n",
"plt.show()"
]
}
],
"metadata": {
"date": 1712983675.7542555,
"filename": "commod_price.md",
"kernelspec": {
"display_name": "Python",
"language": "python3",
"name": "python3"
},
"title": "Commodity Prices"
},
"nbformat": 4,
"nbformat_minor": 5
}