teilab.utils.math_utils module¶
- teilab.utils.math_utils.assign_rank(arr: nptyping.types._ndarray.NDArray[Any, nptyping.types._object.Object], method: str = 'average') nptyping.types._ndarray.NDArray[Any, nptyping.types._number.Float] [source]¶
Assign rank to data, dealing with ties appropriately.
- Parameters
arr (NDArray[Any,Number]) – The array of values to be ranked
method (str, optional) – The method used to assign ranks to tied elements. Defaults to
"average"
.
The following ``method``s are available.
method
description
"average"
The average of the ranks that would have been assigned to all the tied values is assigned to each value.
"min"
The minimum of the ranks that would have been assigned to all the tied values is assigned to each value.
"max"
The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
"dense"
Like
"min"
, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements."ordinal"
All values are given a distinct rank, corresponding to the order that the values occur in
arr
- Returns
An array of size equal to the size of
arr
, containing rank scores.- Return type
NDArray[Any,float]
Examples
>>> import numpy as np >>> from teilab.utils import assign_rank >>> arr = np.asarray([0,2,3,2]) >>> assign_rank(arr, method="average") array([1. , 2.5, 4. , 2.5]) >>> assign_rank(arr, method="min") array([1, 2, 4, 2]) >>> assign_rank(arr, method="max") array([1, 3, 4, 3]) >>> assign_rank(arr, method="dense") array([1, 2, 3, 2]) >>> assign_rank(arr, method="ordinal") array([1, 2, 4, 3])
- teilab.utils.math_utils.tiecorrect(ranks: nptyping.types._ndarray.NDArray[Any, nptyping.types._object.Object]) float [source]¶
Tie correction factor for Mann-Whitney U and Kruskal-Wallis H tests.
- Parameters
ranks (NDArray[Any,Number]) – A 1-D sequence of ranks. Typically this will be the array returned by
assign_rank
.- Returns
Correction factor for
U
orH
.- Return type
float
Examples
>>> import numpy as np >>> from teilab.utils import tiecorrect, assign_rank >>> tiecorrect(np.asarray([0,2,3,2])) 0.9 >>> ranks = assign_rank(np.asarray([1,3,2,4,5,7,2,8,4]), method="average") >>> ranks array([1. , 4. , 2.5, 5.5, 7. , 8. , 2.5, 9. , 5.5]) >>> tiecorrect(ranks) 0.9833333333333333
- teilab.utils.math_utils.optimize_linear(X: nptyping.types._ndarray.NDArray[Any, nptyping.types._object.Object], Y: nptyping.types._ndarray.NDArray[Any, nptyping.types._object.Object]) Tuple[float, float, callable] [source]¶
Optimize linear function using least-squares method.
\[\begin{split}\begin{cases} a&=\frac{\displaystyle n\sum x_iy_i-\sum x_i\sum y_i}{\displaystyle n\sum x^2_i-\left( \sum x_i \right)^2}\\ b&=\frac{\displaystyle \sum x^2_i\sum y_i-\sum x_iy_i\sum x_i}{\displaystyle n\sum x^2_i-\left( \sum x_i \right)^2} \end{cases}\end{split}\]- Parameters
X (NDArray[Any,Number]) – explanatory variables.
Y (NDArray[Any,Number]) – objective variables.
- Returns
Optimal linear functions and their components.
- Return type
Tuple[float,float,callable]