Introduction to GAUSS Programming Language" by Eduardo Rossi ( 2
Total Page:16
File Type:pdf, Size:1020Kb
7htriXuWtcih ti 5)GEE GhcvYrscty i` BcttsVurab 31A@ Eprcha ! WigpcfYX Vy 9Urtch 0urXU 'sYY fcst i` rY`YrYhWYs( UvUcfUVfY Ut% wwwpcttYXugUV !Wigputcha )prcf ! Duhhcha 5)GEE ch IchXiws FbY6Yfp9Yhu EyhtUx @itY ih @ugYrcWUf BrYWcscih HUrcUVfYs ! 1rYUtcha gUtrcWYs ! DY`YrYhWcha 9UtrcWYs # DYsbUpcha 9UtrcWYs $ 1ihWUtYhUtcih !9UtrcxApYrUtirs " 1ih`irgUVcfcty UhX tbY Xit ipYrUtirs # EpYWcUf gUtrcx ipYrUtcihs 8iUXcha 5Uuss 2UtU wctb Gscha tbY 9Utrcx 3Xctir FYxt fYs '( EprYUXsbYYts " Etircha gUtrcWYs ' fYs( # ! 2UtUsYts ' fYs( 7h 2Yptb # E6AI BD7@F 4AD9)F @3I 183)D 2383F3 3xUgpfY 3xUgpfY 6 Flow of Control 25 6.1 Conditional branching: IF ................................. 25 6.2 Loop statements: DO WHILE/UNTIL and FOR ....................... 26 7 Suspending execution 29 7.1 Temporary suspension using commands - PAUSE, WAIT . 29 7.2 Terminating a program using commands - END . 29 8 Publication Quality Graphics 29 8.1 Some Commands . 30 9 GAUSS Procedures 32 9.1 Global and Local variables . 33 9.2 Naming Conventions . 33 9.3 Writing Procedures . 33 9.4 Further Examples . 35 10 GAUSS Functions and keywords 37 11 Monte Carlo Simulations 38 12 References: 40 12.1 References for this handout . 40 12.2 Useful GAUSS Resources: . 40 12.3 GAUSS Resources for Economists: . 40 13 Appendix: GAUSS Functions and Routines - Quick Reference 41 13.1 Linear Regression . 41 13.2 Descriptive Statistics . 41 13.3 Cumulative Distribution Functions . 41 13.4 Di¤erentiation and Integration Routines . 41 13.5 Root Finding, Polynomial Multiplication and Interpolation . 42 13.6 Random Number Generators and Seeds . 42 2 1 Introduction GAUSS is a programming language designed to operate with and on matrices. If the core of your task is matrix manipulation in any way, then GAUSS is likely to be a better bet than a lower level programming language. However, GAUSS is not appropriate for, say, writing a menu system; a general-purpose language is probably easier. Nor is GAUSS appropriate for standard applications on standard datasets. There is little point in writing a probit estimation routine in GAUSS for a small dataset. Firstly, there are already routines commercially available for non-linear estimation using GAUSS. More importantly, TSP, LimDep, etc will already perform the estimation and there is no necessity to learn anything at all about GAUSS to use these programs. However, to get extra speci…cation tests, for example, a straightforward solution would be to code a routine and amend the preexisting GAUSS probit program to call the new procedure at the appropriate point in its working. 1.1 Running GAUSS in Windows 1.1.1 Operating Mode In the graduate computer lab, open GAUSS with Start - Econometrics Softwares - GAUSS 7.0 - GAUSS 7.0 GAUSS instructions can be carried out in two ways: 1. command-line: this is the dialog window you see in front of you when you start GAUSS. In this mode, commands typed into the GAUSS interface are executed immediately. This allows for an instant response to a command, but the commands cannot be stored. This is therefore not suitable for writing large programs, or for commands which need to be run repeatedly. The place for typing commands is marked by >>. 2. program or batch: In this mode, GAUSS commands are typed into a text …le. This …le is then sent to be GAUSS to be run. This allows one to develop and store complex programs. Example programs are in the directory C: gauss7.0 examples. n n 1.1.2 Example Let’srun an example program. 1. In the main menu: File - Open - C: gauss7.0 examples ols.e n n n 2. create new folder C: data n 3. In the main menu: File - Save As... - C: data ols.e n n 4. In the main menu: File - Change Working Directory - C: data n 3 5. Run active …le 6. look at results in the Output window 7. note that output is also stored in …le C: data ols_output.txt n n 4 1.2 The Help Menu GAUSS has two electronic help systems, corresponding to the GAUSS pdf manuals (available at http://www.aptech.com). 1. The "Command Reference" is an easy way to pick up information on commands (as long as they are not deemed "obsolete"), and is organised both alphbetically and by function, which is useful. It is very terse. 2. The "User Guide" is slightly more chatty, and has more examples. 1.3 Syntax GAUSS is not case-sensitive Commands are separated by a semi-colon Anything within the /*...*/ markers is ignored by the program - the markers are used for comments. For example: /* this is a comment */. 1.4 Note on Numerical Precision In Gauss, eight bytes are used to store each element of a matrix. Hence, each cell in a matrix can contain up to eight text characters, or numerical data with a range of about 1:0E 35. If you enter text of more than eight characters into the cells in a matrix, the text will be truncated. Numerical data are stored in scienti…c notation to around 12 places of precision. 5 2 Basics Matrix Operations 2.1 Variables GAUSS variables are of two types: matrices and strings. 1. Matrices may contain numerical data or character data or both. 2. Strings are pieces of text of unlimited length. These are used to give information to the user. If you try to assign a string value to an element of the matrix, all but the …rst eight characters will be lost. Variables need to have names to reference them. Acceptable names for variables can contain alphanumeric data and the underscore "_", and must not begin with a number . Reserved words may not be used; standard procedure names may be reassigned, but this is not generally a good idea. Variables names are not case-sensitive. 1. Acceptable variable names: eric Eric eric1 eric_1 _eric1 _e_r_i_c 2. Unacceptable variable names: 1eric 100 if (reserved word) delif (GAUSS procedure - legal, but foolish) 2.2 Creating matrices 1. Square brackets [] refer to dimensions of a matrix or the coordinates of a block, depending on context. The …rst number refers to the row, the second the column. 2. Curly braces are used within GAUSS to group variables together. f g New matrices can be de…ned at any point (except inside procedures). The easiest way is to assign a value to one. There are two ways to do this: (1) by assigning a constant value or (2) by assigning the result of some operation. 2.2.1 Creating a matrix using constant assignment (LET) The keyword LET creates matrices using constants. Two di¤erent forms of creating a matrix (or vector) x can be utilized: Specifying the shape of the matrix x with the list of constants on the right-hand side If curly braces are not used, a list of constants separated by space will create a column vector. Adding commas has no e¤ect. Hence the following three operations are equivalent: Command Resulting shape of x LET x = 1 2 3 4 5 6; Column vector 6x1 LET x = 1,2,3, 4,5, 6; Column vector 6x1 LET x = 1 2, 3 4, 5 6; Column vector 6x1 If the list of constants is enclosed in braces , then a row vector will be produced. Inserting commas in the list of constants instructs GAUSSfg to form a matrix, breaking the rows at the commas. 6 Command Resulting shape of x LET x = {1 2 3 4 5 6}; Row vector 1x6 LET x = {1,2,3, 4,5, 6}; Column vector 6x1 LET x = {1 2, 3 4, 5 6}; Matrix 3x2 Specifying the shape of the matrix x using square brackets at x[ ] An r c matrix will be created using the following commands. The constants on the right-hand side will be allocated to the matrix on a row-by-row basis. Adding commas has no e¤ects. Command Resulting shape of x LET x[3,2] = 1 2 3 4 5 6; Matrix 3x2 LET x[3,2] = 1, 2, 3, 4, 5, 6; Matrix 3x2 If only one constant is entered, then the whole matrix will be …lled with that number. Command Resulting shape of x LET x[3,2] = 5; Matrix 3x2 Note: LET x = a*b; is illegal as "a*b" is a value and not considered a "constant". In practice, GAUSS will interpret "a*b" as a string constant and will create a string variable containing the letters and …gures "a*b". The desired outcome is achieved using the syntax described in the following section. 2.2.2 Creating a matrix using variable assignment (result of some operation) The results of any operation can be placed into a matrix without a LET explicit declaration. Assigning a value is done by writing down an equation. The result of the operation m1= m2 + m3; will be that the value "m2+m3" is contained in a variable called "m1". If the variable m1 did not exist before this statement, it will have been created. The size and type of a variable depends entirely on the last thing done with it. Suppose m1 existed prior to the last operation. If m2 and m3 are both scalars, then m1 will now be a scalar - regardless of whether it was previously a matrix, vector, scalar, or string. Variables have no …xed size or type in GAUSS - they can be changed at will simply by assigning a di¤erent value to them. 2.2.3 Comparison of constant and variable assignment Why use constant assignments rather than just creating matrices as a result of mathematical or other operations? The answer is that sometimes it is awkward to create matrices of appropriate shapes.