Sunday, October 12, 2008

Overview


Why MATLAB?

As for the choice of MATLAB ? Well, an easy and honest answer would be that this is what I know best. On a more serious note, this (other than, for example, R) gives you a good feel of programming for scientific problems if you are not going to use C++ or Java. Most financial firms have it, and it interfaces very well with excel. It easily has best graphics compared to what else is available out there (except perhaps Mathematica). The only drawback, though a big one, is that it is damn expensive. Other small crib one could say is that it doesn't have its own econometrics toolbox...yet (I am told it's going to be part of future releases). But if anyone of you with a quantitative bent is is planning to have his / her own startup, then any software will be pretty darn useless. You’ll need to use a more 'low' or middle-level language like C++ / Java. These are languages that are best suited for scalable operations. As for that, an advantage of using MATLAB is that it can ‘talk’ to both C++ and Java, and, also, is not too far removed from the way one generally codes problems in C++. And when it comes to prototyping and building pilots MATLAB is perhaps the best there is.

Getting Started: Installation

Now let’s get to business. I hope you all have your MATLAB CD or the necessary installation files on your laptop. You should install at least the following toolboxes and the documentation CD:
  • MATLAB: Necessary
  • Extended Symbolic Math: Optional
  • Financial
  • Financial Derivatives
  • Financial Time Series
  • GARCH
  • Optimization
  • Partial Differential Equation
  • Spline: Optional
  • Symbolic Math
  • System Identification
Btw, if you've enough space and RAM on your machine, please feel free to install all the toolboxes you feel like playing around with.

1. To start MATLAB: Use the Windows key on your keyboad (let's call it 'W') and the letter 'r', i.e. W-r. This opens the Run command window. Type 'matlab' and press enter. You are in.

[I'm a little biased towards using keyboard for most inputs. I suggest you guys too use the keyboard as often as possible. It's not only faster, but also very efficient.]

2. MATLAB 6 and above has something called MATLAB Desktop, which comprises 5 separate windows. To see the listing go to View in the Menu bar (keyboard shortcut: Alt-V). These are:
  • Command Window: where you input commands
  • Command History: obvious
  • Current Directory: The current folder in which you are working. The place where all variables you save will be stored
  • Workspace: Place where you can see all the variables you created using a session (if you type a = 5 in the Command Window; the variable 'a' appears in the Workspace. Basically anything you 'create' goes here
  • Launch Pad: Shows you what all you've installed as part of MATLAB. Has short-cuts to help for examples, demos etc. Click there and you'll know where all you can go to from there

3. Some basic stuff / commands introduced in the class:

  • MATLAB is case sensitive: upper and lower case characters are not equivalent; so A = 5 and a = 5 are two different assignments
  • A semi-colon at the end of a command suppresses the screen output
  • For accessing help: help, helpwin, doc and lookfor. Btw, any help command can be used in any of the following ways. For example, if you want to see the help on the in-built function sum, you can use help sum or helpwin sum or doc sum (try yourself to the see the difference between the three methods. You'll need the Documentation CD installed for this)
  • MATLAB uses double-precision floating point arithmetic, which is accurate to approximately 15 digits; however, MATLAB displays only 5 digits by default. To display more digits, type format long. For options availabe on format, type help format at command prompt
  • clear: Just using the command clear clears all the variables from the Workspace; to delete a specific variable (say, a), use the command clear a
  • clc: Clears the command window (like a duster on a whiteboard); variables are not cleared from the Workspace
  • who and whos: who lists the variables in the Workspace; whos lists them along with their attributes (size, type etc.)
  • To exit MATLAB use either exit or quit at command prompt and press enter (as a typical Windows message would say: all unsaved changes would be lost when you exit MATLAB)
  • To save the variables in the workspace: Use the command save filename
  • To save whatever you type in a file and whatever appears on the Command Window screen (including error messages, help etc.): Type diary on at command prompt. When you want to stop recording what you type, use diary off. For more on this, type help diary at command prompt

Start-up

This is to tell you how to start MATLAB in the directory you want to start it in, and anything else you may want it to do when it starts

For the first time users: : Open the file 'startupsav.m' (use open startupsav at command prompt). Comment out (using %) or delete the line load matlab.mat. Type out now the commands you want MATLAB to execute when it starts. For example, say you want to start MATLAB in the directory 'c:\MATLAB6p5\work' and clear all the variables from the memory as well as present a clear command window. Use the following commands:

cd('c:\MATLAB6p5\work');
clear;
clc;

and save the file as 'startup.m' in the same directory where 'startupsav.m' is (you won't need to change the directory, it automatically takes you to the directory where 'startupsav.m' is stored).

[Although technically you don't need a semicolon at the end of the commands cd, clear and clc, it's nonetheless a good practice to always use a semi-colon at the end of a line/command; btw, cd stands for 'change directory'; another similar command is mkdir. For more on these, use help cd]

After you have created 'startup.m', for any future changes you need to make in your startup options you'll now need to use 'startup.m' and not 'startupsav.m'.

I think this pretty much covers what was introduced in the class today (other than I guess some examples from the Symbolic Math Toolbox and some other random plotting examples - but those I'll be doing from scratch in one of the next two sessions). Will revisit briefly the material covered today in the next class too. If you've any questions / suggestions, please feel free to leave a comment or drop me a line.

Finally: A Suggestion

Try not to miss the next two sessions. We'll be covering all the MATLAB we'll be needing for financial applications then. So, if you miss those, it may be a little hard to play catch-up as we get to the core of computational finance. There is only as much time we have!

MATLAB for Finance - About the Blog and Course Outline


This is going to be the companion webpage for our MATLAB course. I'll try to post the core of the material covered in each class here within a day or two after we have covered it in the class. Assignments and suggested solutions will be posted here too (I'll also email the class-rep as and when I post an assignment here).

From time to time I'll also be posting some links for help on MATLAB here. So, those serious about learning MATLAB watch out for this space. I'll suggest you subscribe to the feed of this blog in your RSS reader, so you would know when I've added/modified anything.

The course outline is
here.

Test


This is a test post. Please ignore.