BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ObjectiveMetric.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Core/Fitting/ObjectiveMetric.h
6 //! @brief Defines ObjectiveMetric classes.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifndef BORNAGAIN_CORE_FITTING_OBJECTIVEMETRIC_H
16 #define BORNAGAIN_CORE_FITTING_OBJECTIVEMETRIC_H
17 
18 #include "Base/Types/ICloneable.h"
19 #include <functional>
20 #include <memory>
21 #include <vector>
22 
23 class SimDataPair;
24 
25 //! Base class for metric implementations
27 {
28 public:
29  ObjectiveMetric(std::function<double(double)> norm);
30 
31  ObjectiveMetric* clone() const override = 0;
32 
33  //! Computes metric value from SimDataPair object. Calls computeFromArrays internally.
34  //! @param data_pair: SimDataPair object. Can optionally contain data uncertainties
35  //! @param use_weights: boolean, defines if data uncertainties should be taken into account
36  virtual double compute(const SimDataPair& data_pair, bool use_weights) const;
37 
38  //! Computes metric value from data arrays. Negative values in exp_data
39  //! are ignored as well as non-positive weight_factors and uncertainties.
40  //! All arrays involved in the computation must be of the same size.
41  //! @param sim_data: array with simulated intensities.
42  //! @param exp_data: array with intensity values obtained from an experiment.
43  //! @param uncertainties: array with experimental data uncertainties.
44  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
45  //! is chosen.
46  virtual double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
47  std::vector<double> uncertainties,
48  std::vector<double> weight_factors) const = 0;
49 
50  //! Computes metric value from data arrays. Negative values in exp_data
51  //! are ignored as well as non-positive weight_factors.
52  //! All arrays involved in the computation must be of the same size.
53  //! @param sim_data: array with simulated intensities.
54  //! @param exp_data: array with intensity values obtained from an experiment.
55  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
56  //! is chosen.
57  virtual double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
58  std::vector<double> weight_factors) const = 0;
59 
60  void setNorm(std::function<double(double)> norm);
61 
62  //! Returns a copy of the normalization function used.
63  auto norm() const { return m_norm; }
64 
65 private:
66  std::function<double(double)> m_norm; //! normalization function.
67 };
68 
69 //! Implementation of the standard \f$ \chi^2 \f$ metric
70 //! derived from maximum likelihood with Gaussian uncertainties.
71 //! With default L2 norm corresponds to the formula
72 //! \f[\chi^2 = \sum \frac{(I - D)^2}{\delta_D^2}\f]
74 {
75 public:
76  Chi2Metric();
77  Chi2Metric* clone() const override;
78 
79  //! Computes metric value from data arrays. Negative values in exp_data
80  //! are ignored as well as non-positive weight_factors and uncertainties.
81  //! All arrays involved in the computation must be of the same size.
82  //! @param sim_data: array with simulated intensities.
83  //! @param exp_data: array with intensity values obtained from an experiment.
84  //! @param uncertainties: array with experimental data uncertainties.
85  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
86  //! is chosen.
87  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
88  std::vector<double> uncertainties,
89  std::vector<double> weight_factors) const override;
90 
91  //! Computes metric value from data arrays. Negative values in exp_data
92  //! are ignored as well as non-positive weight_factors.
93  //! All arrays involved in the computation must be of the same size.
94  //! @param sim_data: array with simulated intensities.
95  //! @param exp_data: array with intensity values obtained from an experiment.
96  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
97  //! is chosen.
98  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
99  std::vector<double> weight_factors) const override;
100 };
101 
102 //! Implementation of \f$ \chi^2 \f$ metric
103 //! with standard deviation\f$\sigma = max(\sqrt{I}, 1)\f$,
104 //! where \f$I\f$ is the simulated intensity.
105 //! With default L2 norm corresponds to the formula
106 //! \f[\chi^2 = \sum \frac{(I - D)^2}{max(I, 1)}\f]
107 //! for unweighted experimental data. Falls to standard
108 //! Chi2Metric when data uncertainties are taken into account.
110 {
111 public:
113  PoissonLikeMetric* clone() const override;
114 
116 
117  //! Computes metric value from data arrays. Negative values in exp_data
118  //! are ignored as well as non-positive weight_factors.
119  //! All arrays involved in the computation must be of the same size.
120  //! @param sim_data: array with simulated intensities.
121  //! @param exp_data: array with intensity values obtained from an experiment.
122  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
123  //! is chosen.
124  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
125  std::vector<double> weight_factors) const override;
126 };
127 
128 //! Implementation of the standard \f$ \chi^2 \f$ metric with intensity \f$I\f$
129 //! and experimental data \f$D\f$
130 //! being replaced by \f$ \log_{10} I \f$ and \f$\log_{10} D\f$ accordingly.
131 //! With default L2 norm corresponds to the formula
132 //! \f[\chi^2 = \sum \frac{(\log_{10} I - log_{10} D)^2 D^2 \ln^2{10}}{\delta_D^2}\f]
134 {
135 public:
136  LogMetric();
137  LogMetric* clone() const override;
138 
139  //! Computes metric value from data arrays. Negative values in exp_data
140  //! are ignored as well as non-positive weight_factors and uncertainties.
141  //! All arrays involved in the computation must be of the same size.
142  //! @param sim_data: array with simulated intensities.
143  //! @param exp_data: array with intensity values obtained from an experiment.
144  //! @param uncertainties: array with experimental data uncertainties.
145  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
146  //! is chosen.
147  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
148  std::vector<double> uncertainties,
149  std::vector<double> weight_factors) const override;
150 
151  //! Computes metric value from data arrays. Negative values in exp_data
152  //! are ignored as well as non-positive weight_factors.
153  //! All arrays involved in the computation must be of the same size.
154  //! @param sim_data: array with simulated intensities.
155  //! @param exp_data: array with intensity values obtained from an experiment.
156  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
157  //! is chosen.
158  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
159  std::vector<double> weight_factors) const override;
160 };
161 
162 //! Implementation of relative difference metric.
163 //! With default L2 norm and weighting off corresponds to the formula
164 //! \f[Result = \sum \frac{(I - D)^2}{(I + D)^2}\f]
165 //! where \f$I\f$ is the simulated intensity, \f$D\f$ - experimental data.
166 //! If weighting is on, falls back to the standard \f$\chi^2\f$ metric.
168 {
169 public:
171  RelativeDifferenceMetric* clone() const override;
172 
174 
175  //! Computes metric value from data arrays. Negative values in exp_data
176  //! are ignored as well as non-positive weight_factors.
177  //! All arrays involved in the computation must be of the same size.
178  //! @param sim_data: array with simulated intensities.
179  //! @param exp_data: array with intensity values obtained from an experiment.
180  //! @param weight_factors: user-defined weighting factors. Used linearly, no matter which norm
181  //! is chosen.
182  double computeFromArrays(std::vector<double> sim_data, std::vector<double> exp_data,
183  std::vector<double> weight_factors) const override;
184 };
185 
186 //! Implementation of relative difference metric.
187 //! With default L2 norm and weighting off corresponds to the formula
188 //! \f[Result = \sum (I \cdot Q^4 - D \cdot Q^4)^2\f]
189 //! where \f$Q\f$ is the scattering vector magnitude. If weighting is on,
190 //! coincides with the metric provided by Chi2Metric class.
191 class RQ4Metric : public Chi2Metric
192 {
193 public:
194  RQ4Metric();
195  RQ4Metric* clone() const override;
196  //! Computes metric value from SimDataPair object. Calls computeFromArrays internally.
197  //! @param data_pair: SimDataPair object. Can optionally contain data uncertainties
198  //! @param use_weights: boolean, defines if data uncertainties should be taken into account
199  double compute(const SimDataPair& data_pair, bool use_weights) const override;
200 };
201 
202 #endif // BORNAGAIN_CORE_FITTING_OBJECTIVEMETRIC_H
Defines and implements the standard mix-in ICloneable.
Implementation of the standard metric derived from maximum likelihood with Gaussian uncertainties.
Chi2Metric * clone() const override
double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const override
Computes metric value from data arrays.
Interface for polymorphic classes that should not be copied, except by explicit cloning.
Definition: ICloneable.h:25
Implementation of the standard metric with intensity and experimental data being replaced by and ...
double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const override
Computes metric value from data arrays.
LogMetric * clone() const override
Base class for metric implementations.
virtual double compute(const SimDataPair &data_pair, bool use_weights) const
Computes metric value from SimDataPair object.
virtual double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > weight_factors) const =0
Computes metric value from data arrays.
virtual double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const =0
Computes metric value from data arrays.
std::function< double(double)> m_norm
ObjectiveMetric * clone() const override=0
auto norm() const
Returns a copy of the normalization function used.
ObjectiveMetric(std::function< double(double)> norm)
void setNorm(std::function< double(double)> norm)
Implementation of metric with standard deviation , where is the simulated intensity.
PoissonLikeMetric * clone() const override
double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const override
Computes metric value from data arrays.
Implementation of relative difference metric.
double compute(const SimDataPair &data_pair, bool use_weights) const override
Computes metric value from SimDataPair object.
RQ4Metric * clone() const override
Implementation of relative difference metric.
double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > uncertainties, std::vector< double > weight_factors) const override
Computes metric value from data arrays.
RelativeDifferenceMetric * clone() const override
Holds pair of simulation/experimental data to fit.
Definition: SimDataPair.h:26