Qbik
Qbik

Reputation: 6157

Algorithm for market clearing

I've got discrete step functions for supply and demand. I'm searching for an algorithm to find the equilibrium price, The data are below in R, but a solution any language (or pseudo-code) is acceptable.

demand = data.frame(volume = c(8,2,3,1,1), price=c(1,2,3,4,5))
supply = data.frame(volume = c(3,2,4,2,3), price=c(5,4,3,2,1))

demand$volume <- cumsum(demand$volume)
supply$volume <- cumsum(supply$volume)

plot(demand, type="s")
lines(supply, type="s", col=3)

enter image description here

Upvotes: 1

Views: 1005

Answers (2)

majeed simaan
majeed simaan

Reputation: 220

I was looking into a similar problem and found this great description: https://www.youtube.com/watch?v=FYfbM56L-mE&ab_channel=31761-Renewablesinelectricitymarkets

You can motivate a similar analysis for your problem. Consider an LP formulation. Given the dual solution, you can find the market-clearing price as follows:

demand = data.frame(Type = "demand",Q = c(8,2,3,1,1), P=c(1,2,3,4,5))
supply = data.frame(Type = "supply",Q = c(3,2,4,2,3), P=c(5,4,3,2,1))
ds <- rbind(supply,demand)

By representing the problem from LP, do the following:

ds[ds$Type == "demand","Q"] <- ds[ds$Type == "demand","Q"]
ds[ds$Type == "supply","Q"] <- ds[ds$Type == "supply","Q"]

P_s <- ds[ds$Type == "supply","P"]
P_d <- ds[ds$Type == "demand","P"]
Q_s <- ds[ds$Type == "supply","Q"]
Q_d <- ds[ds$Type == "demand","Q"]
c_vec <- c(P_s,-P_d)
A_mat <- diag(length(c_vec))
b_vec <- c(Q_s,Q_d)
dir_1 <- rep("<=",length(b_vec))

A2_mat <- c(rep(1,length(Q_s)),rep(-1,length(Q_d)))
b2_vec <- 0

A_mat <- rbind(A_mat,A2_mat)
b_vec <- c(b_vec,b2_vec)
dir_1 <- c(dir_1,"=")

library(lpSolve)
sol <- lp ("min", c_vec, A_mat, dir_1, b_vec, compute.sens=TRUE)
price_mc <- sol$duals[nrow(ds) + 1] # extracts the dual, which corresponds to the price

In your example, the market-clearing price is $2.

Upvotes: 2

Prune
Prune

Reputation: 77850

You need to take partial cumsum volumes from opposite ends of the price range.

demand_cum = (15, 7, 5,  2,  1)
supply_cum = ( 3, 5, 9, 11, 14)

This shows you total, cumulative demand & supply at each price. Now can you spot the equilibrium?

Upvotes: 1

Related Questions