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 for (size_t i = 0; i < m_values->size(); i++) {
60 auto currentWeight = m_values->getFitWeight(i);
61 // Checking whether currentWeight != 0.0
62 if (std::abs(currentWeight) > std::max(1.0, std::abs(currentWeight)) * std::numeric_limits<double>::epsilon()) {
63 m_values->setFitWeight(i, 1.0);
64 }
65 } else {
66 m_values->setFitWeight(i, 1.0);
67 }
68 }
69}
70
73 if (!m_values || m_values->size() == 0) {
74 return 0;
75 }
76
77 double sum = 0.0;
78 for (size_t i = 0; i < m_values->size(); ++i) {
79 double difference = m_values->getCalculated(i) - m_values->getFitData(i);
80 sum += difference * difference;
81 }
82
83 auto degreesOfFreedom = static_cast<double>(m_values->size() - nParams());
84 double residualVariance = sum / degreesOfFreedom;
85
86 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
87 g_log.debug() << "== Statistics of residuals ==\n";
88 std::ios::fmtflags prevState = g_log.debug().flags();
89 g_log.debug() << std::left << std::fixed << std::setw(10);
90 g_log.debug() << "Residual sum of squares: " << sum << '\n';
91 g_log.debug() << "Residual variance: " << residualVariance << '\n';
92 g_log.debug() << "Residual standard deviation: " << sqrt(residualVariance) << '\n';
93 g_log.debug() << "Degrees of freedom: " << static_cast<size_t>(degreesOfFreedom) << '\n';
94 g_log.debug() << "Number of observations: " << m_values->size() << '\n';
95 g_log.debug().flags(prevState);
96 }
97
98 return residualVariance;
99}
100
101} // namespace Mantid::CurveFitting::CostFunctions
#define DECLARE_COSTFUNCTION(classname, username)
Macro for declaring a new type of cost functions to be used with the CostFunctionFactory.
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 updateValidateFitWeights() override
Update the fit weights of m_values so that correct fit weights are used by the minimizer and GSLFunct...
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:145
bool is(int level) const
Returns true if at least the given log level is set.
Definition Logger.cpp:177
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
Kernel::Logger g_log("ExperimentInfo")
static logger object