Commands
Arrays and Matrices
Name = begin number:step width:end number
If there is no step width defined than it is automatically one
(begin number ⋯ end number)
Name = [number1, number2, number3; number4, number5, number6]
(number
number 1
4
number 2 number 3
number 5 number 6 )
size(Name)
number of rows , number of columns
Name = linspace( begin number, end number, number of steps)
(begin number ⋯ end number)
For example linspace (0,10,3)
(0 5 10)
If you don’t define a number of steps Matlab automatically assumes you want 100 steps
randn(n, m)
return a matrix n x m matrix of normally distributed random numbers
Saving and loading of documents
save (‘file_name’)
load (‘file_name.mat’)
Clearing Matlab
clear
this command clears the workspace
clc
this command clear the command window
Reading in file
Filename = ‘file.csv’
Name = cvsread(filename)
Read in comma separated file
Name(:,colomn number)
Prints that colomn
Name = num2string(filename)
Convert numerical data into a data string
dataNumber = datenum(date_strings, ‘yyyymmdd’)
Turns a date string into matlab internal date numbers
Dates = datastr(dataNumber, formatOut)
Format matlab internal date numbers into readable date numbers. The Formatout is the format you get the
dates in. If you for example use 24 as formatOut you get the dates in the following manner dd/mm/yyyy.
Instead of 24 you can also write ‘dd/mm/yyyy’ as FormatOut.
,New_vector = Name(:,column)
Write a certain statistic in own vector
If you want to have a vector with the log returns you have to create a vector with only closing prices.
Then create a new vector by using the following formula
Log_returns = diff(log(closeprices))
Graphs
plot (Name)
plot vector Name in a graph. If the input is a matrix Matlab will plot each column.
plot( Namex, Namey)
Plot vector Namey in a graph and use Namex as the x-coordinates
If you want to plot the log returns Namex is dateNumber(2:end), because you are taking difference the
first data point gets lost in the process. This will plot the returns against the matlab internal dates. If
you want readable date to be on the x-axis you have to use the following command
datetick (‘x’)
title(‘title_name’)
xlabel(“xlabel_name’)
ylabel(“ylabel_name’)
The commands above will add a title and/or labels fort the x-axis and y-axis
qqplot(name)
Displays a quantile-quantile plot of the quantiles of the sample data versus the theoretical quantiles values from
a normal distribution.
qqplot(name)
Displays a quantile-quantile plot of the quantiles of the sample data versus the theoretical quantiles values from
the distribution specified
Basic data analysis
length(name)
Gives the number of observations
sum(name)
Gives the sum of all the observations
prod(name)
Gives the product of all observations
min(name)
max(name)
Gives the minimum and maximum of all the observations
range(name)
Gives the difference between the minimum and maximum
mean(name)
median(name)
var(name)
std(name)
sqrt(var(name))
skewness(name)
kurtosis(name)
, Gives information about the distribution of the variable name
corrcoef(name)
Returns the matrix of correlation coefficients for name, where the columns of name represent random variables
and the rows represent observations.
quantile(name, probability)
Gives you the value that belongs to that probability. The chance that a value is smaller than the output is equal
to the probability you put in for input
sort(name)
Puts the values of the vector in the order from small to large
abs(name)
Makes all the values of the vector absolute
diff(name)
Takes the differences between the observations
Matrix Calculations
MatrixA= [number 1, number 2; number3, number 4]
MatrixB= [number 5, number 6; number7, number 8]
MatrixA .* MatrixB
(number 1∗number 5
number 3∗number 7
number 2∗number 6
number 4∗number 8 )
MatrixA * MatrixB
Multiplying matrices the same way as always (row times column)
MatrixA’
Gives the transpose of matrix A
Distributions
normalpdf(x, mu, sigma)
the chance you find value x in the normal distribution with mean mu and standard deviation sigma. If you put in
a vector your output will be a vector with probabilities. If mu and sigma are not defined Matlab assume mu to
be zero and sigma to be 1 (Z-distribution).
normalcdf(x, mu, sigma)
the chance you find value x or smaller in the normal distribution with mean mu and standard deviation sigma. If
you put in a vector your output will be a vector with probabilities. If mu and sigma are not defined Matlab
assume mu to be zero and sigma to be 1 (Z-distribution).
norminv(p, mu, sigma)
The output is the value for which the probability you will obtain that exact value or something smaller, is equal
to p.
normrnd(mu, sigma, number of rows, number of columns)
Creates a matrix with every cell containing a random variable of a normal distribution with mean mu and
standard deviation sigma.
[muhat,sigmahat,muci,sigmaci] = normfit(name)
Estimates the mu and sigma for each colomn of data. Also gives the confidence intervals (first a row with the
lower bound and then a row with upper bounds). If muci and sigmaci aren’t put in you only receive the
estimates as output