Utility Maximization in R using the “NlcOptim” package

[This article was first published on R – Jacob Smith Economics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
If you’re here you probably know what this is.

In this blog post we will discuss how its possible to numerically solve utility maximization problems in R using the NlcOptim package. This package is particularly useful because it allows us to solve these problems with as few lines of code as possible. Lets get into it.

A Consumer’s utility maximization problem is really just a constrained optimization problem and more often then not these types of problems are non linear (as seen in their objective problem). Fortunately the NLC optim package is perfect for these types of problems.

Numerical Solutions

The solutions which we will be looking for are numeric. this means we wont be solving for a general functional form as we do when solving for Marshallian demand from utility maximization problems, instead they will be numbers. This is often something that takes getting used to as someone getting into computational economics generally, now is a time if any for that culture shock. On now with the code.

Solving a Cobb Douglas Utility Maximization Problem in R

The problem which we will be solving in R is the following cobb douglas utility maximization problem.

The code used to address this problem is the following:

############################
#Utility Maximization in R#
###########################
library('NlcOptim')
library('MASS')

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

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

########
#Solve#
#######
solnl(x0,consumer,budgetconstraint)

The output from the above code is:

$par
     [,1]
[1,]  2.5
[2,]  5.0

$fn
[1] -3.535534

$counts
     nfval ngval
[1,]    19     7

$lambda
$lambda$lower
     [,1]
[1,]    0
[2,]    0

$lambda$upper
     [,1]
[1,]    0
[2,]    0

$lambda$eqnonlin
[1] 0.3535533


$grad
           [,1]
[1,] -0.7070689
[2,] -0.3535723

$hessian
            [,1]        [,2]
[1,]  0.14411452 -0.06859893
[2,] -0.06859893  0.03680243

There are several things worth noting:

  • For our preferences we put a negative sign in front of the function because solnl() searches for a minimum value subject to the constraint.
  • our constraints are oriented as being with reference to zero. We also note an additional fact that there are no equality constraints which is why we write return(list(ceq = NULL, c = f)). the ceq denotes constraints which hold with strict equality and c denotes constraints which hold with the “less than or equal to” inequality . Since we dont have any equality constraints we write ceq = NULL.
  • solnl() is pretty good at solving constrained optimization problems generally, but it does so in a way which requires an initial guess of where the optimum might be. x0 is chosen arbitrarily but should be a feasible value. (100,100) as a guess will not work based on how we have defined our constraint!
  • $par is what the utility maximizing values (or Marshallian demands) are for our problem. We can check this is indeed true from their analytical solutions.

Conclusion

This is a simple example of how you would numerically solve a utility maximization problem in R. To get practice, try more complex utility functions, more goods, more constraints! the documentation on NlcOptim is pretty good. I suspect it can even be used for solving computable general equilibrium models. To do that I’d have to sit down with this a bit more.

To leave a comment for the author, please follow the link and comment on their blog: R – Jacob Smith Economics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)