MDMC.refinement.FoM package
Submodules
MDMC.refinement.FoM.ChiSquared_experror module
The class for Chi Squared figure of merit calculation with errors
- class MDMC.refinement.FoM.ChiSquared_experror.ChiSquaredExpError(obs_pairs: list[ObservablePair], norm: str = 'data_points', n_parameters: int = None)[source]
Bases:
FigureOfMerit
Calculates the figure of merit as a sum of the square difference between data points for a single pair of observables, normalised by the errors and the number of data points, i.e. the reduced chi-squared.
Please see the documentation page explanation/figure-of-merit for mathematical details.
- calculate_single_FoM(obs_pair: ObservablePair)[source]
Calculates the chi-squared figure of merit for a single pair of observables, potentially rescaled if the experimental observable is not on an absolute scale.
- Parameters:
obs_pair (ObservablePair) – An
ObservablePair
for which the FoM is calculated- Returns:
The FoM for the obs_pair
- Return type:
MDMC.refinement.FoM.FoM_abs module
A module for Figure of Merits
- class MDMC.refinement.FoM.FoM_abs.FigureOfMerit(obs_pairs: list[ObservablePair], norm: str = 'data_points', n_parameters: int = None)[source]
Bases:
ABC
Abstract class that defines methods common to all figure of merit calculators
- Parameters:
obs_pairs (list) – A list of
ObservablePairs
norm ({'data_points', 'dof', 'none'}, optional) – What method of normalisation to use when calculating the FoM for a single dataset. Default is ‘data_points’.
n_parameters (int, optional) – The number of parameters being refined. Optional if
norm
is either ‘data_points’ or ‘none’, but required when ‘dof’. Default is None.
- calculate() float [source]
Calculates the FoM value by calculating the FoM for every
ObservablePair
- Returns:
A non-negative float Figure of Merit
- Return type:
- Raises:
ValueError – If calculated value of Figure of Merit is negative
- abstract calculate_single_FoM(obs_pair: ObservablePair) float [source]
Performs the FoM calculation specific to each FoM
- Parameters:
obs_pair (ObservablePair) – An
ObservablePair
for which the FoM is calculated- Returns:
The FoM for the
obs_pair
- Return type:
- data_norm_factor(obs_pair: ObservablePair) int [source]
Calculates the normalisation factor for
obs_pair
. Ifself.norm
is True, then returns the number of data points less the number of refinement parameters if ‘dof’ normalisation was chosen, or just the number of data points for the default ‘data_points’ normalisation. Ifself.norm
is False, then returns 1.- Parameters:
obs_pair (ObservablePair) – An
ObservablePair
for which the normalisation factor is calculated- Returns:
The normalisation factor
- Return type:
- class MDMC.refinement.FoM.FoM_abs.ObservablePair(exp_obs: Observable, MD_obs: Observable, weight: float, rescale_factor: float = 1.0, auto_scale: bool = False)[source]
Bases:
object
Contains a pair of observables for calculating the FoM
Checks the validity of observables
- Parameters:
exp_obs (Observable) – An
Observable
withObservable.origin == 'experiment'
MD_obs (Observable) – An
Observable
withObservable.origin == 'MD'
weight (float) – The relative weight of this pair on a total FoM
rescale_factor (float, optional) – Factor applied to
exp_obs
when calculating the FoM to ensure it is on the same scale asMD_obs
. Default is 1..auto_scale (bool, optional) – If True,
rescale_factor
is set automatically to minimise the FoM for each step of the refinement, overriding a user specified value if set. Note that this process is purely statistical and does not account for physical effects that might impact the scaling. Default is False.
- property MD_obs: Observable
Get or set the MD
Observable
Setting the
Observable
checks its validity- Returns:
The MD
Observable
- Return type:
- calculate_difference() ndarray [source]
Assumes a single dependent variable for each
Observable
- Returns:
An array with the same dimensions as the
dependent_variables
of theexp_obs
andMD_obs
. The array contains the difference between thedependent_variables
taking therescale_factor
into account.- Return type:
- calculate_errors() ndarray [source]
Assumes a single dependent variable error for each
Observable
- Returns:
An array with the same dimensions as the
errors
of theexp_obs
andMD_obs
. The array contains the combination of theerrors
in quadrature, taking therescale_factor
into account.- Return type:
- calculate_exp_errors() ndarray [source]
Assumes a single dependent variable error for each
Observable
. Calculates only the experimental errors.- Returns:
An array with the same dimensions as the
errors
of theexp_obs
, taking therescale_factor
into account.- Return type:
- check_dep_var() None [source]
Checks that
Observable
objects have the samedependent_variables
and that are finite
- check_errors() None [source]
Checks that an
Observable
has errors on thedependent_variable
and that these are float and not NaN
- check_indep_var() None [source]
Checks that
Observable
objects have the sameindependent_variables
and that are finite
- check_origin(origin: str) None [source]
Checks that the
Observable.origin
is correct- Parameters:
origin (str) – A string consisting of either
'experiment'
or'MD'
- property exp_obs: Observable
Get or set the experimental
Observable
Setting the
Observable
checks its validity- Returns:
The experimental
Observable
- Return type:
- property n_averages: dict[str, int]
The number of separate, complete dependent variable calculations we have been able to perform for the
Observable
- Returns:
Each key represents a dependent variable, and the value is the number of times we have calculated it
- Return type:
- validate_obs(obs: Observable, origin: str) None [source]
Performs checks to test the validity of an
Observable
Tests that the
Observable.origin
is as expected. If theObservablePair
has anotherObservable
(i.e. the otherorigin
), then this tests that theindependent_variables
are identical, thedependent_variables
have the same shape, theerrors
have the same shape, and that theObservable
objects are of the same type.- Parameters:
obs (Observable) – The
Observable
to validateorigin (str) – The
Observable.origin
('experiment'
or'MD'
)
- Raises:
AssertionError – If the
Observable.origin
is not the same as theorigin
ParameterAssertionError – If
Observable
does not have identicalindependent_variables
to anyObservable
of the otherObservable.origin
that already exists in theObservablePair
AssertionError – If
Observable
does not have identicaldependent_variables
to anyObservable
of the otherObservable.origin
that already exists in theObservablePair
AssertionError – If
Observable
does not have identicalerrors
to anyObservable
of the otherObservable.origin
that already exists in theObservablePair
AssertionError – If
Observable
does not have identical type to anyObservable
of the otherObservable.origin
that already exists in theObservablePair
MDMC.refinement.FoM.FoM_factory module
Factory class for generating Figure of Merits And ObservablePair class fro defining the obseravble pairs used to calculate the Figure of Merit
- class MDMC.refinement.FoM.FoM_factory.FoMFactory[source]
Bases:
object
Provides a factory for creating a
Figure of Merit
also called FoM. Any FoM within the FoM folder can be created with a string of the class name, as long as it is a subclass ofFigureOfMerit
.
MDMC.refinement.FoM.RSquared_noneerror module
The class for R Squared figure of merit calculation with no errors
- class MDMC.refinement.FoM.RSquared_noneerror.RSquared_noneerror(obs_pairs: list[ObservablePair], norm: str = 'data_points', n_parameters: int = None)[source]
Bases:
FigureOfMerit
Calculates the weighted sum of the Figure of Merits for a number of datasets:
\[FoM_{total} = \frac{\sum_{i} FoM_{i}}{\sum_{i} w_{i}}\]Here the weighted Figure of Merit for the \(i\)-th dataset, \(FoM_{i}\), is given by the sum of the square difference between data points for a single
ObservablePair
, i.e. the reduced chi-squared:\[FoM_{i} = \frac{w_{i}}{\nu_{i}} \sum_{j} (D_{j}^{exp} - D_{j}^{sim})^2\]where the sum is over the \(N_{i}\) data points in the
ObservablePair
corresponding to the \(i\)-th dataset, and the normalisation factor \(\nu_{i}\) is either \(N_{i}\), \(N_{i} - M\) where \(M\) is the number of fitting parameters, or \(1\). \(w_{i}\) is an importance weighting assigned to the \(i\)-th dataset. \(D_{j}\) are the individual data points in the 1-D or 2-D array of the experimentalObservable
(\(exp\)) or simulatedObservable
(\(sim\)). Note that the subtraction and division over the arrays are element-wise. Note also that if the experimentalObservable
is not on an absolute scale, an additionalrescale_factor
can be specified (or automatically determined) by theObservablePair
to scale the experimental data points and errors by a simple linear scaling.- calculate_single_FoM(obs_pair: ObservablePair)[source]
Performs the square difference for an
ObservablePair
Ifobs_pair.auto_scale
is True, then this will also setobs_pair.rescale
to the value which minimizes the FoM. If we labelrescale_factor
\(=\lambda\) then the minimum of the FoM is obtained as:\[\begin{split}FoM_{i}(\lambda) &=& w_{i} \sum_{j} \left(\lambda*D_{j}^{exp} - D_{j}^{sim}\right)^2 \\\\ \left. \frac{dFoM_{i}}{d\lambda}\right|_{\lambda=\lambda_{min}} &=& 0 \\\\ \lambda_{min} &=& \frac{A}{B} \\\\\end{split}\]where we have:
\[\begin{split}A &=& \sum_{j} D_{j}^{exp}*D_{j}^{sim} \\\\ B &=& \sum_{j}\left(D_{j}^{exp}\right)^2\end{split}\]- Parameters:
obs_pair (ObservablePair) – An
ObservablePair
for which the FoM is calculated- Returns:
The FoM for the obs_pair
- Return type:
Module contents
A module for Figure of Merit calculation