/** * Save current parameters, derivatives and hessian. */ void CostFuncLeastSquares::push() { if (m_pushed) { throw std::runtime_error("Least squares: double push."); } // make sure we are not dirty m_pushedValue = valDerivHessian(); getParameters(m_pushedParams); m_pushed = true; }
/** * Return cached or calculate the Hessian. */ const GSLMatrix &CostFuncLeastSquares::getHessian() const { if (m_pushed) { return m_hessian; } if (m_dirtyVal || m_dirtyDeriv || m_dirtyHessian) { valDerivHessian(); } return m_hessian; }
/** * Return cached or calculate the drivatives. */ const GSLVector &CostFuncLeastSquares::getDeriv() const { if (m_pushed) { return m_der; } if (m_dirtyVal || m_dirtyDeriv || m_dirtyHessian) { valDerivHessian(); } return m_der; }
/** * Calculates covariance matrix for fitting function's active parameters. * @param covar :: Output cavariance matrix. * @param epsrel :: Tolerance. */ void CostFuncLeastSquares::calActiveCovarianceMatrix(GSLMatrix& covar, double epsrel) { UNUSED_ARG(epsrel); if (m_hessian.isEmpty()) { valDerivHessian(); } covar = m_hessian; covar.invert(); }
/** Calculate the value and the derivatives of the cost function * @param der :: Container to output the derivatives * @return :: The value of the function */ double CostFuncLeastSquares::valAndDeriv(std::vector<double> &der) const { valDerivHessian(true, false); if (der.size() != nParams()) { der.resize(nParams()); } for (size_t i = 0; i < nParams(); ++i) { der[i] = m_der.get(i); } return m_value; }
/** * Calculates covariance matrix for fitting function's active parameters. * @param covar :: Output cavariance matrix. * @param epsrel :: Tolerance. */ void CostFuncLeastSquares::calActiveCovarianceMatrix(GSLMatrix &covar, double epsrel) { UNUSED_ARG(epsrel); if (m_hessian.isEmpty()) { valDerivHessian(); } if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) { std::ostringstream osHess; osHess << "== Hessian (H) ==\n"; osHess << std::left << std::fixed; for (size_t i = 0; i < m_hessian.size1(); ++i) { for (size_t j = 0; j < m_hessian.size2(); ++j) { osHess << std::setw(10); osHess << m_hessian.get(i, j) << " "; } osHess << "\n"; } g_log.debug() << osHess.str(); } covar = m_hessian; covar.invert(); if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) { std::ostringstream osCovar; osCovar << "== Covariance matrix (H^-1) ==\n"; osCovar << std::left << std::fixed; for (size_t i = 0; i < covar.size1(); ++i) { for (size_t j = 0; j < covar.size2(); ++j) { osCovar << std::setw(10); osCovar << covar.get(i, j) << " "; } osCovar << "\n"; } g_log.debug() << osCovar.str(); } }