Site icon R-bloggers

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:

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.