Brief Tutorial for Gauss
This page is maintained by the software tutors. For errors and/or amendments please contact the current tutor supporting the program.
Introduction
Gauss is a programming language and a program that operates with matrixes. It is used by many econometricians and is often appreciated among economists because of its good optimisation routines. The latest version is Gauss 9.0 (July 2008).
Quick start
You can write Gauss commands directly to the command line (usually in the middle of the screen, starting with “»” sign) or create a script file ( File -> New ) and run it by clicking on F6 or pressing the “Run active file” button on the top of the screen. For example, to create a 3*3 matrix of random numbers from a uniform distribution with the name A, type “ A=rndu(3,3) ;”. In order to see the matrix, type “ print A; ”. You can also see the matrix on the Symbols panel folder Matrices , usually on the left-hand side of the screen.
You are now ready to work!
Basic commands
More useful commands
Useful to know (especially for Matlab users)
Gauss is case sensitive (Matlab is not case sensitive)!
You must use semi-colon “;” after each command (it is not an option as in Matlab).
Gauss first compiles your full program and then executes it (while Matlab goes line-by-line. Compilation makes Gauss faster, but mistakes are more complicated to spot).
Square brackets “[]” are used to refer to a point (or part) of a matrix (Matlab uses round brackets).
Curly brackets “{}” are used if a procedure has several outputs (Matlab always uses square brackets).
Be careful with the multiplication and division: Gauss interprets the commands differently from Matlab.
Gauss does not have fixed file extensions, still many people use .g for the main script file, .gss or .prc for procedures (unlike Matlab which has the .m file).
For procedures (functions in Matlab) which are not in the main library you must declare the use of that library before the command, e.g. library pgraph; for making figures.
For user-defined procedures, libraries must be declared before their use unless they are in the same folder as your script where you are calling the procedure (Matlab finds the functions if in the same folder or defined by the “set path” command).
When selecting a full row/column of a matrix ABC, for example extracting second row, type “ A=ABC[2,];” or “ A=ABC[2,.];” (for Matlab use a colon “:”, for example A=ABC(2,:)).
You can also select multiple columns/rows of a matrix; for selecting the second row second and fourth elements write: “ A=ABC[2,2 4];” . For selecting second row from the second to the fourth element write “ A=ABC[2,2:4];” .
Comments in the script or procedure are made as follows: “ @ Comments here @” or “ /* Comments here */” . They are equivalent and can both be used on multiple rows.
Working directory (usually shown on the top of the screen) is important when reading in data from a specific folder, finding the matrixes you have saved and finding user-defined libraries.
Start a script file by writing “ new; ”; this will clear the memory from previous matrixes.
The ready-made Gauss procedures can be found in the Source panel Gauss.lcg folder, usually on the left-hand side of the screen organised by the names of libraries.
Created matrixes can be seen at the Symbols panel or by typing into command line print A; for looking at the matrix A.
Press “Ctrl+L” to see previously typed commands.
And once more, in the script file, never forget to add the semi-colon “;” at the end of the command! In case you still do miss it, be aware that Gauss starts looking for a mistake in the following line!
If statement
if x==7;
print "x is equal to seven!";
elseif x==13;
print "x is equal to thirteen!";
else;
print "x is neither 7 nor 13";
endif;
Loops
FOR loop was only recently added to the Gauss commands. Therefore you find them fairly rarely in the existing codes, but this is the way to do it now. Following example shows you how to fill a vector with numbers from one to ten. (Matlab users see that the end statement is different for the two loops and brackets for the assignment are square!)
xss2 = zeros(10, 1);
for i(1, 10, 1); @Start, end, Step@
xss2[i,1] = i;
endfor;
print xss2;
The same code function for the while loop is:
xss1 = zeros(10, 1);
i=1;
do while i<=10;
xss1[i,1] = i;
i=i+1;
endo;
print xss1;
Figures
For making a figure, define first the library you want to use (that is pgraph ) and set options to default (look for the language reference for different settings), then you are ready to start a figure. This is an example:
library pgraph;
graphset; @Sets options to defaults@
MYmatrix = rndu(200,3);
MYmatrix = cumsumc(MYmatrix);
xlabel("Time");
ylabel("Data");
title("Graph Example"); @Break the line in the middle@
xy(seqa(1,1,rows(MYmatrix)), MYmatrix); @seqa(1,1,rows(xt)) creates a sequence of numbers from 1 to 200 for the scale@
By default Gauss graphs have black background; if you like white background better, go to View -> Options . Choose Background color “ White ” and “ Convert all colors to black ”.
You can save the figures in .eps format. From the figure window take Convert –> Encapsulated Postscript .
How to create your procedure
Parts of code that you want to use multiple times can be written in procedures. In order to try out how it works save the example procedure (below) in your working directory under the name “ sample_procedure.prc ”.
Example of a Procedure:
proc (2)=sample_procedure(a,b); @This is a two-input requiring and two-output producing procedure@
local x,y; @Define the variables you want to use as locals (can be seen only inside the procedure)@
x=rndn(a,b);
y=rndu(a,b);
retp(x,y); @Define the outputs of the procedure@
endp; @Say that the program is now finished@
Call for this procedure from a script:
{nl,um}=sample_procedure(5,2);
Documentation