Design a site like this with WordPress.com

# Computing A Simple Model of Competitive Equilibrium in R It may not look like much, but its exactly what we want.

In the previous posts we have learned how to compute a supply curve and a demand curve. What do we need to do to derive a competitive equilibrium? Before we answer that directly, lets first define what a competitive equilibrium is so to get everyone on the same page.

Def: A Competitive Equilibrium CE={x,p} is an allocation supported by a price vector such that:

1. Firms maximize their profits.
2. Consumers maximize their utilities.
3. Markets clear.

In this example we will be considering equilibrium in a single market so we will be just considering one price (in another post i’ll see what to do with more than one good).

To start computing this model we begin by defining two loops, one for our firm’s problem and one for our consumer and generate the demand and supply at each price in our sequence. After this we need to generate a excess demand `z` which is simply the quantity demanded minus the quantity supplied. To identify the equilibrium allocation we just search for the row in which this is minimized. This is all detailed in the code below.

```library('NlcOptim')
library('ggplot2')
library('ggthemes')

##################################
#Starting Values and Null Vectors#
##################################
prices<-seq(1,15,by=0.1)
x0<-c(1,1)
q0<-c(1)
qsupply<-NULL
qdemand<-NULL

########
#SUPPLY#
########

#Profit function
profitfunc<-function(q){
return(-p*q+q^2+20)
}

#For Loop
for(p in prices){
cmin<-optim(q0,
profitfunc,
control= list(fnscale=-1),
lower=0,
method = "L-BFGS-B")
q<-cmin\$par
qsupply<-rbind(qsupply,q)}

########
#DEMAND#
########

#Preferences
pref<-function(x){
return(-x^0.5*x^0.5)
}

#Budget constraint
con<-function(x){
f= NULL
f= rbind(f,p*x+x-100)
return(list(ceq = NULL, c = f))
}

#For loop
for(p in prices){
umax<-solnl(x0,pref,con)
d<-umax\$par
qdemand<-rbind(qdemand,d)}

##############################
#Create Excess Demand Vectors#
##############################
z<-qdemand-qsupply
absz<-abs(z)
####################
#Create Data Frame##
####################
df<-data.frame(prices,qdemand,qsupply,z,absz)

##################################
#Plot our Supply and Demand Curve#
##################################
ggplot(df, aes(y=prices)) +
geom_line(aes(x = qdemand, colour = "Demand"),color="blue", size=1.0) +
geom_line(aes(x = qsupply, colour = "Supply"),color="red", size= 1.0)+
labs(xlab="Quantity",ylab="Price",title  ="Equilibrium in our Model")+
theme_excel_new()
```

As previously stated we can find equilibrium in this model with the `subset()` function where we search for the row with the minimum absolute excess demand `absz`. This is seen in the code below:

```####################
#Equilibrium Finder#
####################

subset(df,absz==min(absz))

prices qdemand qsupply             z         absz
d.90     10       5       5 -4.422009e-08 4.422009e-08
```

Seeing how we can compute these models in practice is fascinating to see. The way we are doing this is by simply considering various optimum at different prices and seeing where supply equals demand. Some things to note:

• We don’t define our production technology for our firm in this model. rather we define it implicitly through a cost function we need to define our firm’s cost structure directly before any considerations can be made.
• Though not discussed in this post, we can also get values of what our firms profits and utilities are from `cmin` and `umax` by considering their outputs. we would have to generate null vectors and transform their outputs as they would be negative numbers given how we have oriented our problems.

At the time of posting this blog I’m very much still learning how to compute these models so my syntax may not be the best. If you know anyways to clean this code or extend it to more interesting scenarios (like a model with a labor/leisure decision by our consumer which in turn impacts the productive capacity of the firm) let me know!