Short Guide to Utility Functions (with R Examples)
In this post I will present some common Utility functions with examples using R
. This post is based on the book Intermediate. Microeconomics. A Modern Approach. Eighth Edition by Hal R. Varian.
You can find the code of this post here
https://github.com/giacomovagni/gcodes/blob/master/gcode_project/2023-05-09-cobbs-douglas.R
The concept of Utility is an old concept stemming from English utilitarian philosophy (e.g. Betham, JS Mill). It denotes very generally the idea of happiness or enjoyment. When it is too hard to put an exact number on happiness (e.g. on a scale from
The idea of a “Utility function” is to give Utility or Happiness1 a functional form. The simplest form is the additive form. Imagine that we have two goods (apples and oranges), called
If I derive one point of enjoyment from
Economists have categorised different types of functional forms of Utility, for instance
- (Perfect) substitute
- (Perfect) complement
- “Bads”
- Quasi-linear
- Cobbs-Douglas
And many more.
In this post, I will focus mainly on the Cobbs-Douglas function and on Perfect substitute.
One of the most challenging tasks of consumer theory is choosing a sensible Utility function. This is because the functional form must accurately reflect how individuals make economic decisions in the real world.
Many social scientists have simply given up on this, arguing it is impossible. Nevertheless, despite the difficulties, economists have tried hard to model human decisions in economic settings.
Can we really model mathematically human behavior at the micro-level? What do you think?
Perfect substitute
The form
For instance, imagine that I need to do
In the end, spending a certain energy level (or burning a certain number of calories) is what matters.
We can have different equivalence regarding energy burn between sports; for instance,
A general functional form for substitute goods looks like this
where
Therefore,
For a “Utility” of
We have a substitution plot like this one

We can see that the substitution rate is constant. In other words, the slope
My willingness to exchange tennis for badminton does not change, if I have already played 15 minutes of tennis or 45 minutes.
However, this is not always the case and functions like the Cobbs-Douglas model exactly this; the rate of substitution is allowed to differ according to different combinations. More below.
Indifference Curve & Marginal Utility
Before proceeding, let’s discuss the idea of “indifference curve”.
In the example of burning calories, we can think of the burnt calories as my Utility. For example,
The indifference curve represents the combinations between
For instance, if I want to burn
For simplification, let’s say that
I can have many combinations respecting this Utility level;
The slope of this line is still
The indifference curve is a curve on which I can trade off one good for another and remain at the same level of Utility or happiness.
I can have different levels of happiness of course. I would be even happier if I burned ).
Moreover, economists recognize that the enjoyment of a good is not generally linear but follows diminishing returns. The first
This is a very powerful idea. However, eating ten piece of cakes may no longer add to our happiness but instead cause discomfort. One way to approach this is to acknowledge that we rarely reach these “extreme” points and, in our daily lives, more is typically better. This is particularly true with money, as it’s rare to say that an extra dollar would make us unhappy unless we were in an extreme scenario (!?).
Marginal diminishing return is illustrated by this type of curve.

To understand how much Utility I derive from consuming one good (
Let
For instance from 1 to 2 seconds,
The effect of increase in the consumption of
which we can see
This is in other terms the increase in our happiness by the increase in our consumption of
This idea is written as
The
We can see in the figure below, that the slope of our Utility is diminishing as we increase our consumption of

In R
x=seq(0.1, 10, by = 0.10)
x=0:10
# utility function
Ux = function(x, a) exp ( a * log(x) )
library(Deriv)
# derivative #
f_ = Deriv(Ux, "x")
df = data.frame(x, f_d = f_(x, a = 0.5), U = Ux(a = 0.5, x))
plot(df$x, df$U, xlim = c(0,4), ylim = c(0,2), xlab = 'x', ylab = "U", axes = F)
axis(side = 1)
axis(side = 2)
polygon(c(1,1,0), c(0, 1,0), col = 'gray90', lty = 4)
polygon(c(1,2,2), c(1, 1.414214,1), col = 'gray90', lty = 4)
polygon(c(2,3,3), c(1.414214, 1.732051,1.414214), col = 'gray90', lty = 4)
polygon(c(3,4,4), c(1.732051, 2.000000,1.732051), col = 'gray90', lty = 4)
The indifference curve measures the trade-off we are willing to make to keep our Utility constant; the indifference curve can also be expressed as the Marginal Utility of
plus the change in the consumption of
In order word, to keep my utility constant, how much I am willing to exchange one good for another?
This rate is called the Marginal Rate of substitution (MRS). It reveals the rate at which a person is willing to substitute one good for another while maintaining the same level of utility.
We can see from the equation above that it is the Marginal Utility (marginal enjoyment) of consuming
Cobbs-Douglas Utility Function
The Cobbs-Douglas Utility Function is a convex function, which means substantially that people prefer a combination of goods rather than specializing in one type of good. When specialization happens, the shape is said to be concave. With a concave shape, it “costs” more to substitute one good with another, especially at the extreme. For instance, this would apply when substituting a sport with another because one needs to learn all the rules and develop all the necessary skills to play the game.
The Cobbs-Douglas Utility Function is one of the most common functional forms.
A simple convex utility function can be defined as
For instance, the indifference curve can be retrieved by choosing a utility level and then solving for
Let’s fix the Utility to
Let’s imagine
The keep the level of Utility at 6, we would need to have the following combinations of
All along this curve, the combinations give a fixed level of Utility of

R code
f_convex = function(x,y) x*y
# solve for y
f_convex_y = function(x,U) U/x
x = seq(0, 10, by = 1)
yU6 = f_convex_y(x, U = 6)
plot(x,yU6, type = 'l', xlim = c(0,10), ylim = c(0, 10))
Two Canonical Cobbs-Douglas expressions
Two common expressions of the Cobbs-Douglas utility function are the following
for example
and
Which is
For the first expression, we can solve for
or
in R
f_cobbs = function(x,y,a,b) (x^a) * (y^b)
f_cobbs_y = function(x,U,a,b) (U / x^a)^(1/b)
f_cobbs2_y = function(x,U,a,b) (U * x^(-a))^(1/b)
And for the second expression, we can solve for
with R
f_cobbs_ln = function(x,y,a,b) a*log(x) + b*log(y)
f_cobbs_ln_y = function(x,U,a,b) exp ( (U - a*log(x) ) / b)
Budget
Let’s imagine that we can only pick
If we calculate the Utility for all the combinations according to
We have the following Utility distribution
We can see that the maximum Utility we can reach here is
Let’s calculate the points from an Indifference Curve with a Utility of
This would look like this

In the table below, we use the equation
The table shows the values of
The only combination within the budget for a Utility of
Now if we look at the indifference curve with a Utility of
This is why the combination
Prices
Economists usually work with a price tag for goods. Let’s imagine we are dining in a restaurant, and we can get either food
Our budget (
We want to spend all of our budget tonight. The budget will be the number of units of food multiplied by their respective prices.
If I buy
In R
f = function(x,px,y,py) x*px + py*y
f_y = function(x,px,py, I) (I - x*px)/py
f(x = 20, px = 3, y = 20, py = 2)
# verify
f_y(x = 20, px = 3, py = 2, I = 100)
We can find the maximum units of
For
and for
f_max = function(I,px) I/px
f_max(I = 100, px = 3)
f_max(I = 100, px = 2)
This gives us units
We can draw our budget line with
x = 0:33
y = 50 - (px/py) * x
plot(x,y)

Imagine that, in this case, our preferences take the following form
Although
We ask: what is the optimal combination of
Strategy 1
Calculate the maximum Utility from this function, draw the indifference curve of the maximum Utility and find the combination that respects the budget.
With R
x = 0:33 # max x if y is 0
y = 50 - (px/py) * x # max y if x is 0
maxU = max(f_cobbs(x = x, y = y, a = 0.7, b = 0.3))
We get
Let’s predict the number of units of
y = f_cobbs_y(x, U = maxU, a=0.7, b=0.3)
df = data.frame(x, y) %>% mutate(Budget = x*px+y*py)
w = which.min(df$Budget - 100)
df[w, ]
The combination that respects our budget is
We can verify with
f = function(x,px,y,py) x*px + py*y
f(x = 23, px = 3, y = 15.5, py = 2)

Strategy 2. Marginal Utility and Marginal Rate of Substitution
We said above that the the Marginal Rate of Substitution (MRS) is the slope of the indifference curve.
“It can be interpreted as the rate at which a consumer is just willing to substitute a small amount of good 2 for good 1.” (Varian , p.67)
When our indifference curve’s slope equals (i.e. is tangent to) our budget line’s slope, we have the optimal combinations of
Varian p.75 discuss exceptions to this result.
The slope of our budget line is
# find maximum number of x I can get given their price
x_max = f_max(I = 100, p = px)
# find maximum number of x I can get given their price
y_max = f_max(I = 100, p = py)
Let’s create more precise values for
x = seq(0, x_max, by = 0.001)
# calculate the values for y that respect the budget #
y = y_max + (slope * x)
## plots ##
plot(x,y, type = "l", xlim = c(0,60), ylim = c(0,60))
abline(h=0,v=0, col = 'gray')
# maximum Utility
max_U = max(f_cobbs(x = x, y = y, a = a, b = b))
df2 = data.frame(x, y = f_cobbs_y(x, U = max_U, a = a, b = b)) %>% mutate(Budget = x*px+y*py)
df2$slope = slope
# indifference curve #
points(df2$x, df2$y, type = "l")
I couldn’t find how to do a partial derivative with R
, so I created the following differential function
f_derive = function(h = 0.01, f = f_cobbs, x, y, a, b) (f(x = x+h, y = y, a = a, b = b) - f(x = x,y = y,a = a, b = b)) / h
MUxy = - f_derive(h = 0.01, x = x, y = y, a = a, b = b) / f_derive(h = 0.01, x = y, y = x, a = b, b = a)
df2$MRS = MUxy
w2 = which.min(abs(df2$slope - df2$MRS))
df2[w2, ]
abline(h = df2[w2, ]$y, v = df2[w2, ]$x, col = 'red')
That’s it!
Notes
-
Although Varian writes, “Economists have abandoned the old-fashioned view of Utility as being a measure of happiness.” (p.55), I find it helpful to still think of it in the old-fashioned way. Moreover, the field of Happiness Studies has revamped the old conception of Utility. ↩