Matlab is a language particularly good at matrix computation. This makes it ideal for carrying out econometric estimations. This page gives a simple introduction on how to use Matlab.

First you will have to make a Tab-delimited data file, which we name "input.txt" and looks like this:

1 0.39 1.88 2 0.29 2.88 3 0.19 3.88 4 0.09 4.88

Type:

load input.txt;

Then there is variable "input" in memory, which is a matrix with 4 rows and 3 columns. We can assign columns of "input" to different variables:

y = input(:, 2); x = input(:,3);

Now we have two more variables in memory, y and x,

x =

0.39 0.29 0.19 0.09y =

1.88 2.88 3.88 4.88

The function "ones(m, n)" creates a matrix of ones with m rows and n columns. If n = 1, it is a vector of ones, which is useful in adding a constant to regressions. For example,

X = [ones(4, 1) x];

creats a new variable X, which is a matrix of 4 rows and 2 columns:

X=

1 0.39 1 0.29 1 0.19 1 0.09

Note that the space between "ones(4,1)" and "x" is important, which denotes horizontal concatenation. If we use ";", which denotes vertical concatenation, we will get a different X which is a column vector with 8 elements.

The matrix-form econometric fomula can easily be translated into Matlab language. Take the familiar fomula of OLS estimator as an example:

The corresponding Matlab statement is:

beta_hat = inv(X' * X) * X' * y,

where inv(.) is a function that calculates inverse of a square matrix, ' denotes operation of "transpose", and * multiplication.

To save results to a file on hard disk, the first thing to do is to specify a file to write to or create a new file. Both can be accomplished by:

fid = fopen('filename.txt', 'w');

"fid" is a number, called the "handle" of the file, returned by the function fopen(). 'w' gains permission to write to the file, or create a new one if the file 'filename.txt' does not exist.

Then it is ready to write data to the file using function "fprintf()". For example, if we type

then the file "filename.txt" will look like this:

fprintf(fid, '%3.2f %3.2f\n', [x y]') ;

0.39 1.88 0.29 2.88 0.19 3.88 0.09 4.88

'%3.2f %3.2f\n' is called the "format string", which tells Matlab to write both x and y in the format of float number with total length of 3 and 2 digits in fraction.

In the end, we should close the file using the funtion "fclose()":

fclose(fid);

Now we can estimate a classical regression model specified as:

or in matrix form,

,

Copy the following data into a text file, name it "input.txt". The 3th column corresponds to x, random numbers from a uniform distribution with range (0, 1). The second column corresponds to y, random numbers generated by

,

1 1.023238 0.019640 2 2.365127 0.681277 3 1.748397 0.379481 4 2.677743 0.831796 5 1.997575 0.502813 6 2.424230 0.709471 7 1.859978 0.428892 8 1.600016 0.304617 9 1.357601 0.189654 10 1.386270 0.193431

Now we pretend not knowing a and b and estimate them from the data using OLS. Copy the following Matlab code into Matlab Editor,

%%%%%work.m%%%%% % Load data and assign variables:

load input.txt;

y = input(:, 2);

x = input(:, 3);

n = length(x);

X = [ones(n, 1) x]; % Regressor matrix

m = 2; % Number of regressors% OLS estimate

beta_hat = (X'*X)\X'*y;% Calculates regression statistics

e = y - X*beta_hat; %Estimated disturbances

sse = e'*e; %SSE (Sum of Square Errors)

s2 = sse/(n - m); %Estimate of sigma square

var = inv(X'*X)*s2; %Variance of the OLS estimate

se = sqrt(diag(var)); %Standard Error of the estimates% Report results

fid = fopen('output.txt', 'w');

fprintf(fid, '%f (%f)\n', [beta_hat se]');

fclose(fid);%%%%%%End of work.m%%%%%%

and type

work ;

The output file "output.txt" looks like this:

0.981432 (0.004195)

2.033886 (0.008550)

The estimated value of a is 0.981432 with a standard error of 0.004195, and the estimated value of b is 2.033886 with a standard error of 0.008550. Not bad!

Always consult online documentation for help.

*(Joe Junhui Qian 2004.09.08)*