Lecture 1
Lecture 1
Lecture 1
Introduction of the MATLAB BTM500
(2) Editor
(3) Workspace
(6) Toolbar
.m Files
• Primary way to save and execute your MATLAB code
• Scripts or documents where you can write multiple lines of MATLAB
commands
• Use a semicolon (;) at the end of a line to suppress output
• Use a percent sign (%) to add comments
• To divide your code into sections that can be run separately, use ‘%
%’
Creating a .m File:
1. In MATLAB, navigate to File > New script.
2. This opens the editor where you can start typing your MATLAB
code.
3. After writing your code, save the file with a ‘.m’ extension.
Introduction of the MATLAB BTM500
Variables
• Variables are the basic building blocks.
• Capitalization matters: ‘x’ is not the same as ‘X’.
• A matrix is a variable that contains multiple entries,
e.g., V=[.98 1.02 .99 1.07];.
• You will get errors if try to add vectors of different sizes of different
orientations.
• Always use brackets [ ] to define matrices.
• Always use parentheses ( ) to access elements of matrices.
• The row is always first, the column is always second:
M(3,2) is not the same as M(2,3).
Introduction of the MATLAB BTM500
Elementary Algebra
• +, -, *, / (addition, subtraction, multiplication, division) follow the
rules of matrix algebra.
• Use ./ and .* for element-wise operations on matrices and vectors.
• Use ‘ to transpose a matrix.
• You can always multiply a matrix by a scalar.
Storing Data
1. Open new ‘.m’ file
2. Type dataset = [ ];
3. Copy all of the data from your Excel or text file and paste it inside
[ ].
4. Save the ‘.m’ file in a working directory; call it ‘filename.m’.
5. At the MATLAB prompt type: filename;
This executes any code in the file ‘filename.m’.
Loading Data
• I have created two daily return time series (year 2006) for British
Petroleum (BP) and Volcom (VLCM), which are stored in files
‘data_bp.m’ and ‘data_vlcm.m’.
• When you type ‘data_bp;’ into the MATLAB prompt, a variable
called ‘bp’ is loaded into MATLAB.
• ‘bp’ has a length of 251 (251 trading days in 2006) and 4 columns:
- Perm Number (Identifier number)
- Date
- Price
- Holding period return
Descriptive Statistics
• Useful functions: max, min, var, cov, sum, abs, exp, sqrt, round
Introduction of the MATLAB BTM500
Getting Help
• ‘help’ lists topic area.
• ‘help graph2d’ lists functions within ‘graph2d’ (e.g., ‘plot’).
• ‘help plot’ gives help on plotting.
• ‘lookfor’ searches for keywords within help.
Potential Problems
• All rows must have the same size!
• MATLAB only understands numbers; it will give you errors if there
are any letters in what you pasted.
• Date formats (e.g., ‘28-Dec-1979’) may be problematic – use three
columns for day, month, and year.
• Don’t forget the semicolon; otherwise, all data will appear on
screen.
Introduction of the MATLAB BTM500
Poisson Distribution
• The Poisson distribution helps us predict how often an event is
likely to occur over a certain period of time.
• For example:
• The number of emails you receive per hour.
• The number of cars passing through a toll booth in a day.
• The number of defects in a length of wire.
• Poisson probability density function (pdf) − 𝜆 𝑥
𝑒 𝜆
𝑓 ( 𝑥| 𝜆 ¿=
𝑥!
• Poisson cumulative distribution function (cdf)
𝑓𝑙𝑜𝑜𝑟 (𝑥) 𝑖
𝜆
𝐹 ( 𝑥| 𝜆 ¿=𝑒
−𝜆
∑ 𝑖!
𝑖 =0
Introduction of the MATLAB BTM500
Next week
• Logic
• Loops
• Creating your own functions