Overview/Tutorial of the MATLAB R Language

Một phần của tài liệu Handbook of computational finance (Trang 781 - 785)

We provide a brief overview and tutorial of MATLAB to help the reader better understand how it can be used to analyze financial data. This introduction only scratches the surface of what MATLAB can do, and we refer the reader to the other sources mentioned above.

MATLAB will execute under Windows, Linux, and Macintosh operating sys- tems. This introduction will focus on the Windows version, but most of the information applies to all systems. The main MATLAB software package contains many functions for analyzing data of all types.

28.2.1 Getting Around in MATLAB

When MATLAB is started, a desktop environment is opened. This includes several windows, such as the Command Window, the Workspace Browser, the Command History and more. In addition, there is an Editor/Debugger that can be opened using the File menu. This editor can be used for creating MATLAB M-file scripts and functions. The MATLAB environment has many ways to execute commands, including the typical main menu items along the top of the window, toolbar buttons, context menus (right-click in an area), and specialized GUIs.

The Command Window is the main entry point for interacting with MATLAB.

The prompt is indicated by a double-arrow, where the user can type commands, execute functions, and see output. The Command History window allows the user to see the commands that were executed for previous sessions, allowing the user to also re-execute commands using various Windows shortcuts (copy/paste, drag and drop, for example). The Workspace Browser shows information about the variables and objects that are in the current workspace, and it includes the ability to edit the variables in a spreadsheet-like interface. Finally, there is a Help window that provides access to documentation, help files, examples, and demos.

One can also accesshelpfiles from the command line. Thehelpfiles provide information about the function and also gives references for other related functions.

From the command line, just typehelpfuncname to get help on a specific function.

The commandhelp generalprovides a list of general purpose commands, and the wordhelpused alone returns a list of topics. The commandlookforkeyword will do a search of the first comment line of the M-files (on the MATLAB path) and return functions that contain that word.

The user can enter commands interactively at the command line or save them in an M-file. Thus, it is important to know some commands for file management. The commands shown in Table28.1can be used for this purpose.

Variables can be created at the command line or in M-file scripts and functions (covered later). A variable name cannot start with a number, and they are case sensitive. So,Temp,temp, andTEMPare all different objects. The variables that

Table 28.1 Basic commands for file management

Command Usage

dir, ls Shows the files in the current directory deletefilename Deletes filename

cd, pwd Shows the current directory cd dir, chdir Changes the current directory whichfilename Displays the path to filename

what Lists the .mand .matfiles in the current directory Table 28.2 Basic commands for working with variables

Command Usage

who Lists all variables in the workspace

whos Lists all variables and information about the variables clear Removes all variables from the workspace

clear x y Removes variablesxandyfrom the workspace Table 28.3 Basic commands for working with external data files

Command Usage

load filename Loads all variables in filename.mat load filename var1 Loads only var1in filename.mat

Loads ascii filename.txt

load filename.txt -ascii stores in the workspace with the same name

are created in a MATLAB session live in the workspace. We already mentioned the Workspace Browser; there are additional commands for workspace management.

The commonly used ones are summarized in Table28.2.

It is also necessary to get data into and out of MATLAB for analysis. One of the simplest ways to get data into MATLAB is to use theloadcommand at the prompt; thesavecommand works similarly to export your data in the MATLAB .matformat or ascii. Table28.3shows some of the common ways for loading data.

You can also use the commands in the File menu to load variables and to save the workspace. There is also the usual window for browsing directories and selecting files for importing.

MATLAB uses certain punctuation and characters in special ways. The percent sign denotes a comment line. Characters following the % on any command line is ignored. Commas have many uses in MATLAB; the most important is in array building to concatenate elements along a row. A semi-colon tells MATLAB not to display the results of the preceding command. Leaving the semi-colon off can cause a lot of data to be dumped to the command window, which can be helpful when debugging MATLAB programs but in other cases clutters up the window. Three periods denote the continuation of a statement. Comment statements and variable names, however, cannot be continued with this punctuation. The colon is used to specify a sequence of numbers; for example,

1:10

produces a sequence of numbers 1 through 10. A colon is also used in array indexing to access all elements in that dimension; for example,

A(i,:)

refers to the ith row of the arrayA.

28.2.2 Data Types and Arithmetic

MATLAB has two main data types: floating point numbers (type double) and strings (typechar). The elements in the arrays or variables will be of these two data types.

28.2.2.1 Basic Data Constructs

The fundamental data element in MATLAB is an array. Arrays can be one of the following:

• The00empty array that is created using empty brackets: [ ].

• A11scalar array.

• A row vector, which is a1narray.

• A column vector, which is ann1array.

• A matrix with two dimensions, saymnornn:

• A multi-dimensional array, saym:::n.

Arrays must always be dimensionally conformal and all elements must be of the same data type. In other words, a23matrix must have three elements on each of its two rows.

In most cases, the data analyst will need to import data into MATLAB using one of the many functions and tools that are available for this purpose. We will cover these in a later section. Sometimes, we might want to type in simple arrays at the command line prompt for the purposes of testing code or entering parameters, etc.

Here, we cover some of the ways to build small arrays. Note that these ideas can also be used to combine separate arrays into one large array.

Commas or spaces concatenate elements (an element can be an array) as columns. Thus, we get a row vector from the following:

tempDŒ1; 4; 5I

Recall that the semi-colon at the end of the expression tells MATLAB to not print the value of the variabletempin the command window. We can concatenate two column vectorsaand binto one matrix, as follows:

tempDŒa bI

Table 28.4 Special arrays

Function Usage

zeros, ones Build arrays containing all 0s or all 1s respectively rand, randn Build arrays containing uniform or normal random values eye Create an identity matrix

Using the semi-colon to separate elements of the array tells MATLAB to con- catenate elementsaandbas rows. So, we would get a column vector from this command:

tempDŒ1I4I5I

When we use arrays as building blocks for larger arrays, then the sizes of each array element must be conformal for the type of operation.

There are some useful functions in MATLAB for building special arrays. These are summarized in Table28.4; look at the help file to learn how each function is used.

Cell arrays and structures are a special MATLAB data type that allow for more flexibility. Cell arrays are array-type structures, where the contents of each cell element can vary in size and type (numeric, character, or cell). The cell array has an overall structure that is similar to the basic data arrays we have already discussed.

For example, the cells are arranged in rows and columns. If we have a23 cell array, then each of its two rows has to have three cells.

Structures are similar to cell arrays in that they allow one to combine collections of dissimilar data into a single variable. Individual structure elements are addressed by fields. We use the dot notation to access the fields. Each element of a structure is called a record.

As an example, suppose we had a structure calleddatathat had the following fields:name,dob, andtest. Then we could obtain the information in the tenth record using

data(10).name data(10).dob data(10).text

28.2.2.2 Array Addressing

In Table28.5, we show some of the common ways to access elements of arrays.

Suppose we have a cell array calledA. The last line of Table28.5shows how to access the contents of the ijth cell inA. Curly braces are used to get to the contents, and parentheses point to the cells. The two notations can be combined to access part of the contents of a cell. For example,Af1,1g(1:2)extracts the first two elements of the vector that is contained in cellA(1,1).

Table 28.5 Addressing: Arrays or cell arrays Notation Usage

a(i) Denotes the ith element Addresses the ith column.

A(:,i) Here, the colon operator tells MATLAB to access all rows Addresses the ith row.

A(i,:) Here, the colon tells MATLAB to gather all of the columns A(1,3,4) Addresses the element indexed at three levels

Afi,jg Addresses the contents of the ijth cell Table 28.6 Element-wise arithmetic

Operator Usage

.* Multiply two arrays element-by-element ./ Divide two arrays element-by-element .ˆ Raise each element of an array to some power

28.2.2.3 Arithmetic Operations

MATLAB has the usual mathematical operators found in programming languages, such as addition (+), subtraction (-), multiplication(*), division(/), and exponen- tiation (ˆ). These follow the same order of operations that is found in algebra and can be changed using parentheses. MATLAB also follows the conventions found in linear algebra. In other words, the arrays must have the same dimensions when adding or subtracting vectors or matrices, and the operation is carried out element-by-element. If we are multiplying two matrices,A and B, they must be dimensionally correct; e.g., the number of columns ofAmust be equal to the number of rows ofB.

In some cases, we might want to multiply two arrays element-by-element. In this case, we would put a period in front of the multiplication operator. We can do the same thing to divide two arrays element-by-element, as well exponentiation. We list these in Table28.6.

Một phần của tài liệu Handbook of computational finance (Trang 781 - 785)

Tải bản đầy đủ (PDF)

(817 trang)