0% found this document useful (0 votes)
3 views22 pages

Class 1

The document outlines a MATLAB course focused on finance, covering basics such as matrices, matrix operations, and indexing. It includes details on the course structure, teaching materials, and administrative information. The course emphasizes interactive sessions and practical applications of MATLAB in finance, with assessments based on coursework and an in-class test.

Uploaded by

Promachos IV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Class 1

The document outlines a MATLAB course focused on finance, covering basics such as matrices, matrix operations, and indexing. It includes details on the course structure, teaching materials, and administrative information. The course emphasizes interactive sessions and practical applications of MATLAB in finance, with assessments based on coursework and an in-class test.

Uploaded by

Promachos IV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Outline of Course

MATLAB Basics
Matrices and Matrix Operations

Application of Matlab for Finance


Week 1

Shi, Yining

September 6, 2016

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course
MATLAB Basics
Matrices and Matrix Operations

Today’s Class

I Outline of the Course


I Matlab Basics
I Matrices and Matrix Operation
I Indexing and Colon Operator
I Examples

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course
MATLAB Basics
Matrices and Matrix Operations

What is Matlab?

I A scientific programming Language: MATrix LABoratory


I A very fancy calculator
I A tool for econometrics and finance
I A tool for numerical methods, probability, and statistics
I A tool to produce graphics and plots

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course
MATLAB Basics
Matrices and Matrix Operations

Outline of Course

I Lecture 1 - Variables, Matrices, Indexing, and Operations.


I Lecture 2 - Flow Control, Logical Operators, User defined functions, I/O
I Lecture 3 & 4 - Portfolio Optimization, Trading Strategy, Graphics with
MATLAB
I Lecture 5 - Simulation and Option Pricing
I Lecture 6 - Regression Analysis
I Lecture 7 - Revision and In-class test

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course
MATLAB Basics
Matrices and Matrix Operations

Teaching materials

I Course materials are available on the HUB and consist of


I Lecture slides
I Example question solutions in m-file format
I Each class consists of interactive sessions: a mix of the course leader
demonstrating and the class students implementing example code.
I Examination:
I Coursework/Assignment (50%)
I Individual Coursework / In class test in Lecture 7 (Week 8) (50%)

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course
MATLAB Basics
Matrices and Matrix Operations

Administrative Details

I Class Materials
I Create a directory H://MATLAB_Course in your H: drive
I Hub Website: https://www.imperialbusiness.school
I Download the MATLAB file from the HUB.
I Save the file to H://MATLAB_Course//Class 1 (Class 2 etc )
I We will populate this directory in the following weeks.

Shi, Yining Application of Matlab for Finance Week 1


MATLAB Layout
Outline of Course
Help File
MATLAB Basics
Command Prompt
Matrices and Matrix Operations
Variables

MATLAB Layout

I Command Window
I Type commands and display no-graphic output
I A >> prompt shows the system is ready for input
I Current Directory (CD)
I The directory (folder) that MATLAB is currently working in
I Always change the CD to where your MATLAB files are located
I cd('H:/MATLAB_Course/Class 1') for today
I Editor
I The window where you edit and save m-files
I m-files: the files that save scripts and functions that you’ve
defined/created
I Workspace:
I Store all the variables that you currently created and defined

Shi, Yining Application of Matlab for Finance Week 1


MATLAB Layout
Outline of Course
Help File
MATLAB Basics
Command Prompt
Matrices and Matrix Operations
Variables

Help File

I Search Field: right up corner: "Search Documentation"


I Type a keyword (e.g. skewness)
I Definition, function usage instructions etc.
I Command window: help followed by the name of the function
I help skewness: on-screen text with usage instructions
I In script help: highlight the function name and press F1

Shi, Yining Application of Matlab for Finance Week 1


MATLAB Layout
Outline of Course
Help File
MATLAB Basics
Command Prompt
Matrices and Matrix Operations
Variables

Command Prompt

I Open Hello_World.m
I % Name: Hello_World.m: % comments identifier
I %%: split m-file into subsections
I End a command with a semi-colon (;) to suppress the output
I Semi-colon (;) helps separate multiple commands that in one line
I Execute your commands
I Type (paste) commands in the command window → enter
I Define a 100 by 1 vector of ones
I Type ones(100,1) vs ones(100,1);
I Execution by selection: F9
I Execution by blocks: F5

Shi, Yining Application of Matlab for Finance Week 1


MATLAB Layout
Outline of Course
Help File
MATLAB Basics
Command Prompt
Matrices and Matrix Operations
Variables

Variables

I A variable stores the assigned value in memory so that your programs can read
it, operate on it and save it back to memory.
I Variable names:

I it must begin with a letter


I uppercase and lowercase are distinguished A 6= a
I ensure the name is not already used as a function name.
I All MATLAB variables are stored and used as matrices.

I Elements: numbers, characters, logical statements (1 or 0), etc.


I Scalar (1-by-1): A = 3  
1
I Vector (1-by-n) or (n-by-1): B = [1 2] C =
2
 
1 2
I Matrix (2-by-2) D =
3 4

Shi, Yining Application of Matlab for Finance Week 1


MATLAB Layout
Outline of Course
Help File
MATLAB Basics
Command Prompt
Matrices and Matrix Operations
Variables

Create Variables

   
1 1 2
I Create A = 3 B = [1 2] C = D= E = [0 0 0]
2 3 4
I A = 3
I []: Used to create matrix, not used to clarify order of operations.
I Row vector with two elements, separate the elements with either a comma (,) or
a space
B = [1,2] %or B = [1 2]
I A matrix has multiple rows, separate the rows with semicolons (;)
C = [1;2]
D = [1,2;3,4] %or D = [1 2; 3 4]
I Functions:
ones, zeros, rand, eyes
E = zeros(1,3)

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Matrix Indexing and the Colon Operator

I To reference a particular element in a matrix, specify its row and column number
I Always specify the row first and column second: A(row, column);
I A(2,3) refers to the 2nd row, 3rd column element.
I Colon (:) selects the complete rows or columns:; e.g. A(2, :) refers to the 2nd
row, whereas A(:, 3) refers to the 3rd column of matrix A.
I Colon (:) also selects a particular range of values in a matrix; e.g. A(2,1:3)
selects the first, second and third elements of the 2nd row.
I A(1 : 2, 3 : 4) selects the 2-by-2 sub-matrix of A that is comprised of the first 2
rows for the 3rd and 4th columns of A.
I a step size can also be used; e.g. A(1, 1 : 2 : 5) will select the first rows 1st, 3rd
and 5th elements

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Matrix Indexing and the Colon Operator

" #
1 2 3 4 5
I A=
6 7 8 9 10
I Find A(2, 3), A(2, 1 : 3), A(1 : 2, 3 : 4) and A(1, 1 : 2 : 5)

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Matrix Indexing and the Colon Operator

" #
1 2 3 4 5
A=
6 7 8 9 10

I A(2, 3) = 8
I A(2, 1 : 3) = 6, 7, 8
" #
3 4
I A(1 : 2, 3 : 4) =
8 9
I A(1, 1 : 2 : 5) = [1, 3, 5]

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Matrix Indexing and Colon Operator: Define


Variables

I Define X = [1 2] with matrix indexing and then redefine X = [1 15]


X(1,1)= 1
X(1,2)= 2
X(1,2)=15 %change the value to X(1,2)
" #
1 2 3 4 5
I Define Y =
0 0 0 0 0
Y(1,:)= 1:1:5
Y(2,:)= 0
% Way 2: use functions
Y1 = 1:1:5
Y2 = zeros(1,5)
Y = [Y1;Y2]
% Way 3
Y = [1:1:5;zeros(1,5)]

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Simple Matrix Operations


I Matrix arithmetic operations are defined by the rules of linear algebra
I Matrix multiplication A∗ B needs equality of Columns of A and Rows of B.

A+B Addition

A−B Subtraction

A∗B Multiplication

A/B Division (scalars)

Ab Power (to a scalar)

A0 Transpose

() evaluation order

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

element-by-element operations

I MATLAB supports element-by-element arithmetic operations between matrices.


I The period character (.) distinguishes these operations from the linear algebra
matrix operations.
I Element-by-element operations can only be carried out between matrices of
equal sizes.
I Since the two classes of operations (linear algebra and element-by-element) are
the same for addition and subtraction, the character pairs .+ and .- are not used.

A.∗ B Element-by-element Multiplication

A./B Element-by-element Division

A.B Element-by-element Power

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations


4  
−3 2 3
a = [1 2 3 4] b= 
 2  c=
1 4
4

I a ∗ b = 1 ∗ 4 + 2 ∗ (−3) + 3 ∗ 2 + 4 ∗ 4 = 20
(1 × 4) (4 × 1) (1 × 1)

I b 0 = [4 − 3 2 4] and a ∗ b 0 give error message

I a. ∗ b 0 = [1 2 3 4].∗[4 −3 2 4] = [1∗4, 2∗−3, 3∗2, 4∗4] = [4, −6, −6, 16]

 
7 18
I c2 =
6 19

 2
32
  
2 4 9
I c.2 = =
12 42 1 16

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Exercises 
4  
−3 2 3
I (1) Create a = [1 2 3 4] b=  c= .
 2  1 4
4
I (2) Calculate and create

I X1 = a ∗ b
I X2 = b ∗ a
I X3 = X 1 ∗ X 2
I X4 = a/2
I Extend Matrix X3 with a 5th column whose value is b, and call the
new Matrix M (eg. M = [X3,b])
I Try a ∗ b 0 (What the message says, why?)
I (3) perform element by element calculation a. ∗ b 0 , a0 . ∗ b, a0 ./b and c.2

I (4) Using the colon operator, create a 1-by-25 raw vector with:

I elements containing the integers 1 to 25.


I the first element 0 and the last element 12, with a step size of 0.5.
I (5) Use the Help Field to find out what the commands clear a,
clear all and clc do.
Shi, Yining Application of Matlab for Finance Week 1
Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Predefined Functions

I MATLAB provides a large number of standard elementary mathematical


functions, including abs(.), sqrt(.), exp(.), and sin(.), cos(.),
which require inputs.
I Special functions that do not need an argument include pi, which returns the

number 3.1415 and i which returns the imaginary unit, −1
I Basic graphic function plot(.) is used to visualize the evolution of the vector
A. Also use area(.) to have a different type of graph.
I The best way to find a particular function along with its usage instructions is via
the MATLAB help file.

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Exercises 2
I (6) Using the functions zeros(.), ones(.) and eye(.) to create
I a 4-by-4 matrix A full of zeros
I a 3-by-4 matrix B full of ones
I a 5-by-5 identity matrix I
I a 4-by-4 matrix C full of fives
I

1 5 5 5
 
5 1 5 5
V =
 

5 5 1 5
5 5 5 1
I (7) Find the size and length of matrix B above

I size(.) to return the dimension size of a matrix (row number, col


number)
I length(.) returns the maximum dimension of a matrix.

Shi, Yining Application of Matlab for Finance Week 1


Outline of Course Indexing and Colon Operator
MATLAB Basics Simple Matrix Operations
Matrices and Matrix Operations Element-by-Element operations

Examples

I (8) Find the value of sin(pi/2), cos(pi), exp(2), abs(-5)and


sqrt(122)
" #
1 3 5 4 7 3
I (9) Create a vector A with values and find
1 4 3 2 1 9
I mean(A), mean(A,2)
I sum(A), sum(A,2), cumsum(A(1,:))
I prod(A(1,:)), cumprod(A(1,:))
I (10) Create B equals to the second row of A, and make graphs

I line plot plot(.)


I area figure area(.)
I bar figure bar(.)

Shi, Yining Application of Matlab for Finance Week 1

You might also like