bool LogisticRegression::solve(double tolerance) { if (numObservations() <= 0) { THEA_WARNING << "LogisticRegression: Solving empty problem"; has_solution = true; solution.resize(0); } else { StdLinearSolver llsq(StdLinearSolver::Method::DEFAULT, StdLinearSolver::Constraint::UNCONSTRAINED); llsq.setTolerance(tolerance); typedef Eigen::Map< MatrixX<double, MatrixLayout::ROW_MAJOR> > M; M a(&llsq_coeffs[0], numObservations(), ndims); has_solution = llsq.solve(MatrixWrapper<M>(&a), &llsq_consts[0]); if (has_solution) solution = Eigen::Map<VectorXd>(const_cast<double *>(llsq.getSolution()), ndims); else solution.resize(0); } return has_solution; }
void test01 ( ) /******************************************************************************/ /* Purpose: TEST01 calls LLSQ to match 15 data values. Licensing: This code is distributed under the GNU LGPL license. Modified: 07 March 2012 Author: John Burkardt */ { double a; double b; double error; int i; int n = 15; double x[15] = { 1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83 }; double y[15] = { 52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46 }; printf ( "\n" ); printf ( "TEST01\n" ); printf ( " LLSQ can compute the formula for a line of the form\n" ); printf ( " y = A * x + B\n" ); printf ( " which minimizes the RMS error to a set of N data values.\n" ); llsq ( n, x, y, &a, &b ); printf ( "\n" ); printf ( " Estimated relationship is y = %g * x + %g\n", a, b ); printf ( " Expected value is y = 61.272 * x - 39.062\n" ); printf ( "\n" ); printf ( " I X Y B+A*X |error|\n" ); printf ( "\n" ); error = 0.0; for ( i = 0; i < n; i++ ) { printf ( " %4d %7g %7g %7g %7g\n", i, x[i], y[i], b + a * x[i], b + a * x[i] - y[i] ); error = error + pow ( b + a * x[i] - y[i], 2 ); } error = sqrt ( error / ( double ) n ); printf ( "\n" ); printf ( " RMS error = %g\n", error ); return; }
void Optimizer::updateNorma(const TVReal <s, const TVComplex &vs, real &normak, real &normab) { TVReal as(vs.size()); real mn=std::numeric_limits<real>::max(); for(size_t i(0); i<vs.size(); i++) { as[i] = log(vs[i].a()); if(as[i] > std::numeric_limits<real>::max() || as[i] < -std::numeric_limits<real>::max()) { //inf } else { if(mn>as[i]) mn = as[i]; } } for(size_t i(0); i<vs.size(); i++) { if(as[i] > std::numeric_limits<real>::max() || as[i] < -std::numeric_limits<real>::max()) { as[i] = mn; } } if(mn == std::numeric_limits<real>::max()) { normak = 0; normab = 0; return; } llsq(<s[0], &as[0], as.size(), normak, normab); if( _isnan(normak) || _isnan(normab) || normak > std::numeric_limits<real>::max() || normak < -std::numeric_limits<real>::max()|| normab > std::numeric_limits<real>::max() || normab < -std::numeric_limits<real>::max()) { normak = 0; normab = 0; } }