BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PoissonLikeMetric Class Reference

Implementation of $ \chi^2 $ metric with standard deviation $\sigma = max(\sqrt{I}, 1)$, where $I$ is the simulated intensity. More...

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

Public Member Functions

 PoissonLikeMetric ()
 
PoissonLikeMetricclone () const override
 
virtual double compute (const SimDataPair &data_pair, bool use_weights) const
 Computes metric value from SimDataPair object. More...
 
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. More...
 
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. 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. 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. 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
 

Detailed Description

Implementation of $ \chi^2 $ metric with standard deviation $\sigma = max(\sqrt{I}, 1)$, where $I$ is the simulated intensity.

With default L2 norm corresponds to the formula

\[\chi^2 = \sum \frac{(I - D)^2}{max(I, 1)}\]

for unweighted experimental data. Falls to standard Chi2Metric when data uncertainties are taken into account.

Definition at line 112 of file ObjectiveMetric.h.

Constructor & Destructor Documentation

◆ PoissonLikeMetric()

PoissonLikeMetric::PoissonLikeMetric ( )

Definition at line 119 of file ObjectiveMetric.cpp.

119 : Chi2Metric() {}

Member Function Documentation

◆ clone()

PoissonLikeMetric * PoissonLikeMetric::clone ( ) const
overridevirtual

Reimplemented from Chi2Metric.

Definition at line 121 of file ObjectiveMetric.cpp.

122 {
123  return copyMetric(*this);
124 }

◆ 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 60 of file ObjectiveMetric.cpp.

61 {
62  if (use_weights && !data_pair.containsUncertainties())
63  throw std::runtime_error("Error in ObjectiveMetric::compute: the metric is weighted, but "
64  "the simulation-data pair does not contain uncertainties");
65 
66  if (use_weights)
67  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
68  data_pair.uncertainties_array(), data_pair.user_weights_array());
69  else
70  return computeFromArrays(data_pair.simulation_array(), data_pair.experimental_array(),
71  data_pair.user_weights_array());
72 }
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::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.
std::vector< double > simulation_array() const
Returns the flattened simulated intensities cut to the ROI area.
bool containsUncertainties() const
Definition: SimDataPair.cpp:86

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 >  uncertainties,
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.
uncertaintiesarray with experimental data uncertainties.
weight_factorsuser-defined weighting factors. Used linearly, no matter which norm is chosen.

Implements ObjectiveMetric.

Definition at line 88 of file ObjectiveMetric.cpp.

91 {
92  checkIntegrity(sim_data, exp_data, uncertainties, weight_factors);
93 
94  double result = 0.0;
95  auto norm_fun = norm();
96  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
97  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0 && uncertainties[i] > 0.0)
98  result += norm_fun((exp_data[i] - sim_data[i]) / uncertainties[i]) * weight_factors[i];
99 
100  return std::isfinite(result) ? result : double_max;
101 }
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.
uncertaintiesarray 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.

91 {
92  checkIntegrity(sim_data, exp_data, uncertainties, weight_factors);
93 
94  double result = 0.0;
95  auto norm_fun = norm();
96  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
97  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0 && uncertainties[i] > 0.0)
98  result += norm_fun((exp_data[i] - sim_data[i]) / uncertainties[i]) * weight_factors[i];
99 
100  return std::isfinite(result) ? result : double_max;
101 }

References ObjectiveMetric::norm().

Here is the call graph for this function:

◆ 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.

105 {
106  checkIntegrity(sim_data, exp_data, weight_factors);
107 
108  auto norm_fun = norm();
109  double result = 0.0;
110  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i)
111  if (exp_data[i] >= 0.0 && weight_factors[i] > 0.0)
112  result += norm_fun(exp_data[i] - sim_data[i]) * weight_factors[i];
113 
114  return std::isfinite(result) ? result : double_max;
115 }

◆ computeFromArrays() [4/4]

double PoissonLikeMetric::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 126 of file ObjectiveMetric.cpp.

129 {
130  checkIntegrity(sim_data, exp_data, weight_factors);
131 
132  double result = 0.0;
133  auto norm_fun = norm();
134  for (size_t i = 0, sim_size = sim_data.size(); i < sim_size; ++i) {
135  if (weight_factors[i] <= 0.0 || exp_data[i] < 0.0)
136  continue;
137  const double variance = std::max(1.0, sim_data[i]);
138  const double value = (sim_data[i] - exp_data[i]) / std::sqrt(variance);
139  result += norm_fun(value) * weight_factors[i];
140  }
141 
142  return std::isfinite(result) ? result : double_max;
143 }

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 computeFromArrays(), LogMetric::computeFromArrays(), Chi2Metric::computeFromArrays(), RelativeDifferenceMetric::computeFromArrays(), and ObjectiveMetric::setNorm().

◆ setNorm()

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

Definition at line 74 of file ObjectiveMetric.cpp.

75 {
76  m_norm = std::move(norm);
77 }

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 34 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: