pynkowski.stats.utils_st
This submodule contains utility functions that are useful for different statistics.
1'''This submodule contains utility functions that are useful for different statistics.''' 2import numpy as np 3 4def subsample_us(us, dus, iters=1_000): 5 """Return the thresholds where MFs (except for v0) are computed before averaging within the bins 'dus'. 6 7 Parameters 8 ---------- 9 us : np.array 10 The thresholds at which MFs have to be computed. 11 12 dus : np.array 13 The width of the bins associated to the thresholds 'us'. 14 15 iters : int, optional 16 the number of thresholds to consider within each bin. 17 18 Returns 19 ------- 20 us : np.array 21 The sequence of thresholds where MFs are computed before averaging within each bin, with shape (us.shape, iters). 22 23 """ 24 return np.vstack([np.linspace(u-du/2, u+du/2, iters) for u, du in zip(us, dus)]) 25 26def define_ubins(us, edges): 27 """Return the bins for the computation of statistics. They are returned as (centre of bin, width of bin). 28 29 Parameters 30 ---------- 31 us : np.array 32 The thresholds at which MFs have to be computed. 33 34 edges : bool, optional 35 If False (default), the given `us` is assumed to be an array of uniformly distributed thresholds, which are taken as the central values of the bins. 36 If True, input `us` is assumed to be a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. 37 In the latter case, the effective thresholds are the central value of the given bins. 38 39 Returns 40 ------- 41 us : np.array 42 The central value of the bins. 43 44 dus : np.array 45 The width of the bins. 46 47 """ 48 us = np.atleast_1d(us) 49 50 if edges: 51 dus = (us[1:]-us[:-1]) 52 us = (us[1:]+us[:-1])/2. 53 else: 54 if us.shape == (1,): 55 dus = np.array([0.1]) 56 else: 57 dus = (us[1]-us[0])*np.ones(us.shape[0]) 58 if not (np.isclose(us[1:]-us[:-1], dus[0])).all(): 59 raise ValueError('The threshold distribution is not uniform. Please set `edges=True`.') 60 return us, dus 61 62 63 64__all__ = ["subsample_us", "define_ubins"] 65 66__docformat__ = "numpy" 67 68
def
subsample_us(us, dus, iters=1000):
5def subsample_us(us, dus, iters=1_000): 6 """Return the thresholds where MFs (except for v0) are computed before averaging within the bins 'dus'. 7 8 Parameters 9 ---------- 10 us : np.array 11 The thresholds at which MFs have to be computed. 12 13 dus : np.array 14 The width of the bins associated to the thresholds 'us'. 15 16 iters : int, optional 17 the number of thresholds to consider within each bin. 18 19 Returns 20 ------- 21 us : np.array 22 The sequence of thresholds where MFs are computed before averaging within each bin, with shape (us.shape, iters). 23 24 """ 25 return np.vstack([np.linspace(u-du/2, u+du/2, iters) for u, du in zip(us, dus)])
Return the thresholds where MFs (except for v0) are computed before averaging within the bins 'dus'.
Parameters
- us (np.array): The thresholds at which MFs have to be computed.
- dus (np.array): The width of the bins associated to the thresholds 'us'.
- iters (int, optional): the number of thresholds to consider within each bin.
Returns
- us (np.array): The sequence of thresholds where MFs are computed before averaging within each bin, with shape (us.shape, iters).
def
define_ubins(us, edges):
27def define_ubins(us, edges): 28 """Return the bins for the computation of statistics. They are returned as (centre of bin, width of bin). 29 30 Parameters 31 ---------- 32 us : np.array 33 The thresholds at which MFs have to be computed. 34 35 edges : bool, optional 36 If False (default), the given `us` is assumed to be an array of uniformly distributed thresholds, which are taken as the central values of the bins. 37 If True, input `us` is assumed to be a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. 38 In the latter case, the effective thresholds are the central value of the given bins. 39 40 Returns 41 ------- 42 us : np.array 43 The central value of the bins. 44 45 dus : np.array 46 The width of the bins. 47 48 """ 49 us = np.atleast_1d(us) 50 51 if edges: 52 dus = (us[1:]-us[:-1]) 53 us = (us[1:]+us[:-1])/2. 54 else: 55 if us.shape == (1,): 56 dus = np.array([0.1]) 57 else: 58 dus = (us[1]-us[0])*np.ones(us.shape[0]) 59 if not (np.isclose(us[1:]-us[:-1], dus[0])).all(): 60 raise ValueError('The threshold distribution is not uniform. Please set `edges=True`.') 61 return us, dus
Return the bins for the computation of statistics. They are returned as (centre of bin, width of bin).
Parameters
- us (np.array): The thresholds at which MFs have to be computed.
- edges (bool, optional):
If False (default), the given
us
is assumed to be an array of uniformly distributed thresholds, which are taken as the central values of the bins. If True, inputus
is assumed to be a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. In the latter case, the effective thresholds are the central value of the given bins.
Returns
- us (np.array): The central value of the bins.
- dus (np.array): The width of the bins.