example.cpp
Go to the documentation of this file.
1 
9 
10 
11 #define WANT_STREAM // include.h will get stream fns
12 #define WANT_MATH // include.h will get math fns
13  // newmatap.h will get include.h
14 
15 #include "newmatap.h" // need matrix applications
16 
17 #include "newmatio.h" // need matrix output routines
18 
19 #ifdef use_namespace
20 using namespace NEWMAT; // access NEWMAT namespace
21 #endif
22 
23 
24 // demonstration of matrix package on linear regression problem
25 
26 
27 void test1(Real* y, Real* x1, Real* x2, int nobs, int npred)
28 {
29  cout << "\n\nTest 1 - traditional, bad\n";
30 
31  // traditional sum of squares and products method of calculation
32  // but not adjusting means; maybe subject to round-off error
33 
34  // make matrix of predictor values with 1s into col 1 of matrix
35  int npred1 = npred+1; // number of cols including col of ones.
36  Matrix X(nobs,npred1);
37  X.column(1) = 1.0;
38 
39  // load x1 and x2 into X
40  // [use << rather than = when loading arrays]
41  X.column(2) << x1; X.column(3) << x2;
42 
43  // vector of Y values
44  ColumnVector Y(nobs); Y << y;
45 
46  // form sum of squares and product matrix
47  // [use << rather than = for copying Matrix into SymmetricMatrix]
48  SymmetricMatrix SSQ; SSQ << X.t() * X;
49 
50  // calculate estimate
51  // [bracket last two terms to force this multiplication first]
52  // [ .i() means inverse, but inverse is not explicity calculated]
53  ColumnVector A = SSQ.i() * (X.t() * Y);
54 
55  // Get variances of estimates from diagonal elements of inverse of SSQ
56  // get inverse of SSQ - we need it for finding D
57  DiagonalMatrix D; D << SSQ.i();
58  ColumnVector V = D.as_column();
59 
60  // Calculate fitted values and residuals
61  ColumnVector Fitted = X * A;
62  ColumnVector Residual = Y - Fitted;
63  Real ResVar = sum_square(Residual) / (nobs-npred1);
64 
65  // Get diagonals of Hat matrix (an expensive way of doing this)
66  DiagonalMatrix Hat; Hat << X * (X.t() * X).i() * X.t();
67 
68  // print out answers
69  cout << "\nEstimates and their standard errors\n\n";
70 
71  // make vector of standard errors
72  ColumnVector SE(npred1);
73  for (int i=1; i<=npred1; i++) SE(i) = sqrt(V(i)*ResVar);
74  // use concatenation function to form matrix and use matrix print
75  // to get two columns
76  cout << setw(11) << setprecision(5) << (A | SE) << endl;
77 
78  cout << "\nObservations, fitted value, residual value, hat value\n";
79 
80  // use concatenation again; select only columns 2 to 3 of X
81  cout << setw(9) << setprecision(3) <<
82  (X.columns(2,3) | Y | Fitted | Residual | Hat.as_column());
83  cout << "\n\n";
84 }
85 
86 void test2(Real* y, Real* x1, Real* x2, int nobs, int npred)
87 {
88  cout << "\n\nTest 2 - traditional, OK\n";
89 
90  // traditional sum of squares and products method of calculation
91  // with subtraction of means - less subject to round-off error
92  // than test1
93 
94  // make matrix of predictor values
95  Matrix X(nobs,npred);
96 
97  // load x1 and x2 into X
98  // [use << rather than = when loading arrays]
99  X.column(1) << x1; X.column(2) << x2;
100 
101  // vector of Y values
102  ColumnVector Y(nobs); Y << y;
103 
104  // make vector of 1s
105  ColumnVector Ones(nobs); Ones = 1.0;
106 
107  // calculate means (averages) of x1 and x2 [ .t() takes transpose]
108  RowVector M = X.sum_columns() / nobs;
109 
110  // and subtract means from x1 and x2
111  Matrix XC(nobs,npred);
112  XC = X - Ones * M;
113 
114  // do the same to Y [use sum to get sum of elements]
115  ColumnVector YC(nobs);
116  Real m = sum(Y) / nobs; YC = Y - Ones * m;
117 
118  // form sum of squares and product matrix
119  // [use << rather than = for copying Matrix into SymmetricMatrix]
120  SymmetricMatrix SSQ; SSQ << XC.t() * XC;
121 
122  // calculate estimate
123  // [bracket last two terms to force this multiplication first]
124  // [ .i() means inverse, but inverse is not explicity calculated]
125  ColumnVector A = SSQ.i() * (XC.t() * YC);
126 
127  // calculate estimate of constant term
128  // [AsScalar converts 1x1 matrix to Real]
129  Real a = m - (M * A).as_scalar();
130 
131  // Get variances of estimates from diagonal elements of inverse of SSQ
132  // [ we are taking inverse of SSQ - we need it for finding D ]
133  Matrix ISSQ = SSQ.i(); DiagonalMatrix D; D << ISSQ;
134  ColumnVector V = D.AsColumn();
135  Real v = 1.0/nobs + (M * ISSQ * M.t()).as_scalar();
136  // for calc variance of const
137 
138  // Calculate fitted values and residuals
139  int npred1 = npred+1;
140  ColumnVector Fitted = X * A + a;
141  ColumnVector Residual = Y - Fitted;
142  Real ResVar = sum_square(Residual) / (nobs-npred1);
143 
144  // Get diagonals of Hat matrix (an expensive way of doing this)
145  Matrix X1(nobs,npred1); X1.column(1)<<Ones; X1.columns(2,npred1)<<X;
146  DiagonalMatrix Hat; Hat << X1 * (X1.t() * X1).i() * X1.t();
147 
148  // print out answers
149  cout << "\nEstimates and their standard errors\n\n";
150  cout.setf(ios::fixed, ios::floatfield);
151  cout << setw(11) << setprecision(5) << a << " ";
152  cout << setw(11) << setprecision(5) << sqrt(v*ResVar) << endl;
153  // make vector of standard errors
154  ColumnVector SE(npred);
155  for (int i=1; i<=npred; i++) SE(i) = sqrt(V(i)*ResVar);
156  // use concatenation function to form matrix and use matrix print
157  // to get two columns
158  cout << setw(11) << setprecision(5) << (A | SE) << endl;
159  cout << "\nObservations, fitted value, residual value, hat value\n";
160  cout << setw(9) << setprecision(3) <<
161  (X | Y | Fitted | Residual | Hat.as_column());
162  cout << "\n\n";
163 }
164 
165 void test3(Real* y, Real* x1, Real* x2, int nobs, int npred)
166 {
167  cout << "\n\nTest 3 - Cholesky\n";
168 
169  // traditional sum of squares and products method of calculation
170  // with subtraction of means - using Cholesky decomposition
171 
172  Matrix X(nobs,npred);
173  X.column(1) << x1; X.column(2) << x2;
174  ColumnVector Y(nobs); Y << y;
175  RowVector M = X.sum_columns() / nobs;
176  Matrix XC(nobs,npred);
177  ColumnVector Ones(nobs); Ones = 1.0;
178  XC = X - Ones * M;
179  ColumnVector YC(nobs);
180  Real m = sum(Y) / nobs; YC = Y - Ones * m;
181  SymmetricMatrix SSQ; SSQ << XC.t() * XC;
182 
183  // Cholesky decomposition of SSQ
185 
186  // calculate estimate
187  ColumnVector A = L.t().i() * (L.i() * (XC.t() * YC));
188 
189  // calculate estimate of constant term
190  Real a = m - (M * A).AsScalar();
191 
192  // Get variances of estimates from diagonal elements of invoice of SSQ
193  DiagonalMatrix D; D << L.t().i() * L.i();
194  ColumnVector V = D.as_column();
195  Real v = 1.0/nobs + sum_square(L.i() * M.t());
196 
197  // Calculate fitted values and residuals
198  int npred1 = npred+1;
199  ColumnVector Fitted = X * A + a;
200  ColumnVector Residual = Y - Fitted;
201  Real ResVar = sum_square(Residual) / (nobs-npred1);
202 
203  // Get diagonals of Hat matrix (an expensive way of doing this)
204  Matrix X1(nobs,npred1); X1.column(1)<<Ones; X1.columns(2,npred1)<<X;
205  DiagonalMatrix Hat; Hat << X1 * (X1.t() * X1).i() * X1.t();
206 
207  // print out answers
208  cout << "\nEstimates and their standard errors\n\n";
209  cout.setf(ios::fixed, ios::floatfield);
210  cout << setw(11) << setprecision(5) << a << " ";
211  cout << setw(11) << setprecision(5) << sqrt(v*ResVar) << endl;
212  ColumnVector SE(npred);
213  for (int i=1; i<=npred; i++) SE(i) = sqrt(V(i)*ResVar);
214  cout << setw(11) << setprecision(5) << (A | SE) << endl;
215  cout << "\nObservations, fitted value, residual value, hat value\n";
216  cout << setw(9) << setprecision(3) <<
217  (X | Y | Fitted | Residual | Hat.as_column());
218  cout << "\n\n";
219 }
220 
221 void test4(Real* y, Real* x1, Real* x2, int nobs, int npred)
222 {
223  cout << "\n\nTest 4 - QR triangularisation\n";
224 
225  // QR triangularisation method
226 
227  // load data - 1s into col 1 of matrix
228  int npred1 = npred+1;
229  Matrix X(nobs,npred1); ColumnVector Y(nobs);
230  X.column(1) = 1.0; X.column(2) << x1; X.column(3) << x2; Y << y;
231 
232  // do Householder triangularisation
233  // no need to deal with constant term separately
234  Matrix X1 = X; // Want copy of matrix
235  ColumnVector Y1 = Y;
237  QRZ(X1, U); QRZ(X1, Y1, M); // Y1 now contains resids
238  ColumnVector A = U.i() * M;
239  ColumnVector Fitted = X * A;
240  Real ResVar = sum_square(Y1) / (nobs-npred1);
241 
242  // get variances of estimates
243  U = U.i(); DiagonalMatrix D; D << U * U.t();
244 
245  // Get diagonals of Hat matrix
246  DiagonalMatrix Hat; Hat << X1 * X1.t();
247 
248  // print out answers
249  cout << "\nEstimates and their standard errors\n\n";
250  ColumnVector SE(npred1);
251  for (int i=1; i<=npred1; i++) SE(i) = sqrt(D(i)*ResVar);
252  cout << setw(11) << setprecision(5) << (A | SE) << endl;
253  cout << "\nObservations, fitted value, residual value, hat value\n";
254  cout << setw(9) << setprecision(3) <<
255  (X.columns(2,3) | Y | Fitted | Y1 | Hat.as_column());
256  cout << "\n\n";
257 }
258 
259 void test5(Real* y, Real* x1, Real* x2, int nobs, int npred)
260 {
261  cout << "\n\nTest 5 - singular value\n";
262 
263  // Singular value decomposition method
264 
265  // load data - 1s into col 1 of matrix
266  int npred1 = npred+1;
267  Matrix X(nobs,npred1); ColumnVector Y(nobs);
268  X.column(1) = 1.0; X.column(2) << x1; X.column(3) << x2; Y << y;
269 
270  // do SVD
271  Matrix U, V; DiagonalMatrix D;
272  SVD(X,D,U,V); // X = U * D * V.t()
273  ColumnVector Fitted = U.t() * Y;
274  ColumnVector A = V * ( D.i() * Fitted );
275  Fitted = U * Fitted;
276  ColumnVector Residual = Y - Fitted;
277  Real ResVar = sum_square(Residual) / (nobs-npred1);
278 
279  // get variances of estimates
280  D << V * (D * D).i() * V.t();
281 
282  // Get diagonals of Hat matrix
283  DiagonalMatrix Hat; Hat << U * U.t();
284 
285  // print out answers
286  cout << "\nEstimates and their standard errors\n\n";
287  ColumnVector SE(npred1);
288  for (int i=1; i<=npred1; i++) SE(i) = sqrt(D(i)*ResVar);
289  cout << setw(11) << setprecision(5) << (A | SE) << endl;
290  cout << "\nObservations, fitted value, residual value, hat value\n";
291  cout << setw(9) << setprecision(3) <<
292  (X.columns(2,3) | Y | Fitted | Residual | Hat.as_column());
293  cout << "\n\n";
294 }
295 
296 int main()
297 {
298  cout << "\nDemonstration of Matrix package\n";
299  cout << "\nPrint a real number (may help lost memory test): " << 3.14159265 << "\n";
300 
301  // Test for any memory not deallocated after running this program
302  Real* s1; { ColumnVector A(8000); s1 = A.data(); }
303 
304  {
305  // the data
306 
307  Real y[] = { 8.3, 5.5, 8.0, 8.5, 5.7, 4.4, 6.3, 7.9, 9.1 };
308  Real x1[] = { 2.4, 1.8, 2.4, 3.0, 2.0, 1.2, 2.0, 2.7, 3.6 };
309  Real x2[] = { 1.7, 0.9, 1.6, 1.9, 0.5, 0.6, 1.1, 1.0, 0.5 };
310 
311 
312  int nobs = 9; // number of observations
313  int npred = 2; // number of predictor values
314 
315  // we want to find the values of a,a1,a2 to give the best
316  // fit of y[i] with a0 + a1*x1[i] + a2*x2[i]
317  // Also print diagonal elements of hat matrix, X*(X.t()*X).i()*X.t()
318 
319  // this example demonstrates five methods of calculation
320 
321  Try
322  {
323  test1(y, x1, x2, nobs, npred);
324  test2(y, x1, x2, nobs, npred);
325  test3(y, x1, x2, nobs, npred);
326  test4(y, x1, x2, nobs, npred);
327  test5(y, x1, x2, nobs, npred);
328  }
329  CatchAll { cout << BaseException::what(); }
330  }
331 
332 #ifdef DO_FREE_CHECK
333  FreeCheck::Status();
334 #endif
335  Real* s2; { ColumnVector A(8000); s2 = A.data(); }
336  cout << "\n\nThe following test does not work with all compilers - see documentation\n";
337  cout << "Checking for lost memory: "
338  << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
339  if (s1 != s2) cout << " - see section 2.8\n"; else cout << " - ok\n";
340 
341  return 0;
342 
343 }
344 
345 
void test3(Real *y, Real *x1, Real *x2, int nobs, int npred)
Definition: example.cpp:165
#define Try
Definition: myexcept.h:190
GetSubMatrix columns(int, int) const
Definition: submat.cpp:77
void test1(Real *y, Real *x1, Real *x2, int nobs, int npred)
Definition: example.cpp:27
double Real
Definition: include.h:307
void test4(Real *y, Real *x1, Real *x2, int nobs, int npred)
Definition: example.cpp:221
GetSubMatrix column(int) const
Definition: submat.cpp:68
ReturnMatrix sum_columns() const
Definition: newmat8.cpp:805
#define CatchAll
Definition: myexcept.h:194
Real * data()
Definition: newmat.h:502
Upper triangular matrix.
Definition: newmat.h:799
void test2(Real *y, Real *x1, Real *x2, int nobs, int npred)
Definition: example.cpp:86
TransposedMatrix t() const
Definition: newmat6.cpp:320
void SVD(const Matrix &, DiagonalMatrix &, Matrix &, Matrix &, bool=true, bool=true)
Definition: svd.cpp:30
Real sum_square(const BaseMatrix &B)
Definition: newmat.h:2094
The usual rectangular matrix.
Definition: newmat.h:625
InvertedMatrix i() const
Definition: newmat6.cpp:329
ReturnMatrix Cholesky(const SymmetricMatrix &S)
Definition: cholesky.cpp:36
FloatVector FloatVector * a
void QRZ(Matrix &X, UpperTriangularMatrix &U)
Definition: hholder.cpp:117
static const char * what()
Definition: myexcept.h:93
Diagonal matrix.
Definition: newmat.h:896
Lower triangular matrix.
Definition: newmat.h:848
Real sum(const BaseMatrix &B)
Definition: newmat.h:2105
Row vector.
Definition: newmat.h:953
ColedMatrix AsColumn() const
Definition: newmat.h:2142
Column vector.
Definition: newmat.h:1008
int main()
Definition: example.cpp:296
ColedMatrix as_column() const
Definition: newmat6.cpp:336
void test5(Real *y, Real *x1, Real *x2, int nobs, int npred)
Definition: example.cpp:259
Symmetric matrix.
Definition: newmat.h:753


kni
Author(s): Martin Günther
autogenerated on Fri Jun 7 2019 22:06:44