BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
meanRelativeDifferenceMetric Class Reference

Description

Implementation of relative difference metric. With default L2 norm and weighting off corresponds to the formula.

\[Result = \sum \frac{(I - D)^2}{(I + D)^2}\]

where $I$ is the simulated intensity, $D$ - experimental data. If weighting is on, falls back to the standard $\chi^2$ metric.

Definition at line 168 of file ObjectiveMetric.h.

Inheritance diagram for meanRelativeDifferenceMetric:
[legend]
Collaboration diagram for meanRelativeDifferenceMetric:
[legend]

Public Member Functions

 meanRelativeDifferenceMetric ()
 
meanRelativeDifferenceMetricclone () const override
 
virtual double compute (const SimDataPair &data_pair, bool use_weights) const
 Computes metric value from SimDataPair object. Calls computeFromArrays internally. More...
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > exp_stdv, std::vector< double > weight_factors) const override
 Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors and uncertainties. All arrays involved in the computation must be of the same size. More...
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > exp_stdv, std::vector< double > weight_factors) const override
 Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors and uncertainties. All arrays involved in the computation must be of the same size. More...
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > weight_factors) const override
 Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors. All arrays involved in the computation must be of the same size. More...
 
double computeFromArrays (std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > weight_factors) const override
 Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors. All arrays involved in the computation must be of the same size. More...
 
auto norm () const
 Returns a copy of the normalization function used. More...
 
void setNorm (std::function< double(double)> norm)
 
virtual void transferToCPP ()
 Used for Python overriding of clone (see swig/tweaks.py) More...
 

Private Attributes

std::function< double(double)> m_norm
 

Constructor & Destructor Documentation

◆ meanRelativeDifferenceMetric()

meanRelativeDifferenceMetric::meanRelativeDifferenceMetric ( )

Definition at line 209 of file ObjectiveMetric.cpp.

210  : Chi2Metric()
211 {
212 }

Member Function Documentation

◆ clone()

meanRelativeDifferenceMetric * meanRelativeDifferenceMetric::clone ( ) const
overridevirtual

Reimplemented from Chi2Metric.

Definition at line 214 of file ObjectiveMetric.cpp.

215 {
216  return copyMetric(*this);
217 }

◆ compute()

double ObjectiveMetric::compute ( const SimDataPair data_pair,
bool  use_weights 
) const
virtualinherited

Computes metric value from SimDataPair object. Calls computeFromArrays internally.

Parameters
data_pairSimDataPair object. Can optionally contain data uncertainties
use_weightsboolean, defines if data uncertainties should be taken into account

Reimplemented in RQ4Metric.

Definition at line 66 of file ObjectiveMetric.cpp.

67 {
68  if (use_weights && !data_pair.containsUncertainties())
69  throw std::runtime_error("Error in ObjectiveMetric::compute: the metric is weighted, but "
70  "the simulation-data pair does not contain uncertainties");
71 
72  if (use_weights)
73  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
74  data_pair.uncertainties_array(), data_pair.user_weights_array());
75  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
76  data_pair.user_weights_array());
77 }
virtual double computeFromArrays(std::vector< double > sim_data, std::vector< double > exp_data, std::vector< double > exp_stdv, std::vector< double > weight_factors) const =0
Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positi...
std::vector< double > experimental_array() const
Returns the flattened experimental data cut to the ROI area.
std::vector< double > user_weights_array() const
Returns a flat array of user weights cut to the ROI area.
std::vector< double > uncertainties_array() const
Returns the flattened experimental uncertainties cut to the ROI area. If no uncertainties are availab...
std::vector< double > simulation_array() const
Returns the flattened simulated intensities cut to the ROI area.
bool containsUncertainties() const

References ObjectiveMetric::computeFromArrays(), SimDataPair::containsUncertainties(), SimDataPair::experimental_array(), SimDataPair::simulation_array(), SimDataPair::uncertainties_array(), and SimDataPair::user_weights_array().

Referenced by RQ4Metric::compute().

Here is the call graph for this function:

◆ computeFromArrays() [1/4]

double Chi2Metric::computeFromArrays ( std::vector< double >  sim_data,
std::vector< double >  exp_data,
std::vector< double >  exp_stdv,
std::vector< double >  weight_factors 
) const
overridevirtualinherited

Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors and uncertainties. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
exp_stdvarray with experimental data uncertainties.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Implements ObjectiveMetric.

Definition at line 96 of file ObjectiveMetric.cpp.

99 {
100  checkIntegrity(sim_data, exp_data, exp_stdv, weight_factors);
101 
102  double result = 0.0;
103  auto norm_fun = norm();
104  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
105  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0 && exp_stdv[i] > 0.0)
106  result += norm_fun((exp_data[i] - sim_data[i]) / exp_stdv[i]) * weight_factors[i];
107 
108  return std::isfinite(result) ? result : double_max;
109 }
auto norm() const
Returns a copy of the normalization function used.

Referenced by RQ4Metric::compute().

◆ computeFromArrays() [2/4]

double Chi2Metric::computeFromArrays
override

Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors and uncertainties. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
exp_stdvarray with experimental data uncertainties.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Definition at line 90 of file ObjectiveMetric.cpp.

99 {
100  checkIntegrity(sim_data, exp_data, exp_stdv, weight_factors);
101 
102  double result = 0.0;
103  auto norm_fun = norm();
104  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
105  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0 && exp_stdv[i] > 0.0)
106  result += norm_fun((exp_data[i] - sim_data[i]) / exp_stdv[i]) * weight_factors[i];
107 
108  return std::isfinite(result) ? result : double_max;
109 }

◆ computeFromArrays() [3/4]

double Chi2Metric::computeFromArrays
override

Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Definition at line 101 of file ObjectiveMetric.cpp.

113 {
114  checkIntegrity(sim_data, exp_data, weight_factors);
115 
116  auto norm_fun = norm();
117  double result = 0.0;
118  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
119  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0)
120  result += norm_fun(exp_data[i] - sim_data[i]) * weight_factors[i];
121 
122  return std::isfinite(result) ? result : double_max;
123 }

◆ computeFromArrays() [4/4]

double meanRelativeDifferenceMetric::computeFromArrays ( std::vector< double >  sim_data,
std::vector< double >  exp_data,
std::vector< double >  weight_factors 
) const
overridevirtual

Computes metric value from data arrays. Negative values in exp_data are ignored as well as non-positive weight_factors. All arrays involved in the computation must be of the same size.

Parameters
sim_dataarray with simulated intensities.
exp_dataarray with intensity values obtained from an experiment.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Reimplemented from Chi2Metric.

Definition at line 219 of file ObjectiveMetric.cpp.

222 {
223  checkIntegrity(sim_data, exp_data, weight_factors);
224 
225  double result = 0.0;
226  auto norm_fun = norm();
227  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i) {
228  if (weight_factors[i] <= 0.0 || exp_data[i] < 0.0)
229  continue;
230  const double sim_val = std::max(double_min, sim_data[i]);
231  const double exp_val = std::max(double_min, exp_data[i]);
232  result += norm_fun((exp_val - sim_val) / (exp_val + sim_val)) * weight_factors[i];
233  }
234 
235  return std::isfinite(result) ? result : double_max;
236 }

References ObjectiveMetric::norm().

Here is the call graph for this function:

◆ norm()

auto ObjectiveMetric::norm ( ) const
inlineinherited

Returns a copy of the normalization function used.

Definition at line 67 of file ObjectiveMetric.h.

67 { return m_norm; }
std::function< double(double)> m_norm

References ObjectiveMetric::m_norm.

Referenced by LogMetric::computeFromArrays(), Chi2Metric::computeFromArrays(), PoissonLikeMetric::computeFromArrays(), computeFromArrays(), and ObjectiveMetric::setNorm().

◆ setNorm()

void ObjectiveMetric::setNorm ( std::function< double(double)>  norm)
inherited

Definition at line 79 of file ObjectiveMetric.cpp.

80 {
81  m_norm = std::move(norm);
82 }

References ObjectiveMetric::m_norm, and ObjectiveMetric::norm().

Here is the call graph for this function:

◆ transferToCPP()

virtual void ICloneable::transferToCPP ( )
inlinevirtualinherited

Used for Python overriding of clone (see swig/tweaks.py)

Definition at line 32 of file ICloneable.h.

Member Data Documentation

◆ m_norm

std::function<double(double)> ObjectiveMetric::m_norm
privateinherited

Definition at line 70 of file ObjectiveMetric.h.

Referenced by ObjectiveMetric::norm(), and ObjectiveMetric::setNorm().


The documentation for this class was generated from the following files: