Thursday, October 16, 2008

Input and Assignments


In the next two sessions we'll be covering all the basics (or, well most of it anyways) we need to know for our purpose. I hope that by now you all have MATLAB installed on your machines. To check if you've all the toolboxes that you intended to install, use ver at command prompt (hereafter, >>).

For those who missed the first session may want to go through the overview.

We begin with two very useful help commands:

help general: it lists a whole lot of general purpose commands you may need as you learn to use MATLAB
help punct: it lists how various punctuation marks are read / understood by MATLAB

[as you would notice all the commands we went through in the previous class (cd, clc, clear etc.) are part of the list that you see when you access help general]

In general, like in most other situations, I would suggest that when in doubt seek help (or if that doesn't help, you know where to go).

Before getting to topics for the day, there is another feature that you may find useful. MATLAB contains an extensive selection of demos that you can run/play. For the list of demos available type demo at >> (you may need to have the Documentation CD installed for this). I would strongly suggest spending some time with in-built demos. Some of them are a great way to while away time (esp. the ones on Graphics). 

Basic Input: Variables and Assignments

In finance, as in most other fields of enquiry, things that vary (variables) are the ones which are most interesting. Prices, volatility, capital structure - these are some of the things that makes finance really 'tick'. In fact, uncertainty about the future value of these variables is what finance today is all about - well, the most interesting part of it anyways.

Where a software like MATLAB comes into the picture is to help study the behaviour of variables. It not only helps you compute and solve involved problems of mathematical nature, but also help you do all kinds of what-if analyses.

So, how do we assign values to variables? It's very simple. In MATLAB, you can play around with variables with no set-up cost. If you want to assign a value to a variable, you do how you'll do it on paper, i.e. use an 'equal to' sign. For instance:

>> x = 5;

After the assignment is made, the variable x is stored in the Workspace, until changed to some other value (by a similar assignment). Now you can choose x the way you want. You can evaluate or plot functions of x or what not. For example, try:

>> x^4 + sin(x) + exp(x);

(or anything else that comes to your mind).

The general form of a MATLAB input expression is:

Variable Name = Expression

This holds for scalars, vectors as well as matrices. Of course, the nature of the expression on the RHS decides the nature of the variable (whether it is a scalar, or a vector, or matrix, or a complex - as in a complex number (a + ib) - version of those)

Examples of vector and matrix assignments are:

>> x = [1 2 3 4] ;
% a row vector

>> x = [1, 2, 3, 4] ;
% this is also a row vector; spaces and commas are equivalent inside a 
% matrix/vector.
% Although you do not need an extra space after the comma, it's a good practice
% and makes the code or whatever you have written on the screen more readable

>> x = [1; 2; 3; 4] ;
% a column vector (a way to change from a row to column vector is use the transpose 
% operator: '
% example:  y = x' ;

>> x = [1 2 3; 4 5 6] ;
% a two-by-three (2 rows, 3 columns; hereafter sizes denoted as, for example in this % case, 2 X 3) matrix.
% Needless to say you can use the transpose operator ' to transpose a matrix too.

Say, you already have a variable y in the workspace memory. Then, needless to say, you can also assign a matrix using the value(s) stored in y. Say, y were a 2 X 2 matrix, then you can use that to create something else like:

>> x = [1 2; y] ;
% you want to guess what the size of this new matrix will be?

[More formally this is called concatenation - which is just another way of saying you are attaching one thing to the other in a way that makes sense (the linear algebra way)].

Please note that assigning the value of one variable to another, say x = y or x = someFunction(y) as in the concatenation example above only assigns the value of that variable. Thus, if you make any changes in y, they will not be reflected in x, unless after you make any changes in y, you also make changes in x, say by redefining x = someFunction(y) or perhaps x = someOtherFunction(y), as the case may be [ack: Kevin Sheppard].

I guess by now it is clear what's the use of a semicolon within a matrix and how it helps separates rows and columns. This brings us to:

Assignment 1, Q1: What happens when you raise a scalar to a matrix and vice-versa. For example, if x is a scalar and y is a matrix, what happens when you do x ^ y. What happens when you do x .^ y (or power(x, y)). Can you always do this, irrespective of the sizes of x and y ? What happens when you do exp(y) ? What happens when you raise one matrix to another, say y ^ y or y .^ y ? What happens when you do, for example, sin(y) ? Can you think of any other operations like this, where you may have to mix scalars and matrices with some in-built function ? Use help power, help exp, help ^, help log and help log10.

[NB: Recall that while the size of a matrix or a vector is obviously given by its dimension, MATLAB reads scalars also as matrices of size 1 X 1]

Variable Names

Variables can take any name as long as it contains only alphabets (lower or upper case), numbers and underscore (_). Operators and mathematical symbols are not allowed within variable names. They are case sensitive and must begin with an alphabet.

A suggestion: Use variable names that are intuitive and correspond as closely to the nature of the variables you are studying as possible, rather than something that may have caught your fancy at that point in time. When you are dealing with matrices (many variables at the same time), it' s best to select a variable name that is intuitive and shortest amongst the ones you can think of.

Higher Dimensional Arrays [ack: Kevin Sheppard]

A typical matrix is a two-dimensional (2-D) array, which you can think of as an analog of a 2-D coordinate (say, a typical X-Y axes kind of) system. Just like you have 3-D and higher (N) dimensions in math and coordinate geometry, you can also have multi- or N - dimensional arrays.

In MATLAB N-dimensional arrays are available for N up to about 30, depending on the size of each matrix dimension. Higher dimensional arrays can be useful for tracking matrix values through time, such as a time-varying covariance matrix (since a covariance matrix is a 2-D array, a time varying covariance matrix would require another dimension - coordinate if you will - to keep track of time; so for each time index you'll have a separate covariance matrix: so, a 3-D array). Unlike scalars, vectors and matrices, higher dimension arrays cannot be directly allocated and can only be constructed by calling functions/matrices such as ones(1, 2, 3).

More later.

No comments: