Appendix B
Software
In this appendix we briefly discuss some of the MATLAB scripts mentioned throughout the book. They are intended to invite the reader to explore the algorithms by varying parameters and observing how the output changes. The operation of all scripts is
briefly discussed. The source code is available from this book’s web page at https://
www.springer.com/9783030636425.
B.1 Markowitz Simulation
The following script markowitz.m encodes the portfolio optimization from
Chap. 3. After clearing the workspace, we generate test data for the returns r 1 , r 2 ,
and r 3 over a period of 1000 trading days before initializing the vector e that appears
in (3.11), the average returns r , and the covariance matrix C from (3.8). In the next
step the matrix, here called A that appears in (3.16) is prepared and inverted. In the
following loop over the desired return ρ we first calculate the Lagrange multiplier λ i
from (3.17) and then the portfolio return vector w from (3.18) before storing ρ and
the resulting volatility σ . After the loop, we plot ρ versus σ —the efficient frontier—
in Fig. 3.2 and annotate the axes. Finally we plot the return and volatility of the test
data, determine the minimum parameters from (3.20) and (3.21) and add the points
to the plot.
% markowitz.m
clear all; close(’all’)
N=1000; % number of trading days
r1=0.01*ones(N,1)+0.01*randn(N,1);
%..make test data
r2=0.02*ones(N,1)+0.02*randn(N,1)-0.3*r1;
r3=0.03*ones(N,1)+0.03*randn(N,1)+0.3*r1-0.5*r2;
ee=ones(3,1);
% three stocks
ra=[mean(r1); mean(r2) ; mean(r3)];
% average return
© The Editor(s) (if applicable) and The Author(s), under exclusive
license to Springer Nature Switzerland AG 2021
V. Ziemann, Physics and Finance, Undergraduate Lecture Notes in Physics,
https://doi.org/10.1007/978-3-030-63643-2
265
Software
In this appendix we briefly discuss some of the MATLAB scripts mentioned throughout the book. They are intended to invite the reader to explore the algorithms by varying parameters and observing how the output changes. The operation of all scripts is
briefly discussed. The source code is available from this book’s web page at https://
www.springer.com/9783030636425.
B.1 Markowitz Simulation
The following script markowitz.m encodes the portfolio optimization from
Chap. 3. After clearing the workspace, we generate test data for the returns r 1 , r 2 ,
and r 3 over a period of 1000 trading days before initializing the vector e that appears
in (3.11), the average returns r , and the covariance matrix C from (3.8). In the next
step the matrix, here called A that appears in (3.16) is prepared and inverted. In the
following loop over the desired return ρ we first calculate the Lagrange multiplier λ i
from (3.17) and then the portfolio return vector w from (3.18) before storing ρ and
the resulting volatility σ . After the loop, we plot ρ versus σ —the efficient frontier—
in Fig. 3.2 and annotate the axes. Finally we plot the return and volatility of the test
data, determine the minimum parameters from (3.20) and (3.21) and add the points
to the plot.
% markowitz.m
clear all; close(’all’)
N=1000; % number of trading days
r1=0.01*ones(N,1)+0.01*randn(N,1);
%..make test data
r2=0.02*ones(N,1)+0.02*randn(N,1)-0.3*r1;
r3=0.03*ones(N,1)+0.03*randn(N,1)+0.3*r1-0.5*r2;
ee=ones(3,1);
% three stocks
ra=[mean(r1); mean(r2) ; mean(r3)];
% average return
© The Editor(s) (if applicable) and The Author(s), under exclusive
license to Springer Nature Switzerland AG 2021
V. Ziemann, Physics and Finance, Undergraduate Lecture Notes in Physics,
https://doi.org/10.1007/978-3-030-63643-2
265
