28. Commodity Prices#

28.1. Outline#

For more than half of all countries around the globe, commodities account for the majority of total exports.

Examples of commodities include copper, diamonds, iron ore, lithium, cotton and coffee beans.

In this lecture we give an introduction to the theory of commodity prices.

The lecture is quite advanced relative to other lectures in this series.

We need to compute an equilibrium, and that equilibrium is described by a price function.

We will solve an equation where the price function is the unknown.

This is harder than solving an equation for an unknown number, or vector.

The lecture will discuss one way to solve a functional equation (an equation where the unknown object is a function).

For this lecture we need the yfinance library.

!pip install yfinance
Hide code cell output
Collecting yfinance
  Downloading yfinance-0.2.56-py2.py3-none-any.whl.metadata (5.8 kB)
Requirement already satisfied: pandas>=1.3.0 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (2.2.2)
Requirement already satisfied: numpy>=1.16.5 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (1.26.4)
Requirement already satisfied: requests>=2.31 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (2.32.3)
Collecting multitasking>=0.0.7 (from yfinance)
  Downloading multitasking-0.0.11-py3-none-any.whl.metadata (5.5 kB)
Requirement already satisfied: platformdirs>=2.0.0 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (3.10.0)
Requirement already satisfied: pytz>=2022.5 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (2024.1)
Collecting frozendict>=2.3.4 (from yfinance)
  Downloading frozendict-2.4.6-py312-none-any.whl.metadata (23 kB)
Collecting peewee>=3.16.2 (from yfinance)
  Downloading peewee-3.17.9.tar.gz (3.0 MB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/3.0 MB ? eta -:--:--
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 81.0 MB/s eta 0:00:00
?25h
  Installing build dependencies ... ?25l-
 \
 |
 done
?25h  Getting requirements to build wheel ... ?25l-
 done
?25h  Preparing metadata (pyproject.toml) ... ?25l-
 done
?25hRequirement already satisfied: beautifulsoup4>=4.11.1 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from yfinance) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: tzdata>=2022.7 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2024.8.30)
Requirement already satisfied: six>=1.5 in /home/runner/miniconda3/envs/quantecon/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.16.0)
Downloading yfinance-0.2.56-py2.py3-none-any.whl (113 kB)
Downloading frozendict-2.4.6-py312-none-any.whl (16 kB)
Downloading multitasking-0.0.11-py3-none-any.whl (8.5 kB)
Building wheels for collected packages: peewee
  Building wheel for peewee (pyproject.toml) ... ?25l-
 \
 |
 done
?25h  Created wheel for peewee: filename=peewee-3.17.9-cp312-cp312-linux_x86_64.whl size=303871 sha256=ebbf6610cae5fd4846748481d5f0a005231fa91c7de490da5a368b4bfab70412
  Stored in directory: /home/runner/.cache/pip/wheels/43/ef/2d/2c51d496bf084945ffdf838b4cc8767b8ba1cc20eb41588831
Successfully built peewee
Installing collected packages: peewee, multitasking, frozendict, yfinance
Successfully installed frozendict-2.4.6 multitasking-0.0.11 peewee-3.17.9 yfinance-0.2.56

We will use the following imports

import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.optimize import brentq
from scipy.stats import beta

28.2. Data#

The figure below shows the price of cotton in USD since the start of 2016.

Hide code cell source
s = yf.download('CT=F', '2016-1-1', '2023-4-1')['Close']
Hide code cell output
YF.download() has changed argument auto_adjust default to True
[*********************100%***********************]  1 of 1 completed

Hide code cell source
fig, ax = plt.subplots()

ax.plot(s, marker='o', alpha=0.5, ms=1)
ax.set_ylabel('cotton price in USD', fontsize=12)
ax.set_xlabel('date', fontsize=12)

plt.show()
_images/f17bfe7250554804c8970fa6d45f251f20fff56138e0c9292515587192cd1471.png

The figure shows surprisingly large movements in the price of cotton.

What causes these movements?

In general, prices depend on the choices and actions of

  1. suppliers,

  2. consumers, and

  3. speculators.

Our focus will be on the interaction between these parties.

We will connect them together in a dynamic model of supply and demand, called the competitive storage model.

This model was developed by [Samuelson, 1971], [Wright and Williams, 1982], [Scheinkman and Schechtman, 1983], [Deaton and Laroque, 1992], [Deaton and Laroque, 1996], and [Chambers and Bailey, 1996].

28.3. The competitive storage model#

In the competitive storage model, commodities are assets that

  1. can be traded by speculators and

  2. have intrinsic value to consumers.

Total demand is the sum of consumer demand and demand by speculators.

Supply is exogenous, depending on “harvests”.

Note

These days, goods such as basic computer chips and integrated circuits are often treated as commodities in financial markets, being highly standardized, and, for these kinds of commodities, the word “harvest” is not appropriate.

Nonetheless, we maintain it for simplicity.

The equilibrium price is determined competitively.

It is a function of the current state (which determines current harvests and predicts future harvests).

28.4. The model#

Consider a market for a single commodity, whose price is given at t by pt.

The harvest of the commodity at time t is Zt.

We assume that the sequence {Zt}t1 is IID with common density function ϕ, where ϕ is nonnegative.

Speculators can store the commodity between periods, with It units purchased in the current period yielding αIt units in the next.

Here the parameter α(0,1) is a depreciation rate for the commodity.

For simplicity, the risk free interest rate is taken to be zero, so expected profit on purchasing It units is

Etpt+1αItptIt=(αEtpt+1pt)It

Here Etpt+1 is the expectation of pt+1 taken at time t.

28.5. Equilibrium#

In this section we define the equilibrium and discuss how to compute it.

28.5.1. Equilibrium conditions#

Speculators are assumed to be risk neutral, which means that they buy the commodity whenever expected profits are positive.

As a consequence, if expected profits are positive, then the market is not in equilibrium.

Hence, to be in equilibrium, prices must satisfy the “no-arbitrage” condition

(28.1)#αEtpt+1pt0

This means that if the expected price is lower than the current price, there is no room for arbitrage.

Profit maximization gives the additional condition

(28.2)#αEtpt+1pt<0 implies It=0

We also require that the market clears, with supply equaling demand in each period.

We assume that consumers generate demand quantity D(p) corresponding to price p.

Let P:=D1 be the inverse demand function.

Regarding quantities,

  • supply is the sum of carryover by speculators and the current harvest, and

  • demand is the sum of purchases by consumers and purchases by speculators.

Mathematically,

  • supply is given by Xt=αIt1+Zt, which takes values in S:=R+, while

  • demand =D(pt)+It

Thus, the market equilibrium condition is

(28.3)#αIt1+Zt=D(pt)+It

The initial condition X0S is treated as given.

28.5.2. An equilibrium function#

How can we find an equilibrium?

Our path of attack will be to seek a system of prices that depend only on the current state.

(Our solution method involves using an ansatz, which is an educated guess — in this case for the price function.)

In other words, we take a function p on S and set pt=p(Xt) for every t.

Prices and quantities then follow

(28.4)#pt=p(Xt),It=XtD(pt),Xt+1=αIt+Zt+1

We choose p so that these prices and quantities satisfy the equilibrium conditions above.

More precisely, we seek a p such that (28.1) and (28.2) hold for the corresponding system (28.4).

(28.5)#p(x)=max{α0p(αI(x)+z)ϕ(z)dz,P(x)}(xS)

where

(28.6)#I(x):=xD(p(x))(xS)

It turns out that such a p will suffice, in the sense that (28.1) and (28.2) hold for the corresponding system (28.4).

To see this, observe first that

Etpt+1=Etp(Xt+1)=Etp(αI(Xt)+Zt+1)=0p(αI(Xt)+z)ϕ(z)dz

Thus (28.1) requires that

α0p(αI(Xt)+z)ϕ(z)dzp(Xt)

This inequality is immediate from (28.5).

Second, regarding (28.2), suppose that

α0p(αI(Xt)+z)ϕ(z)dz<p(Xt)

Then by (28.5) we have p(Xt)=P(Xt)

But then D(p(Xt))=Xt and It=I(Xt)=0.

As a consequence, both (28.1) and (28.2) hold.

We have found an equilibrium, which verifies the ansatz.

28.5.3. Computing the equilibrium#

We now know that an equilibrium can be obtained by finding a function p that satisfies (28.5).

It can be shown that, under mild conditions there is exactly one function on S satisfying (28.5).

Moreover, we can compute this function using successive approximation.

This means that we start with a guess of the function and then update it using (28.5).

This generates a sequence of functions p1,p2,

We continue until this process converges, in the sense that pk and pk+1 are very close together.

Then we take the final pk that we computed as our approximation of p.

To implement our update step, it is helpful if we put (28.5) and (28.6) together.

This leads us to the update rule

(28.7)#pk+1(x)=max{α0pk(α(xD(pk+1(x)))+z)ϕ(z)dz,P(x)}

In other words, we take pk as given and, at each x, solve for q in

(28.8)#q=max{α0pk(α(xD(q))+z)ϕ(z)dz,P(x)}

Actually we can’t do this at every x, so instead we do it on a grid of points x1,,xn.

Then we get the corresponding values q1,,qn.

Then we compute pk+1 as the linear interpolation of the values q1,,qn over the grid x1,,xn.

Then we repeat, seeking convergence.

28.6. Code#

The code below implements this iterative process, starting from p0=P.

The distribution ϕ is set to a shifted Beta distribution (although many other choices are possible).

The integral in (28.8) is computed via Monte Carlo.

α, a, c = 0.8, 1.0, 2.0
beta_a, beta_b = 5, 5
mc_draw_size = 250
gridsize = 150
grid_max = 35
grid = np.linspace(a, grid_max, gridsize)

beta_dist = beta(5, 5)
Z = a + beta_dist.rvs(mc_draw_size) * c    # Shock observations
D = P = lambda x: 1.0 / x
tol = 1e-4


def T(p_array):

    new_p = np.empty_like(p_array)

    # Interpolate to obtain p as a function.
    p = interp1d(grid,
                 p_array,
                 fill_value=(p_array[0], p_array[-1]),
                 bounds_error=False)

    # Update
    for i, x in enumerate(grid):

        h = lambda q: q - max(α * np.mean(p(α * (x - D(q)) + Z)), P(x))
        new_p[i] = brentq(h, 1e-8, 100)

    return new_p


fig, ax = plt.subplots()

price = P(grid)
ax.plot(grid, price, alpha=0.5, lw=1, label="inverse demand curve")
error = tol + 1
while error > tol:
    new_price = T(price)
    error = max(np.abs(new_price - price))
    price = new_price

ax.plot(grid, price, 'k-', alpha=0.5, lw=2, label=r'$p^*$')
ax.legend()
ax.set_xlabel('$x$')
ax.set_ylabel("prices")

plt.show()
_images/7effcfb804e8333dfa533891539cbbba8078c40fc80c03a00aadeb0707d7501c.png

The figure above shows the inverse demand curve P, which is also p0, as well as our approximation of p.

Once we have an approximation of p, we can simulate a time series of prices.

# Turn the price array into a price function
p_star = interp1d(grid,
                  price,
                  fill_value=(price[0], price[-1]),
                  bounds_error=False)

def carry_over(x):
    return α * (x - D(p_star(x)))

def generate_cp_ts(init=1, n=50):
    X = np.empty(n)
    X[0] = init
    for t in range(n-1):
            Z = a + c * beta_dist.rvs()
            X[t+1] = carry_over(X[t]) + Z
    return p_star(X)

fig, ax = plt.subplots()
ax.plot(generate_cp_ts(), label="price")
ax.set_xlabel("time")
ax.legend()
plt.show()
_images/4d710752b9c1a29e850814542d6297973bb07f330ea00c14ea67da39f5c5b165.png