Utils¶
This module provides utils that can be used in processes.
- class miscore.utils.WeibullDistribution(scale=None, shape=None, mean=None)[source]¶
Weibull distribution. Can be used as follows.
>>> wd = WeibullDistribution(mean=1, shape=2) >>> wd.mean 1.0 >>> wd.variance 0.27323954473516254 >>> wd(0.2) 1.431502705169328
See https://en.wikipedia.org/wiki/Weibull_distribution for further details.
- Parameters:
scale (
SupportsFloat|None) – The scale parameter.shape (
SupportsFloat|None) – The shape parameter.mean (
SupportsFloat|None) – The mean.
- Raises:
ValueError – WeibullDistribution should be initialized with one of the following combinations: ‘scale’ and ‘shape’; ‘mean’ and ‘shape’.
AssertionError – Scale should be positive.
AssertionError – Shape should be positive.
- property variance: float¶
Returns the variance of the distribution.
- Returns:
The variance of the distribution
- __call__(r)[source]¶
Draws a value from the distribution using the quantile (inverse cumulative distribution) function.
- Parameters:
r (
float) – A (random) number in [0, 1].- Return type:
- Returns:
The value drawn from the distribution
- Raises:
ValueError – Raised when r is not in [0, 1]”
- class miscore.utils.PiecewiseLinear(data, cumulative=False, weigh=False)[source]¶
A piecewise linear function. Can be used as follows:
>>> data = [(0, 3.0), (0.2, 4.0), (1.0, 5.5)] >>> pl = PiecewiseLinear(data) >>> pl(0.1) 3.5 >>> pl.array([0.1, 0.5, 1.0]) array([3.5 , 4.5625, 5.5 ])
Or alternatively, with a pandas Series:
>>> np_data = np.array(data, dtype=np.float64) >>> pd_data = pd.Series(np_data[:, 0], index=np_data[:, 1]) >>> pl = PiecewiseLinear(pd_data) >>> pl(0.1) 3.5
See https://en.wikipedia.org/wiki/Piecewise_linear_function for further details.
- Parameters:
data (
Sequence[Tuple[SupportsFloat,SupportsFloat]] |Series) – The data to construct the piecewise linear function with. Should be a sequence of sequences, or a pandas Series object, with the x values as Index. The latter sequences should each have two elements: an x-value and a y-value. Thus, an Nx2 matrix is valid input.cumulative (
bool) – Whether or not the y-values should be converted to their cumulative sums. Defaults to False.weigh (
bool) – Whether or not the y-values should be weighted with the first differences in x-values. Defaults to False.
- Raises:
AssertionError – PiecewiseLinear should be initialized with a sequence.
AssertionError – Each entry should be a sequence.
AssertionError – Each entry should have exactly two elements.
AssertionError – Each value should be (convertible to) a float.
AssertionError – x should be non-decreasing.
- Data¶
Type hinting for the input data that can be used to create a
PiecewiseLinearobject.alias of
Sequence[Tuple[SupportsFloat,SupportsFloat]] |Series
- reverse()[source]¶
Returns the reversed version of the PiecewiseLinear.
- Returns:
The reversed version of the PiecewiseLinear.
- inverse_integral(h)[source]¶
Inverse_integral solves for
tin \(\int^t_0 F(s) ds = h\) withF(s)a PiecewiseLinear.- Parameters:
h (
float) – Area under the curve.- Returns:
Returns upper bound of the integral.
- Raises:
AssertionError – Inverse integral is not possible for this PiecewiseLinear. Either the first data point is not defined for x=0 or it has negative y values.
AssertionError – h must be equal to or greater than 0.
- class miscore.utils.PiecewiseConstant(data, cumulative=False)[source]¶
A piecewise constant function. Can be used as follows:
>>> data = [(0, 3.0), (0.2, 4.0), (1.0, 5.5)] >>> pc = PiecewiseConstant(data) >>> pc(0.3) 4.0 >>> pc(-1) 3.0 >>> pc(2) 5.5
Or alternatively, with a pandas Series:
>>> np_data = np.array(data, dtype=np.float64) >>> pd_data = pd.Series(np_data[:, 0], index=np_data[:, 1]) >>> pc = PiecewiseConstant(pd_data) >>> pc(0.3) 4.0
See https://en.wikipedia.org/wiki/Step_function for further details.
- Parameters:
data (
Sequence[Tuple[SupportsFloat,SupportsFloat]] |Series) – The data to construct the piecewise constant function with. Should be a sequence of sequences, or a pandas Series object, with the x values as Index The latter sequences should each have two elements: an x-value and a y-value. Thus, an Nx2 matrix is valid input.cumulative (
bool) – Whether or not the y-values should be converted to their cumulative sums. Defaults to False.
- Raises:
AssertionError – PiecewiseConstant should be initialized with a sequence.
AssertionError – Each entry should be a sequence.
AssertionError – Each entry should have exactly two elements.
AssertionError – Each value should be (convertible to) a float.
AssertionError – x should be non-decreasing.
- Data¶
Type hinting for the input data that can be used to create a
PiecewiseConstantobject.alias of
Sequence[Tuple[SupportsFloat,SupportsFloat]] |Series
- class miscore.utils.CategoricalDist(data)[source]¶
A categorical distribution. Can be used as follows:
>>> data = [(3, "a"), (0.5, None), (1, 3)] >>> cd = CategoricalDist(data)
>>> # The sum of the weights is 3 + 0.5 + 1 = 4.5 >>> # 0.1 is smaller than 3 / 4.5, and therefore: >>> cd(0.1) 'a' >>> # 0.7 is in between 3/4.5 and (3+0.5)/4.5 and therefore: >>> cd(0.7) >>> # 1 is larger than (3+0.5)/4.5 and therefore: >>> cd(1) 3
Or alternatively, with a pandas Series:
>>> idx = [x[1] for x in data] >>> vals = [x[0] for x in data] >>> pd_data = pd.Series(vals, index=idx) >>> cd = CategoricalDist(pd_data) >>> cd(0.1) 'a'
See https://en.wikipedia.org/wiki/Categorical_distribution for further details.
- Parameters:
data (
Sequence[Tuple[SupportsFloat,Any]] |Series) – The data to construct the categorical distribution with. Should be a sequence of sequences or a pandas Series object, with the weights as Index The latter sequences should each have two elements: a weight and a value.- Raises:
AssertionError – CategoricalDist should be initialized with a sequence.
AssertionError – Each entry should be a sequence.
AssertionError – Each entry should have exactly two elements.
AssertionError – The first value of each entry should be (convertible to) a float.
AssertionError – Each weight should be non-negative.
- Data¶
Type hinting for the input data that can be used to create a
CategoricalDistobject.alias of
Sequence[Tuple[SupportsFloat,Any]] |Series
- class miscore.utils.InverseGeneralisedLogisticFunction(k, q, b, v, a=0.0, c=1.0)[source]¶
The inverse function of the generalised logistic function. Can be used as follows:
>>> func = InverseGeneralisedLogisticFunction(k=20, q=100, b=0.05, v=0.15) >>> func(np.array([0.1])) array([88.22693142]) >>> func(np.array([0.5])) array([98.15153043])
See also: https://en.wikipedia.org/wiki/Generalised_logistic_function.
- Parameters:
- Raises:
AssertionError – k should be positive.
AssertionError – b should be positive.
AssertionError – v should be positive.
AssertionError – q should be positive.