uravu.relationship

The Relationship class is a foundational component of the uravu package, and acts as the main API for use of the package. This class enables the storage of the relationship between the model and the data.

Objects of this class offer easy methods to perform maximum likelihood evaluation, Markiv chain Monte Carlo (MCMC) for posterior probabiltiy determination and Bayesian evidence estimation by nested sampling.

See the tutorials online for more guidence of how to use this package.

class uravu.relationship.Relationship(function: Callable, abscissa: Union[List[float], numpy.ndarray], ordinate: Union[List[Union[uravu.distribution.Distribution, scipy.stats._distn_infrastructure.rv_frozen, float]], numpy.ndarray], bounds: Tuple[Tuple[float, float]] = None, ordinate_error: Union[List[float], numpy.ndarray] = None)[source]

Bases: object

The Relationship class is the base of the uravu package, enabling the use of Bayesian inference for the assessment of a model’s ability to describe some data.

Attributes: :param function (callable): The function that is modelled. :param abscissa (array_like): The abscissa data that the modelling should be performed on. :param ordinate (list or uravu.distribution.Distribution or array_like): The ordinate data against with the model should be compared. This should be an list or uravu.distribution.Distribution unless a ordinate_error is given. :param variables (list of uravu.distribution.Distribution): Variables in the function.

bounds (tuple): The minimum and maximum values for each parameters. ln_evidence (uncertainties.core.Variable): The natural-log of the Bayesian evidence for the model to the given data. mcmc_results (dict): The results from emcee.EnsembleSampler.run_mcmc() sampling. nested_sampling_results (dict): The results from dynesty.NestedSampler.run_nested() nested sampling.
Parameters:
  • function – The functional relationship to be modelled.
  • abscissa – The abscissa data. If multi-dimensional, the array is expected to have the shape (N, d), where N is the number of data points and d is the dimensionality.
  • ordinate – The ordinate data. This should have a shape (N,).
  • bounds – The minimum and maximum values for each parameters. Defaults to None.
  • ordinate_error – The uncertainty in the ordinate, this should be the standard error in the measurement. Only used if ordinate is not a list or uravu.distribution.Distribution. Defaults to None.
bayesian_information_criteria()[source]

Calculate the Bayesian information criteria for the relationship.

Returns:Bayesian information criteria.
Return type:float
flatchain

Sampling flatchain.

Type:return
get_sample(i)[source]

Return the variable values for a given sample.

Parameters:i (int) – The sample index.
Returns:Variable values at given index.
Return type:list of float
len_parameters

Determine the number of variables in the assessment function.

Returns:py:attr`int`: Number of variables.
max_likelihood(method, x0=None, **kwargs)[source]

Determine values for the variables which maximise the likelihood for the Relationship. For keyword arguments see the scipy.optimize.minimize() documentation.

Parameters:x0 (array_like) – Initial guess values for the parameters.
mcmc(prior_function=None, walkers=50, n_samples=500, n_burn=500, progress=True)[source]

Perform MCMC to get the posterior probability distributions for the variables of the relationship. Note, running this method will populate the variables attribute with Distribution objects. Once run, a result dictionary containing the distributions, chain, and samples from emcee is piped into the class variable mcmc_results.

Parameters:
  • prior_function (callable, optional) – The function to populated some prior distributions. Default is the broad uniform priors in prior().
  • walkers (int, optional) – Number of MCMC walkers. Default is 50.
  • n_samples (int, optional) – Number of sample points. Default is :py:attr:500`.
  • n_burn (int, optional) – Number of burn in samples. Default is 500.
  • progress (bool, optional) – Show tqdm progress for sampling. Default is True.
mcmc_done

Has MCMC been performed? Determined based on the type of the variables.

Returns:Has MCMC been performed?
Return type:bool
nested_sampling(prior_function=None, progress=True, dynamic=False, **kwargs)[source]

Perform nested sampling, or dynamic nested sampling, to determine the Bayesian natural-log evidence. For keyword arguments see the dynesty.NestedSampler.run_nested() documentation. Once run, the result dictionary produced by dynesty.NestedSampler.run_nested() is piped into the class variable nested_sampling_results.

Parameters:
  • prior_function (callable, optional) – The function to populate some prior distributions. Default is the broad uniform priors in prior().
  • progress (bool, optional) – Show tqdm progress for sampling. Default is True.
nested_sampling_done

Has nested sampling been performed? Determined based on if the ln_evidence has a value.

Returns:Has nested sampling been performed?
Return type:bool
prior()[source]

Standard priors for the relationship. These priors are broad, uninformative, for normal variables running the range [x - x * 10, x + x * 10) (where x is the variable value).

Returns:scipy.stats functions describing the priors.
Return type:list of scipy.stats.rv_continuous
variable_medians

The median values for each of the variables.

Returns:Variable medians.
Return type:array_like
variable_modes

The mode values for each of the variables.

Returns:Variable modes.
Return type:array_like
x

Abscissa values.

Returns:Abscissa values.
Return type:array_like
y

Ordinate values.

Returns:Ordinate values.
Return type:array_like