Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96159
License: OTHER
Kernel: SageMath 8.6

Optimization Review

Notebook for the Public Finance course, UCSC, AY 2018/2019 by Duccio Gamannossi degl' Innocenti. You can find more information on the course here.

1. Introduction

Economist usually assume that individuals seek to maximize their utility by making optimal choices given the constraints they face (e.g., individual's budget to be allocated between different goods, hours in a day to be devoted to work or leisure, etc.). This notebook provides a brief illustration of some methods to be used to solve the optimization problems considered during the course Public Finance (UCSC A.A. 2018/2019).

Note that these methods, while working in the simple setting considered in the course, might be unable to deliver results in particular cases.

2. Uncostrained Optimization

In this course, only well-behaved utility function will be considered. We are generally speaking about a function uu that in the univariate case is twice differentiable with u′′<0u''<0.

In this simple setting, if a well-behaved objective function has an interior optimum, it will be at a point where its first derivative relative to the variable of choice is equal to 00.

This follows from:

  • A derivative equal to zero identifies a point where the slope changes of sign.

  • Given that u′′<0u''<0 the sign can only change from positive to negative

  • A maximum is reached when u′u' switches sign from positive to negative.

    Starting from a point where u′(x‾)>0u'(\underline{x})>0 (the utility increases when its argument raises) subsequent increases in xx deliver diminishing marginal increases in uu (due to u′′<0u''<0). For a given x∗>x‾x^*>\underline{x} it will be that u′(x∗)=0u'(x^*)=0 (an increase in xx will deliver no increase in utility) so that for any higher value of the argument xˉ>x∗\bar{x}>x^* utility will get lower by increasing xx (given that u′(xˉ)<0u'(\bar{x})<0). Hence, x∗x^* is a maximum.

    Given that the derivative represents the slope of the curve of the function with respect to the variable of choice, we can give a simple graphical representation of this behaviour (see below). Drawing the function uu on the yy axes versus the xx, it is that when u′(x‾)>0u'(\underline{x})>0 the curve goes up from left to right until u′(x∗)=0u'(x^*)=0 where it stops increasing. For any point at the right of x∗x^* the curve of uu is downward sloping.

In order to solve an unconstrained optimization problem:

  1. Define the function to be maximized

  2. Evaluate the FOCFOC, i.e., the derivative of the function with respect to the choice variable

  3. Set FOC=0FOC=0 and solve for the choice variable

A simple unconstrained utility maximization problem

We want to find the optimal consumption of coffe for a student at UCSC. As we know, the coffe price is p=.6p=.6 euro. We are considering a particular student who feels awful before her first coffe in the morning, enjoys less and less every additional coffe and starts shaking due to caffeine when taking more than five in a day. Then, we could model her utility from coffee as U(z)=5z−z2U(z)=5z - z^2. The problem of our student is then to maximize her objective function F(z,p)=5z−z2−zpF(z,p) = 5z - z^2 - zp where zz is the amount of coffe consumed.

The first step in the analysis is to define the Utility function and the Objective function:

U_simple(z) = 5*z-z^2 F(z, p) = 5*z-z^2 - (z*p) html("U(z)=" + latex(U_simple(z)) + "\\\\" + "F(z,p)=" + latex(F(z, p)))

We might want to explore the problem visually, so to have a better idea about it.

p=.6 zmin = 0 zmax = 5 plot_simple = plot([U_simple(z), p*z, F(z,p)], (z, zmin, zmax), axes_labels=['$z$','$F(z)$'], legend_label = ["Utility from consumption", "Cost of consumption", "Objective function"]) plot_simple.set_legend_options(handlelength=2, borderaxespad = 0, labelspacing =.05, bbox_to_anchor=(1.05, 1), loc=2) show(plot_simple, ymin = -2, ymax = 8, figsize = 10)
Image in a Jupyter notebook

From the graph we can see that the optimal quantity of good, z∗z^* is around 22. As we previously stated, the maximum will be at the point where the slope of the Objective function is equal to zero.

@interact def tangent_line(z0 = slider(0, 5, .1, default = 1)): df(z) = diff(F(z, p=.6), z) tanf(z) = F(z=z0, p =.6) + df(z=z0)*(z-z0) plot_simple_tan = plot([U_simple(z), p*z, F(z,p), tanf], zmin, zmax, axes_labels=['$z$','$F(z)$'], legend_label = ["Utility from consumption", "Cost of consumption", "Objective function", "Slope of Objective function"]) plot_simple_tan.set_legend_options(handlelength=2, borderaxespad = 0, labelspacing =.05, bbox_to_anchor=(1.05, 1), loc=2) show(plot_simple_tan, ymin = -2, ymax = 10, figsize = 10)

We can then compute the FOCFOC, equate it to zero and solve for zz to identify precisely the optimal quantity.

FOC_simple = derivative(F(z, p), z) z_star = solve(FOC_simple == 0, z)[0] html(latex(z_star))

So it is z∗=115=2.2z^*=\frac{11}{5}=2.2. So it seems that our student of interest will usually get a couple coffe, which sounds about right. We already know from the plot that the FOCFOC is identifying a maximum, but we can express it more concisely by reporting the SOCSOC:

SOC = derivative(F(z, p), z, z) html("SOC=" + latex(SOC) + "<0")

During the course we will generally assume interior so you will not need to worry about second order conditions

3. Constrained Optimization

In this course we will only deal with choices over two goods at most. The general form of these constrained optimization problem is:

maxx,zU(x,z) s.t. w=pxx+pyymax_{x,z}U(x,z)\mbox{ } s.t.\mbox{ } w=p_xx+p_yy

That we can define in SageMath as:

var('x, z') U = function('U'); html("U="+latex(U(x, z)))

Here are presented three strategies to solve this problem:

  • A - Substitution

  • B - Lagrangean optimization

  • C - The Equimarginal principle

A - Substitution

An optimization problem in two variables x,z{x, z} where the constraints define a relation between the two goods, can be restated as a single variable one by substitution. Indeed, once the quantity of the first good is chosen so it is the money left to be spent on the other. Chosing xx then implies a choice for zz and the two-variable maximization problem boils down to the optimal choice of just one of them.

Analytically, in order to characterize the solution to the problem using substitution:

  1. Use the constraints of the problem to identify the relationship between the two goods (that is, the budget constraint). Let's express zz as a function of xx.

  2. Substitute zz in the objective function (i.e. UU) with the expression obtained in point 1 so to restate the problem as an unconstrained single-variable one (the constraint is now embeeded into the objective function due to the substitution).

  3. Solve the problem as an uncostrained maximization one by computing the FOC=dUxFOC=\frac{dU}{x} and imposing FOC=0FOC=0 .

Algebraically, it is:

1. Identify the relationship between the two goods

Define the second constraint

var('p_x, p_z, w') cnstr= w == p_x*x+p_z*z html(latex(cnstr))

Solve the constraint for zz

z_cnstr = solve(cnstr, z)[0].rhs() html("z_{cnstr}="+latex(z_cnstr))
2. Substitute zcnstrz_{cnstr} in the objective function
function('U_sub'); U_sub(x) = U(x, z_cnstr) html("U_{sub}="+latex(U_sub(x)))
3. Solve the problem as an uncostrained maximization one
FOC_sub = diff(U_sub(x), x) html("FOC_{sub}="+latex(FOC_sub) + "=0")

Here, the notation Dn(U)\mathrm{D}_n(U) stands for the derivative of the function UU relative to argument in the nthnth position. We can get back to the usual (Leibniz) notation:

pretty_FOC_sub = FOC_sub.substitute({FOC_sub.operands()[0]:-(p_x/p_z)*diff(U(x), x), FOC_sub.operands()[1]:diff(U(x), x)}) html("FOC_{sub}="+latex(pretty_FOC_sub) + "=0")

That can be re-arranged as:

pretty_FOC_sub_eq = pretty_FOC_sub==0 pretty_FOC_sub_eq = pretty_FOC_sub_eq.subtract_from_both_sides(diff(U(x), x)).divide_both_sides(-p_x) html("FOC_{sub}="+latex(pretty_FOC_sub_eq))

So, the solution of this problems will be such that

  1. The optimal quantity of good zz consumed will be z∗=pxx−wpzz^*= \frac{p_xx−w}{pz} to satisfy the budget constraint

  2. The optimal quantities of goods consumed x∗,z∗{x^*, z^*} needs to satisfy the condition FOCsubFOC_{sub}

We will get back to the interpretation of the second point soon.

B - Lagrangean Optimization

An alternative method for optimization is the Lagrangean method. This technique allows to embed the budget constraint in the maximization and is particularly useful with choices involving more than two goods.

Analytically, in order to characterize the solution to the problem using the Lagrangean method:

  1. Collect the terms of the budget constraint on one side - we will refer to this expression as BCcollectBC_{collect}.

  2. Define the Lagrangean Function L\mathcal{L} by adding BCcollectBC_{collect} multiplied by the lagrangean multiplier λ\lambda to the objective function of the problem

L(x,z,λ)=U(x,z)+λ(w−pxx−pyy)\mathcal{L}(x, z, \lambda) = U(x,z) + \lambda(w-p_xx-p_yy)
Notice that, when the constraint is fulfilled and the individual uses his entire budget, the additional term becomes zero (including the multiplier $\lambda$). Furthermore, the additional term implies that, whenever the budget constraint is not binding, there is margin for an improvement of the objective function by reallocation. Crucially, adding the budget constraint to the objective function does not alter the problem at the optimum. So, the combination of $x, z$ that maximizes $\mathcal{L}$ also maximizes $U$. To sum up, the addition of the constraint as in the $Lagrangean$ identifies a maximum that is both satisfying the constraint and that identifies a maximum for the underlying objective function $U$.
  1. Evaluate the FOCsFOCs of this new problem L(x,z,λ)\mathcal{L}(x, z, \lambda) (relative to the variables x,z,λx, z, \lambda) and solve the system FOCs=0FOCs=0 to characterize the solution.

1. Use the constraints to restate the budget constraint
BC_collect = cnstr.subtract_from_both_sides(p_x*x+p_z*z).lhs() html("BC_{collect}=" + latex(BC_collect))
2. Define the Lagrangean Function L\mathcal{L}
var('l', latex_name='\lambda') L(x, z, l) = U(x, z) + l*(BC_collect) html(LatexExpr("\mathcal{L}=") + latex(L(x, z, l)))
3. Evaluate the FOC
DLDx = diff(L(x, z, l), x) html(LatexExpr("\\frac{\partial \mathcal{L}}{\partial x}=") + latex(DLDx) + "=0")
DLDz = diff(L(x, z, l), z) html(LatexExpr("\\frac{\partial \mathcal{L}}{\partial z}=") + latex(DLDz) + "=0")
DLDl = diff(L(x, z, l), l) html(LatexExpr("\\frac{\partial \mathcal{L}}{\partial \lambda}=") + latex(DLDl) + "=0")

The system {∂L∂x=0\frac{\partial \mathcal{L}}{\partial x}=0, ∂L∂z=0\frac{\partial \mathcal{L}}{\partial z}=0, ∂L∂λ=0\frac{\partial \mathcal{L}}{\partial \lambda}=0} provides a characterization of the solution.

In particular, ∂L∂λ=0\frac{\partial \mathcal{L}}{\partial \lambda}=0 implies a binding budged constraint. Moreover, by inspecting ∂L∂x=0\frac{\partial \mathcal{L}}{\partial x}=0 and ∂L∂z=0\frac{\partial \mathcal{L}}{\partial z}=0 we can see that :

Equation ∂L∂x=0\frac{\partial \mathcal{L}}{\partial x}=0 can be re-arranged

l_DLDx = solve(DLDx==0, l)[0] html(latex(l_DLDx))

similarly, for equation ∂L∂z=0\frac{\partial \mathcal{L}}{\partial z}=0 it is

l_DLDz = solve(DLDz==0, l)[0] html(latex(l_DLDz))

and, equating λ\lambda we get:

html("FOC_{\mathcal{L}_{2-3}=}" + latex(l_DLDx.rhs()) + "=" + latex(l_DLDz.rhs()))

the same condition stated in FOCsubFOC_{sub}. These conditions are particular case of:

C - The Equimarginal principle

The equi-marginal principle states that maximization of a given objective function is attained when the allocation of resources among different alternatives is such that the utility derived from the last unit of money spent on each is equal. In our setting, the individual will maximize his objective function when the marginal utility per euro spent is the same across the goods he can buy.

We can rewrite this condition as:

EMP = l_DLDx.rhs() == l_DLDz.rhs() EMP = EMP.divide_both_sides(diff(U(x, z), z)).multiply_both_sides(p_x) html("MRS_{xz}=" + latex(EMP))

Where the left-hand-side is the marginal rate of substitution, the rate at which the consumer is willing to trade off between the two goods). This condition has a simple graphical interpretation: it identifies the solution as the point where the slope of the indifference curve MRSxzMRS_{xz} is the same of the slope of the budget constraint px/pzp_x/p_z.