/** * Get the index of the function to which parameter i belongs * @param i :: The parameter index * @return function index of the requested parameter */ size_t CompositeFunction::functionIndex(std::size_t i) const { if (i >= nParams()) { throw std::out_of_range("Function parameter index (" + std::to_string(i) + ") out of range (" + std::to_string(nParams()) + ")."); } return m_IFunction[i]; }
/** * Returns the index of parameter if the ref points to this ParamFunction * @param ref :: A reference to a parameter * @return Parameter index or number of nParams() if parameter not found */ size_t TiesFunction::getParameterIndex(const ParameterReference& ref)const { if (ref.getFunction() == this && ref.getIndex() < nParams()) { return ref.getIndex(); } return nParams(); }
/** * Copy the parameter values to a GSLVector. * @param params :: A vector to copy the parameters to */ void CostFuncLeastSquares::getParameters(GSLVector ¶ms) const { if (params.size() != nParams()) { params.resize(nParams()); } for (size_t i = 0; i < nParams(); ++i) { params.set(i, getParameter(i)); } }
void FunctionChangesNParams::iterationFinished() { auto np = nParams(); if (m_canChange && np < m_maxNParams) { declareParameter("A" + std::to_string(np), 0.0); throw Mantid::Kernel::Exception::FitSizeWarning(np, nParams()); } m_canChange = false; }
/** * Writes a string that can be used in Fit.IFunction to create a copy of this * IFunction * @return string representation of the function */ std::string IFunction::asString() const { std::ostringstream ostr; ostr << "name=" << this->name(); // print the attributes std::vector<std::string> attr = this->getAttributeNames(); for (const auto &attName : attr) { std::string attValue = this->getAttribute(attName).value(); if (!attValue.empty() && attValue != "\"\"") { ostr << ',' << attName << '=' << attValue; } } // print the parameters for (size_t i = 0; i < nParams(); i++) { const ParameterTie *tie = getTie(i); if (!tie || !tie->isDefault()) { ostr << ',' << parameterName(i) << '=' << getParameter(i); } } // collect non-default constraints std::string constraints; for (size_t i = 0; i < nParams(); i++) { const IConstraint *c = getConstraint(i); if (c && !c->isDefault()) { std::string tmp = c->asString(); if (!tmp.empty()) { if (!constraints.empty()) { constraints += ","; } constraints += tmp; } } } // print constraints if (!constraints.empty()) { ostr << ",constraints=(" << constraints << ")"; } // collect the non-default ties std::string ties; for (size_t i = 0; i < nParams(); i++) { const ParameterTie *tie = getTie(i); if (tie && !tie->isDefault()) { std::string tmp = tie->asString(this); if (!tmp.empty()) { if (!ties.empty()) { ties += ","; } ties += tmp; } } } // print the ties if (!ties.empty()) { ostr << ",ties=(" << ties << ")"; } return ostr.str(); }
/** * Copy the parameter values from a GSLVector. * @param params :: A vector to copy the parameters from */ void CostFuncLeastSquares::setParameters(const GSLVector ¶ms) { if (nParams() != params.size()) { throw std::runtime_error( "Parameter vector has wrong size in CostFuncLeastSquares."); } for (size_t i = 0; i < nParams(); ++i) { setParameter(i, params.get(i)); } m_function->applyTies(); }
/** 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; }
/** * Returns the index of parameter if the ref points to one of the member * function * @param ref :: A reference to a parameter * @return Parameter index or number of nParams() if parameter not found */ size_t CompositeFunction::getParameterIndex(const ParameterReference &ref) const { if (ref.getLocalFunction() == this && ref.getLocalIndex() < nParams()) { return ref.getLocalIndex(); } for (size_t iFun = 0; iFun < nFunctions(); iFun++) { IFunction_sptr fun = getFunction(iFun); size_t iLocalIndex = fun->getParameterIndex(ref); if (iLocalIndex < fun->nParams()) { return m_paramOffsets[iFun] + iLocalIndex; } } return nParams(); }
/** Sets a new value to the i-th parameter. * @param i :: The parameter index * @param value :: The new value * @param explicitlySet :: A boolean falgging the parameter as explicitly set * (by user) */ void ParamFunction::setParameter(size_t i, const double &value, bool explicitlySet) { // Cppcheck confused by the check for NaN if (boost::math::isnan(value)) { // Check for NaN or -NaN std::stringstream errmsg; errmsg << "Trying to set a NaN or infinity value (" << value << ") to parameter " << this->parameterName(i); g_log.warning(errmsg.str()); // throw std::runtime_error(errmsg.str()); } else if (value <= -DBL_MAX || value >= DBL_MAX) { // Infinity value std::stringstream errmsg; errmsg << "Trying to set an infinity value (" << value << ") to parameter " << this->parameterName(i); g_log.warning(errmsg.str()); // throw std::runtime_error(errmsg.str()); } if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } if (explicitlySet && value != m_parameters[i]) { m_explicitlySet[i] = true; } m_parameters[i] = value; }
/** * Return a vector with all parameter names. */ std::vector<std::string> IFunction::getParameterNames() const { std::vector<std::string> out; for (size_t i = 0; i < nParams(); ++i) { out.push_back(parameterName(i)); } return out; }
/** Sets a new parameter description to the i-th parameter. * @param i :: The parameter index * @param description :: New parameter description */ void ParamFunction::setParameterDescription(size_t i, const std::string &description) { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } m_parameterDescriptions[i] = description; }
/// Calculates the residual variance from the internally stored FunctionValues. double CostFuncUnweightedLeastSquares::getResidualVariance() const { if (!m_values || m_values->size() == 0) { return 0; } double sum = 0.0; for (size_t i = 0; i < m_values->size(); ++i) { double difference = m_values->getCalculated(i) - m_values->getFitData(i); sum += difference * difference; } double degreesOfFreedom = static_cast<double>(m_values->size() - nParams()); double residualVariance = sum / degreesOfFreedom; if (g_log.is(Kernel::Logger::Priority::PRIO_INFORMATION)) { g_log.information() << "== Statistics of residuals ==" << std::endl; std::ios::fmtflags prevState = g_log.information().flags(); g_log.information() << std::left << std::fixed << std::setw(10); g_log.information() << "Residual sum of squares: " << sum << std::endl; g_log.information() << "Residual variance: " << residualVariance << std::endl; g_log.information() << "Residual standard deviation: " << sqrt(residualVariance) << std::endl; g_log.information() << "Degrees of freedom: " << static_cast<size_t>(degreesOfFreedom) << std::endl; g_log.information() << "Number of observations: " << m_values->size() << std::endl; g_log.information().flags(prevState); } return residualVariance; }
/** * Prepare the function for a fit. */ void CompositeFunction::setUpForFit() { IFunction::setUpForFit(); // set up the member functions for (size_t i = 0; i < nFunctions(); i++) { getFunction(i)->setUpForFit(); } // unfortuately the code below breaks some system tests (IRISFuryAndFuryFit) // it looks as if using numeric derivatives can give different fit results // to fit with analytical ones // // if parameters have non-constant ties enable numerical derivatives // for(size_t i = 0; i < nParams(); ++i) //{ // ParameterTie* tie = getTie( i ); // if ( tie && !tie->isConstant() ) // { // useNumericDerivatives( true ); // break; // } //} // instead of automatically switching to numeric derivatives // log a warning about a danger of not using it if (!getAttribute("NumDeriv").asBool()) { for (size_t i = 0; i < nParams(); ++i) { ParameterTie *tie = getTie(i); if (tie && !tie->isConstant()) { g_log.warning() << "Numeric derivatives should be used when " "non-constant ties defined.\n"; break; } } } }
/** Returns the description of parameter i * @param i :: The index of a parameter * @return the description of the parameter at the requested index */ std::string ParamFunction::parameterDescription(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } return m_parameterDescriptions[i]; }
/** Get the i-th parameter. * @param i :: The parameter index * @return the value of the requested parameter */ double ChFun::getParameter(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } return ypoints()[i]; }
/** * Set the fitting error for a parameter * @param i :: The index of a parameter * @param err :: The error value to set */ void ChFun::setError(size_t i, double err) { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } m_errors[i] = err; }
/** * Get the fitting error for a parameter * @param i :: The index of a parameter * @return :: the error */ double ChFun::getError(size_t i) const { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } return m_errors[i]; }
/** Get the i-th parameter. * @param i :: The parameter index * @return the value of the requested parameter */ double ParamFunction::getParameter(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } return m_parameters[i]; }
/** Returns the name of parameter i * @param i :: The index of a parameter * @return the name of the parameter at the requested index */ std::string ChFun::parameterName(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } return "Y" + boost::lexical_cast<std::string>( i ); }
/** * @param ref :: The reference * @return A ParamFunction containing parameter pointed to by ref */ IFitFunction* ParamFunction::getContainingFunction(const ParameterReference& ref)const { if (ref.getFunction() == this && ref.getIndex() < nParams()) { return ref.getFunction(); } return NULL; }
/** Returns the description of parameter i * @param i :: The index of a parameter * @return the description of the parameter at the requested index */ std::string ChFun::parameterDescription(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } return "Function value at point " + boost::lexical_cast<std::string>(i); }
/// Checks if a parameter has been set explicitly bool ParamFunction::isExplicitlySet(size_t i)const { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } return m_explicitlySet[i]; }
/** Makes a parameter active again. It doesn't change the parameter's tie. * @param i :: A declared parameter index to be restored to active */ void ParamFunction::restoreActive(size_t i) { if (i >= nParams()) throw std::out_of_range("ParamFunction parameter index out of range."); if (nParams() == nActive()) return; std::vector<size_t >::iterator it = std::find_if(m_indexMap.begin(),m_indexMap.end(),std::bind2nd(std::greater<size_t>(),i)); if (it != m_indexMap.end()) { m_indexMap.insert(it,i); } else { m_indexMap.push_back(i); } }
/// Get the address of the parameter /// @param i :: the index of the parameter required /// @returns the address of the parameter double* ParamFunction::getParameterAddress(size_t i) { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } return &m_parameters[i]; }
/** Get tie of parameter number i * @param i :: The index of a declared parameter. * @return A pointer to the tie */ ParameterTie *ParamFunction::getTie(size_t i) const { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } auto it = std::find_if(m_ties.cbegin(), m_ties.cend(), ReferenceEqual(i)); if (it != m_ties.cend()) { return *it; } return nullptr; }
/** * Reset fitting parameters after changes to some attributes. */ void BSpline::resetParameters() { if (nParams() > 0) { clearAllParameters(); } size_t np = gsl_bspline_ncoeffs(m_bsplineWorkspace.get()); for (size_t i = 0; i < np; ++i) { std::string pname = "A" + boost::lexical_cast<std::string>(i); declareParameter(pname); } }
/** This method doesn't create a tie * @param i :: A declared parameter index to be removed from active */ void ParamFunction::removeActive(size_t i) { if (!isActive(i)) return; if (i >= nParams()) throw std::out_of_range("ParamFunction parameter index out of range."); if (m_indexMap.size() == 0)// This should never happen { for(size_t j=0;j<nParams();j++) if (j != i) m_indexMap.push_back(j); } else { std::vector<size_t>::iterator it = std::find(m_indexMap.begin(),m_indexMap.end(),i); if (it != m_indexMap.end()) { m_indexMap.erase(it); } } }
/** Sets a new value to the i-th parameter. * @param i :: The parameter index * @param value :: The new value * @param explicitlySet :: A boolean falgging the parameter as explicitly set (by user) */ void ParamFunction::setParameter(size_t i, const double& value, bool explicitlySet) { if (i >= nParams()) { throw std::out_of_range("ParamFunction parameter index out of range."); } m_parameters[i] = value; if (explicitlySet) { m_explicitlySet[i] = true; } }
/** Sets a new value to the i-th parameter. * @param i :: The parameter index * @param value :: The new value * @param explicitlySet :: A boolean falgging the parameter as explicitly set (by user) */ void ChFun::setParameter(size_t i, const double& value, bool explicitlySet) { if (i >= nParams()) { throw std::out_of_range("ChFun parameter index out of range."); } setP( i, value ); if (explicitlySet) { m_explicitlySet[i] = true; } this->updateStateRequired(); }
/** Get constraint of parameter number i * @param i :: The index of a declared parameter. * @return A pointer to the constraint or NULL */ IConstraint* TiesFunction::getConstraint(size_t i)const { if (i >= nParams()) { throw std::out_of_range("TiesFunction parameter index out of range."); } std::vector<IConstraint*>::const_iterator it = std::find_if(m_constraints.begin(),m_constraints.end(),ReferenceEqual(i)); if (it != m_constraints.end()) { return *it; } return NULL; }