1 / 42

Introduction

Introduction. Downloads. Today’s work is in: matlab_lec01.m Datasets we need today: data_bp.m, data_vlcm.m All course materials can be found here: http://personal.lse.ac.uk/faviluki/fm457.html Save all of your matlab files to H:Matlab

wattan
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction

  2. Downloads • Today’s work is in: matlab_lec01.m • Datasets we need today: data_bp.m, data_vlcm.m • All course materials can be found here: http://personal.lse.ac.uk/faviluki/fm457.html • Save all of your matlab files to H:\Matlab • Make sure same folder is selected as Current Folder at the top of the Matlab window

  3. What is Matlab? • “The Language of Technological Computing” • A very fancy calculator • A simple and user friendly programming language • A tool for data analysis and statistics • A tool to produce pretty plots

  4. Accessing Matlab

  5. Basics • From any LSE computer: Start/Programs/Specialist and teaching software/Mathematics/Matlab/Matlab R2012a • Student version can be purchased at discount: http://www.mathworks.co.uk/academia/student_version/ • One of several online tutorials: http://www.indiana.edu/~statmath/math/matlab/gettingstarted/index.html • Matlab Website: http://www.mathworks.com/

  6. Course Outline • Introduction to Matlab • Simulation • Volatility • GARCH and VAR • Multiple Securities • Factor Models • Option Pricing • Fixed Income Only by practicing what you see in class will you get comfortable with Matlab!!!

  7. .m files • .m files are just text files • Do work in .m file • Paste work into Matlab prompt • Use ; to end lines • Save .m files • Use % to add comments

  8. The Matlab Prompt • This is where commands are executed >> denotes where you type commands • Type or paste commands, then hit enter • If entering multiple commands, separate them by ; • Multiple commands may be on same line, as long as they are separated by ; • Ending a command with ; also tells Matlab to not display the output • If want output displayed, do not use ; >>2+2 ans = 4 >>

  9. Variables • Variables are the basic building blocks • Capitalization matters, x not same as X • Matrix is a variable that contains multiple entries, i.e. V=[.98 1.02 .99 1.07]; • Matlab is great with vectors and matrices, however, their orientation and size matter • You will get errors if try to add vectors of different size or different orientation

  10. Basic Operations • Always use brackets [ ] to define matrices • Always use parenthesis ( ) to call values of matrices • The row is always first, the column is always second, i.e. M1(3,2) is not the same as M1(2,3) • To see which variables exist, use >>whos • To delete variables x and y, use >>clear x y • To find out size use >>size(M1)

  11. Elementary Algebra • Use +, -, *, / for basic operations • Use ./ and .* for element by element operations on matrices and vectors • Use / and * for matrix multiplication, but this only makes sense if you are familiar with Linear Algebra • Use ‘ to transpose a matrix • You can always multiply a matrix by a scalar • You can always overwrite old variables

  12. Ranges of numbers Colon specifies range of numbers >>V3=[1:5] sets V3 to be the numbers 1 through 5 >>V3=[1:3:13]’ sets V3 to be the numbers 1 through 13, skipping every 3rd. Note, it is transposed >>M5=M1(1:2,1:5) sets M5 to be all the numbers in rows 1-2 and columns 1-5 of M1 >>V3=M1(:,2) sets V3 to be the whole second column of M1

  13. Finding Data: Yahoo • Go to finance.yahoo.com • Enter a quote and hit get quotes • Click on historical prices (left toolbar) • 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

  14. Finding Data: WRDS • Go to http://wrds-web.wharton.upenn.edu/wrds/connect/ • Follow directions to sign in • 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

  15. Stock Return • For most of our purposes the correct security return to use is the Holding Period Return • This is the return to you if you buy the security today, sell the security tomorrow, and keep any dividend payments from the security • Rt+1=(Pt+1+Dt)/Pt • Sometimes 1 is subtracted so that when the return is 3%, .03 instead of 1.03 is reported • CRSP does this • Use whatever convention you are most comfortable with, but make sure you are consistent

  16. CRSP output

  17. Loading Data • Open new .m file • Type dataset=[ ]; • Copy all of the data from your Excel or text file and paste it inside [ ] • Save the .m file in Matlab’s work directory (I recommend H:\Matlab) • For example call the file matlabin.m

  18. Loading Data • At the Matlab prompt type: matlabin; • This executes any code in the file matlabin.m • Inside matlabin.m you have defined a matrix called dataset, this matrix now exists in Matlab’s memory just like any other matrix

  19. Potential Problems • All rows must have same size!!!!!!!! • Matlab only knows numbers, it will give you errors if there are any letters in what you pasted!!!!!!! • Do not include field headings in your copy/paste

  20. Potential Problems • Date formats (i.e. 28-Dec-1979) may be problematic. If pasting from Excel, I suggest not pasting dates, or formatting them to numbers • Don’t forget semicolon, otherwise all data will appear on screen • This procedure may crash your computer if dataset is too big (50000 numbers should be no problem)

  21. Writing Data • Matlab can write any variable to a text file >>dlmwrite(‘filename.txt’, X, ’ ’); • You can then open this text file manually, or have Excel open it • If open in notepad, sometimes line breaks look weird. Just open it in something else, like Word or Wordpad • Related functions: dlmread, xlswrite, xlsread

  22. Data Sets • I have created datasets from two securities British Petroleum (BP) and Volcom (VLCM) which are stored in files data_bp.m and data_vlcm.m • These contain the daily return for the year 2006 for each security • When you type >>data_bp; into the Matlab prompt a variable called bp is loaded into Matlab • Note that the file name (data_bp) is different from the variable name (bp), having same name will lead to problems!

  23. Data Sets • bp has length 251 (251 trading days in 2006) • bp has width 4, the rows are: • Perm Number (unimportant info from CRSP) • Date • Price • Holding period return

  24. Useful functions

  25. More functions >>var(bp(:,4)) %variance >>cov(bp(:,4),bp(:,4)) %similar to corrcoef but gives the variance and covariance >>sum(bp(:,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

  26. Plots >>[a b]=size(vlcm); t=1:a; >>plot(t,vlcm(:,4),’b’);

  27. Plots >>hold on; %keeps what is on current plot >>plot(t,bp(:,4),’r--’);

  28. Multiple plots >>hold off; %turns off hold on >>subplot(2,1,1); %splits plot into 2 plots %split is vertical, currently on top plot >>plot(t,vlcm(:,3),’b’); >>subplot(2,1,2); %switches to bottom plot >>plot(vlcm(:,4),bp(:,4),’k.’);

  29. Labels >>xlabel(‘Volcom’); >>ylabel(‘British Petroleum’); >>title(‘Scatter Plot of Returns’);

  30. 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

  31. 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

  32. Next week • Logic • Loops • Creating your own functions

  33. Functions we learned • General: whos, clear, size, dlmwrite, save • Matrix: zeros, ones, whos • Math: +, -, *, /, ^,sum, exp, abs, sqrt, round • Stats: min, max, mean, std, corrcoef, var, cov • Plots: plot, hold, subplot, xlabel, ylabel, title

More Related