In a previous post I included a problem definition and an example. Upon reviewing the post, I discovered that not only were both the problem definition and the example incorrect, they were inconsistent. In this post I aim to correct the problem definition and then reimplement the example with the new problem definition.
Part 1: Correct the Problem Definition
Let's start with a recap of the problem we are trying to solve. Note that I altered the problem definition a bit to correct two major issues:
The constraint in the original problem definition was not in terms of the givens. The requirement is that the expression we optimize must be expressible entirely in the terms we are given. A previous definition omitted the payouts and the potential economic scenarios, both of which are referenced in our constraint equation.
Optimization was not with respect to the correct property. The original expression I defined was optimizing payout minus the cost of the portfolio. But we are not optimizing for dollars at time T, we are optimizing for total utility after T timesteps. Not only does that mean we must convert dollars to utility in this expression but we must consider the utility of consumption both today and at the liquidation period, not just at the liquidation period.
The new problem definition corrects both of these issues by augmenting the list of given quantities and rewriting the objective function in terms of collective utility.
Portfolio Problem Definition |
Given:
Unknown: A portfolio P := {q1, …, qm } where qi is the number of units of asset i held in the portfolio. Constraint: We want to find P* such that the expected utility of consumption is maximized. The total expected utility of consumption can be expressed as the amount of utility consumed today plus the amount of utility consumed after liquidation. Let U(P) be the expected utility of consumption of a portfolio P. We define it as U : P → ℝ where P := ℕM q1, …, qm → utility of today’s consumption + utility of future consumption q1, …, qm → u(ct) + 𝛽 SUMs=1..S Pr(ωs)u(ct+T(ωs)) [1.1] Where ct is the consumption today. We consume whatever we decided to not spend on investment so
ct = TotalFunds - cost of portfolio ct = N - SUMi=1..M qi pi,t [1.2] The amount consumed T steps from now is written as ct+T(ωs). Note that we have a dependence on the outcome because the future is uncertain and the payout of a particular asset is dependent on the economic conditions. The amount consumed T steps from now is the payout from our investments T steps from now (i.e. assume we liquidate at time t+T). ct+T(ωs) = WT(ωs) + SUMi=1..M qi Xi, t+T(ωs) [1.3] Recall from the previous post that the utility function u is defined as u : (0, ∞) → ℝ c → c1-𝛾/(1 - 𝛾) [1.4] If we assume a relative risk aversion of 𝛾 = 1 we can reduce [1.4] to c → log(c) [1.5] Substituting [1.2] through [1.5] into [1.1] gives us q1, …, qm → log(N - SUMi=1..M qi pi,t) + 𝛽 SUMs=1..S Pr(ωs)log(WT(ωs) +SUMi=1..M qi Xi, t+T(ωs)) [1.6] Framing [1.6] as an optimization problem gives us P* = maxq[1]…q[m] log(N - SUMi=1..M qi pi,t) + 𝛽 SUMs=1..S Pr(ωs)log(WT(ωs) +SUMi=1..M qi Xi, t+T(ωs)) [1.7] Subject to the constraints SUMi=1…M qi pi,t < N [1.8a] SUMi=1..M qi Xi, t+T(ωs) > 0 ∀ωs [1.8b] WT(ωs) > 0 ∀ωs [1.8c] Note that the constraint [1.8b] is purely mathematical because we defined the utility function as log(c) and log is undefined in the negative domain. |
Part 2: Correct the Example
Now let's look at the example that was provided. I have revised the definition so that it complements the above problem definition.
Portfolio Problem Instance |
Given:
|
The example from the first blog post tried to solve this problem instance but it did not solve this problem instance by optimizing [1.7] from the corrected problem definition. Let us do that now.
We want to maximize [1.7] subject to [1.8a], [1.8b], and [1.8c] with the values in the Problem Instance box. To constrain [1.7] to our budget we employ the method of Lagrange multipliers. From source [1],
The method of Lagrange multipliers | ||
Let’s suppose we want to find the local max or min of a function f : R2 → R. For unconstrained cases, we find the maximum or minimum by setting the derivative to zero and solving for the point values. For example the local maximum of a concave surface will look like the following: Photo credit: source [2]. The max is the coordinate colored yellow on the plane where the derivative of the function is zero. It is often the case that we want to find the local max or min of a surface subject to some constraint. For example, consider a similar surface except that we want to find the minimum subject to the boundary defined by the level surface in green. Now the minimum occurs not where the derivative is zero but at the boundary of our constraint, as illustrated below. Photo credit: source [2]. Note that the level surface in green defines a set of points D = {[x,y] : g([x,y]) = c} and the boundary of the surface is defined by {z : z = f([x,y]) where [x,y] in D} That is, if we have a problem where we want to find the max or min of a surface subject to a constraint we have to check not only the “flat” points, but the points along the boundary. Note that this is exactly the problem we are trying to solve with our portfolio optimizer. We want to find the optimal allocations but we are constrained by our budget. Now that the motivation is clear, let’s define the problem more formally.
How can we solve for P? We will solve for P by finding a relationship between ▽g and ▽f at max/min points on the boundary that is equivalent to the criterion that the max/min points on the boundary are where the derivative of the boundary is zero. To do this we will express the boundary with a parameterization and identify (1) a relationship between ▽f and the parameterization at max/min points (2) a relationship between ▽g and the parameterization at max/min points (3) reduce (1) and (2) to a relationship between ▽g and ▽f by removing the intermediary parameterization. Let x(t) = [x1(t), …, xN(t)] parameterize the feasible set in RN where g(x(t)) = c for all t. We can visualize the boundary by introducing a lifted parameterization C : R → RN x R defined by t → [[x1(t), …, xN(t)], f([x1(t), …, xN(t)])] where g([x1(t), …, xN(t)]) = c. The second term in the image of C are the boundary points on F. x is the parameterization I mentioned that will enable us to define an expression we can evaluate to find the objective set of points P by relating ▽f to ▽g. Let P0 denote the point x(t0) on the boundary where the extrema occurs. Then
Let us informally explain why [2.1] is true. We say two vectors x and y are orthogonal if xᐧy = 0. To show ▽f(P0) is orthogonal to the velocity vector dx(t0)/dt then we must write out the dot product and show that it must be zero if P0 is a max/min of f at the boundary. We can write ▽f(P0) as [∂f(P0)/∂x1 … ∂f(P0)/∂xN] We can write the velocity vector dx(t0)/dt as [dx1(t0)/dt … dxN(t0)/dt] And the dot product is ▽f(P0) . dx(t0)/dt = ∂f(P0)/∂x1 * dx1(t0)/dt + … + ∂f(P0)/∂xN * dxN(t0)/dt = df(P0)/dt = d/dt [f(x(t))] |t=t0 The extrema of f along the boundary occur at df(x(t))/dt = ▽f(x(t)) * dx(t)/dt = 0. Thus if P0 is an extrema at the boundary then df(P0)/dt = 0 which means that ▽f(P0) . dx(t0)/dt = 0 which means that the gradient of f is orthogonal to the velocity vector of x at local extrema. Now let us show why [2.2] is true. We can write ▽g(P0) as [∂g(P0)/∂x1 … ∂g(P0)/∂xN] And the dot product with dx(t0)/dt is ▽g(P0) . dx(t0)/dt = ∂g(P0)/∂x1 * dx1(t0)/dt + … + ∂g(P0)/∂xN * dxN(t0)/dt = dg(P0)/dt = d/dt [g(x(t))] |t=t0 Note that because g(P0) = c dg(P0)/dt = 0 And thus ▽g(P0) is also orthogonal to dx(t0)/dt. This leads us to conclude that ▽f and ▽g are parallel and we can write ▽f = λ▽g where λ is a scalar multiplier. |
Now that we have a method for solving for the optimal portfolio, let’s apply this method to our particular problem instance.
Method of Lagrange Multipliers Applied to Portfolio Problem Instance |
Given:
Unknown: A portfolio allocation P* = [q1, …, qM] Constraint: P* maximizes [3.1] subject to g(P*) = N. Note that we will assume that the investor invests everything. Otherwise this would be a less than or equal to relationship. |
In the blue box we learned we can solve for P* by solving for P in the expression ▽f(P) = λ▽g(P) subject to g(P) = N. Let us write this out in terms of our portfolio functions to derive the system of equations to solve:
Derive our System of Equations |
▽f(P) = λ▽g(P) ▽f(P) - λ▽g(P) = 0 ∂/∂q[i](log(N-Σi=1..M qi pi,t)+ 𝛽 Σs=1..SPr(ωs)log(WT(ωs)+Σi=1..M qi Xi,t+T(ωs))) - λ(∂/∂q[i](SUMi=1…M qi pi,t) = 0 for i=1,2,3 ∂/∂q[i](log(N-Σi=1..M qi pi,t)) + ∂/∂q[i](𝛽 Σs=1..SPr(ωs)log(WT(ωs)+Σi=1..M qi Xi,t+T(ωs)))) - λpi,t = 0 for i=1,2,3 -pi,t/(N-Σi=1..M qi pi,t) + 𝛽 Σs=1..SPr(ωs) Xi,t+T(ωs)/(WT(ωs)+Σi=1..M qi Xi,t+T(ωs))) - λpi,t = 0 for i=1,2,3 Rewriting this as a single summation over states gives us 𝛽 Σs=1..SPr(ωs) Xi,t+T(ωs)/(WT(ωs)+Σi=1..M qi Xi,t+T(ωs))) - pi,t (λ + 1/(N-Σi=1..M qi pi,t)) = 0 for i=1,2,3 [3.2] Subject to SUMi=1…M qi pi,t = N [3.3] |
After this derivation we have both (a) the system of equations we need to solve and (b) the parameter values (the payouts and outcome probabilities). We can use a simple root finder numerical method to solve this. The root finder will start at a point and iteratively update that point until the image of that point under the system of equations is zero. Let’s dive into the code used to solve this
Solve our System of Equations | ||||||
Root Finder. Given a system of equations F: RN → RM and a starting point q0, the root finder will find the point q in RN s.t. F(q) = 0. Note that here, F is the residual function. In this case, the residual is [3.2] and [3.3]. Note that this is a slow simplistic algorithm but for our toy problem it is a reasonable solution.
Parameter Values. We need to define our payouts and economic scenarios. I have provided these values through a Scenario and Asset class. A model is a function that accepts a set of assets and returns a list of scenarios. The model’s job is to determine what the payout of each asset is under different economic scenarios.
Build Scenarios and Find Portfolio. We will visit the portfolio finder next, but first let us set up the main function that binds the parameter values with the system of equations.
Portfolio Finder. We need to define F and q0 correctly in order to call the rootSolver. The following function does just that. But it blows up (this is why I colored the box red; do not use this code)! How can we fix it? Here is the original code that implements our analysis:
Note the highlighted terms. I prevented the denominators from becoming zero, but even if they are not zero and very small, that highlighted term on line 39 is going to blow up. A huge residual will prevent the root solver from converging. This term came from the logarithmic utility term log(N-Σi=1..M qi pi,t). This represents the utility gained from consuming the leftover funds after purchasing our portfolio. But if we spend everything on our portfolio then this term is zero and the log of 0 is undefined. To solve this, we can say that we have already set aside funds necessary to maintain the lifestyle today and omit that term entirely.
The revised code will find the uninteresting solution of purchasing three units of each asset, or [3,3,3]. |
I hope you enjoyed this walkthrough of how to find the optimal portfolio with respect to utility. Perhaps in a future entry I will productize the toy by improving the numerical solver and wiring in Bayesian models for the world generator.
Sources:
[1] Thomas calculus 11th addition, 2005
[2] https://www.youtube.com/watch?v=5A39Ht9Wcu0
[3] My last blog post, which relied on Asset Pricing, John H. Cochrane, 2001
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment