lezargus.library.uncertainty module#

Different mathematical operations which we also propagate uncertainty.

lezargus.library.uncertainty.add(augend: ndarray, addend: ndarray, augend_uncertainty: ndarray = None, addend_uncertainty: ndarray = None) tuple[numpy.ndarray, numpy.ndarray][source]#

Add two values and propagate uncertainties.

Parameters:
  • augend (ndarray) – The “left”-side of the addition.

  • addend (ndarray) – The “right”-side of the addition.

  • augend_uncertainty (ndarray, default = None) – The uncertainty on the augend term. If None, we assume that the uncertainty is 0.

  • addend_uncertainty (ndarray, default = None) – The uncertainty on the addend term. If None, we assume that the uncertainty is 0.

Returns:

  • result (ndarray) – The result of the addition operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.covariance(param_1: ndarray, param_2: ndarray) float[source]#

Compute the covariance for two parameters.

If the covariance cannot be computed, we default to 0.

Parameters:
  • param_1 (ndarray) – The first parameter.

  • param_2 (ndarray) – The second parameter.

Returns:

covar – The covariance.

Return type:

float

lezargus.library.uncertainty.divide(numerator: ndarray, denominator: ndarray, numerator_uncertainty: ndarray = None, denominator_uncertainty: ndarray = None) tuple[numpy.ndarray, numpy.ndarray][source]#

Divide two values and propagate uncertainties.

Note, the typical formula for the propagation of uncertainties for division can lead to issues because of division by zero. We rewrite the equations. This reformulation is based on Astropy’s reformulation. See Multiplication and Division for more information.

Parameters:
  • numerator (ndarray) – The numerator of the division; the top value.

  • denominator (ndarray) – The denominator of the division; the bottom value.

  • numerator_uncertainty (ndarray, default = None) – The uncertainty on the numerator term. If None, we assume that the uncertainty is 0.

  • denominator_uncertainty (ndarray, default = None) – The uncertainty on the denominator term. If None, we assume that the uncertainty is 0.

Returns:

  • result (ndarray) – The result of the division operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.exponentiate(base: ndarray, exponent: ndarray, base_uncertainty: ndarray, exponent_uncertainty: ndarray) tuple[numpy.ndarray, numpy.ndarray][source]#

Compute the exponent of two values and propagate uncertainties.

Parameters:
  • base (ndarray) – The base of the exponentiation; the lower value.

  • exponent (ndarray) – The exponent of the exponentiation; the upper value.

  • base_uncertainty (ndarray, default = None) – The uncertainty on the base term. If None, we assume that the uncertainty is 0.

  • exponent_uncertainty (ndarray, default = None) – The uncertainty on the exponent term. If None, we assume that the uncertainty is 0.

Returns:

  • result (ndarray) – The result of the exponentiation operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.integrate_discrete(variable: ndarray, integrand: ndarray, integrand_uncertainty: ndarray = None) tuple[float, float][source]#

Integrate discrete values and propagate the errors.

Parameters:
  • variable (ndarray) – The variable being integrated over.

  • integrand (ndarray) – The integrand function being integrated.

  • integrand_uncertainty (ndarray, default = None) – The uncertainty in the integrand. If None, we assume that the uncertainty is 0.

Returns:

  • result (float) – The result the integration.

  • uncertainty (float) – The uncertainty on the integration.

lezargus.library.uncertainty.logarithm(antilogarithm: ndarray, base: ndarray, antilogarithm_uncertainty: ndarray = None) tuple[numpy.ndarray, numpy.ndarray][source]#

Compute the logarithm of two values and propagate uncertainties.

Parameters:
  • antilogarithm (ndarray) – The inside value of the logarithm; what we are taking a logarithm of.

  • base (ndarray) – The logarithm base.

  • antilogarithm_uncertainty (ndarray, default = None) – The uncertainty in the anti-logarithm.

Returns:

  • result (ndarray) – The result of the exponentiation operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.multiply(multiplier: ndarray, multiplicand: ndarray, multiplier_uncertainty: ndarray = None, multiplicand_uncertainty: ndarray = None) tuple[numpy.ndarray, numpy.ndarray][source]#

Multiply two values and propagate uncertainties.

Note, the typical formula for the propagation of uncertainties for multiplication can lead to issues because of division by zero. We rewrite the equations. This reformulation is based on Astropy’s reformulation. See Multiplication and Division for more information.

Parameters:
  • multiplier (ndarray) – The “left”-side of the multiplication.

  • multiplicand (ndarray) – The “right”-side of the multiplication.

  • multiplier_uncertainty (ndarray, default = None) – The uncertainty on the multiplier term. If None, we assume that the uncertainty is 0.

  • multiplicand_uncertainty (ndarray, default = None) – The uncertainty on the multiplicand term. If None, we assume that the uncertainty is 0.

Returns:

  • result (ndarray) – The result of the multiplication operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.nan_weighted_mean(values: ndarray, uncertainties: ndarray = None, weights: ndarray = None) tuple[float, float][source]#

Calculate the no-NaN weighted mean and uncertainty.

This function is similar to library.uncertainty.weighted_mean(), but we do not include any non-finite values.

See Weighted Mean for more information.

Parameters:
  • values (ndarray) – The values which we will compute the weighted mean of.

  • uncertainties (ndarray, default = None) – The uncertainties in the values. If None, we default to no uncertainty.

  • weights (ndarray, default = None) – The weights for the given values for the weighted mean. If None, we assume equal weights.

Returns:

  • mean_value (float) – The calculated mean.

  • mean_uncertainty (float) – The calculated uncertainty in the mean.

lezargus.library.uncertainty.subtract(minuend: ndarray, subtrahend: ndarray, minuend_uncertainty: ndarray = None, subtrahend_uncertainty: ndarray = None) tuple[numpy.ndarray, numpy.ndarray][source]#

Subtract two values and propagate uncertainties.

Parameters:
  • minuend (ndarray) – The “left”-side of the subtraction.

  • subtrahend (ndarray) – The “right”-side of the subtraction.

  • minuend_uncertainty (ndarray, default = None) – The uncertainty on the minuend term. If None, we assume that the uncertainty is 0.

  • subtrahend_uncertainty (ndarray, default = None) – The uncertainty on the subtrahend term. If None, we assume that the uncertainty is 0.

Returns:

  • result (ndarray) – The result of the subtraction operation.

  • uncertainty (ndarray) – The propagated uncertainty.

lezargus.library.uncertainty.weighted_mean(values: ndarray, uncertainties: ndarray = None, weights: ndarray = None) tuple[float, float][source]#

Calculate a weighted mean, propagating uncertainties where needed.

This function calculates the weighted arithmetic mean of a group of samples and weights. If the weights are not provided, we default to equal weights and thus the ordinary arithmetic mean. If any value, uncertainty, or weight is NaN, the result is a NaN.

See Weighted Mean for more information.

Parameters:
  • values (ndarray) – The values which we will compute the weighted mean of.

  • uncertainties (ndarray, default = None) – The uncertainties in the values. If None, we default to no uncertainty.

  • weights (ndarray, default = None) – The weights for the given values for the weighted mean. If None, we assume equal weights.

Returns:

  • mean_value (float) – The calculated mean.

  • mean_uncertainty (float) – The calculated uncertainty in the mean.