|
OR/MS Today - June 2006 Software Review MATLAB Software answers needs of users involved in numerical and symbolic analysis. By Manuel Tarrazo MATLAB stands for MATrix-LABoratory. It started as a utility to facilitate access to high-performance linear, algebraic, numerical analysis software, which is essential in many areas of science and practically all areas of business analysis. In the late 1970s, Cleve Moler used Fortran and portions of LINPAK (linear systems package) and EISPACK (eigenvalues package) to develop the first version of MATLAB, primarily because he "wanted students to be able to use the new packages without using Fortran programs" [1]. He and two others founded The MathWorks in 1984 to commercialize MATLAB. The MathWorks now offers a very complex array of products to meet professional and scientific needs in both academia and industry, which are organized around two main products, MATLAB and Simulink. Each of these products can be purchased separately, and a number of "toolboxes" and "blocksets" complement or extend each of them. The MATLAB "family" includes toolboxes in specialized areas (distributed computing, finance, bioinformatics, fuzzy logic, control, signal processing and communications) and clusters of procedures (optimization, symbolic analysis, partial differential equations, genetic algorithms and direct search, statistics and data analysis, neural networks, splines, curve fitting, GARCH, wavelet, filter design, etc.). This family also includes a number of utilities (links for EXCEL, image acquisition, data acquisition, instrument control, datafeed, database, compiler, report generator, etc.). Modeling needs are addressed by Simulink and related products. (See the "Products" section on The MathWorks site: www.mathworks.com.) There are also more than 300 third-party products available in the United States and many other countries, as shown on The MathWorks Web site: www.mathworks.com/products/connections/product_name.html. In March 2004 I purchased the following products from The MathWorks: MATLAB (Release 13), EXCEL Link, Extended Symbolic Toolbox and Neural Networks Toolbox. One year after my purchase, The MathWorks sent me a maintenance bill that enables me to receive updates for another year. I must confess that I have not used the EXCEL link or the Neural Toolbox, mainly for lack of time. I also have yet to install the Release 14 of the MATLAB because it will not work in the Windows ME desktop I still use at home. Clicking on the MATLAB's desktop icon opens a window with three clearly defined spaces and a menu. There is a "Command Window," which is an interpreter that parses single line instructions or groups of instructions collected in "script" files (known as "batch" files in other programming environments). There is also a "Work Space" and a "Command History." The Work Space shows the names of the variables and memory characteristics; one can access a matrix editor by clicking on its name. As should be expected, matrix manipulation in MATLAB is very easy. I can select a column in A, say the third, by simply typing A(:,3). The Command History window also allows highlight-and-execute for previous commands, command blocks and easy copy-by-dragging. The up-arrow-key rewrites previous commands. The Help Command is also very well designed and includes access to the text in the manuals, as well as numerous examples and demos. One can work directly with the interpreter (Command Window) or type the name of a script file. To create objects (variables, matrices, functions, expressions, etc.) with MATLAB is easier than with any programming language I have ever used. In programming, one usually has some data that must be processed in a certain way (functions) to obtain a given output which, in turn, is represented in a numerical or a graphical way. Single line functions can be defined in the Command Window with expressions, anonymous and inline functions. Multiline functions reside in external files named after the function. When the function is called, it does its job and its output appears in the Work Space ready for further use. Global variables can be used to change the way the function works. Program 1: common procedures. This program illustrates different ways to get something fundamental done: 1) create a variable, 2) assign some values to it, 3) make this variable determine the values of a second variable, and 4) graph them both. Version A is straightforward; Version B, C, D use the symbolic function plotter ezplot(). The output from this program is omitted to optimize space.
Program 2: symbolic equation solver. This solver can be applied to both linear and nonlinear equations.
Program 3: symbolic toolbox and simultaneous differential equations (first order, non-homogenous, Cauchy's problem). Version A shows the "automatic" way of doing it; that is, with the analytic differential equations solver: dsolve.
Program 4: textbook version of the tangent portfolio. Given a variance-covariance matrix, and a vector of returns, the tangent portfolio is the grouping of securities that maximizes the returns-to-standard-deviation ratio. The program could be easily modified to calculate means and variance-covariance from a comma-delimited data file. This program shows the convenience and reusability of MATLAB code: once the script is completed it will work every time for any problem dimension. There is no need of further variable declarations or re-dimensioning. The output below is self-explanatory.
Program 5: symbolic solver and simultaneous equations system. The same portfolio optimization problem using a set of simultaneous equations is now processed analytically. The solutions below are the optimal portfolio weights in rational numbers. These and the other similar programs below showcase, first, the ability MATLAB has to process information as we often do on blackboards, research papers and textbooks, that is, symbolically. Second, the decimal representation of numbers is replaced by their rational expression, which paves the way to studying issues concerning numerical accuracy.
Program 6: Portfolio optimization using fminsearch(). This program uses a general purpose minimization function to compute optimal weights (output not printed). Using a slightly different syntax, fminsearch ('lopt_fun1', xinit), would work as well. To compute optimal weights using fminsearch() is a two-step process. First, we must write an external function and save it in the active directory in MATLAB. Second, we must write the code to invoke fminsearch() itself. The two pieces seem rather straightforward once the two pieces are completed and in working order, but learning how to do it could definitely be more user-friendly. Note that the variance-covariance matrix and the vector of returns are declared "global" variables, which saves us from rewriting the function for different problems, or from passing values each time the function is called. I was disappointed that MATLAB does not include more optimization functions. The procedures included in the Optimization Toolbox are probably state-of-the-art and should be employed in professional and research endeavors. Still, there is ground between those professional-grade functions and fminsearch() that general users of MATLAB may like to see covered by general purpose functions.
Program 7: surface chart. Here we present the code for plotting a straightforward 3D (surface) chart in MATLAB (Figure 2). The MATLAB meshgrid() function is similar to the data-table needed in Excel to create surface charts. This program shows the ease of creating graphics with MATLAB. This program could be used to study the portfolio variance, which is a quadratic form.
First, MATLAB shows a clear preference for working with text files. All scripts and functions are written as text. This strategy makes file sharing easy and links MATLAB code to other text-based programming environments. An alternative strategy, followed by at least one MATLAB competitor, is to use graphic-based editors requiring a proprietary reader and converters. Using Microsoft Word is an option in both MATLAB and its competitor, but it needs to be "handled with care." For example, I often write notes, save pieces of code, output and graphics in a Word file, but copying MATLAB text code into a Word file usually triggers autocorrect actions that disable the code. This especially affects quotation signs. Second, default MATLAB graphics, as the surface chart shows, tend to be uncomplicated, elegant and direct. Moreover, graphical output appears in a graphics editor that gives users complete control over the output. Third, learning MATLAB is a complex endeavor. In my experience, the major difficulties stem from the following factors:
Concerning syntax, it is good to keep in mind that the reason we purchase specialized tools in the first place is because the problems we are studying are complicated (e.g., variable coefficients, non-homogeneous differential equations). Taking care of using the correct syntax is also a way to save us a great deal of aggravation and time wasted. Fourth, the MATLAB documentation and examples are quite satisfactory. The help facility is well designed and easy to navigate. There are also efficient utilities to manage the work to be done: the diary; editors for files, graphics, vectors and matrices; a code checker and profiler; and the possibility of using M-books, which enable MATLAB to be used from within Microsoft Word. Overall, I am very satisfied with my purchase of MATLAB. It does not relieve me from knowing my field, and MATLAB will not write papers for me. But it is a good workhorse to try out things in the best conditions, and there seems to be no better tool for venturing in complex areas of learning, where traditional textbooks and problem-solvers are no longer enough.
Manuel Tarrazo is an associate professor of finance at the School of Business & Management, University of San Francisco. References
OR/MS Today copyright © 2006 by the Institute for Operations Research and the Management Sciences. All rights reserved. Lionheart Publishing, Inc. 506 Roswell Rd., Suite 220, Marietta, GA 30060 USA Phone: 770-431-0867 | Fax: 770-432-6969 E-mail: lpi@lionhrtpub.com URL: http://www.lionhrtpub.com Web Site © Copyright 2006 by Lionheart Publishing, Inc. All rights reserved. |