double NotPeriodogram::logLikelihood() const { // Get the data const VectorXd& y = Data::get_instance().get_y_eigen(); // Compute the diagonal elements. VectorXd diag(y.size()); for(size_t i=0; i<y.size(); i++) diag[i] = calculate_C(i, i); HODLRSolverMatrix matrix(*this); HODLR_Tree<HODLRSolverMatrix>* solver = new HODLR_Tree<HODLRSolverMatrix> (&matrix, y.size(), 30); solver->assemble_Matrix(diag, 1E-10, 's'); // Factorize the matrix. solver->compute_Factor(); double logdet; // Extract the log-determinant. solver->compute_Determinant(logdet); double* b = new double[y.size()]; double* out = new double[y.size()]; for(int i=0; i<y.size(); i++) b[i] = y[i]; MatrixXd b_vec = RowMajorMap(b, y.size(), 1); MatrixXd alpha(y.size(), 1); solver->solve(b_vec, alpha); for(int i=0; i<y.size(); i++) out[i] = alpha(i, 0); double exponent = 0.; for(int i=0; i<y.size(); i++) exponent += y(i)*out[i]; double logL = -0.5*y.size()*log(2*M_PI) - 0.5*logdet - 0.5*exponent; if(isnan(logL) || isinf(logL)) logL = -1E300; delete[] b; delete[] out; delete solver; return logL; }
int main() { srand (time(NULL)); unsigned N = 2000; unsigned nRhs = 1; unsigned nLeaf = 100; double tolerance= 1e-15; Test_Kernel kernel(N); MatrixXcd xExact = MatrixXcd::Random(N, nRhs); MatrixXcd bExact(N,nRhs), bFast(N,nRhs), xFast(N,nRhs); cout << endl << "Number of particles is: " << N << endl; clock_t start, end; cout << endl << "Setting things up..." << endl; start = clock(); HODLR_Tree<Test_Kernel>* A = new HODLR_Tree<Test_Kernel>(&kernel, N, nLeaf); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Assembling the matrix in HODLR form..." << endl; start = clock(); VectorXcd diagonal = 4.0*VectorXcd::Ones(N); A->assemble_Matrix(diagonal, tolerance); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Exact matrix vector product..." << endl; start = clock(); for (unsigned i=0; i<N; ++i) { bExact(i,0) = diagonal(i)*xExact(i,0); for (unsigned j=0; j<i; ++j) { bExact(i,0) = bExact(i,0)+kernel.get_Matrix_Entry(i, j)*xExact(j,0); } for (unsigned j=i+1; j<N; ++j) { bExact(i,0) = bExact(i,0)+kernel.get_Matrix_Entry(i, j)*xExact(j,0); } } end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Fast matrix matrix product..." << endl; start = clock(); A->matMatProduct(xExact, bFast); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Factoring the matrix..." << endl; start = clock(); A->compute_Factor(); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Solving the system..." << endl; start = clock(); A->solve(bExact, xFast); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Error in computed solution: " << (xFast-xExact).norm()/xExact.norm()<< endl; cout << endl << "Error in matrix matrix product: " << (bFast-bExact).cwiseAbs().maxCoeff() << endl; // MatrixXcd B; // cout << endl << "Assembling the entire matrix..." << endl; // start = clock(); // get_Matrix(0, 0, N, N, B); // end = clock(); // cout << endl << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; // // cout << endl << "Exact determinant is: " << setprecision(16) << log(fabs(B.partialPivLu().determinant())) << endl; complex<double> determinant; cout << endl << "Computing the log determinant..." << endl; start = clock(); A->compute_Determinant(determinant); end = clock(); cout << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; cout << endl << "Log determinant is: " << setprecision(16) << determinant << endl; MatrixXcd K; kernel.get_Matrix(0, 0, N, N, K); for (unsigned k=0; k<N; ++k) { K(k,k) = diagonal(k); } complex<double> exact_determinant; exact_determinant = compute_Determinant(K); cout << endl << "Exact log determinant is: " << setprecision(16) << exact_determinant << endl; // // cout << endl << "Exact matrix matrix product..." << endl; // start = clock(); // MatrixXcd bExact = B*x; // end = clock(); // cout << endl << "Time taken is: " << double(end-start)/double(CLOCKS_PER_SEC)<< endl; // // cout << endl << (bExact-b).cwiseAbs().maxCoeff() << endl; }