tensiometer.utilities

This file contains some utilities that are used in the tensiometer package.

tensiometer.utilities.KL_decomposition(matrix_a, matrix_b)[source]

Computes the Karhunen–Loeve (KL) decomposition of the matrix A and B.

Notice that B has to be real, symmetric and positive.

The algorithm is taken from this link. The algorithm is NOT optimized for speed but for precision.

Parameters:
  • matrix_a – the first matrix.
  • matrix_b – the second matrix.
Returns:

the KL eigenvalues and the KL eigenvectors.

tensiometer.utilities.QR_inverse(matrix)[source]

Invert a matrix with the QR decomposition. This is much slower than standard inversion but has better accuracy for matrices with higher condition number.

Parameters:matrix – the input matrix.
Returns:the inverse of the matrix.
tensiometer.utilities.bernoulli_thin(chain, temperature=1, num_repeats=1)[source]

Function that thins a chain with a Bernoulli process.

Parameters:
  • chainMCSamples the input chain.
  • temperature – temperature of the Bernoulli process. If T=1 then this produces a unit weight chain.
  • num_repeats – number of repetitions of the Bernoulli process.
Returns:

a MCSamples chain with the reweighted chain.

tensiometer.utilities.clopper_pearson_binomial_trial(k, n, alpha=0.32)[source]

http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval alpha confidence intervals for a binomial distribution of k expected successes on n trials.

Parameters:
  • k – number of success.
  • n – total number of trials.
  • alpha – (optional) confidence level.
Returns:

lower and upper bound.

tensiometer.utilities.from_chi2_to_sigma(val, dofs, exact_threshold=6)[source]

Computes the effective number of standard deviations for a chi squared variable. This matches the probability computed from the chi squared variable to the number of standard deviations that an event with the same probability would have had in a Gaussian distribution as in Eq. (G1) of (Raveri and Hu 18).

\[n_{\sigma}^{\rm eff}(x, {\rm dofs}) \equiv \sqrt{2} {\rm Erf}^{-1}({\rm CDF}(\chi^2_{\rm dofs}(x)))\]

For very high statistical significant events this function switches from the direct formula to an accurate asyntotic expansion.

Parameters:
  • val – value of the chi2 variable
  • dofs – number of degrees of freedom of the chi2 variable
  • exact_threshold – (default 6) threshold of value/dofs to switch to the asyntotic formula.
Returns:

the effective number of standard deviations.

tensiometer.utilities.from_confidence_to_sigma(P)[source]

Transforms a probability to effective number of sigmas. This matches the input probability with the number of standard deviations that an event with the same probability would have had in a Gaussian distribution as in Eq. (G1) of (Raveri and Hu 18).

\[n_{\sigma}^{\rm eff}(P) \equiv \sqrt{2} {\rm Erf}^{-1}(P)\]
Parameters:P – the input probability.
Returns:the effective number of standard deviations.
tensiometer.utilities.from_sigma_to_confidence(nsigma)[source]

Gives the probability of an event at a given number of standard deviations in a Gaussian distribution.

Parameters:nsigma – the input number of standard deviations.
Returns:the probability to exceed the number of standard deviations.
tensiometer.utilities.get_separate_mcsamples(chain)[source]

Function that returns separate MCSamples for each sampler chain.

Parameters:chainMCSamples the input chain.
Returns:list of MCSamples with the separate chains.
tensiometer.utilities.make_list(elements)[source]

Checks if elements is a list. If yes returns elements without modifying it. If not creates and return a list with elements inside.

Parameters:elements – an element or a list of elements
Returns:a list containing elements.
tensiometer.utilities.random_samples_reshuffle(chain)[source]

Performs a coherent random reshuffle of the samples.

Parameters:chainMCSamples the input chain.
Returns:a MCSamples chain with the reshuffled chain.