void NonLinearLeastSquares::MakeCovariance() { if (Covariance.Nrows()==0) { UpperTriangularMatrix UI = U.i(); Covariance << UI * UI.t() * errorvar; SE << Covariance; // get diagonals for (int i = 1; i<=n_param; i++) SE(i) = sqrt(SE(i)); } }
// produces the Cholesky decomposition of A - x.t() * x where A = chol.t() * chol void downdate_Cholesky(UpperTriangularMatrix &chol, RowVector x) { int nRC = chol.Nrows(); // solve R^T a = x LowerTriangularMatrix L = chol.t(); ColumnVector a(nRC); a = 0.0; int i, j; for (i = 1; i <= nRC; ++i) { // accumulate subtr sum Real subtrsum = 0.0; for(int k = 1; k < i; ++k) subtrsum += a(k) * L(i,k); a(i) = (x(i) - subtrsum) / L(i,i); } // test that l2 norm of a is < 1 Real squareNormA = a.SumSquare(); if (squareNormA >= 1.0) Throw(ProgramException("downdate_Cholesky() fails", chol)); Real alpha = sqrt(1.0 - squareNormA); // compute and apply Givens rotations to the vector a ColumnVector cGivens(nRC); cGivens = 0.0; ColumnVector sGivens(nRC); sGivens = 0.0; for(i = nRC; i >= 1; i--) alpha = pythag(alpha, a(i), cGivens(i), sGivens(i)); // apply Givens rotations to the jth column of chol ColumnVector xtilde(nRC); xtilde = 0.0; for(j = nRC; j >= 1; j--) { // only the first j rotations have an affect on chol,0 for(int k = j; k >= 1; k--) GivensRotation(cGivens(k), -sGivens(k), chol(k,j), xtilde(j)); } }
void test4(Real* y, Real* x1, Real* x2, int nobs, int npred) { cout << "\n\nTest 4 - QR triangularisation\n"; // QR triangularisation method // load data - 1s into col 1 of matrix int npred1 = npred+1; Matrix X(nobs,npred1); ColumnVector Y(nobs); X.Column(1) = 1.0; X.Column(2) << x1; X.Column(3) << x2; Y << y; // do Householder triangularisation // no need to deal with constant term separately Matrix X1 = X; // Want copy of matrix ColumnVector Y1 = Y; UpperTriangularMatrix U; ColumnVector M; QRZ(X1, U); QRZ(X1, Y1, M); // Y1 now contains resids ColumnVector A = U.i() * M; ColumnVector Fitted = X * A; Real ResVar = Y1.SumSquare() / (nobs-npred1); // get variances of estimates U = U.i(); DiagonalMatrix D; D << U * U.t(); // Get diagonals of Hat matrix DiagonalMatrix Hat; Hat << X1 * X1.t(); // print out answers cout << "\nEstimates and their standard errors\n\n"; ColumnVector SE(npred1); for (int i=1; i<=npred1; i++) SE(i) = sqrt(D(i)*ResVar); cout << setw(11) << setprecision(5) << (A | SE) << endl; cout << "\nObservations, fitted value, residual value, hat value\n"; cout << setw(9) << setprecision(3) << (X.Columns(2,3) | Y | Fitted | Y1 | Hat.AsColumn()); cout << "\n\n"; }