Design a site like this with WordPress.com
Get started

Computing the demand for multiple goods in R.

An illustration of demand with Cobb Douglas preferences.
(I didn’t add lines because it looks better this way)

Note to reader: If you are new to optimization in R be sure to check out a short video I made on the topic here.

The solving of economic models on on the blackboard is very different than programming it. Some things that take getting used to are:

  • The fact computers use numerical methods to solve constrained optimization problems which rely on initial guesses for where our perspective optimum may lie.
  • We need to define a place to store our data before any computing.
  • We often need to “trick” our solvers into doing what we want them to do what we want them to by reorienting maximization problems as minimization of negative objective problems.

In this blog we embark on a journey for computing the demand curves for two goods considering changes along two different prices.

For details on how we do this with one good check out a previous blog i made here. Note that my coding practices at the time of creating the blog were a bit sloppy, which I hope to update at a later date.

For now lets look at our code.

The Code

The code that I used to compute two demand curves is seen below:

library('NlcOptim')
library('ggplot2')
library('ggpubr')
library('ggthemes')

#set up model parameters
p<-seq(1,20,by=1)
m<-100

#storage for demands and prices
x1<-NULL 
x2<-NULL
p1data<-NULL
p2data<-NULL

#Guess
guess<-c(1,1)

#Utility function
pref<-function(x){
  return(-x[1]^0.5*x[2]^0.5)
}

#constraint
con<-function(x){
  f=NULL
  f=rbind(f,p1*x[1]+p2*x[2]-m)
  return(list(ceq = NULL, c = f))
}

#Loops
for(p1 in p){
  for(p2 in p){
    umax<-solnl(guess,pref,con)
    d1<-umax$par[1,1]
    d2<-umax$par[2,1]
    x1<-rbind(x1,d1)
    x2<-rbind(x2,d2)
    p1data<-rbind(p1data,p1)
    p2data<-rbind(p2data,p2)
  }
}

#store data
df<-data.frame(p1data,p2data,x1,x2)

#Plot it in ggplot
p1x1<-qplot(df$x1,df$p1data)+geom_point(color="blue",size=1)+xlab('Good 1')+ylab('Price 1')+theme_solarized()
p1x2<-qplot(df$x2,df$p1data)+geom_point(color="blue",size=1)+xlab('Good 2')+ylab('Price 1')+theme_solarized()
p2x1<-qplot(df$x1,df$p2data)+geom_point(color="blue",size=1)+xlab('Good 1')+ylab('Price 2')+theme_solarized()
p2x2<-qplot(df$x2,df$p2data)+geom_point(color="blue",size=1)+xlab('Good 2')+ylab('Price 2')+theme_solarized()


ggarrange(p1x1,p1x2,p2x1,p2x2,ncol=2,nrow=2)

Having stared at this wall of code, lets talk about the Loop in the code, that is:

for(p1 in p){
  for(p2 in p){
    umax<-solnl(guess,pref,con)
    d1<-umax$par[1,1]
    d2<-umax$par[2,1]
    x1<-rbind(x1,d1)
    x2<-rbind(x2,d2)
    p1data<-rbind(p1data,p1)
    p2data<-rbind(p2data,p2)
  }
}

The key to generating data under multiple price changes is to have a loop for the change of each price. The generation of this sort of data is important for making comparisons between the change in price of good 1 to the demand for good 2 and vice versa.

Its also clear that if we wish to consider an objective problem with more goods, we need to have an additional outer loop per good with null vectors for storage.

Whats Next?

This may appear as a cute exercise (and it is), but the over arching agenda behind the computing of multiple demand curves is to then generate multiple supply curves and address questions relating to general equilibrium such as how changes in different parameters of our model will impact welfare, quantity demanded across both goods and profits of firms.

This is only the beginning of some cooler stuff I hope to share.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: