Mantid
Loading...
Searching...
No Matches
CostFuncUnweightedLeastSquares.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
10#include <cmath>
11
13
14namespace {
16Kernel::Logger g_log("CostFuncUnweightedLeastSquares");
17} // namespace
18
19DECLARE_COSTFUNCTION(CostFuncUnweightedLeastSquares, Unweighted least squares)
20
22
25
26 double variance = getResidualVariance();
27 covar *= variance;
28
29 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
30 g_log.debug() << "== Final covariance matrix (H^-1) ==\n";
31
32 std::ios::fmtflags prevState = g_log.debug().flags();
33 g_log.debug() << std::left << std::fixed;
34
35 for (size_t i = 0; i < covar.size1(); ++i) {
36 for (size_t j = 0; j < covar.size2(); ++j) {
37 g_log.debug() << std::setw(10);
38 g_log.debug() << covar.get(i, j) << " ";
39 }
40 g_log.debug() << '\n';
41 }
42 g_log.debug().flags(prevState);
43 }
44}
45
48 std::vector<double> weights(values->size());
49 for (size_t i = 0; i < weights.size(); ++i) {
50 weights[i] = values->getFitWeight(i) != 0 ? 1 : 0;
51 }
52
53 return weights;
54}
55
58 if (!m_values || m_values->size() == 0) {
59 return 0;
60 }
61
62 double sum = 0.0;
63 for (size_t i = 0; i < m_values->size(); ++i) {
64 double difference = m_values->getCalculated(i) - m_values->getFitData(i);
65 sum += difference * difference;
66 }
67
68 auto degreesOfFreedom = static_cast<double>(m_values->size() - nParams());
69 double residualVariance = sum / degreesOfFreedom;
70
71 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
72 g_log.debug() << "== Statistics of residuals ==\n";
73 std::ios::fmtflags prevState = g_log.debug().flags();
74 g_log.debug() << std::left << std::fixed << std::setw(10);
75 g_log.debug() << "Residual sum of squares: " << sum << '\n';
76 g_log.debug() << "Residual variance: " << residualVariance << '\n';
77 g_log.debug() << "Residual standard deviation: " << sqrt(residualVariance) << '\n';
78 g_log.debug() << "Degrees of freedom: " << static_cast<size_t>(degreesOfFreedom) << '\n';
79 g_log.debug() << "Number of observations: " << m_values->size() << '\n';
80 g_log.debug().flags(prevState);
81 }
82
83 return residualVariance;
84}
85
86} // namespace Mantid::CurveFitting::CostFunctions
#define DECLARE_COSTFUNCTION(classname, username)
Macro for declaring a new type of cost functions to be used with the CostFunctionFactory.
Definition: ICostFunction.h:66
API::FunctionValues_sptr m_values
Shared poinetr to the function values.
size_t nParams() const override
Number of parameters.
void calActiveCovarianceMatrix(EigenMatrix &covar, double epsrel=1e-8) override
Calculates covariance matrix for fitting function's active parameters.
In contrast to CostFuncLeastSquares, this variant of the cost function assumes that there are no weig...
void calActiveCovarianceMatrix(EigenMatrix &covar, double epsrel) override
Calculates covariance matrix for fitting function's active parameters.
std::vector< double > getFitWeights(API::FunctionValues_sptr values) const override
Return unit weights for all data points.
double getResidualVariance() const
Calculates the residual variance from the internally stored FunctionValues.
A wrapper around Eigen::Matrix.
Definition: EigenMatrix.h:33
double get(size_t i, size_t j) const
Get an element.
size_t size1() const
First size of the matrix.
size_t size2() const
Second size of the matrix.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
bool is(int level) const
Returns true if at least the given log level is set.
Definition: Logger.cpp:146
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
Kernel::Logger g_log("ExperimentInfo")
static logger object