(This article was first published on

**Econometrics by Simulation**, and kindly contributed to R-bloggers)# 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.

To

**leave a comment**for the author, please follow the link and comment on his blog:**Econometrics by Simulation**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...