Matlab Lecture 1b
Matlab Lecture 1b
• Introduction to Matlab
• Simulation
• Portfolio Analysis
• Statistics and Time Series
• Fixed Income
• Options
• Go to finance.yahoo.com
• Enter a quote and hit get quotes
• Click on historical prices
• Enter a date range, scroll to bottom and click
Download To Spreadsheet
• Open spreadsheet in Excel
• Problem: dates come out weird if load into
Matlab
• Problem: this gives prices, usually need to
convert them to returns
Finding Data: WRDS
• Go to wrds.wharton.upenn.edu/connect
• Select dataset (i.e. CRSP)
• Select series (i.e. daily stocks)
• Select company name (i.e. VLCM) and
which info you want (i.e. prices and holding
period return)
• Click submit request and wait a moment
• Open the text file
• CRSP has historical stock and bond data,
COMPUSTAT has accounting data
CRSP output
Loading Data
• >>var(dataset(:,4)) %variance
• >>cov(dataset(:,4),bp(:,4)) %similar to corrcoef but
gives the variance and covariance
• >>sum(dataset(:,4)) %adds up all numbers in the
4th row of dataset
• >>abs(x) %absolute value
• >>x.^3 %takes powers (note, . only needed for
element by element)
• >>exp(x) %exponent
• >>sqrt(x) %square root
• >>round(x) %rounds to nearest integer
Plots
• >>[a b]=size(dataset); t=1:a;
• >>plot(t,dataset(:,4),’b’);
Plots
• >>hold on;
• >>plot(t,bp(:,4),’r--’);
Multiple plots
• >>hold off;
• >>subplot(2,1,1);
• >>plot(t,dataset(:,3),’b’);
• >>subplot(2,1,2);
• >>plot(dataset(:,4),bp(:,4),’k.’);
Labels
• >>xlabel(‘Volcom’);
• >>ylabel(‘British Petroleum’);
• >>title(‘Scatter Plot’);
Saving your work
• >>save workspace1 x y % saves the
variables x and y in a file called
workspace1.mat
• >>save workspace1 % saves all variables
in workspace1.mat
• >>load workspace1 % loads the variables
in workspace1.mat
• >>clear all % deletes all variables
Getting help
• >>help %lists topic areas, for example one
of these is graph2d
• >>help graph2d %lists functions within
graph2d, for example plot
• >>help plot %gives help on plot
• >>lookfor keyword %searches for
keywords within help, pretty slow
Next week
• Logic
• Loops
• Creating your own functions
Functions we learned