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

Description

Interface for one-dimensional distributions.

Definition at line 33 of file Distributions.h.

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

Public Member Functions

 IDistribution1D (const std::vector< double > &PValues)
 
void checkNodeArgs () const
 Raises exception if a parameter value is invalid. More...
 
virtual std::string className () const =0
 Returns the class name, to be hard-coded in each leaf class that inherits from INode. More...
 
IDistribution1Dclone () const override=0
 
virtual std::vector< double > equidistantPoints (size_t nbr_samples, double sigma_factor, const RealLimits &limits=RealLimits()) const =0
 Returns equidistant interpolation points, with range computed in distribution-specific way from mean and width parameter, taking into account limits and sigma_factor. More...
 
virtual std::vector< double > equidistantPointsInRange (size_t nbr_samples, double xmin, double xmax) const
 Returns equidistant interpolation points from xmin to xmax. More...
 
std::vector< ParameterSampleequidistantSamples (size_t nbr_samples, double sigma_factor=0., const RealLimits &limits=RealLimits()) const
 Returns equidistant samples, using intrinsic parameters, weighted with probabilityDensity(). More...
 
std::vector< ParameterSampleequidistantSamplesInRange (size_t nbr_samples, double xmin, double xmax) const
 Returns equidistant samples from xmin to xmax, weighted with probabilityDensity(). More...
 
virtual bool isDelta () const =0
 Returns true if the distribution is in the limit case of a Dirac delta distribution. More...
 
virtual double mean () const =0
 Returns the distribution-specific mean. More...
 
virtual std::vector< const INode * > nodeChildren () const
 Returns all children. More...
 
std::vector< const INode * > nodeOffspring () const
 Returns all descendants. More...
 
virtual std::vector< ParaMetaparDefs () const
 Returns the parameter definitions, to be hard-coded in each leaf class. More...
 
virtual double probabilityDensity (double x) const =0
 Returns the distribution-specific probability density for value x. More...
 
virtual std::string pythonConstructor (const std::string &units) const =0
 Prints distribution with constructor parameters in given units. ba.DistributionGaussian(2.0*deg, 0.02*deg) More...
 
virtual void transferToCPP ()
 Used for Python overriding of clone (see swig/tweaks.py) More...
 

Protected Member Functions

void adjustMinMaxForLimits (double &xmin, double &xmax, const RealLimits &limits) const
 modifies xmin and xmax if they are outside of limits More...
 
std::vector< ParameterSamplegenerateSamplesFromValues (const std::vector< double > &sample_values) const
 Returns weighted samples from given interpolation points and probabilityDensity(). More...
 

Protected Attributes

std::vector< double > m_P
 

Constructor & Destructor Documentation

◆ IDistribution1D()

IDistribution1D::IDistribution1D ( const std::vector< double > &  PValues)

Definition at line 36 of file Distributions.cpp.

37  : INode(PValues)
38 {
39 }
INode()=default

Member Function Documentation

◆ adjustMinMaxForLimits()

void IDistribution1D::adjustMinMaxForLimits ( double &  xmin,
double &  xmax,
const RealLimits limits 
) const
protected

modifies xmin and xmax if they are outside of limits

Definition at line 81 of file Distributions.cpp.

83 {
84  if (limits.hasLowerLimit() && xmin < limits.lowerLimit())
85  xmin = limits.lowerLimit();
86  if (limits.hasUpperLimit() && xmax > limits.upperLimit())
87  xmax = limits.upperLimit();
88  if (xmin > xmax) {
89  std::ostringstream ostr;
90  ostr << "IDistribution1D::adjustMinMaxForLimits() -> Error. Can't' adjust ";
91  ostr << "xmin:" << xmin << " xmax:" << xmax << " for given limits " << limits << std::endl;
92  throw std::runtime_error(ostr.str());
93  }
94 }
bool hasUpperLimit() const
if has upper limit
Definition: RealLimits.cpp:66
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:71
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:49
bool hasLowerLimit() const
if has lower limit
Definition: RealLimits.cpp:44

References RealLimits::hasLowerLimit(), RealLimits::hasUpperLimit(), RealLimits::lowerLimit(), and RealLimits::upperLimit().

Referenced by DistributionGate::equidistantPoints(), DistributionLorentz::equidistantPoints(), DistributionGaussian::equidistantPoints(), DistributionLogNormal::equidistantPoints(), DistributionCosine::equidistantPoints(), and DistributionTrapezoid::equidistantPoints().

Here is the call graph for this function:

◆ checkNodeArgs()

void INode::checkNodeArgs ( ) const
inherited

Raises exception if a parameter value is invalid.

Definition at line 27 of file INode.cpp.

28 {
29  size_t nP = m_P.size();
30  if (parDefs().size() != nP) {
31  std::cerr << "BUG in class " << className() << std::endl;
32  std::cerr << "#m_P = " << nP << std::endl;
33  std::cerr << "#PDf = " << parDefs().size() << std::endl;
34  for (const ParaMeta& pm : parDefs())
35  std::cerr << " PDf: " << pm.name << std::endl;
36  ASSERT(0);
37  }
38  ASSERT(parDefs().size() == nP);
39  for (size_t i = 0; i < nP; ++i) {
40  const ParaMeta pm = parDefs()[i];
41 
43  if (pm.vMin == -INF) {
44  ASSERT(pm.vMax == +INF);
45  // nothing to do
46  } else if (pm.vMax == +INF) {
47  ASSERT(pm.vMin == 0);
48  limits = RealLimits::nonnegative();
49  } else {
50  limits = RealLimits::limited(pm.vMin, pm.vMax);
51  }
52  limits.check(pm.name, m_P[i]);
53  }
54 }
#define ASSERT(condition)
Definition: Assert.h:45
const double INF
Definition: INode.h:26
virtual std::vector< ParaMeta > parDefs() const
Returns the parameter definitions, to be hard-coded in each leaf class.
Definition: INode.h:51
std::vector< double > m_P
Definition: INode.h:63
virtual std::string className() const =0
Returns the class name, to be hard-coded in each leaf class that inherits from INode.
Limits for a real fit parameter.
Definition: RealLimits.h:24
static RealLimits limitless()
Creates an object without bounds (default)
Definition: RealLimits.cpp:139
void check(const std::string &name, double value) const
Throws if value is outside limits. Parameter 'name' is for exception message.
Definition: RealLimits.cpp:170
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:124
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:134
Metadata of one model parameter.
Definition: INode.h:29
double vMin
Definition: INode.h:33
double vMax
Definition: INode.h:34
std::string name
Definition: INode.h:30

References ASSERT, RealLimits::check(), INode::className(), INF, RealLimits::limited(), RealLimits::limitless(), INode::m_P, ParaMeta::name, RealLimits::nonnegative(), INode::parDefs(), ParaMeta::vMax, and ParaMeta::vMin.

Referenced by BarGauss::BarGauss(), BarLorentz::BarLorentz(), Bipyramid4::Bipyramid4(), Box::Box(), CantellatedCube::CantellatedCube(), Cone::Cone(), ConstantBackground::ConstantBackground(), CosineRippleBox::CosineRippleBox(), CosineRippleGauss::CosineRippleGauss(), CosineRippleLorentz::CosineRippleLorentz(), Cylinder::Cylinder(), DistributionCosine::DistributionCosine(), DistributionGate::DistributionGate(), DistributionGaussian::DistributionGaussian(), DistributionLogNormal::DistributionLogNormal(), DistributionLorentz::DistributionLorentz(), DistributionTrapezoid::DistributionTrapezoid(), Dodecahedron::Dodecahedron(), EllipsoidalCylinder::EllipsoidalCylinder(), FootprintGauss::FootprintGauss(), FootprintSquare::FootprintSquare(), FuzzySphere::FuzzySphere(), GaussSphere::GaussSphere(), HemiEllipsoid::HemiEllipsoid(), HollowSphere::HollowSphere(), HorizontalCylinder::HorizontalCylinder(), Icosahedron::Icosahedron(), LongBoxGauss::LongBoxGauss(), LongBoxLorentz::LongBoxLorentz(), PlatonicOctahedron::PlatonicOctahedron(), PlatonicTetrahedron::PlatonicTetrahedron(), Prism3::Prism3(), Prism6::Prism6(), Profile1DCauchy::Profile1DCauchy(), Profile1DCosine::Profile1DCosine(), Profile1DGate::Profile1DGate(), Profile1DGauss::Profile1DGauss(), Profile1DTriangle::Profile1DTriangle(), Profile1DVoigt::Profile1DVoigt(), Profile2DCauchy::Profile2DCauchy(), Profile2DCone::Profile2DCone(), Profile2DGate::Profile2DGate(), Profile2DGauss::Profile2DGauss(), Profile2DVoigt::Profile2DVoigt(), Pyramid2::Pyramid2(), Pyramid3::Pyramid3(), Pyramid4::Pyramid4(), Pyramid6::Pyramid6(), RotationEuler::RotationEuler(), RotationX::RotationX(), RotationY::RotationY(), RotationZ::RotationZ(), SawtoothRippleBox::SawtoothRippleBox(), SawtoothRippleGauss::SawtoothRippleGauss(), SawtoothRippleLorentz::SawtoothRippleLorentz(), Sphere::Sphere(), Spheroid::Spheroid(), TruncatedCube::TruncatedCube(), TruncatedSphere::TruncatedSphere(), and TruncatedSpheroid::TruncatedSpheroid().

Here is the call graph for this function:

◆ className()

virtual std::string INode::className ( ) const
pure virtualinherited

Returns the class name, to be hard-coded in each leaf class that inherits from INode.

Implemented in Particle, SphericalDetector, SpecularSimulation, ScatteringSimulation, OffspecSimulation, DepthProbeSimulation, PoissonBackground, ConstantBackground, GaussSphere, FuzzySphere, RotationEuler, RotationZ, RotationY, RotationX, IdentityRotation, ParticleCoreShell, ParticleComposition, MesoCrystal, Crystal, MultiLayer, Layer, Lattice3D, HexagonalLattice2D, SquareLattice2D, BasicLattice2D, LayerRoughness, LayerInterface, TruncatedSpheroid, TruncatedSphere, TruncatedCube, Spheroid, Sphere, SawtoothRippleLorentz, SawtoothRippleGauss, SawtoothRippleBox, Pyramid6, Pyramid4, Pyramid3, Pyramid2, Prism6, Prism3, PlatonicTetrahedron, PlatonicOctahedron, LongBoxLorentz, LongBoxGauss, Icosahedron, HorizontalCylinder, HollowSphere, HemiEllipsoid, EllipsoidalCylinder, Dodecahedron, Cylinder, CosineRippleLorentz, CosineRippleGauss, CosineRippleBox, Cone, CantellatedCube, Box, Bipyramid4, BarLorentz, BarGauss, Profile2DVoigt, Profile2DCone, Profile2DGate, Profile2DGauss, Profile2DCauchy, Profile1DVoigt, Profile1DCosine, Profile1DTriangle, Profile1DGate, Profile1DGauss, Profile1DCauchy, MisesGaussPeakShape, MisesFisherGaussPeakShape, LorentzFisherPeakShape, GaussFisherPeakShape, IsotropicLorentzPeakShape, IsotropicGaussPeakShape, ParticleLayout, InterferenceTwin, InterferenceRadialParaCrystal, InterferenceNone, InterferenceHardDisk, InterferenceFinite3DLattice, InterferenceFinite2DLattice, Interference3DLattice, Interference2DSuperLattice, Interference2DParaCrystal, Interference2DLattice, Interference1DLattice, DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, DistributionGate, ResolutionFunction2DGaussian, ConvolutionDetectorResolution, PolFilter, RectangularDetector, FootprintSquare, FootprintGauss, and Beam.

Referenced by INode::checkNodeArgs(), ExemplarySamples::createBasic2DParaCrystalWithFTDis(), IProfile1D::pythonConstructor(), IProfile2D::pythonConstructor(), IFormFactor::pythonConstructor(), and IFormFactor::shapeName().

◆ clone()

◆ equidistantPoints()

virtual std::vector<double> IDistribution1D::equidistantPoints ( size_t  nbr_samples,
double  sigma_factor,
const RealLimits limits = RealLimits() 
) const
pure virtual

Returns equidistant interpolation points, with range computed in distribution-specific way from mean and width parameter, taking into account limits and sigma_factor.

Implemented in DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, and DistributionGate.

Referenced by equidistantSamples().

◆ equidistantPointsInRange()

std::vector< double > IDistribution1D::equidistantPointsInRange ( size_t  nbr_samples,
double  xmin,
double  xmax 
) const
virtual

Returns equidistant interpolation points from xmin to xmax.

Definition at line 70 of file Distributions.cpp.

72 {
73  if (nbr_samples < 2 || DoubleEqual(xmin, xmax))
74  return {mean()};
75  std::vector<double> result(nbr_samples);
76  for (size_t i = 0; i < nbr_samples; ++i)
77  result[i] = xmin + i * (xmax - xmin) / (nbr_samples - 1.0);
78  return result;
79 }
virtual double mean() const =0
Returns the distribution-specific mean.

References mean().

Referenced by DistributionGate::equidistantPoints(), DistributionLorentz::equidistantPoints(), DistributionGaussian::equidistantPoints(), DistributionLogNormal::equidistantPoints(), DistributionCosine::equidistantPoints(), DistributionTrapezoid::equidistantPoints(), and equidistantSamplesInRange().

Here is the call graph for this function:

◆ equidistantSamples()

std::vector< ParameterSample > IDistribution1D::equidistantSamples ( size_t  nbr_samples,
double  sigma_factor = 0.,
const RealLimits limits = RealLimits() 
) const

Returns equidistant samples, using intrinsic parameters, weighted with probabilityDensity().

Definition at line 43 of file Distributions.cpp.

46 {
47  if (nbr_samples == 0)
48  throw std::runtime_error("IDistribution1D::generateSamples: "
49  "number of generated samples must be bigger than zero");
50  if (isDelta())
51  return {ParameterSample(mean())};
52  return generateSamplesFromValues(equidistantPoints(nbr_samples, sigma_factor, limits));
53 }
std::vector< ParameterSample > generateSamplesFromValues(const std::vector< double > &sample_values) const
Returns weighted samples from given interpolation points and probabilityDensity().
virtual bool isDelta() const =0
Returns true if the distribution is in the limit case of a Dirac delta distribution.
virtual std::vector< double > equidistantPoints(size_t nbr_samples, double sigma_factor, const RealLimits &limits=RealLimits()) const =0
Returns equidistant interpolation points, with range computed in distribution-specific way from mean ...
A parameter value with a weight, as obtained when sampling from a distribution.

References equidistantPoints(), generateSamplesFromValues(), isDelta(), and mean().

Here is the call graph for this function:

◆ equidistantSamplesInRange()

std::vector< ParameterSample > IDistribution1D::equidistantSamplesInRange ( size_t  nbr_samples,
double  xmin,
double  xmax 
) const

Returns equidistant samples from xmin to xmax, weighted with probabilityDensity().

Definition at line 58 of file Distributions.cpp.

59 {
60  if (nbr_samples == 0)
61  throw std::runtime_error("IDistribution1D::generateSamples: "
62  "number of generated samples must be bigger than zero");
63  if (isDelta())
64  return {ParameterSample(mean())};
65  return generateSamplesFromValues(equidistantPointsInRange(nbr_samples, xmin, xmax));
66 }
virtual std::vector< double > equidistantPointsInRange(size_t nbr_samples, double xmin, double xmax) const
Returns equidistant interpolation points from xmin to xmax.

References equidistantPointsInRange(), generateSamplesFromValues(), isDelta(), and mean().

Here is the call graph for this function:

◆ generateSamplesFromValues()

std::vector< ParameterSample > IDistribution1D::generateSamplesFromValues ( const std::vector< double > &  sample_values) const
protected

Returns weighted samples from given interpolation points and probabilityDensity().

Definition at line 99 of file Distributions.cpp.

100 {
101  std::vector<ParameterSample> result;
102  double norm_factor = 0.0;
103  for (double value : sample_values) {
104  double pdf = probabilityDensity(value);
105  result.emplace_back(value, pdf);
106  norm_factor += pdf;
107  }
108  if (norm_factor <= 0.0)
109  throw std::runtime_error("IDistribution1D::generateSamples: "
110  "total probability must be bigger than zero");
111  for (ParameterSample& sample : result)
112  sample.weight /= norm_factor;
113  return result;
114 }
virtual double probabilityDensity(double x) const =0
Returns the distribution-specific probability density for value x.

References probabilityDensity().

Referenced by equidistantSamples(), and equidistantSamplesInRange().

Here is the call graph for this function:

◆ isDelta()

virtual bool IDistribution1D::isDelta ( ) const
pure virtual

Returns true if the distribution is in the limit case of a Dirac delta distribution.

Implemented in DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, and DistributionGate.

Referenced by equidistantSamples(), and equidistantSamplesInRange().

◆ mean()

◆ nodeChildren()

◆ nodeOffspring()

std::vector< const INode * > INode::nodeOffspring ( ) const
inherited

Returns all descendants.

Definition at line 61 of file INode.cpp.

62 {
63  std::vector<const INode*> result;
64  result.push_back(this);
65  for (const auto* child : nodeChildren()) {
66  for (const auto* p : child->nodeOffspring())
67  result.push_back(p);
68  }
69  return result;
70 }
virtual std::vector< const INode * > nodeChildren() const
Returns all children.
Definition: INode.cpp:56

References INode::nodeChildren().

Here is the call graph for this function:

◆ parDefs()

virtual std::vector<ParaMeta> INode::parDefs ( ) const
inlinevirtualinherited

Returns the parameter definitions, to be hard-coded in each leaf class.

Reimplemented in ConstantBackground, GaussSphere, FuzzySphere, RotationEuler, RotationZ, RotationY, RotationX, Crystal, Layer, HexagonalLattice2D, SquareLattice2D, BasicLattice2D, LayerRoughness, TruncatedSpheroid, TruncatedSphere, TruncatedCube, Spheroid, Sphere, SawtoothRippleLorentz, SawtoothRippleGauss, SawtoothRippleBox, Pyramid6, Pyramid4, Pyramid3, Pyramid2, Prism6, Prism3, PlatonicTetrahedron, PlatonicOctahedron, LongBoxLorentz, LongBoxGauss, Icosahedron, HorizontalCylinder, HollowSphere, HemiEllipsoid, EllipsoidalCylinder, Dodecahedron, Cylinder, CosineRippleLorentz, CosineRippleGauss, CosineRippleBox, Cone, CantellatedCube, Box, Bipyramid4, BarLorentz, BarGauss, Profile2DVoigt, Profile2DCone, Profile2DGate, Profile2DGauss, Profile2DCauchy, Profile1DVoigt, Profile1DCosine, Profile1DTriangle, Profile1DGate, Profile1DGauss, Profile1DCauchy, MisesGaussPeakShape, MisesFisherGaussPeakShape, LorentzFisherPeakShape, GaussFisherPeakShape, IsotropicLorentzPeakShape, IsotropicGaussPeakShape, ParticleLayout, InterferenceTwin, InterferenceRadialParaCrystal, InterferenceHardDisk, Interference2DSuperLattice, Interference2DParaCrystal, Interference1DLattice, DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, DistributionGate, ResolutionFunction2DGaussian, PolFilter, FootprintSquare, and FootprintGauss.

Definition at line 51 of file INode.h.

51 { return {}; }

Referenced by INode::checkNodeArgs(), and IFormFactor::pythonConstructor().

◆ probabilityDensity()

virtual double IDistribution1D::probabilityDensity ( double  x) const
pure virtual

Returns the distribution-specific probability density for value x.

Implemented in DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, and DistributionGate.

Referenced by generateSamplesFromValues().

◆ pythonConstructor()

virtual std::string IDistribution1D::pythonConstructor ( const std::string &  units) const
pure virtual

Prints distribution with constructor parameters in given units. ba.DistributionGaussian(2.0*deg, 0.02*deg)

Implemented in DistributionTrapezoid, DistributionCosine, DistributionLogNormal, DistributionGaussian, DistributionLorentz, and DistributionGate.

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


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