Você está na página 1de 22

A Short Introduction to GAUSS

Luís Catela Nunes

Faculdade de Economia - Universidade Nova de Lisboa

October 2005

Contents

0. Introduction.............................................................................................2

1. Creating matrices ....................................................................................3

2. Matrix operations....................................................................................4

3. GAUSS commands .................................................................................6

4. Graphics ..................................................................................................9

5. Conditional branching...........................................................................11

6. Random numbers ..................................................................................12

7. Loops.....................................................................................................14

8. Simulation .............................................................................................16

9. Procedures.............................................................................................20

10. Maximum likelihood estimation – MAXLIK.....................................21


0. Introductory notes

1. The main GAUSS executable program is c:\gauss\gauss.exe.


2. GAUSS does not distinguish between lowercase and uppercase letters.
3. All instructions end with a semicolon ( ; ).
4. Comments within programs start with /* or @, and end with */ or @,
respectively.
5. Advice: write the command NEW; at the beginning of a program in order
to wipe out everything in memory and to avoid mixing up calculations
with results obtained from previous commands.
6. When you close GAUSS all matrices in memory are erased. The command
SAVE can store a matrix in a file, and the command LOAD can read it
back.
7. The decimal symbol in GAUSS is the dot ( . ).
8. Folder and file names must use the DOS format (a maximum of 8
characters without spaces).
9. In the folder c:\gauss there are two files which can be edited:
• Startup – a command file that is executed every time GAUSS starts.
This can be useful for example if you want to automatically change the
working folder when you start GAUSS (for example, if you your files
are all in c:\work then you may want to add to the Startup file the
following command: chdir c:\work;).
• Gauss.cfg – GAUSS configuration file where one can change the
amount of memory available to store matrices. For example:
max_workspace = 8.0, allocates 8 megabytes of memory. When
working with large matrices it is possible that GAUSS reports the
following error:
“(0) : error G0030 : Insufficient workspace memory”.
This is a case where you need to increase the value of
max_workspace. Note that the maximum value you can choose
depends on the physical RAM installed in the computer.
10. All the examples in these course notes can be found in
c:\gauss\examples\curso.
11. Where to obtain additional information on GAUSS?
• GAUSS online Help.
• GAUSS manuals.
• http://www.aptech.com .
• The GAUSS mailing list: http://www.aptech.com/s2_gaussians.html .

2
1. Creating matrices

1.1. Examples (ex-01.gss)


Program Output
LET W[4,2] = 1 5 W =
2 5 1.0000000 5.0000000
3 5.8 2.0000000 5.0000000
4 5; 3.0000000 5.8000000
"W ="; W; 4.0000000 5.0000000

WW = { 1 5 , WW =
2 5 , 1.0000000 5.0000000
3 5.8 , 2.0000000 5.0000000
4 5 }; 3.0000000 5.8000000
"WW ="; WW; 4.0000000 5.0000000

W1 = W[3,2]; W1 =
"W1 ="; W1; 5.8000000

"W[3,.]"; W[3,.]
W[3,.]; 3.0000000 5.8000000
W[2:4,1]
"W[2:4,1]"; 2.0000000
W[2:4,1]; 3.0000000
4.0000000
"W[1 3 4,.]"; W[1 3 4,.]
W[1 3 4,.]; 1.0000000 5.0000000
3.0000000 5.8000000
4.0000000 5.0000000

LET Y[4,1]=10 20 30 40; Y =


"Y ="; 10.000000
Y; 20.000000
30.000000
40.000000
"W~Y"; W~Y
W~Y; 1.0000000 5.0000000 10.000000
2.0000000 5.0000000 20.000000
3.0000000 5.8000000 30.000000
4.0000000 5.0000000 40.000000

LET Z[2,2]=100 0 0 100; Z =


"Z ="; 100.00000 0.00000000
Z; 0.00000000 100.00000
W|Z
"W|Z"; 1.0000000 5.0000000
W|Z; 2.0000000 5.0000000
3.0000000 5.8000000
4.0000000 5.0000000
100.00000 0.00000000
0.00000000 100.00000

"(Y~W~Y)|(Z~Z)"; (Y~W~Y)|(Z~Z)
(Y~W~Y)|(Z~Z); 10.000000 1.0000000 5.0000000 10.000000
20.000000 2.0000000 5.0000000 20.000000
30.000000 3.0000000 5.8000000 30.000000
40.000000 4.0000000 5.0000000 40.000000
100.00000 0.00000000 100.00000 0.00000000
0.00000000 100.00000 0.00000000 100.00000

3
1.2. More examples on matrix creation (ex-02.gss)

(a) Reading data from a text file called data.txt:


1.1 1 3
0.1 2 2
0.9 3 1
2 4 0

Program Output
load a[4,3] = a=
c:\gauss\examples\curso\data.txt; 1.1000000 1.0000000 3.0000000
0.10000000 2.0000000 2.0000000
"a="; a; 0.90000000 3.0000000 1.0000000
2.0000000 4.0000000 0.00000000

(b) Special matrices:


b=zeros(5,3); b=
0.00000000 0.00000000 0.00000000
"b="; b; 0.00000000 0.00000000 0.00000000
0.00000000 0.00000000 0.00000000
0.00000000 0.00000000 0.00000000
0.00000000 0.00000000 0.00000000

c=ones(2,4); c=
1.0000000 1.0000000 1.0000000 1.0000000
"c="; c; 1.0000000 1.0000000 1.0000000 1.0000000

(c) Additional instructions to create matrices:


EYE Creates an identity matrix.
SEQA Creates an additive sequence vector.

2. Matrix operations
Commonly used matrix operators:

+ Addition
- Subtraction
* Matrix multiplication
.* Element-by-element multiplication
^ Element-by-element exponentiation
./ Element-by-element division
/ Division or linear equation solution
.*. Kronecker (tensor) product
‘ Transpose operator

4
Examples of addition operations (ex-03.gss):

Program Output
let a[5,2] = 10 10 a=
10 20 10.000000 10.000000
10 30 10.000000 20.000000
10 40 10.000000 30.000000
10 50; 10.000000 40.000000
"a="; a; 10.000000 50.000000

let b[5,2] = 1 1 b=
2 1 1.0000000 1.0000000
3 1 2.0000000 1.0000000
4 1 3.0000000 1.0000000
5 1; 4.0000000 1.0000000
"b="; b; 5.0000000 1.0000000
"a+b="; a+b; a+b=
11.000000 11.000000
12.000000 21.000000
13.000000 31.000000
14.000000 41.000000
15.000000 51.000000

c = 100*ones(5,1); c=
"c="; c; 100.00000
100.00000
100.00000
100.00000
100.00000
"a+c="; a+c; a+c=
110.00000 110.00000
110.00000 120.00000
110.00000 130.00000
110.00000 140.00000
110.00000 150.00000

let d[1,2] = 100 200; d=


"d="; d; 100.00000 200.00000

"a+d="; a+d; a+d=


110.00000 210.00000
110.00000 220.00000
110.00000 230.00000
110.00000 240.00000
110.00000 250.00000

e = 1000; e=
"e="; e; 1000.0000

"a+e="; a+e; a+e=


1010.0000 1010.0000
1010.0000 1020.0000
1010.0000 1030.0000
1010.0000 1040.0000
1010.0000 1050.0000

5
3. GAUSS commands
3.1 Commonly used GAUSS commands:
Linear equation solution:
INV Matrix inversion.
b/A Linear equation solution Ax = b.

Operations on the columns of a matrix:


CUMSUMC Cumulative sum of the values in each column.
SUMC Sum of the values in each column.
MAXC Maximum element of each column.
MINC Minimum element of each column.

Matrix dimensions:
COLS The number of columns of a matrix.
ROWS The number of rows of a matrix.

Sub-matrix extraction:
DIAG Extracts the diagonal of a matrix to a column vector.
DIAGRV Puts a column vector into the diagonal of a matrix.
SELIF Select rows of a matrix that verify some logical
condition.
TRIMR Eliminates rows at the top and/or bottom of a matrix.

Mathematical functions:
ABS Absolute value.
INT Round to the nearest integer.
EXP Exponential function.
LN Natural logarithm.
LOG Logarithm of base 10.
SQRT Square-root.
SIN Sine.

Cumulative distribution functions


CDFBVN Computes the cdf of a bivariate normal distribution.
CDFCHIC Computes the complement of the cdf of a chi-squared.
CDFFC Computes the complement of the cdf of the F distrib.
CDFN Cdf of a normal distribution.
CDFNI Inverse cdf of a normal distribution.
CDFTC Complement of the cdf of the Student’s t distribution.
PDFN Normal pdf.

Descriptive statistics:
CORRX Correlation matrix of the columns of a matrix.
MEANC Mean of the elements in each column of a matrix.
STDC Standard deviation of each column of a matrix.
VCX Variance-covariance matrix.

Differentiation and integration:


GRADP Computes the first derivatives of a function.
HESSP Computes the second derivatives of a function.
INTQUAD1 Integrates a function in R.

There are many other functions allowing for example to compute eigenvalues and
eigenvectors, several matrix decompositions or to take care of missing values.

6
3.2. Example: least-squares estimation
Program - ex04.gss
/* READING THE DATA */
LET Y[8,1] = 3.1 7.6 10.4 11.2 9.0 17.9 15.7 9.3;
LET X1[8,1] = 1.2 3.1 2.5 5.2 3.9 7.5 6.7 1.2;

/* T = NUMBER OF OBSERVATIONS */
T = ROWS(Y);

/* CREATE MATRIX X BY CONCATENATING A COLUMN FOR THE CONSTANT TERM */


X = ONES(T,1)~X1;

/* SHOW THE MATRICES X AND Y */


"X ="; X;
"Y ="; Y;

/* K = NUMBER OF PARAMETERS TO ESTIMATE */


K = COLS(X);

/* LEAST SQUARES ESTIMATOR */


B = INV(X'X)*X'Y;
"B ="; B;

/* RESIDUALS */
EHAT = Y - X*B;
"EHAT ="; EHAT;

/* CHECKING IF THE SUM OF THE RESIDUALS EQUALS ZERO */


"SUM OF RESIDUALS =";; SUMC(EHAT);

/* COMPUTING THE SQUARED RESIDUALS */


EHAT2 = EHAT.*EHAT;
"EHAT2 ="; EHAT2;

/* THE SUM OF THE SQUARED RESIDUALS */


RSS = SUMC(EHAT2);
"RSS =";; RSS;

/* ESTIMATOR OF THE ERROR VARIANCE */


S2 = RSS./(T-K);
"S2 =";; S2;

/* THE ESTIMATED B VARIANCE-COVARIANCE ESTIMATOR */


VCOVB = S2*INV(X'X);
"VCOVB =";; VCOVB;

/* EXTRACT THE DIAGONAL TO OBTAIN THE ESTIMATED VARIANCES */


VARB = DIAG(VCOVB);
"VARB =";; VARB;

/* COMPUTING THE STANDARD ERRORS*/


SEB = SQRT(VARB);
"SEB =";; SEB;

/* T-STATISTICS */
TSTATB = B ./ SEB;
"TSTATB =";; TSTATB;

/* P-VALUES */
PVALUES = 2*CDFTC(TSTATB,T-K);
"PVALUES =";; PVALUES;

7
Output:
X =
1.0000000 1.2000000
1.0000000 3.1000000
1.0000000 2.5000000
1.0000000 5.2000000
1.0000000 3.9000000
1.0000000 7.5000000
1.0000000 6.7000000
1.0000000 1.2000000
Y =
3.1000000
7.6000000
10.400000
11.200000
9.0000000
17.900000
15.700000
9.3000000
B =
3.8391681
1.7088388
EHAT =
-2.7897747
-1.5365685
2.2887348
-1.5251300
-1.5036395
1.2445407
0.41161179
3.4102253
SUM OF RESIDUALS = -1.5099033e-014
EHAT2 =
7.7828429
2.3610426
5.2383071
2.3260215
2.2609318
1.5488816
0.16942426
11.629637
RSS = 33.317088
S2 = 5.5528481
VCOVB =
2.8368781 -0.54767337
-0.54767337 0.13998041
VARB =
2.8368781
0.13998041
SEB =
1.6843034
0.37413957
TSTATB =
2.2793803
4.5673833
PVALUES =
0.062853166
0.0038210152

It is possible to modify the above program so that the output could be stored in a file:
OUTPUT FILE = file name RESET;
...
(commands)
...
OUTPUT OFF;
END;

8
4. Graphics

4.1. Some commands to produce graphics in GAUSS:

xy X,Y graph
bar Bar graph
hist Histogram
xyz X, Y, Z graph
surface 3D surface graph
contour countour graph

It is possible to save GAUSS graphs in several formats. There is a program called


c:\gauss\playw.exe that converts GAUSS graphic formats to other formats recognized
in Word.

4.2. Example – A graph of the X1 and Y series from the least squares example.

Program - ex-04b.gss
LET Y[8,1] = 3.1 7.6 10.4 11.2 9.0 17.9 15.7 9.3;
LET X1[8,1] = 1.2 3.1 2.5 5.2 3.9 7.5 6.7 1.2;

LIBRARY PGRAPH;
GRAPHSET;

/* Y graph */
XY(1,Y);

/* X1 graph */
XY(1,X1);

/* X1 and Y graph */
XY(1,X1~Y);

/* X1-Y graph */
/* The following option turns off the option to connect points with
lines: */
_PLCTRL = -1;
XY(X1,Y);

/* To save this grpah in a file that can be included in Word it is


necessary to change a few control variables. A complete description
can be found in the GAUSS manual. */
/* White background */
_PMCOLOR = {0,0,0,0,0,0,0,0,15} ;
/* Blue symbols: */
_PCOLOR = 1;
/* Graph filename: */
_PTEK = "C:\\GAUSS\\EXAMPLES\\CURSO\\EX-04B.TKF" ;
/* This graph must be converted to the metafile format
(using the c:\gauss\playw.exe program)
in order to be used in a Word document. */
/* Titles: */
TITLE("X1-Y GRAPH");
XLABEL("X1");
YLABEL("Y");
/* Finally the graph: */
XY(X1,Y);

9
One of the graphs:

10
5. Conditional branching with IF

The IF command conditionally executes a series of commands depending on the result


of a logical condition.

Example:
IF A < 0;
"A IS NEGATIVE";
B = 2*A;
ELSEIF A == 0;
"A EQUALS ZERO";
B = 0;
ELSE;
"A IS POSITIVE";
B = A^2;
ENDIF;

The expression after the IF should always result in a scalar value. The expression is
TRUE if the value is different from zero, and FALSE if it equals zero.

The ELSE command is optional and can be used only once in the IF instruction. The
ELSEIF is also optional but can be used several times.

Examples of relational operators that take the value 0 (FALSE) or 1 (TRUE):


== Equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
/= Not equal

When comparing matrices, the result only equals 1 (TRUE) if all the comparisons
result in 1 (TRUE).

11
6. Random numbers

6.1. Pseudo-random number generators:


RNDN matrix of N(0,1)random numbers.
RNDU matrix of uniformly distributed over [0,1] random
numbers.

6.2. Examples:

(a) Generate a sample of ten N(0,1) random numbers.


Program: ex-05a.gss
T = 10;
Z = RNDN(T,1);
"Z ="; Z;

(b) Generate 10000 random numbers N(0,1) and N(µ,σ2) with µ = 10, σ = 5.
Program: ex-05b.gss
T = 10000;
Z = RNDN(T,1);

/* INITIALIZE GRAPHS */
LIBRARY PGRAPH;
GRAPHSET;
/* HISTOGRAM OF THE Z VALUES WITH 30 CATEGORIES*/
CALL HIST(Z,30);

/* GENERATE: X ~ N( MU, SIGMA^2 ) */


MU = 10;
SIGMA = 5;
X = MU + SIGMA*Z;

/* HISTOGRAM OF THE VALUES IN X */


CALL HIST(X,30);

The graphs:

12
(c) Generate 500 random numbers with a Cauchy distribution.

Program: ex-05c.gss
/* GENERATE T RANDOM NUMBERS WITH A CAUCHY DISTRIBUTION */
T=500;
/* THE CAUCHY CAN BE OBTAINED AS THE RATIO OF TWO NORMALS */
CAUCHY=RNDN(T,1)./RNDN(T,1);
/* FREQUENCY HISTOGRAM */
LIBRARY PGRAPH;
GRAPHSET;
CALL HISTP(CAUCHY,100);

Graph:

13
7. Loops

7.1. DO command

The DO command performs loops, or cycles, of instructions. The format is the


following:
DO WHILE scalar expression;
...
(Instructions)
...
ENDO;

The set of instructions between DO WHILE and ENDO is executed while the scalar
expression is true VERDADE. See the command IF for a discussion of relational
operators.

Examples of loops with the DO command (ex-06a.gss):


/* SIMPLE EXAMPLE OF A LOOP WITH THE DO COMMAND */
I = 1;
DO WHILE I <= 8;
"I = ";; I;
I = I + 1;
ENDO;

/* ANOTHER EXAMPLE*/
M = ZEROS(13,1);
I = 1;
DO WHILE I <= 13;
M[I,1] = I^2;
I = I + 1;
ENDO;
"M =";
M;

Output:
I = 1.0000000
I = 2.0000000
I = 3.0000000
I = 4.0000000
I = 5.0000000
I = 6.0000000
I = 7.0000000
I = 8.0000000
M =
1.0000000
4.0000000
9.0000000
16.000000
25.000000
36.000000
49.000000
64.000000
81.000000
100.00000
121.00000
144.00000
169.00000

14
7.2. Alternatives to the DO command

Sometimes the are more efficient alternatives to the DO command.

Some matrix operators may be used to compute the matrix of results of all element-
by-element comparisons:

.== Equal to
.< Less than
.<= Less than or equal to
.> Greater than
.>= Greater than or equal to
./= Not equal to

Since the result of these operators is not scalar, they cannot be used in IF or DO
commands. However, they may be very useful in other circumstances as shown in the
following example.

Example (ex-05.gss): Consider 100000 N(0,1) random numbers generated by:

Z = RNDN(100000,1);

To count the number of negative values on this vector, the following program could
be used:

NUM = 0;
I = 1;
DO WHILE I <= ROWS(Z);
IF Z[I] < 0;
NUM = NUM + 1;
ENDIF;
I = I + 1;
ENDO;
"Number of negative values =";; NUM;

An alternative simpler and much faster program:

NUM = SUMC( Z .< 0 );


"Number of negative values =";; NUM;

15
8. Simulation

(a) Computing the sample means of 10000 random samples of size 10 generated from
a N(10,5^2) distribution.

Program – ex-06b.gss
/* SAVE EXECUTION START TIME */
TEMPO=HSEC;
/* SAMPLE SIZE */
T = 10;
/* POPULATION MEAN */
MU = 10;
/* POPULATION STANDARD DEVIATION */
SIGMA = 5;

/* NÚMERO OF SAMPLES TO GENERATE */


NSIMUL = 10000;
/* CREATE A MATRIX WHERE TO STORE THE VALUES OF THE SAMPLES MEAN FOR EACH
RANDOM SAMPLE */
MEDIAS = ZEROS(NSIMUL,1);

/* START THE "LOOP" GENERATING THE RANDOM SAMPLES */


SIMUL=1;
DO WHILE SIMUL <= NSIMUL;
/* EACH SAMPLE HAS T OBSERVATIONS */
Z = RNDN(T,1);
X = MU + SIGMA * Z;
/* COMPUTE THE SAMPLE MEAN AND SAVE THE VALUE IN ROW SIMUL
OF THE MEDIAS MATRIX */
MEDIAS[SIMUL,1] = MEANC(X);

SIMUL = SIMUL + 1;
ENDO;
/* AT THE END OF THE LOOP THE MATRIX MEDIAS HAS ALL THE NSIMUL MEANS STORED
IN IT */

"THE POPULATION MEAN IS: MU = ";; MU;


"THE MINIMUM SAMPLE MEAN WAS"
MINC(MEDIAS);
"THE MAXIMUM SAMPLE MEAN WAS"
MAXC(MEDIAS);
"THE AVERAGE OF THE SAMPLE MEANS WAS"
MEANC(MEDIAS);
"THE STANDARD DEVIATION OF THE SAMPLE MEANS WAS"
STDC(MEDIAS);
"THE THEORETICAL STANDARD DEVIATION OF THE SAMPLE MEANS = SIGMA/SQRT(T) ="
SIGMA/SQRT(T);

/* CONTAR O TEMPO QUE LEVOU ESTE PROGRAMA A SER EXECUTADO */


TEMPO=(HSEC-TEMPO)/100;
"TOTAL TIME=";;TEMPO;;"SECONDS";
Output:
THE POPULATION MEAN IS: MU = 10.000000
THE MINIMUM SAMPLE MEAN WAS
3.0688746
THE MAXIMUM SAMPLE MEAN WAS
16.943361
THE AVERAGE OF THE SAMPLE MEANS WAS 9.9923756
THE STANDARD DEVIATION OF THE SAMPLE MEANS WAS 1.5753960
THE THEORETICAL STANDARD DEVIATION OF THE SAMPLE MEANS = SIGMA/SQRT(T) =
1.5811388
TOTAL TIME= 3.3500000 SECONDS

16
(b) The same computations without loops:

Program – ex-07.gss
TEMPO=HSEC;

T = 10;
MU = 10;
SIGMA = 5;
NSIMUL = 10000;

/* GENERATE NSIMUL RANDOM SAMPLES OF SIZE T */


/* EACH SAMPLE IS A COLUMN */
Z = RNDN(T,NSIMUL);
X = MU + SIGMA * Z;
/* MEANC COMPUTES THE MEAN OF EACH COLUMN */
/* MEDIAS IS THE VECTOR CONTAINING THE NSIMUL SAMPLE MEANS */
MEDIAS = MEANC(X);

"POPULATION MEAN: MU = ";; MU;


"MINIMUM SAMPLE MEAN:"
MINC(MEDIAS);
"MAXIMUM SAMPLE MEAN:"
MAXC(MEDIAS);
"AVERAGE SAMPLE MEAN:"
MEANC(MEDIAS);
"STANDARD DEVIATION OF THE SAMPLE MEANS:"
STDC(MEDIAS);
"THEORETICAL STANDARD DEVIATION OF THE SAMPLE MEANS = SIGMA/SQRT(T)
="
SIGMA/SQRT(T);

TEMPO=(HSEC-TEMPO)/100;
"TOTAL TIME=";;TEMPO;;"SECONDS";

Output:
POPULATION MEAN: MU = 10.000000
MINIMUM SAMPLE MEAN:
4.3308605
MAXIMUM SAMPLE MEAN:
15.875061
AVERAGE SAMPLE MEAN:
9.9888622
STANDARD DEVIATION OF THE SAMPLE MEANS:
1.5977483
THEORETICAL STANDARD DEVIATION OF THE SAMPLE MEANS = SIGMA/SQRT(T) =
1.5811388
TOTAL TIME= 3.3500000 SECONDS

17
(c) Study of the distribution of the t-statistic when observations follow a normal
distribution.

Program – ex-08.gss
/* SIMULATION OF THE T-STATISTIC FOR RANDOM SAMPLES WITH NORMALLY
DISTRIBUTED OBSERVATIONS */
T = 10;
MU = 10;
SIGMA = 5;
NSIMUL = 10000;

Z = RNDN(T,NSIMUL);
X = MU + SIGMA * Z;
MEDIAS = MEANC(X);
DP = STDC(X);
/* T-STATISTIC */
ESTT = (MEDIAS - MU) ./ (DP ./ SQRT(T) );

/* HISTOGRAM OF THE T-STATISTICS FOR THE NSIMUL RANDOM SAMPLES */


LIBRARY PGRAPH;
GRAPHSET;
CALL HIST(ESTT,30);

"PERCENTILES 2.5% AND 97.5% OF THE SIMULATED VALUES "


QUANTILE(ESTT, 0.025|0.975 );
"PERCENTILES 2.5% AND 97.5% OF THE STUDENT-T WITH T-1 D.F."
CDFTCI( 1-(0.025|0.975) , T-1 );

Output:
PERCENTILES 2.5% AND 97.5% OF THE SIMULATED VALUES
-2.2881654
2.2547969
PERCENTILES 2.5% AND 97.5% OF THE STUDENT-T WITH T-1 D.F.
-2.2621572
2.2621572

18
(d) Distribution of the t-statistic when observations follow a Cauchy distribution.

Program – ex-09.gss
NEW;
/* SIMULATION OF THE T-STATISTIC WHEN OBSERVATIONS FOLLOW A CAUCHY DISTR. */
T = 10;
MU = 0;
SIGMA = 1;
NSIMUL = 5000;

/* GENERATE THE CAUCHY OBSERVATIONS (FROM 2 NORMALS) */


Z1 = RNDN(T,NSIMUL);
Z2 = RNDN(T,NSIMUL);
CAUCHY = Z1 ./ Z2;
X = MU + SIGMA * CAUCHY;
MEDIAS = MEANC(X);
DP = STDC(X);
/* T-STATISTIC */
ESTT = (MEDIAS - MU) ./ (DP ./ SQRT(T) );

/* HISTOGRAM OF THE T-STATISTICS FOR THE NSIMUL RANDOM SAMPLES */


LIBRARY PGRAPH;
GRAPHSET;
CALL HIST(ESTT,30);

"PERCENTILES 2.5% AND 97.5% OF THE SIMULATED VALUES "


QUANTILE(ESTT, 0.025|0.975 );
"PERCENTILES 2.5% AND 97.5% OF THE STUDENT-T WITH T-1 D.F."
CDFTCI( 1-(0.025|0.975) , T-1 );

Output:
PERCENTILES 2.5% AND 97.5% OF THE SIMULATED VALUES
-1.8825505
1.8735276
PERCENTILES 2.5% AND 97.5% OF THE STUDENT-T WITH T-1 D.F.
-2.2621572
2.2621572

19
9. Procedures
The commands to create a new procedure are the following:

proc Definition of the procedure.


local Declare variables local to the procedure.
retp Output of the procedure.
endp End of the procedure definition.

The only mandatory commands are proc and endp. If the command retp is not used
then the procedure will not return anything when called.

Example: Program ex-10.gss.


/* PROCEDURE CALLED MINQ */
/* INPUTS: X , Y */
/* THERE WILL BE TWO OUTPUTS AS DEFINED BELOW*/
PROC (2) = MINQ( X , Y );
/* LOCAL VARIABLES USED INSIDE THE PROCEDURE */
LOCAL T, K, B, EHAT, EHAT2, RSS, S2, VCOVB;
/* SET OF INSTRUCTIONS */
T = ROWS(Y);
K = COLS(X);
B = INV(X'X)*X'Y;
EHAT = Y - X*B;
EHAT2 = EHAT.*EHAT;
RSS = SUMC(EHAT2);
S2 = RSS./(T-K);
VCOVB = S2*INV(X'X);
/* THE TWO OUTPUTS OF THIS PROCEDURE ARE B, VCOVB (THESE ARE
THE LEAST-SQUARES ESTIMATOR AND CORREPONDING ESTIMATED
VARIANCE-COVARIANCE MATRIX) */
RETP( B , VCOVB );
/* END OF PROCEDURE DEFINITION */
ENDP;

/* MAIN PROGRAM */
/* READ DATA */
LET Y[8,1] = 3.1 7.6 10.4 11.2 9.0 17.9 15.7 9.3;
LET X1[8,1] = 1.2 3.1 2.5 5.2 3.9 7.5 6.7 1.2;
T = ROWS(Y);
X = ONES(T,1)~X1;

/* CALL THE PROCEDURE AND STORE THE RESULTS IN BHAT AND V */


{BHAT,V} = MINQ(X,Y);

/* SHOW THE RESULTS */


"BHAT ="; BHAT;
"V ="; V;

Output:
BHAT =

3.8391681
1.7088388
V =

2.8368781 -0.54767337
-0.54767337 0.13998041

20
10. Maximum likelihood estimation – MAXLIK

The library MAXLIK in GAUSS contains a set of More detailed information can be
found in the corresponding manual.

Example: Programa ex-ml1.gss.


/* EXAMPLE USING MAXLIK:
ESTIMATION OF THE NORMAL LINEAR REGRESSION MODEL BY MAXIMUM
LIKELIHOOD.
*/
NEW;
/* CALL THE MAXLIK LIBRARY*/
LIBRARY MAXLIK;
#INCLUDE MAXLIK.EXT;
MAXSET;

/* DEFINE THE LOG-LIKELIHOOD FUNCTION.


THE PROCEDURE ACCEPTS HAS INPUT:
1º A COLUMN VECTOR CONTAINING THE PARAMETERS TO ESTIMATE,
2º THE DATA MATRIX.
THE OUTPUT IS A VECTOR WITH THE LOG-LIKELIHOOD FOR EACH
OBSERVATION*/
PROC MYLLF( PARAM , DADOS );
LOCAL Y, X1, BETA1, BETA2, S, E, F;

Y = DADOS[.,1];
X1 = DADOS[.,2];

BETA1 = PARAM[1,1];
BETA2 = PARAM[2,1];
S = PARAM[3,1];

E = Y - BETA1 - BETA2 .* X1;


/* COMPUTE LOG-LIKELIHOOD FOR EACH OBSERVATION */
F = -0.5 * LN( 2*PI ) -0.5 * LN( S^2) -0.5*(E^2)./(S^2);

RETP(F);
ENDP;

/* READ DATA */
LET Y[8,1] = 3.1 7.6 10.4 11.2 9.0 17.9 15.7 9.3;
LET X1[8,1] = 1.2 3.1 2.5 5.2 3.9 7.5 6.7 1.2;
/* ALL DATA TO BE USED ARE STORED IN ONE MATRIX */
DADOS = Y ~ X1;

/* NAMING THE PARAMETERS */


LET _MAX_PARNAMES = B1 B2 S;
/* STARTING VALUES FOR THE NUMERICAL OPTIMIZATION */
LET PARAMINI = 0 0 1;

/* COMMANDS TO CALL THE ESTIMATION PROCEDURES */


{PARAMFIN,FFIN,GRADFIN,COV,RETCODE}=MAXLIK(DADOS,0,&MYLLF,PARAMINI);
CALL MAXPRT(PARAMFIN, FFIN, GRADFIN, COV, RETCODE);

21
Output:

================================================================================

iteration: 1
algorithm: BFGS step method: STEPBT
function: 65.62894 step length: 0.00000 backsteps: 0
--------------------------------------------------------------------------------
param. param. value relative grad.
B1 0.0000 0.1604
B2 0.0000 0.7566
S 1.0000 1.9568

================================================================================

iteration: 2
algorithm: BFGS step method: STEPBT
function: 5.63411 step length: 1.00000 backsteps: 0
--------------------------------------------------------------------------------
param. param. value relative grad.
B1 0.1604 0.1503
B2 0.7566 0.6840
S 2.9568 1.1115

================================================================================

... (ETC) ...

================================================================================

iteration: 28
algorithm: BFGS step method: STEPBT
function: 2.13225 step length: 1.00000 backsteps: 0
--------------------------------------------------------------------------------
param. param. value relative grad.
B1 3.8404 0.0007
B2 1.7090 0.0014
S 2.0408 0.0000

===============================================================================
MAXLIK Version 4.0.22 9/21/99 12:03 pm
===============================================================================

return code = 0
normal convergence

Mean log-likelihood -2.13225


Number of cases 8

Covariance matrix of the parameters computed by the following method:


Inverse of computed Hessian

Parameters Estimates Std. err. Est./s.e. Prob. Gradient


------------------------------------------------------------------
B1 3.8392 1.4586 2.632 0.0042 0.0000
B2 1.7088 0.3240 5.274 0.0000 0.0000
S 2.0407 0.5102 4.000 0.0000 -0.0000

Correlation matrix of the parameters


1.000 -0.869 -0.000
-0.869 1.000 0.000
-0.000 0.000 1.000

Number of iterations 28
Minutes to convergence 0.01017

22

Você também pode gostar