Utilities
- class multineas.util.Stats[source]
Bases:
objectAbstract class with useful routines
Attr. [HC]
- calc_correlations_from_covariances()[source]
Compute the standard deviations and corresponding correlation coefficients given a set of covariance matrices.
- Parameters:
Sigmas (numpy.ndarray) – Array of covariance matrices (Ngauss x Nvars x Nvars).
- Returns:
sigmas (numpy.ndarray) – Array of standard deviations (Ngauss x Nvars).
rhos (numpy.ndarray) – Array of correlation coefficients (Ngauss x Nvars * (Nvars-1) / 2).
Examples
>>> Sigmas = [ ... [[1. , 0.2, 0.6], ... [0.2, 4. , 1.8], ... [0.6, 1.8, 9. ]] ... ] >>> sigmas, rhos = Stats.calc_correlations_from_covariances(Sigmas) >>> print(sigmas) [1. 2. 3.] >>> print(rhos) [[0.1 0.2 0.3]]
Attr. [HC]
- calc_covariance_from_correlations(rhos)[source]
Compute covariance matrices from the standard deviations and correlations (rho).
- Parameters:
sigmas (numpy.ndarray) – Array of values of standard deviation for variables (Ngauss x Nvars).
rhos (numpy.ndarray) – Array with correlations (Ngauss x Nvars x (Nvars-1)/2).
- Returns:
Sigmas – Array with covariance matrices corresponding to these sigmas and rhos (Ngauss x Nvars x Nvars).
- Return type:
numpy.ndarray
Examples
>>> import numpy as np >>> sigmas = np.array([[1, 2, 3]]) >>> # rho_12, rho_13, rho_23 >>> rhos = np.array([[0.1, 0.2, 0.3]]) >>> S = Stats.calc_covariance_from_correlations(sigmas, rhos) >>> print(S) [[[1. 0.2 0.6] [0.2 4. 1.8] [0.6 1.8 9. ]]]
This is equivalent to:
>>> rho = rhos[0] >>> sigma = sigmas[0] >>> R = np.eye(3) >>> Stats.set_matrix_off_diagonal(R, rho) >>> M = np.zeros((3, 3)) >>> for i in range(3): ... for j in range(3): ... M[i,j] = R[i,j] * sigma[i] * sigma[j] >>> print(M) [[1. 0.2 0.6] [0.2 4. 1.8] [0.6 1.8 9. ]]
Sources
Based on: https://www.visiondummy.com/2014/04/geometric-interpretation-covariance-matrix/
Attr. [HC]
- calc_covariance_from_rotation(angles)[source]
Compute covariance matrices from the stds and the angles.
- Parameters:
sigmas (numpy.ndarray) – Array of values of standard deviation for variables (Ngauss x 3).
angles (numpy.ndarray) – Euler angles expressing the directions of the principal axes of the distribution (Ngauss x 3).
- Returns:
Sigmas – Array with covariance matrices corresponding to these sigmas and angles (Ngauss x 3 x 3). Attribution ———– [HC] This class was mostly developed by human intelligences.
- Return type:
numpy.ndarray
- flatten_symmetric_matrix()[source]
Given a symmetric matrix the routine returns the flatten version of the Matrix.
- Parameters:
M (numpy.ndarray) – Matrix (n x n).
- Returns:
F – Flatten array (nx(n+1)/2).
- Return type:
numpy.ndarray
Examples
>>> M = np.array([[1, 0.2], [0.2, 3]]) >>> F = Stats.flatten_symmetric_matrix(M) >>> print(F) [1. 0.2 3. ] Attribution ----------- [HC] This class was mostly developed by human intelligences.
- gen_index()[source]
Given a set of (normalized) probabilities, randomly generate an index n following the probabilities.
For instance if we have 3 events with probabilities 0.1, 0.7, 0.2, gen_index will generate a number in the set (0,1,2) having those probabilities, ie. 1 will have 70% of probability.
- Parameters:
probs (numpy.ndarray) – Probabilities (N), adimensional. NOTE: It should be normalized, ie. sum(probs)=1
- Returns:
n – Index in the set [0,1,2,… len(probs)-1].
- Return type:
int
Examples
>>> n = Stats.gen_index([0.1, 0.7, 0.2]) Attribution ----------- [HC] This class was mostly developed by human intelligences.
- phi = 1.618033988749895
- set_matrix_off_diagonal(off)[source]
Set a matrix with the terms of the off diagonal
- Parameters:
M (numpy.ndarray) – Matrix (n x n).
off (list or numpy.ndarray) – Terms off diagonal (n x (n-1) / 2).
- Returns:
Implicitly the matrix M has now the off diagonal terms.
- Return type:
Examples
>>> M = np.eye(3) >>> off = [0.1, 0.2, 0.3] >>> Stats.set_matrix_off_diagonal(M, off) >>> print(M) [[1. , 0.1, 0.2], [0.1, 1. , 0.3], [0.2, 0.3, 1. ]] Attribution ----------- [HC] This class was mostly developed by human intelligences.
- unflatten_symmetric_matrix(M)[source]
Given a flatten version of a matrix, returns the symmetric matrix.
- Parameters:
F (numpy.ndarray) – Flatten array (n x (n+1)/2).
M (numpy.ndarray) – Matrix where the result will be stored (n x n).
- Returns:
It return the results in matrix M.
- Return type:
Examples
>>> F = [1, 0.2, 3] >>> M = np.zeros((2, 2)) >>> Stats.unflatten_symmetric_matrix(F, M) >>> print(M) [[1. 0.2] [0.2 3. ]] Attribution ----------- [HC] This class was mostly developed by human intelligences.
- class multineas.util.Util[source]
Bases:
objectThis abstract class contains useful methods for the package.
Attr. [HC]
- DTIME = -1
- DUTIME = []
- TIME = 1769110385.9823818
- TIMESTART = 1769110385.9823813
- cos = <ufunc 'cos'>
- el_time(start=False)[source]
Compute the time elapsed since last call of this routine. The displayed time is preseneted in the more convenient unit, ns (nano seconds), us (micro seconds), ms (miliseconds), s (seconds), min (minutes), h (hours), d (days)
- Parameters:
verbose (int or bool, optional) – Show the time in screen (default 1).
start (int or bool, optional) – Compute time from program start (default 0).
- Returns:
dt (float) – Elapsed time in seconds.
dtu_unit (list) – List containing [time in units, unit string].
Examples
>>> Util.el_time() # basic usage (show output) >>> Util.el_time(verbose=0) # no output >>> Util.el_time(start=True) # measure elapsed time since program start >>> print(Util.DTIME, Util.DUTIME) # show values of elapsed time Attribution ----------- [HC] This class was mostly developed by human intelligences.
- error_msg(msg)[source]
Add a custom message msg to an error handle.
- Parameters:
error (Exception) – Error handle (eg. except ValueError as error).
msg (str) – Message to add to error. Attribution ———– [HC] This class was mostly developed by human intelligences.
- exp = <ufunc 'exp'>
- static f2u(x, s)[source]
Convert from a finite interval [0,s] to an unbound one [-inf,inf].
- Parameters:
x (float or array_like) – Value in the interval [0,s].
s (float) – Scale (upper limit of the interval).
- Returns:
u (float or array_like) – Unbound value.
Attribution
———–
[HC] This function was mostly developed by human intelligences.
- static get_data(filename)[source]
Get the full path of the filename which is one of the datafiles provided with the package.
- Parameters:
filename (str) – Name of the data file.
- Returns:
path – Full path to package datafile.
- Return type:
str
Examples
>>> from multineas.util import Util >>> path=Util.get_data("nea_extended.json.gz") Attribution ----------- [HC] This function was mostly developed by human intelligences.
- log = <ufunc 'log'>
- mantisa_exp()[source]
Calculate the mantisa and exponent of a number.
- Parameters:
x (float) – Number.
- Returns:
man (float) – Mantisa.
exp (float) – Exponent.
Examples
>>> m, e = Util.mantisa_exp(234.5) # returns m=2.345, e=2 >>> m, e = Util.mantisa_exp(-0.000023213) # return m=-2.3213, e=-5 Attribution ----------- [HC] This class was mostly developed by human intelligences.
- sin = <ufunc 'sin'>
- static t_if(p, s, f)[source]
Transform a set of parameters using a transformation function f and scales s.
This routine allows the conversion from a finite interval [0,s] to an unbound one [-inf,inf] (using f=Util.f2u) or vice versa (using f=Util.u2f).
- Parameters:
p (list or array_like) – Parameters to transform.
s (list or array_like) – Scales for each parameter. If s[i] > 0, the transformation is applied. If s[i] == 0, the parameter is unchanged.
f (function) – Transformation function (e.g. Util.f2u or Util.u2f).
- Returns:
tp – Transformed parameters.
- Return type:
list
Examples
>>> scales = [0, 0, 10, 10, 1] >>> minparams = [0.0, 0.0, 1, 1, 0.7] >>> uparams = Util.t_if(minparams, scales, Util.f2u) >>> print(uparams) [0.0, 0.0, -2.197224577336219, -2.197224577336219, 0.8472978603872034] Attribution ----------- [HC] This function was mostly developed by human intelligences.
- static u2f(t, s)[source]
Convert from an unbound interval [-inf,inf] to a finite one [0,s].
- Parameters:
t (float or array_like) – Unbound value.
s (float) – Scale (upper limit of the interval).
- Returns:
x (float or array_like) – Value in the interval [0,s].
Attribution
———–
[HC] This function was mostly developed by human intelligences.