-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopt.html
380 lines (373 loc) · 30.8 KB
/
opt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction to portfolio optimization — meva 0.0.4 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.0.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Optimizers" href="anopt.html" />
<link rel="prev" title="MEVA" href="intro.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="anopt.html" title="Optimizers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="intro.html" title="MEVA"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">meva 0.0.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="introduction-to-portfolio-optimization">
<h1><a class="toc-backref" href="#id1">Introduction to portfolio optimization</a><a class="headerlink" href="#introduction-to-portfolio-optimization" title="Permalink to this headline">¶</a></h1>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#introduction-to-portfolio-optimization" id="id1">Introduction to portfolio optimization</a><ul>
<li><a class="reference internal" href="#objective-function" id="id2">Objective function</a></li>
<li><a class="reference internal" href="#minimum-variance" id="id3">Minimum variance</a></li>
<li><a class="reference internal" href="#mean-variance" id="id4">Mean–variance</a></li>
<li><a class="reference internal" href="#general-linear-equality-constraints" id="id5">General linear-equality constraints</a></li>
<li><a class="reference internal" href="#soft-constraints" id="id6">Soft constraints</a></li>
<li><a class="reference internal" href="#inverse-of-covariance-matrix" id="id7">Inverse of covariance matrix</a></li>
<li><a class="reference internal" href="#numerical-optimization" id="id8">Numerical optimization</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="objective-function">
<h2><a class="toc-backref" href="#id2">Objective function</a><a class="headerlink" href="#objective-function" title="Permalink to this headline">¶</a></h2>
<p>In mean-variance portfolio optimization we maximize, subject to various
constraints on the portfolio weights <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/>, the objective function</p>
<div class="math" id="equation-eq_L">
<p><span class="eqno">(1)<a class="headerlink" href="#equation-eq_L" title="Permalink to this equation">¶</a></span><img src="_images/math/69e291ebed123fe0503fff380a3e3033da262847.png" alt="L = \rho x' \mu - \frac{1}{2}x' V x,"/></p>
</div><p>where <img class="math" src="_images/math/9a51ab9a0b521705e1e8762fac6bdd6f11771758.png" alt="\rho"/> (a scalar) is the risk tolerance, <img class="math" src="_images/math/d79e8a2c7ce54906c2b25549da38bdbe02cf40d6.png" alt="\mu"/> (a <img class="math" src="_images/math/0513e8867bb00ee895bfb106e3d72f4b1a607309.png" alt="n
\times 1"/> vector) is the expected return, <img class="math" src="_images/math/fae0e7a73748991e5540d874416000583f64f58e.png" alt="V"/> is the (<img class="math" src="_images/math/6804df8649f0c07996685c51e663fa404605d946.png" alt="n \times n"/>)
covariance matrix of asset returns, <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> (<img class="math" src="_images/math/87b4baf6267e3f4ad66197c9d824b73d747ab334.png" alt="n \times 1"/>) is a vector
of portfolio weights, and <img class="math" src="_images/math/e11f2701c4a39c7fe543a6c4150b421d50f1c159.png" alt="n"/> is the number of assets.</p>
<p>Because we like return (the first term in Eq <a class="reference internal" href="#equation-eq_L">(1)</a>) but don’t like
variance (second term), we want to find the portfolio <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> that maximizes
Eq <a class="reference internal" href="#equation-eq_L">(1)</a>. To do so, the derivative of <img class="math" src="_images/math/ae2b750f71e1fc0daaa3de9a85d42794d7cd1326.png" alt="L"/> is more useful than
<img class="math" src="_images/math/ae2b750f71e1fc0daaa3de9a85d42794d7cd1326.png" alt="L"/> itself:</p>
<div class="math" id="equation-eq_dLdx">
<p><span class="eqno">(2)<a class="headerlink" href="#equation-eq_dLdx" title="Permalink to this equation">¶</a></span><img src="_images/math/f3050509211c21121cef772e08fa90f7140fbf7d.png" alt="\frac{\partial L}{\partial x}&= \rho \mu - \frac{1}{2}(V + V')x \\
&= \rho \mu - Vx"/></p>
</div><p>where we have made use of the fact that <img class="math" src="_images/math/840c188c69a93ddb38d6d00c7de07a7fec314408.png" alt="\frac{\partial}{\partial x}x'Ax
= (A + A')x"/> and that the covariance matrix <img class="math" src="_images/math/fae0e7a73748991e5540d874416000583f64f58e.png" alt="V"/> is symmetric (i.e.
<img class="math" src="_images/math/a4330b2b587db0a9387d61908426be652ffd25f5.png" alt="V = V'"/>).</p>
<p>Here’s some more matrix algebra that will come in handy:</p>
<blockquote>
<div><ul class="simple">
<li><img class="math" src="_images/math/d872cfe2f94fd40e7756939092c6ea24326ea321.png" alt="(AB)^{-1} = B^{-1}A^{-1}"/></li>
<li><img class="math" src="_images/math/e7cde0c6edea5a1459acb45ea31bf7c226345ebf.png" alt="(A+B)' = A' + B'"/></li>
<li><img class="math" src="_images/math/dca032d6d51ed851106a295823ebf5dd379fca0d.png" alt="(AB)' = B' A'"/></li>
<li><img class="math" src="_images/math/551af6c785fe399304967516cbfd5412740b4a19.png" alt="AI=IA"/></li>
<li><img class="math" src="_images/math/a8948c145b1796917e9562c441878a1ce4833cc0.png" alt="(A')^{-1} = (A^{-1})'"/></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="minimum-variance">
<h2><a class="toc-backref" href="#id3">Minimum variance</a><a class="headerlink" href="#minimum-variance" title="Permalink to this headline">¶</a></h2>
<p>Let’s start by finding the minimum variance portfolio subject to the constraint
that the weights <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> sum to <img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/>. (If we did not add a sum to
<img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> constraint then the minimum variance portfolio would be to have zero
weight in each asset.)</p>
<p>Because the sum to <img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> constraint is a linear equality constraint
(<img class="math" src="_images/math/a7660cf70423d61ad9bee4caf6cec73ff65f787d.png" alt="x'\mathbf{1} = k"/>), we can use the <a class="reference external" href="http://en.wikipedia.org/wiki/Lagrange_multiplier">Lagrange multiplier</a> method to add the
constraint to Eq <a class="reference internal" href="#equation-eq_L">(1)</a>:</p>
<div class="math">
<p><img src="_images/math/a5088dcf6f132a6af12ef9be1520e8490ed1abb2.png" alt="L = \rho x' \mu - \frac{1}{2} x'Vx + \lambda (x'\mathbf{1} - k)"/></p>
</div><p>where <img class="math" src="_images/math/826f020edbab7d71dec3c5bd8644a4453b9b6e22.png" alt="\mathbf{1}"/> is a <img class="math" src="_images/math/87b4baf6267e3f4ad66197c9d824b73d747ab334.png" alt="n \times 1"/> vector of ones.</p>
<p>Setting <img class="math" src="_images/math/9a51ab9a0b521705e1e8762fac6bdd6f11771758.png" alt="\rho"/> to zero since we have no tolerance for risk, gives:</p>
<div class="math">
<p><img src="_images/math/d3d574ca4b5df44944e28b16e1157e0758f186f6.png" alt="L = - \frac{1}{2} x'Vx + \lambda (x'\mathbf{1} - k)"/></p>
</div><p>Setting the first derivative of the Langrangian (with respect to a change in
the portfolio weights) to zero and solving for the minimum variance portfolio
gives</p>
<div class="math" id="equation-eq_x">
<p><span class="eqno">(3)<a class="headerlink" href="#equation-eq_x" title="Permalink to this equation">¶</a></span><img src="_images/math/8fd7e1b87723b3f04b099bf7e2e4945fae5ea062.png" alt="\frac{\partial L}{\partial x} &= 0 \\
- \frac{1}{2} (V + V')x + \lambda \mathbf{1}&= 0\\
Vx &= \lambda \mathbf{1}\\
V^{-1}Vx &= \lambda V^{-1} \mathbf{1}\\
x &= \lambda V^{-1} \mathbf{1}"/></p>
</div><p>But what is the value of the Langrange multipler <img class="math" src="_images/math/76f1d8ace30435987c01a00ca53a71cba1f40e6c.png" alt="\lambda"/>? From the
sum-to-<img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> constaint, <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> is also given by</p>
<div class="math" id="equation-eq_x1">
<p><span class="eqno">(4)<a class="headerlink" href="#equation-eq_x1" title="Permalink to this equation">¶</a></span><img src="_images/math/5ecd47bea357f71704ee1ede9d161671a0f4a594.png" alt="x' \mathbf{1} = k"/></p>
</div><p>Substituting Eq <a class="reference internal" href="#equation-eq_x">(3)</a> into Eq <a class="reference internal" href="#equation-eq_x1">(4)</a> gives</p>
<div class="math" id="equation-eq_lambda">
<p><span class="eqno">(5)<a class="headerlink" href="#equation-eq_lambda" title="Permalink to this equation">¶</a></span><img src="_images/math/2e46ff4394adb445a09b0323d2773fc07baa32d2.png" alt="\left(\lambda V^{-1} \mathbf{1}\right) ' \mathbf{1} &= k\\
\lambda (V^{-1} \mathbf{1}) ' \mathbf{1} &= k\\
\lambda \mathbf{1}' V^{-1} \mathbf{1} &= k\\
\lambda &= \frac{k}{ \mathbf{1}' V^{-1} \mathbf{1}}"/></p>
</div><p>Finally substituting Eq <a class="reference internal" href="#equation-eq_lambda">(5)</a> into Eq <a class="reference internal" href="#equation-eq_x">(3)</a> gives the minimum
variance portfolio that sums to <img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/>:</p>
<div class="math">
<p><img src="_images/math/752e72b7e27470be7154ff152fc701f22cb22172.png" alt="x &= \left( \frac{k}{ \mathbf{1}' V^{-1} \mathbf{1}} \right) V^{-1}
\mathbf{1}\\
x &=k \frac{V^{-1} \mathbf{1}}{ \mathbf{1}' V^{-1} \mathbf{1}}"/></p>
</div></div>
<div class="section" id="mean-variance">
<h2><a class="toc-backref" href="#id4">Mean–variance</a><a class="headerlink" href="#mean-variance" title="Permalink to this headline">¶</a></h2>
<p>Next, let’s find the optimal mean-variance portfolio without a constrait on
the sum of the portfolio weights.</p>
<p>We wish to find the <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> that maximizes Eq <a class="reference internal" href="#equation-eq_L">(1)</a>. To do so we take
the derivative of <img class="math" src="_images/math/ae2b750f71e1fc0daaa3de9a85d42794d7cd1326.png" alt="L"/> (as is already done in Eq <a class="reference internal" href="#equation-eq_x">(3)</a>) and solve
for <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/> to get the optimimal portfolio with no constraint on the sum
of the weights:</p>
<div class="math">
<p><img src="_images/math/b2b3a239741334f696ea98b082d6c14b029ff084.png" alt="Vx &= \rho \mu \\
x &= \rho V^{-1} \mu."/></p>
</div><p>Let’s now add a sum-to-<img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> constraint.</p>
<p>Given that we are optimizing a convex function subject to linear equality
constraints (sum-to-<img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/>), the method of Lagrange is guaranteed to
produce an optimal solution. The Lagrangian is</p>
<div class="math">
<p><img src="_images/math/a5088dcf6f132a6af12ef9be1520e8490ed1abb2.png" alt="L = \rho x' \mu - \frac{1}{2} x'Vx + \lambda (x'\mathbf{1} - k)"/></p>
</div><p>Setting the first derivative equal to zero and simplifying gives</p>
<div class="math" id="equation-eq_xrho">
<p><span class="eqno">(6)<a class="headerlink" href="#equation-eq_xrho" title="Permalink to this equation">¶</a></span><img src="_images/math/7fa5951bc64768172399bade4c7deee2b3b34772.png" alt="\frac{\partial L}{\partial x} &= 0 \\
\rho \mu - Vx + \lambda \mathbf{1}&= 0 \\
Vx &= \left(\rho \mu + \lambda \mathbf{1}\right) \\
V^{-1}Vx &= \left( \rho V^{-1} \mu + \lambda V^{-1} \mathbf{1} \right) \\
x &= \left(\rho V^{-1} \mu + \lambda V^{-1} \mathbf{1}\right)"/></p>
</div><p>Substituting Eq <a class="reference internal" href="#equation-eq_xrho">(6)</a> in Eq <a class="reference internal" href="#equation-eq_x1">(4)</a>:</p>
<div class="math" id="equation-eq_lambdarho">
<p><span class="eqno">(7)<a class="headerlink" href="#equation-eq_lambdarho" title="Permalink to this equation">¶</a></span><img src="_images/math/554b58ed003033053ec26323bc4819538e90736b.png" alt="\left(\rho V^{-1} \mu + \lambda V^{-1} \mathbf{1}\right)' \mathbf{1}&= k\\
\rho \mu' V^{-1} \mathbf{1} + \lambda \mathbf{1}' V^{-1} \mathbf{1} &= k \\
\lambda \mathbf{1}' V^{-1} \mathbf{1}
&= k - \rho \mu' V^{-1} \mathbf{1} \\
\lambda &= \frac{k - \rho \mu' V^{-1} \mathbf{1}}
{\mathbf{1}' V^{-1} \mathbf{1}}"/></p>
</div><p>Finally substituting Eq <a class="reference internal" href="#equation-eq_lambdarho">(7)</a> into Eq <a class="reference internal" href="#equation-eq_xrho">(6)</a> gives
the portfolio that, given the sum-to-<img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> constraint, makes the optimal
trade-off between expected return and variance:</p>
<div class="math">
<p><img src="_images/math/bb007ef297bc7b6fad7bdc3f2186d71ef1446a9e.png" alt="x = \rho \left( V^{-1} \mu +
\frac{k - \mu' V^{-1} \mathbf{1}}
{\mathbf{1}' V^{-1} \mathbf{1} } V^{-1} \mathbf{1} \right)"/></p>
</div></div>
<div class="section" id="general-linear-equality-constraints">
<h2><a class="toc-backref" href="#id5">General linear-equality constraints</a><a class="headerlink" href="#general-linear-equality-constraints" title="Permalink to this headline">¶</a></h2>
<p>In the preceding section we described mean-variance optimization under a linear
equality constraint of <img class="math" src="_images/math/a7660cf70423d61ad9bee4caf6cec73ff65f787d.png" alt="x'\mathbf{1} = k"/>. We can generalize that result
so that it applies to multiple, simultaneous linear equality constraints of the
form <img class="math" src="_images/math/786b0e7853d3ee7a7bb8003a4855e227539b51d1.png" alt="a'x = b"/>.</p>
<p>We want to maximize the utility</p>
<div class="math">
<p><img src="_images/math/a8b89bdd497db9ddf74f4b0bc0ed63a8a7bab376.png" alt="\rho x' \mu - \frac{1}{2} x'Vx"/></p>
</div><p>subject to the constraints <img class="math" src="_images/math/68b5f9a665563dcd248be686e8cd30fb9bcb40d8.png" alt="a_k'x = b_k"/> for constraints <img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/>,
<img class="math" src="_images/math/6e55439fa6f66ff33d1b3c4c0dadf6c90765b882.png" alt="k=0,...,K-1"/>. The Lagrangian for this problem is</p>
<div class="math">
<p><img src="_images/math/05e708099d4314eaf66a1a18214b326c768ed6d8.png" alt="L = \rho x' \mu - \frac{1}{2} x'Vx + \sum_k \lambda_k (a_k'x - b_k)."/></p>
</div><p>Taking the partial of <img class="math" src="_images/math/ae2b750f71e1fc0daaa3de9a85d42794d7cd1326.png" alt="L"/> with respect to <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/>, we find that</p>
<div class="math">
<p><img src="_images/math/b16b27d083b987690c0ac83a0f0de72849630e13.png" alt="\frac{\partial L}{\partial x} &= \rho\mu - Vx + \sum_k \lambda_k a_k,"/></p>
</div><p>and setting this to zero yields</p>
<div class="math">
<p><img src="_images/math/7971bc055f9936a4fd049cf4dd6d94e0f148f3fb.png" alt="x = \rho V^{-1}\mu + \sum_k \lambda_k V^{-1} a_k."/></p>
</div><p>Substituting this solution into each constraint yields, for each
<img class="math" src="_images/math/2c5272697b0fb0a6a7d4694304a2541b685e2ec4.png" alt="j\le K"/>,</p>
<div class="math">
<p><img src="_images/math/7e2ab95c12cfa4771b5cf21f9e6f2e6120577938.png" alt="a_j' \rho V^{-1}\mu + \sum_k \lambda_k a_j' V^{-1} a_k &= b_j \\
\sum_k \lambda_k a_j' V^{-1} a_k &= b_j - a_j' \rho V^{-1}\mu,"/></p>
</div><p>which can be expressed in matrix form as</p>
<div class="math">
<p><img src="_images/math/b870d254fd1dec08b8a8e155bbe9a7f07854e9b2.png" alt="\left[a_j' V^{-1} a_k\right] \left(\lambda_k\right)
= \left(b_j - a_j' \rho V^{-1} \mu\right),"/></p>
</div><p>where square brackets indicate a matrix and parentheses indicate a column
vector, and where <img class="math" src="_images/math/6b21e0b0899a0d2879d3b8019087fa630bab4ea2.png" alt="j"/> indexes columns and <img class="math" src="_images/math/0b7c1e16a3a8a849bb8ffdcdbf86f65fd1f30438.png" alt="k"/> indexes rows.</p>
<p>By solving the matrix equation above, we can find the <img class="math" src="_images/math/8f9c88566f7a9909b5b529a6b6bfa6e3430a7d9c.png" alt="\lambda_k,"/> and
this in turn allows us to find the optimal <img class="math" src="_images/math/a59f68a4202623bb859a7093f0316bf466e6f75d.png" alt="x"/>.</p>
</div>
<div class="section" id="soft-constraints">
<h2><a class="toc-backref" href="#id6">Soft constraints</a><a class="headerlink" href="#soft-constraints" title="Permalink to this headline">¶</a></h2>
<p>In this section we add soft linear equality constraints. Soft constraints are
like equality constraint but instead of insisting on equality at all cost, we
allow a deviation from equality. A penalty proportional to the squared
deviation from equality is subtracted from the objective function</p>
<p>A generic quadratic penality <img class="math" src="_images/math/bcb2457ac9d8995a4f34d57cadac7ecbbe58f3bd.png" alt="Z"/> can be expressed as</p>
<div class="math">
<p><img src="_images/math/d07c9a0dedd3d51d04bbea4d73a9dafe23f046ad.png" alt="Z = - \theta (x'q - q_{0})^{2},"/></p>
</div><p>where <img class="math" src="_images/math/3be04d4207434584251f6921820c24ac9fa8c6f1.png" alt="\theta"/> is a scalar that tells us the strength of the penality,
<img class="math" src="_images/math/620a3ce6403ec82f1347af9985bc03f7a9382f4a.png" alt="q"/> is a <img class="math" src="_images/math/87b4baf6267e3f4ad66197c9d824b73d747ab334.png" alt="n \times 1"/> vector such as a vector of stock betas, and
<img class="math" src="_images/math/8d4ad0cc77347f70e12a5e6ece6f01d893e53fd4.png" alt="q_{0}"/> is a scalar away from which we penalize values of <img class="math" src="_images/math/2591b69a82df91ebb79c4aacd5351451d11907fd.png" alt="x'q"/>.
Simplifying gives</p>
<div class="math" id="equation-eq_soft">
<p><span class="eqno">(8)<a class="headerlink" href="#equation-eq_soft" title="Permalink to this equation">¶</a></span><img src="_images/math/f4cf76258081866db14b85c9d8d333dc57668134.png" alt="Z&= - \theta \left( x'q x' q - 2 x' q q_{0} + q_{0}^{2} \right) \\
&= - \theta x'q q' x + 2 \theta x' q q_{0} - \theta q_{0}^{2}"/></p>
</div><p>Adding the quadratic penalty <img class="math" src="_images/math/bcb2457ac9d8995a4f34d57cadac7ecbbe58f3bd.png" alt="Z"/> to the objective function <img class="math" src="_images/math/ae2b750f71e1fc0daaa3de9a85d42794d7cd1326.png" alt="L"/> in
Eq <a class="reference internal" href="#equation-eq_soft">(8)</a> gives</p>
<div class="math">
<p><img src="_images/math/7947776fc8aee3a6c002ad8f64463361b28940c4.png" alt="L &= \rho x' \mu - \frac{1}{2}x' V x - \theta x'q q' x + 2 \theta x' q q_{0} - \theta q_{0}^{2} \\
&= \rho x' \mu + 2 \theta x' q q_{0} - x' \left( \frac{1}{2} V + \theta q q' \right) x - \theta q_{0}^{2}\\
&= \rho x' \left( \mu + \frac{2 \theta q_{0}}{\rho} q \right) - \frac{1}{2} x' \left( V + 2 \theta q q'\right) x - \theta q_{0}^{2}\\
&= \rho x' \mu_{q} - \frac{1}{2} x' V_{q} x - \theta q_{0}^{2},"/></p>
</div><p>where</p>
<div class="math">
<p><img src="_images/math/426448aa5500d59c4bebc266c1b8c44e97def429.png" alt="\mu_{q} &= \mu + 2 \frac{\theta q_{0}}{\rho} q \\
V_{q} &= V + 2 \theta q q'."/></p>
</div><p>So adding soft linear equality constraints to an algorithm that finds
mean-variance optimal portfolios could not be easier—no modification is
necessary. We need only to adjust the expected return and the covariance
matrix.</p>
</div>
<div class="section" id="inverse-of-covariance-matrix">
<h2><a class="toc-backref" href="#id7">Inverse of covariance matrix</a><a class="headerlink" href="#inverse-of-covariance-matrix" title="Permalink to this headline">¶</a></h2>
<p>The equations above take the inverse of the <img class="math" src="_images/math/6804df8649f0c07996685c51e663fa404605d946.png" alt="n \times n"/> covariance
matrix of asset returns, where <img class="math" src="_images/math/e11f2701c4a39c7fe543a6c4150b421d50f1c159.png" alt="n"/> is the number of assets.</p>
<p>Of the two meva optimizers, <cite>aopt</cite> and <cite>nopt</cite>, only <cite>aopt</cite> takes an
explicit inverse. In fact the bottleneck of the analytical optimizer <cite>aopt</cite> is
typically in finding the inverse of the covariance matrix.</p>
<p>Here’s a trick that for 500 assets and 10 risk factors, speeds up <cite>aopt</cite> by a
factor of 7. (The downside of the trick is that the individual variance,
<img class="math" src="_images/math/3cf5aa5abe312e6fd4996975341a43aa566e8d8d.png" alt="D"/>, can no longer be zero.)</p>
<p>Lemma 0.28 of Edgar Brunner’s lecture notes (October 14, 2002) states: “Let
<img class="math" src="_images/math/3d046d173a3b176ee92db36ba42ed44ffa076966.png" alt="E=E_{m \times n}"/>, <img class="math" src="_images/math/43a4d13ab00bde0c80732e263a1d3d149ae17c60.png" alt="F=E_{m \times n}"/>, <img class="math" src="_images/math/e97ebd756982daea595bf9a006612803ae6f7224.png" alt="C=C_{m \times
m}"/> and assume that the inverses <img class="math" src="_images/math/a8ebdbc25ca4abaf2eecfb3f743bdc77806d12cc.png" alt="C^{-1}"/> and <img class="math" src="_images/math/209c6cb2358e0793423e953b5d653e0b2d40f519.png" alt="(C - EF)^{-1}"/>
exist. Then,</p>
<div class="math" id="equation-eq_qi">
<p><span class="eqno">(9)<a class="headerlink" href="#equation-eq_qi" title="Permalink to this equation">¶</a></span><img src="_images/math/2a9e6e48e820add122b627747a2e027633d3a0e2.png" alt="(C - EF)^{-1} = C^{-1} + C^{-1}E\left( I - FC^{-1}E\right)^{-1}FC^{-1}.""/></p>
</div><p>Making the following substitutions</p>
<div class="math">
<p><img src="_images/math/56c8dc644d151e2ff4f8304de2cc271e57312957.png" alt="C = D\\
E = -B\\
F = B',"/></p>
</div><p>Eq <a class="reference internal" href="#equation-eq_qi">(9)</a> becomes</p>
<div class="math" id="equation-eq_qibd">
<p><span class="eqno">(10)<a class="headerlink" href="#equation-eq_qibd" title="Permalink to this equation">¶</a></span><img src="_images/math/37d107cd21ca8be7b815e280b0f1cd47a6beb58d.png" alt="(D + BB')^{-1} = D^{-1} - D^{-1}B\left( I + B'D^{-1}B\right)^{-1}B'D^{-1}."/></p>
</div><p>Note that</p>
<div class="math">
<p><img src="_images/math/9ac0e86c4fd43527fd957965d707895b8432f1cb.png" alt="\left(B'D^{-1}\right)' = D^{-1}B."/></p>
</div><p>Finally, making the substitution <img class="math" src="_images/math/ae5acce616afeddda7ed12b29f312c278a177d5b.png" alt="Z = B'D^{-1}"/> in Eq <a class="reference internal" href="#equation-eq_qibd">(10)</a>
gives</p>
<div class="math" id="equation-eq_qibdx">
<p><span class="eqno">(11)<a class="headerlink" href="#equation-eq_qibdx" title="Permalink to this equation">¶</a></span><img src="_images/math/e21426cd4c28189fa7934641e2fd058a7c7b5b79.png" alt="(D + BB')^{-1} = D^{-1} - Z'\left(I + XB\right)^{-1}Z."/></p>
</div><p>Let’s try an example. Create a covariance matrix:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">n</span> <span class="o">=</span> <span class="mi">500</span><span class="p">;</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">10</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="n">k</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">cov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
</pre></div>
</div>
<p>Take the inverse the brute-force way:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">timeit</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">cov</span><span class="p">)</span>
<span class="go">10 loops, best of 3: 59.9 ms per loop</span>
</pre></div>
</div>
<p>And now that fast way:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">meva.opt.util</span> <span class="k">import</span> <span class="n">fast_inverse</span>
<span class="gp">>>> </span><span class="n">timeit</span> <span class="n">fast_inverse</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
<span class="go">100 loops, best of 3: 7.1 ms per loop</span>
</pre></div>
</div>
</div>
<div class="section" id="numerical-optimization">
<h2><a class="toc-backref" href="#id8">Numerical optimization</a><a class="headerlink" href="#numerical-optimization" title="Permalink to this headline">¶</a></h2>
<p>For some problems an analytical solution does not exist.</p>
<p>We’ve seen that linear equality constraints can be easily handled analytically.
But what happens when you want the sum of the shorts to be <img class="math" src="_images/math/8b058f6323328c6a0fd741e20b35726ecc4ced5f.png" alt="-1"/> and the
sum of the longs to be <img class="math" src="_images/math/d839e144267ecbb8a87acbc8a7dfda7824a1693e.png" alt="1"/>? In that case we do not know ahead of time
which assets will be short and which will be long.</p>
<p>The numerical optimizer in meva is based on an heuristic, gradient method. The
heuristic part refers to dealing with the complications of a long-short
portfolio with constraints.</p>
<p>See <a class="reference external" href="http://www.stanford.edu/~wfsharpe/mia/opt/mia_opt1.htm">here</a> for
background on the gradient method of portfolio optimization.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/icon.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Introduction to portfolio optimization</a><ul>
<li><a class="reference internal" href="#objective-function">Objective function</a></li>
<li><a class="reference internal" href="#minimum-variance">Minimum variance</a></li>
<li><a class="reference internal" href="#mean-variance">Mean–variance</a></li>
<li><a class="reference internal" href="#general-linear-equality-constraints">General linear-equality constraints</a></li>
<li><a class="reference internal" href="#soft-constraints">Soft constraints</a></li>
<li><a class="reference internal" href="#inverse-of-covariance-matrix">Inverse of covariance matrix</a></li>
<li><a class="reference internal" href="#numerical-optimization">Numerical optimization</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="intro.html"
title="previous chapter">MEVA</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="anopt.html"
title="next chapter">Optimizers</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/opt.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="anopt.html" title="Optimizers"
>next</a> |</li>
<li class="right" >
<a href="intro.html" title="MEVA"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">meva 0.0.4 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011-2012, Keith Goodman.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.4.
</div>
</body>
</html>