new; /* erases everything in memory and closes files and windows 1 */ /* This is a Gauss batch program to illustrate basic matrix operations */ /* Load the first 10 observations from the times series used by ** Theil to model textile demand. The data represent indices of ** per-capita textile consumption, real per-capita disposable ** income, and the real price of textiles. */ load yxcol[] = matrixalg.asc; /* The next 3 lines set up an output file */ format /rd 11,5; /* right justified numbers of form xxxxxx.xxxxx */ outwidth 110; /* 110 columns in output */ output file = matrixalg.out reset; /* Overwrite any earlier version */ print on; /* Print the results of the following operations to matrixalg.out */ yx = reshape(yxcol,10,3); y = yx[.,1]; /* Copy first column of yx to vector y */ x = yx[.,2 3]; /* Copy remaining columns of yx to matrix x */ n = rows(x); /* Number of rows in matrix x */ x = ones(n,1)~x; /* Create nx3 design matrix by concatenation */ y2 = y^2; /* Square the elements of y */ y2sum = sumc(y2); /* Sum the elements of y2 */ ynorm = sqrt(y2sum); /* Calculate the norm or length of y */ u = x[.,2]; /* Copy the second column of x to vector u */ unorm = sqrt(sumc(u^2)); /* Calculate the norm of u in one step */ z = x[.,1] + x[.,2]; /* Add the first two columns of x */ w = 2*x[.,1]; /* Scalar multiplication of a vector */ ip = y'u; /* Calculate the inner or dot product of y and u */ cosyu = ip/(ynorm*unorm); /* Cosine of the angle between y and u */ angleyu = arccos(cosyu); /* Angle between y and u, in degrees */ rankx = rank(x); /* Compute rank of x by singular value decomposition */; xtx = x'x; /* Multiply x' by x */ detxtx = det(xtx); /* Get determinant of x'x */ xtxi = invpd(xtx); /* Get inverse of positive definite x'x */ bols = xtxi*x'y; /* OLS estimate of regression coefficients */ resid = y - x*bols; /* OLS residual */ xr = x'resid; /* Check orthogonality of regressors and residuals */ end; /* Terminate program, revert to command mode, close open files, ** turn screen on */