Consumer’s Choosing an Optimal Bundle – Utility Maximization

November 2, 2013
By

[This article was first published on Econometrics by Simulation, 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.

# The theoretical basis of classical consumer theory lies
# in utility maximization. The idea is that consumers
# make consumption decisions based on choosing a bundle
# of goods that will maximize individual utility.
# Despite this hypothesis being largely unsupported
# by reproducible results indicating the superiority
# any utility function over all other functions this
# theory persists.
 
# In this simulation I set up an easy framework for the
# user to simulate the decision of the consumer as a
# function of the utility function, price of goods,
# and consumer budget.
 
 
# Uof is the function that takes a choice of
# x and y and calculates the corresponding z as
# well as expected utility.
 
Uof <- function(XY) {
# x cannot be less than 0 or more than all of the
# budget expended on x
x <- min(max(XY[1],0), b/px)
# y cannot be less than 0 or more than the remaining
# budget left after x expenditures
y <- min(max(XY[2],0), (b - x*px)/py)
 
# z is purchased with whatever portion of the budget
# remains
z <- (b - x*px - y*py)/pz
 
# Display the quantity of x,y, and z chosen.
print(paste0("x:", x, " y:", y, " z:", z))
 
# Since optim minimizes a function I am making
# the returned value equal to negative utility.
-utility(x,y,z)
}
 
# I have defined a few different potential utility
# functions.
 
cobb.douglas2 <- function(x,y,z) x^.3*y^.3
cobb.douglas3 <- function(x,y,z) x^.3*y^.3*z^.4
lientief3 <- function(x,y,z) min(x,y,z)
linear.concave <- function(x,y,z) x^.3 + y^.2 + z^.5
mixed <- function(x,y,z) min(x, y)^.5*z^.5
addative <- function(x,y,z) 2*x+3*y+z
 
# Choose the utility function to maximize
utility <- cobb.douglas3
 
# Choose the prices
px <- 1
py <- 1
pz <- 1
 
# Choose the total budget
b <- 100
 
# Let's see how much utility we get out of setting
# x=1 and y=1
Uof(c(1,1))
 
# The following command will maximize the utility
# subject the choice of the utility fuction, prices,
# and budget.
optim(c(1,1), Uof, method="BFGS")
 
# In general it is a good idea not to use such
# computational methods as this since by instead
# solving mathematically in closed form for solutions
# to utility maximization functions, you can discover
# how exactly a change in one parameter in the model
# may lead to a change in quantity demanded of a
# type of good.
 
# Of course you could do something fairly simple along
# these lines in R as well.
 
# For instance, define vectors:
 
# Once again choose what utility function
utility <- linear.concave
 
xv <- NULL
yv <- NULL
 
pxv <- seq(.25,10,.25)
 
for (px in pxv) {
res <- optim(c(1,1), Uof, method="BFGS")
xv <- c(xv, max(res$par[1],0))
yv <- c(yv, max(res$par[2],0))
}
 
plot(xv, pxv, type="l", main="Demand for X(px)",
xlab="Quantity of X", ylab="Price of X")
 
# The map of the demand function for X 
 
  
 
plot(yv, pxv, type="l", main="Demand for Y(px)",
xlab="Quantity of Y", ylab="Price of X") 
 
  
# The map of the demand function for y as a function
# of the price of x. It is a little erratic probably
# because of lack of precision in the optimization
# algorithm.

Highlighted by Pretty R at inside-R.org

To leave a comment for the author, please follow the link and comment on their blog: Econometrics by Simulation.

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.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

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)