2015-9-11nonparametric econometrics1 intro to matlab for data analysis and statistical modeling
TRANSCRIPT
![Page 1: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/1.jpg)
23/4/19 Nonparametric Econometrics 1
Intro to Matlab for Data Analysisand Statistical Modeling
![Page 2: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/2.jpg)
23/4/19 Nonparametric Econometrics 2
Useful linkshttp://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp
The help links on this page include: Mathworks’ Getting Started (the official online documentation of Matlab) Kermit Sigmon’s MATLAB Primer (a very good beginner manual); University of Utah’s MATLAB Tutorial Some others.
MATLAB’s online help manual
![Page 3: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/3.jpg)
23/4/19 Nonparametric Econometrics 3
What is MATLAB?
MATLAB is a matrix-based tool for numerical computations. It’s very powerful and easy to use.Both programming language and interactive environmentVery fast native functions; somehow slow when processing loopsLots of available toolboxes
![Page 4: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/4.jpg)
23/4/19 Nonparametric Econometrics 4
Launching Matlab
Click “MATLAB xx” from the start menu(on Unix systems: type “matlab” to enter interactive mode)
![Page 5: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/5.jpg)
23/4/19 Nonparametric Econometrics 5
The Interface
Main Window: Input/OutputWorkspace: consists of the variables you create during a MATLAB session;Current Directory browser: shows you where you are.Command History: double click them to evaluate them;Editor/Debugger: pops up when you create an M-file (click on “New” button to launch it.)
![Page 6: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/6.jpg)
23/4/19 Nonparametric Econometrics 6
Entering Matrices
Matrices can be Entered manually
A = [1 2 3 ; 4 5 6 ; 7 8 9] Generated by built-in functions
(e.g., eye(3), ones(2,1), zeros(3,3)
Loaded from a file (e.g.,data)
![Page 7: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/7.jpg)
23/4/19 Nonparametric Econometrics 7
Matrix operations:+ addition- subtraction* multiplication ^ power‘ transpose\ left division, / division
x = A \ b is the solution of A * x = b x = b / A is the solution of x * A = b
To make ‘*’ , ‘^’, ‘\’ and ‘/’ apply element-by-element, we precede the operators by ‘.’
![Page 8: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/8.jpg)
23/4/19 Nonparametric Econometrics 8
Subscripts:Subscripts: the element in row i and column
j of A is denoted by A(i, j). i,j can also be vectors of indices or logical arrays:A=4*[1 2 3 4 5 6 7 8 9]’b=A>18; c=(5 6 7 8 9)’
A(b) gives same result as A(5;6;7;8;9) because b=(0 0 0 0 1 1 1 1 1)’
![Page 9: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/9.jpg)
23/4/19 Nonparametric Econometrics 9
The Colon Operator ‘:’
The colon ‘:’ is one of MATLAB ’s most important operators. It has many formats:[0:0.2:3] is a row vector containing numbers from 0 to 3, in increments of 0.2
e.g., A=[0:0.2:3] or A=0:0.2:3Subscript expressions involving colons refer to portions of a matrix: A(1:3 , 2) is the first to the third elements of the second column of A.
![Page 10: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/10.jpg)
23/4/19 Nonparametric Econometrics 10
Working with Matrices:Four functions that generate basic matrices:Zeros: all zeros. A = zeros(1,3)Ones: all ones. A = ones(2,4)Rand: elements are U[0,1] random variables
A = rand(3,5)Randn: elements are standard-normal random variables A = randn(2,5)Be careful: Matlab always sets the same seed.Get ‘more random’ numbers by typing rand('state', sum(100*clock))
Sometimes you want the seed to be fixed in your code, you can set rand(‘state’,r) where r is the rth simulation
![Page 11: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/11.jpg)
23/4/19 Nonparametric Econometrics 11
A=eye(3) gives a 3-by-3 identity matrixsparse(m,n): same as zeros(m,n), use if most elements are zeros.
e.g., A=eye(3); B=sparse(A)Concatenation: join small (compatible) matrices to make bigger ones:
B = [A A-2; A*2 A/4]
Deleting rows and columns: B(:,2) = [ ]
![Page 12: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/12.jpg)
23/4/19 Nonparametric Econometrics 12
Putting it together
Many operations can take Matrix inputs. Example:A = [1 2 3 ; 4 5 6 ; 7 8 9] B=A>5Use indices and element-by-element operations to avoid slow and unwieldy loops:beta=0.925;auxil=1:200;betavec(auxil)=beta.^auxil;betavec=betavec’
![Page 13: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/13.jpg)
23/4/19 Nonparametric Econometrics 13
Suppressing Output:
If you simply type a statement and press Enter, MATLAB automatically displays the results on screen. If you end the line with a semicolon ‘;’, MATLAB performs the computation but does not display any result.
Example: C = randn(5,1) versus
C = randn(5,1);
![Page 14: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/14.jpg)
23/4/19 Nonparametric Econometrics 14
Functions: MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, sin. For a list of the elementary mathematical functions, type: help elfun
(e.g.,sin, cos, exp, log, log10,log2,abs,real)For a list of more advanced specificic mathematical functions and elematary matrix functions, type
help specfun (e.g., factorial,erf)help elmat (e.g., zeros,ones,eye
For a list of data analysis functions, typehelp datafun (e.g., max,min,
mean,median, std, var, sort, hist, corrcoeff skewness, kurtosis,cumsum)
![Page 15: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/15.jpg)
23/4/19 Nonparametric Econometrics 15
Flow Control:
MATLAB has following flow controls:
If statementFor loopsWhile loopsContinue statementBreak statement
![Page 16: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/16.jpg)
23/4/19 Nonparametric Econometrics 16
if … elseif … else … end
If A > B‘greater’
elseif A < B‘less’
elseif A = = B‘equal’
else error(‘Unexpected situation’)end
![Page 17: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/17.jpg)
23/4/19 Nonparametric Econometrics 17
for … end
for i = 1:m
for j = 1:n
H(i,j) = 1/(i+j);
end
end
![Page 18: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/18.jpg)
23/4/19 Nonparametric Econometrics 18
Graphics: plot
x = [0 : .01 : 2*pi];
y = sin(x);
plot(x,y)
y2 = sin(x-.25)
y3 = sin(x-.5)
plot(x,y,x,y2,x,y3)
![Page 19: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/19.jpg)
23/4/19 Nonparametric Econometrics 19
Programming with MATLAB:
Files that contain code in the MATLAB language are called M-files. You can create M-files using the matlab editor, then use them as you would any other MATLAB functions or commands. There are two types of M-files: Scripts and Functions.
![Page 20: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/20.jpg)
23/4/19 Nonparametric Econometrics 20
Scripts
Scripts: a bunch of code grouped together; doesn’t accept argument or return output.Exampleopen m-file editortype disp(‘Hello, China!’)save as test.m in c:\tempadd c:\temp to path directory (File/Set Path)Execute by typing “test”
![Page 21: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/21.jpg)
23/4/19 Nonparametric Econometrics 21
Functions:
Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. For example, save this as area.m in c:\temp:function ar = area(radius)ar=pi*radius^2;
![Page 22: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/22.jpg)
23/4/19 Nonparametric Econometrics 22
Function functions
Once you have defined a function, you can use functions that have functions as arguments – function functions!E. g. search for minima, zero values.Example: first we define the function x2-3:function x=example(input)x=input.^2-3;Now, we can easily find minima and zeros:fminbnd(@example,-2,2), fzero(@example,2)
![Page 23: 2015-9-11Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling](https://reader036.vdocuments.us/reader036/viewer/2022062516/56649e375503460f94b26f33/html5/thumbnails/23.jpg)
23/4/19 Nonparametric Econometrics 23
Learn from othersThere are lots of Matlab functions already out there:Google them!http://www.mathworks.com/matlabcentral/James LeSage’s econometrics toolbox:http://www.spatial-econometrics.com/Don’t forget to “set paths” so that Matlab can find your new .m-files.