Design a site like this with WordPress.com
Get started

Deriving the Demand Curve from the Consumer’s Objective Problem in R.

Note: This blog builds off of what was discussed previously here

Continuing with our adventures with the NlcOptim package for R we start with trying to use it for deriving useful objects like the demand curve. Since we are economists we’re not going to simply define a downward sloping demand curve, rather we want to derive it from our consumer’s objective problem.

The way we will do this practically is by embedding our consumer’s problem in the context of a for loop. In the code below we should note that we solve for our demands for each change in price of good 1 (denoted in the code by x[1]) at each price p in our vector of prices.

We repeat this and then store information on prices and quantity demanded in a data frame which is subsequently plotted. Details on this procedure is given in the code below.

#############################
#Deriving Demand Curves in R#
#############################
library('NlcOptim')

################################################
#Initializing by defining sequence of prices,  #
#Null vector for storage of demands and initial#
#"guess" for our solver.                       #
################################################

price<-seq(1,15,by=0.1)
qdemand<-NULL
x0<-c(1,1)

##############
#Preferences#
#############
preferences<-function(x){
  return(-(x[1]+1)^0.5*x[2]^0.5)
}

###################
#Budget Constraint#
###################
budgetconstraint<-function(x){
  f=NULL
  f= rbind(f,p*x[1]+1*x[2]-10)
  return(list(ceq = NULL, c = f))
}

###########################
#Define for loop and Solve#
##########################

for(p in price){
  umax<-solnl(x0,preferences,budgetconstraint)
  d<- umax$par[1]
  qdemand<-rbind(qdemand,d)}

############
#Plot data#
##########
df<-data.frame(price,qdemand)
plot(qdemand,price,type='l',col='blue', xlab='Quantity',ylab='Price',
     main="The Demand Curve", lwd=4.0)
Behold our Micro-Founded Demand Curve.

The ability to do this is very exciting as it gives us tools for truly simulating demand under several conditions. The only story we need to make this more interesting is by adding a supply curve to this graph which will be given its own discussion in a separate post.

If you know any ways to make this code more efficient, be sure to let me know in the comments on this blog!

Advertisement

One response to “Deriving the Demand Curve from the Consumer’s Objective Problem in R.”

  1. […] 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 […]

    Like

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: