SlideShare a Scribd company logo
1 of 12
Download to read offline
A few solvers for the Markowitz portfolio selection
problem
September 24, 2016
Abstract
The most commonly suggested tool to solve the Markowitz portfolio selec-
tion problem is Excel, which might be unwieldy for bigger models. The goal
was to verify if other modeling tools can be used for this particular purpose,
both commercial and open-source ones were reviewed including AMPL, Julia,
Python and the C programming language. Some slight differences in numeri-
cal solutions provided by these tools are presented. As some developments in
the modeling of capital markets are attributed to the computational complexity
of the classical Markowitz model, the ease of use and performance were in focus.
The Markowitz (1952) portfolio selection model (see also Markowitz (1959))
was in itself quite simple but the computational part of the problem was so
troublesome that it lead to different simplifications (see Sharpe (1963), Sharpe
(2000)). The development of microcomputers gave mass access to nonlinear
solvers allowing the solution to the original quadratic problem. The most com-
monly suggested tool to solve the problem is Microsoft Excel1 (see Jackson
(2001), Laws (2003)), which might be unwieldy for bigger models and limits
the number of model variables. Let us remind the classic portfolio problem -
let xi be the portfolio weight invested in security i, µi be the mean, σij be the
covariance of securities i and j, then E is the expected return of a portfolio and
1
Microsoft Excel is a registered trademark of Microsoft Corporation
1
V is its risk measure:
E =
N
i=1
xiµi (1)
V =
N
i=1
N
j=1
xixjσij (2)
N
i=1
xi = 1 (3)
xi 0 for i = 1..N (4)
As we can see, to solve the problem we have to calculate n(n − 1)/2 covariance
pairs and n variances. Two variants are usually considered - maximize expected
return with variance not greater than an assumed value vmax:
E → MAX (5)
V vmax (6)
or minimize variance with expected return not lesser than an assumed value
emin:
V → MIN (7)
E emin (8)
Below you will find implementations of the second variant (minimize the
risk) in four popular languages. This issue seems to be trivial today in terms
of complexity.
Numerical examples are based on stock prices of Bank of America Corpora-
tion, Ford Motor Company, General Electric and AT&T. Returns include both
price gains and dividends and were calculated for twelve consecutive quarters
starting on Jan 1st 2010.
AMPL
AMPL is a commercial tool (see Fourer (1990)), but it is available for students
and teachers at no charge. The tool is well-established among scientists and
has an extensive documentation2 - Fourer (2002). Input for the solver is split
into the model and the data file. Let us name the model file mps.mod:
2
http://ampl.com/resources/the-ampl-book
2
set A; # assets
set T := {1..12};
param Rets {T,A}; # returns on assets
param mean {j in A} = (sum {i in T} Rets[i,j]) / card(T);
param Diff {i in T, j in A} = Rets[i,j] - mean[j];
param Covar {i in A, j in A} =
sum {k in T} (Diff[k,i]*Diff[k,j]) / (card{T}-1);
var X {A} >=0;
var Mean = sum {j in A} mean[j] * X[j];
minimize Risk: sum {i in A} (X[i]*(sum {j in A} Covar[i,j]*X[j]));
subject to TotalOne: sum {j in A} X[j] = 1;
subject to Reve: Mean >= 0.035;
and the data file mps.dat:
set A := BAC F GE T ;
param Rets: BAC F GE T :=
1 0.1383 0.2228 0.1845 -0.0812
2 -0.2029 -0.2019 -0.2079 -0.0574
3 -0.0649 0.158 0.1593 0.1923
4 0.0038 0.3695 0.1265 0.0344
5 -0.0599 -0.1357 0.1045 0.0462
6 -0.1795 -0.0904 -0.0654 0.0398
7 -0.4472 -0.3103 -0.1995 -0.0862
8 0.0072 0.1483 0.2308 0.0891
9 0.6517 0.1258 0.1024 0.0425
10 -0.1539 -0.2361 0.0522 0.1479
11 0.0981 0.0554 0.1166 0.0536
12 0.2969 0.3092 -0.0715 -0.0954
You can now use AMPL graphical user interface - amplide - to run the
following commands:
3
model mps.mod;
data mps.dat;
solve;
display X;
You will get the following result:
MINOS 5.51: optimal solution found.
5 iterations, objective 0.01062155067
Nonlin evals: obj = 11, grad = 10.
ampl: display X;
X [*] :=
BAC 0
F 0.0428628
GE 0.437821
T 0.519316
Julia
Julia is a high-level, high-performance dynamic programming
language for technical computing3.
Julia and its packages4 are licensed under MIT, GPL, MPL, BSD and EPL
licenses. Let us put the Julia code into mps.jl. I omitted covariance calculation
for the sake of clarity:
using JuMP
using Ipopt
N=4
r_min=0.035
m = Model()
C = [0.076900 0.039968 0.018111 -0.000288 ; 0.039968 0.050244
3
http://julialang.org/
4
https://jump.readthedocs.io/en/latest
4
0.019033 -0.000060 ; 0.018111 0.019033 0.021381 0.007511 ;
-0.000288 -0.000060 0.007511 0.008542]
Mean = [0.0073 0.0346 0.0444 0.0271]
@variable(m, 0 <= x[i=1:N] <= 1)
@objective(m,Min,sum{x[j]*sum{x[i]*C[i,j],i=1:N},j=1:N})
@constraint(m, sum{x[i]*Mean[i],i=1:N} >=r_min)
@constraint(m, sum{x[i],i=1:N} ==1.0)
status = solve(m)
for i= 1:N
println(getvalue(x[i]))
end
Then after running julia mps.jl we will get:
EXIT: Optimal Solution Found.
1.3095345121754e-7
0.043278618483219226
0.4378847609266113
0.5188364896367182
You will find more details on Julia in Balbaert (2015).
Python
Let us put the Python code into mps.py5:
from cvxopt import matrix
from cvxopt.solvers import qp
import numpy as np
n = 4
Cov = matrix([[0.076900, 0.039968, 0.018111, -0.000288 ],
[ 0.039968, 0.050244, 0.019033, -0.000060 ],
[ 0.018111, 0.019033, 0.021381, 0.007511 ],
5
https://wellecks.wordpress.com/2014/03/23/portfolio-optimization-with-python
5
[-0.000288, -0.000060, 0.007511, 0.008542 ]])
Mean = matrix([0.0073, 0.0346, 0.0444, 0.0271])
r_min = 0.035
G=matrix(np.concatenate((-np.transpose(Mean),-np.identity(n)),0))
h=matrix(np.concatenate((-np.ones((1,1))*r_min,np.zeros((n,1))),0))
A = matrix(1.0, (1,n))
b = matrix(1.0)
q = matrix(np.zeros((n, 1)))
sol = qp(Cov, q, G, h, A, b)
print(sol[’x’])
Then after having executed python mps.py we will get:
Optimal solution found.
[ 8.47e-07]
[ 4.33e-02]
[ 4.38e-01]
[ 5.19e-01]
Cvxopt6 and NumPy7 are licensed respectively under GPL and BSD (see
also Boyd (2004) and Idris (2015)). The Python language can be learnt with
Lutz (2003).
C
The Ipopt library8 (see Surhone (2011) and Russell (2012)) is distributed with
an example of usage — the hs071
¯
c.c file — with four variables, two con-
straints and goal minimisation, which perfectly fits our need.
The covariance matrix is symmetric so the gradient of the objective is given
6
http://cvxopt.org
7
http://www.numpy.org
8
https://projects.coin-or.org/Ipopt
6
by 







2 ∗ N
i=1 xici1
2 ∗ N
i=1 xici2
2 ∗ N
i=1 xici3
2 ∗ N
i=1 xici4








(9)
and the Jacobian of the constraints g(x) is


µ1 µ2 µ3 µ4
1 1 1 1

 (10)
With such a gradient of the objective the Hessian of the Lagrangian function
is the covariance matrix multiplied by 2.
Make the following changes to adapt this example file to our optimisation
problem. First remove the whole if-block with the second call to IpoptSolve,
most probably between lines 165 and 206. Then put covariance and mean
matrices somewhere at the beginning making them global:
Number c[4][4] =
{{0.076900, 0.039968, 0.018111, -0.000288},
{0.039968, 0.050244, 0.019033, -0.000060},
{0.018111, 0.019033, 0.021381, 0.007511},
{-0.000288, -0.000060, 0.007511, 0.008542}};
Number MoR[4] = {0.0073, 0.0346, 0.0444, 0.0271};
Secondly move the assignment of the number of stocks/variables to the
beginning of the main function and change the hardcoded values of nonzeros in
the Jacobian and the Hessian:
Index n=4;
Index nele_jac = 2*n;
Index nele_hess = n*(n+1)/2;
Then change the lower and upper limits of variables:
7
for (i=0; i<n; i++) {
x_L[i] = 0.0;
x_U[i] = 1.0;
}
and the limits of constraints - the minimum return and the sum of variables:
g_L[0] = 0.035;
g_U[0] = 1e19;
g_L[1] = 1;
g_U[1] = 1;
The eval
¯
f goal function should contain:
Number sum;
int i,j;
*obj_value=0.0;
for (i=0; i<n; i++) {
sum=0.0;
for (j=0; j<n; j++)
sum += x[j] * c[j][i];
*obj_value += x[i]*sum;
}
The eval
¯
grad
¯
f gradient calculation shall change to:
int i,j;
for (i=0; i<n; i++) {
grad_f[i] =0;
for (j=0; j<n; j++)
grad_f[i] += x[j]*c[j][i];
grad_f[i] = grad_f[i] * 2;
}
The two contraints are calculated within eval
¯
g :
8
int i;
g[0]=0.0;
for (i=0; i<n; i++) g[0] += x[i]*MoR[i];
g[0] += my_data->g_offset[0];
g[1]=0;
for (i=0; i<n; i++) g[1] += x[i];
g[1] += my_data->g_offset[1];
Finanaly there are two functions left - the Jacobian, eval
¯
jac
¯
g :
int i=0;
if (values == NULL) {
for (i=0; i<n; i++) {
iRow[i] = 0;
jCol[i] = i;
}
for (i=0; i<n; i++) {
iRow[n+i] = 1;
jCol[n+i] = i;
}
}
else {
for (i=0; i<n; i++)
values[i] = MoR[i];
for ( ; i<n+n; i++)
values[i] = 1;
}
and the Hessian, eval
¯
h :
if (values == NULL) {
idx=0;
for (row = 0; row < n; row++) {
9
for (col = 0; col <= row; col++) {
iRow[idx] = row;
jCol[idx] = col;
idx++;
}
}
}
else {
idx=0;
for (row = 0; row < n; row++) {
for (col = 0; col <= row; col++) {
values[idx] = obj_factor * (2*c[row][col]);
idx++;
}
}
}
You have to remove -pedantic-errors from the Makefile to compile this if you
have not placed variable definitions at the beginning of each function. Running
the program will give the following result:
x[0] = 0.000000e+00
x[1] = 4.327805e-02
x[2] = 4.378846e-01
x[3] = 5.188373e-01
The C programming language was described by Kernighan (1988).
An example with twenty stocks
Let us see the results for twenty stocks9 selected out of S&P100 for twelve
consecutive quarters starting on Jan 1st 2010. The same input data gave slightly
9
Stocks included: AIG BAC IBM CSCO DELL F GE GOOG HAL JPM KO MCD MS ORCL
PFE S T VZ WFC XOM
10
different results for four tools (Julia also uses Ipopt):
Software AMPL Python (Ipopt) C (Ipopt) Excel
Objective (risk) 0.002900 0.002874 0.002898 0.003178
AIG 0.0006 0.0003 0.0209
BAC 0.0067 0.0067 0.0067
IBM 0.1892 0.1890 0.1890 0.2743
KO 0.0718 0.0721 0.0719 0.0989
MCD 0.4291 0.4290 0.4290 0.4852
S 0.0966 0.0965 0.0965 0.1183
VZ 0.2066 0.2060 0.2066 0.0019
Table 1: Portfolio structures computed by different tools
As we can see, Ipopt found the best solution - the portfolio with the lowest risk.
Minor differences for Python and C can be caused by lack of precision in input
data. Excel excludes Verizon (XOM with 0.0005 is not shown in the table).
All tools provide results in the blink of an eye — a model with 200 stocks gets
solved by AMPL within less than one second.
Conclusions
The Markowitz portfolio selection problem is no longer a computational trouble.
There are open-source tools to solve models with hundreds of variables within
milliseconds on a personal computer. You can use the language which suites
you best. Especially Python deserves recommendation as it gets more and
more popular in the scientific community. These tools can be fed with data
in a much more convenient way than editing formulas in an office spreadsheet,
which moreover has the limit of number of variables.
It is found that different tools can suggest different portfolio weights and
greater portfolio variance with the same constraints (see table 1) - another
argument to use open-source tools.
11
References
1. Balbaert, Ivo Getting Started with Julia, Packt Publishing, 2015.
2. Boyd, Stephen, L. Vandenberghe Convex Optimization, Cambridge Univer-
sity Press, 2004.
3. Fourer, Robert, D.M. Gay, B.W. Kernighan ”A Modeling Language for
Mathematical Programming” Management Science, 36 (1990), pp. 519–554.
4. Fourer, Robert, D.M. Gay, B.W. Kernighan AMPL: A Modeling Language
for Mathematical Programming, Duxbury Press, 2002.
5. Idris, Ivan NumPy Beginner’s Guide, Packt Publishing, 2015.
6. Jackson, Mary, M. Staunton Advanced Modelling in Finance using Excel
and VBA, Chichester: John Wiley & Sons, 2001.
7. Kernighan, Brian W., D.M. Ritchie The C Programming Language, Prentice
Hall, 1988.
8. Laws, Jason ”Portfolio Analysis Using Excel” In C. L. Dunis, J. Laws P.
Nam, ed., Applied Quantitative Methods for Trading and Investment, John
Wiley & Sons, 2003.
9. Lutz, Mark Learning Python, 2nd ed. Sebastopol: O’Reilly, 2003.
10. Markowitz, Harry M. ”Portfolio Selection” The Journal of Finance, Vol. 7,
No. 1 (1952), pp. 77–91.
11. Markowitz, Harry M. Portfolio Selection: Efficient Diversification of Invest-
ments, NJ: John Wiley & Sons, 1959.
12. Russell, Jesse, R. Cohn IPOPT, Bookvika Publishing, 2012.
13. Sharpe, William F. ”A Simplified Model for Portfolio Analysis”, Manage-
ment Science, Vol. 19, No. 2 (1963), pp 277–293.
14. Sharpe William F. Portfolio Theory and Capital Markets, McGraw-Hill,
2000.
15. Surhone, Lambert M., M.T. Tennoe, S.F. Henssonow Ipopt, Betascript
Publishing, 2011.
12

More Related Content

What's hot

Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cppAlamgir Hossain
 
Solution of matlab chapter 6
Solution of matlab chapter 6Solution of matlab chapter 6
Solution of matlab chapter 6AhsanIrshad8
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comBaileya126
 
Cis 115 Education Organization -- snaptutorial.com
Cis 115   Education Organization -- snaptutorial.comCis 115   Education Organization -- snaptutorial.com
Cis 115 Education Organization -- snaptutorial.comDavisMurphyB99
 
Cis 115 Effective Communication / snaptutorial.com
Cis 115  Effective Communication / snaptutorial.comCis 115  Effective Communication / snaptutorial.com
Cis 115 Effective Communication / snaptutorial.comBaileyao
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer GraphicsKamal Acharya
 
Curve fitting
Curve fittingCurve fitting
Curve fittingdusan4rs
 
Boolean algebra and logic gates
Boolean algebra and logic gatesBoolean algebra and logic gates
Boolean algebra and logic gatesZareenRauf1
 
Functional Programming In Mathematica
Functional Programming In MathematicaFunctional Programming In Mathematica
Functional Programming In MathematicaHossam Karim
 
Boolean Algebra
Boolean AlgebraBoolean Algebra
Boolean AlgebraHau Moy
 
Image Cryptography and Steganography
Image Cryptography and SteganographyImage Cryptography and Steganography
Image Cryptography and SteganographyMohammad Amin Amjadi
 

What's hot (20)

Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
 
Operation reasearch
Operation reasearchOperation reasearch
Operation reasearch
 
Binary Number System and Codes
Binary Number System and CodesBinary Number System and Codes
Binary Number System and Codes
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Digital logic circuit
Digital logic circuitDigital logic circuit
Digital logic circuit
 
Operations Research
Operations ResearchOperations Research
Operations Research
 
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
 
Solution of matlab chapter 6
Solution of matlab chapter 6Solution of matlab chapter 6
Solution of matlab chapter 6
 
Operation Research
Operation ResearchOperation Research
Operation Research
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.com
 
Cis 115 Education Organization -- snaptutorial.com
Cis 115   Education Organization -- snaptutorial.comCis 115   Education Organization -- snaptutorial.com
Cis 115 Education Organization -- snaptutorial.com
 
Cis 115 Effective Communication / snaptutorial.com
Cis 115  Effective Communication / snaptutorial.comCis 115  Effective Communication / snaptutorial.com
Cis 115 Effective Communication / snaptutorial.com
 
Unit 1(stld)
Unit 1(stld)Unit 1(stld)
Unit 1(stld)
 
Soluciones quiz
Soluciones quizSoluciones quiz
Soluciones quiz
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
Curve fitting
Curve fittingCurve fitting
Curve fitting
 
Boolean algebra and logic gates
Boolean algebra and logic gatesBoolean algebra and logic gates
Boolean algebra and logic gates
 
Functional Programming In Mathematica
Functional Programming In MathematicaFunctional Programming In Mathematica
Functional Programming In Mathematica
 
Boolean Algebra
Boolean AlgebraBoolean Algebra
Boolean Algebra
 
Image Cryptography and Steganography
Image Cryptography and SteganographyImage Cryptography and Steganography
Image Cryptography and Steganography
 

Similar to A few solvers for portfolio selection

Digital electronics k map comparators and their function
Digital electronics k map comparators and their functionDigital electronics k map comparators and their function
Digital electronics k map comparators and their functionkumarankit06875
 
presentation
presentationpresentation
presentation3ashmawy
 
Mm chap08 -_lossy_compression_algorithms
Mm chap08 -_lossy_compression_algorithmsMm chap08 -_lossy_compression_algorithms
Mm chap08 -_lossy_compression_algorithmsEellekwameowusu
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfRajJain516913
 
Matlab practical file
Matlab practical fileMatlab practical file
Matlab practical fileArchita Misra
 
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...IJERA Editor
 
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...IJERA Editor
 
LP linear programming (summary) (5s)
LP linear programming (summary) (5s)LP linear programming (summary) (5s)
LP linear programming (summary) (5s)Dionísio Carmo-Neto
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersSheila Sinclair
 
MITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submissionMITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submissionAshutosh Katti
 
Workshop03.docx lap trinh C cho người mới bắt đầu
Workshop03.docx  lap trinh C cho người mới bắt đầuWorkshop03.docx  lap trinh C cho người mới bắt đầu
Workshop03.docx lap trinh C cho người mới bắt đầulinhtran111111111111
 
Linear programming problems
Linear programming problemsLinear programming problems
Linear programming problemsHanna Elise
 
RSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESRSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESacijjournal
 

Similar to A few solvers for portfolio selection (20)

Digital electronics k map comparators and their function
Digital electronics k map comparators and their functionDigital electronics k map comparators and their function
Digital electronics k map comparators and their function
 
presentation
presentationpresentation
presentation
 
PECCS 2014
PECCS 2014PECCS 2014
PECCS 2014
 
Mm chap08 -_lossy_compression_algorithms
Mm chap08 -_lossy_compression_algorithmsMm chap08 -_lossy_compression_algorithms
Mm chap08 -_lossy_compression_algorithms
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
 
Matlab practical file
Matlab practical fileMatlab practical file
Matlab practical file
 
Matlab1
Matlab1Matlab1
Matlab1
 
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
 
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
Determination of Optimal Product Mix for Profit Maximization using Linear Pro...
 
LP linear programming (summary) (5s)
LP linear programming (summary) (5s)LP linear programming (summary) (5s)
LP linear programming (summary) (5s)
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
MITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submissionMITCOE 2011-12 conm-submission
MITCOE 2011-12 conm-submission
 
Xgboost
XgboostXgboost
Xgboost
 
Linear programing problem
Linear programing problemLinear programing problem
Linear programing problem
 
Workshop03.docx lap trinh C cho người mới bắt đầu
Workshop03.docx  lap trinh C cho người mới bắt đầuWorkshop03.docx  lap trinh C cho người mới bắt đầu
Workshop03.docx lap trinh C cho người mới bắt đầu
 
Report Final
Report FinalReport Final
Report Final
 
Linear programming problems
Linear programming problemsLinear programming problems
Linear programming problems
 
Document from Saikrish.S.pdf
Document from Saikrish.S.pdfDocument from Saikrish.S.pdf
Document from Saikrish.S.pdf
 
RSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESRSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENES
 

More from Bogusz Jelinski

Dispatching taxi cabs with passenger pool
Dispatching taxi cabs with passenger poolDispatching taxi cabs with passenger pool
Dispatching taxi cabs with passenger poolBogusz Jelinski
 
Dispatching taxi / minibuses - about Kabina project
Dispatching taxi / minibuses - about Kabina projectDispatching taxi / minibuses - about Kabina project
Dispatching taxi / minibuses - about Kabina projectBogusz Jelinski
 
Dividend portfolio – multi-period performance of portfolio selection based so...
Dividend portfolio – multi-period performance of portfolio selection based so...Dividend portfolio – multi-period performance of portfolio selection based so...
Dividend portfolio – multi-period performance of portfolio selection based so...Bogusz Jelinski
 
Mobile development in 2020
Mobile development in 2020 Mobile development in 2020
Mobile development in 2020 Bogusz Jelinski
 
Home security with Raspberry Pi
Home security with Raspberry PiHome security with Raspberry Pi
Home security with Raspberry PiBogusz Jelinski
 
Maturity of-code-mgmt-2016-04-06
Maturity of-code-mgmt-2016-04-06Maturity of-code-mgmt-2016-04-06
Maturity of-code-mgmt-2016-04-06Bogusz Jelinski
 

More from Bogusz Jelinski (8)

Dispatching taxi cabs with passenger pool
Dispatching taxi cabs with passenger poolDispatching taxi cabs with passenger pool
Dispatching taxi cabs with passenger pool
 
robotaxi2023.pdf
robotaxi2023.pdfrobotaxi2023.pdf
robotaxi2023.pdf
 
Dispatching taxi / minibuses - about Kabina project
Dispatching taxi / minibuses - about Kabina projectDispatching taxi / minibuses - about Kabina project
Dispatching taxi / minibuses - about Kabina project
 
Dividend portfolio – multi-period performance of portfolio selection based so...
Dividend portfolio – multi-period performance of portfolio selection based so...Dividend portfolio – multi-period performance of portfolio selection based so...
Dividend portfolio – multi-period performance of portfolio selection based so...
 
Mobile development in 2020
Mobile development in 2020 Mobile development in 2020
Mobile development in 2020
 
Jakt i-polen
Jakt i-polenJakt i-polen
Jakt i-polen
 
Home security with Raspberry Pi
Home security with Raspberry PiHome security with Raspberry Pi
Home security with Raspberry Pi
 
Maturity of-code-mgmt-2016-04-06
Maturity of-code-mgmt-2016-04-06Maturity of-code-mgmt-2016-04-06
Maturity of-code-mgmt-2016-04-06
 

Recently uploaded

Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxEran Akiva Sinbar
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555kikilily0909
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2John Carlo Rollon
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Masticationvidulajaib
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.PraveenaKalaiselvan1
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsssuserddc89b
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptArshadWarsi13
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
 

Recently uploaded (20)

Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Mastication
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physics
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.ppt
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
 

A few solvers for portfolio selection

  • 1. A few solvers for the Markowitz portfolio selection problem September 24, 2016 Abstract The most commonly suggested tool to solve the Markowitz portfolio selec- tion problem is Excel, which might be unwieldy for bigger models. The goal was to verify if other modeling tools can be used for this particular purpose, both commercial and open-source ones were reviewed including AMPL, Julia, Python and the C programming language. Some slight differences in numeri- cal solutions provided by these tools are presented. As some developments in the modeling of capital markets are attributed to the computational complexity of the classical Markowitz model, the ease of use and performance were in focus. The Markowitz (1952) portfolio selection model (see also Markowitz (1959)) was in itself quite simple but the computational part of the problem was so troublesome that it lead to different simplifications (see Sharpe (1963), Sharpe (2000)). The development of microcomputers gave mass access to nonlinear solvers allowing the solution to the original quadratic problem. The most com- monly suggested tool to solve the problem is Microsoft Excel1 (see Jackson (2001), Laws (2003)), which might be unwieldy for bigger models and limits the number of model variables. Let us remind the classic portfolio problem - let xi be the portfolio weight invested in security i, µi be the mean, σij be the covariance of securities i and j, then E is the expected return of a portfolio and 1 Microsoft Excel is a registered trademark of Microsoft Corporation 1
  • 2. V is its risk measure: E = N i=1 xiµi (1) V = N i=1 N j=1 xixjσij (2) N i=1 xi = 1 (3) xi 0 for i = 1..N (4) As we can see, to solve the problem we have to calculate n(n − 1)/2 covariance pairs and n variances. Two variants are usually considered - maximize expected return with variance not greater than an assumed value vmax: E → MAX (5) V vmax (6) or minimize variance with expected return not lesser than an assumed value emin: V → MIN (7) E emin (8) Below you will find implementations of the second variant (minimize the risk) in four popular languages. This issue seems to be trivial today in terms of complexity. Numerical examples are based on stock prices of Bank of America Corpora- tion, Ford Motor Company, General Electric and AT&T. Returns include both price gains and dividends and were calculated for twelve consecutive quarters starting on Jan 1st 2010. AMPL AMPL is a commercial tool (see Fourer (1990)), but it is available for students and teachers at no charge. The tool is well-established among scientists and has an extensive documentation2 - Fourer (2002). Input for the solver is split into the model and the data file. Let us name the model file mps.mod: 2 http://ampl.com/resources/the-ampl-book 2
  • 3. set A; # assets set T := {1..12}; param Rets {T,A}; # returns on assets param mean {j in A} = (sum {i in T} Rets[i,j]) / card(T); param Diff {i in T, j in A} = Rets[i,j] - mean[j]; param Covar {i in A, j in A} = sum {k in T} (Diff[k,i]*Diff[k,j]) / (card{T}-1); var X {A} >=0; var Mean = sum {j in A} mean[j] * X[j]; minimize Risk: sum {i in A} (X[i]*(sum {j in A} Covar[i,j]*X[j])); subject to TotalOne: sum {j in A} X[j] = 1; subject to Reve: Mean >= 0.035; and the data file mps.dat: set A := BAC F GE T ; param Rets: BAC F GE T := 1 0.1383 0.2228 0.1845 -0.0812 2 -0.2029 -0.2019 -0.2079 -0.0574 3 -0.0649 0.158 0.1593 0.1923 4 0.0038 0.3695 0.1265 0.0344 5 -0.0599 -0.1357 0.1045 0.0462 6 -0.1795 -0.0904 -0.0654 0.0398 7 -0.4472 -0.3103 -0.1995 -0.0862 8 0.0072 0.1483 0.2308 0.0891 9 0.6517 0.1258 0.1024 0.0425 10 -0.1539 -0.2361 0.0522 0.1479 11 0.0981 0.0554 0.1166 0.0536 12 0.2969 0.3092 -0.0715 -0.0954 You can now use AMPL graphical user interface - amplide - to run the following commands: 3
  • 4. model mps.mod; data mps.dat; solve; display X; You will get the following result: MINOS 5.51: optimal solution found. 5 iterations, objective 0.01062155067 Nonlin evals: obj = 11, grad = 10. ampl: display X; X [*] := BAC 0 F 0.0428628 GE 0.437821 T 0.519316 Julia Julia is a high-level, high-performance dynamic programming language for technical computing3. Julia and its packages4 are licensed under MIT, GPL, MPL, BSD and EPL licenses. Let us put the Julia code into mps.jl. I omitted covariance calculation for the sake of clarity: using JuMP using Ipopt N=4 r_min=0.035 m = Model() C = [0.076900 0.039968 0.018111 -0.000288 ; 0.039968 0.050244 3 http://julialang.org/ 4 https://jump.readthedocs.io/en/latest 4
  • 5. 0.019033 -0.000060 ; 0.018111 0.019033 0.021381 0.007511 ; -0.000288 -0.000060 0.007511 0.008542] Mean = [0.0073 0.0346 0.0444 0.0271] @variable(m, 0 <= x[i=1:N] <= 1) @objective(m,Min,sum{x[j]*sum{x[i]*C[i,j],i=1:N},j=1:N}) @constraint(m, sum{x[i]*Mean[i],i=1:N} >=r_min) @constraint(m, sum{x[i],i=1:N} ==1.0) status = solve(m) for i= 1:N println(getvalue(x[i])) end Then after running julia mps.jl we will get: EXIT: Optimal Solution Found. 1.3095345121754e-7 0.043278618483219226 0.4378847609266113 0.5188364896367182 You will find more details on Julia in Balbaert (2015). Python Let us put the Python code into mps.py5: from cvxopt import matrix from cvxopt.solvers import qp import numpy as np n = 4 Cov = matrix([[0.076900, 0.039968, 0.018111, -0.000288 ], [ 0.039968, 0.050244, 0.019033, -0.000060 ], [ 0.018111, 0.019033, 0.021381, 0.007511 ], 5 https://wellecks.wordpress.com/2014/03/23/portfolio-optimization-with-python 5
  • 6. [-0.000288, -0.000060, 0.007511, 0.008542 ]]) Mean = matrix([0.0073, 0.0346, 0.0444, 0.0271]) r_min = 0.035 G=matrix(np.concatenate((-np.transpose(Mean),-np.identity(n)),0)) h=matrix(np.concatenate((-np.ones((1,1))*r_min,np.zeros((n,1))),0)) A = matrix(1.0, (1,n)) b = matrix(1.0) q = matrix(np.zeros((n, 1))) sol = qp(Cov, q, G, h, A, b) print(sol[’x’]) Then after having executed python mps.py we will get: Optimal solution found. [ 8.47e-07] [ 4.33e-02] [ 4.38e-01] [ 5.19e-01] Cvxopt6 and NumPy7 are licensed respectively under GPL and BSD (see also Boyd (2004) and Idris (2015)). The Python language can be learnt with Lutz (2003). C The Ipopt library8 (see Surhone (2011) and Russell (2012)) is distributed with an example of usage — the hs071 ¯ c.c file — with four variables, two con- straints and goal minimisation, which perfectly fits our need. The covariance matrix is symmetric so the gradient of the objective is given 6 http://cvxopt.org 7 http://www.numpy.org 8 https://projects.coin-or.org/Ipopt 6
  • 7. by         2 ∗ N i=1 xici1 2 ∗ N i=1 xici2 2 ∗ N i=1 xici3 2 ∗ N i=1 xici4         (9) and the Jacobian of the constraints g(x) is   µ1 µ2 µ3 µ4 1 1 1 1   (10) With such a gradient of the objective the Hessian of the Lagrangian function is the covariance matrix multiplied by 2. Make the following changes to adapt this example file to our optimisation problem. First remove the whole if-block with the second call to IpoptSolve, most probably between lines 165 and 206. Then put covariance and mean matrices somewhere at the beginning making them global: Number c[4][4] = {{0.076900, 0.039968, 0.018111, -0.000288}, {0.039968, 0.050244, 0.019033, -0.000060}, {0.018111, 0.019033, 0.021381, 0.007511}, {-0.000288, -0.000060, 0.007511, 0.008542}}; Number MoR[4] = {0.0073, 0.0346, 0.0444, 0.0271}; Secondly move the assignment of the number of stocks/variables to the beginning of the main function and change the hardcoded values of nonzeros in the Jacobian and the Hessian: Index n=4; Index nele_jac = 2*n; Index nele_hess = n*(n+1)/2; Then change the lower and upper limits of variables: 7
  • 8. for (i=0; i<n; i++) { x_L[i] = 0.0; x_U[i] = 1.0; } and the limits of constraints - the minimum return and the sum of variables: g_L[0] = 0.035; g_U[0] = 1e19; g_L[1] = 1; g_U[1] = 1; The eval ¯ f goal function should contain: Number sum; int i,j; *obj_value=0.0; for (i=0; i<n; i++) { sum=0.0; for (j=0; j<n; j++) sum += x[j] * c[j][i]; *obj_value += x[i]*sum; } The eval ¯ grad ¯ f gradient calculation shall change to: int i,j; for (i=0; i<n; i++) { grad_f[i] =0; for (j=0; j<n; j++) grad_f[i] += x[j]*c[j][i]; grad_f[i] = grad_f[i] * 2; } The two contraints are calculated within eval ¯ g : 8
  • 9. int i; g[0]=0.0; for (i=0; i<n; i++) g[0] += x[i]*MoR[i]; g[0] += my_data->g_offset[0]; g[1]=0; for (i=0; i<n; i++) g[1] += x[i]; g[1] += my_data->g_offset[1]; Finanaly there are two functions left - the Jacobian, eval ¯ jac ¯ g : int i=0; if (values == NULL) { for (i=0; i<n; i++) { iRow[i] = 0; jCol[i] = i; } for (i=0; i<n; i++) { iRow[n+i] = 1; jCol[n+i] = i; } } else { for (i=0; i<n; i++) values[i] = MoR[i]; for ( ; i<n+n; i++) values[i] = 1; } and the Hessian, eval ¯ h : if (values == NULL) { idx=0; for (row = 0; row < n; row++) { 9
  • 10. for (col = 0; col <= row; col++) { iRow[idx] = row; jCol[idx] = col; idx++; } } } else { idx=0; for (row = 0; row < n; row++) { for (col = 0; col <= row; col++) { values[idx] = obj_factor * (2*c[row][col]); idx++; } } } You have to remove -pedantic-errors from the Makefile to compile this if you have not placed variable definitions at the beginning of each function. Running the program will give the following result: x[0] = 0.000000e+00 x[1] = 4.327805e-02 x[2] = 4.378846e-01 x[3] = 5.188373e-01 The C programming language was described by Kernighan (1988). An example with twenty stocks Let us see the results for twenty stocks9 selected out of S&P100 for twelve consecutive quarters starting on Jan 1st 2010. The same input data gave slightly 9 Stocks included: AIG BAC IBM CSCO DELL F GE GOOG HAL JPM KO MCD MS ORCL PFE S T VZ WFC XOM 10
  • 11. different results for four tools (Julia also uses Ipopt): Software AMPL Python (Ipopt) C (Ipopt) Excel Objective (risk) 0.002900 0.002874 0.002898 0.003178 AIG 0.0006 0.0003 0.0209 BAC 0.0067 0.0067 0.0067 IBM 0.1892 0.1890 0.1890 0.2743 KO 0.0718 0.0721 0.0719 0.0989 MCD 0.4291 0.4290 0.4290 0.4852 S 0.0966 0.0965 0.0965 0.1183 VZ 0.2066 0.2060 0.2066 0.0019 Table 1: Portfolio structures computed by different tools As we can see, Ipopt found the best solution - the portfolio with the lowest risk. Minor differences for Python and C can be caused by lack of precision in input data. Excel excludes Verizon (XOM with 0.0005 is not shown in the table). All tools provide results in the blink of an eye — a model with 200 stocks gets solved by AMPL within less than one second. Conclusions The Markowitz portfolio selection problem is no longer a computational trouble. There are open-source tools to solve models with hundreds of variables within milliseconds on a personal computer. You can use the language which suites you best. Especially Python deserves recommendation as it gets more and more popular in the scientific community. These tools can be fed with data in a much more convenient way than editing formulas in an office spreadsheet, which moreover has the limit of number of variables. It is found that different tools can suggest different portfolio weights and greater portfolio variance with the same constraints (see table 1) - another argument to use open-source tools. 11
  • 12. References 1. Balbaert, Ivo Getting Started with Julia, Packt Publishing, 2015. 2. Boyd, Stephen, L. Vandenberghe Convex Optimization, Cambridge Univer- sity Press, 2004. 3. Fourer, Robert, D.M. Gay, B.W. Kernighan ”A Modeling Language for Mathematical Programming” Management Science, 36 (1990), pp. 519–554. 4. Fourer, Robert, D.M. Gay, B.W. Kernighan AMPL: A Modeling Language for Mathematical Programming, Duxbury Press, 2002. 5. Idris, Ivan NumPy Beginner’s Guide, Packt Publishing, 2015. 6. Jackson, Mary, M. Staunton Advanced Modelling in Finance using Excel and VBA, Chichester: John Wiley & Sons, 2001. 7. Kernighan, Brian W., D.M. Ritchie The C Programming Language, Prentice Hall, 1988. 8. Laws, Jason ”Portfolio Analysis Using Excel” In C. L. Dunis, J. Laws P. Nam, ed., Applied Quantitative Methods for Trading and Investment, John Wiley & Sons, 2003. 9. Lutz, Mark Learning Python, 2nd ed. Sebastopol: O’Reilly, 2003. 10. Markowitz, Harry M. ”Portfolio Selection” The Journal of Finance, Vol. 7, No. 1 (1952), pp. 77–91. 11. Markowitz, Harry M. Portfolio Selection: Efficient Diversification of Invest- ments, NJ: John Wiley & Sons, 1959. 12. Russell, Jesse, R. Cohn IPOPT, Bookvika Publishing, 2012. 13. Sharpe, William F. ”A Simplified Model for Portfolio Analysis”, Manage- ment Science, Vol. 19, No. 2 (1963), pp 277–293. 14. Sharpe William F. Portfolio Theory and Capital Markets, McGraw-Hill, 2000. 15. Surhone, Lambert M., M.T. Tennoe, S.F. Henssonow Ipopt, Betascript Publishing, 2011. 12