/* The following program represents a primer on basic commands in GAUSS. Written by James Morley, Jan. 18th, 2008 I will provide many, many comments to explain everything. The first thing to mention is the structure for code. In GAUSS, every line that you want the program to read must end with a semi-colon (i.e., ";") For example, the first line of real code (as opposed to comments) uses the command "new" to clear the workspace memory. The line reads new; In terms of comments, there are two ways to signal to GAUSS not to read text. First, as is being done already, you can use "/" and then "*" to begin a long comment. For example, see the very beginning of this program. Then, as is done a few lines below you can use "*" and then "/" to end the long comment. Anything in between will not be read by GAUSS. GAUSS also allows for shorter comments within lines that are started and ended with "@". For example, the second line of real code is a repeat of the first, with a comment new; @this command clears the workspace memory and should be at the start of any program@ */ new; new; @this command clears the workspace memory and should be at the start of any program@ library pgraph; @"library" tells GAUSS that it is going to read in an extra module, in this case "pgraph", which allows us to plot graphs directly in GAUSS@ /* The first substantive part of this program will show how to simulate data, how to output data to a file, and how to read data into the workspace*/ @Simulate Data@ yy=rndn(100,1); @This creates a vector of realizations from a N(0,1) random variable that is of dimension 100x1@ /* Notes: 1. You can draw from a uniform r.v. using "rndu" instead. 2. If you want, say, a N(10, 20^2) variable, simply write the line as follows: "yy=10+20*rndn(100,1);" */ xy(rows(yy),yy); @this uses the pgraph module to plot your simulated data in a separate window. WARNING: GAUSS graphs are ugly!@ @Output Data@ output file=gauss_example1.out reset; output on; yy; output off; /* Notes: 1. This will create a new file in the working directory called "test.out". The working directory can be set by selecting the folder icon in the top right of the task bar. 2. If there is a pre-existing file (i.e., you have already run the program once, it will be written over due to the inclusion of "reset" in the line above. 3. If you want to append to the file, rather than overwrite, change the code to "output file=test.out;" 4. The vector yy will be "printed" to the file "test.out". You can then retreive it from the file in other programs such as EViews or Excel and create more attractive graphs there */ @Input Data@ load y[100,1]=gauss_example1.out; /* Notes: 1. "load" will read the contents of a data file in from the working directory. 2. You have to specify the correct dimension of the data in the file, as is done with "y[100,1]" 3. You can call the series anything you want. I chose "y" */ /* The second substantive part of this program will show how to construct the autocorrelation function for the data series*/ acf={}; @creates an variable "acf" and begins by making it equal to the empty set by setting it to "{}"@ @the point of doing this is that we will append things to acf in a loop, but we want to have the first element set within the loop@ y_bar=meanc(y); @meanc(.) is a function that calculates the mean(s) of a column(s) of the input, in this case "y", which is a vector@ var_y=(1/(rows(y)-1))*sumc((y-y_bar).*(y-y_bar)); @This uses formula 12.5 in Stock and Watson to construct the sample variance@ /* Notes: 1. "rows(y)" returns the number of rows in y, which is 100 in this case. For columns, the command would be "cols(y)", which is 1 in this case. 2. sumc(.) is a function that calculates the sum of a column of the input. 3. In general, "*" is standard multiplication for scalars. When used with vectors or matrices, it conducts matrix multiplication. 4. However, in this case, we have ".*", which means scalar multiplication of element by element for two matrices of the same dimension. */ j=1; @j is the variable that we will loop over. We are setting its initial value for the loop here@ do until j>12; @this defines the terms of the loop. That is we will repeat everything between here and "endo" below, until the condition "j>12" is satisfied@ cov_j=(1/(rows(y)-j-1))*sumc( ( y[j+1:rows(y)]-meanc(y[j+1:rows(y)]) ) .* ( y[1:rows(y)-j]-meanc(y[1:rows(y)-j]) ) ); @This uses the fomula 12.5 in SW. See notes below.@ acf=acf|cov_j/var_y; @append the autocorrelation to the variable acf by attaching it below. I.e., verticle concatenation. for horizontal concatenation, use "~"@ j=j+1; @advance the variable that is looped over@ endo; /* Notes: 1. When y[j+1:rows(y)] is specified, it corresponds to the j+1 to rows(y) elements of the vector y. 2. We could treat y as a matrix instead of a vector by writing the following: y[j+1:rows(y),1]. The "1" means that we are considering the first column of y. 3. We could consider all columns with the following: y[j+1:rows(y),.]. The "." denotes all columns, although there is only one in this case. */ @Plot the ACF@ xy(rows(acf),acf~zeros(rows(acf),1)~ones(rows(acf),1)); /* Notes: 1. "zeros(10,20)" would create a matrix of zeros, with dimension 10x20. 2. "ones(10,20)" would create a matrix of ones, with dimension 10x20. 3. See the use of "~" to horizontally concatenate the vector that contains the acf with vectors of zeros and ones. */ /* The third substantive part of this program will show how to conduct least squares estimation*/ @Generate an "explanatory" variable@ x1=rndn(100,1); @There are different ways to conduct least squares@ @Method 1: The long way@ @Define vector y and matrix X@ y=y; @straightforward in this case@ X=ones(100,1)~x1; @See notes@ @OLS@ b_l=inv(X'X)*X'y; @just basic matrix multiplication. You don't have to write "X'*X" when the first matrix is transposed, but you can if you want.@ /* Notes: 1. GAUSS is not case sensitive. I made "X" a capital letter for my own benefit of thinking of it as a matrix. 2. ones(100,1) are included as a regressor to allow for a constant. */ @Method 2: The shortcut@ @OLS@ b_s=y/X; " b_l b_s "; @Anything between " and " will be printed to the screen@ b_l~b_s; @without "", the contents of b_l and b_s will be printed to the screen@ @Method 3: Using a procedure@ @OLS@ b_p=least_squares(y,x1); @you can call on a procedure that you've written elsewhere (see after "end") to do the calculation. This is useful if it is complex@ @print results to the screen@ "y x1" y~x1; ""; " b_l b_s b_p "; @Anything between " and " will be printed to the screen@ b_l~b_s~b_p; @without "", the contents of b_l, b_s, and b_p will be printed to the screen@ end; @denotes the end of the program.@ @========================@ proc least_squares(y_,x_); local b_; @in a procedure, you have to define the local variables to be determined in the procedure itself.@ b_=y_/(ones(rows(y_),1)~x_); retp(b_); endp; @========================@